Advertisement
Meta__

cMd v0.1a

Nov 26th, 2011
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.21 KB | None | 0 0
  1. /************************************************************************************\
  2. **                                                                                  **
  3. **  cMd v0.1 (also called "mcmd") by Meta. (Capital "M" because of "Meta")          **
  4. **  Copyright 2011 Meta (http://meta.spitnex.de/?lang=en). All rights reserved.     **
  5. **                                                                                  **
  6. **  Time in tests (no liez!): around 2-4 ms! (can't believe it myself)              **
  7. **  And yes, the uncommented 3 lines below (depends on "benchmark" definition) are  **
  8. **  the FULL code! (instead of sscanf by Y_less)                                    **
  9. **                                                                                  **
  10. **************************************************************************************
  11. **                                                                                  **
  12. **                      === TUTORIAL ===                                            **
  13. **                                                                                  **
  14. **  - Use "do_cMd;" in "OnPlayerCommandText" to call the command like that:         **
  15. **                                                                                  **
  16. **  public OnPlayerCommandText(playerid, cmdtext[])                                 **
  17. **  {                                                                               **
  18. **      // code ...                                                                 **
  19. **      do_cMd;                                                                     **
  20. **      // code ...                                                                 **
  21. **  }                                                                               **
  22. **                                                                                  **
  23. **                                                                                  **
  24. **  - Define Commands like that:                                                    **
  25. **                                                                                  **
  26. **  cMd:CommandName;                                                                **
  27. **  {                                                                               **
  28. **      // code ...                                                                 **
  29. **  }                                                                               **
  30. **                                                                                  **
  31. **    ... as a standalone function like public or stock.                            **
  32. **                                                                                  **
  33. **  - playerid and params[] are available in every command, params[] includes the   **
  34. **    command's parameters you can split with sscanf                                **
  35. **                                                                                  **
  36. \************************************************************************************/
  37.  
  38. #define benchmark
  39.  
  40. #pragma tabsize 0
  41.  
  42. #if !defined benchmark
  43.     #define do_cMd; \
  44.      new cMdtMp[2][128],cMd;sscanf(cmdtext[1],"ss",cMdtMp[0],cMdtMp[1]);for(new v_cMd;v_cMd<strlen(cMdtMp[0]);v_cMd++){cMdtMp[0][v_cMd]=tolower(cMdtMp[0][v_cMd]);}if(!strlen(cMdtMp[1])){cMdtMp[1]=" ";} \
  45.         format(cmdtext,32,"cMd_%s",cMdtMp[0]);if(funcidx(cmdtext)!=-1&&!cMd){cMd=1;CallLocalFunction(cmdtext,"ds",playerid,cMdtMp[1]);}
  46. #else
  47.     #define do_cMd; \
  48.         new cMdtMp[2][128],cMd,cMd_time=GetTickCount();sscanf(cmdtext[1],"ss",cMdtMp[0],cMdtMp[1]);for(new v_cMd;v_cMd<strlen(cMdtMp[0]);v_cMd++){cMdtMp[0][v_cMd]=tolower(cMdtMp[0][v_cMd]);}if(!strlen(cMdtMp[1])){cMdtMp[1]=" ";} \
  49.         format(cmdtext,32,"cMd_%s",cMdtMp[0]);if(funcidx(cmdtext)!=-1&&!cMd){cMd=1;CallLocalFunction(cmdtext,"ds",playerid,cMdtMp[1]);}printf("cMd_Start:  %d\ncMd_End:    %d\nDifference: %d",cMd_time,GetTickCount(),GetTickCount()-cMd_time);
  50. #endif
  51.  
  52. #define cMd:%0; forward cMd_%0(playerid, params[128]); public cMd_%0(playerid, params[128])
  53.  
  54.  
  55. #if !defined sscanf
  56. /*----------------------------------------------------------------------------*-
  57. Function:
  58.     sscanf
  59. Params:
  60.     string[] - String to extract parameters from.
  61.     format[] - Parameter types to get.
  62.     {Float,_}:... - Data return variables.
  63. Return:
  64.     0 - Successful, not 0 - fail.
  65. Notes:
  66.     A fail is either insufficient variables to store the data or insufficient
  67.     data for the format string - excess data is disgarded.
  68.  
  69.     A string in the middle of the input data is extracted as a single word, a
  70.     string at the end of the data collects all remaining text.
  71.  
  72.     The format codes are:
  73.  
  74.     c - A character.
  75.     d, i - An integer.
  76.     h, x - A hex number (e.g. a colour).
  77.     f - A float.
  78.     s - A string.
  79.     z - An optional string.
  80.     pX - An additional delimiter where X is another character.
  81.     '' - Encloses a litteral string to locate.
  82.     u - User, takes a name, part of a name or an id and returns the id if they're connected.
  83.  
  84.     Now has IsNumeric integrated into the code.
  85.  
  86.     Added additional delimiters in the form of all whitespace and an
  87.     optioanlly specified one in the format string.
  88. -*----------------------------------------------------------------------------*/
  89.  
  90. stock sscanf(string[], format[], {Float,_}:...)
  91. {
  92. #if defined isnull
  93.     if (isnull(string))
  94. #else
  95.     if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  96. #endif
  97.     {
  98.         return format[0];
  99.     }
  100.     new formatPos = 0, stringPos = 0, paramPos = 2, paramCount = numargs(), delim = ' ';
  101.     while (string[stringPos] && string[stringPos] <= ' ')
  102.     {
  103.         stringPos++;
  104.     }
  105.     while (paramPos < paramCount && string[stringPos])
  106.     {
  107.         switch (format[formatPos++])
  108.         {
  109.             case EOS:
  110.             {
  111.                 return 0;
  112.             }
  113.             case 'i', 'd':
  114.             {
  115.                 new neg = 1, num = 0, ch = string[stringPos];
  116.                 if (ch == '-')
  117.                 {
  118.                     neg = -1;
  119.                     ch = string[++stringPos];
  120.                 }
  121.                 do
  122.                 {
  123.                     stringPos++;
  124.                     if ('0' <= ch <= '9')
  125.                     {
  126.                         num = (num * 10) + (ch - '0');
  127.                     }
  128.                     else
  129.                     {
  130.                         return -1;
  131.                     }
  132.                 }
  133.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  134.                 setarg(paramPos, 0, num * neg);
  135.             }
  136.             case 'h', 'x':
  137.             {
  138.                 new num = 0, ch = string[stringPos];
  139.                 do
  140.                 {
  141.                     stringPos++;
  142.                     switch (ch)
  143.                     {
  144.                         case 'x', 'X':
  145.                         {
  146.                             num = 0;
  147.                             continue;
  148.                         }
  149.                         case '0' .. '9':
  150.                         {
  151.                             num = (num << 4) | (ch - '0');
  152.                         }
  153.                         case 'a' .. 'f':
  154.                         {
  155.                             num = (num << 4) | (ch - ('a' - 10));
  156.                         }
  157.                         case 'A' .. 'F':
  158.                         {
  159.                             num = (num << 4) | (ch - ('A' - 10));
  160.                         }
  161.                         default:
  162.                         {
  163.                             return -1;
  164.                         }
  165.                     }
  166.                 }
  167.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  168.                 setarg(paramPos, 0, num);
  169.             }
  170.             case 'c':
  171.             {
  172.                 setarg(paramPos, 0, string[stringPos++]);
  173.             }
  174.             case 'f':
  175.             {
  176.  
  177.                 new changestr[16], changepos = 0, strpos = stringPos;
  178.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  179.                 {
  180.                     changestr[changepos++] = string[strpos++];
  181.                 }
  182.                 changestr[changepos] = EOS;
  183.                 setarg(paramPos,0,_:floatstr(changestr));
  184.             }
  185.             case 'p':
  186.             {
  187.                 delim = format[formatPos++];
  188.                 continue;
  189.             }
  190.             case '\'':
  191.             {
  192.                 new end = formatPos - 1, ch;
  193.                 while ((ch = format[++end]) && ch != '\'') {}
  194.                 if (!ch)
  195.                 {
  196.                     return -1;
  197.                 }
  198.                 format[end] = EOS;
  199.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  200.                 {
  201.                     if (format[end + 1])
  202.                     {
  203.                         return -1;
  204.                     }
  205.                     return 0;
  206.                 }
  207.                 format[end] = '\'';
  208.                 stringPos = ch + (end - formatPos);
  209.                 formatPos = end + 1;
  210.             }
  211.             case 'u':
  212.             {
  213.                 new end = stringPos - 1, id = 0, bool:num = true, ch;
  214.                 while ((ch = string[++end]) && ch != delim)
  215.                 {
  216.                     if (num)
  217.                     {
  218.                         if ('0' <= ch <= '9')
  219.                         {
  220.                             id = (id * 10) + (ch - '0');
  221.                         }
  222.                         else
  223.                         {
  224.                             num = false;
  225.                         }
  226.                     }
  227.                 }
  228.                 if (num && IsPlayerConnected(id))
  229.                 {
  230.                     setarg(paramPos, 0, id);
  231.                 }
  232.                 else
  233.                 {
  234.                     #if !defined foreach
  235.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  236.                         #define __SSCANF_FOREACH__
  237.                     #endif
  238.                     string[end] = EOS;
  239.                     num = false;
  240.                     new name[MAX_PLAYER_NAME];
  241.                     id = end - stringPos;
  242.                     foreach (Player, playerid)
  243.                     {
  244.                         GetPlayerName(playerid, name, sizeof (name));
  245.                         if (!strcmp(name, string[stringPos], true, id))
  246.                         {
  247.                             setarg(paramPos, 0, playerid);
  248.                             num = true;
  249.                             break;
  250.                         }
  251.                     }
  252.                     if (!num)
  253.                     {
  254.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  255.                     }
  256.                     string[end] = ch;
  257.                     #if defined __SSCANF_FOREACH__
  258.                         #undef foreach
  259.                         #undef __SSCANF_FOREACH__
  260.                     #endif
  261.                 }
  262.                 stringPos = end;
  263.             }
  264.             case 's', 'z':
  265.             {
  266.                 new i = 0, ch;
  267.                 if (format[formatPos])
  268.                 {
  269.                     while ((ch = string[stringPos++]) && ch != delim)
  270.                     {
  271.                         setarg(paramPos, i++, ch);
  272.                     }
  273.                     if (!i)
  274.                     {
  275.                         return -1;
  276.                     }
  277.                 }
  278.                 else
  279.                 {
  280.                     while ((ch = string[stringPos++]))
  281.                     {
  282.                         setarg(paramPos, i++, ch);
  283.                     }
  284.                 }
  285.                 stringPos--;
  286.                 setarg(paramPos, i, EOS);
  287.             }
  288.             default:
  289.             {
  290.                 continue;
  291.             }
  292.         }
  293.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  294.         {
  295.             stringPos++;
  296.         }
  297.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  298.         {
  299.             stringPos++;
  300.         }
  301.         paramPos++;
  302.     }
  303.     do
  304.     {
  305.         if ((delim = format[formatPos++]) > ' ')
  306.         {
  307.             if (delim == '\'')
  308.             {
  309.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  310.             }
  311.             else if (delim != 'z')
  312.             {
  313.                 return delim;
  314.             }
  315.         }
  316.     }
  317.     while (delim > ' ');
  318.     return 0;
  319. }
  320.  
  321. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement