-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_file.cpp
More file actions
122 lines (104 loc) · 3.36 KB
/
config_file.cpp
File metadata and controls
122 lines (104 loc) · 3.36 KB
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
/*************************************************
* Publicly released by Rhoban System, August 2012
* www.rhoban-system.fr
*
* Freely usable for non-commercial purposes
*
* Licence Creative Commons *CC BY-NC-SA
* http://creativecommons.org/licenses/by-nc-sa/3.0
*************************************************/
/** ***************************************************************************
* \file config_file.h
* \author Hugo Gimbert - Olivier Ly
* \date 2011-01
*
* \brief Ligh config file management.
* The syntax of a config file is simple.
* Each line is
* - either a definition of form IDENT = value where value is a numerical value, int or double
* - or a comment, introduced by a '#' character.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config_file.h"
/****************************************************************************/
FILE * fd;
int cf_verbose = 0;
void config_file_verbose() { cf_verbose = 1; }
void config_file_silent() { cf_verbose = 0; }
#define is_space(x) (x==' ' || x=='\t')
char line_buffer[256];
int lb_idx;
char lexem_buffer[256];
/* return the length of the line, or -1 if there is a eof. */
int read_line() {
int idx = 0;
int c;
while ( idx < 255 && ((c = fgetc(fd)) != EOF) && (c != '\n') )
line_buffer[idx++] = c;
line_buffer[idx]=0;
if (c == EOF) return -1;
else return idx;
}
void eat_spaces(int lb_idx_max) {
while (is_space(line_buffer[lb_idx]) && (lb_idx <= lb_idx_max)) lb_idx++;
}
void eat_lexem(int lb_idx_max) {
eat_spaces(lb_idx_max);
sscanf(line_buffer+lb_idx, "%s", lexem_buffer);
lb_idx += (int) strlen(lexem_buffer);
}
#define SYNTAX_ERROR(reason) { printf("config file syntax error: %s\n", reason); fclose(fd); return 2; }
/* return 1 if the char has been found, (caution) 2 otherwise */
int eat_char(char c, int lb_idx_max) {
eat_spaces(lb_idx_max);
if (line_buffer[lb_idx++] != c) { SYNTAX_ERROR("char unexpected"); }
return 1;
}
int get_value(char * file, char * id) {
if ((fd = fopen(file, "r")) == NULL) {
printf("get_int_value: unable to open file %s\n", file);
return 1;
}
int length;
int found = 0;
int print_it = 0;
while (!found && (length = read_line()) != -1) {
lb_idx=0;
eat_spaces(length-1);
if ((length-lb_idx) == 0) continue;
if (line_buffer[lb_idx] == '#') { continue; }
eat_lexem(length-1);
if (strcmp(id, lexem_buffer) == 0) { found = 1; print_it = 1; }
if (cf_verbose && print_it == 1) printf("%s = ", lexem_buffer);
eat_char('=', length-1);
eat_lexem(length-1);
if (cf_verbose && print_it == 1) { printf("%s\n", lexem_buffer); print_it = 0; }
}
fclose(fd);
if (found) return 0;
else return -1;
}
int get_int_value(char * file, char * id, int * value) {
int ret = get_value(file, id);
if (ret == 0) {
*value = atoi(lexem_buffer);
return 0;
} else {
if (cf_verbose) printf("%s = %d [Default value]\n", id, *value);
return ret;
}
}
int get_double_value(char * file, char * id, double * value) {
int ret = get_value(file, id);
if (ret == 0) {
*value = atof(lexem_buffer);
return 0;
} else {
if (cf_verbose) printf("%s = %f [Default value]\n", id, *value);
return ret;
}
}
/****************************************************************************/
/****************************************************************************/