Freeciv21
Develop your civilization from humble roots to a global empire
dataio_raw.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 // utility
16 #include "support.h"
17 
18 struct cm_parameter;
19 struct worklist;
20 struct unit_order;
21 struct requirement;
22 struct act_prob;
23 
24 struct data_in {
25  const void *src;
26  size_t src_size, current;
27 };
28 
29 struct raw_data_out {
30  void *dest;
32  bool too_short; // set to 1 if try to read past end
33 };
34 
35 /* Used for dio_<put|get>_type() methods.
36  * NB: we only support integer handling currently. */
37 enum data_type {
44 
45  DIOT_LAST
46 };
47 
48 // network string conversion
49 typedef char *(*DIO_PUT_CONV_FUN)(const char *src, size_t *length);
51 
52 typedef bool (*DIO_GET_CONV_FUN)(char *dst, size_t ndst, const char *src,
53  size_t nsrc);
55 
56 bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src,
57  size_t nsrc);
58 
59 // General functions
60 void dio_output_init(struct raw_data_out *dout, void *destination,
61  size_t dest_size);
62 void dio_output_rewind(struct raw_data_out *dout);
63 size_t dio_output_used(struct raw_data_out *dout);
64 
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);
69 
70 size_t data_type_size(enum data_type type);
71 
72 // gets
73 bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest)
74  fc__attribute((nonnull(3)));
75 
76 bool dio_get_uint8_raw(struct data_in *din, int *dest)
77  fc__attribute((nonnull(2)));
78 bool dio_get_uint16_raw(struct data_in *din, int *dest)
79  fc__attribute((nonnull(2)));
80 bool dio_get_uint32_raw(struct data_in *din, int *dest)
81  fc__attribute((nonnull(2)));
82 
83 bool dio_get_sint8_raw(struct data_in *din, int *dest)
84  fc__attribute((nonnull(2)));
85 bool dio_get_sint16_raw(struct data_in *din, int *dest)
86  fc__attribute((nonnull(2)));
87 bool dio_get_sint32_raw(struct data_in *din, int *dest)
88  fc__attribute((nonnull(2)));
89 
90 bool dio_get_bool8_raw(struct data_in *din, bool *dest)
91  fc__attribute((nonnull(2)));
92 bool dio_get_bool32_raw(struct data_in *din, bool *dest)
93  fc__attribute((nonnull(2)));
94 bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
95  fc__attribute((nonnull(2)));
96 bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
97  fc__attribute((nonnull(2)));
98 bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
99  fc__attribute((nonnull(2)));
100 bool dio_get_string_raw(struct data_in *din, char *dest,
101  size_t max_dest_size) fc__attribute((nonnull(2)));
102 bool dio_get_cm_parameter_raw(struct data_in *din,
103  struct cm_parameter *param)
104  fc__attribute((nonnull(2)));
105 bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
106  fc__attribute((nonnull(2)));
107 bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
108  fc__attribute((nonnull(2)));
109 bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
110  fc__attribute((nonnull(2)));
111 bool dio_get_action_probability_raw(struct data_in *din,
112  struct act_prob *aprob)
113  fc__attribute((nonnull(2)));
114 
115 // Should be a function but we need some macro magic.
116 #define DIO_BV_GET(pdin, bv) \
117  dio_get_memory_raw((pdin), (bv).vec, sizeof((bv).vec))
118 
119 #define DIO_GET(f, d, ...) dio_get_##f##_raw(d, ##__VA_ARGS__)
120 
121 // puts
122 void dio_put_type_raw(struct raw_data_out *dout, enum data_type type,
123  int value);
124 
125 void dio_put_uint8_raw(struct raw_data_out *dout, int value);
126 void dio_put_uint16_raw(struct raw_data_out *dout, int value);
127 void dio_put_uint32_raw(struct raw_data_out *dout, int value);
128 
129 void dio_put_sint8_raw(struct raw_data_out *dout, int value);
130 void dio_put_sint16_raw(struct raw_data_out *dout, int value);
131 void dio_put_sint32_raw(struct raw_data_out *dout, int value);
132 
133 void dio_put_bool8_raw(struct raw_data_out *dout, bool value);
134 void dio_put_bool32_raw(struct raw_data_out *dout, bool value);
135 void dio_put_ufloat_raw(struct raw_data_out *dout, float value,
136  int float_factor);
137 void dio_put_sfloat_raw(struct raw_data_out *dout, float value,
138  int float_factor);
139 
140 void dio_put_memory_raw(struct raw_data_out *dout, const void *value,
141  size_t size);
142 void dio_put_string_raw(struct raw_data_out *dout, const char *value);
143 void dio_put_city_map_raw(struct raw_data_out *dout, const char *value);
144 void dio_put_cm_parameter_raw(struct raw_data_out *dout,
145  const struct cm_parameter *param);
146 void dio_put_worklist_raw(struct raw_data_out *dout,
147  const struct worklist *pwl);
148 void dio_put_unit_order_raw(struct raw_data_out *dout,
149  const struct unit_order *order);
150 void dio_put_requirement_raw(struct raw_data_out *dout,
151  const struct requirement *preq);
153  const struct act_prob *aprob);
154 
155 // Should be a function but we need some macro magic.
156 #define DIO_BV_PUT(pdout, bv) \
157  dio_put_memory_raw((pdout), (bv).vec, sizeof((bv).vec))
158 
159 #define DIO_PUT(f, d, ...) dio_put_##f##_raw(d, ##__VA_ARGS__)
void dio_put_cm_parameter_raw(struct raw_data_out *dout, const struct cm_parameter *param)
Insert cm_parameter struct.
Definition: dataio_raw.cpp:436
void dio_put_action_probability_raw(struct raw_data_out *dout, const struct act_prob *aprob)
Serialize an action probability.
Definition: dataio_raw.cpp:880
void dio_put_unit_order_raw(struct raw_data_out *dout, const struct unit_order *order)
Insert the given unit_order struct/.
Definition: dataio_raw.cpp:460
bool dio_get_action_probability_raw(struct data_in *din, struct act_prob *aprob) fc__attribute((nonnull(2)))
De-serialize an action probability.
Definition: dataio_raw.cpp:861
void dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
Insert a float number, which is multiplied by 'float_factor' before being encoded into an uint32.
Definition: dataio_raw.cpp:369
void dio_input_init(struct data_in *dout, const void *src, size_t src_size)
Initializes the input to the given input buffer and the given number of valid input bytes.
Definition: dataio_raw.cpp:169
void dio_put_bool8_raw(struct raw_data_out *dout, bool value)
Insert value 0 or 1 using 8 bits.
Definition: dataio_raw.cpp:346
void dio_output_init(struct raw_data_out *dout, void *destination, size_t dest_size)
Initializes the output to the given output buffer and the given buffer size.
Definition: dataio_raw.cpp:144
bool dio_get_uint8_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Receive uint8 value to dest.
Definition: dataio_raw.cpp:492
void dio_put_uint16_raw(struct raw_data_out *dout, int value)
Insert value using 16 bits.
Definition: dataio_raw.cpp:248
void dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
Insert a float number, which is multiplied by 'float_factor' before being encoded into a sint32.
Definition: dataio_raw.cpp:388
void dio_set_get_conv_callback(DIO_GET_CONV_FUN fun)
Sets string conversion callback to use when getting text.
Definition: dataio_raw.cpp:104
bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src, size_t nsrc)
Call the get_conv callback.
Definition: dataio_raw.cpp:112
bool dio_get_type_raw(struct data_in *din, enum data_type type, int *dest) fc__attribute((nonnull(3)))
Receive value using 'size' bits to dest.
Definition: dataio_raw.cpp:555
bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size) fc__attribute((nonnull(2)))
Take string.
Definition: dataio_raw.cpp:725
size_t dio_input_remaining(struct data_in *din)
Return the number of unread bytes.
Definition: dataio_raw.cpp:185
void dio_put_uint8_raw(struct raw_data_out *dout, int value)
Insert value using 8 bits.
Definition: dataio_raw.cpp:229
void dio_put_sint16_raw(struct raw_data_out *dout, int value)
Insert value using 16 bits.
Definition: dataio_raw.cpp:326
bool(* DIO_GET_CONV_FUN)(char *dst, size_t ndst, const char *src, size_t nsrc)
Definition: dataio_raw.h:52
bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size) fc__attribute((nonnull(2)))
Take memory block directly.
Definition: dataio_raw.cpp:710
bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl) fc__attribute((nonnull(2)))
Take worklist item count and then kind and number for each item, and put them to provided worklist.
Definition: dataio_raw.cpp:826
void dio_put_city_map_raw(struct raw_data_out *dout, const char *value)
bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor) fc__attribute((nonnull(2)))
Get an unsigned float number, which have been multiplied by 'float_factor' and encoded into an uint32...
Definition: dataio_raw.cpp:622
bool dio_get_bool8_raw(struct data_in *din, bool *dest) fc__attribute((nonnull(2)))
Take boolean value from 8 bits.
Definition: dataio_raw.cpp:581
bool dio_get_uint32_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Receive uint32 value to dest.
Definition: dataio_raw.cpp:534
void dio_put_sint32_raw(struct raw_data_out *dout, int value)
Insert value using 32 bits.
Definition: dataio_raw.cpp:334
void dio_input_rewind(struct data_in *din)
Rewinds the stream so that the get-functions start from the beginning.
Definition: dataio_raw.cpp:180
void dio_put_uint32_raw(struct raw_data_out *dout, int value)
Insert value using 32 bits.
Definition: dataio_raw.cpp:267
char *(* DIO_PUT_CONV_FUN)(const char *src, size_t *length)
Definition: dataio_raw.h:49
bool dio_get_bool32_raw(struct data_in *din, bool *dest) fc__attribute((nonnull(2)))
Take boolean value from 32 bits.
Definition: dataio_raw.cpp:601
bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor) fc__attribute((nonnull(2)))
Get a signed float number, which have been multiplied by 'float_factor' and encoded into a sint32 by ...
Definition: dataio_raw.cpp:638
bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq) fc__attribute((nonnull(2)))
De-serialize a requirement.
Definition: dataio_raw.cpp:890
void dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
Insert block directly from memory.
Definition: dataio_raw.cpp:406
bool dio_get_cm_parameter_raw(struct data_in *din, struct cm_parameter *param) fc__attribute((nonnull(2)))
Get city manager parameters.
Definition: dataio_raw.cpp:763
void dio_put_worklist_raw(struct raw_data_out *dout, const struct worklist *pwl)
Insert number of worklist items as 8 bit value and then insert 8 bit kind and 8 bit number for each w...
Definition: dataio_raw.cpp:475
bool dio_get_sint32_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Take value from 32 bits.
Definition: dataio_raw.cpp:689
bool dio_get_uint16_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Receive uint16 value to dest.
Definition: dataio_raw.cpp:513
void dio_put_requirement_raw(struct raw_data_out *dout, const struct requirement *preq)
Serialize a requirement.
Definition: dataio_raw.cpp:917
bool dio_input_skip(struct data_in *din, size_t size)
Skips 'n' bytes.
Definition: dataio_raw.cpp:216
size_t data_type_size(enum data_type type)
Return the size of the data_type in bytes.
Definition: dataio_raw.cpp:193
void dio_put_type_raw(struct raw_data_out *dout, enum data_type type, int value)
Insert value using 'size' bits.
Definition: dataio_raw.cpp:286
data_type
Definition: dataio_raw.h:37
@ DIOT_UINT8
Definition: dataio_raw.h:38
@ DIOT_UINT32
Definition: dataio_raw.h:40
@ DIOT_UINT16
Definition: dataio_raw.h:39
@ DIOT_LAST
Definition: dataio_raw.h:45
@ DIOT_SINT32
Definition: dataio_raw.h:43
@ DIOT_SINT8
Definition: dataio_raw.h:41
@ DIOT_SINT16
Definition: dataio_raw.h:42
void dio_put_string_raw(struct raw_data_out *dout, const char *value)
Insert nullptr-terminated string.
Definition: dataio_raw.cpp:418
size_t dio_output_used(struct raw_data_out *dout)
Return the maximum number of bytes used.
Definition: dataio_raw.cpp:157
bool dio_get_sint8_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Take value from 8 bits.
Definition: dataio_raw.cpp:653
bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order) fc__attribute((nonnull(2)))
Take unit_order struct and put it in the provided orders.
Definition: dataio_raw.cpp:801
void dio_set_put_conv_callback(DIO_PUT_CONV_FUN fun)
Sets string conversion callback to be used when putting text.
Definition: dataio_raw.cpp:76
void dio_put_bool32_raw(struct raw_data_out *dout, bool value)
Insert value 0 or 1 using 32 bits.
Definition: dataio_raw.cpp:357
void dio_output_rewind(struct raw_data_out *dout)
Rewinds the stream so that the put-functions start from the beginning.
Definition: dataio_raw.cpp:163
bool dio_get_sint16_raw(struct data_in *din, int *dest) fc__attribute((nonnull(2)))
Take value from 16 bits.
Definition: dataio_raw.cpp:671
void dio_put_sint8_raw(struct raw_data_out *dout, int value)
Insert value using 8 bits.
Definition: dataio_raw.cpp:318
size_t size
Definition: specvec.h:64
const void * src
Definition: dataio_raw.h:25
size_t src_size
Definition: dataio_raw.h:26
size_t current
Definition: dataio_raw.h:26
bool too_short
Definition: dataio_raw.h:32
void * dest
Definition: dataio_raw.h:30
size_t used
Definition: dataio_raw.h:31
size_t dest_size
Definition: dataio_raw.h:31
size_t current
Definition: dataio_raw.h:31
int fc__attribute((nonnull(1, 3)))