aboutsummaryrefslogtreecommitdiff
path: root/src/cfgio.c
blob: b8a18294b2551e4f6a30778da6312cb97cd10165 (plain)
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "cfgio.h"

#include <sys/stat.h>
#include <unistd.h>

/***************************************************
 * help!!! functions
 ***************************************************/

char *
cfg_resolve_path(const char *filename)
{
  char *fn = NULL;

  if (filename[0] != '~') return strdup(filename);
  
  if (filename[1] == '/' ) {
    /* own home directory */
    char *home;
    if ((home = getenv ("HOME")) == NULL) {
      fn = strdup(filename);
    } else {
      fn = (char *)malloc(strlen(home) + strlen(filename+1) + 1);
      sprintf(fn, "%s%s", home,filename+1);
    }
  } else {
    /* other home directory */ 
    struct passwd *pw;
    char *dir = strchr(filename, '/');
    size_t len = (size_t)(dir - filename-1);
    char user[len];
    strncpy(user, filename+1, len);
    user[len] = '\0';
    
    if (!(pw = getpwnam(user))) {
      fn = strdup(filename); 
    } else {
      fn = (char *) malloc(strlen(pw->pw_dir) + strlen(dir) + 1);
      sprintf (fn, "%s%s",pw->pw_dir, dir);
    }  
  }  

  return fn;
}

/***************************************************
 * open functions
 ***************************************************/
int cfg_save(CfgHandle *h, int cl){
  if (h && h->file) {
    FILE *f = h->file;
    CfgGroup *group;
    group = h->first;

    while (group) {
      CfgKey *key;
      fprintf(f, "[%s]\n", group->name);
      key = group->first;
      while (key) {
	fprintf(f, "%s=%s\n", key->name, key->value ? key->value : "");
	key = key->next;
      }
      fprintf(f, "\n");
      group = group->next;
    }
    if (cl) return cfg_close(h);
    return 0;
  }
  return -1;
}

int cfg_save_as(CfgHandle *h, const char* filename, int err, int cl){
  if (h && filename) {
    int res;
    res = cfg_handle_change_filename(h, filename);
    if (res) return res;
    res = cfg_open(h, err, "w+");
    if (res) return res;
    return cfg_save(h,cl);
  }
  return -1;
}
  

int cfg_read(CfgHandle *h, int cl){
  if (h && h->file){
    char *gname = NULL;
    FILE* file = h->file;
    int offset;
    char *line = (char*)malloc(_CFG_MAX_LINE_SIZE);
    while ((line = fgets(line, _CFG_MAX_LINE_SIZE, file)) != NULL) {
      for (offset = strlen(line); offset > 0 && isspace(line[offset-1]); line[--offset] = '\0');
      for (offset = 0;isspace(line[offset]); offset++);
      switch (line[offset]) {
      case '#': 
      case '\0': break;
      case '[':	
	free (gname);
	gname = strdup(line+offset+1);
	gname[strlen(gname)-1] = '\0';
	cfg_group_new(h, gname);
	break;
      default: {
	char *k, *v, *vo, *p;
	size_t len;
	int i;
	p = strchr(line+offset, '=');
	if (p) {
	  len = (size_t)(p - (line+offset) + 1);
	  k = (char*)malloc(len);
	  k = strncpy(k, line+offset, len);
	  k[len-1] = '\0';
	  for (i = strlen(k); i > 0 && isspace(k[i-1]); k[--i] = '\0');
	  v = (char*)malloc(strlen(p+1)+1);
	  /* 	  v = (char*)malloc(strlen(p+1)); */
	  v = strcpy(v, p+1);
	  for (vo = v; isspace(vo[0]); vo++);
	  /* 	  for (vo = v+1; isspace(vo[0]); vo++); */
	  cfg_key_new_with_value(h, gname, k, vo);
	  free (v);
	} else {
	  len = (size_t)(strlen(line) - offset + 1);
	  k = (char*)malloc(len);
	  k = strncpy(k, line+offset, len);
	  k[len-1] = '\0';	    
	  vo = NULL;
	  cfg_key_new_with_value(h, gname, k, vo);
	}
	  
	free (k);
      }
      }
    }   
    free (line);
    free (gname);
    if (cl) return cfg_close(h);
    return 0;
  }
  return -1;
}


