Advertisement
Guest User

utils.inc

a guest
Apr 21st, 2011
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.58 KB | None | 0 0
  1. IsNumeric(const string[])
  2. {
  3.     for (new i = 0, j = strlen(string); i < j; i++)
  4.     {
  5.         if (string[i] > '9' || string[i] < '0') return 0;
  6.     }
  7.     return 1;
  8. }
  9.  
  10. ReturnUser(text[], playerid = INVALID_PLAYER_ID)
  11. {
  12.     new pos = 0;
  13.     while (text[pos] < 0x21) // Strip out leading spaces
  14.     {
  15.         if (text[pos] == 0) return INVALID_PLAYER_ID; // No passed text
  16.         pos++;
  17.     }
  18.     new userid = INVALID_PLAYER_ID;
  19.     if (IsNumeric(text[pos])) // Check whole passed string
  20.     {
  21.         // If they have a numeric name you have a problem (although names are checked on id failure)
  22.         userid = strval(text[pos]);
  23.         if (userid >=0 && userid < MAX_PLAYERS)
  24.         {
  25.             if(!IsPlayerConnected(userid))
  26.             {
  27.                 /*if (playerid != INVALID_PLAYER_ID)
  28.                 {
  29.                     SendClientMessage(playerid, 0xFF0000AA, "User not connected");
  30.                 }*/
  31.                 userid = INVALID_PLAYER_ID;
  32.             }
  33.             else
  34.             {
  35.                 return userid; // A player was found
  36.             }
  37.         }
  38.         /*else
  39.         {
  40.             if (playerid != INVALID_PLAYER_ID)
  41.             {
  42.                 SendClientMessage(playerid, 0xFF0000AA, "Invalid user ID");
  43.             }
  44.             userid = INVALID_PLAYER_ID;
  45.         }
  46.         return userid;*/
  47.         // Removed for fallthrough code
  48.     }
  49.     // They entered [part of] a name or the id search failed (check names just incase)
  50.     new len = strlen(text[pos]);
  51.     new count = 0;
  52.     new name[MAX_PLAYER_NAME];
  53.     for (new i = 0; i < MAX_PLAYERS; i++)
  54.     {
  55.         if (IsPlayerConnected(i))
  56.         {
  57.             GetPlayerName(i, name, sizeof (name));
  58.             if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
  59.             {
  60.                 if (len == strlen(name)) // Exact match
  61.                 {
  62.                     return i; // Return the exact player on an exact match
  63.                     // Otherwise if there are two players:
  64.                     // Me and MeYou any time you entered Me it would find both
  65.                     // And never be able to return just Me's id
  66.                 }
  67.                 else // Partial match
  68.                 {
  69.                     count++;
  70.                     userid = i;
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     if (count != 1)
  76.     {
  77.         if (playerid != INVALID_PLAYER_ID)
  78.         {
  79.             if (count)
  80.             {
  81.                 SendClientMessage(playerid, 0xFF0000AA, "Multiple users found, please narrow earch");
  82.             }
  83.             else
  84.             {
  85.                 SendClientMessage(playerid, 0xFF0000AA, "No matching user found");
  86.             }
  87.         }
  88.         userid = INVALID_PLAYER_ID;
  89.     }
  90.     return userid; // INVALID_USER_ID for bad return
  91. }
  92.  
  93. stock sscanf(string[], format[], {Float,_}:...)
  94. {
  95.     #if defined isnull
  96.         if (isnull(string))
  97.     #else
  98.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  99.     #endif
  100.         {
  101.             return format[0];
  102.         }
  103.     #pragma tabsize 4
  104.     new
  105.         formatPos = 0,
  106.         stringPos = 0,
  107.         paramPos = 2,
  108.         paramCount = numargs(),
  109.         delim = ' ';
  110.     while (string[stringPos] && string[stringPos] <= ' ')
  111.     {
  112.         stringPos++;
  113.     }
  114.     while (paramPos < paramCount && string[stringPos])
  115.     {
  116.         switch (format[formatPos++])
  117.         {
  118.             case '\0':
  119.             {
  120.                 return 0;
  121.             }
  122.             case 'i', 'd':
  123.             {
  124.                 new
  125.                     neg = 1,
  126.                     num = 0,
  127.                     ch = string[stringPos];
  128.                 if (ch == '-')
  129.                 {
  130.                     neg = -1;
  131.                     ch = string[++stringPos];
  132.                 }
  133.                 do
  134.                 {
  135.                     stringPos++;
  136.                     if ('0' <= ch <= '9')
  137.                     {
  138.                         num = (num * 10) + (ch - '0');
  139.                     }
  140.                     else
  141.                     {
  142.                         return -1;
  143.                     }
  144.                 }
  145.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  146.                 setarg(paramPos, 0, num * neg);
  147.             }
  148.             case 'h', 'x':
  149.             {
  150.                 new
  151.                     num = 0,
  152.                     ch = string[stringPos];
  153.                 do
  154.                 {
  155.                     stringPos++;
  156.                     switch (ch)
  157.                     {
  158.                         case 'x', 'X':
  159.                         {
  160.                             num = 0;
  161.                             continue;
  162.                         }
  163.                         case '0' .. '9':
  164.                         {
  165.                             num = (num << 4) | (ch - '0');
  166.                         }
  167.                         case 'a' .. 'f':
  168.                         {
  169.                             num = (num << 4) | (ch - ('a' - 10));
  170.                         }
  171.                         case 'A' .. 'F':
  172.                         {
  173.                             num = (num << 4) | (ch - ('A' - 10));
  174.                         }
  175.                         default:
  176.                         {
  177.                             return -1;
  178.                         }
  179.                     }
  180.                 }
  181.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  182.                 setarg(paramPos, 0, num);
  183.             }
  184.             case 'c':
  185.             {
  186.                 setarg(paramPos, 0, string[stringPos++]);
  187.             }
  188.             case 'f':
  189.             {
  190.                 setarg(paramPos, 0, _:floatstr(string[stringPos]));
  191.             }
  192.             case 'p':
  193.             {
  194.                 delim = format[formatPos++];
  195.                 continue;
  196.             }
  197.             case '\'':
  198.             {
  199.                 new
  200.                     end = formatPos - 1,
  201.                     ch;
  202.                 while ((ch = format[++end]) && ch != '\'') {}
  203.                 if (!ch)
  204.                 {
  205.                     return -1;
  206.                 }
  207.                 format[end] = '\0';
  208.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  209.                 {
  210.                     if (format[end + 1])
  211.                     {
  212.                         return -1;
  213.                     }
  214.                     return 0;
  215.                 }
  216.                 format[end] = '\'';
  217.                 stringPos = ch + (end - formatPos);
  218.                 formatPos = end + 1;
  219.             }
  220.             case 'u':
  221.             {
  222.                 new
  223.                     end = stringPos - 1,
  224.                     id = 0,
  225.                     bool:num = true,
  226.                     ch;
  227.                 while ((ch = string[++end]) && ch != delim)
  228.                 {
  229.                     if (num)
  230.                     {
  231.                         if ('0' <= ch <= '9')
  232.                         {
  233.                             id = (id * 10) + (ch - '0');
  234.                         }
  235.                         else
  236.                         {
  237.                             num = false;
  238.                         }
  239.                     }
  240.                 }
  241.                 if (num && IsPlayerConnected(id))
  242.                 {
  243.                     setarg(paramPos, 0, id);
  244.                 }
  245.                 else
  246.                 {
  247.                     #if !defined foreach
  248.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  249.                         #define __SSCANF_FOREACH__
  250.                     #endif
  251.                     string[end] = '\0';
  252.                     num = false;
  253.                     new
  254.                         name[MAX_PLAYER_NAME];
  255.                     id = end - stringPos;
  256.                     foreach (Player, playerid)
  257.                     {
  258.                         GetPlayerName(playerid, name, sizeof (name));
  259.                         if (!strcmp(name, string[stringPos], true, id))
  260.                         {
  261.                             setarg(paramPos, 0, playerid);
  262.                             num = true;
  263.                             break;
  264.                         }
  265.                     }
  266.                     if (!num)
  267.                     {
  268.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  269.                     }
  270.                     string[end] = ch;
  271.                     #if defined __SSCANF_FOREACH__
  272.                         #undef foreach
  273.                         #undef __SSCANF_FOREACH__
  274.                     #endif
  275.                 }
  276.                 stringPos = end;
  277.             }
  278.             case 's', 'z':
  279.             {
  280.                 new
  281.                     i = 0,
  282.                     ch;
  283.                 if (format[formatPos])
  284.                 {
  285.                     while ((ch = string[stringPos++]) && ch != delim)
  286.                     {
  287.                         setarg(paramPos, i++, ch);
  288.                     }
  289.                     if (!i)
  290.                     {
  291.                         return -1;
  292.                     }
  293.                 }
  294.                 else
  295.                 {
  296.                     while ((ch = string[stringPos++]))
  297.                     {
  298.                         setarg(paramPos, i++, ch);
  299.                     }
  300.                 }
  301.                 stringPos--;
  302.                 setarg(paramPos, i, '\0');
  303.             }
  304.             default:
  305.             {
  306.                 continue;
  307.             }
  308.         }
  309.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  310.         {
  311.             stringPos++;
  312.         }
  313.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  314.         {
  315.             stringPos++;
  316.         }
  317.         paramPos++;
  318.     }
  319.     do
  320.     {
  321.         if ((delim = format[formatPos++]) > ' ')
  322.         {
  323.             if (delim == '\'')
  324.             {
  325.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  326.             }
  327.             else if (delim != 'z')
  328.             {
  329.                 return delim;
  330.             }
  331.         }
  332.     }
  333.     while (delim > ' ');
  334.     return 0;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement