iniparser.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. @mainpage iniparser documentation
  3. @section welcome Introduction
  4. iniParser is a simple C library offering ini file parsing services.
  5. The library is pretty small (less than 1500 lines of C) and robust, and
  6. does not depend on any other external library to compile. It is written
  7. in C and should compile on most platforms without difficulty.
  8. @section inidef What is an ini file?
  9. An ini file is an ASCII file describing simple parameters
  10. (character strings, integers, floating-point values or booleans)
  11. in an explicit format, easy to use and modify for users.
  12. An ini file is segmented into Sections, declared by the following
  13. syntax:
  14. @verbatim
  15. [Section Name]
  16. @endverbatim
  17. i.e. the section name enclosed in square brackets, alone on a
  18. line. Sections names are allowed to contain any character but
  19. square brackets or linefeeds.
  20. In any section are zero or more variables, declared with the
  21. following syntax:
  22. @verbatim
  23. Key = value ; comment
  24. @endverbatim
  25. The key is any string (possibly containing blanks). The value is
  26. any character on the right side of the equal sign. Values can be
  27. given enclosed with quotes. If no quotes are present, the value is
  28. understood as containing all characters between the first and the
  29. last non-blank characters before the comment. The following
  30. declarations are identical:
  31. @verbatim
  32. Hello = "this is a long string value" ; comment
  33. Hello = this is a long string value ; comment
  34. @endverbatim
  35. The semicolon and comment at the end of the line are optional. If
  36. there is a comment, it starts from the first character after the
  37. semicolon up to the end of the line.
  38. Multi-line values can be provided by ending the line with a
  39. backslash (`\`).
  40. @verbatim
  41. Multiple = Line 1 \
  42. Line 2 \
  43. Line 3 \
  44. Line 4 ; comment
  45. @endverbatim
  46. This would yield: "multiple" <- "Line1 Line2 Line3 Line4"
  47. Comments in an ini file are:
  48. - Lines starting with a hash sign
  49. - Blank lines (only blanks or tabs)
  50. - Comments given on value lines after the semicolon (if present)
  51. @section install Compiling/installing the library
  52. Edit the Makefile to indicate the C compiler you want to use, the
  53. options to provide to compile C, and possibly the options to pass
  54. to the ar program on your machine to build a library (.a) from a set
  55. of object (.o) files.
  56. Defaults are set for the gcc compiler and the standard ar library
  57. builder.
  58. Type 'make', that should do it.
  59. To use the library in your programs, add the following line on top
  60. of your module:
  61. @code
  62. #include "iniparser.h"
  63. @endcode
  64. And link your program with the iniparser library by adding
  65. @c -liniparser.a to the compile line.
  66. See the file test/initest.c for an example.
  67. iniparser is an C library. If you want to compile it
  68. with a C++ compiler you will likely run into compatibility
  69. issues. Headers probably have to include the extern "C"
  70. hack and function prototypes will want to add some const
  71. here and there to keep the compiler happy. This job is left
  72. to the reader as there are too many C++ compilers around, each
  73. with its own requirements as to what represents acceptable
  74. C code in a C++ environment. You have been warned.
  75. @section reference Library reference
  76. The library is completely documented in its header file. On-line
  77. documentation has been generated and can be consulted here:
  78. - iniparser.h
  79. @section usage Using the parser
  80. Comments are discarded by the parser. Then sections are
  81. identified, and in each section a new entry is created for every
  82. keyword found. The keywords are stored with the following syntax:
  83. @verbatim
  84. [Section]
  85. Keyword = value ; comment
  86. @endverbatim
  87. is converted to the following key pair:
  88. @verbatim
  89. ("section:keyword", "value")
  90. @endverbatim
  91. This means that if you want to retrieve the value that was stored
  92. in the section called @c Pizza, in the keyword @c Cheese,
  93. you would make a request to the dictionary for
  94. @c "pizza:cheese". All section and keyword names are converted
  95. to lowercase before storage in the structure. The value side is
  96. conserved as it has been parsed, though.
  97. Section names are also stored in the structure. They are stored
  98. using as key the section name, and a NULL associated value. They
  99. can be queried through iniparser_find_entry().
  100. To launch the parser, use the function called iniparser_load(), which
  101. takes an input file name and returns a newly allocated @e dictionary
  102. structure. This latter object should remain opaque to the user and only
  103. accessed through the following accessor functions:
  104. - iniparser_getstring()
  105. - iniparser_getint()
  106. - iniparser_getdouble()
  107. - iniparser_getboolean()
  108. Finally, discard this structure using iniparser_freedict().
  109. All values parsed from the ini file are stored as strings. The
  110. accessors are just converting these strings to the requested type on
  111. the fly, but you could basically perform this conversion by yourself
  112. after having called the string accessor.
  113. Notice that iniparser_getboolean() will return an integer (0 or 1),
  114. trying to make sense of what was found in the file. Strings starting
  115. with "y", "Y", "t", "T" or "1" are considered true values (return 1),
  116. strings starting with "n", "N", "f", "F", "0" are considered false
  117. (return 0). This allows some flexibility in handling of boolean
  118. answers.
  119. If you want to add extra information into the structure that was not
  120. present in the ini file, you can use iniparser_set() to insert a
  121. string.
  122. If you want to add a section to the structure, add a key
  123. with a NULL value. Example:
  124. @verbatim
  125. iniparser_set(ini, "section", NULL);
  126. iniparser_set(ini, "section:key1", NULL);
  127. iniparser_set(ini, "section:key2", NULL);
  128. @endverbatim
  129. @section implementation A word about the implementation
  130. The dictionary structure is a pretty simple dictionary
  131. implementation which might find some uses in other applications.
  132. If you are curious, look into the source.
  133. @section defects Known defects
  134. The dictionary structure is extremely unefficient for searching
  135. as keys are sorted in the same order as they are read from the
  136. ini file, which is convenient when dumping back to a file. The
  137. simplistic first-approach linear search implemented there can
  138. become a bottleneck if you have a very large number of keys.
  139. People who need to load large amounts of data from an ini file
  140. should definitely turn to more appropriate solutions: sqlite3 or
  141. similar. There are otherwise many other dictionary implementations
  142. available on the net to replace this one.
  143. */