configfile_fk.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // configfile_fk.c - handles loading and saving the configuration options
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include "configfile_fk.h"
  9. #define ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
  10. enum ConfigOptionType {
  11. CONFIG_TYPE_BOOL,
  12. CONFIG_TYPE_UINT,
  13. CONFIG_TYPE_FLOAT,
  14. CONFIG_TYPE_ASPECT_RATIO,
  15. };
  16. struct ConfigOption {
  17. const char *name;
  18. enum ConfigOptionType type;
  19. union {
  20. bool *boolValue;
  21. unsigned int *uintValue;
  22. float *floatValue;
  23. };
  24. };
  25. #undef X
  26. #define X(a, b) b,
  27. const char *aspect_ratio_name[] = {ASPECT_RATIOS};
  28. unsigned int aspect_ratio_factor_step = 10;
  29. /*
  30. *Config options and default values
  31. */
  32. unsigned int aspect_ratio = ASPECT_RATIOS_TYPE_STRETCHED;
  33. unsigned int aspect_ratio_factor_percent = 50;
  34. static const struct ConfigOption options[] = {
  35. {.name = "aspect_ratio", .type = CONFIG_TYPE_ASPECT_RATIO, .uintValue = &aspect_ratio},
  36. {.name = "aspect_ratio_factor_percent", .type = CONFIG_TYPE_UINT, .uintValue = &aspect_ratio_factor_percent},
  37. };
  38. // Reads an entire line from a file (excluding the newline character) and returns an allocated string
  39. // Returns NULL if no lines could be read from the file
  40. static char *read_file_line(FILE *file) {
  41. char *buffer;
  42. size_t bufferSize = 64;
  43. size_t offset = 0; // offset in buffer to write
  44. buffer = (char*)malloc(bufferSize);
  45. while (1) {
  46. // Read a line from the file
  47. if (fgets(buffer + offset, bufferSize - offset, file) == NULL) {
  48. free(buffer);
  49. return NULL; // Nothing could be read.
  50. }
  51. offset = strlen(buffer);
  52. assert(offset > 0);
  53. if (feof(file)) // EOF was reached
  54. break;
  55. // If a newline was found, remove the trailing newline and exit
  56. if (buffer[offset - 1] == '\n') {
  57. buffer[offset - 1] = '\0';
  58. break;
  59. }
  60. // If no newline or EOF was reached, then the whole line wasn't read.
  61. bufferSize *= 2; // Increase buffer size
  62. buffer = (char*)realloc(buffer, bufferSize);
  63. assert(buffer != NULL);
  64. }
  65. return buffer;
  66. }
  67. // Returns the position of the first non-whitespace character
  68. static char *skip_whitespace(char *str) {
  69. while (isspace(*str))
  70. str++;
  71. return str;
  72. }
  73. // Returns the position of the first non-whitespace or '=' character
  74. static char *skip_whitespace_or_equal(char *str) {
  75. while (isspace(*str) || *str=='=')
  76. str++;
  77. return str;
  78. }
  79. // NULL-terminates the current whitespace-delimited word, and returns a pointer to the next word
  80. static char *word_split(char *str) {
  81. // Precondition: str must not point to whitespace
  82. assert(!isspace(*str));
  83. // Find either the next whitespace, '=' or end of string
  84. while (!isspace(*str) && *str != '\0' && *str != '=')
  85. str++;
  86. if (*str == '\0') // End of string
  87. return str;
  88. // Terminate current word
  89. *(str++) = '\0';
  90. // Skip whitespace to next word
  91. return skip_whitespace_or_equal(str);
  92. }
  93. // Splits a string into words, and stores the words into the 'tokens' array
  94. // 'maxTokens' is the length of the 'tokens' array
  95. // Returns the number of tokens parsed
  96. static unsigned int tokenize_string(char *str, int maxTokens, char **tokens) {
  97. int count = 0;
  98. str = skip_whitespace(str);
  99. while (str[0] != '\0' && count < maxTokens) {
  100. tokens[count] = str;
  101. str = word_split(str);
  102. count++;
  103. }
  104. return count;
  105. }
  106. // Loads the config file specified by 'filepath'
  107. void configfile_load(const char *filepath) {
  108. FILE *file;
  109. char *line;
  110. unsigned int cur_line = 0;
  111. char *current_section = NULL;
  112. printf("Loading configuration from '%s'\n", filepath);
  113. // Open file or create it if it does not exist
  114. file = fopen(filepath, "r");
  115. if (file == NULL) {
  116. // Create a new config file and save defaults
  117. printf("Config file '%s' not found. Creating it.\n", filepath);
  118. configfile_save(filepath);
  119. return;
  120. }
  121. // Go through each line in the file
  122. while ((line = read_file_line(file)) != NULL) {
  123. char *p = line;
  124. char *tokens[2];
  125. int numTokens;
  126. cur_line++;
  127. // Get tokens
  128. while (isspace(*p)) p++;
  129. numTokens = tokenize_string(p, 2, tokens);
  130. // Get content
  131. if (numTokens != 0) {
  132. // Pass comments
  133. if(tokens[0][0]=='#') continue;
  134. // Check sections - useless for now
  135. if(tokens[0][0]=='['){
  136. p=tokens[0];
  137. while(*p != '\0' && *p!=']') p++;
  138. if(*p == '\0') continue;
  139. *p=0;
  140. if(current_section) free(current_section);
  141. current_section = (char*)malloc(strlen(tokens[0])); //strlen(tokens[0])-1+1
  142. strcpy(current_section, &tokens[0][1]);
  143. printf("New Section: %s\n", current_section);
  144. continue;
  145. }
  146. if (numTokens == 2) {
  147. const struct ConfigOption *option = NULL;
  148. for (unsigned int i = 0; i < ARRAY_LEN(options); i++) {
  149. if (strcmp(tokens[0], options[i].name) == 0) {
  150. option = &options[i];
  151. break;
  152. }
  153. }
  154. if (option == NULL){
  155. printf("Unknown option '%s'\n", tokens[0]);
  156. }
  157. else {
  158. printf("Reading option: '%s', value: '%s'\n", tokens[0], tokens[1]);
  159. switch (option->type) {
  160. case CONFIG_TYPE_BOOL:
  161. if (strcmp(tokens[1], "true") == 0)
  162. *option->boolValue = true;
  163. else if (strcmp(tokens[1], "false") == 0)
  164. *option->boolValue = false;
  165. else{
  166. printf("Unknown CONFIG_TYPE_BOOL value: '%s', using default: %s\n",
  167. tokens[1], (*option->boolValue)?"true":"false");
  168. }
  169. break;
  170. case CONFIG_TYPE_UINT:
  171. sscanf(tokens[1], "%u", option->uintValue);
  172. break;
  173. case CONFIG_TYPE_FLOAT:
  174. sscanf(tokens[1], "%f", option->floatValue);
  175. break;
  176. case CONFIG_TYPE_ASPECT_RATIO:
  177. ;unsigned int cur_ar;
  178. for(cur_ar=0; cur_ar<NB_ASPECT_RATIOS_TYPES; cur_ar++){
  179. if(!strcmp(aspect_ratio_name[cur_ar], tokens[1])){
  180. *option->uintValue = cur_ar;
  181. break;
  182. }
  183. }
  184. if(cur_ar >= NB_ASPECT_RATIOS_TYPES){
  185. printf("Unknown CONFIG_TYPE_ASPECT_RATIO value: '%s', using default value: %s\n",
  186. tokens[1], aspect_ratio_name[*option->uintValue]);
  187. }
  188. break;
  189. default:
  190. printf("Unknown option type '%d'\n", option->type);
  191. break;
  192. }
  193. }
  194. }
  195. else{
  196. fprintf(stderr, "Error in line %d: wrong format\n", cur_line);
  197. }
  198. }
  199. free(line);
  200. }
  201. fclose(file);
  202. }
  203. // Writes the config file to 'filepath'
  204. void configfile_save(const char *filepath) {
  205. FILE *file;
  206. printf("Saving configuration to '%s'\n", filepath);
  207. file = fopen(filepath, "w");
  208. if (file == NULL) {
  209. // error
  210. printf("Could not save\n");
  211. return;
  212. }
  213. printf("Saved !\n");
  214. for (unsigned int i = 0; i < ARRAY_LEN(options); i++) {
  215. const struct ConfigOption *option = &options[i];
  216. switch (option->type) {
  217. case CONFIG_TYPE_BOOL:
  218. fprintf(file, "%s = %s\n", option->name, *option->boolValue ? "true" : "false");
  219. break;
  220. case CONFIG_TYPE_UINT:
  221. fprintf(file, "%s = %u\n", option->name, *option->uintValue);
  222. break;
  223. case CONFIG_TYPE_FLOAT:
  224. fprintf(file, "%s = %f\n", option->name, *option->floatValue);
  225. break;
  226. case CONFIG_TYPE_ASPECT_RATIO:
  227. fprintf(file, "%s = %s\n", option->name, aspect_ratio_name[*option->uintValue]);
  228. break;
  229. default:
  230. assert(0); // unknown type
  231. }
  232. }
  233. fclose(file);
  234. }