jewalky

Untitled

Jan 13th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1.  
  2.     // note: we don't need a real printf format parser.
  3.     //       enough to simply find the subtitution tokens and feed them to the real printf after checking types.
  4.     //       https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification
  5.     FString output;
  6.     bool in_fmt = false;
  7.     FString fmt_current;
  8.     int argnum = -1;
  9.     int argauto = paramnum+1;
  10.     // % = starts
  11.     //  [0-9], -, +, \s, 0, #, . continue
  12.     //  %, s, d, i, u, fF, eE, gG, xX, o, c, p, aA terminate
  13.     // the only combination that is parsed locally is %n$...
  14.     for (int i = 0; i < fmt.Len(); i++)
  15.     {
  16.         char c = fmt[i];
  17.         if (in_fmt)
  18.         {
  19.             if ((c >= '0' && c <= '9') ||
  20.                 c == '-' || c == '+' || (c == ' ' && fmt_current[fmt_current.Len()-1] != ' ') || c == '#' || c == '.')
  21.             {
  22.                 fmt_current += c;
  23.             }
  24.             else
  25.             {
  26.                 if (argnum < 0) argnum = argauto;
  27.  
  28.                 Printf("c = %c\n", c);
  29.  
  30.                 switch (c)
  31.                 {
  32.                 case 's':
  33.                 {
  34.                     PARAM_INT_AT(argnum, _s);
  35.                     //PARAM_POINTER_AT(argnum, _s, int);
  36.                     Printf("p = %s\n", FName(_s).GetChars());
  37.                     //output += _s;
  38.                     in_fmt = false;
  39.                     break;
  40.                 }
  41.  
  42.                 case 'd':
  43.                 case 'i':
  44.                 case 'u':
  45.                 case 'f':
  46.                 case 'F':
  47.                 case 'e':
  48.                 case 'E':
  49.                 case 'g':
  50.                 case 'G':
  51.                 case 'x':
  52.                 case 'X':
  53.                 case 'o':
  54.                 case 'c':
  55.                 case 'p':
  56.                 case 'a':
  57.                 case 'A':
  58.                     output += "{arg}";
  59.                     in_fmt = false;
  60.                     break;
  61.                 default:
  62.                     // invalid character
  63.                     output += fmt_current + c;
  64.                     in_fmt = false;
  65.                     break;
  66.                 }
  67.             }
  68.         }
  69.         else
  70.         {
  71.             if (c == '%')
  72.             {
  73.                 if (i + 1 < fmt.Len() && fmt[i + 1] == '%')
  74.                 {
  75.                     output += '%';
  76.                     i++;
  77.                 }
  78.                 else
  79.                 {
  80.                     in_fmt = true;
  81.                     fmt_current = "%";
  82.                     argnum = -1;
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 output += c;
  88.             }
  89.         }
  90.     }
  91.  
  92.     // list arguments for now
  93.     /*paramnum++; // not sure if this is needed
  94.     for (int i = 1; i < numparam; i++)
  95.     {
  96.         PARAM_STRING_AT()
  97.     }*/
Advertisement
Add Comment
Please, Sign In to add comment