File: registry.c

package info (click to toggle)
freeciv 1.5.2-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,612 kB
  • ctags: 2,887
  • sloc: ansic: 26,147; makefile: 66; sh: 35
file content (458 lines) | stat: -rwxr-xr-x 11,728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/********************************************************************** 
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

/**************************************************************************
  the idea with this file is to create something similar to the ms-windows
  .ini files functions.
  it also demonstrates how ugly code using the genlist class looks.
  however the interface is nice. ie:
  section_file_lookup_string(file, "player%d.unit%d.name", plrno, unitno); 
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>

#include <registry.h>
#include <log.h>
#include <shared.h>

/**************************************************************************
...
**************************************************************************/
void section_file_init(struct section_file *file)
{
  genlist_init(&file->section_list);
}


/**************************************************************************
...
**************************************************************************/
void section_file_free(struct section_file *file)
{
  struct genlist_iterator myiter;

  genlist_iterator_init(&myiter, &file->section_list, 0);

  for(; ITERATOR_PTR(myiter); ) {
    struct genlist_iterator myiter2;
    struct section *psection=(struct section *)ITERATOR_PTR(myiter);

    genlist_iterator_init(&myiter2, &psection->entry_list, 0);
    for(; ITERATOR_PTR(myiter2);) {
      struct section_entry *pentry=
	(struct section_entry *)ITERATOR_PTR(myiter2);
      ITERATOR_NEXT(myiter2);
      free(pentry->name);
      free(pentry->svalue);
      genlist_unlink(&psection->entry_list, pentry);
      free(pentry);
    }

    ITERATOR_NEXT(myiter);
    free(psection->name);
    genlist_unlink(&file->section_list, psection);
    free(psection);
  }

}



/**************************************************************************
...
**************************************************************************/
int section_file_load(struct section_file *my_section_file, char *filename)
{
  FILE *fs;
  char buffer[512];
  int lineno, ival=0;
  struct section *current_section=0;

  if(!(fs=fopen(filename, "r")))
    return 0;


  genlist_init(&my_section_file->section_list);


  lineno=0;

  while(fgets(buffer, 512, fs)) {
    char *cptr;

    lineno++;

    for(cptr=buffer; *cptr && isspace(*cptr); cptr++);

    if(!*cptr || *cptr==';')
      continue;

    else if(*cptr=='[') {
      struct section *psection;

      for(++cptr; *cptr && *cptr!=']'; cptr++);
      
      if(!*cptr) {
	log(LOG_FATAL, "missing ] in %s - line %d", filename, lineno);
	exit(1);
      }

      *cptr='\0';

      psection=(struct section *)malloc(sizeof(struct section));
      psection->name=mystrdup(buffer+1);
      genlist_init(&psection->entry_list);
      genlist_insert(&my_section_file->section_list, psection, -1);

      current_section=psection;

    }
    else {
      char *pname, *pvalue;
      struct section_entry *pentry;

      pvalue=0;

      for(; *cptr && isspace(*cptr); cptr++);

      if(!*cptr)
	continue;

      pname=cptr;

      for(; *cptr && *cptr!='='; cptr++);
      
      if(!*cptr) {
	log(LOG_FATAL, "syntax error in %s - line %d", filename, lineno);
	exit(1);
      }

      
      *cptr++='\0';

      for(; *cptr && isspace(*cptr); cptr++);

      if(isdigit(*cptr) || *cptr=='-')
	ival=atoi(cptr);
      else if(*cptr=='\"') {
	pvalue=++cptr;
	for(; *cptr && *cptr!='\"'; cptr++);
	
	if(!*cptr) {
	  log(LOG_FATAL, "expected end of string in %s - line %d", filename, lineno);
	  exit(1);
	}

	*cptr='\0';

      }
      else {
	log(LOG_FATAL, "syntax error in %s - line %d", filename, lineno);
	exit(1);
      }


      if(!current_section) {
	log(LOG_FATAL, "entry defined before first section in %s - line %d", 
	    filename, lineno);
	exit(1);
      }

      
      pentry=(struct section_entry *)malloc(sizeof(struct section_entry));
      pentry->name=mystrdup(pname);
      
      if(pvalue)
	pentry->svalue=minstrdup(pvalue);
      else {
	pentry->ivalue=ival;
	pentry->svalue=0;
      }
      genlist_insert(&current_section->entry_list, pentry, -1); 

    }
  }

  fclose(fs);
  
  return 1;
}

