Guest User

dutils include

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