iniparser.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Kamailio LDAP Module
  3. *
  4. * Copyright (C) 2007 University of North Carolina
  5. *
  6. * Original author: Christian Schlatter, [email protected]
  7. *
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. /*
  27. Based upon libiniparser, by Nicolas Devillard
  28. Hacked into 1 file (m-iniparser) by Freek/2005
  29. Original terms following:
  30. -- -
  31. Copyright (c) 2000 by Nicolas Devillard (ndevilla AT free DOT fr).
  32. Written by Nicolas Devillard. Not derived from licensed software.
  33. Permission is granted to anyone to use this software for any
  34. purpose on any computer system, and to redistribute it freely,
  35. subject to the following restrictions:
  36. 1. The author is not responsible for the consequences of use of
  37. this software, no matter how awful, even if they arise
  38. from defects in it.
  39. 2. The origin of this software must not be misrepresented, either
  40. by explicit claim or by omission.
  41. 3. Altered versions must be plainly marked as such, and must not
  42. be misrepresented as being the original software.
  43. 4. This notice may not be removed or altered.
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include "iniparser.h"
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /* strlib.c following */
  54. #define ASCIILINESZ 1024
  55. /*-------------------------------------------------------------------------*/
  56. /**
  57. @brief Convert a string to lowercase.
  58. @param s String to convert.
  59. @return ptr to statically allocated string.
  60. This function returns a pointer to a statically allocated string
  61. containing a lowercased version of the input string. Do not free
  62. or modify the returned string! Since the returned string is statically
  63. allocated, it will be modified at each function call (not re-entrant).
  64. */
  65. /*--------------------------------------------------------------------------*/
  66. static char * strlwc(char * s)
  67. {
  68. static char l[ASCIILINESZ+1];
  69. int i ;
  70. if (s==NULL) return NULL ;
  71. memset(l, 0, ASCIILINESZ+1);
  72. i=0 ;
  73. while (s[i] && i<ASCIILINESZ) {
  74. l[i] = (char)tolower((int)s[i]);
  75. i++ ;
  76. }
  77. l[ASCIILINESZ]=(char)0;
  78. return l ;
  79. }
  80. /*-------------------------------------------------------------------------*/
  81. /**
  82. @brief Convert a string to uppercase.
  83. @param s String to convert.
  84. @return ptr to statically allocated string.
  85. This function returns a pointer to a statically allocated string
  86. containing an uppercased version of the input string. Do not free
  87. or modify the returned string! Since the returned string is statically
  88. allocated, it will be modified at each function call (not re-entrant).
  89. */
  90. /*--------------------------------------------------------------------------*/
  91. /*
  92. static char * strupc(char * s)
  93. {
  94. static char l[ASCIILINESZ+1];
  95. int i ;
  96. if (s==NULL) return NULL ;
  97. memset(l, 0, ASCIILINESZ+1);
  98. i=0 ;
  99. while (s[i] && i<ASCIILINESZ) {
  100. l[i] = (char)toupper((int)s[i]);
  101. i++ ;
  102. }
  103. l[ASCIILINESZ]=(char)0;
  104. return l ;
  105. }
  106. */
  107. /*-------------------------------------------------------------------------*/
  108. /**
  109. @brief Skip blanks until the first non-blank character.
  110. @param s String to parse.
  111. @return Pointer to char inside given string.
  112. This function returns a pointer to the first non-blank character in the
  113. given string.
  114. */
  115. /*--------------------------------------------------------------------------*/
  116. static char * strskp(char * s)
  117. {
  118. char * skip = s;
  119. if (s==NULL) return NULL ;
  120. while (isspace((int)*skip) && *skip) skip++;
  121. return skip ;
  122. }
  123. /*-------------------------------------------------------------------------*/
  124. /**
  125. @brief Remove blanks at the end of a string.
  126. @param s String to parse.
  127. @return ptr to statically allocated string.
  128. This function returns a pointer to a statically allocated string,
  129. which is identical to the input string, except that all blank
  130. characters at the end of the string have been removed.
  131. Do not free or modify the returned string! Since the returned string
  132. is statically allocated, it will be modified at each function call
  133. (not re-entrant).
  134. */
  135. /*--------------------------------------------------------------------------*/
  136. static char * strcrop(char * s)
  137. {
  138. static char l[ASCIILINESZ+1];
  139. char * last ;
  140. if (s==NULL) return NULL ;
  141. memset(l, 0, ASCIILINESZ+1);
  142. strcpy(l, s);
  143. last = l + strlen(l);
  144. while (last > l) {
  145. if (!isspace((int)*(last-1)))
  146. break ;
  147. last -- ;
  148. }
  149. *last = (char)0;
  150. return l ;
  151. }
  152. /*-------------------------------------------------------------------------*/
  153. /**
  154. @brief Remove blanks at the beginning and the end of a string.
  155. @param s String to parse.
  156. @return ptr to statically allocated string.
  157. This function returns a pointer to a statically allocated string,
  158. which is identical to the input string, except that all blank
  159. characters at the end and the beg. of the string have been removed.
  160. Do not free or modify the returned string! Since the returned string
  161. is statically allocated, it will be modified at each function call
  162. (not re-entrant).
  163. */
  164. /*--------------------------------------------------------------------------*/
  165. /*
  166. static char * strstrip(char * s)
  167. {
  168. static char l[ASCIILINESZ+1];
  169. char * last ;
  170. if (s==NULL) return NULL ;
  171. while (isspace((int)*s) && *s) s++;
  172. memset(l, 0, ASCIILINESZ+1);
  173. strcpy(l, s);
  174. last = l + strlen(l);
  175. while (last > l) {
  176. if (!isspace((int)*(last-1)))
  177. break ;
  178. last -- ;
  179. }
  180. *last = (char)0;
  181. return (char*)l ;
  182. }
  183. */
  184. /* dictionary.c.c following */
  185. /** Maximum value size for integers and doubles. */
  186. #define MAXVALSZ 1024
  187. /** Minimal allocated number of entries in a dictionary */
  188. #define DICTMINSZ 128
  189. /** Invalid key token */
  190. #define DICT_INVALID_KEY ((char*)-1)
  191. /*
  192. Doubles the allocated size associated to a pointer
  193. 'size' is the current allocated size.
  194. */
  195. static void * mem_double(void * ptr, int size)
  196. {
  197. void *newptr;
  198. newptr = calloc(2*size, 1);
  199. memcpy(newptr, ptr, size);
  200. free(ptr);
  201. return newptr ;
  202. }
  203. /*---------------------------------------------------------------------------
  204. Function codes
  205. ---------------------------------------------------------------------------*/
  206. /*-------------------------------------------------------------------------*/
  207. /**
  208. @brief Compute the hash key for a string.
  209. @param key Character string to use for key.
  210. @return 1 unsigned int on at least 32 bits.
  211. This hash function has been taken from an Article in Dr Dobbs Journal.
  212. This is normally a collision-free function, distributing keys evenly.
  213. The key is stored anyway in the struct so that collision can be avoided
  214. by comparing the key itself in last resort.
  215. */
  216. /*--------------------------------------------------------------------------*/
  217. static unsigned dictionary_hash(char * key)
  218. {
  219. int len ;
  220. unsigned hash ;
  221. int i ;
  222. len = strlen(key);
  223. for (hash=0, i=0 ; i<len ; i++) {
  224. hash += (unsigned)key[i] ;
  225. hash += (hash<<10);
  226. hash ^= (hash>>6) ;
  227. }
  228. hash += (hash <<3);
  229. hash ^= (hash >>11);
  230. hash += (hash <<15);
  231. return hash ;
  232. }
  233. /*-------------------------------------------------------------------------*/
  234. /**
  235. @brief Create a new dictionary object.
  236. @param size Optional initial size of the dictionary.
  237. @return 1 newly allocated dictionary objet.
  238. This function allocates a new dictionary object of given size and returns
  239. it. If you do not know in advance (roughly) the number of entries in the
  240. dictionary, give size=0.
  241. */
  242. /*--------------------------------------------------------------------------*/
  243. static dictionary * dictionary_new(int size)
  244. {
  245. dictionary *d ;
  246. /* If no size was specified, allocate space for DICTMINSZ */
  247. if (size<DICTMINSZ) size=DICTMINSZ ;
  248. d = (dictionary *)calloc(1, sizeof(dictionary));
  249. d->size = size ;
  250. d->val = (char **)calloc(size, sizeof(char*));
  251. d->key = (char **)calloc(size, sizeof(char*));
  252. d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
  253. return d;
  254. }
  255. /*-------------------------------------------------------------------------*/
  256. /**
  257. @brief Delete a dictionary object
  258. @param d dictionary object to deallocate.
  259. @return void
  260. Deallocate a dictionary object and all memory associated to it.
  261. */
  262. /*--------------------------------------------------------------------------*/
  263. static void dictionary_del(dictionary * d)
  264. {
  265. int i ;
  266. if (d==NULL) return ;
  267. for (i=0 ; i<d->size ; i++) {
  268. if (d->key[i]!=NULL)
  269. free(d->key[i]);
  270. if (d->val[i]!=NULL)
  271. free(d->val[i]);
  272. }
  273. free(d->val);
  274. free(d->key);
  275. free(d->hash);
  276. free(d);
  277. return;
  278. }
  279. /*-------------------------------------------------------------------------*/
  280. /**
  281. @brief Get a value from a dictionary.
  282. @param d dictionary object to search.
  283. @param key Key to look for in the dictionary.
  284. @param def Default value to return if key not found.
  285. @return 1 pointer to internally allocated character string.
  286. This function locates a key in a dictionary and returns a pointer to its
  287. value, or the passed 'def' pointer if no such key can be found in
  288. dictionary. The returned character pointer points to data internal to the
  289. dictionary object, you should not try to free it or modify it.
  290. */
  291. /*--------------------------------------------------------------------------*/
  292. static char * dictionary_get(dictionary * d, char * key, char * def)
  293. {
  294. unsigned hash ;
  295. int i ;
  296. hash = dictionary_hash(key);
  297. for (i=0 ; i<d->size ; i++) {
  298. if (d->key==NULL)
  299. continue ;
  300. /* Compare hash */
  301. if (hash==d->hash[i]) {
  302. /* Compare string, to avoid hash collisions */
  303. if (!strcmp(key, d->key[i])) {
  304. return d->val[i] ;
  305. }
  306. }
  307. }
  308. return def ;
  309. }
  310. /*-------------------------------------------------------------------------*/
  311. /**
  312. @brief Set a value in a dictionary.
  313. @param d dictionary object to modify.
  314. @param key Key to modify or add.
  315. @param val Value to add.
  316. @return void
  317. If the given key is found in the dictionary, the associated value is
  318. replaced by the provided one. If the key cannot be found in the
  319. dictionary, it is added to it.
  320. It is Ok to provide a NULL value for val, but NULL values for the dictionary
  321. or the key are considered as errors: the function will return immediately
  322. in such a case.
  323. Notice that if you dictionary_set a variable to NULL, a call to
  324. dictionary_get will return a NULL value: the variable will be found, and
  325. its value (NULL) is returned. In other words, setting the variable
  326. content to NULL is equivalent to deleting the variable from the
  327. dictionary. It is not possible (in this implementation) to have a key in
  328. the dictionary without value.
  329. */
  330. /*--------------------------------------------------------------------------*/
  331. static void dictionary_set(dictionary * d, char * key, char * val)
  332. {
  333. int i ;
  334. unsigned hash ;
  335. if (d==NULL || key==NULL) return ;
  336. /* Compute hash for this key */
  337. hash = dictionary_hash(key) ;
  338. /* Find if value is already in blackboard */
  339. if (d->n>0) {
  340. for (i=0 ; i<d->size ; i++) {
  341. if (d->key[i]==NULL)
  342. continue ;
  343. if (hash==d->hash[i]) { /* Same hash value */
  344. if (!strcmp(key, d->key[i])) { /* Same key */
  345. /* Found a value: modify and return */
  346. if (d->val[i]!=NULL)
  347. free(d->val[i]);
  348. d->val[i] = val ? strdup(val) : NULL ;
  349. /* Value has been modified: return */
  350. return ;
  351. }
  352. }
  353. }
  354. }
  355. /* Add a new value */
  356. /* See if dictionary needs to grow */
  357. if (d->n==d->size) {
  358. /* Reached maximum size: reallocate blackboard */
  359. d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
  360. d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
  361. d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
  362. /* Double size */
  363. d->size *= 2 ;
  364. }
  365. /* Insert key in the first empty slot */
  366. for (i=0 ; i<d->size ; i++) {
  367. if (d->key[i]==NULL) {
  368. /* Add key here */
  369. break ;
  370. }
  371. }
  372. /* Copy key */
  373. d->key[i] = strdup(key);
  374. d->val[i] = val ? strdup(val) : NULL ;
  375. d->hash[i] = hash;
  376. d->n ++ ;
  377. return ;
  378. }
  379. /*-------------------------------------------------------------------------*/
  380. /**
  381. @brief Delete a key in a dictionary
  382. @param d dictionary object to modify.
  383. @param key Key to remove.
  384. @return void
  385. This function deletes a key in a dictionary. Nothing is done if the
  386. key cannot be found.
  387. */
  388. /*--------------------------------------------------------------------------*/
  389. static void dictionary_unset(dictionary * d, char * key)
  390. {
  391. unsigned hash ;
  392. int i ;
  393. hash = dictionary_hash(key);
  394. for (i=0 ; i<d->size ; i++) {
  395. if (d->key[i]==NULL)
  396. continue ;
  397. /* Compare hash */
  398. if (hash==d->hash[i]) {
  399. /* Compare string, to avoid hash collisions */
  400. if (!strcmp(key, d->key[i])) {
  401. /* Found key */
  402. break ;
  403. }
  404. }
  405. }
  406. if (i>=d->size)
  407. /* Key not found */
  408. return ;
  409. free(d->key[i]);
  410. d->key[i] = NULL ;
  411. if (d->val[i]!=NULL) {
  412. free(d->val[i]);
  413. d->val[i] = NULL ;
  414. }
  415. d->hash[i] = 0 ;
  416. d->n -- ;
  417. return ;
  418. }
  419. /*-------------------------------------------------------------------------*/
  420. /**
  421. @brief Dump a dictionary to an opened file pointer.
  422. @param d Dictionary to dump
  423. @param f Opened file pointer.
  424. @return void
  425. Dumps a dictionary onto an opened file pointer. Key pairs are printed out
  426. as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
  427. output file pointers.
  428. */
  429. /*--------------------------------------------------------------------------*/
  430. static void dictionary_dump(dictionary *d, FILE *f)
  431. {
  432. int i;
  433. if (d==NULL || f==NULL) return;
  434. for (i=0; i<d->size; i++) {
  435. if (d->key[i] == NULL)
  436. continue ;
  437. if (d->val[i] != NULL) {
  438. fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
  439. } else {
  440. fprintf(f, "[%s]=UNDEF\n", d->key[i]);
  441. }
  442. }
  443. return;
  444. }
  445. /* iniparser.c.c following */
  446. #define ASCIILINESZ 1024
  447. #define INI_INVALID_KEY ((char*)-1)
  448. /* Private: add an entry to the dictionary */
  449. static void iniparser_add_entry(
  450. dictionary * d,
  451. char * sec,
  452. char * key,
  453. char * val)
  454. {
  455. char longkey[2*ASCIILINESZ+1];
  456. /* Make a key as section:keyword */
  457. if (key!=NULL) {
  458. sprintf(longkey, "%s:%s", sec, key);
  459. } else {
  460. strcpy(longkey, sec);
  461. }
  462. /* Add (key,val) to dictionary */
  463. dictionary_set(d, longkey, val);
  464. return ;
  465. }
  466. /*-------------------------------------------------------------------------*/
  467. /**
  468. @brief Get number of sections in a dictionary
  469. @param d Dictionary to examine
  470. @return int Number of sections found in dictionary
  471. This function returns the number of sections found in a dictionary.
  472. The test to recognize sections is done on the string stored in the
  473. dictionary: a section name is given as "section" whereas a key is
  474. stored as "section:key", thus the test looks for entries that do not
  475. contain a colon.
  476. This clearly fails in the case a section name contains a colon, but
  477. this should simply be avoided.
  478. This function returns -1 in case of error.
  479. */
  480. /*--------------------------------------------------------------------------*/
  481. int iniparser_getnsec(dictionary * d)
  482. {
  483. int i ;
  484. int nsec ;
  485. if (d==NULL) return -1 ;
  486. nsec=0 ;
  487. for (i=0 ; i<d->size ; i++) {
  488. if (d->key[i]==NULL)
  489. continue ;
  490. if (strchr(d->key[i], ':')==NULL) {
  491. nsec ++ ;
  492. }
  493. }
  494. return nsec ;
  495. }
  496. /*-------------------------------------------------------------------------*/
  497. /**
  498. @brief Get name for section n in a dictionary.
  499. @param d Dictionary to examine
  500. @param n Section number (from 0 to nsec-1).
  501. @return Pointer to char string
  502. This function locates the n-th section in a dictionary and returns
  503. its name as a pointer to a string statically allocated inside the
  504. dictionary. Do not free or modify the returned string!
  505. This function returns NULL in case of error.
  506. */
  507. /*--------------------------------------------------------------------------*/
  508. char * iniparser_getsecname(dictionary * d, int n)
  509. {
  510. int i ;
  511. int foundsec ;
  512. if (d==NULL || n<0) return NULL ;
  513. foundsec=0 ;
  514. for (i=0 ; i<d->size ; i++) {
  515. if (d->key[i]==NULL)
  516. continue ;
  517. if (strchr(d->key[i], ':')==NULL) {
  518. foundsec++ ;
  519. if (foundsec>n)
  520. break ;
  521. }
  522. }
  523. if (foundsec<=n) {
  524. return NULL ;
  525. }
  526. return d->key[i] ;
  527. }
  528. /*-------------------------------------------------------------------------*/
  529. /**
  530. @brief Dump a dictionary to an opened file pointer.
  531. @param d Dictionary to dump.
  532. @param f Opened file pointer to dump to.
  533. @return void
  534. This function prints out the contents of a dictionary, one element by
  535. line, onto the provided file pointer. It is OK to specify @c stderr
  536. or @c stdout as output files. This function is meant for debugging
  537. purposes mostly.
  538. */
  539. /*--------------------------------------------------------------------------*/
  540. void iniparser_dump(dictionary * d, FILE * f)
  541. {
  542. dictionary_dump(d,f);
  543. }
  544. /*-------------------------------------------------------------------------*/
  545. /**
  546. @brief Save a dictionary to a loadable ini file
  547. @param d Dictionary to dump
  548. @param f Opened file pointer to dump to
  549. @return void
  550. This function dumps a given dictionary into a loadable ini file.
  551. It is Ok to specify @c stderr or @c stdout as output files.
  552. */
  553. /*--------------------------------------------------------------------------*/
  554. void iniparser_dump_ini(dictionary * d, FILE * f)
  555. {
  556. int i, j ;
  557. char keym[ASCIILINESZ+1];
  558. int nsec ;
  559. char * secname ;
  560. int seclen ;
  561. if (d==NULL || f==NULL) return ;
  562. nsec = iniparser_getnsec(d);
  563. if (nsec<1) {
  564. /* No section in file: dump all keys as they are */
  565. for (i=0 ; i<d->size ; i++) {
  566. if (d->key[i]==NULL)
  567. continue ;
  568. fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
  569. }
  570. return ;
  571. }
  572. for (i=0 ; i<nsec ; i++) {
  573. secname = iniparser_getsecname(d, i) ;
  574. seclen = (int)strlen(secname);
  575. fprintf(f, "\n[%s]\n", secname);
  576. sprintf(keym, "%s:", secname);
  577. for (j=0 ; j<d->size ; j++) {
  578. if (d->key[j]==NULL)
  579. continue ;
  580. if (!strncmp(d->key[j], keym, seclen+1)) {
  581. fprintf(f,
  582. "%-30s = %s\n",
  583. d->key[j]+seclen+1,
  584. d->val[j] ? d->val[j] : "");
  585. }
  586. }
  587. }
  588. fprintf(f, "\n");
  589. return ;
  590. }
  591. /*-------------------------------------------------------------------------*/
  592. /**
  593. @brief Get the string associated to a key, return NULL if not found
  594. @param d Dictionary to search
  595. @param key Key string to look for
  596. @return pointer to statically allocated character string, or NULL.
  597. This function queries a dictionary for a key. A key as read from an
  598. ini file is given as "section:key". If the key cannot be found,
  599. NULL is returned.
  600. The returned char pointer is pointing to a string allocated in
  601. the dictionary, do not free or modify it.
  602. This function is only provided for backwards compatibility with
  603. previous versions of iniparser. It is recommended to use
  604. iniparser_getstring() instead.
  605. */
  606. /*--------------------------------------------------------------------------*/
  607. char * iniparser_getstr(dictionary * d, char * key)
  608. {
  609. return iniparser_getstring(d, key, NULL);
  610. }
  611. /*-------------------------------------------------------------------------*/
  612. /**
  613. @brief Get the string associated to a key
  614. @param d Dictionary to search
  615. @param key Key string to look for
  616. @param def Default value to return if key not found.
  617. @return pointer to statically allocated character string
  618. This function queries a dictionary for a key. A key as read from an
  619. ini file is given as "section:key". If the key cannot be found,
  620. the pointer passed as 'def' is returned.
  621. The returned char pointer is pointing to a string allocated in
  622. the dictionary, do not free or modify it.
  623. */
  624. /*--------------------------------------------------------------------------*/
  625. char * iniparser_getstring(dictionary * d, char * key, char * def)
  626. {
  627. char * lc_key ;
  628. char * sval ;
  629. if (d==NULL || key==NULL)
  630. return def ;
  631. lc_key = strdup(strlwc(key));
  632. sval = dictionary_get(d, lc_key, def);
  633. free(lc_key);
  634. return sval ;
  635. }
  636. /*-------------------------------------------------------------------------*/
  637. /**
  638. @brief Get the string associated to a key, convert to an int
  639. @param d Dictionary to search
  640. @param key Key string to look for
  641. @param notfound Value to return in case of error
  642. @return integer
  643. This function queries a dictionary for a key. A key as read from an
  644. ini file is given as "section:key". If the key cannot be found,
  645. the notfound value is returned.
  646. */
  647. /*--------------------------------------------------------------------------*/
  648. int iniparser_getint(dictionary * d, char * key, int notfound)
  649. {
  650. char * str ;
  651. str = iniparser_getstring(d, key, INI_INVALID_KEY);
  652. if (str==INI_INVALID_KEY) return notfound ;
  653. return atoi(str);
  654. }
  655. /*-------------------------------------------------------------------------*/
  656. /**
  657. @brief Get the string associated to a key, convert to a double
  658. @param d Dictionary to search
  659. @param key Key string to look for
  660. @param notfound Value to return in case of error
  661. @return double
  662. This function queries a dictionary for a key. A key as read from an
  663. ini file is given as "section:key". If the key cannot be found,
  664. the notfound value is returned.
  665. */
  666. /*--------------------------------------------------------------------------*/
  667. double iniparser_getdouble(dictionary * d, char * key, double notfound)
  668. {
  669. char * str ;
  670. str = iniparser_getstring(d, key, INI_INVALID_KEY);
  671. if (str==INI_INVALID_KEY) return notfound ;
  672. return atof(str);
  673. }
  674. /*-------------------------------------------------------------------------*/
  675. /**
  676. @brief Get the string associated to a key, convert to a boolean
  677. @param d Dictionary to search
  678. @param key Key string to look for
  679. @param notfound Value to return in case of error
  680. @return integer
  681. This function queries a dictionary for a key. A key as read from an
  682. ini file is given as "section:key". If the key cannot be found,
  683. the notfound value is returned.
  684. A true boolean is found if one of the following is matched:
  685. - A string starting with 'y'
  686. - A string starting with 'Y'
  687. - A string starting with 't'
  688. - A string starting with 'T'
  689. - A string starting with '1'
  690. A false boolean is found if one of the following is matched:
  691. - A string starting with 'n'
  692. - A string starting with 'N'
  693. - A string starting with 'f'
  694. - A string starting with 'F'
  695. - A string starting with '0'
  696. The notfound value returned if no boolean is identified, does not
  697. necessarily have to be 0 or 1.
  698. */
  699. /*--------------------------------------------------------------------------*/
  700. int iniparser_getboolean(dictionary * d, char * key, int notfound)
  701. {
  702. char * c ;
  703. int ret ;
  704. c = iniparser_getstring(d, key, INI_INVALID_KEY);
  705. if (c==INI_INVALID_KEY) return notfound ;
  706. if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
  707. ret = 1 ;
  708. } else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
  709. ret = 0 ;
  710. } else {
  711. ret = notfound ;
  712. }
  713. return ret;
  714. }
  715. /*-------------------------------------------------------------------------*/
  716. /**
  717. @brief Finds out if a given entry exists in a dictionary
  718. @param ini Dictionary to search
  719. @param entry Name of the entry to look for
  720. @return integer 1 if entry exists, 0 otherwise
  721. Finds out if a given entry exists in the dictionary. Since sections
  722. are stored as keys with NULL associated values, this is the only way
  723. of querying for the presence of sections in a dictionary.
  724. */
  725. /*--------------------------------------------------------------------------*/
  726. int iniparser_find_entry(
  727. dictionary * ini,
  728. char * entry
  729. )
  730. {
  731. int found=0 ;
  732. if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
  733. found = 1 ;
  734. }
  735. return found ;
  736. }
  737. /*-------------------------------------------------------------------------*/
  738. /**
  739. @brief Set an entry in a dictionary.
  740. @param ini Dictionary to modify.
  741. @param entry Entry to modify (entry name)
  742. @param val New value to associate to the entry.
  743. @return int 0 if Ok, -1 otherwise.
  744. If the given entry can be found in the dictionary, it is modified to
  745. contain the provided value. If it cannot be found, -1 is returned.
  746. It is Ok to set val to NULL.
  747. */
  748. /*--------------------------------------------------------------------------*/
  749. int iniparser_setstr(dictionary * ini, char * entry, char * val)
  750. {
  751. dictionary_set(ini, strlwc(entry), val);
  752. return 0 ;
  753. }
  754. /*-------------------------------------------------------------------------*/
  755. /**
  756. @brief Delete an entry in a dictionary
  757. @param ini Dictionary to modify
  758. @param entry Entry to delete (entry name)
  759. @return void
  760. If the given entry can be found, it is deleted from the dictionary.
  761. */
  762. /*--------------------------------------------------------------------------*/
  763. void iniparser_unset(dictionary * ini, char * entry)
  764. {
  765. dictionary_unset(ini, strlwc(entry));
  766. }
  767. /*-------------------------------------------------------------------------*/
  768. /**
  769. @brief Parse an ini file and return an allocated dictionary object
  770. @param ininame Name of the ini file to read.
  771. @return Pointer to newly allocated dictionary
  772. This is the parser for ini files. This function is called, providing
  773. the name of the file to be read. It returns a dictionary object that
  774. should not be accessed directly, but through accessor functions
  775. instead.
  776. The returned dictionary must be freed using iniparser_free().
  777. */
  778. /*--------------------------------------------------------------------------*/
  779. dictionary * iniparser_new(char *ininame)
  780. {
  781. dictionary * d ;
  782. char lin[ASCIILINESZ+1];
  783. char sec[ASCIILINESZ+1];
  784. char key[ASCIILINESZ+1];
  785. char val[ASCIILINESZ+1];
  786. char * where ;
  787. FILE * ini ;
  788. int lineno ;
  789. if ((ini=fopen(ininame, "r"))==NULL) {
  790. return NULL ;
  791. }
  792. sec[0]=0;
  793. /*
  794. * Initialize a new dictionary entry
  795. */
  796. d = dictionary_new(0);
  797. lineno = 0 ;
  798. while (fgets(lin, ASCIILINESZ, ini)!=NULL) {
  799. lineno++ ;
  800. where = strskp(lin); /* Skip leading spaces */
  801. if (*where==';' || *where=='#' || *where==0)
  802. continue ; /* Comment lines */
  803. else {
  804. if (sscanf(where, "[%[^]]", sec)==1) {
  805. /* Valid section name */
  806. strcpy(sec, strlwc(sec));
  807. iniparser_add_entry(d, sec, NULL, NULL);
  808. } else if (sscanf (where, "%[^=] = \"%[^\"]\"", key, val) == 2
  809. || sscanf (where, "%[^=] = '%[^\']'", key, val) == 2
  810. || sscanf (where, "%[^=] = %[^;#]", key, val) == 2) {
  811. strcpy(key, strlwc(strcrop(key)));
  812. /*
  813. * sscanf cannot handle "" or '' as empty value,
  814. * this is done here
  815. */
  816. if (!strcmp(val, "\"\"") || !strcmp(val, "''")) {
  817. val[0] = (char)0;
  818. } else {
  819. strcpy(val, strcrop(val));
  820. }
  821. iniparser_add_entry(d, sec, key, val);
  822. }
  823. }
  824. }
  825. fclose(ini);
  826. return d ;
  827. }
  828. /*-------------------------------------------------------------------------*/
  829. /**
  830. @brief Free all memory associated to an ini dictionary
  831. @param d Dictionary to free
  832. @return void
  833. Free all memory associated to an ini dictionary.
  834. It is mandatory to call this function before the dictionary object
  835. gets out of the current context.
  836. */
  837. /*--------------------------------------------------------------------------*/
  838. void iniparser_free(dictionary * d)
  839. {
  840. dictionary_del(d);
  841. }
  842. #ifdef __cplusplus
  843. }
  844. #endif