2 Copyright (C) Andrew Tridgell 2002
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 automatic marshalling/unmarshalling system for C structures
25 #include "parsers/parse_info.h"
27 /* load all the globals */
28 void load_all_globals(const char *fname)
33 s = file_load(fname, NULL);
35 d_printf("Unable to load globals!\n");
38 memset(&a, 0, sizeof(a));
39 gen_parse(pinfo_all_globals, (char *)&a, s);
42 net_globals = *a.net_globals;
43 game_globals = *a.game_globals;
44 player_globals = *a.player_globals;
45 command_globals = *a.command_globals;
46 gics_globals = *a.gics_globals;
47 timeseal_globals = *a.timeseal_globals;
51 FREE(a.player_globals);
52 FREE(a.command_globals);
54 FREE(a.timeseal_globals);
57 /* save all the globals */
58 void save_all_globals(const char *fname)
60 struct all_globals a, a2;
63 memset(&a, 0, sizeof(a));
64 a.net_globals = &net_globals;
65 a.game_globals = &game_globals;
66 a.player_globals = &player_globals;
67 a.command_globals = &command_globals;
68 a.gics_globals = &gics_globals;
69 a.timeseal_globals = ×eal_globals;
71 s = gen_dump(pinfo_all_globals, (char *)&a, 0);
73 d_printf("Unable to dump globals!\n");
76 file_save(fname, s, strlen(s));
77 memset(&a2, 0, sizeof(a2));
78 gen_parse(pinfo_all_globals, (char *)&a2, s);
79 s2 = gen_dump(pinfo_all_globals, (char *)&a2, 0);
81 d_printf("ERROR: globals parse mismatch!\n");
82 file_save("cmp.dat", s2, strlen(s2));
89 * adump - dump complete server state to globals.dat
91 int com_adump(int p, param_list param)
93 save_all_globals("globals.dat");
99 marshall a player structure
101 const char *marshall_player(const struct player *pp)
103 return gen_dump(pinfo_player, (const char *)pp, 0);
107 unmarshall a player structure
109 int unmarshall_player(struct player *pp, const char *s)
111 return gen_parse(pinfo_player, (char *)pp, s);
116 marshall a game structure
118 const char *marshall_game(const struct game *gg)
120 return gen_dump(pinfo_game, (const char *)gg, 0);
124 unmarshall a game structure
126 int unmarshall_game(struct game *gg, const char *s)
128 return gen_parse(pinfo_game, (char *)gg, s);
132 marshall a news structure
134 const char *marshall_news(const struct news *nn)
136 return gen_dump(pinfo_news, (const char *)nn, 0);
140 unmarshall a news structure
142 int unmarshall_news(struct news *nn, const char *s)
144 return gen_parse(pinfo_news, (char *)nn, s);
150 custom dump/load functions
152 int gen_dump_struct_in_addr(struct parse_string *p, const char *ptr, unsigned indent)
154 return gen_addgen(p, "%s", inet_ntoa(*(struct in_addr *)ptr));
157 int gen_parse_struct_in_addr(char *ptr, const char *str)
159 struct in_addr *a = (struct in_addr *)ptr;
160 if (inet_aton(str, a) == 0) {
161 /* probably an old style */
162 a->s_addr = atoi(str);