jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
9.7KB

  1. /*
  2. History :
  3. 01-28-02 : Change the location of temporary files created in write_private_profile_string
  4. now done in TmpDirectory.
  5. 01-29-02 : Correct bug when the '=' character is not present.
  6. 06-18-02 : Return default value if file does not exist, new write_private_profile_int function.
  7. */
  8. /***** Routines to read profile strings -- by Joseph J. Graf ******/
  9. /***** corrections and improvements -- by D. Fober - Grame ******/
  10. /*
  11. corrections: buffer sizes control
  12. improvements: behavior more similar to windows
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <string>
  19. #include <ctype.h>
  20. #include "profport.h" /* function prototypes in here */
  21. #ifndef _WIN32
  22. static int read_line (FILE *fp, char *bp, int size);
  23. static int read_section(FILE *fp, char *section);
  24. static int read_entry (FILE *fp, char *entry, char *buff, int size);
  25. static char * read_value (char *buff);
  26. static int read_int_value (char *buff, int def);
  27. static char * read_file (char *file);
  28. static char * str_search (char * buff, char * str, int stopCond);
  29. /*****************************************************************
  30. * Function: read_line()
  31. * Arguments: <FILE *> fp - a pointer to the file to be read from
  32. * <char *> bp - a pointer to the copy buffer
  33. * <int> size - size of the copy buffer
  34. * Returns: the line length if successful -1 otherwise
  35. ******************************************************************/
  36. static int read_line(FILE *fp, char *bp, int size)
  37. {
  38. char c = '\0';
  39. int i = 0, limit = size-2;
  40. /* Read one line from the source file */
  41. while (((c = getc(fp)) != '\n') && (i < limit)) {
  42. if (c == EOF) {
  43. if (!i) return -1;
  44. else break;
  45. }
  46. bp[i++] = c;
  47. }
  48. bp[i] = '\0';
  49. return i;
  50. }
  51. static int read_section (FILE *fp, char *section)
  52. {
  53. char buff[MAX_LINE_LENGTH];
  54. char t_section[MAX_LINE_LENGTH];
  55. int n, slen;
  56. sprintf(t_section,"[%s]", section); /* Format the section name */
  57. slen = strlen (t_section);
  58. /* Move through file 1 line at a time until a section is matched or EOF */
  59. do {
  60. n = read_line(fp, buff, MAX_LINE_LENGTH);
  61. if (n == -1)
  62. return 0;
  63. } while (strncmp (buff,t_section, slen));
  64. return 1;
  65. }
  66. static int read_entry (FILE *fp, char *entry, char *buff, int size)
  67. {
  68. int n, elen = strlen (entry);
  69. do {
  70. n = read_line(fp, buff, size);
  71. if (n == -1)
  72. return 0;
  73. else if (*buff == '[')
  74. return 0;
  75. } while (strncmp (buff, entry, elen));
  76. return 1;
  77. }
  78. #define isBlank(c) ((c == ' ') || (c == '\t'))
  79. static char * read_value (char *buff)
  80. {
  81. char * eq = strrchr (buff,'='); /* Parse out the equal sign */
  82. if (eq) {
  83. eq++;
  84. while (*eq && isBlank(*eq))
  85. eq++;
  86. // return *eq ? eq : 0;
  87. return eq;
  88. }
  89. return eq;
  90. }
  91. #define isSignedDigit(c) (isdigit(c) || (c == '+') || (c == '-'))
  92. static int read_int_value (char *buff, int def)
  93. {
  94. char * val = read_value (buff);
  95. char value[20]; int i;
  96. if (!*val) return def;
  97. for (i = 0; isSignedDigit(*val) && (i <= 10); i++ )
  98. value[i] = *val++;
  99. value[i] = '\0';
  100. return value[0] ? atoi(value) : def;
  101. }
  102. static char * read_file (char *file)
  103. {
  104. FILE *fd = fopen (file,"r");
  105. int size; char * buff = 0;
  106. if (!fd) return 0;
  107. if (fseek (fd, 0, SEEK_END) == -1) goto err;
  108. size = ftell (fd);
  109. if (size < 0) goto err;
  110. if (fseek (fd, 0, SEEK_SET) == -1) goto err;
  111. buff = (char *) malloc (size+1);
  112. if (buff) {
  113. *buff = 0;
  114. fread (buff, 1, size, fd);
  115. buff[size] = 0;
  116. }
  117. err:
  118. fclose (fd);
  119. return buff;
  120. }
  121. static char * str_search (char * buff, char * str, int stopCond)
  122. {
  123. char *ptr = buff;
  124. int len = strlen (str);
  125. while (*ptr && strncmp (ptr, str, len)) {
  126. while (*ptr && (*ptr++ != '\n'))
  127. ;
  128. if (*ptr == stopCond)
  129. return 0;
  130. }
  131. return *ptr ? ptr : 0;
  132. }
  133. /**************************************************************************
  134. * Function: get_private_profile_int()
  135. * Arguments: <char *> section - the name of the section to search for
  136. * <char *> entry - the name of the entry to find the value of
  137. * <int> def - the default value in the event of a failed read
  138. * <char *> file_name - the name of the .ini file to read from
  139. * Returns: the value located at entry
  140. ***************************************************************************/
  141. int get_private_profile_int(char *section,
  142. char *entry, int def, char *file_name)
  143. {
  144. FILE *fp = fopen(file_name,"r");
  145. char buff[MAX_LINE_LENGTH];
  146. if( !fp ) return def; /* Return default value if file does not exist */
  147. if (!read_section (fp, section)) goto err;
  148. if (!read_entry (fp, entry, buff, MAX_LINE_LENGTH)) goto err;
  149. def = read_int_value (buff, def);
  150. err:
  151. fclose (fp);
  152. return def;
  153. }
  154. /**************************************************************************
  155. * Function: get_private_profile_string()
  156. * Arguments: <char *> section - the name of the section to search for
  157. * <char *> entry - the name of the entry to find the value of
  158. * <char *> def - default string in the event of a failed read
  159. * <char *> buffer - a pointer to the buffer to copy into
  160. * <int> buffer_len - the max number of characters to copy
  161. * <char *> file_name - the name of the .ini file to read from
  162. * Returns: the number of characters copied into the supplied buffer
  163. ***************************************************************************/
  164. int get_private_profile_string(char *section, char *entry, char *def,
  165. char *buffer, int buffer_len, char *file_name)
  166. {
  167. FILE *fp = fopen (file_name,"r");
  168. char buff[MAX_LINE_LENGTH];
  169. char *val;
  170. if( !fp ) goto err; /* Return default value if file does not exist */
  171. if (!read_section (fp, section)) goto err;
  172. if (!read_entry (fp, entry, buff, MAX_LINE_LENGTH)) goto err;
  173. val = read_value (buff);
  174. if(val) def = val;
  175. err:
  176. if (fp) fclose (fp);
  177. if (def) {
  178. strncpy (buffer, def, buffer_len - 1);
  179. buffer[buffer_len] = '\0';
  180. }
  181. else buffer[buffer_len] = '\0';
  182. return strlen (buffer);
  183. }
  184. /***************************************************************************
  185. * Function: write_private_profile_string()
  186. * Arguments: <char *> section - the name of the section to search for
  187. * <char *> entry - the name of the entry to find the value of
  188. * <char *> buffer - pointer to the buffer that holds the string
  189. * <char *> file_name - the name of the .ini file to read from
  190. * Returns: TRUE if successful, otherwise FALSE
  191. ***************************************************************************/
  192. int write_private_profile_string(char *section,
  193. char *entry, char *buffer, char *file_name)
  194. {
  195. char * content = read_file(file_name);
  196. FILE * fd = fopen(file_name,"w");
  197. char t_section[MAX_LINE_LENGTH], *ptr;
  198. int ret = 0;
  199. if (!fd) goto end;
  200. if (!content) {
  201. fprintf (fd, "[%s]\n%s = %s\n", section, entry, buffer);
  202. ret = 1;
  203. goto end;
  204. }
  205. sprintf(t_section,"[%s]",section); /* Format the section name */
  206. ptr = str_search (content, t_section, 0); /* look for the section start */
  207. if (!ptr) {
  208. /* no such section: add the new section at end of file */
  209. fprintf (fd, "%s\n[%s]\n%s = %s\n", content, section, entry, buffer);
  210. }
  211. else {
  212. char * eptr;
  213. eptr = str_search (ptr, entry, '[');
  214. if (!eptr) {
  215. /* no such entry: looks for next section */
  216. eptr = str_search (++ptr, "[", 0);
  217. if (!eptr) {
  218. /* section is the last one */
  219. fprintf (fd, "%s\n%s = %s\n", content, entry, buffer);
  220. }
  221. else {
  222. while (*ptr && (*ptr != '\n')) ptr++;
  223. *ptr = 0;
  224. fprintf (fd, "%s\n%s = %s", content, entry, buffer);
  225. *ptr = '\n';
  226. fprintf (fd, "%s", ptr);
  227. }
  228. }
  229. else {
  230. *eptr++ = 0;
  231. fprintf (fd, "%s%s = %s", content, entry, buffer);
  232. while (*eptr && (*eptr != '\n')) eptr++;
  233. if (eptr) fprintf (fd, "%s", eptr);
  234. }
  235. }
  236. ret = 1;
  237. end:
  238. if (content) free(content);
  239. if (fd) fclose(fd);
  240. return 0;
  241. }
  242. /***************************************************************************
  243. * Function: write_private_profile_int()
  244. * Arguments: <char *> section - the name of the section to search for
  245. * <char *> entry - the name of the entry to find the value of
  246. * <int> buffer - the value to be written
  247. * <char *> file_name - the name of the .ini file to read from
  248. * Returns: TRUE if successful, otherwise FALSE
  249. ***************************************************************************/
  250. int write_private_profile_int(char *section,
  251. char *entry, int val, char *file_name)
  252. {
  253. char buffer [64];
  254. sprintf(buffer, "%d", val);
  255. return write_private_profile_string (section,entry, buffer, file_name);
  256. }
  257. #endif // #ifndef WIN32
  258. /**************************************************************************
  259. * Function: get_private_profile_float()
  260. * Arguments: <char *> section - the name of the section to search for
  261. * <char *> entry - the name of the entry to find the value of
  262. * <float> def - the default value in the event of a failed read
  263. * <char *> file_name - the name of the .ini file to read from
  264. * Returns: the value located at entry
  265. * Warning: The float value to be read must not contain more than 100 digits.
  266. * Author: CD, 15/11/2006.
  267. ***************************************************************************/
  268. #define maxFloatLen 100
  269. float get_private_profile_float (char * section, char * entry, float def, char * file_name)
  270. {
  271. float result = def;
  272. char buffer[ maxFloatLen ], *endptr;
  273. if ( get_private_profile_string(section, entry, "", buffer, maxFloatLen, file_name) > 0 )
  274. {
  275. result = (float)strtod(buffer, &endptr);
  276. if ((result==0) && (endptr==buffer))
  277. result = def;
  278. }
  279. return result;
  280. }