int cfg_open_file(CfgHandle* h, const char* filename, int reportErrors, const char* mode){
  if ((h->file = fopen (filename, mode)) != NULL) {
    return 0;
  } else {
    if (reportErrors) {
      fprintf (stderr, "Can't open configuration file \"%s\": %s\n",
	       filename, strerror (errno));
    }
    return -1;
  }
}

char *
cfg_find_file(char **files, char **pathes)
{
  /* e.g.:
     char *files[] = { ".blarc", "blarc", "bla", NULL };
     char *pathes[] = { "/", "/usr/etc/", "/etc/", NULL }; */ 
  char **file, **path = pathes;
  char *options_file = NULL, *help = NULL;
  struct stat s;
  
  while (*path) {
    file = files;
    while(*file) {
      free(options_file);
      options_file = (char *)malloc(sizeof(char) * (
						    strlen(*path) + 
						    strlen(*file) + 1));
      sprintf(options_file, "%s%s", *path, *file);
      help = cfg_resolve_path(options_file);
      if (!stat(help, &s)) {
	free(options_file);
	return help;
      }
      free(help);
      file++;
    }
    path++;
  }
  free(options_file);
  
  return NULL;
}

int
cfg_open_user_file(CfgHandle *h, const char* filename, 
		   int reportErrors, const char* mode)
{
  char *fn = cfg_resolve_path(filename);
  int ret = cfg_open_file(h, fn, reportErrors, mode);

  free(fn);
  
  return ret;
}

int cfg_open(CfgHandle* h, int reportErrors, const char* mode){
  int res;
  const char *filename;
  if (!h || !h->filename || !mode) return -1;
  filename = h->filename;
  if (filename[0] == '~') {
    /* Absolute Path of a Users HomeDir */
    res = cfg_open_user_file (h, filename, reportErrors, mode);
  } else if (filename[0] == '/') {
    /*       Absolute pathname: try it - there are no alternatives */
    res = cfg_open_file (h, filename, reportErrors, mode);
  } else {
    /*      Relative pathname: try current directory first */
    res = cfg_open_file (h, filename, 0, mode);
  }  
  return res;
}

int cfg_open_read (CfgHandle *handle, const char* filename, int err, int cl){
  if (handle && filename) {
    int res;
    res = cfg_handle_change_filename(handle, filename);
    if (res) return res;
    res = cfg_open(handle, err, "r");
    if (res) return res;    
    return cfg_read(handle,cl);
  }
  else return -1;
}

int cfg_close (CfgHandle* h){
  if (!h || !h->file) return -1;
  fclose (h->file);
  h->file = NULL;
  return 0;
}


/***************************************************
 * commons
 ***************************************************/

CfgGroup* cfg_find_group(CfgHandle* h, const char* g){
  CfgGroup *tmp;
  if (h == NULL || g == NULL) return NULL;
  tmp = h->first;
  while (tmp && strcmp(tmp->name, g) != 0) tmp = tmp->next;
  return tmp;
}

CfgKey* cfg_find_key(CfgGroup* g, const char* key){
  CfgKey* tmp;
  if (g == NULL || key == NULL) return NULL;
  tmp = g->first;
  while (tmp && strcmp(tmp->name, key) != 0) tmp = tmp->next;
  return tmp;
}

/***************************************************
 * cfg_handle_...
 ***************************************************/
  
CfgHandle* cfg_handle_new_with_filename (const char* filename, int read){
  CfgHandle* h;
  h = (CfgHandle*)malloc(sizeof(CfgHandle));
  if (!h) return NULL;
  
  h->filename = filename ? strdup(filename) : NULL;
  h->file  = NULL;
  h->first = h->last = NULL;

  if (read) {
    int r = cfg_open_read(h, filename, 0, 1);
    if (r != 0) {
      cfg_handle_free(h);
      return NULL;
    }
  }

  return h;
}

CfgHandle* cfg_handle_new (){
  return cfg_handle_new_with_filename(NULL,0);
}


int cfg_handle_change_filename(CfgHandle *h, const char* filename){
  if (h && filename) {
    if (h->file) return -2;
    free (h->filename);
    h->filename = strdup(filename);
    return 0;
  }
  else return -1;
}


