ffecfg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "ffecfg.h"
  5. #ifdef WIN32
  6. #define strcasecmp stricmp
  7. #endif
  8. // Initialises file filename into cfg
  9. // Returns 1 on success, 0 on failure
  10. int CfgOpen(CfgStruct *cfg,char *filename)
  11. {
  12. FILE *fd=fopen(filename,"r");
  13. memset(cfg,0,sizeof(CfgStruct));
  14. if(fd)
  15. {
  16. char buf[512];
  17. char *t;
  18. char *p;
  19. while(fgets(buf,512,fd))
  20. {
  21. if(!(t=strpbrk(buf,"=["))) continue;
  22. while ((p = strrchr(buf,'\n')) != NULL) *p = 0; // Fixed by AlexFA
  23. while ((p = strrchr(buf,'\r')) != NULL) *p = 0;
  24. if(*t=='[')
  25. {
  26. if(!strtok(++t,"]")) continue;
  27. cfg->sections=realloc(cfg->sections,sizeof(CfgSection)*(++cfg->sectioncount));
  28. cfg->currentsection=&cfg->sections[cfg->sectioncount-1];
  29. cfg->currentsection->keycount=0;
  30. cfg->currentsection->name=strdup(t);
  31. cfg->currentsection->keys=NULL;
  32. }
  33. else if(*t=='=')
  34. {
  35. if(!cfg->currentsection) continue;
  36. *(t++)=0;
  37. t+=strspn(t," ");
  38. for(p=t+strlen(t)-1;*p==' ';--p);
  39. *(p+1)=0;
  40. cfg->currentsection->keys=realloc(cfg->currentsection->keys,sizeof(CfgKey)*(++cfg->currentsection->keycount));
  41. cfg->currentsection->keys[cfg->currentsection->keycount-1].data=strdup(t);
  42. t=buf+strspn(buf," ");
  43. if((p=strchr(t,' '))) *p=0;
  44. cfg->currentsection->keys[cfg->currentsection->keycount-1].name=strdup(t);
  45. }
  46. }
  47. fclose(fd);
  48. cfg->currentsection=NULL;
  49. cfg->filename=strdup(filename);
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. // Closes config file - saves if modified
  55. // Returns 1 on success, 0 on failure
  56. int CfgClose(CfgStruct *cfg)
  57. {
  58. int i,j;
  59. if(!cfg->filename) return 0;
  60. for(i=0;i<cfg->sectioncount;++i)
  61. {
  62. cfg->currentsection=&cfg->sections[i];
  63. for(j=0;j<cfg->currentsection->keycount;++j)
  64. {
  65. free(cfg->currentsection->keys[j].data);
  66. free(cfg->currentsection->keys[j].name);
  67. }
  68. free(cfg->currentsection->name);
  69. free(cfg->currentsection->keys);
  70. }
  71. free(cfg->filename);
  72. free(cfg->sections);
  73. memset(cfg,0,sizeof(CfgStruct));
  74. return 1;
  75. }
  76. // Sets offsets in cfg to position of [sectname]
  77. // Returns 1 if found, 0 on failure
  78. int CfgFindSection(CfgStruct *cfg,char *sectname)
  79. {
  80. int i;
  81. if(!cfg->filename) return 0;
  82. for(i=0;i<cfg->sectioncount;++i)
  83. {
  84. if(!strcasecmp(sectname,cfg->sections[i].name))
  85. {
  86. cfg->currentsection=&cfg->sections[i];
  87. return 1;
  88. }
  89. }
  90. return 0;
  91. }
  92. // Reads the value of key keyname as an integer
  93. // Returns 1 on success, 0 on failure
  94. int CfgGetKeyVal(CfgStruct *cfg,char *keyname,int *value)
  95. {
  96. int i;
  97. if(!cfg->filename) return 0;
  98. if(!cfg->currentsection) return 0;
  99. for(i=0;i<cfg->currentsection->keycount;++i)
  100. {
  101. if(!strcasecmp(keyname,cfg->currentsection->keys[i].name))
  102. {
  103. *value=strtol(cfg->currentsection->keys[i].data,NULL,0);
  104. return 1;
  105. }
  106. }
  107. return 0;
  108. }
  109. int CfgGetKeyStr(CfgStruct *cfg,char *keyname,char *value,int buflen)
  110. {
  111. int i;
  112. if(!cfg->filename) return 0;
  113. if(!cfg->currentsection) return 0;
  114. for(i=0;i<cfg->currentsection->keycount;++i)
  115. {
  116. if(!strcasecmp(keyname,cfg->currentsection->keys[i].name))
  117. {
  118. strncpy (value,cfg->currentsection->keys[i].data,buflen);
  119. return 1;
  120. }
  121. }
  122. return 0;
  123. }
  124. int CfgGetKeyValDef(CfgStruct *cfg,char *keyname,int *value,int def)
  125. {
  126. int i;
  127. if(!cfg->filename)
  128. {
  129. *value=def;
  130. return 0;
  131. }
  132. if(!cfg->currentsection)
  133. {
  134. *value=def;
  135. return 0;
  136. }
  137. for(i=0;i<cfg->currentsection->keycount;++i)
  138. {
  139. if(!strcasecmp(keyname,cfg->currentsection->keys[i].name))
  140. {
  141. *value=strtol(cfg->currentsection->keys[i].data,NULL,0);
  142. return 1;
  143. }
  144. }
  145. *value=def;
  146. return 0;
  147. }