Freeciv-3.2
Loading...
Searching...
No Matches
dataio_raw.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14/*
15 * The DataIO module provides a system independent (endianess and
16 * sizeof(int) independent) way to write and read data. It supports
17 * multiple datas which are collected in a buffer. It provides
18 * recognition of error cases like "not enough space" or "not enough
19 * data".
20 */
21
22#ifdef HAVE_CONFIG_H
23#include <fc_config.h>
24#endif
25
26#include "fc_prehdrs.h"
27
28#include <limits.h>
29#include <math.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#ifdef FREECIV_HAVE_SYS_TYPES_H
36#include <sys/types.h>
37#endif
38#ifdef HAVE_SYS_SOCKET_H
39#include <sys/socket.h>
40#endif
41#ifdef HAVE_NETINET_IN_H
42#include <netinet/in.h>
43#endif
44#ifdef HAVE_ARPA_INET_H
45#include <arpa/inet.h>
46#endif
47
48/* utility */
49#include "bitvector.h"
50#include "capability.h"
51#include "log.h"
52#include "mem.h"
53#include "support.h"
54
55/* common */
56#include "events.h"
57#include "player.h"
58#include "requirements.h"
59#include "tech.h"
60#include "worklist.h"
61
62/* common/aicore */
63#include "cm.h"
64
65#include "dataio.h"
66
67static bool get_conv(char *dst, size_t ndst, const char *src,
68 size_t nsrc);
69
72
73/* Uncomment to make field range tests to asserts, fatal with -F */
74/* #define FIELD_RANGE_ASSERT */
75
76#if defined(FREECIV_TESTMATIC) && !defined(FIELD_RANGE_ASSERT)
77#define FIELD_RANGE_ASSERT
78#endif
79
80#ifdef FIELD_RANGE_ASSERT
81/* Do log_error() first, so we get its output even if
82 * fc_assert() is fatal.
83 * fc_assert() before the _action_ might fix
84 * what we should find out to fail. */
85#define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
86 if (_test_) { \
87 log_error(_format_, ## __VA_ARGS__); \
88 fc_assert(!(_test_)); \
89 _action_ \
90 }
91#else /* FIELD_RANGE_ASSERT */
92#define FIELD_RANGE_TEST(_test_, _action_, _format_, ...) \
93 if (_test_) { \
94 log_error(_format_, ## __VA_ARGS__); \
95 _action_ \
96 }
97#endif /* FIELD_RANGE_ASSERT */
98
99/**********************************************************************/
106
107/**********************************************************************/
111static bool get_conv(char *dst, size_t ndst, const char *src,
112 size_t nsrc)
113{
114 size_t len = nsrc; /* length to copy, not including null */
115 bool ret = TRUE;
116
117 if (ndst > 0 && len >= ndst) {
118 ret = FALSE;
119 len = ndst - 1;
120 }
121
122 memcpy(dst, src, len);
123 dst[len] = '\0';
124
125 return ret;
126}
127
128/**********************************************************************/
135
136/**********************************************************************/
139bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src,
140 size_t nsrc)
141{
142 return get_conv_callback(dst, ndst, src, nsrc);
143}
144
145/**********************************************************************/
148static bool enough_space(struct raw_data_out *dout, size_t size)
149{
150 if (dout->current + size > dout->dest_size) {
151 dout->too_short = TRUE;
152 return FALSE;
153 } else {
154 dout->used = MAX(dout->used, dout->current + size);
155 return TRUE;
156 }
157}
158
159/**********************************************************************/
162static bool enough_data(struct data_in *din, size_t size)
163{
164 return dio_input_remaining(din) >= size;
165}
166
167/**********************************************************************/
172 size_t dest_size)
173{
174 dout->dest = destination;
175 dout->dest_size = dest_size;
176 dout->current = 0;
177 dout->used = 0;
178 dout->too_short = FALSE;
179}
180
181/**********************************************************************/
185{
186 return dout->used;
187}
188
189/**********************************************************************/
194{
195 dout->current = 0;
196}
197
198/**********************************************************************/
202void dio_input_init(struct data_in *din, const void *src, size_t src_size)
203{
204 din->src = src;
205 din->src_size = src_size;
206 din->current = 0;
207}
208
209/**********************************************************************/
214{
215 din->current = 0;
216}
217
218/**********************************************************************/
222{
223 return din->src_size - din->current;
224}
225
226/**********************************************************************/
230{
231 switch (type) {
232 case DIOT_UINT8:
233 case DIOT_SINT8:
234 return 1;
235 case DIOT_UINT16:
236 case DIOT_SINT16:
237 return 2;
238 case DIOT_UINT32:
239 case DIOT_SINT32:
240 return 4;
241 case DIOT_LAST:
242 break;
243 }
244
245 fc_assert_msg(FALSE, "data_type %d not handled.", type);
246 return 0;
247}
248
249/**********************************************************************/
252bool dio_input_skip(struct data_in *din, size_t size)
253{
254 if (enough_data(din, size)) {
255 din->current += size;
256 return TRUE;
257 } else {
258 return FALSE;
259 }
260}
261
262/**********************************************************************/
265int dio_put_uint8_raw(struct raw_data_out *dout, int value)
266{
267 uint8_t x = value;
268
269 FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_1_byte);
270
271 FIELD_RANGE_TEST((int) x != value, ,
272 "Trying to put %d into 8 bits; "
273 "it will result %d at receiving side.",
274 value, (int) x);
275
276 if (enough_space(dout, 1)) {
277 memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 1);
278 dout->current++;
279 }
280
281 return 0;
282}
283
284/**********************************************************************/
287int dio_put_uint16_raw(struct raw_data_out *dout, int value)
288{
289 uint16_t x = htons(value);
290
292
293 FIELD_RANGE_TEST((int) ntohs(x) != value, ,
294 "Trying to put %d into 16 bits; "
295 "it will result %d at receiving side.",
296 value, (int) ntohs(x));
297
298 if (enough_space(dout, 2)) {
299 memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 2);
300 dout->current += 2;
301 }
302
303 return 0;
304}
305
306/**********************************************************************/
309int dio_put_uint32_raw(struct raw_data_out *dout, int value)
310{
311 uint32_t x = htonl(value);
312
314
315 FIELD_RANGE_TEST((int) ntohl(x) != value, ,
316 "Trying to put %d into 32 bits; "
317 "it will result %d at receiving side.",
318 value, (int) ntohl(x));
319
320 if (enough_space(dout, 4)) {
321 memcpy(ADD_TO_POINTER(dout->dest, dout->current), &x, 4);
322 dout->current += 4;
323 }
324
325 return 0;
326}
327
328/**********************************************************************/
331int dio_put_type_raw(struct raw_data_out *dout, enum data_type type, int value)
332{
333 switch (type) {
334 case DIOT_UINT8:
335 return dio_put_uint8_raw(dout, value);
336 case DIOT_UINT16:
337 return dio_put_uint16_raw(dout, value);
338 case DIOT_UINT32:
339 return dio_put_uint32_raw(dout, value);
340 case DIOT_SINT8:
341 return dio_put_sint8_raw(dout, value);
342 case DIOT_SINT16:
343 return dio_put_sint16_raw(dout, value);
344 case DIOT_SINT32:
345 return dio_put_sint32_raw(dout, value);
346 case DIOT_LAST:
347 break;
348 }
349
350 fc_assert_msg(FALSE, "data_type %d not handled.", type);
351
352 return -1;
353}
354
355/**********************************************************************/
358int dio_put_sint8_raw(struct raw_data_out *dout, int value)
359{
360 return dio_put_uint8_raw(dout, 0 <= value ? value : value + 0x100);
361}
362
363/**********************************************************************/
366int dio_put_sint16_raw(struct raw_data_out *dout, int value)
367{
368 return dio_put_uint16_raw(dout, 0 <= value ? value : value + 0x10000);
369}
370
371/**********************************************************************/
374int dio_put_sint32_raw(struct raw_data_out *dout, int value)
375{
376#if SIZEOF_INT == 4
377 return dio_put_uint32_raw(dout, value);
378#else
379 return dio_put_uint32_raw(dout, (0 <= value ? value : value + 0x100000000));
380#endif
381}
382
383/**********************************************************************/
386int dio_put_bool8_raw(struct raw_data_out *dout, bool value)
387{
388 FIELD_RANGE_TEST(value != TRUE && value != FALSE,
389 value = (value != FALSE);,
390 "Trying to put a non-boolean: %d", (int) value);
391
392 return dio_put_uint8_raw(dout, value ? 1 : 0);
393}
394
395/**********************************************************************/
398int dio_put_bool32_raw(struct raw_data_out *dout, bool value)
399{
400 FIELD_RANGE_TEST(value != TRUE && value != FALSE,
401 value = (value != FALSE);,
402 "Trying to put a non-boolean: %d",
403 (int) value);
404
405 return dio_put_uint32_raw(dout, value ? 1 : 0);
406}
407
408/**********************************************************************/
412int dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
413{
414 uint32_t v = value * float_factor;
415
416 FIELD_RANGE_TEST(fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
417 "Trying to put %f with factor %d in 32 bits; "
418 "it will result %f at receiving side, having error of %f units.",
419 value, float_factor, (float) v / float_factor,
420 fabsf((float) v / float_factor - value) * float_factor);
421
422 return dio_put_uint32_raw(dout, v);
423}
424
425/**********************************************************************/
429int dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
430{
431 int32_t v = value * float_factor;
432
433 FIELD_RANGE_TEST(fabsf((float) v / float_factor - value) > 1.1 / float_factor, ,
434 "Trying to put %f with factor %d in 32 bits; "
435 "it will result %f at receiving side, having error of %f units.",
436 value, float_factor, (float) v / float_factor,
437 fabsf((float) v / float_factor - value) * float_factor);
438
439 return dio_put_sint32_raw(dout, v);
440}
441
442/**********************************************************************/
448{
449 size_t count;
450 int e = 0;
451
452 for (count = 0; values[count] != stop_value; count++) {
453 /* nothing */
454 }
455
456 if (enough_space(dout, 1 + count)) {
457 size_t i;
458
459 e |= dio_put_uint8_raw(dout, count);
460
461 for (i = 0; i < count; i++) {
462 e |= dio_put_uint8_raw(dout, values[i]);
463 }
464 }
465
466 return e;
467}
468
469/**********************************************************************/
475{
476 size_t count;
477 int e = 0;
478
479 for (count = 0; values[count] != stop_value; count++) {
480 /* nothing */
481 }
482
483 if (enough_space(dout, 1 + 2 * count)) {
484 size_t i;
485
486 e |= dio_put_uint8_raw(dout, count);
487
488 for (i = 0; i < count; i++) {
489 e |= dio_put_uint16_raw(dout, values[i]);
490 }
491 }
492
493 return e;
494}
495
496/**********************************************************************/
499int dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
500{
501 if (enough_space(dout, size)) {
502 memcpy(ADD_TO_POINTER(dout->dest, dout->current), value, size);
503 dout->current += size;
504 }
505
506 return 0;
507}
508
509/**********************************************************************/
512int dio_put_string_raw(struct raw_data_out *dout, const char *value)
513{
514 if (put_conv_callback) {
515 size_t length;
516 char *buffer;
517 int e = 0;
518
519 if ((buffer = (*put_conv_callback) (value, &length))) {
520 e = dio_put_memory_raw(dout, buffer, length + 1);
521 free(buffer);
522 }
523
524 return e;
525 } else {
526 return dio_put_memory_raw(dout, value, strlen(value) + 1);
527 }
528}
529
530/**********************************************************************/
534 const struct cm_parameter *param)
535{
536 int i;
537 int e = 0;
538
539 for (i = 0; i < O_LAST; i++) {
541 }
542
543 e |= dio_put_bool8_raw(dout, param->max_growth);
547
548 for (i = 0; i < O_LAST; i++) {
549 e |= dio_put_uint16_raw(dout, param->factor[i]);
550 }
551
553
554 return e;
555}
556
557/**********************************************************************/
561 const struct unit_order *order)
562{
563 int e = 0;
564
565 e |= dio_put_uint8_raw(dout, order->order);
566 e |= dio_put_uint8_raw(dout, order->activity);
567 e |= dio_put_sint32_raw(dout, order->target);
568 e |= dio_put_sint16_raw(dout, order->sub_target);
569 e |= dio_put_uint8_raw(dout, order->action);
570 e |= dio_put_sint8_raw(dout, order->dir);
571
572 return e;
573}
574
575/**********************************************************************/
580{
581 int i, length = worklist_length(pwl);
582 int e = 0;
583
584 e |= dio_put_uint8_raw(dout, length);
585 for (i = 0; i < length; i++) {
586 const struct universal *pcp = &(pwl->entries[i]);
587
588 e |= dio_put_uint8_raw(dout, pcp->kind);
590 }
591
592 return e;
593}
594
595/**********************************************************************/
598bool dio_get_uint8_raw(struct data_in *din, int *dest)
599{
600 uint8_t x;
601
602 FC_STATIC_ASSERT(sizeof(x) == 1, uint8_not_byte);
603
604 if (!enough_data(din, 1)) {
605 log_packet("Packet too short to read 1 byte");
606
607 return FALSE;
608 }
609
610 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 1);
611 *dest = x;
612 din->current++;
613 return TRUE;
614}
615
616/**********************************************************************/
619bool dio_get_uint16_raw(struct data_in *din, int *dest)
620{
621 uint16_t x;
622
624
625 if (!enough_data(din, 2)) {
626 log_packet("Packet too short to read 2 bytes");
627
628 return FALSE;
629 }
630
631 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 2);
632 *dest = ntohs(x);
633 din->current += 2;
634 return TRUE;
635}
636
637/**********************************************************************/
640bool dio_get_uint32_raw(struct data_in *din, int *dest)
641{
642 uint32_t x;
643
645
646 if (!enough_data(din, 4)) {
647 log_packet("Packet too short to read 4 bytes");
648
649 return FALSE;
650 }
651
652 memcpy(&x, ADD_TO_POINTER(din->src, din->current), 4);
653 *dest = ntohl(x);
654 din->current += 4;
655 return TRUE;
656}
657
658/**********************************************************************/
661bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
662{
663 switch (type) {
664 case DIOT_UINT8:
665 return dio_get_uint8_raw(din, dest);
666 case DIOT_UINT16:
667 return dio_get_uint16_raw(din, dest);
668 case DIOT_UINT32:
669 return dio_get_uint32_raw(din, dest);
670 case DIOT_SINT8:
671 return dio_get_sint8_raw(din, dest);
672 case DIOT_SINT16:
673 return dio_get_sint16_raw(din, dest);
674 case DIOT_SINT32:
675 return dio_get_sint32_raw(din, dest);
676 case DIOT_LAST:
677 break;
678 }
679
680 fc_assert_msg(FALSE, "data_type %d not handled.", type);
681 return FALSE;
682}
683
684/**********************************************************************/
687bool dio_get_bool8_raw(struct data_in *din, bool *dest)
688{
689 int ival;
690
691 if (!dio_get_uint8_raw(din, &ival)) {
692 return FALSE;
693 }
694
695 if (ival != 0 && ival != 1) {
696 log_packet("Got a bad boolean: %d", ival);
697 return FALSE;
698 }
699
700 *dest = (ival != 0);
701 return TRUE;
702}
703
704/**********************************************************************/
707bool dio_get_bool32_raw(struct data_in *din, bool * dest)
708{
709 int ival;
710
711 if (!dio_get_uint32_raw(din, &ival)) {
712 return FALSE;
713 }
714
715 if (ival != 0 && ival != 1) {
716 log_packet("Got a bad boolean: %d", ival);
717 return FALSE;
718 }
719
720 *dest = (ival != 0);
721 return TRUE;
722}
723
724/**********************************************************************/
728bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
729{
730 int ival;
731
732 if (!dio_get_uint32_raw(din, &ival)) {
733 return FALSE;
734 }
735
736 *dest = (float) ival / float_factor;
737 return TRUE;
738}
739
740/**********************************************************************/
744bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
745{
746 int ival;
747
748 if (!dio_get_sint32_raw(din, &ival)) {
749 return FALSE;
750 }
751
752 *dest = (float) ival / float_factor;
753 return TRUE;
754}
755
756/**********************************************************************/
759bool dio_get_sint8_raw(struct data_in *din, int *dest)
760{
761 int tmp;
762
763 if (!dio_get_uint8_raw(din, &tmp)) {
764 return FALSE;
765 }
766
767 if (tmp > 0x7f) {
768 tmp -= 0x100;
769 }
770 *dest = tmp;
771 return TRUE;
772}
773
774/**********************************************************************/
777bool dio_get_sint16_raw(struct data_in *din, int *dest)
778{
779 int tmp;
780
781 if (!dio_get_uint16_raw(din, &tmp)) {
782 return FALSE;
783 }
784
785 if (tmp > 0x7fff) {
786 tmp -= 0x10000;
787 }
788 *dest = tmp;
789 return TRUE;
790}
791
792/**********************************************************************/
795bool dio_get_sint32_raw(struct data_in *din, int *dest)
796{
797 int tmp;
798
799 if (!dio_get_uint32_raw(din, &tmp)) {
800 return FALSE;
801 }
802
803#if SIZEOF_INT != 4
804 if (tmp > 0x7fffffff) {
805 tmp -= 0x100000000;
806 }
807#endif
808
809 *dest = tmp;
810 return TRUE;
811}
812
813/**********************************************************************/
816bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
817{
818 if (!enough_data(din, dest_size)) {
819 log_packet("Got too short memory");
820 return FALSE;
821 }
822
823 memcpy(dest, ADD_TO_POINTER(din->src, din->current), dest_size);
824 din->current += dest_size;
825 return TRUE;
826}
827
828/**********************************************************************/
831bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
832{
833 char *c;
834 size_t offset, remaining;
835
837
838 if (!enough_data(din, 1)) {
839 log_packet("Got a bad string");
840 return FALSE;
841 }
842
843 remaining = dio_input_remaining(din);
844 c = ADD_TO_POINTER(din->src, din->current);
845
846 /* avoid using strlen (or strcpy) on an (unsigned char*) --dwp */
847 for (offset = 0; offset < remaining && c[offset] != '\0'; offset++) {
848 /* nothing */
849 }
850
851 if (offset >= remaining) {
852 log_packet("Got a too short string");
853 return FALSE;
854 }
855
856 if (!(*get_conv_callback) (dest, max_dest_size, c, offset)) {
857 log_packet("Got a bad encoded string");
858 return FALSE;
859 }
860
861 din->current += offset + 1;
862 return TRUE;
863}
864
865/**********************************************************************/
869 struct cm_parameter *param)
870{
871 int i;
872
873 for (i = 0; i < O_LAST; i++) {
874 if (!dio_get_sint16_raw(din, &param->minimal_surplus[i])) {
875 log_packet("Got a bad cm_parameter");
876 return FALSE;
877 }
878 }
879
880 if (!dio_get_bool8_raw(din, &param->max_growth)
884 log_packet("Got a bad cm_parameter");
885 return FALSE;
886 }
887
888 for (i = 0; i < O_LAST; i++) {
889 if (!dio_get_uint16_raw(din, &param->factor[i])) {
890 log_packet("Got a bad cm_parameter");
891 return FALSE;
892 }
893 }
894
895 if (!dio_get_uint16_raw(din, &param->happy_factor)) {
896 log_packet("Got a bad cm_parameter");
897 return FALSE;
898 }
899
900 return TRUE;
901}
902
903/**********************************************************************/
906bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
907{
908 /* These fields are enums */
909 int iorder, iactivity, idir;
910
913 || !dio_get_sint32_raw(din, &order->target)
914 || !dio_get_sint16_raw(din, &order->sub_target)
915 || !dio_get_uint8_raw(din, &order->action)
916 || !dio_get_sint8_raw(din, &idir)) {
917 log_packet("Got a bad unit_order");
918 return FALSE;
919 }
920
921 order->order = iorder;
922 order->activity = iactivity;
923 order->dir = idir;
924
925 return TRUE;
926}
927
928/**********************************************************************/
933{
934 int i, length;
935
937
938 if (!dio_get_uint8_raw(din, &length)) {
939 log_packet("Got a bad worklist");
940 return FALSE;
941 }
942
943 for (i = 0; i < length; i++) {
944 int identifier;
945 int kind;
946 struct universal univ;
947
949 || !dio_get_uint8_raw(din, &identifier)) {
950 log_packet("Got a too short worklist");
951 return FALSE;
952 }
953
954 /*
955 * FIXME: the value returned by universal_by_number() should be checked!
956 */
957 univ = universal_by_number(kind, identifier);
958 worklist_append(pwl, &univ);
959 }
960
961 return TRUE;
962}
963
964/**********************************************************************/
968bool dio_get_uint8_vec8_raw(struct data_in *din, int **values, int stop_value)
969{
970 int count, inx;
971 int *vec;
972
973 if (!dio_get_uint8_raw(din, &count)) {
974 return FALSE;
975 }
976
977 vec = fc_calloc(count + 1, sizeof(*vec));
978 for (inx = 0; inx < count; inx++) {
979 if (!dio_get_uint8_raw(din, vec + inx)) {
980 free (vec);
981 return FALSE;
982 }
983 }
984 vec[inx] = stop_value;
985 *values = vec;
986
987 return TRUE;
988}
989
990/**********************************************************************/
993bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
994{
995 int count, inx;
996 int *vec;
997
998 if (!dio_get_uint8_raw(din, &count)) {
999 return FALSE;
1000 }
1001
1002 vec = fc_calloc(count + 1, sizeof(*vec));
1003 for (inx = 0; inx < count; inx++) {
1004 if (!dio_get_uint16_raw(din, vec + inx)) {
1005 free (vec);
1006 return FALSE;
1007 }
1008 }
1009 vec[inx] = stop_value;
1010 *values = vec;
1011
1012 return TRUE;
1013}
1014
1015/**********************************************************************/
1019 struct act_prob *aprob)
1020{
1021 int min, max;
1022
1023 if (!dio_get_uint8_raw(din, &min)
1024 || !dio_get_uint8_raw(din, &max)) {
1025 log_packet("Got a bad action probability");
1026 return FALSE;
1027 }
1028
1029 aprob->min = min;
1030 aprob->max = max;
1031
1032 return TRUE;
1033}
1034
1035/**********************************************************************/
1039 const struct act_prob *aprob)
1040{
1041 int e = 0;
1042
1043 e |= dio_put_uint8_raw(dout, aprob->min);
1044 e |= dio_put_uint8_raw(dout, aprob->max);
1045
1046 return e;
1047}
1048
1049/**********************************************************************/
1053{
1054 int type, range, value;
1055 bool survives, present, quiet;
1056
1057 if (!dio_get_uint8_raw(din, &type)
1060 || !dio_get_bool8_raw(din, &survives)
1061 || !dio_get_bool8_raw(din, &present)
1062 || !dio_get_bool8_raw(din, &quiet)) {
1063 log_packet("Got a bad requirement");
1064 return FALSE;
1065 }
1066
1067 /*
1068 * FIXME: the value returned by req_from_values() should be checked!
1069 */
1070 *preq = req_from_values(type, range, survives, present, quiet, value);
1071
1072 return TRUE;
1073}
1074
1075/**********************************************************************/
1079 const struct requirement *preq)
1080{
1081 int type, range, value;
1082 bool survives, present, quiet;
1083 int e = 0;
1084
1085 req_get_values(preq, &type, &range, &survives, &present, &quiet, &value);
1086
1090 e |= dio_put_bool8_raw(dout, survives);
1091 e |= dio_put_bool8_raw(dout, present);
1092 e |= dio_put_bool8_raw(dout, quiet);
1093
1094 return e;
1095}
1096
1097/**********************************************************************/
1101{
1102 struct plocation *out = fc_malloc(sizeof(*out));
1103
1104 out->kind = PADR_FIELD;
1105 out->name = name;
1106 out->sub_location = NULL;
1107
1108 return out;
1109}
1110
1111/**********************************************************************/
1115{
1116 struct plocation *out = fc_malloc(sizeof(*out));
1117
1118 out->kind = PADR_ELEMENT;
1119 out->number = number;
1120 out->sub_location = NULL;
1121
1122 return out;
1123}
1124
1125/**********************************************************************/
1130const char *plocation_name(const struct plocation *loc)
1131{
1132 static char locname[10];
1133
1134 if (loc == NULL) {
1135 return "No location";
1136 }
1137
1138 switch (loc->kind) {
1139 case PADR_FIELD:
1140 return loc->name;
1141 case PADR_ELEMENT:
1142 fc_snprintf(locname, sizeof(locname), SIZE_T_PRINTF, loc->number);
1143 return locname;
1144 }
1145
1146 return "Illegal location";
1147}
char * incite_cost
Definition comments.c:75
bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
bool dio_get_sint8_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:759
int dio_put_uint8_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
Definition dataio_raw.c:447
int dio_put_cm_parameter_raw(struct raw_data_out *dout, const struct cm_parameter *param)
Definition dataio_raw.c:533
int dio_put_type_raw(struct raw_data_out *dout, enum data_type type, int value)
Definition dataio_raw.c:331
int dio_put_action_probability_raw(struct raw_data_out *dout, const struct act_prob *aprob)
int dio_put_uint8_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:265
static bool enough_space(struct raw_data_out *dout, size_t size)
Definition dataio_raw.c:148
struct plocation * plocation_field_new(char *name)
bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
Definition dataio_raw.c:932
bool dio_get_action_probability_raw(struct data_in *din, struct act_prob *aprob)
int dio_put_uint32_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:309
void dio_output_init(struct raw_data_out *dout, void *destination, size_t dest_size)
Definition dataio_raw.c:171
void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun)
Definition dataio_raw.c:131
bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.c:139
bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
Definition dataio_raw.c:744
size_t dio_input_remaining(struct data_in *din)
Definition dataio_raw.c:221
bool dio_get_uint32_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:640
int dio_put_string_raw(struct raw_data_out *dout, const char *value)
Definition dataio_raw.c:512
bool dio_get_sint16_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:777
bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
Definition dataio_raw.c:906
bool dio_get_sint32_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:795
int dio_put_bool8_raw(struct raw_data_out *dout, bool value)
Definition dataio_raw.c:386
bool dio_get_cm_parameter_raw(struct data_in *din, struct cm_parameter *param)
Definition dataio_raw.c:868
int dio_put_sint8_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:358
void dio_input_rewind(struct data_in *din)
Definition dataio_raw.c:213
int dio_put_requirement_raw(struct raw_data_out *dout, const struct requirement *preq)
bool dio_get_uint8_vec8_raw(struct data_in *din, int **values, int stop_value)
Definition dataio_raw.c:968
bool dio_get_uint16_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:619
int dio_put_sint32_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:374
bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
Definition dataio_raw.c:816
static bool get_conv(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.c:111
bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
Definition dataio_raw.c:661
int dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
Definition dataio_raw.c:499
bool dio_get_bool32_raw(struct data_in *din, bool *dest)
Definition dataio_raw.c:707
bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
Definition dataio_raw.c:728
int dio_put_bool32_raw(struct raw_data_out *dout, bool value)
Definition dataio_raw.c:398
bool dio_get_uint8_raw(struct data_in *din, int *dest)
Definition dataio_raw.c:598
static bool enough_data(struct data_in *din, size_t size)
Definition dataio_raw.c:162
static DIO_GET_CONV_FUN get_conv_callback
Definition dataio_raw.c:71
int dio_put_uint16_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:287
bool dio_input_skip(struct data_in *din, size_t size)
Definition dataio_raw.c:252
bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
Definition dataio_raw.c:993
size_t data_type_size(enum data_type type)
Definition dataio_raw.c:229
struct plocation * plocation_elem_new(int number)
bool dio_get_bool8_raw(struct data_in *din, bool *dest)
Definition dataio_raw.c:687
int dio_put_unit_order_raw(struct raw_data_out *dout, const struct unit_order *order)
Definition dataio_raw.c:560
int dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
Definition dataio_raw.c:412
size_t dio_output_used(struct raw_data_out *dout)
Definition dataio_raw.c:184
int dio_put_uint16_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
Definition dataio_raw.c:474
void dio_input_init(struct data_in *din, const void *src, size_t src_size)
Definition dataio_raw.c:202
int dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
Definition dataio_raw.c:429
#define FIELD_RANGE_TEST(_test_, _action_, _format_,...)
Definition dataio_raw.c:92
static DIO_PUT_CONV_FUN put_conv_callback
Definition dataio_raw.c:70
void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun)
Definition dataio_raw.c:102
int dio_put_sint16_raw(struct raw_data_out *dout, int value)
Definition dataio_raw.c:366
bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
Definition dataio_raw.c:831
const char * plocation_name(const struct plocation *loc)
void dio_output_rewind(struct raw_data_out *dout)
Definition dataio_raw.c:193
int dio_put_worklist_raw(struct raw_data_out *dout, const struct worklist *pwl)
Definition dataio_raw.c:579
char *(* DIO_PUT_CONV_FUN)(const char *src, size_t *length)
Definition dataio_raw.h:91
@ PADR_FIELD
Definition dataio_raw.h:57
@ PADR_ELEMENT
Definition dataio_raw.h:59
bool(* DIO_GET_CONV_FUN)(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition dataio_raw.h:94
data_type
Definition dataio_raw.h:43
@ DIOT_UINT8
Definition dataio_raw.h:44
@ DIOT_UINT32
Definition dataio_raw.h:46
@ DIOT_UINT16
Definition dataio_raw.h:45
@ DIOT_LAST
Definition dataio_raw.h:51
@ DIOT_SINT32
Definition dataio_raw.h:49
@ DIOT_SINT8
Definition dataio_raw.h:47
@ DIOT_SINT16
Definition dataio_raw.h:48
@ O_LAST
Definition fc_types.h:101
struct tile * loc
Definition citydlg.c:227
GType type
Definition repodlgs.c:1313
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define log_packet
Definition log.h:137
#define fc_assert(condition)
Definition log.h:176
#define FC_STATIC_ASSERT(cond, tag)
Definition log.h:235
#define fc_calloc(n, esz)
Definition mem.h:38
#define fc_malloc(sz)
Definition mem.h:34
int len
Definition packhand.c:127
void req_get_values(const struct requirement *req, int *type, int *range, bool *survives, bool *present, bool *quiet, int *value)
struct requirement req_from_values(int type, int range, bool survives, bool present, bool quiet, int value)
struct universal universal_by_number(const enum universals_n kind, const int value)
int universal_number(const struct universal *source)
#define ADD_TO_POINTER(p, n)
Definition shared.h:86
#define MAX(x, y)
Definition shared.h:54
size_t size
Definition specvec.h:72
struct sprite int x
Definition sprite_g.h:31
bool allow_disorder
Definition cm.h:44
int factor[O_LAST]
Definition cm.h:47
bool max_growth
Definition cm.h:42
bool allow_specialists
Definition cm.h:45
bool require_happy
Definition cm.h:43
int minimal_surplus[O_LAST]
Definition cm.h:41
int happy_factor
Definition cm.h:48
size_t number
Definition dataio_raw.h:70
enum unit_activity activity
Definition unit.h:94
enum unit_orders order
Definition unit.h:93
int action
Definition unit.h:100
enum direction8 dir
Definition unit.h:102
int target
Definition unit.h:97
int sub_target
Definition unit.h:98
enum universals_n kind
Definition fc_types.h:902
universals_u value
Definition fc_types.h:901
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
void worklist_init(struct worklist *pwl)
Definition worklist.c:38
bool worklist_append(struct worklist *pwl, const struct universal *prod)
Definition worklist.c:147
int worklist_length(const struct worklist *pwl)
Definition worklist.c:57