/**************************************************************************
...
**************************************************************************/
int section_file_save(struct section_file *my_section_file, char *filename)
{
  FILE *fs;

  struct genlist_iterator myiter;

  if(!(fs=fopen(filename, "w")))
    return 0;


  genlist_iterator_init(&myiter, &my_section_file->section_list, 0);
  

  for(; ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter)) {
    struct genlist_iterator myiter2;
    fprintf(fs, "\n[%s]\n",((struct section *)ITERATOR_PTR(myiter))->name);

    genlist_iterator_init(&myiter2, &((struct section *)
			       ITERATOR_PTR(myiter))->entry_list, 0);

    for(; ITERATOR_PTR(myiter2); ITERATOR_NEXT(myiter2)) {
      if(((struct section_entry *)ITERATOR_PTR(myiter2))->svalue)
	fprintf(fs, "%s=\"%s\"\n", 
		((struct section_entry *)ITERATOR_PTR(myiter2))->name,
		((struct section_entry *)ITERATOR_PTR(myiter2))->svalue);
      else
	fprintf(fs, "%s=%d\n", 
		((struct section_entry *)ITERATOR_PTR(myiter2))->name,
		((struct section_entry *)ITERATOR_PTR(myiter2))->ivalue);
    }


  }

  if(ferror(fs) || fclose(fs) == EOF)
    return 0;

  return 1;
}

/**************************************************************************
...
**************************************************************************/
char *secfile_lookup_str(struct section_file *my_section_file, char *path, ...)
{
  struct section_entry *entry;
  char buf[512];
  va_list ap;

  va_start(ap, path);
  vsprintf(buf, path, ap);
  va_end(ap);


  if(!(entry=section_file_lookup_internal(my_section_file, buf))) {
    log(LOG_FATAL, "sectionfile doesn't contain a '%s' entry", buf);
    exit(1);
  }

  if(!entry->svalue) {
    log(LOG_FATAL, "sectionfile entry '%s' doesn't contain a string", buf);
    exit(1);
  }
  
  return entry->svalue;
}

      

/**************************************************************************
...
**************************************************************************/
void secfile_insert_int(struct section_file *my_section_file,
			int val, char *path, ...)
{
  struct section_entry *pentry;
  char buf[512];
  va_list ap;

  va_start(ap, path);
  vsprintf(buf, path, ap);
  va_end(ap);

  pentry=section_file_insert_internal(my_section_file, buf);

  pentry->ivalue=val;
  pentry->svalue=0;
  
}



/**************************************************************************
...
**************************************************************************/
void secfile_insert_str(struct section_file *my_section_file,
			char *sval, char *path, ...)
{
  struct section_entry *pentry;
  char buf[512];
  va_list ap;

  va_start(ap, path);
  vsprintf(buf, path, ap);
  va_end(ap);

  pentry=section_file_insert_internal(my_section_file, buf);
  pentry->svalue=mystrdup(sval);
}



/**************************************************************************
...
**************************************************************************/
int secfile_lookup_int(struct section_file *my_section_file, 
		       char *path, ...)
{
  struct section_entry *entry;
  char buf[512];
  va_list ap;

  va_start(ap, path);
  vsprintf(buf, path, ap);
  va_end(ap);

  if(!(entry=section_file_lookup_internal(my_section_file, buf))) {
    log(LOG_FATAL, "sectionfile doesn't contain a '%s' entry", buf);
    exit(1);
  }

  if(entry->svalue) {
    log(LOG_FATAL, "sectionfile entry '%s' doesn't contain an integer", buf);
    exit(1);
  }
  
  return entry->ivalue;
}



