File: camconfig.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (481 lines) | stat: -rw-r--r-- 13,372 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*  camserv - An internet streaming picture application
 *
 *  Copyright (C) 1999  Jon Travis (jon@dsndata.com)
 *
 *  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 of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "log.h"
#include "camconfig.h"
#include "hash.h"

#define HASH_MAX_SECTIONS  HASHCOUNT_T_MAX
#define HASH_MAX_ENTRIES   HASHCOUNT_T_MAX

struct camconfig_section_st {
  char section_name[ MAX_SECTION_NAME + 1 ];
  hash_t *entryhash;
};

struct camconfig_st {
  hash_t *mainhash;  /* Hash of CamConfigSection's */
};

/*
 * section_new:  Create and initialize a new CamConfigSection structure
 *
 * Arguments:    name = New name for the config section
 *
 * Return values:  Returns NULL on failure, else a pointer to new
 *                 CamConfigSection structure.
 */

static
CamConfigSection *section_new( const char *name ){
  CamConfigSection *res;

  if( (res = malloc( sizeof( *res ))) == NULL )
    return NULL;

  strncpy( res->section_name, name, sizeof( res->section_name ) -1 );
  res->section_name[ sizeof( res->section_name ) - 1 ] = '\0';
  if( (res->entryhash = hash_create( HASH_MAX_SECTIONS, NULL, NULL )) == NULL){
    free( res );
    return NULL;
  }

  return res;
}

/*
 * section_add_pair:  Add or update a key/value pair in a section.  
 *
 * Arguments:         section = Section to update key/value pair in.
 *                    key     = Key to add
 *                    val     = Value associated with 'key'
 *
 * Return values:     Returns -1 on failure, 0 on success.
 */

static
int section_add_pair( CamConfigSection *section, char *key,
		      char *value )
{
  char *keydup, *valdup;
  hnode_t *hnode;

  if( (hnode = hash_lookup( section->entryhash, key )) != NULL ){
    if( (valdup = strdup( value )) == NULL )
      return -1;

    /* Replacing an existing value */
    free( hnode_get( hnode ) );
    hnode_put( hnode, valdup );
    return 0;
  }

  if( (keydup = strdup( key )) == NULL )
    return -1;

  if( (valdup = strdup( value )) == NULL ){
    free( keydup );
    return -1;
  }

  if( !hash_alloc_insert( section->entryhash, keydup, valdup )){
    free( keydup );
    free( valdup );
    return -1;
  }

  return 0;
}

/*
 * section_dest:  Destroy a camconfig section, and all the key/value pairs
 *                held within.
 *
 * Argumetns:     section = Section to destroy.
 */

static
void section_dest( CamConfigSection *section ){
  hscan_t hs;
  hnode_t *node;

  hash_scan_begin( &hs, section->entryhash );
  while( (node = hash_scan_next( &hs ))) {
    char *key, *val;

    key = hnode_getkey( node );
    val = hnode_get( node );
    hash_scan_delete( section->entryhash, node );
    section->entryhash->freenode( node, section->entryhash->context );
    free( key );
    free( val );
  }
  hash_destroy( section->entryhash );
  free( section );
}

/*
 * camconfig_new:   Create and initialize a new camconfig structure.
 *
 * Return values:   Returns NULL on failure, else a valid pointer to a new
 *                  camconfig structure.
 */

CamConfig *camconfig_new(){
  CamConfig *res;

  if( (res = malloc( sizeof( *res )))== NULL )
    return NULL;

  if( (res->mainhash = hash_create( HASH_MAX_SECTIONS, NULL, NULL )) == NULL ){
    free( res );
    return NULL;
  }

  return res;
}

/*
 * camconfig_dest:  Destroy a camconfig structure, all the sections and
 *                  key/value pairs within.
 *
 * Arguments:       ccfg = camconfig structure to destroy
 */

void camconfig_dest( CamConfig *ccfg ){
  hscan_t hs;
  hnode_t *node;

  hash_scan_begin( &hs, ccfg->mainhash );
  while( (node = hash_scan_next( &hs ))) {
    char *key;
    CamConfigSection *val;

    key = hnode_getkey( node );
    val = hnode_get( node );
    hash_scan_delete( ccfg->mainhash, node );
    ccfg->mainhash->freenode( node, ccfg->mainhash->context );
    free( key );
    section_dest( val );
  }
  hash_destroy( ccfg->mainhash );
  free( ccfg );
}

