/xbmc/cores/paplayer/SIDCodec/libsidutils/src/ini/types.i

https://github.com/tmacreturns/XBMC_wireless_setup · Swig · 515 lines · 283 code · 55 blank · 177 comment · 0 complexity · e925b60aeb5b9485ca3c77aca4d58289 MD5 · raw file

  1. /***************************************************************************
  2. types.i - Libini supported data types
  3. Use readString for others
  4. -------------------
  5. begin : Fri Apr 21 2000
  6. copyright : (C) 2000 by Simon White
  7. email : s_a_white@email.com
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "ini.h"
  22. /********************************************************************************************************************
  23. * Function : __ini_read
  24. * Parameters : ini - pointer to ini file database, size - key length returned here
  25. * Returns : -1 for Error and 0 on success.
  26. * Globals Used :
  27. * Globals Modified :
  28. * Description : Common read functionality
  29. ********************************************************************************************************************
  30. * Rev | Date | By | Comment
  31. * ----------------------------------------------------------------------------------------------------------------
  32. ********************************************************************************************************************/
  33. static int __ini_read (ini_t *ini, size_t *size)
  34. {
  35. struct key_tag *_key;
  36. if (!ini->selected)
  37. return -1;
  38. // Locate and read keys value
  39. _key = ini->selected->selected;
  40. if (!_key)
  41. return -1;
  42. if (_key->length)
  43. { // Locate and read back keys data
  44. fseek (ini->ftmp, _key->pos, SEEK_SET);
  45. }
  46. else if (_key == &ini->tmpKey)
  47. return -1; // Can't read tmpKey
  48. *size = _key->length;
  49. return 0;
  50. }
  51. #ifdef INI_ADD_LIST_SUPPORT
  52. /********************************************************************************************************************
  53. * Function : __ini_readList
  54. * Parameters : ini - pointer to ini file database.
  55. * Returns : Pointer to buffer holding data.
  56. * Globals Used :
  57. * Globals Modified :
  58. * Description : Common read functionality from a list
  59. ********************************************************************************************************************
  60. * Rev | Date | By | Comment
  61. * ----------------------------------------------------------------------------------------------------------------
  62. ********************************************************************************************************************/
  63. static char *__ini_readList (ini_t *ini)
  64. {
  65. if (!ini->selected)
  66. return NULL;
  67. // Locate and read keys value
  68. if (!ini->selected->selected)
  69. return NULL;
  70. return __ini_listRead (ini);
  71. }
  72. #endif // INI_ADD_LIST_SUPPORT
  73. /********************************************************************************************************************
  74. * Function : __ini_write
  75. * Parameters : ini - pointer to ini file database. heading - heading name. key - key name.
  76. * Returns : Pointer to new key.
  77. * Globals Used :
  78. * Globals Modified :
  79. * Description : Make calls to add a new key and appends a description of changes in the backup file
  80. ********************************************************************************************************************
  81. * Rev | Date | By | Comment
  82. * ----------------------------------------------------------------------------------------------------------------
  83. ********************************************************************************************************************/
  84. static struct key_tag *__ini_write (ini_t *ini)
  85. {
  86. struct section_tag *section;
  87. struct key_tag *key;
  88. // Is file read only?
  89. if (ini->mode == INI_READ)
  90. return NULL;
  91. // Check to make sure a section/key has
  92. // been asked for by the user
  93. section = ini->selected;
  94. if (!section)
  95. return NULL;
  96. key = section->selected;
  97. if (!key)
  98. return NULL;
  99. // Add or replace key
  100. if (!__ini_addHeading (ini, section->heading))
  101. return NULL;
  102. return __ini_addKey (ini, key->key);
  103. }
  104. /********************************************************************************************************************
  105. * Function : ini_readString
  106. * Parameters : ini - pointer to ini file database.
  107. * : value - keys data
  108. * Returns : -1 for error or the number of chars read.
  109. * Globals Used :
  110. * Globals Modified :
  111. * Description : Reads data part from a key and returns it as a string
  112. ********************************************************************************************************************
  113. * Rev | Date | By | Comment
  114. * ----------------------------------------------------------------------------------------------------------------
  115. ********************************************************************************************************************/
  116. int INI_LINKAGE ini_readString (ini_fd_t fd, char *str, size_t size)
  117. {
  118. ini_t *ini = (ini_t *) fd;
  119. // Check size and reserve space for NULL
  120. if (size-- <= 0)
  121. return -1;
  122. #ifdef INI_ADD_LIST_SUPPORT
  123. if (ini->listDelims)
  124. {
  125. char *data = __ini_readList (ini);
  126. if (!data)
  127. return -1;
  128. strncpy (str, data, size);
  129. }
  130. else
  131. #endif // INI_ADD_LIST_SUPPORT
  132. { // Locate and read back keys data (Index ignored)
  133. // Check to make sure size is correct
  134. size_t length;
  135. if (__ini_read (ini, &length) < 0)
  136. return -1;
  137. if (size > length)
  138. size = length;
  139. size = fread (str, sizeof(char), size, ini->ftmp);
  140. }
  141. str[size] = '\0';
  142. __ini_strtrim (str);
  143. return (int) size;
  144. }
  145. /********************************************************************************************************************
  146. * Function : ini_writeString
  147. * Parameters : ini - pointer to ini file database.
  148. * : value - keys data
  149. * Returns : -1 for Error and 0 on success
  150. * Globals Used :
  151. * Globals Modified :
  152. * Description : Writes data part to a key.
  153. * : Conforms to Microsoft API call. E.g. use NULLS to remove headings/keys
  154. * : Headings and keys will be created as necessary
  155. ********************************************************************************************************************
  156. * Rev | Date | By | Comment
  157. * ----------------------------------------------------------------------------------------------------------------
  158. ********************************************************************************************************************/
  159. int INI_LINKAGE ini_writeString (ini_fd_t fd, const char *str)
  160. {
  161. ini_t *ini = (ini_t *) fd;
  162. struct key_tag *_key;
  163. _key = __ini_write (ini);
  164. if (!_key)
  165. return -1;
  166. // Write data to bottom of backup file
  167. _key->length = strlen (str);
  168. fprintf (ini->ftmp, "%s\n", str);
  169. return 0;
  170. }
  171. /********************************************************************************************************************
  172. * Function : ini_readInt
  173. * Parameters : ini - pointer to ini file database.
  174. * : value - keys data
  175. * Returns : -1 for error or the number of values read.
  176. * Globals Used :
  177. * Globals Modified :
  178. * Description : Reads data part from a key and returns it as a int
  179. ********************************************************************************************************************
  180. * Rev | Date | By | Comment
  181. * ----------------------------------------------------------------------------------------------------------------
  182. ********************************************************************************************************************/
  183. int INI_LINKAGE ini_readInt (ini_fd_t fd, int *value)
  184. {
  185. ini_t *ini = (ini_t *) fd;
  186. #ifdef INI_ADD_LIST_SUPPORT
  187. if (ini->listDelims)
  188. {
  189. char *data = __ini_readList (ini);
  190. if (!data)
  191. return -1;
  192. sscanf (data, "%d", value);
  193. }
  194. else
  195. #endif // INI_ADD_LIST_SUPPORT
  196. {
  197. size_t length;
  198. if (__ini_read (ini, &length) < 0)
  199. return -1;
  200. if (length > 0)
  201. fscanf (ini->ftmp, "%d", value);
  202. }
  203. return 0;
  204. }
  205. #ifdef INI_ADD_EXTRAS
  206. /********************************************************************************************************************
  207. * Function : ini_readLong
  208. * Parameters : ini - pointer to ini file database.
  209. * : value - keys data
  210. * Returns : -1 for error or the number of values read.
  211. * Globals Used :
  212. * Globals Modified :
  213. * Description : Reads data part from a key and returns it as a long
  214. ********************************************************************************************************************
  215. * Rev | Date | By | Comment
  216. * ----------------------------------------------------------------------------------------------------------------
  217. ********************************************************************************************************************/
  218. int INI_LINKAGE ini_readLong (ini_fd_t fd, long *value)
  219. {
  220. ini_t *ini = (ini_t *) fd;
  221. #ifdef INI_ADD_LIST_SUPPORT
  222. if (ini->listDelims)
  223. {
  224. char *data = __ini_readList (ini);
  225. if (!data)
  226. return -1;
  227. sscanf (data, "%ld", value);
  228. }
  229. else
  230. #endif // INI_ADD_LIST_SUPPORT
  231. {
  232. size_t length;
  233. if (__ini_read (ini, &length) < 0)
  234. return -1;
  235. if (length > 0)
  236. fscanf (ini->ftmp, "%ld", value);
  237. }
  238. return 0;
  239. }
  240. /********************************************************************************************************************
  241. * Function : ini_readDouble
  242. * Parameters : ini - pointer to ini file database.
  243. * : value - keys data
  244. * Returns : -1 for error or the number of values read.
  245. * Globals Used :
  246. * Globals Modified :
  247. * Description : Reads data part from a key and returns it as a double (real)
  248. ********************************************************************************************************************
  249. * Rev | Date | By | Comment
  250. * ----------------------------------------------------------------------------------------------------------------
  251. ********************************************************************************************************************/
  252. int INI_LINKAGE ini_readDouble (ini_fd_t fd, double *value)
  253. {
  254. ini_t *ini = (ini_t *) fd;
  255. #ifdef INI_ADD_LIST_SUPPORT
  256. if (ini->listDelims)
  257. {
  258. char *data = __ini_readList (ini);
  259. if (!data)
  260. return -1;
  261. sscanf (data, "%lf", value);
  262. }
  263. else
  264. #endif // INI_ADD_LIST_SUPPORT
  265. {
  266. size_t length;
  267. if (__ini_read (ini, &length) < 0)
  268. return -1;
  269. if (length > 0)
  270. fscanf (ini->ftmp, "%lf", value);
  271. }
  272. return 0;
  273. }
  274. /********************************************************************************************************************
  275. * Function : ini_readBool
  276. * Parameters : ini - pointer to ini file database.
  277. * : value - keys data
  278. * Returns : -1 for error or the number of values read.
  279. * Globals Used :
  280. * Globals Modified :
  281. * Description : Reads data part from a key and returns it as a int. Supported bool strings are 0, 1, true
  282. * : false.
  283. ********************************************************************************************************************
  284. * Rev | Date | By | Comment
  285. * ----------------------------------------------------------------------------------------------------------------
  286. ********************************************************************************************************************/
  287. int INI_LINKAGE ini_readBool (ini_fd_t fd, int *value)
  288. {
  289. ini_t *ini = (ini_t *) fd;
  290. char buffer[6] = "";
  291. #ifdef INI_ADD_LIST_SUPPORT
  292. if (ini->listDelims)
  293. {
  294. char *data = __ini_readList (ini);
  295. if (!data)
  296. return -1;
  297. sscanf (data, "%5s", buffer);
  298. }
  299. else
  300. #endif // INI_ADD_LIST_SUPPORT
  301. {
  302. size_t length;
  303. if (__ini_read (ini, &length) < 0)
  304. return -1;
  305. if (length > 0)
  306. fscanf (ini->ftmp, "%5s", buffer);
  307. }
  308. { // Convert string to lower case
  309. char *p = buffer;
  310. while (*p != '\0')
  311. {
  312. *p = (char) tolower (*p);
  313. p++;
  314. }
  315. }
  316. // Decode supported bool types
  317. switch (*buffer)
  318. {
  319. case '0':
  320. case '1':
  321. if (buffer[1] != '\0')
  322. return -1;
  323. *value = *buffer - '0';
  324. break;
  325. case 't':
  326. if (strcmp (buffer, "true"))
  327. return -1;
  328. *value = 1;
  329. break;
  330. case 'f':
  331. if (strcmp (buffer, "false"))
  332. return -1;
  333. *value = 0;
  334. break;
  335. default:
  336. // No match
  337. return -1;
  338. }
  339. return 0;
  340. }
  341. /********************************************************************************************************************
  342. * Function : ini_writeInt
  343. * Parameters : ini - pointer to ini file database.
  344. * : value - keys data
  345. * Returns : -1 for Error and 0 on success
  346. * Globals Used :
  347. * Globals Modified :
  348. * Description : Writes data part to a key.
  349. * : Headings and keys will be created as necessary
  350. ********************************************************************************************************************
  351. * Rev | Date | By | Comment
  352. * ----------------------------------------------------------------------------------------------------------------
  353. ********************************************************************************************************************/
  354. int INI_LINKAGE ini_writeInt (ini_fd_t fd, int value)
  355. {
  356. ini_t *ini = (ini_t *) fd;
  357. struct key_tag *_key;
  358. long pos;
  359. _key = __ini_write (ini);
  360. if (!_key)
  361. return -1;
  362. // Write data to bottom of backup file
  363. fprintf (ini->ftmp, "%d", value);
  364. pos = ftell (ini->ftmp);
  365. _key->length = (size_t) (pos - _key->pos);
  366. fprintf (ini->ftmp, "\n");
  367. return 0;
  368. }
  369. /********************************************************************************************************************
  370. * Function : ini_writeLong
  371. * Parameters : ini - pointer to ini file database.
  372. * : value - keys data
  373. * Returns : -1 for Error and 0 on success
  374. * Globals Used :
  375. * Globals Modified :
  376. * Description : Writes data part to a key.
  377. * : Headings and keys will be created as necessary
  378. ********************************************************************************************************************
  379. * Rev | Date | By | Comment
  380. * ----------------------------------------------------------------------------------------------------------------
  381. ********************************************************************************************************************/
  382. int INI_LINKAGE ini_writeLong (ini_fd_t fd, long value)
  383. {
  384. ini_t *ini = (ini_t *) fd;
  385. struct key_tag *_key;
  386. long pos;
  387. _key = __ini_write (ini);
  388. if (!_key)
  389. return -1;
  390. // Write data to bottom of backup file
  391. fprintf (ini->ftmp, "%ld", value);
  392. pos = ftell (ini->ftmp);
  393. _key->length = (size_t) (pos - _key->pos);
  394. fprintf (ini->ftmp, "\n");
  395. return 0;
  396. }
  397. /********************************************************************************************************************
  398. * Function : ini_writeDouble
  399. * Parameters : ini - pointer to ini file database.
  400. * : value - keys data
  401. * Returns : -1 for Error and 0 on success
  402. * Globals Used :
  403. * Globals Modified :
  404. * Description : Writes data part to a key.
  405. * : Headings and keys will be created as necessary
  406. ********************************************************************************************************************
  407. * Rev | Date | By | Comment
  408. * ----------------------------------------------------------------------------------------------------------------
  409. ********************************************************************************************************************/
  410. int INI_LINKAGE ini_writeDouble (ini_fd_t fd, double value)
  411. {
  412. ini_t *ini = (ini_t *) fd;
  413. struct key_tag *_key;
  414. long pos;
  415. _key = __ini_write (ini);
  416. if (!_key)
  417. return -1;
  418. // Write data to bottom of backup file
  419. fprintf (ini->ftmp, "%f", value);
  420. pos = ftell (ini->ftmp);
  421. _key->length = (size_t) (pos - _key->pos);
  422. fprintf (ini->ftmp, "\n");
  423. return 0;
  424. }
  425. /********************************************************************************************************************
  426. * Function : ini_writeBool
  427. * Parameters : ini - pointer to ini file database.
  428. * : value - keys data
  429. * Returns : -1 for Error and 0 on success
  430. * Globals Used :
  431. * Globals Modified :
  432. * Description : Writes data part to a key.
  433. * : Headings and keys will be created as necessary
  434. ********************************************************************************************************************
  435. * Rev | Date | By | Comment
  436. * ----------------------------------------------------------------------------------------------------------------
  437. ********************************************************************************************************************/
  438. int INI_LINKAGE ini_writeBool (ini_fd_t fd, int value)
  439. {
  440. ini_t *ini = (ini_t *) fd;
  441. struct key_tag *_key;
  442. long pos;
  443. // Check if value is legal
  444. if ((value < 0) || (value > 1))
  445. return -1;
  446. _key = __ini_write (ini);
  447. if (!_key)
  448. return -1;
  449. // Write data to bottom of backup file
  450. if (value)
  451. fprintf (ini->ftmp, "true");
  452. else
  453. fprintf (ini->ftmp, "false");
  454. pos = ftell (ini->ftmp);
  455. _key->length = (size_t) (pos - _key->pos);
  456. fprintf (ini->ftmp, "\n");
  457. return 0;
  458. }
  459. #endif // INI_ADD_EXTRAS