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 ***********************************************************************/
13 #ifndef FC__DATAIO_RAW_H
14 #define FC__DATAIO_RAW_H
18 #endif /* __cplusplus */
21 #include "bitvector.h"
22 #include "support.h" /* bool type */
30 size_t src_size
, current
;
35 size_t dest_size
, used
, current
;
36 bool too_short
; /* set to 1 if try to read past end */
39 /* Used for dio_<put|get>_type() methods.
40 * NB: we only support integer handling currently. */
52 /* What a location inside a packet is. */
54 /* A field. Addressed by its name. */
56 /* An array element. Addressed by its number. */
60 /* Address of a location inside a packet. */
62 /* The location kind. */
63 enum plocation_kind kind
;
66 /* Used if this is an array element */
69 /* Used if this is a field. */
73 /* If the full address is to a location inside this this field should
74 * point to it. If this location is the final location this field should
76 struct plocation
*sub_location
;
79 struct plocation
*plocation_field_new(char *name
);
80 struct plocation
*plocation_elem_new(int number
);
81 const char *plocation_name(const struct plocation
*loc
);
83 #ifdef FREECIV_JSON_CONNECTION
84 #include "dataio_json.h"
87 /* network string conversion */
88 typedef char *(*DIO_PUT_CONV_FUN
) (const char *src
, size_t *length
);
89 void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun
);
91 typedef bool(*DIO_GET_CONV_FUN
) (char *dst
, size_t ndst
,
92 const char *src
, size_t nsrc
);
93 void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun
);
95 bool dataio_get_conv_callback(char *dst
, size_t ndst
, const char *src
,
98 /* General functions */
99 void dio_output_init(struct raw_data_out
*dout
, void *destination
,
101 void dio_output_rewind(struct raw_data_out
*dout
);
102 size_t dio_output_used(struct raw_data_out
*dout
);
104 void dio_input_init(struct data_in
*dout
, const void *src
, size_t src_size
);
105 void dio_input_rewind(struct data_in
*din
);
106 size_t dio_input_remaining(struct data_in
*din
);
107 bool dio_input_skip(struct data_in
*din
, size_t size
);
109 size_t data_type_size(enum data_type type
);
112 bool dio_get_type_raw(struct data_in
*din
, enum data_type type
, int *dest
)
113 fc__attribute((nonnull (3)));
115 bool dio_get_uint8_raw(struct data_in
*din
, int *dest
)
116 fc__attribute((nonnull (2)));
117 bool dio_get_uint16_raw(struct data_in
*din
, int *dest
)
118 fc__attribute((nonnull (2)));
119 bool dio_get_uint32_raw(struct data_in
*din
, int *dest
)
120 fc__attribute((nonnull (2)));
122 bool dio_get_sint8_raw(struct data_in
*din
, int *dest
)
123 fc__attribute((nonnull (2)));
124 bool dio_get_sint16_raw(struct data_in
*din
, int *dest
)
125 fc__attribute((nonnull (2)));
126 bool dio_get_sint32_raw(struct data_in
*din
, int *dest
)
127 fc__attribute((nonnull (2)));
129 bool dio_get_bool8_raw(struct data_in
*din
, bool *dest
)
130 fc__attribute((nonnull (2)));
131 bool dio_get_bool32_raw(struct data_in
*din
, bool *dest
)
132 fc__attribute((nonnull (2)));
133 bool dio_get_ufloat_raw(struct data_in
*din
, float *dest
, int float_factor
)
134 fc__attribute((nonnull (2)));
135 bool dio_get_sfloat_raw(struct data_in
*din
, float *dest
, int float_factor
)
136 fc__attribute((nonnull (2)));
137 bool dio_get_memory_raw(struct data_in
*din
, void *dest
, size_t dest_size
)
138 fc__attribute((nonnull (2)));
139 bool dio_get_string_raw(struct data_in
*din
, char *dest
, size_t max_dest_size
)
140 fc__attribute((nonnull (2)));
141 bool dio_get_tech_list_raw(struct data_in
*din
, int *dest
)
142 fc__attribute((nonnull (2)));
143 bool dio_get_unit_list_raw(struct data_in
*din
, int *dest
)
144 fc__attribute((nonnull (2)));
145 bool dio_get_building_list_raw(struct data_in
*din
, int *dest
)
146 fc__attribute((nonnull (2)));
147 bool dio_get_worklist_raw(struct data_in
*din
, struct worklist
*pwl
)
148 fc__attribute((nonnull (2)));
149 bool dio_get_requirement_raw(struct data_in
*din
, struct requirement
*preq
)
150 fc__attribute((nonnull (2)));
151 bool dio_get_action_probability_raw(struct data_in
*din
,
152 struct act_prob
*aprob
)
153 fc__attribute((nonnull (2)));
155 bool dio_get_uint8_vec8_raw(struct data_in
*din
, int **values
, int stop_value
)
156 fc__attribute((nonnull (2)));
157 bool dio_get_uint16_vec8_raw(struct data_in
*din
, int **values
, int stop_value
)
158 fc__attribute((nonnull (2)));
160 /* There is currently no need to escape strings in the binary protocol. */
161 #define dio_get_estring_raw dio_get_string_raw
162 #define dio_put_estring_raw dio_put_string_raw
164 #ifndef FREECIV_JSON_CONNECTION
166 /* Should be a function but we need some macro magic. */
167 #define DIO_BV_GET(pdin, location, bv) \
168 dio_get_memory_raw((pdin), (bv).vec, sizeof((bv).vec))
170 #define DIO_GET(f, d, l, ...) dio_get_##f##_raw(d, ## __VA_ARGS__)
172 #endif /* FREECIV_JSON_CONNECTION */
175 void dio_put_type_raw(struct raw_data_out
*dout
, enum data_type type
, int value
);
177 void dio_put_uint8_raw(struct raw_data_out
*dout
, int value
);
178 void dio_put_uint16_raw(struct raw_data_out
*dout
, int value
);
179 void dio_put_uint32_raw(struct raw_data_out
*dout
, int value
);
181 void dio_put_sint8_raw(struct raw_data_out
*dout
, int value
);
182 void dio_put_sint16_raw(struct raw_data_out
*dout
, int value
);
183 void dio_put_sint32_raw(struct raw_data_out
*dout
, int value
);
185 void dio_put_bool8_raw(struct raw_data_out
*dout
, bool value
);
186 void dio_put_bool32_raw(struct raw_data_out
*dout
, bool value
);
187 void dio_put_ufloat_raw(struct raw_data_out
*dout
, float value
, int float_factor
);
188 void dio_put_sfloat_raw(struct raw_data_out
*dout
, float value
, int float_factor
);
190 void dio_put_memory_raw(struct raw_data_out
*dout
, const void *value
, size_t size
);
191 void dio_put_string_raw(struct raw_data_out
*dout
, const char *value
);
192 void dio_put_city_map_raw(struct raw_data_out
*dout
, const char *value
);
193 void dio_put_tech_list_raw(struct raw_data_out
*dout
, const int *value
);
194 void dio_put_unit_list_raw(struct raw_data_out
*dout
, const int *value
);
195 void dio_put_building_list_raw(struct raw_data_out
*dout
, const int *value
);
196 void dio_put_worklist_raw(struct raw_data_out
*dout
, const struct worklist
*pwl
);
197 void dio_put_requirement_raw(struct raw_data_out
*dout
, const struct requirement
*preq
);
198 void dio_put_action_probability_raw(struct raw_data_out
*dout
,
199 const struct act_prob
*aprob
);
201 void dio_put_uint8_vec8_raw(struct raw_data_out
*dout
, int *values
, int stop_value
);
202 void dio_put_uint16_vec8_raw(struct raw_data_out
*dout
, int *values
, int stop_value
);
204 #ifndef FREECIV_JSON_CONNECTION
206 /* Should be a function but we need some macro magic. */
207 #define DIO_BV_PUT(pdout, location, bv) \
208 dio_put_memory_raw((pdout), (bv).vec, sizeof((bv).vec))
210 #define DIO_PUT(f, d, l, ...) dio_put_##f##_raw(d, ## __VA_ARGS__)
212 #endif /* FREECIV_JSON_CONNECTION */
216 #endif /* __cplusplus */
218 #endif /* FC__DATAIO_RAW_H */