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)
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 ***********************************************************************/
18 #endif /* __cplusplus */
20 #include "bitvector.h"
21 #include "support.h" /* bool type */
29 size_t src_size
, current
;
34 size_t dest_size
, used
, current
;
35 bool too_short
; /* set to 1 if try to read past end */
38 /* Used for dio_<put|get>_type() methods.
39 * NB: we only support integer handling currently. */
51 /* network string conversion */
52 typedef char *(*DIO_PUT_CONV_FUN
) (const char *src
, size_t *length
);
53 void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun
);
55 typedef bool(*DIO_GET_CONV_FUN
) (char *dst
, size_t ndst
,
56 const char *src
, size_t nsrc
);
57 void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun
);
59 /* General functions */
60 void dio_output_init(struct data_out
*dout
, void *destination
,
62 void dio_output_rewind(struct data_out
*dout
);
63 size_t dio_output_used(struct data_out
*dout
);
65 void dio_input_init(struct data_in
*dout
, const void *src
, size_t src_size
);
66 void dio_input_rewind(struct data_in
*din
);
67 size_t dio_input_remaining(struct data_in
*din
);
68 bool dio_input_skip(struct data_in
*din
, size_t size
);
70 size_t data_type_size(enum data_type type
);
73 bool dio_get_type(struct data_in
*din
, enum data_type type
, int *dest
)
74 fc__attribute((nonnull (3)));
76 bool dio_get_uint8(struct data_in
*din
, int *dest
)
77 fc__attribute((nonnull (2)));
78 bool dio_get_uint16(struct data_in
*din
, int *dest
)
79 fc__attribute((nonnull (2)));
80 bool dio_get_uint32(struct data_in
*din
, int *dest
)
81 fc__attribute((nonnull (2)));
83 bool dio_get_sint8(struct data_in
*din
, int *dest
)
84 fc__attribute((nonnull (2)));
85 bool dio_get_sint16(struct data_in
*din
, int *dest
)
86 fc__attribute((nonnull (2)));
87 bool dio_get_sint32(struct data_in
*din
, int *dest
)
88 fc__attribute((nonnull (2)));
90 bool dio_get_bool8(struct data_in
*din
, bool *dest
)
91 fc__attribute((nonnull (2)));
92 bool dio_get_bool32(struct data_in
*din
, bool *dest
)
93 fc__attribute((nonnull (2)));
94 bool dio_get_ufloat(struct data_in
*din
, float *dest
, int float_factor
)
95 fc__attribute((nonnull (2)));
96 bool dio_get_sfloat(struct data_in
*din
, float *dest
, int float_factor
)
97 fc__attribute((nonnull (2)));
98 bool dio_get_memory(struct data_in
*din
, void *dest
, size_t dest_size
)
99 fc__attribute((nonnull (2)));
100 bool dio_get_string(struct data_in
*din
, char *dest
, size_t max_dest_size
)
101 fc__attribute((nonnull (2)));
102 bool dio_get_tech_list(struct data_in
*din
, int *dest
)
103 fc__attribute((nonnull (2)));
104 bool dio_get_unit_list(struct data_in
*din
, int *dest
)
105 fc__attribute((nonnull (2)));
106 bool dio_get_building_list(struct data_in
*din
, int *dest
)
107 fc__attribute((nonnull (2)));
108 bool dio_get_worklist(struct data_in
*din
, struct worklist
*pwl
)
109 fc__attribute((nonnull (2)));
110 bool dio_get_requirement(struct data_in
*din
, struct requirement
*preq
)
111 fc__attribute((nonnull (2)));
112 bool dio_get_action_probability(struct data_in
*din
,
113 struct act_prob
*aprob
)
114 fc__attribute((nonnull (2)));
116 bool dio_get_uint8_vec8(struct data_in
*din
, int **values
, int stop_value
)
117 fc__attribute((nonnull (2)));
118 bool dio_get_uint16_vec8(struct data_in
*din
, int **values
, int stop_value
)
119 fc__attribute((nonnull (2)));
121 /* Should be a function but we need some macro magic. */
122 #define DIO_BV_GET(pdin, bv) \
123 dio_get_memory((pdin), (bv).vec, sizeof((bv).vec))
125 #define DIO_GET(f, d, k, ...) dio_get_##f(d, ## __VA_ARGS__)
128 void dio_put_type(struct data_out
*dout
, enum data_type type
, int value
);
130 void dio_put_uint8(struct data_out
*dout
, int value
);
131 void dio_put_uint16(struct data_out
*dout
, int value
);
132 void dio_put_uint32(struct data_out
*dout
, int value
);
134 void dio_put_sint8(struct data_out
*dout
, int value
);
135 void dio_put_sint16(struct data_out
*dout
, int value
);
136 void dio_put_sint32(struct data_out
*dout
, int value
);
138 void dio_put_bool8(struct data_out
*dout
, bool value
);
139 void dio_put_bool32(struct data_out
*dout
, bool value
);
140 void dio_put_ufloat(struct data_out
*dout
, float value
, int float_factor
);
141 void dio_put_sfloat(struct data_out
*dout
, float value
, int float_factor
);
143 void dio_put_memory(struct data_out
*dout
, const void *value
, size_t size
);
144 void dio_put_string(struct data_out
*dout
, const char *value
);
145 void dio_put_city_map(struct data_out
*dout
, const char *value
);
146 void dio_put_tech_list(struct data_out
*dout
, const int *value
);
147 void dio_put_unit_list(struct data_out
*dout
, const int *value
);
148 void dio_put_building_list(struct data_out
*dout
, const int *value
);
149 void dio_put_worklist(struct data_out
*dout
, const struct worklist
*pwl
);
150 void dio_put_requirement(struct data_out
*dout
, const struct requirement
*preq
);
151 void dio_put_action_probability(struct data_out
*dout
,
152 const struct act_prob
*aprob
);
154 void dio_put_uint8_vec8(struct data_out
*dout
, int *values
, int stop_value
);
155 void dio_put_uint16_vec8(struct data_out
*dout
, int *values
, int stop_value
);
157 /* Should be a function but we need some macro magic. */
158 #define DIO_BV_PUT(pdout, k, bv) \
159 dio_put_memory((pdout), (bv).vec, sizeof((bv).vec))
161 #define DIO_PUT(f, d, k, ...) dio_put_##f(d, ## __VA_ARGS__)
165 #endif /* __cplusplus */
167 #endif /* FC__DATAIO_H */