Advertisement
losnato

[Include] sscanf

Jan 30th, 2012
2,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.62 KB | None | 0 0
  1. stock sscanf(string[], format[], {Float,_}:...)
  2. {
  3.     #if defined isnull
  4.         if (isnull(string))
  5.     #else
  6.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  7.     #endif
  8.         {
  9.             return format[0];
  10.         }
  11.     #pragma tabsize 4
  12.     new
  13.         formatPos = 0,
  14.         stringPos = 0,
  15.         paramPos = 2,
  16.         paramCount = numargs(),
  17.         delim = ' ';
  18.     while (string[stringPos] && string[stringPos] <= ' ')
  19.     {
  20.         stringPos++;
  21.     }
  22.     while (paramPos < paramCount && string[stringPos])
  23.     {
  24.         switch (format[formatPos++])
  25.         {
  26.             case '\0':
  27.             {
  28.                 return 0;
  29.             }
  30.             case 'i', 'd':
  31.             {
  32.                 new
  33.                     neg = 1,
  34.                     num = 0,
  35.                     ch = string[stringPos];
  36.                 if (ch == '-')
  37.                 {
  38.                     neg = -1;
  39.                     ch = string[++stringPos];
  40.                 }
  41.                 do
  42.                 {
  43.                     stringPos++;
  44.                     if ('0' <= ch <= '9')
  45.                     {
  46.                         num = (num * 10) + (ch - '0');
  47.                     }
  48.                     else
  49.                     {
  50.                         return -1;
  51.                     }
  52.                 }
  53.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  54.                 setarg(paramPos, 0, num * neg);
  55.             }
  56.             case 'h', 'x':
  57.             {
  58.                 new
  59.                     num = 0,
  60.                     ch = string[stringPos];
  61.                 do
  62.                 {
  63.                     stringPos++;
  64.                     switch (ch)
  65.                     {
  66.                         case 'x', 'X':
  67.                         {
  68.                             num = 0;
  69.                             continue;
  70.                         }
  71.                         case '0' .. '9':
  72.                         {
  73.                             num = (num << 4) | (ch - '0');
  74.                         }
  75.                         case 'a' .. 'f':
  76.                         {
  77.                             num = (num << 4) | (ch - ('a' - 10));
  78.                         }
  79.                         case 'A' .. 'F':
  80.                         {
  81.                             num = (num << 4) | (ch - ('A' - 10));
  82.                         }
  83.                         default:
  84.                         {
  85.                             return -1;
  86.                         }
  87.                     }
  88.                 }
  89.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  90.                 setarg(paramPos, 0, num);
  91.             }
  92.             case 'c':
  93.             {
  94.                 setarg(paramPos, 0, string[stringPos++]);
  95.             }
  96.             case 'f':
  97.             {
  98.  
  99.                 new changestr[16], changepos = 0, strpos = stringPos;    
  100.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  101.                 {
  102.                     changestr[changepos++] = string[strpos++];
  103.                     }
  104.                 changestr[changepos] = '\0';
  105.                 setarg(paramPos,0,_:floatstr(changestr));
  106.             }
  107.             case 'p':
  108.             {
  109.                 delim = format[formatPos++];
  110.                 continue;
  111.             }
  112.             case '\'':
  113.             {
  114.                 new
  115.                     end = formatPos - 1,
  116.                     ch;
  117.                 while ((ch = format[++end]) && ch != '\'') {}
  118.                 if (!ch)
  119.                 {
  120.                     return -1;
  121.                 }
  122.                 format[end] = '\0';
  123.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  124.                 {
  125.                     if (format[end + 1])
  126.                     {
  127.                         return -1;
  128.                     }
  129.                     return 0;
  130.                 }
  131.                 format[end] = '\'';
  132.                 stringPos = ch + (end - formatPos);
  133.                 formatPos = end + 1;
  134.             }
  135.             case 'u':
  136.             {
  137.                 new
  138.                     end = stringPos - 1,
  139.                     id = 0,
  140.                     bool:num = true,
  141.                     ch;
  142.                 while ((ch = string[++end]) && ch != delim)
  143.                 {
  144.                     if (num)
  145.                     {
  146.                         if ('0' <= ch <= '9')
  147.                         {
  148.                             id = (id * 10) + (ch - '0');
  149.                         }
  150.                         else
  151.                         {
  152.                             num = false;
  153.                         }
  154.                     }
  155.                 }
  156.                 if (num && IsPlayerConnected(id))
  157.                 {
  158.                     setarg(paramPos, 0, id);
  159.                 }
  160.                 else
  161.                 {
  162.                     #if !defined foreach
  163.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  164.                         #define __SSCANF_FOREACH__
  165.                     #endif
  166.                     string[end] = '\0';
  167.                     num = false;
  168.                     new
  169.                         name[MAX_PLAYER_NAME];
  170.                     id = end - stringPos;
  171.                     foreach (Player, playerid)
  172.                     {
  173.                         GetPlayerName(playerid, name, sizeof (name));
  174.                         if (!strcmp(name, string[stringPos], true, id))
  175.                         {
  176.                             setarg(paramPos, 0, playerid);
  177.                             num = true;
  178.                             break;
  179.                         }
  180.                     }
  181.                     if (!num)
  182.                     {
  183.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  184.                     }
  185.                     string[end] = ch;
  186.                     #if defined __SSCANF_FOREACH__
  187.                         #undef foreach
  188.                         #undef __SSCANF_FOREACH__
  189.                     #endif
  190.                 }
  191.                 stringPos = end;
  192.             }
  193.             case 's', 'z':
  194.             {
  195.                 new
  196.                     i = 0,
  197.                     ch;
  198.                 if (format[formatPos])
  199.                 {
  200.                     while ((ch = string[stringPos++]) && ch != delim)
  201.                     {
  202.                         setarg(paramPos, i++, ch);
  203.                     }
  204.                     if (!i)
  205.                     {
  206.                         return -1;
  207.                     }
  208.                 }
  209.                 else
  210.                 {
  211.                     while ((ch = string[stringPos++]))
  212.                     {
  213.                         setarg(paramPos, i++, ch);
  214.                     }
  215.                 }
  216.                 stringPos--;
  217.                 setarg(paramPos, i, '\0');
  218.             }
  219.             default:
  220.             {
  221.                 continue;
  222.             }
  223.         }
  224.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  225.         {
  226.             stringPos++;
  227.         }
  228.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  229.         {
  230.             stringPos++;
  231.         }
  232.         paramPos++;
  233.     }
  234.     do
  235.     {
  236.         if ((delim = format[formatPos++]) > ' ')
  237.         {
  238.             if (delim == '\'')
  239.             {
  240.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  241.             }
  242.             else if (delim != 'z')
  243.             {
  244.                 return delim;
  245.             }
  246.         }
  247.     }
  248.     while (delim > ' ');
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement