Advertisement
Guest User

Untitled

a guest
Oct 9th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  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.  
  18. #if defined _dini_included
  19. #endinput
  20. #endif
  21.  
  22. #define _dini_included
  23. #pragma library dini
  24.  
  25. #if defined MAX_STRING
  26. #define DINI_MAX_STRING MAX_STRING
  27. #else
  28. #define DINI_MAX_STRING 255
  29. #endif
  30. stock dini_Exists(filename[]) {
  31. return fexist(filename);
  32. }
  33.  
  34. stock dini_Remove(filename[]) {
  35. return fremove(filename);
  36. }
  37.  
  38. stock dini_Create(filename[]) {
  39. if (fexist(filename)) return false;
  40. new File:fhnd;
  41. fhnd=fopen(filename,io_write);
  42. if (fhnd) {
  43. fclose(fhnd);
  44. return true;
  45. }
  46. return false;
  47. }
  48.  
  49. stock dini_Set(filename[],key[],value[]) {
  50. // If we have no key, it can't be set
  51. // we also have no chance to set the value, if all together is bigger then the max string
  52. new key_length = strlen(key);
  53. new value_length = strlen(value);
  54. if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
  55.  
  56. new File:fohnd, File:fwhnd;
  57. new tmpres[DINI_MAX_STRING];
  58. new bool:wasset=false;
  59.  
  60. // Let's remove the old *.part file if there was one.
  61. format(tmpres,sizeof(tmpres),"%s.part",filename);
  62. fremove(tmpres);
  63.  
  64. // We'll open the source file.
  65. fohnd=fopen(filename,io_read);
  66. if (!fohnd) return false;
  67.  
  68. fwhnd=fopen(tmpres,io_write);
  69. if (!fwhnd) {
  70. // we can't open the second file for writing, so .. let's close the open one and exit.
  71. fclose(fohnd);
  72. return false;
  73. }
  74.  
  75. while (fread(fohnd,tmpres)) {
  76. if (
  77. !wasset
  78. && tmpres[key_length]=='='
  79. && !strcmp(tmpres, key, true, key_length)
  80. ) {
  81. // We've got what needs to be replaced!
  82. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  83. wasset=true;
  84. } else {
  85. DINI_StripNewLine(tmpres);
  86. }
  87. fwrite(fwhnd,tmpres);
  88. fwrite(fwhnd,"\r\n");
  89. }
  90.  
  91. if (!wasset) {
  92. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  93. fwrite(fwhnd,tmpres);
  94. fwrite(fwhnd,"\r\n");
  95. }
  96.  
  97. fclose(fohnd);
  98. fclose(fwhnd);
  99.  
  100. format(tmpres,sizeof(tmpres),"%s.part",filename);
  101. if (DINI_fcopytextfile(tmpres,filename)) {
  102. return fremove(tmpres);
  103. }
  104. return false;
  105. }
  106.  
  107.  
  108. stock dini_IntSet(filename[],key[],value) {
  109. new valuestring[DINI_MAX_STRING];
  110. format(valuestring,DINI_MAX_STRING,"%d",value);
  111. return dini_Set(filename,key,valuestring);
  112. }
  113.  
  114. stock dini_Int(filename[],key[]) {
  115. return strval(dini_Get(filename,key));
  116. }
  117.  
  118. stock dini_FloatSet(filename[],key[],Float:value) {
  119. new valuestring[DINI_MAX_STRING];
  120. format(valuestring,DINI_MAX_STRING,"%f",value);
  121. return dini_Set(filename,key,valuestring);
  122. }
  123.  
  124. stock Float:dini_Float(filename[],key[]) {
  125. return floatstr(dini_Get(filename,key));
  126. }
  127.  
  128. stock dini_Bool(filename[],key[]) {
  129. return strval(dini_Get(filename,key));
  130. }
  131.  
  132. stock dini_BoolSet(filename[],key[],value) {
  133. if (value) {
  134. return dini_Set(filename,key,"1");
  135. }
  136. return dini_Set(filename,key,"0");
  137. }
  138.  
  139. stock dini_Unset(filename[],key[]) {
  140. // If we have no key, it can't be set
  141. // we also have no chance to unset the key, if all together is bigger then the max string
  142. new key_length = strlen(key);
  143. if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  144.  
  145. new File:fohnd, File:fwhnd;
  146. new tmpres[DINI_MAX_STRING];
  147.  
  148. // Let's remove the old *.part file if there was one.
  149. format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  150. fremove(tmpres);
  151.  
  152. // We'll open the source file.
  153. fohnd=fopen(filename,io_read);
  154. if (!fohnd) return false;
  155.  
  156. fwhnd=fopen(tmpres,io_write);
  157. if (!fwhnd) {
  158. // we can't open the second file for writing, so .. let's close the open one and exit.
  159. fclose(fohnd);
  160. return false;
  161. }
  162.  
  163. while (fread(fohnd,tmpres)) {
  164. if (
  165. tmpres[key_length]=='='
  166. && !strcmp(tmpres, key, true, key_length)
  167. ) {
  168. // We've got what needs to be removed!
  169. } else {
  170. DINI_StripNewLine(tmpres);
  171. fwrite(fwhnd,tmpres);
  172. fwrite(fwhnd,"\r\n");
  173. }
  174. }
  175.  
  176. fclose(fohnd);
  177. fclose(fwhnd);
  178.  
  179. format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  180. if (DINI_fcopytextfile(tmpres,filename)) {
  181. return fremove(tmpres);
  182. }
  183. return false;
  184. }
  185.  
  186. stock dini_Get(filename[],key[]) {
  187. new tmpres[DINI_MAX_STRING];
  188.  
  189. new key_length = strlen(key);
  190. if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
  191.  
  192. new File:fohnd;
  193. fohnd=fopen(filename,io_read);
  194. if (!fohnd) return tmpres;
  195.  
  196. while (fread(fohnd,tmpres)) {
  197. if (
  198. tmpres[key_length]=='='
  199. && !strcmp(tmpres, key, true, key_length)
  200. ) {
  201. /* We've got what we need */
  202. DINI_StripNewLine(tmpres);
  203. strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
  204. fclose(fohnd);
  205. return tmpres;
  206. }
  207. }
  208. fclose(fohnd);
  209. return tmpres;
  210. }
  211.  
  212.  
  213. stock dini_Isset(filename[],key[]) {
  214. new key_length = strlen(key);
  215. if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  216.  
  217. new File:fohnd;
  218. fohnd=fopen(filename,io_read);
  219. if (!fohnd) return false;
  220.  
  221. new tmpres[DINI_MAX_STRING];
  222. while (fread(fohnd,tmpres)) {
  223. if (
  224. tmpres[key_length]=='='
  225. && !strcmp(tmpres, key, true, key_length)
  226. ) {
  227. // We've got what we need
  228. fclose(fohnd);
  229. return true;
  230. }
  231. }
  232. fclose(fohnd);
  233. return false;
  234. }
  235.  
  236.  
  237.  
  238. stock DINI_StripNewLine(string[]) {
  239. new len = strlen(string);
  240. if (string[0]==0) return ;
  241. if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  242. string[len - 1] = 0;
  243. if (string[0]==0) return ;
  244. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  245. }
  246. }
  247.  
  248. stock DINI_fcopytextfile(oldname[],newname[]) {
  249. new File:ohnd,File:nhnd;
  250. if (!fexist(oldname)) return false;
  251. ohnd=fopen(oldname,io_read);
  252. if (!ohnd) return false;
  253. nhnd=fopen(newname,io_write);
  254. if (!nhnd) {
  255. fclose(ohnd);
  256. return false;
  257. }
  258. new tmpres[DINI_MAX_STRING];
  259. while (fread(ohnd,tmpres)) {
  260. DINI_StripNewLine(tmpres);
  261. format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  262. fwrite(nhnd,tmpres);
  263. }
  264. fclose(ohnd);
  265. fclose(nhnd);
  266. return true;
  267. }
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement