00001 #include "race.h"
00002 #include <stdio.h>
00003 #include "gamedata.h"
00004 #include "int_types.h"
00005
00006
00007
00008 #define sizeof_char (sizeof(char))
00009 #define sizeof_int8_t (sizeof(int8_t))
00010 #define sizeof_int16_t (sizeof(int16_t))
00011 #define sizeof_int32_t (sizeof(int32_t))
00012 #define sizeof_uint8_t (sizeof(uint8_t))
00013 #define sizeof_uint16_t (sizeof(uint16_t))
00014 #define sizeof_uint32_t (sizeof(uint32_t))
00015
00016
00017 #define get_char (char)get_uint8_t
00018 #define put_char (char)get_uint8_t
00019
00020 static inline uint8_t
00021 get_uint8_t(const void *buf)
00022 {
00023 return *((uint8_t *)buf);
00024 }
00025
00026 #define DECL_UNSIGNED_GET(bits, from) \
00027 static inline uint ## bits ## _t \
00028 get_uint ## bits ##_t (const void *buf) \
00029 { \
00030 return get_uint ## from ##_t (buf) \
00031 | (get_uint ## from##_t ((const char*)buf + (from)/8) << (from)); \
00032 } \
00033
00034 #define DECL_SIGNED_GET(bits) \
00035 static inline int ## bits ## _t \
00036 get_int ## bits ##_t (const void *buf) \
00037 { \
00038 return (int ## bits ## _t) get_uint ## bits ##_t (buf); \
00039 } \
00040
00041 DECL_UNSIGNED_GET(16, 8)
00042 DECL_UNSIGNED_GET(32, 16)
00043 DECL_SIGNED_GET(8)
00044 DECL_SIGNED_GET(16)
00045 DECL_SIGNED_GET(32)
00046
00047 static inline void
00048 put_uint8_t(void *buf, uint8_t v)
00049 {
00050 *((uint8_t *)buf) = v;
00051 }
00052
00053
00054 #define DECL_UNSIGNED_PUT(bits, from) \
00055 static inline void \
00056 put_uint ## bits ## _t (void *buf, uint ## bits ## _t v) \
00057 { \
00058 put_uint ## from ##_t (buf, (v & ((1 << (from))-1))); \
00059 put_uint ## from ##_t ((char*)buf + (from)/8, \
00060 (v >> (from)) & ((1L << (from))-1)); \
00061 } \
00062
00063 #define DECL_SIGNED_PUT(bits) \
00064 static inline void \
00065 put_int ## bits ##_t (void *buf, int##bits##_t v) \
00066 { \
00067 put_uint ## bits ## _t (buf, (uint##bits##_t)v); \
00068 } \
00069
00070 DECL_UNSIGNED_PUT(16, 8)
00071 DECL_UNSIGNED_PUT(32, 16)
00072 DECL_SIGNED_PUT(8)
00073 DECL_SIGNED_PUT(16)
00074 DECL_SIGNED_PUT(32)
00075
00076 #define DECL_xINT_FREAD(bits, sign) \
00077 size_t fread_##sign##int##bits##_t \
00078 (sign##int##bits##_t *p, size_t n, FILE *f) { \
00079 int i = 0; int elems = 0; \
00080 elems = fread (p, (bits)/8, n, f); \
00081 for (i = 0; i < elems; ++i, ++p) { \
00082 *p = get_##sign##int##bits##_t(p); \
00083 } \
00084 return elems; \
00085 } \
00086
00087 #define DECL_xINT_FWRITE(bits, sign) \
00088 size_t fwrite_##sign##int##bits##_t \
00089 (const sign##int##bits##_t *p, size_t n, FILE *f) { \
00090 sign##int##bits##_t t[256]; \
00091 int i = 0, elems = 0, total = 0; \
00092 while (n > 0) { \
00093 elems = (n < 256) ? n : 256; \
00094 for (i = 0; i < elems; ++i, ++p) \
00095 put_##sign##int##bits##_t(t+i, *p); \
00096 elems = fwrite(t, (bits)/8, elems, f); \
00097 if (!elems) break; \
00098 n -= elems; \
00099 total += elems; \
00100 } \
00101 return total; \
00102 } \
00103
00104 DECL_xINT_FREAD(8, )
00105 DECL_xINT_FREAD(16, )
00106 DECL_xINT_FREAD(32, )
00107 DECL_xINT_FREAD(8, u)
00108 DECL_xINT_FREAD(16,u)
00109 DECL_xINT_FREAD(32,u)
00110 DECL_xINT_FWRITE(8, )
00111 DECL_xINT_FWRITE(16, )
00112 DECL_xINT_FWRITE(32, )
00113 DECL_xINT_FWRITE(8, u)
00114 DECL_xINT_FWRITE(16,u)
00115 DECL_xINT_FWRITE(32,u)
00116
00117
00118
00119 #define DECL_FREAD(struct, type, bufelems) \
00120 size_t \
00121 fread_##type(struct type *dst, size_t num, FILE *f) \
00122 { \
00123 uint8_t tmp[(bufelems)*sizeof_##type]; \
00124 int i = 0, total = 0, elems = 0; \
00125 while (num > 0) \
00126 { \
00127 elems = (num < (bufelems)) ? num : (bufelems); \
00128 elems = fread(tmp, sizeof_##type, elems, f); \
00129 if (!elems) \
00130 break; \
00131 for (i = 0; i < elems; ++i) \
00132 get_##type(dst++, tmp+i*sizeof_##type); \
00133 total += elems; \
00134 num -= elems; \
00135 } \
00136 return total; \
00137 } \
00138
00139 #define DECL_FWRITE(struct, type, bufelems) \
00140 size_t \
00141 fwrite_##type(const struct type *src, size_t num, FILE *f) \
00142 { \
00143 uint8_t tmp[(bufelems)*sizeof_##type]; \
00144 int i = 0, total = 0, elems = 0; \
00145 while (num > 0) \
00146 { \
00147 elems = (num < (bufelems)) ? num : (bufelems); \
00148 for (i = 0; i < elems; ++i) \
00149 put_##type(tmp+i*sizeof_##type, src++); \
00150 elems = fwrite(tmp, sizeof_##type, elems, f); \
00151 if (!elems) \
00152 break; \
00153 total += elems; \
00154 num -= elems; \
00155 } \
00156 return total; \
00157 } \
00158
00159 #define DECL_GET_START(struct, type) \
00160 static inline void \
00161 get_##type (struct type *dst, const uint8_t *src) \
00162 { \
00163 int i = 0; \
00164
00165 #define DECL_GET_FIELD_SCALAR(type, name, num) \
00166 for (i = 0; i < (num); ++i) \
00167 { \
00168 *(((type *)&dst->name)+i) = get_##type(src); \
00169 src += sizeof_##type; \
00170 } \
00171
00172 #define DECL_GET_FIELD_STRUCT(str, type, name, num) \
00173 for (i = 0; i < (num); ++i) \
00174 { \
00175 get_##type((((str type *)&dst->name)+i), src); \
00176 src += sizeof_##type; \
00177 } \
00178
00179 #define DECL_GET_END }
00180
00181 #define DECL_PUT_START(struct, type) \
00182 static inline void \
00183 put_##type (uint8_t *dst, const struct type *src) \
00184 { \
00185 int i = 0; \
00186
00187 #define DECL_PUT_FIELD_SCALAR(type, name, num) \
00188 for (i = 0; i < (num); ++i) \
00189 { \
00190 put_##type(dst, *(((type *)&src->name)+i)); \
00191 dst += sizeof_##type; \
00192 } \
00193
00194 #define DECL_PUT_FIELD_STRUCT(str, type, name, num) \
00195 for (i = 0; i < (num); ++i) \
00196 { \
00197 put_##type(dst, ((str type *)(&src->name))+i); \
00198 dst += sizeof_##type; \
00199 } \
00200
00201 #define DECL_PUT_END }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 DECL_GET_START(struct, oLIST)
00216 DECL_GET_FIELD_SCALAR(int16_t, aIdx, 1)
00217 DECL_GET_FIELD_SCALAR(int16_t, sIdx, 1)
00218 DECL_GET_END
00219
00220 DECL_PUT_START(struct, oLIST)
00221 DECL_PUT_FIELD_SCALAR(int16_t, aIdx, 1)
00222 DECL_PUT_FIELD_SCALAR(int16_t, sIdx, 1)
00223 DECL_PUT_END
00224
00225
00226
00227
00228
00229
00230 DECL_GET_START(struct, oGROUP)
00231 DECL_GET_FIELD_SCALAR(uint8_t, ID, 10)
00232 DECL_GET_FIELD_STRUCT(struct, oLIST, oLIST, 5)
00233 DECL_GET_END
00234
00235 DECL_PUT_START(struct, oGROUP)
00236 DECL_PUT_FIELD_SCALAR(uint8_t, ID, 10)
00237 DECL_PUT_FIELD_STRUCT(struct, oLIST, oLIST, 5)
00238 DECL_PUT_END
00239
00240 DECL_FREAD(struct, oGROUP, 32)
00241
00242
00243
00244
00245 DECL_GET_START(struct, Table)
00246 DECL_GET_FIELD_SCALAR(uint8_t, fname, 8)
00247 DECL_GET_FIELD_SCALAR(int32_t, foffset, 1)
00248 DECL_GET_FIELD_SCALAR(uint16_t, size, 1)
00249 DECL_GET_END
00250
00251 DECL_PUT_START(struct, Table)
00252 DECL_PUT_FIELD_SCALAR(uint8_t, fname, 8)
00253 DECL_PUT_FIELD_SCALAR(int32_t, foffset, 1)
00254 DECL_PUT_FIELD_SCALAR(uint16_t, size, 1)
00255 DECL_PUT_END
00256
00257 DECL_FREAD(struct, Table, 32)
00258
00259
00260
00261
00262 DECL_GET_START(struct, oFGROUP)
00263 DECL_GET_FIELD_SCALAR(uint8_t, ID, 15)
00264 DECL_GET_FIELD_STRUCT(struct, oLIST, oLIST, 5)
00265 DECL_GET_END
00266
00267 DECL_PUT_START(struct, oFGROUP)
00268 DECL_PUT_FIELD_SCALAR(uint8_t, ID, 15)
00269 DECL_PUT_FIELD_STRUCT(struct, oLIST, oLIST, 5)
00270 DECL_PUT_END
00271
00272 DECL_FREAD(struct, oFGROUP, 32)
00273
00274
00275
00276
00277 DECL_GET_START(, SimpleHdr)
00278 DECL_GET_FIELD_SCALAR(uint16_t, size, 1)
00279 DECL_GET_FIELD_SCALAR(uint32_t, offset, 1)
00280 DECL_GET_END
00281
00282 DECL_PUT_START(, SimpleHdr)
00283 DECL_PUT_FIELD_SCALAR(uint16_t, size, 1)
00284 DECL_PUT_FIELD_SCALAR(uint32_t, offset, 1)
00285 DECL_PUT_END
00286
00287 DECL_FREAD(, SimpleHdr, 32)
00288
00289
00290 #if 0
00291
00292 DECL_GET_START(, REPLAY)
00293 DECL_GET_FIELD_SCALAR(uint8_t, Qty, 1)
00294 DECL_GET_FIELD_SCALAR(uint16_t, Off, 35)
00295 DECL_GET_END
00296
00297 DECL_PUT_START(, REPLAY)
00298 DECL_PUT_FIELD_SCALAR(uint8_t, Qty, 1)
00299 DECL_PUT_FIELD_SCALAR(uint16_t, Off, 35)
00300 DECL_PUT_END
00301
00302 DECL_FREAD(, REPLAY, 32)
00303 DECL_FWRITE(, REPLAY, 32)
00304 #endif
00305
00306 #if 0
00307 #include <stdio.h>
00308 uint8_t arr[1000];
00309 int main (void)
00310 {
00311 char *fname = "/tmp/data.test";
00312 FILE *f = fopen(fname, "w+b");
00313 struct oGROUP a, b = {
00314 "abcdefgh",
00315 {
00316 { 0x1234, 0x4321 },
00317 { 0x1034, 0x0321 },
00318 { 0x0234, 0x4301 },
00319 { 0x1204, 0x4021 },
00320 { 0x1230, 0x4320 }
00321 } };
00322 fwrite_oGROUP(&b, 1, f);
00323 fseek(f, 0, SEEK_SET);
00324 fread_oGROUP(&a, 1, f);
00325 fclose(f);
00326 remove(fname);
00327 return 0;
00328 }
00329 #endif