int cfg_handle_add_group(CfgHandle* h, CfgGroup* g){
  if (h && g) {
    CfgGroup* last = h->last;
    if (last) {
      last->next = g;
      g->prev = last;
    } else {
      h->first = g;
    }
    h->last  = g;
    g->pp = h;
    return 0;
  }
  else 
    return -1;
}

void cfg_handle_free (CfgHandle* h){
  CfgGroup *ga, *gb;
  CfgKey *ka, *kb;
  if (!h) return;
  ga = h->first;
  if (h->file) fclose (h->file);
  while (ga){
    gb = ga->next;
    ka = ga->first;
    while (ka) {
      kb = ka->next;
      free (ka->name);
      free (ka->value);
      free (ka);
      ka = kb;      
    }
    free (ga->name);
    free (ga);
    ga = gb;
  }
  free (h);
}


/***************************************************
 * cfg_group_...
 ***************************************************/

int cfg_group_exist(CfgHandle* h, const char* name){
  if (h == NULL || name == NULL) 
    return -1;
  else {
    CfgGroup* tmp = h->first;
    while (tmp && strcmp(tmp->name,name) != 0 ) 
      tmp = tmp->next;
    if (tmp !=  NULL) return -1;
  }
  return 0;
}

int cfg_group_new (CfgHandle* handle, const char* name){
  if (cfg_find_group(handle, name) == NULL) {
    CfgGroup* g = (CfgGroup*)malloc(sizeof(CfgGroup));
    if (!g) return -1;
    g->name = strdup(name);
    g->prev = NULL;
    g->next = NULL;
    g->first = NULL;
    g->last = NULL;
    g->pp = NULL;
    return cfg_handle_add_group(handle, g);
  } else 
    return -1;
}

int cfg_group_add_key(CfgGroup* g, CfgKey* k){
  if (g && k) {
    CfgKey* last = g->last;
    if (last) {
      last->next = k;
      k->prev = last;
    } else {
      g->first = k;
    }
    
    g->last  = k;
    k->pp = g;
    return 0;
  } else 
    return -1;
}

void cfg_group_free(CfgHandle* h, const char* name){
  if (h && name) {
    CfgGroup *tmp, *prev, *next;
    CfgKey *a, *b;
    tmp = cfg_find_group(h, name);
    if (!tmp) return;
    
    a = tmp->first;
    while (a) {
      b = a->next;
      free (a->name);
      free (a->value);
      free (a);
      a = b;
    }

    prev = tmp->prev;
    next = tmp->next;
    
    free (tmp->name);
    if (prev) {
      prev->next = next;
    } else {
      tmp->pp->first= next;
    }

    if (next) {
      next->prev = prev;
    } else {
      tmp->pp->last = prev;
    }

    free(tmp);
  }
}

/***************************************************
 * cfg_key_...
 ***************************************************/

int
cfg_key_new (CfgHandle* h, const char* g, const char* key)
{
  CfgGroup *group;
  if ((group = cfg_find_group(h, g)) == NULL) return -1;
  //  if (cfg_find_key(group, key) != NULL) return -1;
  if (h && g && key) {
    CfgKey* k = (CfgKey*)malloc(sizeof(CfgKey));
    if (!k) return -1;
    k->name = strdup(key);
    k->value = NULL;
    k->prev = NULL;
    k->next = NULL;
    k->pp = NULL;
    return cfg_group_add_key(group, k);
  } else 
    return -1;
}

int
cfg_key_new_with_value(CfgHandle* h, const char* g, const char* key,
		       const char* value)
{
  if (cfg_key_new (h, g, key) == 0) {
    CfgKey* tmp = cfg_find_group(h, g)->last;
    if (!tmp) return -1;
    tmp->value = ((value == NULL) ? NULL : strdup(value));
    return 0;
  } else 
    return -1;
}

int
cfg_key_set_value(CfgHandle* h, const char* g, const char* key,
		  const char* value)
{
  if (h && g && key && value) {
    CfgKey* tmp = cfg_find_key(cfg_find_group(h,g), key);
    if (!tmp) return -1;
    free (tmp->value);
    tmp->value = ((value == NULL) ? NULL : strdup(value));
    return 0;
  } else return -1;
}

