dfiles.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /********************************************
  2. * DUtils functions 1.6, Dini 1.4 *
  3. * (c) Copyright 2006 by DracoBlue *
  4. * Xtreme Administration Filterscript *
  5. * Programmers: Xtreme *
  6. * Include Programmer: DracoBlue *
  7. ********************************************/
  8. #if defined _dutils_included
  9. #endinput
  10. #endif
  11. #define _dutils_included
  12. #pragma library dutils
  13. #define MAX_STRING 255
  14. #if !defined floatstr
  15. native Float:floatstr(const string[]);
  16. #endif
  17. new PRIVATE_Last_Money[MAX_PLAYERS];
  18. /**
  19. * Return the truncated value
  20. * @param Float:value
  21. */
  22. trunc(Float:value) {
  23. return floatround(value,floatround_floor);
  24. }
  25. #pragma unused trunc
  26. /**
  27. * Sets money for player
  28. * @param playerid
  29. * howmuch
  30. */
  31. SetPlayerMoney(playerid,howmuch) {
  32. PRIVATE_Last_Money[playerid]=howmuch;
  33. GivePlayerMoney(playerid,howmuch-GetPlayerMoney(playerid));
  34. }
  35. #pragma unused SetPlayerMoney
  36. /**
  37. * Copies a file (Source file won't be deleted!)
  38. * @param oldname
  39. * newname
  40. * @requires WINDOWS
  41. */
  42. fcopy(oldname[],newname[]) {
  43. new File:ohnd,File:nhnd;
  44. if (!fexist(oldname)) return false;
  45. ohnd=fopen(oldname,io_read);
  46. nhnd=fopen(newname,io_write);
  47. new buf2[1];
  48. new i;
  49. for (i=flength(ohnd);i>0;i--) {
  50. fputchar(nhnd, fgetchar(ohnd, buf2[0],false),false);
  51. }
  52. fclose(ohnd);
  53. fclose(nhnd);
  54. return true;
  55. }
  56. #pragma unused fcopy
  57. /**
  58. * Copies a textfile (Source file won't be deleted!)
  59. * @param oldname
  60. * newname
  61. */
  62. fcopytextfile(oldname[],newname[]) {
  63. new File:ohnd,File:nhnd;
  64. if (!fexist(oldname)) return false;
  65. ohnd=fopen(oldname,io_read);
  66. nhnd=fopen(newname,io_write);
  67. new tmpres[MAX_STRING];
  68. while (fread(ohnd,tmpres)) {
  69. StripNewLine(tmpres);
  70. format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  71. fwrite(nhnd,tmpres);
  72. }
  73. fclose(ohnd);
  74. fclose(nhnd);
  75. return true;
  76. }
  77. #pragma unused fcopytextfile
  78. /**
  79. * Renames a file (Source file will be deleted!)
  80. * @param oldname
  81. * newname
  82. * @requires WINDOWS (because fcopy does)
  83. */
  84. frename(oldname[],newname[]) {
  85. if (!fexist(oldname)) return false;
  86. fremove(newname);
  87. if (!fcopy(oldname,newname)) return false;
  88. fremove(oldname);
  89. return true;
  90. }
  91. #pragma unused frename
  92. /**
  93. * Strips Newline from the end of a string.
  94. * Idea: Y_Less, Bugfixing (when length=1) by DracoBlue
  95. * @param string
  96. */
  97. stock StripNewLine(string[])
  98. {
  99. new len = strlen(string);
  100. if (string[0]==0) return ;
  101. if ((string[len - 1] == '\n') || (string[len - 1] == '\r'))
  102. {
  103. string[len - 1] = 0;
  104. if (string[0]==0) return ;
  105. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  106. }
  107. }
  108. #pragma unused StripNewLine
  109. /**
  110. * Copies items from one array/string into return.
  111. * @param source
  112. * index (where to start, 0 is first)
  113. * numbytes (how much)
  114. */
  115. ret_memcpy(source[],index=0,numbytes) {
  116. new tmp[MAX_STRING];
  117. new i=0;
  118. tmp[0]=0;
  119. if (index>=strlen(source)) return tmp;
  120. if (numbytes+index>=strlen(source)) numbytes=strlen(source)-index;
  121. if (numbytes<=0) return tmp;
  122. for (i=index;i<numbytes+index;i++) {
  123. tmp[i-index]=source[i];
  124. if (source[i]==0) return tmp;
  125. }
  126. tmp[numbytes]=0;
  127. return tmp;
  128. }
  129. #pragma unused ret_memcpy
  130. /**
  131. * Copies items from one array/string into another.
  132. * @param dest
  133. * source
  134. * count
  135. */
  136. stock copy(dest[],source[],count) {
  137. dest[0]=0;
  138. if (count<0) return false;
  139. if (count>strlen(source)) count=strlen(source);
  140. new i=0;
  141. for (i=0;i<count;i++) {
  142. dest[i]=source[i];
  143. if (source[i]==0) return true;
  144. }
  145. dest[count]=0;
  146. return true;
  147. }
  148. #pragma unused copy
  149. /**
  150. * Deletes the first 'count' items of a array/string
  151. * @param string[]
  152. * count
  153. */
  154. stock delete(string[],count) {
  155. new tmp[MAX_STRING];
  156. tmp[0]=0;
  157. if (count<=0) {
  158. format(tmp,sizeof(tmp),"%s",string);
  159. return tmp;
  160. }
  161. tmp=ret_memcpy(string,count,strlen(string));
  162. return tmp;
  163. }
  164. #pragma unused delete
  165. /**
  166. * Sets a string's value to source.
  167. * @param dest
  168. * source
  169. * count
  170. */
  171. stock set(dest[],source[]) {
  172. new count = strlen(source);
  173. new i=0;
  174. for (i=0;i<count;i++) {
  175. dest[i]=source[i];
  176. }
  177. dest[count]=0;
  178. }
  179. #pragma unused set
  180. /**
  181. * Checks wether two strings are equal (case insensetive)
  182. * @param str1
  183. * str2
  184. */
  185. stock equal(str1[],str2[],bool:ignorecase) {
  186. if (strlen(str1)!=strlen(str2)) return false;
  187. if (strcmp(str1,str2,ignorecase)==0) return true;
  188. return false;
  189. }
  190. #pragma unused equal
  191. /**
  192. * Returns an element of a string splitted by ' ', default index is 0.
  193. * @param string
  194. * index
  195. */
  196. stock mod(up,down) {
  197. return up-(floatround((up/down),floatround_floor))*down;
  198. }
  199. #pragma unused mod
  200. stock div(up,down) {
  201. return (floatround((up/down),floatround_floor));
  202. }
  203. #pragma unused div
  204. /**
  205. * Returns a hashed value in adler32 as int
  206. * @param buf
  207. */
  208. stock num_hash(buf[])
  209. {
  210. new length=strlen(buf);
  211. new s1 = 1;
  212. new s2 = 0;
  213. new n;
  214. for (n=0; n<length; n++)
  215. {
  216. s1 = (s1 + buf[n]) % 65521;
  217. s2 = (s2 + s1) % 65521;
  218. }
  219. return (s2 << 16) + s1;
  220. }
  221. #pragma unused num_hash
  222. /**
  223. * Returns a hashed value in adler32 as string
  224. * @param buf
  225. */
  226. stock hash(str2[])
  227. {
  228. new tmpdasdsa[MAX_STRING];
  229. tmpdasdsa[0]=0;
  230. valstr(tmpdasdsa,num_hash(str2));
  231. return tmpdasdsa;
  232. }
  233. #pragma unused hash
  234. /**
  235. * Returns a string which has 'newstr' where 'trg' was before
  236. * @param trg
  237. * newstr
  238. * src
  239. */
  240. strreplace(trg[],newstr[],src[]) {
  241. new f=0;
  242. new s1[MAX_STRING];
  243. new tmp[MAX_STRING];
  244. format(s1,sizeof(s1),"%s",src);
  245. f = strfind(s1,trg);
  246. tmp[0]=0;
  247. while (f>=0)
  248. {
  249. strcat(tmp,ret_memcpy(s1, 0, f));
  250. strcat(tmp,newstr);
  251. format(s1,sizeof(s1),"%s",ret_memcpy(s1, f+strlen(trg), strlen(s1)-f));
  252. f = strfind(s1,trg);
  253. }
  254. strcat(tmp,s1);
  255. return tmp;
  256. }
  257. #pragma unused strreplace
  258. strlower(txt[]) {
  259. new tmp[MAX_STRING];
  260. tmp[0]=0;
  261. if (txt[0]==0) return tmp;
  262. new i=0;
  263. for (i=0;i<strlen(txt);i++) {
  264. tmp[i]=tolower(txt[i]);
  265. }
  266. tmp[strlen(txt)]=0;
  267. return tmp;
  268. }
  269. #pragma unused strlower
  270. strtok(const string[], &index)
  271. {
  272. new length = strlen(string);
  273. while ((index < length) && (string[index] <= ' '))
  274. {
  275. index++;
  276. }
  277. new offset = index;
  278. new result[20];
  279. while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  280. {
  281. result[index - offset] = string[index];
  282. index++;
  283. }
  284. result[index - offset] = EOS;
  285. return result;
  286. }
  287. stock dini_Exists(filename[]) {
  288. if (fexist(filename)) return true;
  289. return false;
  290. }
  291. stock dini_Remove(filename[]) {
  292. if (!fexist(filename)) return false;
  293. fremove(filename);
  294. return true;
  295. }
  296. stock dini_Create(filename[]) {
  297. new File:fhnd;
  298. if (fexist(filename)) return false;
  299. fhnd=fopen(filename,io_write);
  300. fclose(fhnd);
  301. return true;
  302. }
  303. stock dini_PRIVATE_ExtractKey(line[]) {
  304. new tmp[MAX_STRING];
  305. tmp[0]=0;
  306. if (strfind(line,"=",true)==-1) return tmp;
  307. set(tmp,strlower(ret_memcpy(line,0,strfind(line,"=",true))));
  308. return tmp;
  309. }
  310. stock dini_PRIVATE_ExtractValue(line[]) {
  311. new tmp[MAX_STRING];
  312. tmp[0]=0;
  313. if (strfind(line,"=",true)==-1) {
  314. return tmp;
  315. }
  316. set(tmp,ret_memcpy(line,strfind(line,"=",true)+1,strlen(line)));
  317. return tmp;
  318. }
  319. stock dini_Set(filename[],key[],value[]) {
  320. new File:fohnd, File:fwhnd;
  321. new bool:wasset=false;
  322. new tmpres[MAX_STRING];
  323. if (key[0]==0) return false; /* If we have no sign in key, it can't be set*/
  324. format(tmpres,sizeof(tmpres),"%s.part",filename);
  325. fohnd=fopen(filename,io_read);
  326. if (!fohnd) return false;
  327. fremove(tmpres);
  328. fwhnd=fopen(tmpres,io_write);
  329. // if (!fwhnd) return false;
  330. while (fread(fohnd,tmpres)) {
  331. StripNewLine(tmpres);
  332. if ((!wasset)&&(equal(dini_PRIVATE_ExtractKey(tmpres),key,true))) {
  333. /* We've got what needs to be replaced! */
  334. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  335. wasset=true;
  336. }
  337. fwrite(fwhnd,tmpres);
  338. fwrite(fwhnd,"\r\n");
  339. }
  340. if (!wasset) {
  341. format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  342. fwrite(fwhnd,tmpres);
  343. fwrite(fwhnd,"\r\n");
  344. }
  345. fclose(fohnd);
  346. fclose(fwhnd);
  347. format(tmpres,sizeof(tmpres),"%s.part",filename);
  348. if (fcopytextfile(tmpres,filename)) {
  349. return fremove(tmpres);
  350. } else return false;
  351. }
  352. stock dini_IntSet(filename[],key[],value) {
  353. new valuestring[MAX_STRING];
  354. format(valuestring,sizeof(valuestring),"%d",value);
  355. return dini_Set(filename,key,valuestring);
  356. }
  357. stock dini_Int(filename[],key[]) {
  358. return strval(dini_Get(filename,key));
  359. }
  360. stock dini_FloatSet(filename[],key[],Float:value) {
  361. new valuestring[MAX_STRING];
  362. format(valuestring,sizeof(valuestring),"%f",value);
  363. return dini_Set(filename,key,valuestring);
  364. }
  365. stock Float:dini_Float(filename[],key[]) {
  366. return floatstr(dini_Get(filename,key));
  367. }
  368. stock dini_Bool(filename[],key[]) {
  369. return strval(dini_Get(filename,key));
  370. }
  371. stock dini_BoolSet(filename[],key[],value) {
  372. new valuestring[MAX_STRING];
  373. format(valuestring,sizeof(valuestring),"%d",value);
  374. return dini_Set(filename,key,valuestring);
  375. }
  376. stock dini_Unset(filename[],key[]) {
  377. new File:fohnd, File:fwhnd;
  378. new tmpres[MAX_STRING];
  379. format(tmpres,sizeof(tmpres),"%s.part",filename);
  380. fohnd=fopen(filename,io_read);
  381. if (!fohnd) return false;
  382. fremove(tmpres);
  383. fwhnd=fopen(tmpres,io_write);
  384. // if (!fwhnd) return false;
  385. while (fread(fohnd,tmpres)) {
  386. StripNewLine(tmpres);
  387. if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  388. /* We've got what needs to be removed! */
  389. } else {
  390. format(tmpres,sizeof(tmpres),"%s",tmpres);
  391. fwrite(fwhnd,tmpres);
  392. fwrite(fwhnd,"\r\n");
  393. }
  394. }
  395. fclose(fohnd);
  396. fclose(fwhnd);
  397. format(tmpres,sizeof(tmpres),"%s.part",filename);
  398. if (fcopytextfile(tmpres,filename)) {
  399. return fremove(tmpres);
  400. }
  401. return false;
  402. }
  403. stock dini_Get(filename[],key[]) {
  404. new File:fohnd;
  405. new tmpres[MAX_STRING];
  406. new tmpres2[MAX_STRING];
  407. tmpres[0]=0;
  408. fohnd=fopen(filename,io_read);
  409. if (!fohnd) return tmpres;
  410. while (fread(fohnd,tmpres)) {
  411. StripNewLine(tmpres);
  412. if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  413. /* We've got what we need */
  414. tmpres2[0]=0;
  415. strcat(tmpres2,dini_PRIVATE_ExtractValue(tmpres));
  416. fclose(fohnd);
  417. return tmpres2;
  418. }
  419. }
  420. fclose(fohnd);
  421. return tmpres;
  422. }
  423. stock dini_Isset(filename[],key[]) {
  424. new File:fohnd;
  425. new tmpres[MAX_STRING];
  426. fohnd=fopen(filename,io_read);
  427. if (!fohnd) return false;
  428. while (fread(fohnd,tmpres)) {
  429. StripNewLine(tmpres);
  430. if (equal(dini_PRIVATE_ExtractKey(tmpres),key,true)) {
  431. /* We've got what we need */
  432. fclose(fohnd);
  433. return true;
  434. }
  435. }
  436. fclose(fohnd);
  437. return false;
  438. }
  439. stock udb_hash(buf[]) {
  440. new length=strlen(buf);
  441. new s1 = 1;
  442. new s2 = 0;
  443. new n;
  444. for (n=0; n<length; n++)
  445. {
  446. s1 = (s1 + buf[n]) % 65521;
  447. s2 = (s2 + s1) % 65521;
  448. }
  449. return (s2 << 16) + s1;
  450. }
  451. stock udb_encode(nickname[]) {
  452. new tmp[MAX_STRING];
  453. set(tmp,nickname);
  454. tmp=strreplace("_","_00",tmp);
  455. tmp=strreplace(";","_01",tmp);
  456. tmp=strreplace("!","_02",tmp);
  457. tmp=strreplace("/","_03",tmp);
  458. tmp=strreplace("\\","_04",tmp);
  459. tmp=strreplace("[","_05",tmp);
  460. tmp=strreplace("]","_06",tmp);
  461. tmp=strreplace("?","_07",tmp);
  462. tmp=strreplace(".","_08",tmp);
  463. tmp=strreplace("*","_09",tmp);
  464. tmp=strreplace("<","_10",tmp);
  465. tmp=strreplace(">","_11",tmp);
  466. tmp=strreplace("{","_12",tmp);
  467. tmp=strreplace("}","_13",tmp);
  468. tmp=strreplace(" ","_14",tmp);
  469. tmp=strreplace("\"","_15",tmp);
  470. tmp=strreplace(":","_16",tmp);
  471. tmp=strreplace("|","_17",tmp);
  472. tmp=strreplace("=","_18",tmp);
  473. return tmp;
  474. }