/**************************************************************************
...
**************************************************************************/
int section_file_lookup(struct section_file *my_section_file, 
			char *path, ...)
{
  char buf[512];
  va_list ap;

  va_start(ap, path);
  vsprintf(buf, path, ap);
  va_end(ap);

  return !!section_file_lookup_internal(my_section_file, buf);
}





/**************************************************************************
...
**************************************************************************/
struct section_entry *section_file_lookup_internal(struct section_file 
						   *my_section_file,  
						   char *fullpath) 
						   
{
  char *pdelim;
  char sec_name[512];
  char ent_name[512];
  struct genlist_iterator myiter;


  if(!(pdelim=strchr(fullpath, '.'))) /* i dont like strtok */
    return 0;
  strncpy(sec_name, fullpath, pdelim-fullpath);
  sec_name[pdelim-fullpath]='\0';
  strcpy(ent_name, pdelim+1);

  genlist_iterator_init(&myiter, &my_section_file->section_list, 0);

  for(; ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter))
    if(!strcmp(((struct section *)ITERATOR_PTR(myiter))->name, sec_name)) {
      struct genlist_iterator myiter2;
      genlist_iterator_init(&myiter2, &((struct section *)
				 ITERATOR_PTR(myiter))->entry_list, 0);
      for(; ITERATOR_PTR(myiter2); ITERATOR_NEXT(myiter2)) {
   	if(!strcmp(((struct section_entry *)ITERATOR_PTR(myiter2))->name, 
		   ent_name))
	  return (struct section_entry *)ITERATOR_PTR(myiter2);
      }
    }

  return 0;
}


/**************************************************************************
...
**************************************************************************/
struct section_entry *section_file_insert_internal(struct section_file 
						   *my_section_file, 
						   char *fullpath)
{
  char *pdelim;
  char sec_name[512];
  char ent_name[512];
  struct genlist_iterator myiter;
  struct section *psection;
  struct section_entry *pentry;


  if(!(pdelim=strchr(fullpath, '.'))) /* i dont like strtok */
    return 0;
  strncpy(sec_name, fullpath, pdelim-fullpath);
  sec_name[pdelim-fullpath]='\0';
  strcpy(ent_name, pdelim+1);


  genlist_iterator_init(&myiter, &my_section_file->section_list, 0);

  for(; ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter))
    if(!strcmp(((struct section *)ITERATOR_PTR(myiter))->name, sec_name)) {

      struct genlist_iterator myiter2;
      genlist_iterator_init(&myiter2, &((struct section *)
				 ITERATOR_PTR(myiter))->entry_list, 0);
      for(; ITERATOR_PTR(myiter2); ITERATOR_NEXT(myiter2)) {
   	if(!strcmp(((struct section_entry *)ITERATOR_PTR(myiter2))->name, 
		   ent_name))
	  return (struct section_entry *)ITERATOR_PTR(myiter2);
      }

      pentry=(struct section_entry *)malloc(sizeof(struct section_entry));
      pentry->name=mystrdup(ent_name);
      genlist_insert(&((struct section *)
			    ITERATOR_PTR(myiter))->entry_list, pentry, -1);

      return pentry;
    }


  psection=(struct section *)malloc(sizeof(struct section));
  psection->name=mystrdup(sec_name);
  genlist_init(&psection->entry_list);
  genlist_insert(&my_section_file->section_list, psection, -1);
  
  pentry=(struct section_entry *)malloc(sizeof(struct section_entry));
  pentry->name=mystrdup(ent_name);
  genlist_insert(&psection->entry_list, pentry, -1);

  return pentry;
}