CUBRID Engine  latest
ini_parser.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2007 by Nicolas Devillard.
3  * MIT License
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include "porting.h"
27 #include "ini_parser.h"
28 #include "chartype.h"
29 
30 #define INI_BUFSIZ (512)
31 #define INI_INVALID_KEY ((char*)-1)
32 
34 {
41 };
43 
44 static void *ini_dblalloc (void *ptr, int size);
45 static unsigned int ini_table_hash (char *key);
46 static INI_TABLE *ini_table_new (int size);
47 static void ini_table_free (INI_TABLE * vd);
48 static const char *ini_table_get (INI_TABLE * ini, char *key, const char *def, int *lineno);
49 static int ini_table_set (INI_TABLE * vd, char *key, char *val, int lineno);
50 #if defined (ENABLE_UNUSED_FUNCTION)
51 static void ini_table_unset (INI_TABLE * ini, char *key);
52 #endif /* ENABLE_UNUSED_FUNCTION */
53 static char *ini_str_lower (const char *s);
54 static char *ini_str_trim (char *s);
55 static INI_LINE_STATUS ini_parse_line (char *input_line, char *section, char *key, char *value);
56 static const char *ini_get_internal (INI_TABLE * ini, const char *key, const char *def, int *lineno);
57 
58 /*
59  * ini_dblalloc() - Doubles the allocated size associated to a pointer
60  * return: new allocated pointer
61  * p(in): pointer
62  * size(in): current allocated size
63  */
64 static void *
65 ini_dblalloc (void *p, int size)
66 {
67  void *newp;
68 
69  newp = calloc (2 * size, 1);
70  if (newp == NULL)
71  {
72  return NULL;
73  }
74  memcpy (newp, p, size);
75  free (p);
76 
77  return newp;
78 }
79 
80 /*
81  * ini_table_hash() - Compute the hash key for a string
82  * return: hasn value
83  * key(in): Character string to use for key
84  * size(in): current allocated size
85  *
86  * Note: This hash function has been taken from an Article in Dr Dobbs Journal.
87  * This is normally a collision-free function, distributing keys evenly.
88  * The key is stored anyway in the struct so that collision can be avoided
89  * by comparing the key itself in last resort.
90  */
91 static unsigned int
92 ini_table_hash (char *key)
93 {
94  size_t len;
95  size_t i;
96  unsigned int hash;
97 
98  len = strlen (key);
99  for (hash = 0, i = 0; i < len; i++)
100  {
101  hash += (unsigned) key[i];
102  hash += (hash << 10);
103  hash ^= (hash >> 6);
104  }
105  hash += (hash << 3);
106  hash ^= (hash >> 11);
107  hash += (hash << 15);
108  return hash;
109 }
110 
111 /*
112  * ini_table_new() - Create a new INI_TABLE object
113  * return: newly allocated INI_TABLE objet
114  * size(in): Optional initial size of the INI_TABLE
115  *
116  * Note: If you do not know in advance (roughly) the number of entries
117  * in the INI_TABLE, give size=0.
118  */
119 static INI_TABLE *
120 ini_table_new (int size)
121 {
122  INI_TABLE *ini;
123 
124  /* If no size was specified, allocate space for 128 */
125  if (size < 128)
126  {
127  size = 128;
128  }
129 
130  ini = (INI_TABLE *) calloc (1, sizeof (INI_TABLE));
131  if (ini == NULL)
132  {
133  return NULL;
134  }
135  ini->size = size;
136  ini->val = (char **) calloc (size, sizeof (char *));
137  if (ini->val == NULL)
138  {
139  goto error;
140  }
141  ini->key = (char **) calloc (size, sizeof (char *));
142  if (ini->key == NULL)
143  {
144  goto error;
145  }
146  ini->lineno = (int *) calloc (size, sizeof (int));
147  if (ini->lineno == NULL)
148  {
149  goto error;
150  }
151  ini->hash = (unsigned int *) calloc (size, sizeof (unsigned int));
152  if (ini->hash == NULL)
153  {
154  goto error;
155  }
156 
157  return ini;
158 
159 error:
160  if (ini->hash != NULL)
161  {
162  free (ini->hash);
163  }
164  if (ini->lineno != NULL)
165  {
166  free (ini->lineno);
167  }
168  if (ini->key != NULL)
169  {
170  free (ini->key);
171  }
172  if (ini->val != NULL)
173  {
174  free (ini->val);
175  }
176  if (ini != NULL)
177  {
178  free (ini);
179  }
180  return NULL;
181 }
182 
183 /*
184  * ini_table_free() - Delete a INI_TABLE object
185  * return: void
186  * ini(in): INI_TABLE object to deallocate
187  *
188  * Note:
189  */
190 static void
192 {
193  int i;
194 
195  if (ini == NULL)
196  {
197  return;
198  }
199  for (i = 0; i < ini->size; i++)
200  {
201  if (ini->key[i] != NULL)
202  {
203  free (ini->key[i]);
204  }
205  if (ini->val[i] != NULL)
206  {
207  free (ini->val[i]);
208  }
209  }
210  free (ini->val);
211  ini->val = NULL;
212  free (ini->key);
213  ini->key = NULL;
214  free (ini->lineno);
215  ini->lineno = NULL;
216  free (ini->hash);
217  ini->hash = NULL;
218  free (ini);
219  ini = NULL;
220  return;
221 }
222 
223 /*
224  * ini_table_get() - Get a value from a INI_TABLE
225  * return: pointer to internally allocated character string
226  * ini(in): INI_TABLE object to search
227  * key(in): Key to look for in the INI_TABLE
228  * def(in): Default value to return if key not found
229  *
230  * Note: The returned character pointer points to data internal to the
231  * INI_TABLE object, you should not try to free it or modify it
232  */
233 static const char *
234 ini_table_get (INI_TABLE * ini, char *key, const char *def, int *lineno)
235 {
236  unsigned int hash;
237  int i;
238 
239  hash = ini_table_hash (key);
240  for (i = 0; i < ini->size; i++)
241  {
242  if (ini->key[i] == NULL)
243  {
244  continue;
245  }
246  /* Compare hash */
247  if (hash == ini->hash[i])
248  {
249  /* Compare string, to avoid hash collisions */
250  if (!strcmp (key, ini->key[i]))
251  {
252  if (lineno)
253  {
254  *lineno = ini->lineno[i];
255  }
256  return ini->val[i];
257  }
258  }
259  }
260  return def;
261 }
262 
263 /*
264  * ini_table_set() - Set a value in a INI_TABLE
265  * return: 0 if Ok, anything else otherwise
266  * ini(in): INI_TABLE object to modify
267  * key(in): Key to modify or add
268  * val(in): Value to add
269  * lineno(in): line number in ini file
270  *
271  * Note:
272  */
273 static int
274 ini_table_set (INI_TABLE * ini, char *key, char *val, int lineno)
275 {
276  int i;
277  unsigned int hash;
278 
279  if (ini == NULL || key == NULL)
280  {
281  return -1;
282  }
283 
284  /* Compute hash for this key */
285  hash = ini_table_hash (key);
286  /* Find if value is already in INI_TABLE */
287  if (ini->n > 0)
288  {
289  for (i = 0; i < ini->size; i++)
290  {
291  if (ini->key[i] == NULL)
292  {
293  continue;
294  }
295  if (hash == ini->hash[i])
296  { /* Same hash value */
297  if (!strcmp (key, ini->key[i]))
298  { /* Same key */
299  /* Found a value: modify and return */
300  if (ini->val[i] != NULL)
301  {
302  free (ini->val[i]);
303  }
304  ini->val[i] = val ? strdup (val) : NULL;
305  /* Value has been modified: return */
306  return 0;
307  }
308  }
309  }
310  }
311  /* Add a new value */
312  /* See if INI_TABLE needs to grow */
313  if (ini->n == ini->size)
314  {
315 
316  /* Reached maximum size: reallocate INI_TABLE */
317  ini->val = (char **) ini_dblalloc (ini->val, ini->size * sizeof (char *));
318  ini->key = (char **) ini_dblalloc (ini->key, ini->size * sizeof (char *));
319  ini->lineno = (int *) ini_dblalloc (ini->lineno, ini->size * sizeof (int));
320  ini->hash = (unsigned int *) ini_dblalloc (ini->hash, ini->size * sizeof (unsigned int));
321  if ((ini->val == NULL) || (ini->key == NULL) || (ini->lineno == NULL) || (ini->hash == NULL))
322  {
323  /* Cannot grow INI_TABLE */
324  return -1;
325  }
326  /* Double size */
327  ini->size *= 2;
328  }
329 
330  /* Insert key in the first empty slot */
331  for (i = 0; i < ini->size; i++)
332  {
333  if (ini->key[i] == NULL)
334  {
335  /* Add key here */
336  break;
337  }
338  }
339  ini->n++;
340  ini->lineno[i] = lineno;
341  ini->hash[i] = hash;
342  ini->key[i] = strdup (key);
343  if (val == NULL)
344  {
345  ini->nsec++; /* section */
346  ini->val[i] = NULL;
347  }
348  else
349  {
350  ini->val[i] = strdup (val);
351  }
352  return 0;
353 }
354 
355 #if defined (ENABLE_UNUSED_FUNCTION)
356 /*
357  * ini_table_unset() - Delete a key in a INI_TABLE
358  * return: void
359  * ini(in): INI_TABLE object to modify
360  * key(in): Key to remove
361  *
362  * Note:
363  */
364 static void
365 ini_table_unset (INI_TABLE * ini, char *key)
366 {
367  unsigned int i, hash;
368 
369  if (key == NULL)
370  {
371  return;
372  }
373 
374  hash = ini_table_hash (key);
375  for (i = 0; i < ini->size; i++)
376  {
377  if (ini->key[i] == NULL)
378  {
379  continue;
380  }
381  /* Compare hash */
382  if (hash == ini->hash[i])
383  {
384  /* Compare string, to avoid hash collisions */
385  if (!strcmp (key, ini->key[i]))
386  {
387  /* Found key */
388  break;
389  }
390  }
391  }
392  if (i >= ini->size)
393  {
394  /* Key not found */
395  return;
396  }
397 
398  free (ini->key[i]);
399  ini->key[i] = NULL;
400  if (ini->val[i] != NULL)
401  {
402  free (ini->val[i]);
403  ini->val[i] = NULL;
404  }
405  ini->lineno[i] = EOF;
406  ini->hash[i] = 0;
407  ini->n--;
408  return;
409 }
410 #endif /* ENABLE_UNUSED_FUNCTION */
411 
412 /*
413  * ini_str_lower() - Convert a string to lowercase
414  * return: statically allocated string
415  * s(in): String to convert
416  *
417  * Note: not re-entrant
418  */
419 static char *
420 ini_str_lower (const char *s)
421 {
422  static char result[INI_BUFSIZ + 1];
423  int i;
424 
425  if (s == NULL)
426  {
427  return NULL;
428  }
429  memset (result, 0, INI_BUFSIZ + 1);
430  i = 0;
431  while (s[i] && i < INI_BUFSIZ)
432  {
433  result[i] = (char) char_tolower ((int) s[i]);
434  i++;
435  }
436  result[INI_BUFSIZ] = (char) 0;
437  return result;
438 }
439 
440 /*
441  * ini_str_trim() - Remove blanks at the beginning and the end of a string
442  * return: statically allocated string
443  * s(in): String to strip
444  *
445  * Note: not re-entrant
446  */
447 static char *
448 ini_str_trim (char *s)
449 {
450  static char result[INI_BUFSIZ + 1];
451  char *last;
452 
453  if (s == NULL)
454  {
455  return NULL;
456  }
457 
458  while (char_isspace ((int) *s) && *s)
459  {
460  s++;
461  }
462  memset (result, 0, INI_BUFSIZ + 1);
463  strcpy (result, s);
464  last = result + strlen (result);
465  while (last > result)
466  {
467  if (!char_isspace ((int) *(last - 1)))
468  {
469  break;
470  }
471  last--;
472  }
473  *last = (char) 0;
474  return result;
475 }
476 
477 /*
478  * ini_parse_line() - Load a single line from an INI file
479  * return: line status
480  * input_line(in): input line
481  * section(out): section name
482  * key(out): key name
483  * value(out): value
484  *
485  * Note:
486  */
487 static INI_LINE_STATUS
488 ini_parse_line (char *input_line, char *section, char *key, char *value)
489 {
490  INI_LINE_STATUS status;
491  char line[INI_BUFSIZ + 1];
492  int len;
493 
494  strcpy (line, ini_str_trim (input_line));
495  len = (int) strlen (line);
496 
497  status = LINE_UNPROCESSED;
498  if (len < 1)
499  {
500  /* Empty line */
501  status = LINE_EMPTY;
502  }
503  else if (line[0] == '#')
504  {
505  /* Comment line */
506  status = LINE_COMMENT;
507  }
508  else if (line[0] == '[' && line[len - 1] == ']')
509  {
510  /* Section name */
511  char leading_char;
512 
513  sscanf (line, "[%[^]]", section);
514  strcpy (section, ini_str_trim (section));
515  leading_char = section[0];
516  if (leading_char == '@' || leading_char == '%')
517  {
518  sprintf (section, "%c%s", leading_char, ini_str_trim (section + 1));
519  }
520  strcpy (section, ini_str_lower (section));
521  status = LINE_SECTION;
522  }
523  else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2 || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
524  || sscanf (line, "%[^=] = %[^;#]", key, value) == 2)
525  {
526  /* Usual key=value, with or without comments */
527  strcpy (key, ini_str_trim (key));
528  strcpy (key, ini_str_lower (key));
529  strcpy (value, ini_str_trim (value));
530  /*
531  * sscanf cannot handle '' or "" as empty values
532  * this is done here
533  */
534  if (!strcmp (value, "\"\"") || (!strcmp (value, "''")))
535  {
536  value[0] = 0;
537  }
538  status = LINE_VALUE;
539  }
540  else if (sscanf (line, "%[^=] = %[;#]", key, value) == 2 || sscanf (line, "%[^=] %[=]", key, value) == 2)
541  {
542  /*
543  * Special cases:
544  * key=
545  * key=;
546  * key=#
547  */
548  strcpy (key, ini_str_trim (key));
549  strcpy (key, ini_str_lower (key));
550  value[0] = 0;
551  status = LINE_VALUE;
552  }
553  else
554  {
555  /* Generate syntax error */
556  status = LINE_ERROR;
557  }
558  return status;
559 }
560 
561 /*
562  * ini_parser_load() - Parse an ini file
563  * return: Pointer to newly allocated INI_TABLE
564  * ininame(in): ini file to read
565  *
566  * Note: The returned INI_TABLE must be freed using ini_parser_free()
567  */
568 INI_TABLE *
569 ini_parser_load (const char *ininame)
570 {
571  FILE *in;
572 
573  char line[INI_BUFSIZ + 1];
574  char section[INI_BUFSIZ + 1];
575  char key[INI_BUFSIZ + 1];
576  char tmp[(INI_BUFSIZ + 1) * 2];
577  char val[INI_BUFSIZ + 1];
578 
579  int last = 0;
580  int len;
581  int lineno = 0;
582  int errs = 0;
583 
584  INI_TABLE *ini;
585 
586  if ((in = fopen (ininame, "r")) == NULL)
587  {
588  fprintf (stderr, "ini_parser: cannot open %s\n", ininame);
589  return NULL;
590  }
591 
592  ini = ini_table_new (0);
593  if (!ini)
594  {
595  fclose (in);
596  return NULL;
597  }
598 
599  memset (line, 0, INI_BUFSIZ);
600  memset (section, 0, INI_BUFSIZ);
601  memset (key, 0, INI_BUFSIZ);
602  memset (val, 0, INI_BUFSIZ);
603  last = 0;
604 
605  while (fgets (line + last, INI_BUFSIZ - last, in) != NULL)
606  {
607  lineno++;
608  len = (int) strlen (line) - 1;
609  /* Safety check against buffer overflows */
610  if (line[len] != '\n' && len >= INI_BUFSIZ - 2)
611  {
612  fprintf (stderr, "ini_parser: input line too long in %s (%d)\n", ininame, lineno);
613  ini_table_free (ini);
614  fclose (in);
615  return NULL;
616  }
617  /* Get rid of \n and spaces at end of line */
618  while ((len > 0) && ((line[len] == '\n') || (char_isspace (line[len]))))
619  {
620  line[len] = 0;
621  len--;
622  }
623  /* Detect multi-line */
624  if (line[len] == '\\')
625  {
626  /* Multi-line value */
627  last = len;
628  continue;
629  }
630  else
631  {
632  last = 0;
633  }
634  switch (ini_parse_line (line, section, key, val))
635  {
636  case LINE_EMPTY:
637  case LINE_COMMENT:
638  break;
639 
640  case LINE_SECTION:
641  errs = ini_table_set (ini, section, NULL, lineno);
642  break;
643 
644  case LINE_VALUE:
645  sprintf (tmp, "%s:%s", section, key);
646  errs = ini_table_set (ini, tmp, val, lineno);
647  break;
648 
649  case LINE_ERROR:
650  fprintf (stderr, "ini_parser: syntax error in %s (%d):\n", ininame, lineno);
651  fprintf (stderr, "-> %s\n", line);
652  errs++;
653  break;
654 
655  default:
656  break;
657  }
658  memset (line, 0, INI_BUFSIZ);
659  last = 0;
660  if (errs < 0)
661  {
662  fprintf (stderr, "ini_parser: memory allocation failure\n");
663  break;
664  }
665  }
666  if (errs)
667  {
668  ini_table_free (ini);
669  ini = NULL;
670  }
671  fclose (in);
672  return ini;
673 }
674 
675 /*
676  * ini_parser_free() - Free all memory associated to an ini INI_TABLE
677  * return: void
678  * ini(in): Dictionary to free
679  *
680  * Note:
681  */
682 void
684 {
685  ini_table_free (ini);
686 }
687 
688 /*
689  * ini_findsec() - Find name for section n in a INI_TABLE
690  * return: true or false
691  * ini(in): INI_TABLE to examine
692  * sec(in): section name to search
693  *
694  * Note:
695  */
696 int
697 ini_findsec (INI_TABLE * ini, const char *sec)
698 {
699  int i;
700 
701  if (ini == NULL || sec == NULL)
702  {
703  return 0;
704  }
705 
706  for (i = 0; i < ini->size; i++)
707  {
708  if (ini->key[i] == NULL)
709  {
710  continue;
711  }
712  if (strcasecmp (ini->key[i], sec) == 0)
713  {
714  return 1;
715  }
716  }
717 
718  return 0;
719 }
720 
721 /*
722  * ini_getsecname() - Get name for section n in a INI_TABLE
723  * return: name of section or NULL if fail
724  * ini(in): INI_TABLE to examine
725  *
726  * Note:
727  */
728 char *
729 ini_getsecname (INI_TABLE * ini, int n, int *lineno)
730 {
731  int i, foundsec;
732 
733  if (ini == NULL || n < 0)
734  {
735  return NULL;
736  }
737  foundsec = 0;
738  for (i = 0; i < ini->size; i++)
739  {
740  if (ini->key[i] == NULL)
741  {
742  continue;
743  }
744  if (strchr (ini->key[i], ':') == NULL)
745  {
746  foundsec++;
747  if (foundsec > n)
748  {
749  break;
750  }
751  }
752  }
753  if (foundsec <= n)
754  {
755  return NULL;
756  }
757  if (lineno != NULL && ini->lineno != NULL)
758  {
759  *lineno = ini->lineno[i];
760  }
761  return ini->key[i];
762 }
763 
764 /*
765  * ini_hassec() - Check key string has section
766  * return: 1 true, or 0 false
767  * key(in): key to examine
768  *
769  * Note:
770  */
771 int
772 ini_hassec (const char *key)
773 {
774  return (key[0] != ':');
775 }
776 
777 /*
778  * ini_seccmp() - compare two key has same section
779  * return: 0 if not equal, or return section length
780  * key1(in): key to examine
781  * key2(in): key to examine
782  *
783  * Note:
784  */
785 int
786 ini_seccmp (const char *key1, const char *key2)
787 {
788  const char *s1 = strchr (key1, ':');
789  const char *s2 = strchr (key2, ':');
790  int key1_sec_len, key2_sec_len;
791 
792  if (s1)
793  {
794  key1_sec_len = CAST_STRLEN (s1 - key1);
795  }
796  else
797  {
798  key1_sec_len = (int) strlen (key1);
799  }
800 
801  if (s2)
802  {
803  key2_sec_len = CAST_STRLEN (s2 - key2);
804  }
805  else
806  {
807  key2_sec_len = (int) strlen (key2);
808  }
809 
810  if (key1_sec_len != key2_sec_len)
811  {
812  return 0;
813  }
814 
815  if (strncasecmp (key1, key2, key1_sec_len) == 0)
816  {
817  return key1_sec_len;
818  }
819 
820  return 0;
821 }
822 
823 /*
824  * ini_get_internal() - Get the string associated to a key
825  * return: pointer to statically allocated character string
826  * ini(in): INI_TABLE to search
827  * key(in): key to look for
828  * def(in): default value to return if key not found
829  * lineno(out): line number
830  *
831  * Note: A key as read from an ini file is given as "section:key"
832  * If the key cannot be found, the pointer passed as 'def' is returned
833  * do not free or modify returned pointer
834  */
835 static const char *
836 ini_get_internal (INI_TABLE * ini, const char *key, const char *def, int *lineno)
837 {
838  char *lc_key;
839  const char *sval;
840 
841  if (ini == NULL || key == NULL)
842  {
843  return def;
844  }
845 
846  lc_key = ini_str_lower (key);
847  sval = ini_table_get (ini, lc_key, def, lineno);
848  return sval;
849 }
850 
851 /*
852  * ini_getstr() - Get the string associated to a key
853  * return: pointer to statically allocated character string
854  * ini(in): INI_TABLE to search
855  * sec(in): section to look for
856  * key(in): key to look for
857  * def(in): default value to return if key not found
858  * lineno(out): line number
859  *
860  * Note: A key as read from an ini file is given as "key"
861  * If the key cannot be found, the pointer passed as 'def' is returned
862  * do not free or modify returned pointer
863  */
864 const char *
865 ini_getstr (INI_TABLE * ini, const char *sec, const char *key, const char *def, int *lineno)
866 {
867  char sec_key[INI_BUFSIZ + 1];
868 
869  sprintf (sec_key, "%s:%s", sec, key);
870  return ini_get_internal (ini, sec_key, def, lineno);
871 }
872 
873 /*
874  * ini_getint() - Get the string associated to a key, convert to an int
875  * return: int
876  * ini(in): INI_TABLE to search
877  * sec(in): section to look for
878  * key(in): key to look for
879  * def(in): default value to return if key not found
880  * lineno(out): line number
881  *
882  * Note:
883  */
884 int
885 ini_getint (INI_TABLE * ini, const char *sec, const char *key, int def, int *lineno)
886 {
887  const char *str;
888  int val;
889 
890  str = ini_getstr (ini, sec, key, INI_INVALID_KEY, lineno);
891  if (str == INI_INVALID_KEY)
892  {
893  return def;
894  }
895 
896  parse_int (&val, str, 0);
897  return val;
898 }
899 
900 /*
901  * ini_getuint() - Get the string associated to a key, convert to positive int
902  * return: positive int
903  * ini(in): INI_TABLE to search
904  * sec(in): section to look for
905  * key(in): key to look for
906  * def(in): default value to return if key not found
907  * lineno(out): line number
908  *
909  * Note:
910  */
911 int
912 ini_getuint (INI_TABLE * ini, const char *sec, const char *key, int def, int *lineno)
913 {
914  int result;
915 
916  result = ini_getint (ini, sec, key, def, lineno);
917  if (result <= 0)
918  {
919  return def;
920  }
921 
922  return result;
923 }
924 
925 #if defined (ENABLE_UNUSED_FUNCTION)
926 /*
927  * ini_getuint_min() - Get the string associated to a key, convert to
928  * positive int with minimum limit
929  * return: positive int
930  * ini(in): INI_TABLE to search
931  * sec(in): section to look for
932  * key(in): key to look for
933  * def(in): default value to return if key not found
934  * min(in): minimum limit
935  * lineno(out): line number
936  *
937  * Note:
938  */
939 int
940 ini_getuint_min (INI_TABLE * ini, const char *sec, const char *key, int def, int min, int *lineno)
941 {
942  int result;
943 
944  result = ini_getuint (ini, sec, key, def, lineno);
945  if (result < min)
946  {
947  return min;
948  }
949 
950  return result;
951 }
952 #endif /* ENABLE_UNUSED_FUNCTION */
953 
954 /*
955  * ini_getuint_max() - Get the string associated to a key, convert to
956  * positive int with maximum limit
957  * return: positive int
958  * ini(in): INI_TABLE to search
959  * sec(in): section to look for
960  * key(in): key to look for
961  * def(in): default value to return if key not found
962  * max(in): maximum limit
963  * lineno(out): line number
964  *
965  * Note:
966  */
967 int
968 ini_getuint_max (INI_TABLE * ini, const char *sec, const char *key, int def, int max, int *lineno)
969 {
970  int result;
971 
972  result = ini_getuint (ini, sec, key, def, lineno);
973  if (result > max)
974  {
975  return max;
976  }
977 
978  return result;
979 }
980 
981 /*
982  * ini_gethex() - Get the string associated to a key, convert to an hex
983  * return: int
984  * ini(in): INI_TABLE to search
985  * sec(in): section to look for
986  * key(in): key to look for
987  * def(in): default value to return if key not found
988  * lineno(out): line number
989  *
990  * Note: the conversion may overflow. see strtol().
991  */
992 int
993 ini_gethex (INI_TABLE * ini, const char *sec, const char *key, int def, int *lineno)
994 {
995  const char *str;
996  int val;
997 
998  str = ini_getstr (ini, sec, key, INI_INVALID_KEY, lineno);
999  if (str == INI_INVALID_KEY)
1000  {
1001  return def;
1002  }
1003 
1004  parse_int (&val, str, 16);
1005  return val;
1006 }
1007 
1008 /*
1009  * ini_getfloat() - Get the string associated to a key, convert to an float
1010  * return: float
1011  * ini(in): INI_TABLE to search
1012  * sec(in): section to look for
1013  * key(in): key to look for
1014  * def(in): default value to return if key not found
1015  * lineno(out): line number
1016  *
1017  * Note: the conversion may overflow. see strtod().
1018  */
1019 float
1020 ini_getfloat (INI_TABLE * ini, const char *sec, const char *key, float def, int *lineno)
1021 {
1022  const char *str;
1023 
1024  str = ini_getstr (ini, sec, key, INI_INVALID_KEY, lineno);
1025  if (str == INI_INVALID_KEY)
1026  {
1027  return def;
1028  }
1029  return (float) strtod (str, NULL);
1030 }
int char_isspace(int c)
Definition: chartype.c:109
unsigned int * hash
Definition: ini_parser.h:43
char ** key
Definition: ini_parser.h:40
const char * ini_getstr(INI_TABLE *ini, const char *sec, const char *key, const char *def, int *lineno)
Definition: ini_parser.c:865
int ini_gethex(INI_TABLE *ini, const char *sec, const char *key, int def, int *lineno)
Definition: ini_parser.c:993
static char * ini_str_trim(char *s)
Definition: ini_parser.c:448
int char_tolower(int c)
Definition: chartype.c:146
int parse_int(int *ret_p, const char *str_p, int base)
Definition: porting.c:2290
int ini_seccmp(const char *key1, const char *key2)
Definition: ini_parser.c:786
static unsigned int ini_table_hash(char *key)
Definition: ini_parser.c:92
#define CAST_STRLEN
Definition: porting.h:470
void ini_parser_free(INI_TABLE *ini)
Definition: ini_parser.c:683
INI_TABLE * ini_parser_load(const char *ininame)
Definition: ini_parser.c:569
enum ini_line_status INI_LINE_STATUS
Definition: ini_parser.c:42
#define INI_BUFSIZ
Definition: ini_parser.c:30
ini_line_status
Definition: ini_parser.c:33
int ini_getint(INI_TABLE *ini, const char *sec, const char *key, int def, int *lineno)
Definition: ini_parser.c:885
int ini_findsec(INI_TABLE *ini, const char *sec)
Definition: ini_parser.c:697
char * ini_getsecname(INI_TABLE *ini, int n, int *lineno)
Definition: ini_parser.c:729
int nsec
Definition: ini_parser.h:39
static INI_TABLE * ini_table_new(int size)
Definition: ini_parser.c:120
static INI_LINE_STATUS ini_parse_line(char *input_line, char *section, char *key, char *value)
Definition: ini_parser.c:488
int size
Definition: ini_parser.h:37
#define min(a, b)
#define NULL
Definition: freelistheap.h:34
int ini_getuint_max(INI_TABLE *ini, const char *sec, const char *key, int def, int max, int *lineno)
Definition: ini_parser.c:968
static char * ini_str_lower(const char *s)
Definition: ini_parser.c:420
static const char * ini_table_get(INI_TABLE *ini, char *key, const char *def, int *lineno)
Definition: ini_parser.c:234
float ini_getfloat(INI_TABLE *ini, const char *sec, const char *key, float def, int *lineno)
Definition: ini_parser.c:1020
static int ini_table_set(INI_TABLE *vd, char *key, char *val, int lineno)
Definition: ini_parser.c:274
#define max(a, b)
static void error(const char *msg)
Definition: gencat.c:331
#define strlen(s1)
Definition: intl_support.c:43
int ini_hassec(const char *key)
Definition: ini_parser.c:772
int i
Definition: dynamic_load.c:954
static void ini_table_free(INI_TABLE *vd)
Definition: ini_parser.c:191
char * strdup(const char *str)
Definition: porting.c:901
int * lineno
Definition: ini_parser.h:42
char ** val
Definition: ini_parser.h:41
const char ** p
Definition: dynamic_load.c:945
static const char * ini_get_internal(INI_TABLE *ini, const char *key, const char *def, int *lineno)
Definition: ini_parser.c:836
#define INI_INVALID_KEY
Definition: ini_parser.c:31
int ini_getuint(INI_TABLE *ini, const char *sec, const char *key, int def, int *lineno)
Definition: ini_parser.c:912
static void * ini_dblalloc(void *ptr, int size)
Definition: ini_parser.c:65