PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tzclock-3.0.1/src/TzClockConfig.c

#
C | 413 lines | 345 code | 15 blank | 53 comment | 27 complexity | a64d8226ee9ccae3c02a257051dff4fc MD5 | raw file
Possible License(s): GPL-2.0
  1. /*----------------------------------------------------------------------------------------------------*
  2. * *
  3. * T z C L O C K . C *
  4. * ================= *
  5. * *
  6. * TzClock developed by Chris Knight based on glock by Eric L. Smith. *
  7. * *
  8. *----------------------------------------------------------------------------------------------------*
  9. * *
  10. * TzClock is free software; you can redistribute it and/or modify it under the terms of the GNU *
  11. * General Public License version 2 as published by the Free Software Foundation. Note that I *
  12. * am not granting permission to redistribute or modify TzClock under the terms of any later *
  13. * version of the General Public License. *
  14. * *
  15. * TzClock is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without *
  16. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License along with this program (in *
  20. * the file "COPYING"); if not, write to the Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111, USA. *
  22. * *
  23. *----------------------------------------------------------------------------------------------------*/
  24. /**
  25. * @file
  26. * @brief .
  27. * @version $Id: TzClockConfig.c 1193 2011-06-26 16:10:55Z chris $
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "TzClockDisp.h"
  33. #include "list.h"
  34. typedef struct _configEntry
  35. {
  36. char *configName;
  37. char *configValue;
  38. }
  39. CONFIG_ENTRY;
  40. void *configQueue = NULL;
  41. int configSetValue (char *configName, char *configValue);
  42. /*----------------------------------------------------------------------------------------------------*
  43. * *
  44. * C O N F I G L O A D *
  45. * ==================== *
  46. * *
  47. *----------------------------------------------------------------------------------------------------*/
  48. /**
  49. * @brief .
  50. * @param configFile .
  51. * @result .
  52. */
  53. int configLoad (const char *configFile)
  54. {
  55. FILE *inFile;
  56. char readBuff[512], configName[81], configValue[256];
  57. int i, j, quote;
  58. if (configQueue == NULL)
  59. {
  60. if ((configQueue = queueCreate ()) == NULL)
  61. return 0;
  62. }
  63. if (configFile[0] == 0)
  64. return 0;
  65. if ((inFile = fopen (configFile, "r")) == NULL)
  66. return 0;
  67. while (fgets (readBuff, 512, inFile))
  68. {
  69. i = 0;
  70. quote = 0;
  71. // Skip leading white space
  72. //==================================================
  73. while (readBuff[i] <= ' ')
  74. i++;
  75. // Skip comments
  76. //==================================================
  77. if (readBuff[i] == '#')
  78. continue;
  79. // Read parameter name
  80. //==================================================
  81. j = 0;
  82. while (readBuff[i] > ' ' && readBuff[i] != '=' && j < 80)
  83. {
  84. configName[j++] = readBuff[i];
  85. configName[j] = 0;
  86. i ++;
  87. }
  88. // Skip to equal sign
  89. //==================================================
  90. while (readBuff[i] != 0 && readBuff[i] != '=')
  91. i++;
  92. // No equal sign then this is not a config line
  93. //==================================================
  94. if (readBuff[i] == 0)
  95. continue;
  96. // Skip while space after the equal sign
  97. //==================================================
  98. i++;
  99. while (readBuff[i] <= ' ')
  100. i++;
  101. // Read parameter value
  102. //==================================================
  103. configValue[j = 0] = 0;
  104. while (readBuff[i] > 0 && j < 255)
  105. {
  106. if (readBuff[i] == '"')
  107. {
  108. quote = quote == 0 ? 1 : 0;
  109. }
  110. else
  111. {
  112. if (quote == 0 && (readBuff[i] <= ' ' || readBuff[i] == '#'))
  113. break;
  114. configValue[j++] = readBuff[i];
  115. configValue[j] = 0;
  116. }
  117. i ++;
  118. }
  119. // Save the name and value
  120. //==================================================
  121. configSetValue (configName, configValue);
  122. }
  123. fclose (inFile);
  124. return 1;
  125. }
  126. /*----------------------------------------------------------------------------------------------------*
  127. * *
  128. * C O N F I G S A V E *
  129. * ==================== *
  130. * *
  131. *----------------------------------------------------------------------------------------------------*/
  132. /**
  133. * @brief .
  134. * @param configFile .
  135. * @result .
  136. */
  137. int configSave (const char *configFile)
  138. {
  139. int i = 0;
  140. FILE *outFile;
  141. CONFIG_ENTRY *foundEntry = NULL;
  142. if (configQueue != NULL)
  143. {
  144. if ((outFile = fopen (configFile, "w+")) == NULL)
  145. return 0;
  146. fprintf (outFile, "#--------------------------------------------------\n");
  147. fprintf (outFile, "# This file was generated by an application.\n");
  148. fprintf (outFile, "# Please be careful when changing values by hand.\n");
  149. fprintf (outFile, "#--------------------------------------------------\n");
  150. while ((foundEntry = queueRead (configQueue, i++)) != NULL)
  151. {
  152. fprintf (outFile, "%s = \"%s\"\n", foundEntry -> configName, foundEntry -> configValue);
  153. }
  154. fclose (outFile);
  155. }
  156. return 1;
  157. }
  158. /*----------------------------------------------------------------------------------------------------*
  159. * *
  160. * C O N F I G F R E E *
  161. * ==================== *
  162. * *
  163. *----------------------------------------------------------------------------------------------------*/
  164. /**
  165. * @brief .
  166. * @result .
  167. */
  168. void configFree ()
  169. {
  170. CONFIG_ENTRY *foundEntry = NULL;
  171. if (configQueue != NULL)
  172. {
  173. while ((foundEntry = queueGet (configQueue)) != NULL)
  174. {
  175. free (foundEntry -> configName);
  176. free (foundEntry -> configValue);
  177. free (foundEntry);
  178. }
  179. queueDelete (configQueue);
  180. configQueue = NULL;
  181. }
  182. }
  183. /*----------------------------------------------------------------------------------------------------*
  184. * *
  185. * C O N F I G F I N D E N T R Y *
  186. * =============================== *
  187. * *
  188. *----------------------------------------------------------------------------------------------------*/
  189. /**
  190. * @brief .
  191. * @param configName .
  192. * @result .
  193. */
  194. static CONFIG_ENTRY *configFindEntry (char *configName)
  195. {
  196. int rec = 0;
  197. CONFIG_ENTRY *foundEntry = NULL;
  198. if (configQueue != NULL)
  199. {
  200. while ((foundEntry = queueRead (configQueue, rec)) != NULL)
  201. {
  202. if (strcmp (configName, foundEntry -> configName) == 0)
  203. break;
  204. rec ++;
  205. }
  206. }
  207. return foundEntry;
  208. }
  209. /*----------------------------------------------------------------------------------------------------*
  210. * *
  211. * C O N F I G S E T V A L U E *
  212. * ============================= *
  213. * *
  214. *----------------------------------------------------------------------------------------------------*/
  215. /**
  216. * @brief .
  217. * @param configName .
  218. * @param configValue .
  219. * @result .
  220. */
  221. int configSetValue (char *configName, char *configValue)
  222. {
  223. CONFIG_ENTRY *newEntry = NULL;
  224. if (configQueue == NULL)
  225. {
  226. if ((configQueue = queueCreate ()) == NULL)
  227. return 0;
  228. }
  229. if ((newEntry = configFindEntry (configName)) == NULL)
  230. {
  231. if ((newEntry = malloc (sizeof (CONFIG_ENTRY))) == NULL)
  232. return 0;
  233. if ((newEntry -> configName = malloc (strlen (configName) + 1)) == NULL)
  234. {
  235. free (newEntry);
  236. return 0;
  237. }
  238. strcpy (newEntry -> configName, configName);
  239. if ((newEntry -> configValue = malloc (strlen (configValue) + 1)) == NULL)
  240. {
  241. free (newEntry -> configName);
  242. free (newEntry);
  243. return 0;
  244. }
  245. strcpy (newEntry -> configValue, configValue);
  246. queuePut (configQueue, newEntry);
  247. }
  248. else
  249. {
  250. char *tempPtr;
  251. if ((tempPtr = malloc (strlen (configValue) + 1)) == NULL)
  252. return 0;
  253. strcpy (tempPtr, configValue);
  254. free (newEntry -> configValue);
  255. newEntry -> configValue = tempPtr;
  256. }
  257. return 1;
  258. }
  259. /*----------------------------------------------------------------------------------------------------*
  260. * *
  261. * C O N F I G S E T I N T V A L U E *
  262. * ==================================== *
  263. * *
  264. *----------------------------------------------------------------------------------------------------*/
  265. /**
  266. * @brief .
  267. * @param configName .
  268. * @param configValue .
  269. * @result .
  270. */
  271. int configSetIntValue (char *configName, int configValue)
  272. {
  273. char numBuffer[21];
  274. sprintf (numBuffer, "%d", configValue);
  275. return configSetValue (configName, numBuffer);
  276. }
  277. /*----------------------------------------------------------------------------------------------------*
  278. * *
  279. * C O N F I G S E T B O O L V A L U E *
  280. * ====================================== *
  281. * *
  282. *----------------------------------------------------------------------------------------------------*/
  283. /**
  284. * @brief Set a boolean value in the config.
  285. * @param configName Config parameter name.
  286. * @param configValue Config value.
  287. * @result None.
  288. */
  289. int configSetBoolValue (char *configName, bool configValue)
  290. {
  291. return configSetValue (configName, configValue ? "true" : "false");
  292. }
  293. /*----------------------------------------------------------------------------------------------------*
  294. * *
  295. * C O N F I G G E T V A L U E *
  296. * ============================= *
  297. * *
  298. *----------------------------------------------------------------------------------------------------*/
  299. /**
  300. * @brief .
  301. * @param configName .
  302. * @param value .
  303. * @result .
  304. */
  305. int configGetValue (char *configName, char *value, int maxLen)
  306. {
  307. CONFIG_ENTRY *foundEntry = configFindEntry (configName);
  308. if (foundEntry != NULL)
  309. {
  310. strncpy (value, foundEntry -> configValue, maxLen);
  311. value[maxLen] = 0;
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. /*----------------------------------------------------------------------------------------------------*
  317. * *
  318. * C O N F I G G E T I N T V A L U E *
  319. * ==================================== *
  320. * *
  321. *----------------------------------------------------------------------------------------------------*/
  322. /**
  323. * @brief .
  324. * @param configName .
  325. * @param value .
  326. * @result .
  327. */
  328. int configGetIntValue (const char *configName, int *value)
  329. {
  330. CONFIG_ENTRY *foundEntry = configFindEntry ((char *)configName);
  331. *value = 0;
  332. if (foundEntry != NULL)
  333. {
  334. sscanf (foundEntry -> configValue, "%i", value);
  335. return 1;
  336. }
  337. return 0;
  338. }
  339. /*----------------------------------------------------------------------------------------------------*
  340. * *
  341. * C O N F I G G E T B O O L V A L U E *
  342. * ====================================== *
  343. * *
  344. *----------------------------------------------------------------------------------------------------*/
  345. /**
  346. * @brief Read a boolen value from the config.
  347. * @param configName Config parameter name.
  348. * @param value Pointer to place to save the value.
  349. * @result None.
  350. */
  351. int configGetBoolValue (const char *configName, bool *value)
  352. {
  353. CONFIG_ENTRY *foundEntry = configFindEntry ((char *)configName);
  354. *value = FALSE;
  355. if (foundEntry != NULL)
  356. {
  357. if (strcmp (foundEntry -> configValue, "true") == 0)
  358. *value = 1;
  359. else if (strcmp (foundEntry -> configValue, "false") == 0)
  360. *value = 0;
  361. else
  362. {
  363. int i;
  364. sscanf (foundEntry -> configValue, "%i", &i);
  365. *value = (i != 0);
  366. }
  367. return 1;
  368. }
  369. return 0;
  370. }