/*
 * camconfig_add_section:  Add a new section into a camconfig structure.
 *                         Note that duplicate section names are disallowed.
 *
 * Arguments:              ccfg = Camconfig structure to add section to.
 *                         newsec = New section name to add.
 *
 * Return valueS:          Returns NULL on failure, else a valid pointer to
 *                         the new section that was created and added to
 *                         the camconfig structure.
 */

static
CamConfigSection *camconfig_add_section( CamConfig *ccfg, char *newsec )
{
  CamConfigSection *res;
  char *keyval;
  hnode_t *node;

  if( hash_lookup( ccfg->mainhash, newsec ) != NULL ){
    camserv_log( "camconfig", "Section \"%s\" multi-defined in cfg",
		 newsec );
    return NULL;
  }
    
  if( (res = section_new( newsec )) == NULL )
    return NULL;

  if( (keyval = strdup( newsec )) == NULL ){
    section_dest( res );
    return NULL;
  }

  if( (node = hnode_create( res )) == NULL ){
    section_dest( res );
    free( keyval );
    return NULL;
  }

  hash_insert( ccfg->mainhash, node, keyval );
  return res;
}

/*
 * camconfig_read:  Create a camconfig structure, read a file containing
 *                  sections, and key/value pairs into it, and return it.
 *
 * Arguments:       fp = FILE to read camconfig data from.
 *
 * Return Values:   Returns NULL on failure, else a valid pointer to a new
 *                  camconfig structure.
 */

CamConfig *camconfig_read( FILE *fp ){
  CamConfigSection *current_section;
  CamConfig *ccfg;
  char buf[ 1024 ], *cp, *endcp, key[ 1024 ], value[ 1024 ];
  int lineno;

  if( (ccfg = camconfig_new()) == NULL ){
    camserv_log( "camconfig", "Error allocating memory for config!");
    return NULL;
  }

  current_section = NULL;
  lineno = 0;
  while( fgets( buf, sizeof( buf ), fp ) != NULL ){
    lineno++;
    if( buf[ 0 ] == '#' || buf[ 0 ] == '\n' ) 
      continue;

    if( buf[ 0 ] == '[' ) {  /* Begin a section */
      if( (endcp = strrchr( buf, ']' )) == NULL ){
	camserv_log( "camconfig", "Malformed section on line: %d", lineno );
	continue;
      }
      cp = &buf[ 1 ];
      *endcp = '\0';

      if( (current_section = camconfig_add_section( ccfg, cp )) == NULL ){
	camserv_log( "camconfig", "Error adding section! (malloc?)");
	camconfig_dest( ccfg );
	return NULL;
      }
      continue;
    } 

    /* key-val pair */
    if( current_section == NULL ){
      camserv_log( "camconfig","Line %d not in a section!", lineno );
      continue; /* Non-fatal error */
    }

    if( sscanf( buf, "%s %[^\n]s", key, value ) != 2 ){
      camserv_log( "camconfig", "Malformed input on line %d", lineno );
      continue;
    }

    if( section_add_pair( current_section, key, value ) == -1 ){
      camserv_log( "camconfig", "Malloc failure adding key-value pair!" );
      camconfig_dest( ccfg );
      return NULL;
    }
  }
  return ccfg;
}

/*
 * camconfig_set_str:  Set a string value of a key/value pair in the ccfg.
 *
 * Arguments:          ccfg = Camconfig struct to set the key/value pair in.
 *                     section = Section to contain 'key'
 *                     key  = Key to set (copied in locally)
 *                     val  = Value associated with the key. (copied in local)
 *
 * Return values:      Returns -1 on failure (add to an undefined section, or
 *                     malloc failure), else 0 on success.
 */

int camconfig_set_str( CamConfig *ccfg, char *secname, char *key, char *val ){
  hnode_t *node;
  CamConfigSection *section;

  /* Can't add to an undefined section! */
  if( (node = hash_lookup( ccfg->mainhash, secname )) == NULL )
    return -1;
  
  section = hnode_get( node );
  return section_add_pair( section, key, val );
}

/*
 * camconfig_set_int:  Set an int value of a key/value pair in the ccfg.
 *
 * Arguments:          ccfg = Camconfig struct to set the key/value pair in.
 *                     secname = Section to contain 'key'
 *                     key  = Key to set (copied in locally)
 *                     val  = Value associated with the key.
 *
 * Return values:      Returns -1 on failure (add to an undefined section, or
 *                     malloc failure), else 0 on success.
 */

int camconfig_set_int( CamConfig *ccfg, char *secname, char *key, int val ){
  char buf[ 1024 ];

  sprintf( buf, "%d", val );
  return camconfig_set_str( ccfg, secname, key, buf );
}

/*
 * camconfig_query_str:  Query a string value from the camconfig structure.
 *
 * Arguments:            ccfg = Camconfig struct to get the key/value pair from
 *                       secname = Section containing 'key'
 *                       key  = Key of the item
 *
 * Return values:        Returns -1 on failure (add to an undefined section, or
 *                       malloc failure), else 0 on success.
 */

const char *camconfig_query_str( CamConfig *ccfg, char *secname, char *key ){
  hnode_t *node;
  CamConfigSection *section;

  if( (node = hash_lookup( ccfg->mainhash, secname )) == NULL )
    return NULL;

  section = hnode_get( node );

  if( (node = hash_lookup ( section->entryhash, key )) == NULL )
    return NULL;

  return hnode_get( node );
}

/*
 * camconfig_query_int:  Query an int value from the camconfig structure.
 *
 * Arguments:            ccfg = Camconfig struct to get the key/value pair from
 *                       secname = Section containing 'key'
 *                       key  = Key of the item
 *                       err  = Location to place an error flag.
 *
 * Return values:        On failure, -1 will be returned, and *err will be
 *                       set to 1, else *err will be 0, and the return value
 *                       will be a valid integer representation of the value.
 */

int camconfig_query_int( CamConfig *ccfg, char *secname, char *key, int *err){
  hnode_t *node;
  CamConfigSection *section;
  int res;

  if( (node = hash_lookup( ccfg->mainhash, secname )) == NULL ){
    *err = 1;
    return -1;
  }

  section = hnode_get( node );

  if( (node = hash_lookup ( section->entryhash, key )) == NULL ){
    *err = 1;
    return -1;
  }

  *err = 0;
  sscanf( hnode_get( node ), "%d", &res );
  return res;
}  

/*
 * camconfig_query_def_float:  Query float value from the camconfig structure,
 *                             and use a default if it does not exist.
 *
 * Arguments:            ccfg = Camconfig struct to get the key/value pair from
 *                       secname = Section containing 'key'
 *                       key  = Key of the item
 *                       def = Default value to return if the key is not found.
 *
 * Return values:        On failure, 'def' is returned, else the value 
 *                       converted to (float) will be returned.
 */

float camconfig_query_def_float( CamConfig *ccfg, char *secname, char *key,
				 float def )
{
  hnode_t *node;
  CamConfigSection *section;

  if( (node = hash_lookup( ccfg->mainhash, secname )) == NULL ){
    camserv_log( "camconfig", "Using default of \"%f\" for [%s]:%s",
		 def, secname, key );
    return def;
  }

  section = hnode_get( node );

  if( (node = hash_lookup ( section->entryhash, key )) == NULL ){
    camserv_log( "camconfig", "Using default of \"%f\" for [%s]:%s",
		 def, secname, key );
    return def;
  }

  return atof( hnode_get( node ));
}  

/*
 * camconfig_query_def_int:  Query int value from the camconfig structure,
 *                           and use a default if it does not exist.
 *
 * Arguments:            ccfg = Camconfig struct to get the key/value pair from
 *                       secname = Section containing 'key'
 *                       key  = Key of the item
 *                       def = Default value to return if the key is not found.
 *
 * Return values:        On failure, 'def' is returned, else the value 
 *                       converted to (int) will be returned.
 */

int camconfig_query_def_int( CamConfig *ccfg, char *secname, char *key,int def)
{
  int err, res;

  res = camconfig_query_int( ccfg, secname, key, &err );
  if( err == 1 ){
    camserv_log( "camconfig", "Using default of \"%d\" for [%s]:%s",
		 def, secname, key );
    return def;
  }  else 
    return res;
}

static void printhash( hash_t *hash ){
  hscan_t hs;
  hnode_t *hn;

  hash_scan_begin(&hs, hash);
  while ((hn = hash_scan_next(&hs)))
    printf("%s\t%s\n", (char*) hnode_getkey(hn),
	   (char*) hnode_get(hn));
}

static void printcfg( CamConfig *ccfg ){
  hscan_t hs;
  hnode_t *hn;

  hash_scan_begin(&hs, ccfg->mainhash);
  while ((hn = hash_scan_next(&hs))){
    CamConfigSection *sec;

    sec = hnode_get( hn );
    printf("-------%s--------\n", sec->section_name );
    printhash( sec->entryhash );
  }
}