WeenSoares

[Include]SII

Feb 16th, 2012
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. //======================================================
  2. // Slick's INI Include 2.0.7
  3. //
  4. // (c) Copyright 2008-2010, [DRuG]Slick
  5. // This file is provided as is (no warranties).
  6. //======================================================
  7.  
  8.  
  9. #pragma dynamic 45000 // Change accordingly!!
  10.  
  11. #if defined MAX_STRING
  12. #define INI_MAX_VALUE MAX_STRING
  13. #else
  14. #define INI_MAX_VALUE 128
  15. #endif
  16. #define INI_MAX_KEY 24
  17. #define INI_MAX_LINE (INI_MAX_KEY + INI_MAX_VALUE + 3)
  18. #define INI_MAX_FILENAME 256
  19. #define INI_MAX_LINES 256
  20.  
  21.  
  22. enum E_CACHE
  23. {
  24. E_KEY[INI_MAX_KEY],
  25. E_VALUE[INI_MAX_VALUE]
  26. }
  27.  
  28. enum E_FILE
  29. {
  30. E_FILENAME[INI_MAX_FILENAME],
  31. bool: E_OPEN
  32. }
  33.  
  34. static gCache[INI_MAX_LINES][E_CACHE];
  35. static gEmptyCache[E_CACHE];
  36. static gFile[E_FILE];
  37. static gNoFile[E_FILE];
  38.  
  39.  
  40. #define INI_Exist fexist
  41.  
  42.  
  43. stock INI_Open(const filename[])
  44. {
  45. if (!filename[0]) return false;
  46. if (gFile[E_OPEN])
  47. {
  48. if (!strcmp(gFile[E_FILENAME], filename, true)) return true;
  49. //return false;
  50. INI_Close();
  51. }
  52. new File: h;
  53. h = fopen(filename, io_readwrite);
  54. if (h)
  55. {
  56. INI_strcpy(gFile[E_FILENAME], filename, INI_MAX_FILENAME, INI_MAX_FILENAME);
  57. new line[INI_MAX_LINE];
  58. new ln = -1;
  59. new separator;
  60. while (((ln + 1) < INI_MAX_LINES) && (fread(h, line)))
  61. {
  62. ln ++;
  63. INI_StripLine(line);
  64. separator = strfind(line, "=", false);
  65. if ((line[0] == ';') || (line[0] == '=') || (separator == -1) || (separator > INI_MAX_KEY))
  66. {
  67. if (line[0] == ';')
  68. {
  69. INI_strcpy(gCache[ln][E_VALUE], line, INI_MAX_VALUE, INI_MAX_VALUE);
  70. }
  71. else
  72. {
  73. INI_strcpy(gCache[ln][E_VALUE][1], line, INI_MAX_VALUE - 1, INI_MAX_VALUE - 1);
  74. gCache[ln][E_VALUE][0] = ';';
  75. }
  76. continue;
  77. }
  78. INI_strcpy(gCache[ln][E_KEY], line, separator, INI_MAX_KEY);
  79. INI_strcpy(gCache[ln][E_VALUE], line[separator + 1], INI_MAX_VALUE, INI_MAX_VALUE);
  80. if (!gCache[ln][E_VALUE][0]) gCache[ln][E_VALUE][0] = ' ';
  81. }
  82. fclose(h);
  83. gFile[E_OPEN] = true;
  84. return 1;
  85. }
  86. return 0;
  87. }
  88.  
  89.  
  90. stock INI_Save()
  91. {
  92. if (!gFile[E_OPEN]) return false;
  93. new File: h;
  94. h = fopen(gFile[E_FILENAME], io_write);
  95. if (h)
  96. {
  97. new line[INI_MAX_LINE];
  98. new ln = -1;
  99. while (((ln + 1) < INI_MAX_LINES) && (gCache[ln + 1][E_VALUE][0]))
  100. {
  101. ln ++;
  102. if (gCache[ln][E_VALUE][0] == ';')
  103. {
  104. if (gCache[ln][E_VALUE][1])
  105. {
  106. format(line, sizeof(line), "%s\r\n", gCache[ln][E_VALUE]);
  107. fwrite(h, line);
  108. continue;
  109. }
  110. fwrite(h, "\r\n");
  111. continue;
  112. }
  113. format(line, sizeof(line), "%s=%s\r\n", gCache[ln][E_KEY], gCache[ln][E_VALUE]);
  114. fwrite(h, line);
  115. }
  116. fclose(h);
  117. return true;
  118. }
  119. return false;
  120. }
  121.  
  122.  
  123. stock INI_Close()
  124. {
  125. if (!gFile[E_OPEN]) return false;
  126. for (new ln; ln < INI_MAX_LINES; ln++) gCache[ln] = gEmptyCache;
  127. gFile = gNoFile;
  128. return true;
  129. }
  130.  
  131.  
  132. stock INI_ReadString(dest[], const key[], maxlength = sizeof(dest))
  133. {
  134. if ((!gFile[E_OPEN]) || (!key[0])) return false;
  135. new ln = -1;
  136. while (((ln + 1) < INI_MAX_LINES) && (gCache[ln + 1][E_VALUE][0]))
  137. {
  138. ln ++;
  139. if (gCache[ln][E_VALUE][0] == ';') continue;
  140. if (!strcmp(gCache[ln][E_KEY], key, false))
  141. {
  142. INI_strcpy(dest, gCache[ln][E_VALUE], INI_MAX_VALUE, maxlength);
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148.  
  149.  
  150. stock INI_ReadInt(const key[])
  151. {
  152. new dest[12];
  153. if (INI_ReadString(dest, key)) return strval(dest);
  154. return false;
  155. }
  156.  
  157.  
  158. stock Float: INI_ReadFloat(const key[])
  159. {
  160. new dest[12];
  161. if (INI_ReadString(dest, key)) return floatstr(dest);
  162. return 0.0;
  163. }
  164.  
  165.  
  166. stock INI_WriteString(const key[], const value[])
  167. {
  168. if ((!gFile[E_OPEN]) || (!key[0]) || (key[0] == ';') || (strfind(key, "=", false) != -1)) return false;
  169. new ln = -1;
  170. while (((ln + 1) < INI_MAX_LINES) && (gCache[ln + 1][E_VALUE][0]))
  171. {
  172. ln ++;
  173. if (gCache[ln][E_VALUE][0] == ';') continue;
  174. if (!strcmp(gCache[ln][E_KEY], key, false))
  175. {
  176. INI_strcpy(gCache[ln][E_VALUE], value, INI_MAX_VALUE, INI_MAX_VALUE);
  177. return true;
  178. }
  179. }
  180. ln ++;
  181. if (ln < INI_MAX_LINES)
  182. {
  183. INI_strcpy(gCache[ln][E_KEY], key, INI_MAX_KEY, INI_MAX_KEY);
  184. INI_strcpy(gCache[ln][E_VALUE], value, INI_MAX_VALUE, INI_MAX_VALUE);
  185. return true;
  186. }
  187. return false;
  188. }
  189.  
  190.  
  191. stock INI_WriteInt(const key[], value)
  192. {
  193. new dest[12];
  194. format(dest, sizeof(dest), "%i", value);
  195. return INI_WriteString(key, dest);
  196. }
  197.  
  198.  
  199. stock INI_WriteFloat(const key[], Float: value)
  200. {
  201. new dest[12];
  202. format(dest, sizeof(dest), "%0.4f", value);
  203. return INI_WriteString(key, dest);
  204. }
  205.  
  206.  
  207. stock INI_RemoveEntry(const key[])
  208. {
  209. if ((!gFile[E_OPEN]) || (!key[0]) || (strfind(key, "=", false) != -1)) return false;
  210. new ln = -1;
  211. while (((ln + 1) < INI_MAX_LINES) && (gCache[ln + 1][E_VALUE][0]))
  212. {
  213. ln ++;
  214. if (gCache[ln][E_VALUE][0] == ';') continue;
  215. if (!strcmp(gCache[ln][E_KEY], key, false))
  216. {
  217. for (; ln < (INI_MAX_LINES - 1); ln ++)
  218. {
  219. INI_strcpy(gCache[ln][E_KEY], gCache[ln + 1][E_KEY], INI_MAX_KEY, INI_MAX_KEY);
  220. INI_strcpy(gCache[ln][E_VALUE], gCache[ln + 1][E_VALUE], INI_MAX_VALUE, INI_MAX_VALUE);
  221. }
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227.  
  228.  
  229. stock INI_Remove(const filename[])
  230. {
  231. if (!filename[0]) return false;
  232. if ((gFile[E_OPEN]) && (!strcmp(gFile[E_FILENAME], filename, true))) gFile = gNoFile;
  233. return fremove(filename);
  234. }
  235.  
  236.  
  237. stock INI_strcpy(dest[], const source[], numcells = sizeof(source), maxlength = sizeof(dest))
  238. {
  239. new i;
  240. while ((source[i]) && (i < numcells) && (i < maxlength))
  241. {
  242. dest[i] = source[i];
  243. i ++;
  244. }
  245. dest[(i == maxlength) ? (i - 1) : (i)] = '\0';
  246. }
  247.  
  248.  
  249. stock INI_StripLine(string[])
  250. {
  251. new l;
  252. l = strlen(string);
  253. if (string[l - 2] == '\r') string[l - 2] = '\0';
  254. if (string[l - 1] == '\n') string[l - 1] = '\0';
  255. }
Add Comment
Please, Sign In to add comment