alcConfig.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. unsigned int entryCount;
  44. } ConfigBlock;
  45. static ConfigBlock *cfgBlocks;
  46. static unsigned int 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. int 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. unsigned int i;
  64. i = 1;
  65. while(buffer[i] && buffer[i] != ']')
  66. i++;
  67. if(!buffer[i])
  68. {
  69. ERR("config parse error: bad line \"%s\"\n", buffer);
  70. continue;
  71. }
  72. buffer[i] = 0;
  73. do {
  74. i++;
  75. if(buffer[i] && !isspace(buffer[i]))
  76. {
  77. if(buffer[i] != '#')
  78. WARN("config warning: extra data after block: \"%s\"\n", buffer+i);
  79. break;
  80. }
  81. } while(buffer[i]);
  82. nextBlock = NULL;
  83. for(i = 0;i < cfgCount;i++)
  84. {
  85. if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
  86. {
  87. nextBlock = cfgBlocks+i;
  88. TRACE("found block '%s'\n", nextBlock->name);
  89. break;
  90. }
  91. }
  92. if(!nextBlock)
  93. {
  94. nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
  95. if(!nextBlock)
  96. {
  97. ERR("config parse error: error reallocating config blocks\n");
  98. continue;
  99. }
  100. cfgBlocks = nextBlock;
  101. nextBlock = cfgBlocks+cfgCount;
  102. cfgCount++;
  103. nextBlock->name = strdup(buffer+1);
  104. nextBlock->entries = NULL;
  105. nextBlock->entryCount = 0;
  106. TRACE("found new block '%s'\n", nextBlock->name);
  107. }
  108. curBlock = nextBlock;
  109. continue;
  110. }
  111. /* Look for the option name */
  112. i = 0;
  113. while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
  114. !isspace(buffer[i]))
  115. i++;
  116. if(!buffer[i] || buffer[i] == '#' || i == 0)
  117. {
  118. ERR("config parse error: malformed option line: \"%s\"\n", buffer);
  119. continue;
  120. }
  121. /* Seperate the option */
  122. if(buffer[i] != '=')
  123. {
  124. buffer[i++] = 0;
  125. while(isspace(buffer[i]))
  126. i++;
  127. if(buffer[i] != '=')
  128. {
  129. ERR("config parse error: option without a value: \"%s\"\n", buffer);
  130. continue;
  131. }
  132. }
  133. /* Find the start of the value */
  134. buffer[i++] = 0;
  135. while(isspace(buffer[i]))
  136. i++;
  137. /* Check if we already have this option set */
  138. ent = curBlock->entries;
  139. while((unsigned int)(ent-curBlock->entries) < curBlock->entryCount)
  140. {
  141. if(strcasecmp(ent->key, buffer) == 0)
  142. break;
  143. ent++;
  144. }
  145. if((unsigned int)(ent-curBlock->entries) >= curBlock->entryCount)
  146. {
  147. /* Allocate a new option entry */
  148. ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
  149. if(!ent)
  150. {
  151. ERR("config parse error: error reallocating config entries\n");
  152. continue;
  153. }
  154. curBlock->entries = ent;
  155. ent = curBlock->entries + curBlock->entryCount;
  156. curBlock->entryCount++;
  157. ent->key = strdup(buffer);
  158. ent->value = NULL;
  159. }
  160. /* Look for the end of the line (Null term, new-line, or #-symbol) and
  161. eat up the trailing whitespace */
  162. memmove(buffer, buffer+i, strlen(buffer+i)+1);
  163. i = 0;
  164. while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
  165. i++;
  166. do {
  167. i--;
  168. } while(i >= 0 && isspace(buffer[i]));
  169. buffer[++i] = 0;
  170. free(ent->value);
  171. ent->value = strdup(buffer);
  172. TRACE("found '%s' = '%s'\n", ent->key, ent->value);
  173. }
  174. }
  175. void ReadALConfig(void)
  176. {
  177. const char *str;
  178. FILE *f;
  179. cfgBlocks = calloc(1, sizeof(ConfigBlock));
  180. cfgBlocks->name = strdup("general");
  181. cfgCount = 1;
  182. #ifdef _WIN32
  183. if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
  184. {
  185. size_t p = strlen(buffer);
  186. snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
  187. f = fopen(buffer, "rt");
  188. if(f)
  189. {
  190. LoadConfigFromFile(f);
  191. fclose(f);
  192. }
  193. }
  194. #else
  195. f = fopen("/etc/openal/alsoft.conf", "r");
  196. if(f)
  197. {
  198. LoadConfigFromFile(f);
  199. fclose(f);
  200. }
  201. if((str=getenv("HOME")) != NULL && *str)
  202. {
  203. snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
  204. f = fopen(buffer, "r");
  205. if(f)
  206. {
  207. LoadConfigFromFile(f);
  208. fclose(f);
  209. }
  210. }
  211. #endif
  212. if((str=getenv("ALSOFT_CONF")) != NULL && *str)
  213. {
  214. f = fopen(str, "r");
  215. if(f)
  216. {
  217. LoadConfigFromFile(f);
  218. fclose(f);
  219. }
  220. }
  221. }
  222. void FreeALConfig(void)
  223. {
  224. unsigned int i;
  225. for(i = 0;i < cfgCount;i++)
  226. {
  227. unsigned int j;
  228. for(j = 0;j < cfgBlocks[i].entryCount;j++)
  229. {
  230. free(cfgBlocks[i].entries[j].key);
  231. free(cfgBlocks[i].entries[j].value);
  232. }
  233. free(cfgBlocks[i].entries);
  234. free(cfgBlocks[i].name);
  235. }
  236. free(cfgBlocks);
  237. cfgBlocks = NULL;
  238. cfgCount = 0;
  239. }
  240. const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
  241. {
  242. unsigned int i, j;
  243. if(!keyName)
  244. return def;
  245. if(!blockName)
  246. blockName = "general";
  247. for(i = 0;i < cfgCount;i++)
  248. {
  249. if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
  250. continue;
  251. for(j = 0;j < cfgBlocks[i].entryCount;j++)
  252. {
  253. if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
  254. {
  255. TRACE("Found %s:%s = \"%s\"\n", blockName, keyName,
  256. cfgBlocks[i].entries[j].value);
  257. if(cfgBlocks[i].entries[j].value[0])
  258. return cfgBlocks[i].entries[j].value;
  259. return def;
  260. }
  261. }
  262. }
  263. TRACE("Key %s:%s not found\n", blockName, keyName);
  264. return def;
  265. }
  266. int ConfigValueExists(const char *blockName, const char *keyName)
  267. {
  268. const char *val = GetConfigValue(blockName, keyName, "");
  269. return !!val[0];
  270. }
  271. int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
  272. {
  273. const char *val = GetConfigValue(blockName, keyName, "");
  274. if(!val[0]) return 0;
  275. *ret = val;
  276. return 1;
  277. }
  278. int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
  279. {
  280. const char *val = GetConfigValue(blockName, keyName, "");
  281. if(!val[0]) return 0;
  282. *ret = strtol(val, NULL, 0);
  283. return 1;
  284. }
  285. int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
  286. {
  287. const char *val = GetConfigValue(blockName, keyName, "");
  288. if(!val[0]) return 0;
  289. *ret = strtoul(val, NULL, 0);
  290. return 1;
  291. }
  292. int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
  293. {
  294. const char *val = GetConfigValue(blockName, keyName, "");
  295. if(!val[0]) return 0;
  296. #ifdef HAVE_STRTOF
  297. *ret = strtof(val, NULL);
  298. #else
  299. *ret = (float)strtod(val, NULL);
  300. #endif
  301. return 1;
  302. }
  303. int GetConfigValueBool(const char *blockName, const char *keyName, int def)
  304. {
  305. const char *val = GetConfigValue(blockName, keyName, "");
  306. if(!val[0]) return !!def;
  307. return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
  308. strcasecmp(val, "on") == 0 || atoi(val) != 0);
  309. }