[07a747]: checkconfig.c  Maximize  Restore  History

Download this file

139 lines (116 with data), 2.2 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libconfig.h>
#define DEFCONFIGFILE "/etc/oggstreamer.cfg"
enum {
T_BOOL,
T_INT,
T_STRING
} typedef CONFIG_MODE;
struct {
const char *string;
int integer;
} typedef RETURN_VALUES;
config_t cfg;
int readconfig(CONFIG_MODE mode, RETURN_VALUES *rets, char *string, char *file)
{
int ret=1;
config_init(&cfg);
/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, file) )
{
fprintf(stderr, "%s:%d - %s\n", file,
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return -1;
}
switch(mode)
{
case T_BOOL:
if(!config_lookup_bool(&cfg,string, &(rets->integer)) )
ret = -1;
break;
case T_INT:
if(!config_lookup_int(&cfg,string, &(rets->integer)) )
ret = -1;
break;
case T_STRING:
if(!config_lookup_string(&cfg,string, &(rets->string)) )
ret = -1;
break;
}
return ret;
}
void print_usage()
{
fprintf(stderr,
"checkconfig options name [configfile]\n" \
"\t-b BOOL entry\n" \
"\t-i INTEGER entry\n" \
"\t-s STRING entry\n"
"\tname of the configstring\n" \
"\tconfigfile (default /etc/oggstreamer.cfg)\n");
}
int main (int argc, char **argv)
{
CONFIG_MODE mode;
RETURN_VALUES rets;
char *string,*file=NULL;
if( !strncmp(argv[1], "-b", 2 ) )
{
mode = T_BOOL;
}
else if ( !strncmp(argv[1], "-i", 2) )
{
mode = T_INT;
}
else if ( !strncmp(argv[1], "-s", 2) )
{
mode = T_STRING;
}
else
{
print_usage();
return -1;
}
if (argc == 3)
{
string = argv[2];
}
else if (argc == 4)
{
string = argv[2];
file = argv[3];
}
else
{
print_usage();
return -1;
}
if(file == NULL) file = DEFCONFIGFILE;
if(readconfig(mode,&rets,string,file) == -1)
{
fprintf(stderr,"Error reading Configstring \"%s\" in File \"%s\"\n", string, file);
return -1;
} else {
switch(mode)
{
case T_BOOL:
if(rets.integer)
printf("TRUE");
else
printf("FALSE");
break;
case T_INT:
printf("%d",rets.integer);
break;
case T_STRING:
printf("%s",rets.string);
break;
}
}
return 1;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks