iniparser.c 28 KB

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