libiniparser.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*-------------------------------------------------------------------------*/
  2. /**
  3. @file iniparser.c
  4. @author N. Devillard
  5. @date Sep 2007
  6. @version 3.0
  7. @brief Parser for ini files.
  8. */
  9. /*--------------------------------------------------------------------------*/
  10. /*
  11. $Id: iniparser.c,v 2.18 2008-01-03 18:35:39 ndevilla Exp $
  12. $Revision: 2.18 $
  13. $Date: 2008-01-03 18:35:39 $
  14. */
  15. /*---------------------------- Includes ------------------------------------*/
  16. #include <ctype.h>
  17. #include <libiniparser.h>
  18. /*---------------------------- Defines -------------------------------------*/
  19. #define ASCIILINESZ (1024)
  20. #define INI_INVALID_KEY ((char*)-1)
  21. /*---------------------------------------------------------------------------
  22. Private to this module
  23. ---------------------------------------------------------------------------*/
  24. /**
  25. * This enum stores the status for each parsed line (internal use only).
  26. */
  27. typedef enum _line_status_ {
  28. LINE_UNPROCESSED,
  29. LINE_ERROR,
  30. LINE_EMPTY,
  31. LINE_COMMENT,
  32. LINE_SECTION,
  33. LINE_VALUE
  34. } line_status ;
  35. /*-------------------------------------------------------------------------*/
  36. /**
  37. @brief Convert a string to lowercase.
  38. @param s String to convert.
  39. @return ptr to statically allocated string.
  40. This function returns a pointer to a statically allocated string
  41. containing a lowercased version of the input string. Do not free
  42. or modify the returned string! Since the returned string is statically
  43. allocated, it will be modified at each function call (not re-entrant).
  44. */
  45. /*--------------------------------------------------------------------------*/
  46. static char * strlwc(const char * s)
  47. {
  48. static char l[ASCIILINESZ+1];
  49. int i ;
  50. if (s==NULL) return NULL ;
  51. memset(l, 0, ASCIILINESZ+1);
  52. i=0 ;
  53. while (s[i] && i<ASCIILINESZ) {
  54. l[i] = (char)tolower((int)s[i]);
  55. i++ ;
  56. }
  57. l[ASCIILINESZ]=(char)0;
  58. return l ;
  59. }
  60. /*-------------------------------------------------------------------------*/
  61. /**
  62. @brief Remove blanks at the beginning and the end of a string.
  63. @param s String to parse.
  64. @return ptr to statically allocated string.
  65. This function returns a pointer to a statically allocated string,
  66. which is identical to the input string, except that all blank
  67. characters at the end and the beg. of the string have been removed.
  68. Do not free or modify the returned string! Since the returned string
  69. is statically allocated, it will be modified at each function call
  70. (not re-entrant).
  71. */
  72. /*--------------------------------------------------------------------------*/
  73. static char * strstrip(char * s)
  74. {
  75. static char l[ASCIILINESZ+1];
  76. char * last ;
  77. if (s==NULL) return NULL ;
  78. while (isspace((int)*s) && *s) s++;
  79. memset(l, 0, ASCIILINESZ+1);
  80. strcpy(l, s);
  81. last = l + strlen(l);
  82. while (last > l) {
  83. if (!isspace((int)*(last-1)))
  84. break ;
  85. last -- ;
  86. }
  87. *last = (char)0;
  88. return (char*)l ;
  89. }
  90. /*-------------------------------------------------------------------------*/
  91. /**
  92. @brief Get number of sections in a dictionary
  93. @param d Dictionary to examine
  94. @return int Number of sections found in dictionary
  95. This function returns the number of sections found in a dictionary.
  96. The test to recognize sections is done on the string stored in the
  97. dictionary: a section name is given as "section" whereas a key is
  98. stored as "section:key", thus the test looks for entries that do not
  99. contain a colon.
  100. This clearly fails in the case a section name contains a colon, but
  101. this should simply be avoided.
  102. This function returns -1 in case of error.
  103. */
  104. /*--------------------------------------------------------------------------*/
  105. int iniparser_getnsec(dictionary * d)
  106. {
  107. int i ;
  108. int nsec ;
  109. if (d==NULL) return -1 ;
  110. nsec=0 ;
  111. for (i=0 ; i<d->size ; i++) {
  112. if (d->key[i]==NULL)
  113. continue ;
  114. if (strchr(d->key[i], ':')==NULL) {
  115. nsec ++ ;
  116. }
  117. }
  118. return nsec ;
  119. }
  120. /*-------------------------------------------------------------------------*/
  121. /**
  122. @brief Get name for section n in a dictionary.
  123. @param d Dictionary to examine
  124. @param n Section number (from 0 to nsec-1).
  125. @return Pointer to char string
  126. This function locates the n-th section in a dictionary and returns
  127. its name as a pointer to a string statically allocated inside the
  128. dictionary. Do not free or modify the returned string!
  129. This function returns NULL in case of error.
  130. */
  131. /*--------------------------------------------------------------------------*/
  132. char * iniparser_getsecname(dictionary * d, int n)
  133. {
  134. int i ;
  135. int foundsec ;
  136. if (d==NULL || n<0) return NULL ;
  137. foundsec=0 ;
  138. for (i=0 ; i<d->size ; i++) {
  139. if (d->key[i]==NULL)
  140. continue ;
  141. if (strchr(d->key[i], ':')==NULL) {
  142. foundsec++ ;
  143. if (foundsec>n)
  144. break ;
  145. }
  146. }
  147. if (foundsec<=n) {
  148. return NULL ;
  149. }
  150. return d->key[i] ;
  151. }
  152. /*-------------------------------------------------------------------------*/
  153. /**
  154. @brief Dump a dictionary to an opened file pointer.
  155. @param d Dictionary to dump.
  156. @param f Opened file pointer to dump to.
  157. @return void
  158. This function prints out the contents of a dictionary, one element by
  159. line, onto the provided file pointer. It is OK to specify @c stderr
  160. or @c stdout as output files. This function is meant for debugging
  161. purposes mostly.
  162. */
  163. /*--------------------------------------------------------------------------*/
  164. void iniparser_dump(dictionary * d, FILE * f)
  165. {
  166. int i ;
  167. if (d==NULL || f==NULL) return ;
  168. for (i=0 ; i<d->size ; i++) {
  169. if (d->key[i]==NULL)
  170. continue ;
  171. if (d->val[i]!=NULL) {
  172. fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
  173. } else {
  174. fprintf(f, "[%s]=UNDEF\n", d->key[i]);
  175. }
  176. }
  177. return ;
  178. }
  179. /*-------------------------------------------------------------------------*/
  180. /**
  181. @brief Save a dictionary to a loadable ini file
  182. @param d Dictionary to dump
  183. @param f Opened file pointer to dump to
  184. @return void
  185. This function dumps a given dictionary into a loadable ini file.
  186. It is Ok to specify @c stderr or @c stdout as output files.
  187. */
  188. /*--------------------------------------------------------------------------*/
  189. void iniparser_dump_ini(dictionary * d, FILE * f)
  190. {
  191. int i, j ;
  192. char keym[ASCIILINESZ+1];
  193. int nsec ;
  194. char * secname ;
  195. int seclen ;
  196. if (d==NULL || f==NULL) return ;
  197. nsec = iniparser_getnsec(d);
  198. if (nsec<1) {
  199. /* No section in file: dump all keys as they are */
  200. for (i=0 ; i<d->size ; i++) {
  201. if (d->key[i]==NULL)
  202. continue ;
  203. fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
  204. }
  205. return ;
  206. }
  207. for (i=0 ; i<nsec ; i++) {
  208. secname = iniparser_getsecname(d, i) ;
  209. seclen = (int)strlen(secname);
  210. fprintf(f, "\n[%s]\n", secname);
  211. sprintf(keym, "%s:", secname);
  212. for (j=0 ; j<d->size ; j++) {
  213. if (d->key[j]==NULL)
  214. continue ;
  215. if (!strncmp(d->key[j], keym, seclen+1)) {
  216. fprintf(f,
  217. "%-30s = %s\n",
  218. d->key[j]+seclen+1,
  219. d->val[j] ? d->val[j] : "");
  220. }
  221. }
  222. }
  223. fprintf(f, "\n");
  224. return ;
  225. }
  226. /*-------------------------------------------------------------------------*/
  227. /**
  228. @brief Get the string associated to a key
  229. @param d Dictionary to search
  230. @param key Key string to look for
  231. @param def Default value to return if key not found.
  232. @return pointer to statically allocated character string
  233. This function queries a dictionary for a key. A key as read from an
  234. ini file is given as "section:key". If the key cannot be found,
  235. the pointer passed as 'def' is returned.
  236. The returned char pointer is pointing to a string allocated in
  237. the dictionary, do not free or modify it.
  238. */
  239. /*--------------------------------------------------------------------------*/
  240. char * iniparser_getstring(dictionary * d, const char * key, char * def)
  241. {
  242. char * lc_key ;
  243. char * sval ;
  244. if (d==NULL || key==NULL)
  245. return def ;
  246. lc_key = strlwc(key);
  247. sval = dictionary_get(d, lc_key, def);
  248. return sval ;
  249. }
  250. /*-------------------------------------------------------------------------*/
  251. /**
  252. @brief Get the string associated to a key, convert to an int
  253. @param d Dictionary to search
  254. @param key Key string to look for
  255. @param notfound Value to return in case of error
  256. @return integer
  257. This function queries a dictionary for a key. A key as read from an
  258. ini file is given as "section:key". If the key cannot be found,
  259. the notfound value is returned.
  260. Supported values for integers include the usual C notation
  261. so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
  262. are supported. Examples:
  263. "42" -> 42
  264. "042" -> 34 (octal -> decimal)
  265. "0x42" -> 66 (hexa -> decimal)
  266. Warning: the conversion may overflow in various ways. Conversion is
  267. totally outsourced to strtol(), see the associated man page for overflow
  268. handling.
  269. Credits: Thanks to A. Becker for suggesting strtol()
  270. */
  271. /*--------------------------------------------------------------------------*/
  272. int iniparser_getint(dictionary * d, const char * key, int notfound)
  273. {
  274. char * str ;
  275. str = iniparser_getstring(d, key, INI_INVALID_KEY);
  276. if (str==INI_INVALID_KEY) return notfound ;
  277. return (int)strtol(str, NULL, 0);
  278. }
  279. /*-------------------------------------------------------------------------*/
  280. /**
  281. @brief Get the string associated to a key, convert to a boolean
  282. @param d Dictionary to search
  283. @param key Key string to look for
  284. @param notfound Value to return in case of error
  285. @return integer
  286. This function queries a dictionary for a key. A key as read from an
  287. ini file is given as "section:key". If the key cannot be found,
  288. the notfound value is returned.
  289. A true boolean is found if one of the following is matched:
  290. - A string starting with 'y'
  291. - A string starting with 'Y'
  292. - A string starting with 't'
  293. - A string starting with 'T'
  294. - A string starting with '1'
  295. A false boolean is found if one of the following is matched:
  296. - A string starting with 'n'
  297. - A string starting with 'N'
  298. - A string starting with 'f'
  299. - A string starting with 'F'
  300. - A string starting with '0'
  301. The notfound value returned if no boolean is identified, does not
  302. necessarily have to be 0 or 1.
  303. */
  304. /*--------------------------------------------------------------------------*/
  305. int iniparser_getboolean(dictionary * d, const char * key, int notfound)
  306. {
  307. char * c ;
  308. int ret ;
  309. c = iniparser_getstring(d, key, INI_INVALID_KEY);
  310. if (c==INI_INVALID_KEY) return notfound ;
  311. if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
  312. ret = 1 ;
  313. } else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
  314. ret = 0 ;
  315. } else {
  316. ret = notfound ;
  317. }
  318. return ret;
  319. }
  320. /*-------------------------------------------------------------------------*/
  321. /**
  322. @brief Finds out if a given entry exists in a dictionary
  323. @param ini Dictionary to search
  324. @param entry Name of the entry to look for
  325. @return integer 1 if entry exists, 0 otherwise
  326. Finds out if a given entry exists in the dictionary. Since sections
  327. are stored as keys with NULL associated values, this is the only way
  328. of querying for the presence of sections in a dictionary.
  329. */
  330. /*--------------------------------------------------------------------------*/
  331. int iniparser_find_entry(
  332. dictionary * ini,
  333. char * entry
  334. )
  335. {
  336. int found=0 ;
  337. if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
  338. found = 1 ;
  339. }
  340. return found ;
  341. }
  342. /*-------------------------------------------------------------------------*/
  343. /**
  344. @brief Delete an entry in a dictionary
  345. @param ini Dictionary to modify
  346. @param entry Entry to delete (entry name)
  347. @return void
  348. If the given entry can be found, it is deleted from the dictionary.
  349. */
  350. /*--------------------------------------------------------------------------*/
  351. void iniparser_unset(dictionary * ini, char * entry)
  352. {
  353. dictionary_unset(ini, strlwc(entry));
  354. }
  355. /*-------------------------------------------------------------------------*/
  356. /**
  357. @brief Load a single line from an INI file
  358. @param input_line Input line, may be concatenated multi-line input
  359. @param section Output space to store section
  360. @param key Output space to store key
  361. @param value Output space to store value
  362. @return line_status value
  363. */
  364. /*--------------------------------------------------------------------------*/
  365. static line_status iniparser_line(
  366. char * input_line,
  367. char * section,
  368. char * key,
  369. char * value)
  370. {
  371. line_status sta ;
  372. char line[ASCIILINESZ+1];
  373. int len ;
  374. strcpy(line, strstrip(input_line));
  375. len = (int)strlen(line);
  376. sta = LINE_UNPROCESSED ;
  377. if (len<1) {
  378. /* Empty line */
  379. sta = LINE_EMPTY ;
  380. } else if (line[0]=='#') {
  381. /* Comment line */
  382. sta = LINE_COMMENT ;
  383. } else if (line[0]=='[' && line[len-1]==']') {
  384. /* Section name */
  385. sscanf(line, "[%[^]]", section);
  386. strcpy(section, strstrip(section));
  387. strcpy(section, strlwc(section));
  388. sta = LINE_SECTION ;
  389. } else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2
  390. || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
  391. || sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
  392. /* Usual key=value, with or without comments */
  393. strcpy(key, strstrip(key));
  394. strcpy(key, strlwc(key));
  395. strcpy(value, strstrip(value));
  396. /*
  397. * sscanf cannot handle '' or "" as empty values
  398. * this is done here
  399. */
  400. if (!strcmp(value, "\"\"") || (!strcmp(value, "''"))) {
  401. value[0]=0 ;
  402. }
  403. sta = LINE_VALUE ;
  404. } else if (sscanf(line, "%[^=] = %[;#]", key, value)==2
  405. || sscanf(line, "%[^=] %[=]", key, value) == 2) {
  406. /*
  407. * Special cases:
  408. * key=
  409. * key=;
  410. * key=#
  411. */
  412. strcpy(key, strstrip(key));
  413. strcpy(key, strlwc(key));
  414. value[0]=0 ;
  415. sta = LINE_VALUE ;
  416. } else {
  417. /* Generate syntax error */
  418. sta = LINE_ERROR ;
  419. }
  420. return sta ;
  421. }
  422. /*-------------------------------------------------------------------------*/
  423. /**
  424. @brief Parse an ini file and return an allocated dictionary object
  425. @param ininame Name of the ini file to read.
  426. @return Pointer to newly allocated dictionary
  427. This is the parser for ini files. This function is called, providing
  428. the name of the file to be read. It returns a dictionary object that
  429. should not be accessed directly, but through accessor functions
  430. instead.
  431. The returned dictionary must be freed using iniparser_freedict().
  432. */
  433. /*--------------------------------------------------------------------------*/
  434. dictionary * iniparser_load(const char * ininame)
  435. {
  436. FILE * in ;
  437. char line [ASCIILINESZ+1] ;
  438. char section [ASCIILINESZ+1] ;
  439. char key [ASCIILINESZ+1] ;
  440. char tmp [ASCIILINESZ+1] ;
  441. char val [ASCIILINESZ+1] ;
  442. int last=0 ;
  443. int len ;
  444. int lineno=0 ;
  445. int errs=0;
  446. dictionary * dict ;
  447. if ((in=fopen(ininame, "r"))==NULL) {
  448. fprintf(stderr, "iniparser: cannot open %s\n", ininame);
  449. return NULL ;
  450. }
  451. dict = dictionary_new(0) ;
  452. if (!dict) {
  453. fclose(in);
  454. return NULL ;
  455. }
  456. memset(line, 0, ASCIILINESZ);
  457. memset(section, 0, ASCIILINESZ);
  458. memset(key, 0, ASCIILINESZ);
  459. memset(val, 0, ASCIILINESZ);
  460. last=0 ;
  461. while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
  462. lineno++ ;
  463. len = (int)strlen(line)-1;
  464. /* Safety check against buffer overflows */
  465. if (line[len]!='\n') {
  466. fprintf(stderr,
  467. "iniparser: input line too long in %s (%d)\n",
  468. ininame,
  469. lineno);
  470. dictionary_del(dict);
  471. fclose(in);
  472. return NULL ;
  473. }
  474. /* Get rid of \n and spaces at end of line */
  475. while ((len>=0) &&
  476. ((line[len]=='\n') || (isspace(line[len])))) {
  477. line[len]=0 ;
  478. len-- ;
  479. }
  480. /* Detect multi-line */
  481. if (line[len]=='\\') {
  482. /* Multi-line value */
  483. last=len ;
  484. continue ;
  485. } else {
  486. last=0 ;
  487. }
  488. switch (iniparser_line(line, section, key, val)) {
  489. case LINE_EMPTY:
  490. case LINE_COMMENT:
  491. break ;
  492. case LINE_SECTION:
  493. errs = dictionary_set(dict, section, NULL);
  494. break ;
  495. case LINE_VALUE:
  496. sprintf(tmp, "%s:%s", section, key);
  497. errs = dictionary_set(dict, tmp, val) ;
  498. break ;
  499. case LINE_ERROR:
  500. fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
  501. ininame,
  502. lineno);
  503. fprintf(stderr, "-> %s\n", line);
  504. errs++ ;
  505. break;
  506. default:
  507. break ;
  508. }
  509. memset(line, 0, ASCIILINESZ);
  510. last=0;
  511. if (errs<0) {
  512. fprintf(stderr, "iniparser: memory allocation failure\n");
  513. break ;
  514. }
  515. }
  516. if (errs) {
  517. dictionary_del(dict);
  518. dict = NULL ;
  519. }
  520. fclose(in);
  521. return dict ;
  522. }
  523. /*-------------------------------------------------------------------------*/
  524. /**
  525. @brief Free all memory associated to an ini dictionary
  526. @param d Dictionary to free
  527. @return void
  528. Free all memory associated to an ini dictionary.
  529. It is mandatory to call this function before the dictionary object
  530. gets out of the current context.
  531. */
  532. /*--------------------------------------------------------------------------*/
  533. void iniparser_freedict(dictionary * d)
  534. {
  535. dictionary_del(d);
  536. }
  537. /* vim: set ts=4 et sw=4 tw=75 */