1
0

dutils.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * DUtils functions 1.9
  3. * (c) Copyright 2006-2007 by DracoBlue
  4. *
  5. * @author : DracoBlue (http://dracoblue.com)
  6. * @date : 8th April 2006
  7. * @update : 21st June. 2007
  8. *
  9. * This file is provided as is (no warranties).
  10. *
  11. */
  12. #if defined _dutils_included
  13. #endinput
  14. #endif
  15. #define _dutils_included
  16. #pragma library dutils
  17. #define MAX_STRING 255
  18. new PRIVATE_Last_Money[MAX_PLAYERS];
  19. /*
  20. * First version released by mike, this one created by DracoBlue
  21. * Has also a fix to use "-" and "+" in the beginning of the number.
  22. */
  23. stock isNumeric(const string[]) {
  24. new length=strlen(string);
  25. if (length==0) return false;
  26. for (new i = 0; i < length; i++) {
  27. if (
  28. (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
  29. || (string[i]=='-' && i!=0) // A '-' but not at first.
  30. || (string[i]=='+' && i!=0) // A '+' but not at first.
  31. ) return false;
  32. }
  33. if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
  34. return true;
  35. }
  36. /*
  37. * Originally created by mabako, tuned by DracoBlue
  38. */
  39. stock mktime(hour,minute,second,day,month,year) {
  40. new timestamp2;
  41. timestamp2 = second + (minute * 60) + (hour * 3600);
  42. new days_of_month[12];
  43. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) {
  44. days_of_month = {31,29,31,30,31,30,31,31,30,31,30,31}; // Schaltjahr
  45. } else {
  46. days_of_month = {31,28,31,30,31,30,31,31,30,31,30,31}; // keins
  47. }
  48. new days_this_year = 0;
  49. days_this_year = day;
  50. if(month > 1) { // No January Calculation, because its always the 0 past months
  51. for(new i=0; i<month-1;i++) {
  52. days_this_year += days_of_month[i];
  53. }
  54. }
  55. timestamp2 += days_this_year * 86400;
  56. for(new j=1970;j<year;j++) {
  57. timestamp2 += 31536000;
  58. if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp2 += 86400; // Schaltjahr + 1 Tag
  59. }
  60. return timestamp2;
  61. }
  62. /**
  63. * Return if a Email is valid or not
  64. * @param value
  65. */
  66. stock ValidEmail(email[]) {
  67. new len=strlen(email);
  68. new cstate=0;
  69. new i;
  70. for(i=0;i<len;i++) {
  71. if ((cstate==0 || cstate==1) && (email[i]>='A' && email[i]<='Z') || (email[i]>='a' && email[i]<='z') || (email[i]=='.') || (email[i]=='-') || (email[i]=='_'))
  72. {
  73. } else {
  74. // Ok no A..Z,a..z,_,.,-
  75. if ((cstate==0) &&(email[i]=='@')) {
  76. // its an @ after the name, ok state=1;
  77. cstate=1;
  78. } else {
  79. // Its stuff which is not allowed
  80. return false;
  81. }
  82. }
  83. }
  84. if (cstate<1) return false;
  85. if (len<6) return false;
  86. // A toplevel domain has only 3 to 4 signs :-)
  87. if ((email[len-3]=='.') || (email[len-4]=='.') || (email[len-5]=='.')) return true;
  88. return false;
  89. }
  90. /**
  91. * Return a timestamp
  92. */
  93. stock Time() {
  94. new hour,minute,second;
  95. new year, month,day;
  96. gettime(hour, minute, second);
  97. getdate(year, month, day);
  98. return mktime(hour,minute,second,day,month,year);
  99. }
  100. /**
  101. * Return a timestamp
  102. */
  103. stock Now() {
  104. new hour,minute,second;
  105. new year, month,day;
  106. gettime(hour, minute, second);
  107. getdate(year, month, day);
  108. return mktime(hour,minute,second,day,month,year);
  109. }
  110. /**
  111. * Return the value of an hex-string
  112. * @param string
  113. */
  114. stock HexToInt(string[]) {
  115. if (string[0]==0) return 0;
  116. new i;
  117. new cur=1;
  118. new res=0;
  119. for (i=strlen(string);i>0;i--) {
  120. if (string[i-1]<58) res=res+cur*(string[i-1]-48); else res=res+cur*(string[i-1]-65+10);
  121. cur=cur*16;
  122. }
  123. return res;
  124. }
  125. /**
  126. * Return the int as string
  127. * @param number
  128. */
  129. stock IntToHex(number)
  130. {
  131. new m=1;
  132. new depth=0;
  133. while (number>=m) {
  134. m = m*16;
  135. depth++;
  136. }
  137. depth--;
  138. new str[MAX_STRING];
  139. for (new i = depth; i >= 0; i--)
  140. {
  141. str[i] = ( number & 0x0F) + 0x30; // + (tmp > 9 ? 0x07 : 0x00)
  142. str[i] += (str[i] > '9') ? 0x07 : 0x00;
  143. number >>= 4;
  144. }
  145. str[8] = '\0';
  146. return str;
  147. }
  148. /**
  149. * Return the string as int
  150. * @param string
  151. */
  152. stock StrToInt(string[]) {
  153. return strval(string);
  154. }
  155. /**
  156. * Return the value as string
  157. * @param value
  158. */
  159. stock IntToStr(value) {
  160. new tmp[MAX_STRING];
  161. valstr(tmp, value);
  162. return tmp;
  163. }
  164. /**
  165. * Return the truncated value
  166. * @param Float:value
  167. */
  168. stock trunc(Float:value) {
  169. return floatround(value,floatround_floor);
  170. }
  171. /**
  172. * Sets money for player
  173. * @param playerid
  174. * howmuch
  175. */
  176. stock SetPlayerMoney(playerid,howmuch) {
  177. PRIVATE_Last_Money[playerid]=howmuch;
  178. GivePlayerMoney(playerid,howmuch-GetPlayerMoney(playerid));
  179. }
  180. /**
  181. * Copies a file (Source file won't be deleted!)
  182. * @param oldname
  183. * newname
  184. * @requires WINDOWS
  185. */
  186. stock fcopy(oldname[],newname[]) {
  187. new File:ohnd,File:nhnd;
  188. if (!fexist(oldname)) return false;
  189. ohnd=fopen(oldname,io_read);
  190. nhnd=fopen(newname,io_write);
  191. new buf2[1];
  192. new i;
  193. for (i=flength(ohnd);i>0;i--) {
  194. fputchar(nhnd, fgetchar(ohnd, buf2[0],false),false);
  195. }
  196. fclose(ohnd);
  197. fclose(nhnd);
  198. return true;
  199. }
  200. /**
  201. * Copies a textfile (Source file won't be deleted!)
  202. * @param oldname
  203. * newname
  204. */
  205. stock fcopytextfile(oldname[],newname[]) {
  206. new File:ohnd,File:nhnd;
  207. if (!fexist(oldname)) return false;
  208. ohnd=fopen(oldname,io_read);
  209. nhnd=fopen(newname,io_write);
  210. new tmpres[MAX_STRING];
  211. while (fread(ohnd,tmpres)) {
  212. StripNewLine(tmpres);
  213. format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  214. fwrite(nhnd,tmpres);
  215. }
  216. fclose(ohnd);
  217. fclose(nhnd);
  218. return true;
  219. }
  220. /**
  221. * Renames a file (Source file will be deleted!)
  222. * @param oldname
  223. * newname
  224. * @requires WINDOWS (because fcopy does)
  225. */
  226. stock frename(oldname[],newname[]) {
  227. if (!fexist(oldname)) return false;
  228. fremove(newname);
  229. if (!fcopy(oldname,newname)) return false;
  230. fremove(oldname);
  231. return true;
  232. }
  233. /**
  234. * Strips Newline from the end of a string.
  235. * Idea: Y_Less, Bugfixing (when length=1) by DracoBlue
  236. * @param string
  237. */
  238. stock StripNewLine(string[])
  239. {
  240. new len = strlen(string);
  241. if (string[0]==0) return ;
  242. if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  243. string[len - 1] = 0;
  244. if (string[0]==0) return ;
  245. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  246. }
  247. }
  248. /**
  249. * Copies items from one array/string into return.
  250. * @param source
  251. * index (where to start, 0 is first)
  252. * numbytes (how much)
  253. */
  254. ret_memcpy(source[],index=0,numbytes) {
  255. new tmp[MAX_STRING];
  256. new i=0;
  257. tmp[0]=0;
  258. if (index>=strlen(source)) return tmp;
  259. if (numbytes+index>=strlen(source)) numbytes=strlen(source)-index;
  260. if (numbytes<=0) return tmp;
  261. for (i=index;i<numbytes+index;i++) {
  262. tmp[i-index]=source[i];
  263. if (source[i]==0) return tmp;
  264. }
  265. tmp[numbytes]=0;
  266. return tmp;
  267. }
  268. /**
  269. * Copies items from one array/string into another.
  270. * @param dest
  271. * source
  272. * count
  273. */
  274. stock copy(dest[],source[],count) {
  275. dest[0]=0;
  276. if (count<0) return false;
  277. if (count>strlen(source)) count=strlen(source);
  278. new i=0;
  279. for (i=0;i<count;i++) {
  280. dest[i]=source[i];
  281. if (source[i]==0) return true;
  282. }
  283. dest[count]=0;
  284. return true;
  285. }
  286. /**
  287. * Deletes the first 'count' items of a array/string
  288. * @param string[]
  289. * count
  290. */
  291. stock delete(string[],count) {
  292. new tmp[MAX_STRING];
  293. tmp[0]=0;
  294. if (count<=0) {
  295. format(tmp,sizeof(tmp),"%s",string);
  296. return tmp;
  297. }
  298. tmp=ret_memcpy(string,count,strlen(string));
  299. return tmp;
  300. }
  301. /**
  302. * Sets a string's value to source.
  303. * @param dest
  304. * source
  305. * count
  306. */
  307. stock set(dest[],source[]) {
  308. new count = strlen(source);
  309. new i=0;
  310. for (i=0;i<count;i++) {
  311. dest[i]=source[i];
  312. }
  313. dest[count]=0;
  314. }
  315. /**
  316. * Checks wether two strings are equal (case insensetive)
  317. * @param str1
  318. * str2
  319. */
  320. stock equal(str1[],str2[],bool:ignorecase) {
  321. if (strlen(str1)!=strlen(str2)) return false;
  322. if (strcmp(str1,str2,ignorecase)==0) return true;
  323. return false;
  324. }
  325. /**
  326. * Returns an element of a string splitted by ' ', default index is 0.
  327. * @param string
  328. * index
  329. */
  330. strtok(const string[], &index,seperator=' ')
  331. {
  332. new length = strlen(string);
  333. new offset = index;
  334. new result[MAX_STRING];
  335. while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
  336. {
  337. result[index - offset] = string[index];
  338. index++;
  339. }
  340. result[index - offset] = EOS;
  341. if ((index < length) && (string[index] == seperator))
  342. {
  343. index++;
  344. }
  345. return result;
  346. }
  347. stock mod(up,down) {
  348. return up-(floatround((up/down),floatround_floor))*down;
  349. }
  350. stock div(up,down) {
  351. return (floatround((up/down),floatround_floor));
  352. }
  353. /**
  354. * Returns a hashed value in adler32 as int
  355. * @param buf
  356. */
  357. stock num_hash(buf[])
  358. {
  359. new length=strlen(buf);
  360. new s1 = 1;
  361. new s2 = 0;
  362. new n;
  363. for (n=0; n<length; n++) {
  364. s1 = (s1 + buf[n]) % 65521;
  365. s2 = (s2 + s1) % 65521;
  366. }
  367. return (s2 << 16) + s1;
  368. }
  369. /**
  370. * Returns a hashed value in adler32 as string
  371. * @param buf
  372. */
  373. stock hash(str2[]) {
  374. new tmpdasdsa[MAX_STRING];
  375. tmpdasdsa[0]=0;
  376. valstr(tmpdasdsa,num_hash(str2));
  377. return tmpdasdsa;
  378. }
  379. /**
  380. * Returns a string which has 'newstr' where 'trg' was before
  381. * @param trg
  382. * newstr
  383. * src
  384. */
  385. stock strreplace(trg[],newstr[],src[]) {
  386. new f=0;
  387. new s1[MAX_STRING];
  388. new tmp[MAX_STRING];
  389. format(s1,sizeof(s1),"%s",src);
  390. f = strfind(s1,trg);
  391. tmp[0]=0;
  392. while (f>=0) {
  393. strcat(tmp,ret_memcpy(s1, 0, f));
  394. strcat(tmp,newstr);
  395. format(s1,sizeof(s1),"%s",ret_memcpy(s1, f+strlen(trg), strlen(s1)-f));
  396. f = strfind(s1,trg);
  397. }
  398. strcat(tmp,s1);
  399. return tmp;
  400. }
  401. /**
  402. * Returns the string with lowercase
  403. * @param txt
  404. */
  405. stock strlower(txt[]) {
  406. new tmp[MAX_STRING];
  407. tmp[0]=0;
  408. if (txt[0]==0) return tmp;
  409. new i=0;
  410. for (i=0;i<strlen(txt);i++) {
  411. tmp[i]=tolower(txt[i]);
  412. }
  413. tmp[strlen(txt)]=0;
  414. return tmp;
  415. }
  416. /**
  417. * Returns the string with uppercase
  418. * @param txt
  419. */
  420. stock strupper(txt[]) {
  421. new tmp[MAX_STRING];
  422. tmp[0]=0;
  423. if (txt[0]==0) return tmp;
  424. new i=0;
  425. for (i=0;i<strlen(txt);i++) {
  426. tmp[i]=toupper(txt[i]);
  427. }
  428. tmp[strlen(txt)]=0;
  429. return tmp;
  430. }