Guest User

Untitled

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