/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following functions.
- miniconf_read
- miniconf_create_param
1 /***************************************
2 $Revision: 1.1 $
3
4 Miniconf. miniconf.c - functions to deal with simple configuration files.
5
6 Status: NOT REVIEWED, NOT TESTED, NOT COMPLETE
7
8 Implementation by: Tiago Antao
9
10 ******************/ /******************
11 Copyright (c) 2002 RIPE NCC
12
13 All Rights Reserved
14
15 Permission to use, copy, modify, and distribute this software and its
16 documentation for any purpose and without fee is hereby granted,
17 provided that the above copyright notice appear in all copies and that
18 both that copyright notice and this permission notice appear in
19 supporting documentation, and that the name of the author not be
20 used in advertising or publicity pertaining to distribution of the
21 software without specific, written prior permission.
22
23 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
25 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
26 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 ***************************************/
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <rip.h>
34 #include "miniconf.h"
35
36 /*
37 miniconf_read - Reads a configuration file and returns its contents
38
39 The order of the parameters in the file _must_ match the order in
40 the conf structure.
41
42 Returns 1 if OK, 0 if not OK
43
44 NOTE: completely initialize conf (ie malloc _everything_!) before
45 calling this function.
46 */
47 int miniconf_read(miniconf_conf *conf, int conf_size, char *file){
/* [<][>][^][v][top][bottom][index][help] */
48 FILE* f;
49 char line[160];
50 char* space_pos;
51 int current_conf;
52
53 current_conf = 0;
54 f = fopen(file, "r");
55
56 if (f == NULL) {
57 return 0;
58 }
59 while (fgets(line, 160, f) != NULL && current_conf<conf_size) {
60 if (line[0] != '#') {
61 space_pos = index(line, ' ');
62 if (space_pos == NULL) {
63 return 0;
64 }
65 if (strncmp(line, conf[current_conf].name, space_pos - line) != 0) {
66 return 0;
67 }
68 if (strlen(space_pos + 1) >= conf[current_conf].max_size) {
69 return 0;
70 }
71 strcpy(conf[current_conf].value, space_pos + 1);
72 conf[current_conf].value[strlen(space_pos + 1) - 1] = 0;
73 current_conf++;
74 }
75 }
76
77 return 1;
78 }
79
80 void miniconf_create_param(miniconf_conf* conf, char* name, int size) {
/* [<][>][^][v][top][bottom][index][help] */
81 conf->name = UT_malloc(strlen(name) + 1);
82 strcpy(conf->name, name);
83 conf->max_size = size;
84 conf->value = UT_malloc(size);
85 }