Advertisement
Guest User

Untitled

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