alcConfig.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW64__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include "alMain.h"
  33. #ifdef _WIN32_IE
  34. #include <shlobj.h>
  35. #endif
  36. typedef struct ConfigEntry {
  37. char *key;
  38. char *value;
  39. } ConfigEntry;
  40. typedef struct ConfigBlock {
  41. char *name;
  42. ConfigEntry *entries;
  43. size_t entryCount;
  44. } ConfigBlock;
  45. static ConfigBlock *cfgBlocks;
  46. static size_t cfgCount;
  47. static char buffer[1024];
  48. static void LoadConfigFromFile(FILE *f)
  49. {
  50. ConfigBlock *curBlock = cfgBlocks;
  51. ConfigEntry *ent;
  52. while(fgets(buffer, sizeof(buffer), f))
  53. {
  54. size_t i = 0;
  55. while(isspace(buffer[i]))
  56. i++;
  57. if(!buffer[i] || buffer[i] == '#')
  58. continue;
  59. memmove(buffer, buffer+i, strlen(buffer+i)+1);
  60. if(buffer[0] == '[')
  61. {
  62. ConfigBlock *nextBlock;
  63. i = 1;
  64. while(buffer[i] && buffer[i] != ']')
  65. i++;
  66. if(!buffer[i])
  67. {
  68. AL_PRINT("config parse error: bad line \"%s\"\n", buffer);
  69. continue;
  70. }
  71. buffer[i] = 0;
  72. do {
  73. i++;
  74. if(buffer[i] && !isspace(buffer[i]))
  75. {
  76. if(buffer[i] != '#')
  77. AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i);
  78. break;
  79. }
  80. } while(buffer[i]);
  81. nextBlock = NULL;
  82. for(i = 0;i < cfgCount;i++)
  83. {
  84. if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
  85. {
  86. nextBlock = cfgBlocks+i;
  87. // AL_PRINT("found block '%s'\n", nextBlock->name);
  88. break;
  89. }
  90. }
  91. if(!nextBlock)
  92. {
  93. nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
  94. if(!nextBlock)
  95. {
  96. AL_PRINT("config parse error: error reallocating config blocks\n");
  97. continue;
  98. }
  99. cfgBlocks = nextBlock;
  100. nextBlock = cfgBlocks+cfgCount;
  101. cfgCount++;
  102. nextBlock->name = strdup(buffer+1);
  103. nextBlock->entries = NULL;
  104. nextBlock->entryCount = 0;
  105. // AL_PRINT("found new block '%s'\n", nextBlock->name);
  106. }
  107. curBlock = nextBlock;
  108. continue;
  109. }
  110. /* Look for the option name */
  111. i = 0;
  112. while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
  113. !isspace(buffer[i]))
  114. i++;
  115. if(!buffer[i] || buffer[i] == '#' || i == 0)
  116. {
  117. AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer);
  118. continue;
  119. }
  120. /* Seperate the option */
  121. if(buffer[i] != '=')
  122. {
  123. buffer[i++] = 0;
  124. while(isspace(buffer[i]))
  125. i++;
  126. if(buffer[i] != '=')
  127. {
  128. AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer);
  129. continue;
  130. }
  131. }
  132. /* Find the start of the value */
  133. buffer[i++] = 0;
  134. while(isspace(buffer[i]))
  135. i++;
  136. /* Check if we already have this option set */
  137. ent = curBlock->entries;
  138. while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
  139. {
  140. if(strcasecmp(ent->key, buffer) == 0)
  141. break;
  142. ent++;
  143. }
  144. if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
  145. {
  146. /* Allocate a new option entry */
  147. ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
  148. if(!ent)
  149. {
  150. AL_PRINT("config parse error: error reallocating config entries\n");
  151. continue;
  152. }
  153. curBlock->entries = ent;
  154. ent = curBlock->entries + curBlock->entryCount;
  155. curBlock->entryCount++;
  156. ent->key = strdup(buffer);
  157. ent->value = NULL;
  158. }
  159. /* Look for the end of the line (Null term, new-line, or #-symbol) and
  160. eat up the trailing whitespace */
  161. memmove(buffer, buffer+i, strlen(buffer+i)+1);
  162. i = 0;
  163. while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
  164. i++;
  165. do {
  166. i--;
  167. } while(isspace(buffer[i]));
  168. buffer[++i] = 0;
  169. free(ent->value);
  170. ent->value = strdup(buffer);
  171. // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
  172. }
  173. }
  174. void ReadALConfig(void)
  175. {
  176. FILE *f;
  177. cfgBlocks = calloc(1, sizeof(ConfigBlock));
  178. cfgBlocks->name = strdup("general");
  179. cfgCount = 1;
  180. #ifdef _WIN32
  181. if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  182. {
  183. size_t p = strlen(buffer);
  184. snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
  185. f = fopen(buffer, "rt");
  186. if(f)
  187. {
  188. LoadConfigFromFile(f);
  189. fclose(f);
  190. }
  191. }
  192. #else
  193. f = fopen("/etc/openal/alsoft.conf", "r");
  194. if(f)
  195. {
  196. LoadConfigFromFile(f);
  197. fclose(f);
  198. }
  199. if(getenv("HOME") && *(getenv("HOME")))
  200. {
  201. snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", getenv("HOME"));
  202. f = fopen(buffer, "r");
  203. if(f)
  204. {
  205. LoadConfigFromFile(f);
  206. fclose(f);
  207. }
  208. }
  209. #endif
  210. if(getenv("ALSOFT_CONF"))
  211. {
  212. f = fopen(getenv("ALSOFT_CONF"), "r");
  213. if(f)
  214. {
  215. LoadConfigFromFile(f);
  216. fclose(f);
  217. }
  218. }
  219. }
  220. void FreeALConfig(void)
  221. {
  222. size_t i;
  223. for(i = 0;i < cfgCount;i++)
  224. {
  225. size_t j;
  226. for(j = 0;j < cfgBlocks[i].entryCount;j++)
  227. {
  228. free(cfgBlocks[i].entries[j].key);
  229. free(cfgBlocks[i].entries[j].value);
  230. }
  231. free(cfgBlocks[i].entries);
  232. free(cfgBlocks[i].name);
  233. }
  234. free(cfgBlocks);
  235. cfgBlocks = NULL;
  236. cfgCount = 0;
  237. }
  238. const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
  239. {
  240. size_t i, j;
  241. if(!keyName)
  242. return def;
  243. if(!blockName)
  244. blockName = "general";
  245. for(i = 0;i < cfgCount;i++)
  246. {
  247. if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
  248. continue;
  249. for(j = 0;j < cfgBlocks[i].entryCount;j++)
  250. {
  251. if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
  252. {
  253. if(cfgBlocks[i].entries[j].value[0])
  254. return cfgBlocks[i].entries[j].value;
  255. return def;
  256. }
  257. }
  258. }
  259. return def;
  260. }
  261. int ConfigValueExists(const char *blockName, const char *keyName)
  262. {
  263. const char *val = GetConfigValue(blockName, keyName, "");
  264. return !!val[0];
  265. }
  266. int GetConfigValueInt(const char *blockName, const char *keyName, int def)
  267. {
  268. const char *val = GetConfigValue(blockName, keyName, "");
  269. if(!val[0]) return def;
  270. return strtol(val, NULL, 0);
  271. }
  272. float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
  273. {
  274. const char *val = GetConfigValue(blockName, keyName, "");
  275. if(!val[0]) return def;
  276. #ifdef HAVE_STRTOF
  277. return strtof(val, NULL);
  278. #else
  279. return (float)strtod(val, NULL);
  280. #endif
  281. }
  282. int GetConfigValueBool(const char *blockName, const char *keyName, int def)
  283. {
  284. const char *val = GetConfigValue(blockName, keyName, "");
  285. if(!val[0]) return !!def;
  286. return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  287. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  288. }