config.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * config.c - configuration file reader;
  44. *
  45. * configuration files contain named parts where each part may
  46. * contain one of more named items that have text definitions;
  47. *
  48. * the named file can be searched for the first occurance of a
  49. * named part then the first occurance of a named item;
  50. *
  51. * [part1]
  52. * item1=text
  53. * item2=text
  54. *
  55. * [part2]
  56. * item1=text
  57. * item2=text
  58. *
  59. *--------------------------------------------------------------------*/
  60. #ifndef CONFIG_SOURCE
  61. #define CONFIG_SOURCE
  62. /*====================================================================*
  63. * system header files;
  64. *--------------------------------------------------------------------*/
  65. #include <stdio.h>
  66. #include <ctype.h>
  67. #include <unistd.h>
  68. #include <string.h>
  69. /*====================================================================*
  70. * custom header files;
  71. *--------------------------------------------------------------------*/
  72. #include "../tools/config.h"
  73. #include "../tools/types.h"
  74. #include "../tools/chars.h"
  75. /*====================================================================*
  76. * program variables;
  77. *--------------------------------------------------------------------*/
  78. static char buffer [1024] = "";
  79. static signed c;
  80. /*====================================================================*
  81. *
  82. * bool compare (FILE * fp, char const *sp);
  83. *
  84. * compare file and text characters until they differ or until end
  85. * of text, line or file occurs; a match is declared when the text
  86. * ends before the line or file;
  87. *
  88. * spaces and tabs within the argument string or file string are
  89. * ignored such that "item1", " item1 " and "item 1" all match;
  90. *
  91. *--------------------------------------------------------------------*/
  92. static bool compare (FILE * fp, char const * sp)
  93. {
  94. while (isblank (*sp))
  95. {
  96. sp++;
  97. }
  98. while ((*sp) && (c != '\n') && (c != EOF))
  99. {
  100. if (toupper (c) != toupper (*sp))
  101. {
  102. return (0);
  103. }
  104. do
  105. {
  106. sp++;
  107. }
  108. while (isblank (*sp));
  109. do
  110. {
  111. c = getc (fp);
  112. }
  113. while (isblank (c));
  114. }
  115. return (!*sp);
  116. }
  117. /*====================================================================*
  118. *
  119. * void collect (FILE * fp);
  120. *
  121. * collect text to end of line; remove leading and trailing space
  122. * but preserve embedded space; replace selected escape sequences;
  123. *
  124. *--------------------------------------------------------------------*/
  125. static void collect (FILE * fp)
  126. {
  127. char *bp = buffer;
  128. char *cp = buffer;
  129. while ((c != ';') && (c != '\n') && (c != EOF))
  130. {
  131. if (c == '\\')
  132. {
  133. c = getc (fp);
  134. if (c == 'n')
  135. {
  136. c = '\n';
  137. }
  138. if (c == 't')
  139. {
  140. c = '\t';
  141. }
  142. }
  143. if ((cp - buffer) < (signed)(sizeof (buffer) - 1))
  144. {
  145. *cp++ = c;
  146. }
  147. if (!isblank (c))
  148. {
  149. bp = cp;
  150. }
  151. c = getc (fp);
  152. }
  153. *bp = '\0';
  154. return;
  155. }
  156. /*====================================================================*
  157. *
  158. * void discard (FILE * fp);
  159. *
  160. * read and discard characters until end-of-line or end-of-file
  161. * is detected; read the first character of next line if end of
  162. * file has not been detected;
  163. *
  164. *--------------------------------------------------------------------*/
  165. static void discard (FILE * fp)
  166. {
  167. while ((c != '\n') && (c != EOF))
  168. {
  169. c = getc (fp);
  170. }
  171. if (c != EOF)
  172. {
  173. c = getc (fp);
  174. }
  175. return;
  176. }
  177. /*====================================================================*
  178. *
  179. * Const char * configstring (char const * file, char const * part, char const * item, char const * text)
  180. *
  181. * open the named file, locate the named part and return the named
  182. * item text, if present; return alternative text if the file part
  183. * or item is missing; the calling function must preserve returned
  184. * text because it may be over-written on successive calls;
  185. *
  186. *--------------------------------------------------------------------*/
  187. char const * configstring (char const * file, char const * part, char const * item, char const * text)
  188. {
  189. FILE *fp;
  190. if ((!file) || (!part) || (!item))
  191. {
  192. return (text);
  193. }
  194. if ((fp = fopen (file, "rb")))
  195. {
  196. for (c = getc (fp); c != EOF; discard (fp))
  197. {
  198. while (isblank (c))
  199. {
  200. c = getc (fp);
  201. }
  202. if (c != '[')
  203. {
  204. continue;
  205. }
  206. do
  207. {
  208. c = getc (fp);
  209. }
  210. while (isblank (c));
  211. if (!compare (fp, part))
  212. {
  213. continue;
  214. }
  215. if (c != ']')
  216. {
  217. continue;
  218. }
  219. for (discard (fp); (c != '[') && (c != EOF); discard (fp))
  220. {
  221. while (isblank (c))
  222. {
  223. c = getc (fp);
  224. }
  225. if (c == ';')
  226. {
  227. continue;
  228. }
  229. if (!compare (fp, item))
  230. {
  231. continue;
  232. }
  233. if (c != '=')
  234. {
  235. continue;
  236. }
  237. do
  238. {
  239. c = getc (fp);
  240. }
  241. while (isblank (c));
  242. collect (fp);
  243. text = strdup (buffer);
  244. break;
  245. }
  246. break;
  247. }
  248. fclose (fp);
  249. }
  250. return (text);
  251. }
  252. /*====================================================================*
  253. *
  254. * unsigned confignumber (char const * file, char const * part, char const * item, unsigned number)
  255. *
  256. * open the named file, locate the named part and return the named
  257. * item as an unsigned integer, if present; return a default number
  258. * if the file, part or item is missing;
  259. *
  260. *--------------------------------------------------------------------*/
  261. unsigned confignumber (char const * file, char const * part, char const * item, unsigned number)
  262. {
  263. unsigned value = 0;
  264. char const * string = configstring (file, part, item, "");
  265. if ((string) && (* string))
  266. {
  267. while (isdigit (* string))
  268. {
  269. value *= 10;
  270. value += * string++ - '0';
  271. }
  272. if (! (* string))
  273. {
  274. number = value;
  275. }
  276. }
  277. return (number);
  278. }
  279. /*====================================================================*
  280. *
  281. * unsigned confignumber_range (char const * file, char const * part, char const * item, unsigned number, unsigned min, unsigned max)
  282. *
  283. * open the named file, locate the named part and return the named
  284. * item as an unsigned integer, if present; return a default number
  285. * if the file, part or item is missing or item is out of range;
  286. *
  287. *--------------------------------------------------------------------*/
  288. unsigned confignumber_range (char const * file, char const * part, char const * item, unsigned number, unsigned min, unsigned max)
  289. {
  290. unsigned value = 0;
  291. char const * string = configstring (file, part, item, "");
  292. if ((string) && (* string))
  293. {
  294. while (isdigit (* string))
  295. {
  296. value *= 10;
  297. value += * string++ - '0';
  298. }
  299. if (! (* string))
  300. {
  301. if ((value >= min) && (value <= max))
  302. {
  303. number = value;
  304. }
  305. }
  306. }
  307. return (number);
  308. }
  309. /*====================================================================*
  310. *
  311. * int main (int argc, char const * argv []);
  312. *
  313. * demo/test program; arguments are file, part, item and text in
  314. * that order; you can construct your own configuration file and
  315. * observe behaviour;
  316. *
  317. *--------------------------------------------------------------------*/
  318. #if 0
  319. #include <stdio.h>
  320. int main (int argc, char const * argv [])
  321. {
  322. char const * text = configstring (argv [1], argv [2], argv [3], argv [4]);
  323. printf ("file=[%s] part=[%s] item=[%s] text=[%s]\n", argv [1], argv [2], argv [3], text);
  324. return (0);
  325. }
  326. #endif
  327. /*====================================================================*
  328. *
  329. *--------------------------------------------------------------------*/
  330. #endif