Check-in Lasker-2.2.3 tar ball from samba.org
[capablanca.git] / lasker-2.2.3 / src / parsers / parser.c
1 /*
2    Copyright (C) Andrew Tridgell 2002
3    
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.
8    
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.
13    
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.
17 */
18
19 /*
20   automatic marshalling/unmarshalling system for C structures
21 */
22
23 #define EXTERN
24 #include "includes.h"
25 #include "parsers/parse_info.h"
26
27 /* load all the globals */
28 void load_all_globals(const char *fname)
29 {
30         struct all_globals a;
31         char *s;
32
33         s = file_load(fname, NULL);
34         if (!s) {
35                 d_printf("Unable to load globals!\n");
36                 return;
37         }
38         memset(&a, 0, sizeof(a));
39         gen_parse(pinfo_all_globals, (char *)&a, s);
40         free(s);
41
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;
48
49         FREE(a.net_globals);
50         FREE(a.game_globals);
51         FREE(a.player_globals);
52         FREE(a.command_globals);
53         FREE(a.gics_globals);
54         FREE(a.timeseal_globals);
55 }
56
57 /* save all the globals */
58 void save_all_globals(const char *fname)
59 {
60         struct all_globals a, a2;
61         char *s, *s2;
62
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 = &timeseal_globals;
70
71         s = gen_dump(pinfo_all_globals, (char *)&a, 0);
72         if (!s) {
73                 d_printf("Unable to dump globals!\n");
74                 return;
75         }
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);
80         if (strcmp(s, s2)) {
81                 d_printf("ERROR: globals parse mismatch!\n");
82                 file_save("cmp.dat", s2, strlen(s2));
83         }
84         free(s);
85         free(s2);
86 }
87
88 /*
89  * adump - dump complete server state to globals.dat
90  */
91 int com_adump(int p, param_list param)
92 {
93         save_all_globals("globals.dat");
94         return COM_OK;
95 }
96
97
98 /*
99   marshall a player structure
100 */
101 const char *marshall_player(const struct player *pp)
102 {
103         return gen_dump(pinfo_player, (const char *)pp, 0);
104 }
105
106 /*
107   unmarshall a player structure
108 */
109 int unmarshall_player(struct player *pp, const char *s)
110 {
111         return gen_parse(pinfo_player, (char *)pp, s);
112 }
113
114
115 /*
116   marshall a game structure
117 */
118 const char *marshall_game(const struct game *gg)
119 {
120         return gen_dump(pinfo_game, (const char *)gg, 0);
121 }
122
123 /*
124   unmarshall a game structure
125 */
126 int unmarshall_game(struct game *gg, const char *s)
127 {
128         return gen_parse(pinfo_game, (char *)gg, s);
129 }
130
131 /*
132   marshall a news structure
133 */
134 const char *marshall_news(const struct news *nn)
135 {
136         return gen_dump(pinfo_news, (const char *)nn, 0);
137 }
138
139 /*
140   unmarshall a news structure
141 */
142 int unmarshall_news(struct news *nn, const char *s)
143 {
144         return gen_parse(pinfo_news, (char *)nn, s);
145 }
146
147
148
149 /*
150   custom dump/load functions
151 */
152 int gen_dump_struct_in_addr(struct parse_string *p, const char *ptr, unsigned indent)
153 {
154         return gen_addgen(p, "%s", inet_ntoa(*(struct in_addr *)ptr));
155 }
156
157 int gen_parse_struct_in_addr(char *ptr, const char *str)
158 {
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);
163         }
164         return 0;
165 }