dutils.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * DUtils functions 1.10
  3. * (c) Copyright 2006-2007 by DracoBlue
  4. *
  5. * @author : DracoBlue (http://dracoblue.com)
  6. * @date : 8th April 2006
  7. * @update : 12th July 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. stock IntToHex(number)
  111. {
  112. new m=1;
  113. new depth=0;
  114. while (number>=m) {
  115. m = m*16;
  116. depth++;
  117. }
  118. depth--;
  119. new str[MAX_STRING];
  120. for (new i = depth; i >= 0; i--)
  121. {
  122. str[i] = ( number & 0x0F) + 0x30; // + (tmp > 9 ? 0x07 : 0x00)
  123. str[i] += (str[i] > '9') ? 0x07 : 0x00;
  124. number >>= 4;
  125. }
  126. str[8] = '\0';
  127. return str;
  128. }
  129. /**
  130. * Return the string as int
  131. * @param string
  132. */
  133. stock StrToInt(string[]) {
  134. return strval(string);
  135. }
  136. /**
  137. * Return the value as string
  138. * @param value
  139. */
  140. stock IntToStr(value) {
  141. new tmp[MAX_STRING];
  142. valstr(tmp, value);
  143. return tmp;
  144. }
  145. /**
  146. * Return the truncated value
  147. * @param Float:value
  148. */
  149. stock trunc(Float:value) {
  150. return floatround(value,floatround_floor);
  151. }
  152. /**
  153. * Sets money for player
  154. * @param playerid
  155. * howmuch
  156. */
  157. stock SetPlayerMoney(playerid,howmuch) {
  158. PRIVATE_Last_Money[playerid]=howmuch;
  159. GivePlayerMoney(playerid,howmuch-GetPlayerMoney(playerid));
  160. }
  161. /**
  162. * Copies a file (Source file won't be deleted!)
  163. * @param oldname
  164. * newname
  165. * @requires WINDOWS
  166. */
  167. stock fcopy(oldname[],newname[]) {
  168. new File:ohnd,File:nhnd;
  169. if (!fexist(oldname)) return false;
  170. ohnd=fopen(oldname,io_read);
  171. nhnd=fopen(newname,io_write);
  172. new buf2[1];
  173. new i;
  174. for (i=flength(ohnd);i>0;i--) {
  175. fputchar(nhnd, fgetchar(ohnd, buf2[0],false),false);
  176. }
  177. fclose(ohnd);
  178. fclose(nhnd);
  179. return true;
  180. }
  181. /**
  182. * Copies a textfile (Source file won't be deleted!)
  183. * @param oldname
  184. * newname
  185. */
  186. stock fcopytextfile(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 tmpres[MAX_STRING];
  192. while (fread(ohnd,tmpres)) {
  193. StripNewLine(tmpres);
  194. format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  195. fwrite(nhnd,tmpres);
  196. }
  197. fclose(ohnd);
  198. fclose(nhnd);
  199. return true;
  200. }
  201. /**
  202. * Renames a file (Source file will be deleted!)
  203. * @param oldname
  204. * newname
  205. * @requires WINDOWS (because fcopy does)
  206. */
  207. stock frename(oldname[],newname[]) {
  208. if (!fexist(oldname)) return false;
  209. fremove(newname);
  210. if (!fcopy(oldname,newname)) return false;
  211. fremove(oldname);
  212. return true;
  213. }
  214. /**
  215. * Renames a file (Source file will be deleted!)
  216. * @param oldname
  217. * newname
  218. */
  219. stock frenametextfile(oldname[],newname[]) {
  220. if (!fexist(oldname)) return false;
  221. fremove(newname);
  222. if (!fcopytextfile(oldname,newname)) return false;
  223. fremove(oldname);
  224. return true;
  225. }
  226. /**
  227. * Strips Newline from the end of a string.
  228. * Idea: Y_Less, Bugfixing (when length=1) by DracoBlue
  229. * @param string
  230. */
  231. stock StripNewLine(string[])
  232. {
  233. new len = strlen(string);
  234. if (string[0]==0) return ;
  235. if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  236. string[len - 1] = 0;
  237. if (string[0]==0) return ;
  238. if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  239. }
  240. }
  241. /**
  242. * Copies items from one array/string into return.
  243. * @param source
  244. * index (where to start, 0 is first)
  245. * numbytes (how much)
  246. */
  247. ret_memcpy(source[],index=0,numbytes) {
  248. new tmp[MAX_STRING];
  249. new i=0;
  250. tmp[0]=0;
  251. if (index>=strlen(source)) return tmp;
  252. if (numbytes+index>=strlen(source)) numbytes=strlen(source)-index;
  253. if (numbytes<=0) return tmp;
  254. for (i=index;i<numbytes+index;i++) {
  255. tmp[i-index]=source[i];
  256. if (source[i]==0) return tmp;
  257. }
  258. tmp[numbytes]=0;
  259. return tmp;
  260. }
  261. /**
  262. * Copies items from one array/string into another.
  263. * @param dest
  264. * source
  265. * count
  266. */
  267. stock copy(dest[],source[],count) {
  268. dest[0]=0;
  269. if (count<0) return false;
  270. if (count>strlen(source)) count=strlen(source);
  271. new i=0;
  272. for (i=0;i<count;i++) {
  273. dest[i]=source[i];
  274. if (source[i]==0) return true;
  275. }
  276. dest[count]=0;
  277. return true;
  278. }
  279. /**
  280. * Deletes the first 'count' items of a array/string
  281. * @param string[]
  282. * count
  283. */
  284. stock delete(string[],count) {
  285. new tmp[MAX_STRING];
  286. tmp[0]=0;
  287. if (count<=0) {
  288. format(tmp,sizeof(tmp),"%s",string);
  289. return tmp;
  290. }
  291. tmp=ret_memcpy(string,count,strlen(string));
  292. return tmp;
  293. }
  294. /**
  295. * Sets a string's value to source.
  296. * @param dest
  297. * source
  298. * count
  299. */
  300. stock set(dest[],source[]) {
  301. new count = strlen(source);
  302. new i=0;
  303. for (i=0;i<count;i++) {
  304. dest[i]=source[i];
  305. }
  306. dest[count]=0;
  307. }
  308. /**
  309. * Checks wether two strings are equal (case insensetive)
  310. * @param str1
  311. * str2
  312. */
  313. stock equal(str1[],str2[],bool:ignorecase) {
  314. if (strlen(str1)!=strlen(str2)) return false;
  315. if (strcmp(str1,str2,ignorecase)==0) return true;
  316. return false;
  317. }
  318. /**
  319. * Returns an element of a string splitted by ' ', default index is 0.
  320. * @param string
  321. * index
  322. */
  323. stock strtok(const string[], &index,seperator=' ')
  324. {
  325. new length = strlen(string);
  326. new offset = index;
  327. new result[MAX_STRING];
  328. while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
  329. {
  330. result[index - offset] = string[index];
  331. index++;
  332. }
  333. result[index - offset] = EOS;
  334. if ((index < length) && (string[index] == seperator))
  335. {
  336. index++;
  337. }
  338. return result;
  339. }
  340. stock mod(up,down) {
  341. return up-(floatround((up/down),floatround_floor))*down;
  342. }
  343. stock div(up,down) {
  344. return (floatround((up/down),floatround_floor));
  345. }
  346. /**
  347. * Returns a hashed value in adler32 as int
  348. * @param buf
  349. */
  350. stock num_hash(buf[])
  351. {
  352. new length=strlen(buf);
  353. new s1 = 1;
  354. new s2 = 0;
  355. new n;
  356. for (n=0; n<length; n++) {
  357. s1 = (s1 + buf[n]) % 65521;
  358. s2 = (s2 + s1) % 65521;
  359. }
  360. return (s2 << 16) + s1;
  361. }
  362. /**
  363. * Returns a hashed value in adler32 as string
  364. * @param buf
  365. */
  366. stock hash(str2[]) {
  367. new tmpdasdsa[MAX_STRING];
  368. tmpdasdsa[0]=0;
  369. valstr(tmpdasdsa,num_hash(str2));
  370. return tmpdasdsa;
  371. }
  372. /**
  373. * Returns a string which has 'newstr' where 'trg' was before
  374. * @param trg
  375. * newstr
  376. * src
  377. */
  378. stock strreplace(trg[],newstr[],src[]) {
  379. new f=0;
  380. new s1[MAX_STRING];
  381. new tmp[MAX_STRING];
  382. format(s1,sizeof(s1),"%s",src);
  383. f = strfind(s1,trg);
  384. tmp[0]=0;
  385. while (f>=0) {
  386. strcat(tmp,ret_memcpy(s1, 0, f));
  387. strcat(tmp,newstr);
  388. format(s1,sizeof(s1),"%s",ret_memcpy(s1, f+strlen(trg), strlen(s1)-f));
  389. f = strfind(s1,trg);
  390. }
  391. strcat(tmp,s1);
  392. return tmp;
  393. }
  394. /**
  395. * Returns the string with lowercase
  396. * @param txt
  397. */
  398. stock strlower(txt[]) {
  399. new tmp[MAX_STRING];
  400. tmp[0]=0;
  401. if (txt[0]==0) return tmp;
  402. new i=0;
  403. for (i=0;i<strlen(txt);i++) {
  404. tmp[i]=tolower(txt[i]);
  405. }
  406. tmp[strlen(txt)]=0;
  407. return tmp;
  408. }
  409. /**
  410. * Returns the string with uppercase
  411. * @param txt
  412. */
  413. stock strupper(txt[]) {
  414. new tmp[MAX_STRING];
  415. tmp[0]=0;
  416. if (txt[0]==0) return tmp;
  417. new i=0;
  418. for (i=0;i<strlen(txt);i++) {
  419. tmp[i]=toupper(txt[i]);
  420. }
  421. tmp[strlen(txt)]=0;
  422. return tmp;
  423. }