Dini.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Dini 1.6
  3. * (c) Copyright 2006-2008 by DracoBlue
  4. *
  5. * @author : DracoBlue (http://dracoblue.com)
  6. * @date : 13th May 2006
  7. * @update : 16th Sep 2008
  8. *
  9. * This file is provided as is (no warranties).
  10. *
  11. * It's released under the terms of MIT.
  12. *
  13. * Feel free to use it, a little message in
  14. * about box is honouring thing, isn't it?
  15. *
  16. */
  17. #if defined _dini_included
  18. #endinput
  19. #endif
  20. #define _dini_included
  21. #pragma library dini
  22. #if defined MAX_STRING
  23. #define DINI_MAX_STRING MAX_STRING
  24. #else
  25. #define DINI_MAX_STRING 255
  26. #endif
  27. /*
  28. native dini_Exists(filename[])
  29. native dini_Remove(filename[])
  30. native dini_Create(filename[])
  31. native dini_Set(filename[],key[],value[])
  32. native dini_IntSet(filename[],key[],value)
  33. native dini_Int(filename[],key[])
  34. native dini_FloatSet(filename[],key[],Float:value)
  35. native Float:dini_Float(filename[],key[])
  36. native dini_Bool(filename[],key[])
  37. native dini_BoolSet(filename[],key[],value)
  38. native dini_Unset(filename[],key[])
  39. native dini_Get(filename[],key[])
  40. native dini_Isset(filename[],key[])
  41. native DINI_StripNewLine(string[])
  42. native DINI_fcopytextfile(oldname[],newname[])
  43. */
  44. stock dini_Exists(filename[]) {
  45. return fexist(filename);
  46. }
  47. stock dini_Remove(filename[]) {
  48. return fremove(filename);
  49. }
  50. stock dini_Create(filename[]) {
  51. if (fexist(filename)) return false;
  52. new File:fhnd;
  53. fhnd=fopen(filename,io_write);
  54. if (fhnd) {
  55. fclose(fhnd);
  56. return true;
  57. }
  58. return false;
  59. }
  60. stock dini_Set(filename[],key[],value[]) {
  61. // If we have no key, it can't be set
  62. // we also have no chance to set the value, if all together is bigger then the max string
  63. new key_length = strlen(key);
  64. new value_length = strlen(value);
  65. if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
  66. new File:fohnd, File:fwhnd;
  67. new tmpres[DINI_MAX_STRING];
  68. new bool:wasset=false;
  69. // Let's remove the old *.part file if there was one.
  70. format(tmpres,sizeof(tmpres),"%s.part",filename);
  71. fremove(tmpres);
  72. // We'll open the source file.
  73. fohnd=fopen(filename,io_read);
  74. if (!fohnd) return false;
  75. fwhnd=fopen(tmpres,io_write);
  76. if (!fwhnd) {
  77. // we can't open the second file for writing, so .. let's close the open one and exit.
  78. fclose(fohnd);
  79. return false;
  80. }
  81. while (fread(fohnd,tmpres)) {
  82. if (
  83. !wasset
  84. && tmpres[key_length]=='='
  85. && !strcmp(tmpres, key, true, key_length)
  86. ) {
  87. // We've got what needs to be replaced!
  88. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  89. wasset=true;
  90. } else {
  91. DINI_StripNewLine(tmpres);
  92. }
  93. fwrite(fwhnd,tmpres);
  94. fwrite(fwhnd,"\r\n");
  95. }
  96. if (!wasset) {
  97. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  98. fwrite(fwhnd,tmpres);
  99. fwrite(fwhnd,"\r\n");
  100. }
  101. fclose(fohnd);
  102. fclose(fwhnd);
  103. format(tmpres,sizeof(tmpres),"%s.part",filename);
  104. if (DINI_fcopytextfile(tmpres,filename)) {
  105. return fremove(tmpres);
  106. }
  107. return false;
  108. }
  109. stock dini_IntSet(filename[],key[],value) {
  110. new valuestring[DINI_MAX_STRING];
  111. format(valuestring,DINI_MAX_STRING,"%d",value);
  112. return dini_Set(filename,key,valuestring);
  113. }
  114. stock dini_Int(filename[],key[]) {
  115. return strval(dini_Get(filename,key));
  116. }
  117. stock dini_FloatSet(filename[],key[],Float:value) {
  118. new valuestring[DINI_MAX_STRING];
  119. format(valuestring,DINI_MAX_STRING,"%f",value);
  120. return dini_Set(filename,key,valuestring);
  121. }
  122. stock Float:dini_Float(filename[],key[]) {
  123. return floatstr(dini_Get(filename,key));
  124. }
  125. stock dini_Bool(filename[],key[]) {
  126. return strval(dini_Get(filename,key));
  127. }
  128. stock dini_BoolSet(filename[],key[],value) {
  129. if (value) {
  130. return dini_Set(filename,key,"1");
  131. }
  132. return dini_Set(filename,key,"0");
  133. }
  134. stock dini_Unset(filename[],key[]) {
  135. // If we have no key, it can't be set
  136. // we also have no chance to unset the key, if all together is bigger then the max string
  137. new key_length = strlen(key);
  138. if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  139. new File:fohnd, File:fwhnd;
  140. new tmpres[DINI_MAX_STRING];
  141. // Let's remove the old *.part file if there was one.
  142. format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  143. fremove(tmpres);
  144. // We'll open the source file.
  145. fohnd=fopen(filename,io_read);
  146. if (!fohnd) return false;
  147. fwhnd=fopen(tmpres,io_write);
  148. if (!fwhnd) {
  149. // we can't open the second file for writing, so .. let's close the open one and exit.
  150. fclose(fohnd);
  151. return false;
  152. }
  153. while (fread(fohnd,tmpres)) {
  154. if (
  155. tmpres[key_length]=='='
  156. && !strcmp(tmpres, key, true, key_length)
  157. ) {
  158. // We've got what needs to be removed!
  159. } else {
  160. DINI_StripNewLine(tmpres);
  161. fwrite(fwhnd,tmpres);
  162. fwrite(fwhnd,"\r\n");
  163. }
  164. }
  165. fclose(fohnd);
  166. fclose(fwhnd);
  167. format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  168. if (DINI_fcopytextfile(tmpres,filename)) {
  169. return fremove(tmpres);
  170. }
  171. return false;
  172. }
  173. stock dini_Get(filename[],key[]) {
  174. new tmpres[DINI_MAX_STRING];
  175. new key_length = strlen(key);
  176. if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
  177. new File:fohnd;
  178. fohnd=fopen(filename,io_read);
  179. if (!fohnd) return tmpres;
  180. while (fread(fohnd,tmpres)) {
  181. if (
  182. tmpres[key_length]=='='
  183. && !strcmp(tmpres, key, true, key_length)
  184. ) {
  185. /* We've got what we need */
  186. DINI_StripNewLine(tmpres);
  187. strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
  188. fclose(fohnd);
  189. return tmpres;
  190. }
  191. }
  192. fclose(fohnd);
  193. return tmpres;
  194. }
  195. stock dini_Isset(filename[],key[]) {
  196. new key_length = strlen(key);
  197. if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  198. new File:fohnd;
  199. fohnd=fopen(filename,io_read);
  200. if (!fohnd) return false;
  201. new tmpres[DINI_MAX_STRING];
  202. while (fread(fohnd,tmpres)) {
  203. if (
  204. tmpres[key_length]=='='
  205. && !strcmp(tmpres, key, true, key_length)
  206. ) {
  207. // We've got what we need
  208. fclose(fohnd);
  209. return true;
  210. }
  211. }
  212. fclose(fohnd);
  213. return false;
  214. }
  215. stock DINI_StripNewLine(string[]) {
  216. new len = strlen(string);
  217. if (string[0]==0) return ;
  218. if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  219. string[len - 1] = 0;
  220. if (string[0]==0) return ;
  221. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  222. }
  223. }
  224. stock DINI_fcopytextfile(oldname[],newname[]) {
  225. new File:ohnd,File:nhnd;
  226. if (!fexist(oldname)) return false;
  227. ohnd=fopen(oldname,io_read);
  228. if (!ohnd) return false;
  229. nhnd=fopen(newname,io_write);
  230. if (!nhnd) {
  231. fclose(ohnd);
  232. return false;
  233. }
  234. new tmpres[DINI_MAX_STRING];
  235. while (fread(ohnd,tmpres)) {
  236. DINI_StripNewLine(tmpres);
  237. format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  238. fwrite(nhnd,tmpres);
  239. }
  240. fclose(ohnd);
  241. fclose(nhnd);
  242. return true;
  243. }