alcConfig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 __MINGW32__
  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. ConfigEntry *entries;
  42. unsigned int entryCount;
  43. } ConfigBlock;
  44. static ConfigBlock cfgBlock;
  45. static char buffer[1024];
  46. static char *lstrip(char *line)
  47. {
  48. while(isspace(line[0]))
  49. line++;
  50. return line;
  51. }
  52. static char *rstrip(char *line)
  53. {
  54. size_t len = strlen(line);
  55. while(len > 0 && isspace(line[len-1]))
  56. len--;
  57. line[len] = 0;
  58. return line;
  59. }
  60. static void LoadConfigFromFile(FILE *f)
  61. {
  62. char curSection[128] = "";
  63. ConfigEntry *ent;
  64. while(fgets(buffer, sizeof(buffer), f))
  65. {
  66. char *line, *comment;
  67. char key[256] = "";
  68. char value[256] = "";
  69. comment = strchr(buffer, '#');
  70. if(comment)
  71. {
  72. *(comment++) = 0;
  73. comment = rstrip(lstrip(comment));
  74. }
  75. line = rstrip(lstrip(buffer));
  76. if(!line[0])
  77. continue;
  78. if(line[0] == '[')
  79. {
  80. char *section = line+1;
  81. char *endsection;
  82. endsection = strchr(section, ']');
  83. if(!endsection || section == endsection || endsection[1] != 0)
  84. {
  85. ERR("config parse error: bad line \"%s\"\n", line);
  86. continue;
  87. }
  88. *endsection = 0;
  89. if(strcasecmp(section, "general") == 0)
  90. curSection[0] = 0;
  91. else
  92. {
  93. strncpy(curSection, section, sizeof(curSection)-1);
  94. curSection[sizeof(curSection)-1] = 0;
  95. }
  96. continue;
  97. }
  98. if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
  99. sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
  100. sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
  101. {
  102. /* sscanf doesn't handle '' or "" as empty values, so clip it
  103. * manually. */
  104. if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
  105. value[0] = 0;
  106. }
  107. else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
  108. {
  109. /* Special case for 'key =' */
  110. value[0] = 0;
  111. }
  112. else
  113. {
  114. ERR("config parse error: malformed option line: \"%s\"\n\n", line);
  115. continue;
  116. }
  117. rstrip(key);
  118. if(curSection[0] != 0)
  119. {
  120. size_t len = strlen(curSection);
  121. memmove(&key[len+1], key, sizeof(key)-1-len);
  122. key[len] = '/';
  123. memcpy(key, curSection, len);
  124. }
  125. /* Check if we already have this option set */
  126. ent = cfgBlock.entries;
  127. while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
  128. {
  129. if(strcasecmp(ent->key, key) == 0)
  130. break;
  131. ent++;
  132. }
  133. if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
  134. {
  135. /* Allocate a new option entry */
  136. ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
  137. if(!ent)
  138. {
  139. ERR("config parse error: error reallocating config entries\n");
  140. continue;
  141. }
  142. cfgBlock.entries = ent;
  143. ent = cfgBlock.entries + cfgBlock.entryCount;
  144. cfgBlock.entryCount++;
  145. ent->key = strdup(key);
  146. ent->value = NULL;
  147. }
  148. free(ent->value);
  149. ent->value = strdup(value);
  150. TRACE("found '%s' = '%s'\n", ent->key, ent->value);
  151. }
  152. }
  153. void ReadALConfig(void)
  154. {
  155. const char *str;
  156. FILE *f;
  157. #ifdef _WIN32
  158. if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  159. {
  160. size_t p = strlen(buffer);
  161. snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
  162. TRACE("Loading config %s...\n", buffer);
  163. f = fopen(buffer, "rt");
  164. if(f)
  165. {
  166. LoadConfigFromFile(f);
  167. fclose(f);
  168. }
  169. }
  170. #else
  171. str = "/etc/openal/alsoft.conf";
  172. TRACE("Loading config %s...\n", str);
  173. f = fopen(str, "r");
  174. if(f)
  175. {
  176. LoadConfigFromFile(f);
  177. fclose(f);
  178. }
  179. if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
  180. str = "/etc/xdg";
  181. strncpy(buffer, str, sizeof(buffer)-1);
  182. buffer[sizeof(buffer)-1] = 0;
  183. /* Go through the list in reverse, since "the order of base directories
  184. * denotes their importance; the first directory listed is the most
  185. * important". Ergo, we need to load the settings from the later dirs
  186. * first so that the settings in the earlier dirs override them.
  187. */
  188. while(1)
  189. {
  190. char *next = strrchr(buffer, ':');
  191. if(next) *(next++) = 0;
  192. else next = buffer;
  193. if(next[0] != '/')
  194. WARN("Ignoring XDG config dir: %s\n", next);
  195. else
  196. {
  197. size_t len = strlen(next);
  198. strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
  199. buffer[sizeof(buffer)-1] = 0;
  200. TRACE("Loading config %s...\n", next);
  201. f = fopen(next, "r");
  202. if(f)
  203. {
  204. LoadConfigFromFile(f);
  205. fclose(f);
  206. }
  207. }
  208. if(next == buffer)
  209. break;
  210. }
  211. if((str=getenv("HOME")) != NULL && *str)
  212. {
  213. snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
  214. TRACE("Loading config %s...\n", buffer);
  215. f = fopen(buffer, "r");
  216. if(f)
  217. {
  218. LoadConfigFromFile(f);
  219. fclose(f);
  220. }
  221. }
  222. if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
  223. snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
  224. else
  225. {
  226. buffer[0] = 0;
  227. if((str=getenv("HOME")) != NULL && str[0] != 0)
  228. snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
  229. }
  230. if(buffer[0] != 0)
  231. {
  232. TRACE("Loading config %s...\n", buffer);
  233. f = fopen(buffer, "r");
  234. if(f)
  235. {
  236. LoadConfigFromFile(f);
  237. fclose(f);
  238. }
  239. }
  240. #endif
  241. if((str=getenv("ALSOFT_CONF")) != NULL && *str)
  242. {
  243. TRACE("Loading config %s...\n", str);
  244. f = fopen(str, "r");
  245. if(f)
  246. {
  247. LoadConfigFromFile(f);
  248. fclose(f);
  249. }
  250. }
  251. }
  252. void FreeALConfig(void)
  253. {
  254. unsigned int i;
  255. for(i = 0;i < cfgBlock.entryCount;i++)
  256. {
  257. free(cfgBlock.entries[i].key);
  258. free(cfgBlock.entries[i].value);
  259. }
  260. free(cfgBlock.entries);
  261. }
  262. const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
  263. {
  264. unsigned int i;
  265. char key[256];
  266. if(!keyName)
  267. return def;
  268. if(blockName && strcasecmp(blockName, "general") != 0)
  269. snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
  270. else
  271. {
  272. strncpy(key, keyName, sizeof(key)-1);
  273. key[sizeof(key)-1] = 0;
  274. }
  275. for(i = 0;i < cfgBlock.entryCount;i++)
  276. {
  277. if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
  278. {
  279. TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
  280. if(cfgBlock.entries[i].value[0])
  281. return cfgBlock.entries[i].value;
  282. return def;
  283. }
  284. }
  285. TRACE("Key %s not found\n", key);
  286. return def;
  287. }
  288. int ConfigValueExists(const char *blockName, const char *keyName)
  289. {
  290. const char *val = GetConfigValue(blockName, keyName, "");
  291. return !!val[0];
  292. }
  293. int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
  294. {
  295. const char *val = GetConfigValue(blockName, keyName, "");
  296. if(!val[0]) return 0;
  297. *ret = val;
  298. return 1;
  299. }
  300. int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
  301. {
  302. const char *val = GetConfigValue(blockName, keyName, "");
  303. if(!val[0]) return 0;
  304. *ret = strtol(val, NULL, 0);
  305. return 1;
  306. }
  307. int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
  308. {
  309. const char *val = GetConfigValue(blockName, keyName, "");
  310. if(!val[0]) return 0;
  311. *ret = strtoul(val, NULL, 0);
  312. return 1;
  313. }
  314. int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
  315. {
  316. const char *val = GetConfigValue(blockName, keyName, "");
  317. if(!val[0]) return 0;
  318. #ifdef HAVE_STRTOF
  319. *ret = strtof(val, NULL);
  320. #else
  321. *ret = (float)strtod(val, NULL);
  322. #endif
  323. return 1;
  324. }
  325. int GetConfigValueBool(const char *blockName, const char *keyName, int def)
  326. {
  327. const char *val = GetConfigValue(blockName, keyName, "");
  328. if(!val[0]) return !!def;
  329. return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  330. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  331. }