Advertisement
delaypawn

dutils

Aug 11th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.95 KB | None | 0 0
  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.  
  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.  *  Strips Newline from the end of a string.
  264.  *  Idea: Y_Less, Bugfixing (when length=1) by DracoBlue
  265.  *  @param   string
  266.  */
  267. stock StripNewLine(string[])
  268. {
  269.     new len = strlen(string);
  270.     if (string[0]==0) return ;
  271.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  272.         string[len - 1] = 0;
  273.         if (string[0]==0) return ;
  274.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  275.     }
  276. }
  277.  
  278. /**
  279.  *  Copies items from one array/string into return.
  280.  *  @param   source
  281.  *           index (where to start, 0 is first)
  282.  *           numbytes (how much)
  283.  */
  284. ret_memcpy(source[],index=0,numbytes) {
  285.     new tmp[MAX_STRING];
  286.     new i=0;
  287.     tmp[0]=0;
  288.     if (index>=strlen(source)) return tmp;
  289.     if (numbytes+index>=strlen(source)) numbytes=strlen(source)-index;
  290.     if (numbytes<=0) return tmp;
  291.     for (i=index;i<numbytes+index;i++) {
  292.         tmp[i-index]=source[i];
  293.         if (source[i]==0) return tmp;
  294.     }
  295.     tmp[numbytes]=0;
  296.     return tmp;
  297. }
  298.  
  299. /**
  300.  *  Copies items from one array/string into another.
  301.  *  @param   dest
  302.  *           source
  303.  *           count
  304.  */
  305. stock copy(dest[],source[],count) {
  306.     dest[0]=0;
  307.     if (count<0) return false;
  308.     if (count>strlen(source)) count=strlen(source);
  309.     new i=0;
  310.     for (i=0;i<count;i++) {
  311.         dest[i]=source[i];
  312.         if (source[i]==0) return true;
  313.     }
  314.     dest[count]=0;
  315.     return true;
  316. }
  317.  
  318.  
  319. /**
  320.  *  Deletes the first 'count' items of a array/string
  321.  *  @param   string[]
  322.  *           count
  323.  */
  324. stock delete(string[],count) {
  325.     new tmp[MAX_STRING];
  326.     tmp[0]=0;
  327.     if (count<=0) {
  328.         format(tmp,sizeof(tmp),"%s",string);
  329.         return tmp;
  330.     }
  331.     tmp=ret_memcpy(string,count,strlen(string));
  332.     return tmp;
  333. }
  334.  
  335. /**
  336.  *  Sets a string's value to source.
  337.  *  @param   dest
  338.  *           source
  339.  *           count
  340.  */
  341. stock set(dest[],source[]) {
  342.     new count = strlen(source);
  343.     new i=0;
  344.     for (i=0;i<count;i++) {
  345.         dest[i]=source[i];
  346.     }
  347.     dest[count]=0;
  348. }
  349.  
  350. /**
  351.  *  Checks wether two strings are equal (case insensetive)
  352.  *  @param   str1
  353.  *           str2
  354.  */
  355. stock equal(str1[],str2[],bool:ignorecase) {
  356.     if (strlen(str1)!=strlen(str2)) return false;
  357.     if (strcmp(str1,str2,ignorecase)==0) return true;
  358.     return false;
  359. }
  360.  
  361. /**
  362.  *  Returns an element of a string splitted by ' ', default index is 0.
  363.  *  @param   string
  364.  *           index
  365.  */
  366.  
  367. stock mod(up,down) {
  368.   return up-(floatround((up/down),floatround_floor))*down;
  369. }
  370.  
  371. stock div(up,down) {
  372.   return (floatround((up/down),floatround_floor));
  373. }
  374.  
  375. /**
  376.  *  Returns a hashed value in adler32 as int
  377.  *  @param   buf
  378.  */
  379. stock num_hash(buf[])
  380.  {
  381.     new length=strlen(buf);
  382.     new s1 = 1;
  383.     new s2 = 0;
  384.     new n;
  385.     for (n=0; n<length; n++) {
  386.        s1 = (s1 + buf[n]) % 65521;
  387.        s2 = (s2 + s1)     % 65521;
  388.     }
  389.     return (s2 << 16) + s1;
  390.  }
  391.  
  392. /**
  393.  *  Returns a hashed value in adler32 as string
  394.  *  @param   buf
  395.  */
  396. stock hash(str2[]) {
  397.    new tmpdasdsa[MAX_STRING];
  398.    tmpdasdsa[0]=0;
  399.    valstr(tmpdasdsa,num_hash(str2));
  400.    return tmpdasdsa;
  401. }
  402.  
  403. /**
  404.  *  Returns a string which has 'newstr' where 'trg' was before
  405.  *  @param   trg
  406.  *           newstr
  407.  *           src
  408.  */
  409. stock strreplace(trg[],newstr[],src[]) {
  410.     new f=0;
  411.     new s1[MAX_STRING];
  412.     new tmp[MAX_STRING];
  413.     format(s1,sizeof(s1),"%s",src);
  414.     f = strfind(s1,trg);
  415.     tmp[0]=0;
  416.     while (f>=0) {
  417.         strcat(tmp,ret_memcpy(s1, 0, f));
  418.         strcat(tmp,newstr);
  419.         format(s1,sizeof(s1),"%s",ret_memcpy(s1, f+strlen(trg), strlen(s1)-f));
  420.         f = strfind(s1,trg);
  421.     }
  422.     strcat(tmp,s1);
  423.     return tmp;
  424. }
  425.  
  426. /**
  427.  *  Returns the string with lowercase
  428.  *  @param   txt
  429.  */
  430. stock strlower(txt[]) {
  431.     new tmp[MAX_STRING];
  432.     tmp[0]=0;
  433.     if (txt[0]==0) return tmp;
  434.     new i=0;
  435.     for (i=0;i<strlen(txt);i++) {
  436.         tmp[i]=tolower(txt[i]);
  437.     }
  438.     tmp[strlen(txt)]=0;
  439.     return tmp;
  440. }
  441.  
  442. /**
  443.  *  Returns the string with uppercase
  444.  *  @param   txt
  445.  */
  446. stock strupper(txt[]) {
  447.     new tmp[MAX_STRING];
  448.     tmp[0]=0;
  449.     if (txt[0]==0) return tmp;
  450.     new i=0;
  451.     for (i=0;i<strlen(txt);i++) {
  452.         tmp[i]=toupper(txt[i]);
  453.     }
  454.     tmp[strlen(txt)]=0;
  455.     return tmp;
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement