Advertisement
DraKiNs

[COD] sscanf developement

Sep 12th, 2011
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.17 KB | None | 0 0
  1. /*=================================================================
  2.      _ ____     _____         __  __
  3.     (_)  _ \ ___  |_   _|__  __ _|  \/  |
  4.     | | |_) / __|   | |/ _ \/ _` | |\/| |
  5.     | |  __/\__ \   | |  __/ (_| | |  | |
  6.     |_|_|   |___/   |_|\___|\__,_|_|  |_|
  7.  
  8.         Created by Bruno da Silva
  9.         www.ips-team.blogspot.com
  10.           sscanf Compactible with 0.3d Version!
  11.  
  12. =================================================================*/
  13.  
  14. ////////////////////////////////////////////////////////////
  15. //
  16. //
  17. //  Versão atual: 13/09/2011 - 18:20
  18. //  Versão: Testes Beta (próximo lançamento 15/11)
  19. //
  20. //  Returns:
  21. //   false: error and true: sucess
  22. //
  23. //  Format:
  24. //  u - name or id player
  25. //  s ~ z - string
  26. //  i ~ d - numbers
  27. //  c     - caractere
  28. //  f     - float
  29. //  r(iszd)  - reverte string/número/float
  30. //  p<delimiter> - novo delimitador
  31. //  e<formatenum> - alocar formatação na enum
  32. //  a<formatarray> - alocar formatação aqui
  33. //  g(searchstring) - procurar string e retornar o inicio e o fim dela
  34. //  {comentários na formatação}
  35. //
  36. //
  37. //
  38. //  NOT FINISHED (security problems)
  39. //
  40. //  Autor: [iPs] BrunoSilva (Copyright iPsTeaM)
  41. //  Thank Y_Less (designed it from the script his)
  42. //
  43. //  www.ips-team.blogspot.com
  44. //
  45. ///////////////////////////////////////////////////////////
  46.  
  47.  
  48. sscanf(string[], format[],{Float,_}:...)
  49. {
  50.  
  51.     #if !defined isNull
  52.     #define isNull(%0) (!%0[0]  || %0[0] == '\1' && !%0[1])
  53.     #endif
  54.  
  55.     #if defined strcpy
  56.     #undef strcpy
  57.     #endif
  58.  
  59.     #if !defined foreach
  60.     #define foreach(%0,%1) for (new %1 = 0; %1 != MAX_PLAYERS; ++%1) if(IsPlayerConnected(%1))
  61.     #endif
  62.  
  63.     #define strcpy(%0,%1) strcat((%0[0] = '\0', %0), %1, sizeof(%0))
  64.  
  65.     #define MAX_BUFFER  128
  66.  
  67.     if(isNull(string))
  68.     {
  69.         return false;
  70.  
  71.     }
  72.  
  73.     new
  74.         formatCount = -1,
  75.         stringCount = -1,
  76.         paramsCount = 2,
  77.         delim = 0x20,
  78.         bool:silentMod = false,
  79.         totalParams = numargs()
  80.     ;
  81.  
  82.     static
  83.         temporaria[MAX_BUFFER]
  84.     ;
  85.  
  86.     strcpy(temporaria, string);
  87.  
  88.     #pragma unused string
  89.  
  90.     while(format[++formatCount])
  91.     {
  92.         switch(format[formatCount])
  93.         {
  94.             case '{':
  95.  
  96.                 {
  97.                     silentMod = true;
  98.                     continue;
  99.  
  100.                 }
  101.  
  102.                 case '}':
  103.  
  104.             {
  105.                 silentMod = false;
  106.                 continue;
  107.  
  108.             }
  109.  
  110.  
  111.         }
  112.  
  113.         if(silentMod)
  114.         {
  115.             continue;
  116.  
  117.  
  118.         }
  119.  
  120.         switch(format[formatCount])
  121.         {
  122.             case 'i', 'd':
  123.  
  124.             {
  125.                 new
  126.                 numero,
  127.                 caractere = temporaria[stringCount + 1],
  128.                 negativo = 1                                                ;
  129.  
  130.                 if(caractere == '-')
  131.                 {
  132.                     ++stringCount;
  133.                     negativo = -1;
  134.  
  135.                 }
  136.  
  137.                 while((caractere = temporaria[++stringCount]) != delim && caractere)
  138.                 {
  139.                     if ('0' <= caractere <= '9')
  140.                     {
  141.                         numero =  (numero * 10) + (caractere - '0');
  142.  
  143.                     }
  144.                     else
  145.                     {
  146.                         return false;
  147.  
  148.                     }
  149.  
  150.  
  151.                 }
  152.  
  153.                 setarg(paramsCount, 0, numero * negativo);
  154.                 ++paramsCount;
  155.                 continue;
  156.  
  157.  
  158.             }
  159.  
  160.             case 'f':
  161.  
  162.             {
  163.                
  164.                 new sscanfValor = -1;
  165.                    
  166.                 new
  167.                     stringFloat[15]
  168.                 ;
  169.  
  170.                 while(temporaria[++stringCount] != delim && temporaria[stringCount])
  171.                 {
  172.                     stringFloat[++sscanfValor] = temporaria[stringCount];
  173.  
  174.                 }
  175.  
  176.                 if(sscanfValor != -1)
  177.                 {
  178.                     setarg(paramsCount, 0, _:floatstr(stringFloat));
  179.                     ++paramsCount;
  180.                 }
  181.                
  182.                 #pragma unused sscanfValor
  183.                 continue;
  184.  
  185.  
  186.             }
  187.  
  188.             case 'u':
  189.  
  190.             {
  191.                 new
  192.                     texto,
  193.                     tempVal = stringCount,
  194.                     stringTemp[MAX_BUFFER],
  195.                     caractere = temporaria[stringCount + 1]   ;
  196.  
  197.                 while((caractere = temporaria[++tempVal]) != delim && temporaria[stringCount]) if ('0' > caractere || '9' < caractere)
  198.                 {
  199.                     texto = 1;
  200.                     break;
  201.  
  202.                 }
  203.  
  204.                 if(!texto)
  205.                 {
  206.                     while((caractere = temporaria[++stringCount])!= delim && caractere)
  207.                     {
  208.                         texto  =  (texto * 10) + (caractere - '0');
  209.  
  210.  
  211.                     }
  212.                     texto = 10;
  213.                     setarg(paramsCount, 0, texto);
  214.  
  215.  
  216.                 }
  217.                 else
  218.                 {
  219.                     texto = -1;
  220.                     while(temporaria[++stringCount] != delim && temporaria[stringCount])
  221.                     {
  222.                         stringTemp[++texto] = temporaria[stringCount];
  223.  
  224.                     }
  225.  
  226.                     foreach(Player, i)
  227.                     {
  228.                         static
  229.                         name[MAX_PLAYER_NAME]
  230.                         ;
  231.  
  232.                         GetPlayerName(i, name, MAX_PLAYER_NAME);
  233.                         if(-1 != strfind(name, stringTemp, true))
  234.                         {
  235.                             setarg(paramsCount, 0, i);
  236.                             texto = 10;
  237.                             break;
  238.  
  239.  
  240.                         }
  241.  
  242.  
  243.                     }
  244.  
  245.  
  246.                 }
  247.  
  248.                 if(10 != texto)
  249.                 {
  250.                     setarg(paramsCount, 0, -1);
  251.  
  252.                 }
  253.  
  254.                 ++paramsCount;
  255.                 continue;
  256.  
  257.  
  258.             }
  259.  
  260.             case 's', 'z':
  261.  
  262.             {
  263.                 new
  264.                 sscanfValor = -1
  265.                 ;
  266.  
  267.                 while(temporaria[++stringCount] != delim && temporaria[stringCount])
  268.                 {
  269.                     setarg(paramsCount, ++sscanfValor, temporaria[stringCount]);
  270.  
  271.                 }
  272.  
  273.                 if(sscanfValor != -1)
  274.                 {
  275.                     ++paramsCount;
  276.  
  277.                 }
  278.                 #pragma unused sscanfValor
  279.                 continue;
  280.  
  281.  
  282.             }
  283.  
  284.             case 'c':
  285.  
  286.             {
  287.                 if(!temporaria[++stringCount]) return false;
  288.                 setarg(paramsCount, 0, temporaria[stringCount]);
  289.                 continue;
  290.  
  291.  
  292.             }
  293.  
  294.             case '(':
  295.  
  296.             {
  297.                 switch(format[formatCount - 1])
  298.                 {
  299.                     case 'g':
  300.  
  301.                     {
  302.                         new
  303.                         findString[MAX_BUFFER],
  304.                         sscanfValor = -1
  305.                         ;
  306.  
  307.                         while(format[++formatCount] != ')')
  308.                         {
  309.                             findString[++sscanfValor] = format[formatCount];
  310.  
  311.                         }
  312.  
  313.                         if(-1 != sscanfValor)
  314.                         {
  315.                             sscanfValor = strfind(temporaria, findString, true);
  316.                             if(-1 != sscanfValor)
  317.                             {
  318.                                 setarg(paramsCount, 0, sscanfValor);
  319.                                 stringCount = sscanfValor+strlen(findString)+1;
  320.                                 setarg(++paramsCount, 0, stringCount);
  321.  
  322.                             }
  323.  
  324.                         }
  325.  
  326.                         ++paramsCount;
  327.                         #pragma unused sscanfValor
  328.                         continue;
  329.  
  330.  
  331.                     }
  332.  
  333.                     case 'r':
  334.  
  335.  
  336.                     {
  337.                         while(format[++formatCount] )
  338.                         {
  339.  
  340.                             switch(format[formatCount])
  341.                             {
  342.                                 case ')':
  343.  
  344.                                 {
  345.                                     break;
  346.  
  347.                                 }
  348.  
  349.                                 case 's' , 'z':
  350.  
  351.                                 {
  352.                                     new
  353.                                     sscanfValor = -1,
  354.                                     bufferString[MAX_BUFFER]                        ;
  355.  
  356.                                     while(temporaria[++stringCount] != delim && temporaria[stringCount])
  357.                                     {
  358.                                         bufferString[++sscanfValor] = temporaria[stringCount];
  359.  
  360.  
  361.                                     }
  362.  
  363.                                     new
  364.                                         i = sscanfValor
  365.                                     ;
  366.  
  367.                                     sscanfValor = -1;
  368.  
  369.                                     for( ; 0xFFFFFFFF != i; --i)
  370.                                     {
  371.                                         setarg(paramsCount, ++sscanfValor, bufferString[i]);
  372.  
  373.  
  374.                                     }
  375.  
  376.                                     if(sscanfValor != -1)
  377.                                     {
  378.                                         ++paramsCount;
  379.  
  380.                                     }
  381.                                     #pragma unused sscanfValor
  382.  
  383.                                 }
  384.  
  385.                                 case 'f':
  386.  
  387.  
  388.                                 {
  389.                                     new
  390.                                         sscanfValor = -1,
  391.                                         stringFloat[MAX_BUFFER]                     ;
  392.  
  393.                                     while(temporaria[++stringCount] != delim && temporaria[stringCount])
  394.                                     {
  395.                                         stringFloat[++sscanfValor] = temporaria[stringCount];
  396.  
  397.  
  398.                                     }
  399.  
  400.                                     new
  401.                                         i = sscanfValor
  402.                                     ;
  403.  
  404.                                     sscanfValor = -1;
  405.                                    
  406.                                     for( ; -1 != i; --i)
  407.                                     {
  408.                                         stringFloat[++sscanfValor] = stringFloat[i];
  409.  
  410.                                     }
  411.  
  412.                                     if(sscanfValor != -1)
  413.                                     {
  414.                                         setarg(++paramsCount, 0, _:floatstr(stringFloat));
  415.  
  416.                                     }
  417.                                     #pragma unused sscanfValor
  418.  
  419.  
  420.                                 }
  421.  
  422.                                 case 'd' , 'i':
  423.  
  424.  
  425.                                 {
  426.                                     new
  427.                                     sscanfValor = -1,
  428.                                     stringValue[MAX_BUFFER]                     ;
  429.  
  430.                                     while(temporaria[++stringCount] != delim && temporaria[stringCount])
  431.                                     {
  432.                                         stringValue[++sscanfValor] = temporaria[stringCount];
  433.  
  434.  
  435.                                     }
  436.  
  437.                                     new
  438.                                         i = sscanfValor
  439.                                     ;
  440.  
  441.                                     sscanfValor = -1;
  442.                                    
  443.                                     for( ; -1 != i; --i)
  444.                                     {
  445.                                         stringValue[++sscanfValor] = stringValue[i];
  446.  
  447.                                     }
  448.  
  449.                                     if(sscanfValor != -1)
  450.                                     {
  451.                                         setarg(++paramsCount, 0, strval(stringValue));
  452.  
  453.                                     }
  454.                                     #pragma unused sscanfValor
  455.  
  456.                                 }
  457.  
  458.                             }
  459.  
  460.                         }
  461.                         continue;
  462.  
  463.  
  464.                     }
  465.  
  466.                 }
  467.  
  468.  
  469.             }
  470.  
  471.             case '<':
  472.  
  473.  
  474.             {
  475.                 new
  476.                 paramEnum = -1
  477.                 ;
  478.  
  479.                 switch(format[formatCount-1])
  480.                 {
  481.                     case 'e' , 'a':
  482.  
  483.  
  484.                     {
  485.                         while(format[++formatCount])
  486.                         {
  487.                             switch(format[formatCount])
  488.                             {
  489.                                 case '>':
  490.  
  491.                                 {
  492.                                     break                                       ;
  493.  
  494.                                 }
  495.  
  496.                                 case 'i', 'd':
  497.  
  498.  
  499.                                 {
  500.  
  501.                                     new
  502.                                     numero,
  503.                                     caractere = temporaria[stringCount + 1],
  504.                                     negativo = 1                            ;
  505.  
  506.                                     if(caractere == '-')
  507.                                     {
  508.  
  509.                                         ++stringCount;
  510.                                         negativo = -1;
  511.  
  512.  
  513.                                     }
  514.  
  515.                                     while((caractere = temporaria[++stringCount]) != delim && caractere)
  516.                                     {
  517.  
  518.                                         if ('0' <= caractere <= '9')
  519.                                         {
  520.                                             numero =  (numero * 10) + (caractere - '0');
  521.  
  522.  
  523.                                         }
  524.                                         else
  525.                                         {
  526.                                             return false;
  527.  
  528.  
  529.                                         }
  530.  
  531.  
  532.                                     }
  533.                                     setarg(paramsCount, ++paramEnum, numero * negativo);
  534.                                     continue;
  535.  
  536.  
  537.                                 }
  538.  
  539.                                 case 'u':
  540.  
  541.  
  542.                                 {
  543.                                     new
  544.                                     texto,
  545.                                     tempVal = stringCount,
  546.                                     stringTemp[MAX_BUFFER],
  547.                                     caractere = temporaria[stringCount + 1] ;
  548.  
  549.                                     while((caractere = temporaria[++tempVal]) != delim && temporaria[stringCount]) if ('0' > caractere || '9' < caractere)
  550.                                     {
  551.                                         texto = 1;
  552.                                         break;
  553.  
  554.                                     }
  555.  
  556.                                     if(!texto)
  557.                                     {
  558.                                         while((caractere = temporaria[++stringCount])!= delim && caractere)
  559.                                         {
  560.                                             texto  =  (texto * 10) + (caractere - '0');
  561.  
  562.                                         }
  563.                                         texto = 10;
  564.  
  565.                                         setarg(paramsCount, ++paramEnum, texto);
  566.  
  567.  
  568.                                     }
  569.                                     else
  570.                                     {
  571.                                         texto = -1;
  572.                                         while(temporaria[++stringCount] != delim && temporaria[stringCount])
  573.                                         {
  574.                                             stringTemp[++texto] = temporaria[stringCount];
  575.  
  576.  
  577.                                         }
  578.  
  579.                                         foreach(Player, i)
  580.                                         {
  581.                                             static
  582.                                             name[MAX_PLAYER_NAME]
  583.                                             ;
  584.  
  585.                                             GetPlayerName(i, name, MAX_PLAYER_NAME);
  586.                                             if(-1 != strfind(name, stringTemp, true))
  587.                                             {
  588.                                                 setarg(paramsCount, ++paramEnum, i);
  589.                                                 texto = 10;
  590.                                                 break;
  591.  
  592.  
  593.                                             }
  594.  
  595.  
  596.                                         }
  597.  
  598.  
  599.                                     }
  600.  
  601.                                     if(10 != texto)
  602.                                     {
  603.                                         setarg(paramsCount, ++paramEnum, -1)    ;
  604.  
  605.  
  606.                                     }
  607.                                     continue;
  608.  
  609.  
  610.                                 }
  611.  
  612.                                 case 'f':
  613.  
  614.  
  615.                                 {
  616.                                     new
  617.                                     sscanfValor = -1,
  618.                                     stringFloat[15]                         ;
  619.  
  620.                                     while(temporaria[++stringCount] != delim && temporaria[stringCount])
  621.                                     {
  622.  
  623.                                         stringFloat[++sscanfValor] = temporaria[stringCount];
  624.  
  625.  
  626.                                     }
  627.  
  628.                                     if(sscanfValor != -1)
  629.                                     {
  630.                                         setarg(paramsCount, ++paramEnum, _:floatstr(stringFloat));
  631.  
  632.  
  633.                                     }
  634.                                     #pragma unused sscanfValor
  635.                                     continue;
  636.  
  637.  
  638.                                 }
  639.  
  640.                                 case 'c':
  641.  
  642.  
  643.                                 {
  644.                                     setarg(paramsCount, ++paramEnum, temporaria[++stringCount]);
  645.                                     continue;
  646.  
  647.  
  648.                                 }
  649.  
  650.  
  651.                             }
  652.  
  653.  
  654.                         }
  655.                         ++paramsCount;
  656.  
  657.  
  658.                     }
  659.  
  660.                     case 'p':
  661.  
  662.  
  663.                     {
  664.  
  665.                         delim = format[++formatCount];
  666.  
  667.                         while(format[++formatCount] != '>') continue;
  668.  
  669.  
  670.                     }
  671.  
  672.  
  673.                 }
  674.  
  675.  
  676.             }
  677.  
  678.  
  679.         }
  680.  
  681.     }
  682.     return paramsCount == totalParams;
  683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement