config.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "config.h"
  2. void config_load_default(config_t *cfg)
  3. {
  4. config_add_int(cfg, "pin.data", 2);
  5. config_add_int(cfg, "pin.clk", 3);
  6. config_add_int(cfg, "pin.ack", 4);
  7. config_add_int(cfg, "cache.flush_interval", 86400);
  8. config_add_string(cfg, "cache.file", "/tmp/compteur_elec.cache");
  9. config_add_int(cfg, "mount", 1);
  10. config_add_string(cfg, "mount.file", "192.168.0.88");
  11. config_add_string(cfg, "mount.src", "//192.168.0.88/nas");
  12. config_add_string(cfg, "mount.dst", "/media/nas");
  13. config_add_string(cfg, "mount.username", "ptitcois");
  14. config_add_string(cfg, "mount.password", "e1010898");
  15. config_add_string(cfg, "storage.location", "/media/samba");
  16. config_add_string(cfg, "storage.data", "data");
  17. config_add_string(cfg, "storage.years", "years");
  18. config_add_string(cfg, "storage.monthes", "monthes");
  19. config_add_string(cfg, "storage.days", "days");
  20. config_add_string(cfg, "misc.pidfile", "/tmp/compteur_elec.cache");
  21. }
  22. opt_t* _config_get(config_t* cfg, const char* name)
  23. {
  24. int i;
  25. for(i=0; i<cfg->n; i++)
  26. {
  27. if(!strcmp(name, cfg->opts[i].name))
  28. return &cfg->opts[i];
  29. }
  30. return NULL;
  31. }
  32. int _fgetc(config_t *cfg, FILE* f)
  33. {
  34. int com=0, c;
  35. do{
  36. c = fgetc(f);
  37. if(c=='\n')
  38. {
  39. cfg->c=0;
  40. cfg->line++;
  41. com=0;
  42. }else cfg->c++;
  43. if(c=='#')
  44. com=1;
  45. }while(com);
  46. return c;
  47. }
  48. int _config_next_opt(config_t *cfg, FILE* f)
  49. {
  50. char name[64];
  51. int c, i;
  52. while((c=_fgetc(cfg,f))==' ' || c=='\t' || c=='\r' || c=='\n');
  53. if(c==EOF)
  54. return 0;
  55. name[0]=c;
  56. for(i=1; (c=_fgetc(cfg,f))!=' ' && c!='\t' && c!='\r' && c!='\n' && c!='=' && c!=EOF; i++)
  57. name[i]=c;
  58. name[i]=0;
  59. if(c==EOF)
  60. {
  61. fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
  62. return 0;
  63. }
  64. while( c!='=' && (c=_fgetc(cfg,f))!='=' && c!=EOF);
  65. if(c==EOF)
  66. {
  67. fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
  68. return 0;
  69. }
  70. while((c=_fgetc(cfg,f))==' ' || c=='\t' || c=='\r' || c=='\n');
  71. if(c==EOF)
  72. {
  73. fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
  74. return 0;
  75. }
  76. if(c=='"')
  77. {
  78. char buffer[4096];
  79. for(i=0; (c=_fgetc(cfg, f))!='"'; i++)
  80. buffer[i]=c;
  81. buffer[i]=0;
  82. config_add_string(cfg, name, buffer);
  83. }
  84. else if(c>='0' && c<='9')
  85. {
  86. char buffer[128];
  87. int isfloat=0;
  88. buffer[0]=c;
  89. for(i=1; ((c=_fgetc(cfg, f))>='0' && c<='9') || c=='.' ; i++)
  90. {
  91. if(c=='.') isfloat=1;
  92. buffer[i]=c;
  93. }
  94. buffer[i]=0;
  95. if(isfloat)
  96. config_add_float(cfg, name, atof(buffer));
  97. else
  98. config_add_int(cfg, name, atoi(buffer));
  99. }
  100. else
  101. {
  102. fprintf(stderr, "Erreur de syntaxe: attendue STRING, INT ou FLOAT, token '%c'(%d), ligne %d:%d\n", c, c, cfg->line, cfg->c);
  103. return 0;
  104. }
  105. while( c!=';' && (c=_fgetc(cfg, f))!=';' && c!=EOF);
  106. if(c==EOF)
  107. {
  108. fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. void config_init(config_t *cfg, const char* path)
  114. {
  115. FILE* f;
  116. cfg->n=0;
  117. cfg->c=0;
  118. cfg->line=0;
  119. f=fopen(path, "r");
  120. if(!f)
  121. {
  122. fprintf(stderr, "WARNING: Unable to open config file '%s'", path);
  123. perror(" ");
  124. fprintf(stderr, "Loading default config\n");
  125. return;
  126. }
  127. while(_config_next_opt(cfg, f));
  128. fclose(f);
  129. }
  130. void config_add_int(config_t* cfg, const char* name, int val)
  131. {
  132. union opt_value value;
  133. value._int=val;
  134. config_add(cfg, name, OPT_TYPE_INT, value);
  135. }
  136. void config_add_float(config_t* cfg, const char* name, double val)
  137. {
  138. union opt_value value;
  139. value._float=val;
  140. config_add(cfg, name, OPT_TYPE_FLOAT, value);
  141. }
  142. void config_add_string(config_t* cfg, const char* name, const char* val)
  143. {
  144. union opt_value value;
  145. char * x;
  146. x=malloc(strlen(val)+1);
  147. memcpy(x, val, strlen(val)+1);
  148. value._string=x;
  149. config_add(cfg, name, OPT_TYPE_STRING, value);
  150. }
  151. void config_add(config_t* cfg, const char* name, int type, union opt_value val)
  152. {
  153. opt_t opt, *o;
  154. strcpy(opt.name, name);
  155. opt.type=type;
  156. opt.val = val;
  157. if( (o=_config_get(cfg, name)) )
  158. *o=opt;
  159. else cfg->opts[cfg->n++]=opt;
  160. }
  161. void config_free(config_t *cfg)
  162. {
  163. int i;
  164. for(i=0; i<cfg->n; i++)
  165. if(cfg->opts[i].type==OPT_TYPE_STRING)
  166. free(cfg->opts[i].val._string);
  167. }
  168. const char* config_get_string(config_t* cfg, const char* name)
  169. {
  170. opt_t* op = _config_get(cfg, name);
  171. if(!op || op->type!=OPT_TYPE_STRING) return "(null)";
  172. return op->val._string;
  173. }
  174. int config_get_int(config_t* cfg, const char* name)
  175. {
  176. opt_t* op = _config_get(cfg, name);
  177. if(!op || op->type!=OPT_TYPE_INT) return -1;
  178. return op->val._int;
  179. }
  180. double config_get_float(config_t* cfg, const char* name)
  181. {
  182. opt_t* op = _config_get(cfg, name);
  183. if(!op || op->type!=OPT_TYPE_FLOAT) return 0.0;
  184. return op->val._float;
  185. }
  186. int config_get_type(config_t* cfg, const char* name)
  187. {
  188. opt_t* op = _config_get(cfg, name);
  189. if(!op) return OPT_TYPE_ERROR;
  190. return op->type;
  191. }
  192. void config_print(config_t *cfg)
  193. {
  194. int i;
  195. for(i=0; i<cfg->n; i++)
  196. {
  197. opt_t * o = &cfg->opts[i];
  198. if(o->type==OPT_TYPE_FLOAT)
  199. printf("%s=%f\n", o->name, o->val._float);
  200. if(o->type==OPT_TYPE_INT)
  201. printf("%s=%d\n", o->name, o->val._int);
  202. if(o->type==OPT_TYPE_STRING)
  203. printf("%s=\"%s\"\n", o->name, o->val._string);
  204. }
  205. }