int
cfg_key_set_uint_value (CfgHandle *h, const char* g, const char* key,
			unsigned int value)
{
  char dest[30];
  sprintf(dest,"%ud", value);
  return cfg_key_set_value(h, g, key, dest);
}

int
cfg_key_set_int_value (CfgHandle *h, const char* g, const char* key,
		       int value)
{
  char dest[30];
  sprintf(dest,"%d", value);
  return cfg_key_set_value(h, g, key, dest);  
}

int
cfg_key_set_double_value (CfgHandle *h, const char* g, const char* key,
			  double value)
{
  char dest[30];
  sprintf(dest,"%e",value);
  return cfg_key_set_value(h, g, key, dest);
}

int
cfg_key_set_float_value (CfgHandle *h, const char* g, const char* key,
			 float value)
{	
  char dest[30];
  sprintf(dest,"%f",value);
  return cfg_key_set_value(h, g, key, dest);
}

int
cfg_key_set_bool_value (CfgHandle *h, const char* g, const char* key,
			bool value)
{
  char dest[2];
  sprintf(dest, "%1d", value);
  return cfg_key_set_value(h, g, key, dest);
}

int
cfg_key_set_char_value (CfgHandle *h, const char* g, const char* key,
			char value)
{
  char dest[2];
  sprintf(dest, "%c",value);
  return cfg_key_set_value(h, g, key, dest);  
}

const char*
cfg_key_get_value(CfgHandle* h, const char* g, const char* key)
{
  if (h && g && key) {
    CfgKey* tmp = cfg_find_key(cfg_find_group(h,g), key);
    if (!tmp) return NULL;
    return tmp->value;
  } else
    return NULL;
}

int 
cfg_key_get_value_as_bool(CfgHandle* h, const char* g, const char* key, 
			  bool* res){
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  if (!strcasecmp(nptr, "yes") || !strcasecmp(nptr, "true"))
    *res = TRUE;
  else
    *res = FALSE;

  return (int)*endptr;
}

int
cfg_key_get_value_as_uint(CfgHandle* h, const char* g, const char* key, 
			  unsigned int* res){
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (unsigned int)strtoul(nptr, &endptr, 10);
  return (int)*endptr;
}

int
cfg_key_get_value_as_int(CfgHandle* h, const char* g, const char* key,
			 int* res)
{
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (int)strtol(nptr, &endptr, 10);
  return (int)*endptr;
}
  
int
cfg_key_get_value_as_ushort(CfgHandle* h, const char* g, const char* key,
			    unsigned short* res)
{
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (unsigned short)strtoul(nptr, &endptr, 10);
  return (int)*endptr;
}

int 
cfg_key_get_value_as_short(CfgHandle* h, const char* g, const char* key,
			   short* res)
{
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (short)strtol(nptr, &endptr, 10);
  return (int)*endptr;
}

int 
cfg_key_get_value_as_double(CfgHandle* h, const char* g, const char* key,
			    double* res)
{
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (double)strtod(nptr, &endptr);
  return (int)*endptr;
}

int 
cfg_key_get_value_as_float(CfgHandle* h, const char* g, const char* key, 
			   float* res)
{
  char *endptr;
  const char *nptr = cfg_key_get_value(h, g, key);
  if (!nptr) return -1;
  *res = (float)strtod(nptr, &endptr);
  return (int)*endptr;
}

void
cfg_key_free (CfgHandle* h, const char* g, const char* key)
{
  if (h && g && key) {
    CfgKey *tmp, *prev, *next;
    tmp = cfg_find_key(cfg_find_group(h,g), key);
    if (!tmp) return;
    prev = tmp->prev;
    next = tmp->next;
    
    free (tmp->name);
    free (tmp->value);
    
    if (prev) {
      prev->next = next;
    } else {
      tmp->pp->first = next;
    }

    if (next) {
      next->prev = prev;
    } else {
      tmp->pp->last  = prev;
    }
    
    free (tmp);
  }
}

void
cfg_key_for_each (CfgHandle* h, const char* grp, cfg_key_func func)
{
  CfgGroup *tmp = cfg_find_group(h,grp);

  if (tmp && func) {
    CfgKey *k = tmp->first;      
    while(k) func(k), k = k->next;
  }
}