Lenin_Daniel

getparams

Nov 27th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.36 KB | None | 0 0
  1. #if defined GetParams_Include_V_1
  2. ****************************************************************************
  3. *                    GetParams Include version 2.0                         *
  4. *       Copyright © 2011-2013 "Lenin Ruiz" (Daniel-92) /(=Maxell=)         *
  5. *             This file is provided as is (no warranties).                 *
  6. ****************************************************************************
  7.  
  8. -FAKE NATIVES ------------------------------------------------------------------
  9. native GetParamsStr(const string[],space, dest[], bool:end_string = false, demiliter = ' ', length = sizeof(dest),bool:useindex=false);
  10. native GetParams(const string[], space, bool:end_string = false, demiliter = ' ',bool:use_index);
  11. native GetParamsInt(const string[], space, &dest, demiliter = ' ',bool:use_index);
  12. native GetParamsFloat(const string[], space, &Float:dest, demiliter = ' ',bool:use_index);
  13. native GetParamsIndex(const string[], space, demiliter = ' ');
  14. native CountParams(const string[],demiliter=' ');
  15. native IsNumeric(const string[]);
  16. --------------------------------------------------------------------------------
  17. #endif
  18.    
  19. #if defined _getparams_included
  20.     #endinput
  21. #endif
  22. #if !defined _samp_included
  23.     #include a_samp
  24. #endif
  25. #define _getparams_included
  26. #if !defined MAX_RESULT_LEN
  27.     #define  MAX_RESULT_LEN  128 //Max len of result in GetParams, GetParamsInt and GetParamsFloat
  28. #endif
  29. #if !defined isnull
  30.     #define isnull(%1) \
  31.         ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  32. #endif
  33.  
  34. #pragma tabsize 4
  35.  
  36. new gp_data_result[MAX_RESULT_LEN];
  37.  
  38. //--------------------------------------------------------------------------
  39. stock GetParams(const string[], space, bool:end_string = false, demiliter = ' ',bool:use_index=false) {
  40.     GetParamsStr(string,space,gp_data_result,end_string,demiliter,MAX_RESULT_LEN,use_index);
  41.     return gp_data_result;
  42. }
  43. //------------------------------------------------------------------------------
  44. stock GetParamsInt(const params[], space, &value, demiliter = ' ',bool:use_index=false) {
  45.     if(GetParamsStr(params,space,gp_data_result,false,demiliter,_,use_index)) {
  46.         if(IsNumeric(gp_data_result)) {
  47.             value = strval(gp_data_result);
  48.             return true;
  49.         }
  50.     }
  51.     return false;
  52. }
  53. //------------------------------------------------------------------------------
  54. stock GetParamsFloat(const params[], space, &Float:value, demiliter = ' ',bool:use_index=false) {
  55.     if(GetParamsStr(params,space,gp_data_result,false,demiliter,_,use_index)) {
  56.         if(IsNumeric(gp_data_result)) {
  57.             value = floatstr(gp_data_result);
  58.             return true;
  59.         }
  60.     }
  61.     return false;
  62. }
  63. //------------------------------------------------------------------------------
  64. stock CountParams(const string[], demiliter = ' ') {
  65.     new
  66.         len     = strlen(string),
  67.         spaces  = 1;
  68.        
  69.     if(len == 0) {
  70.         return 0;
  71.     }
  72.     for(new i=0; i < len; i++) if(string[i] == demiliter) {
  73.         for(new s_c=i+1;s_c<len;s_c++,i++) {
  74.             if(string[s_c]!=' ') {
  75.                 break;
  76.             }
  77.         }
  78.         spaces++;
  79.     }
  80.     return spaces;
  81. }
  82. //------------------------------------------------------------------------------
  83. stock GetParamsIndex(const string[], space, demiliter = ' ') {
  84.     new
  85.         len = strlen(string),
  86.         spaces;
  87.  
  88.     if(len == 0) return -1;
  89.     for(new i=0; i < len; i++) if(string[i] == demiliter) {
  90.         for(new s_c=i+1;s_c<len;s_c++,i++)if(string[s_c]!=' ') {
  91.             break;
  92.         }
  93.         spaces++;
  94.         if(spaces >= space) return i;
  95.     }
  96.     return -1;
  97. }
  98.  
  99. //------------------------------------------------------------------------------
  100. stock IsNumeric(const string[]) {
  101.     new
  102.         len = strlen(string),
  103.         bool:d_f = false;
  104.  
  105.     if(len == 0) return false;
  106.     for(new i=0; i < len; i++) {
  107.         switch(string[i]) {
  108.             case '0'..'9': continue;
  109.             case '-','+' : if(i) return false;
  110.             case '.': if(d_f || i == (len-1)) return false; else d_f = true;
  111.             default: return false;
  112.         }
  113.     }
  114.     return true;
  115. }
  116. //------------------------------------------------------------------------------
  117. #undef MAX_RESULT_LEN
  118.  
  119. //------------------------------------------------------------------------------
  120. #if defined GET_PARAMS_USE_PLUGIN
  121.     #pragma library params
  122.     native GetParamsStr(const string[],space,dest[],bool:end_string=false,demiliter = ' ',length=sizeof dest,bool:use_index=false);
  123.     native GetParamsUseIndex(toggle=true);
  124.     #endinput
  125. #endif
  126.  
  127. new
  128.     bool:get_params_use_index   = false,
  129.     get_params_last_index       = 0,
  130.     get_params_last_space       = 0;
  131.  
  132. stock GetParamsStr(const string[],space,dest[],bool:end_string=false,demiliter = ' ',length=sizeof dest,bool:use_index=false) {
  133.     if(space<=0) {
  134.         printf("** GetParams: Se esperaba obtener un parámetro mayor a 0 pero se encontró un %d",space);
  135.         return 0;
  136.     }
  137.     if(!string[0] || string[0]=='\1' || length<=0) {
  138.         return 0;
  139.     }
  140.     static
  141.         len,    space_,
  142.         i,      s_c,
  143.         l_char, l_space,
  144.         index;
  145.        
  146.     if(use_index || get_params_use_index) {
  147.         if(space > get_params_last_space) {
  148.             space_=get_params_last_space;
  149.             i=get_params_last_index;
  150.         }
  151.         else {
  152.             space_=i=0;
  153.             //len = strlen(string);
  154.         }
  155.     }
  156.     else {
  157.         space_=i=0,
  158.         len = strlen(string);
  159.     }
  160.     for(;i<len;i++) if(string[i]!=' ') {
  161.         break;
  162.     }
  163.     l_space     = i;
  164.     get_params_last_space = space;
  165.     for(;i<len;i++) {
  166.         if(string[i]==demiliter) {
  167.             l_char=i;
  168.             for(s_c=i+1;s_c<len;s_c++,i++)if(string[s_c]!=' ') {
  169.                  break;
  170.             }
  171.             space_++;
  172.             if(space_>=space) {
  173.                 s_c=0, get_params_last_index = i+1;
  174.                 if(!end_string){
  175.                     if(length<(l_char-l_space)) l_char=(l_space+length)-1;
  176.                     for(index=l_space;index<l_char;dest[s_c++]=string[index++]) {
  177.                         //continue
  178.                     }
  179.                     dest[s_c]='\0';
  180.                     return l_space+1;
  181.                 }
  182.                 if(length<(len-l_space)) len =(l_space+length)-1;
  183.                 for(index=l_space;index<len;dest[s_c++]=string[index++]) {
  184.                     //continue
  185.                 }
  186.                 dest[s_c]='\0';
  187.                 return l_space+1;
  188.             }
  189.             l_space=i+1;
  190.         }
  191.     }
  192.     if(space_==space-1 && len>l_space && string[l_space]!=demiliter) {
  193.         s_c=0, get_params_last_index = i+1;
  194.         if(length<(len-l_space))len=(l_space+length)-1;
  195.         for(index=l_space;index<len; dest[s_c++]=string[index++]){
  196.             //continue;
  197.         }
  198.         dest[s_c]='\0';
  199.         return l_space+1;
  200.     }
  201.     return 0;
  202. }
  203.  
  204. //--------------------------------------------------------------------------
  205. stock GetParamsUseIndex(bool:toggle = true) {
  206.     get_params_use_index  = toggle;
  207.     get_params_last_index = 0;
  208.     get_params_last_space = 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment