config.c.html 10 KB

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