Y_Less

sscanf

Jul 14th, 2008
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*-
  2. Function:
  3.     sscanf
  4. Params:
  5.     string[] - String to extract parameters from.
  6.     format[] - Parameter types to get.
  7.     {Float,_}:... - Data return variables.
  8. Return:
  9.     0 - Successful, not 0 - fail.
  10. Notes:
  11.     A fail is either insufficient variables to store the data or insufficient
  12.     data for the format string - excess data is disgarded.
  13.  
  14.     A string in the middle of the input data is extracted as a single word, a
  15.     string at the end of the data collects all remaining text.
  16.  
  17.     The format codes are:
  18.  
  19.     c - A character.
  20.     d, i - An integer.
  21.     h, x - A hex number (e.g. a colour).
  22.     f - A float.
  23.     s - A string.
  24.     z - An optional string.
  25.     pX - An additional delimiter where X is another character.
  26.    
  27.     Now has IsNumeric integrated into the code.
  28.    
  29.     Added additional delimiters in the form of all whitespace and an
  30.     optioanlly specified one in the format string.
  31. -*----------------------------------------------------------------------------*/
  32.  
  33. stock sscanf(string[], format[], {Float,_}:...)
  34. {
  35.     if (isnull(string))
  36.     {
  37.         return format[0];
  38.     }
  39.     new
  40.         formatPos = 0,
  41.         stringPos = 0,
  42.         paramPos = 2,
  43.         paramCount = numargs(),
  44.         delim = ' ';
  45.     while (paramPos < paramCount && string[stringPos])
  46.     {
  47.         switch (format[formatPos++])
  48.         {
  49.             case '\0':
  50.             {
  51.                 return 0;
  52.             }
  53.             case 'i', 'd':
  54.             {
  55.                 new
  56.                     neg = 1,
  57.                     num = 0,
  58.                     ch = string[stringPos];
  59.                 if (ch == '-')
  60.                 {
  61.                     neg = -1;
  62.                     ch = string[++stringPos];
  63.                 }
  64.                 do
  65.                 {
  66.                     stringPos++;
  67.                     if (ch >= '0' && ch <= '9')
  68.                     {
  69.                         num = (num * 10) + (ch - '0');
  70.                     }
  71.                     else
  72.                     {
  73.                         return -1;
  74.                     }
  75.                 }
  76.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  77.                 setarg(paramPos, 0, num * neg);
  78.             }
  79.             case 'h', 'x':
  80.             {
  81.                 new
  82.                     ch,
  83.                     num = 0;
  84.                 while ((ch = string[stringPos]) > ' ' && ch != delim)
  85.                 {
  86.                     switch (ch)
  87.                     {
  88.                         case 'x', 'X':
  89.                         {
  90.                             num = 0;
  91.                             continue;
  92.                         }
  93.                         case '0' .. '9':
  94.                         {
  95.                             num = (num << 4) | (ch - '0');
  96.                         }
  97.                         case 'a' .. 'f':
  98.                         {
  99.                             num = (num << 4) | (ch - ('a' - 10));
  100.                         }
  101.                         case 'A' .. 'F':
  102.                         {
  103.                             num = (num << 4) | (ch - ('A' - 10));
  104.                         }
  105.                         default:
  106.                         {
  107.                             return -1;
  108.                         }
  109.                     }
  110.                 }
  111.                 setarg(paramPos, 0, num);
  112.             }
  113.             case 'c':
  114.             {
  115.                 setarg(paramPos, 0, string[stringPos++]);
  116.             }
  117.             case 'f':
  118.             {
  119.                 setarg(paramPos, 0, _:floatstr(string[stringPos]));
  120.             }
  121.             case 'p':
  122.             {
  123.                 delim = format[formatPos++];
  124.                 continue;
  125.             }
  126.             case 's', 'z':
  127.             {
  128.                 new
  129.                     i = 0,
  130.                     ch;
  131.                 if (format[formatPos])
  132.                 {
  133.                     while ((ch = string[stringPos++]) && ch != delim)
  134.                     {
  135.                         setarg(paramPos, i++, ch);
  136.                     }
  137.                     if (!i)
  138.                     {
  139.                         return -1;
  140.                     }
  141.                 }
  142.                 else
  143.                 {
  144.                     while ((ch = string[stringPos++]))
  145.                     {
  146.                         setarg(paramPos, i++, ch);
  147.                     }
  148.                 }
  149.                 stringPos--;
  150.                 setarg(paramPos, i, '\0');
  151.             }
  152.             default:
  153.             {
  154.                 continue;
  155.             }
  156.         }
  157.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  158.         {
  159.             stringPos++;
  160.         }
  161.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  162.         {
  163.             stringPos++;
  164.         }
  165.         paramPos++;
  166.     }
  167.     while (format[formatPos] == 'z') formatPos++;
  168.     if (format[formatPos] > ' ') return format[formatPos];
  169.     return 0;
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment