Advertisement
garfield

[COD]: INI Set e INI Get para SA-MP.

Oct 27th, 2012
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.05 KB | None | 0 0
  1.  
  2. /*
  3.     Manipulação de arquivos INI
  4.     Créditos: SuYaNw(Garfield).
  5.     Exemplo de script:
  6. */
  7.  
  8. public OnFilterScriptInit(){
  9.     s_Set("Stress.ini", "Score",    "200");
  10.     s_Set("Stress.ini", "Colete",   "5867");
  11.     s_Set("Stress.ini", "Armour",   "1234");
  12.    
  13.     printf(s_Get("Stress.ini", "Colete"));
  14.     printf(s_Get("Stress.ini", "Score"));
  15.     printf(s_Get("Stress.ini", "Armour"));
  16.     return true;
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. stock s_Set(filename[], line[], value[]){
  25.  
  26.     if(!fexist(filename) || strfind(filename, ".") == 0 ){
  27.         return false;
  28.     }
  29.  
  30.     #if !defined MAX_STRING
  31.         #define MAX_STRING      (128)
  32.     #endif
  33.    
  34.     static
  35.         File:Arquivo,
  36.         Linha[MAX_STRING],
  37.         Buf[MAX_STRING]
  38.     ;
  39.  
  40.     Linha[0]     = '\0';
  41.     Buf[0]       = '\0';
  42.    
  43.     Arquivo = fopen(filename, io_read);
  44.    
  45.    
  46.     for( ; fread(Arquivo, Linha); ){
  47.         if(strfind(Linha, line) != -1){
  48.             format(Linha, MAX_STRING, "%s=%s\r\n", line, value);
  49.         }
  50.            
  51.  
  52.         strcat(Buf, Linha);
  53.     }
  54.  
  55.     fclose(Arquivo);
  56.     fremove(filename);
  57.    
  58.     Arquivo = fopen(filename, io_write);
  59.  
  60.  
  61.     Linha[0] = '\0';
  62.     if(strfind(Buf, line) == -1){
  63.         format(Linha, MAX_STRING, "%s=%s\r\n", line, value);
  64.         strcat(Buf, Linha);
  65.     }
  66.     fwrite(Arquivo, Buf);
  67.     fclose(Arquivo);
  68.    
  69.     return true;
  70. }
  71.  
  72.  
  73. stock s_Get(filename[], line[]){
  74.  
  75.     #if !defined MAX_STRING
  76.         #define MAX_STRING      (128)
  77.     #endif
  78.  
  79.    
  80.     static
  81.         Buffer[MAX_STRING]
  82.     ;
  83.  
  84.  
  85.     Buffer[0] = '\0';
  86.  
  87.  
  88.    
  89.     static
  90.         Linha[MAX_STRING],
  91.         //Tamanho_fLinha,
  92.         Tamanho_sLinha,
  93.         File:Arquivo
  94.     ;
  95.        
  96.        
  97.     Linha[0] = '\0';
  98.        
  99.     Arquivo = fopen(filename, io_read);
  100.        
  101.     for( ; fread(Arquivo, Linha); ){
  102.        
  103.        
  104.         if(strfind(Linha, line) != -1){
  105.            
  106.             Tamanho_sLinha      = strlen(line);
  107.            
  108.             StripNewLine(Linha);
  109.                
  110.             strdel(Linha, 0, Tamanho_sLinha + 1);
  111.  
  112.             Buffer = Linha;
  113.         }
  114.    
  115.     }
  116.     return Buffer;
  117. }
  118.  
  119.  
  120. stock StripNewLine(str[]){
  121.  
  122.     if(strfind(str, "\n") != -1) {
  123.    
  124.         static
  125.             Tamanho_fLinha
  126.         ;
  127.        
  128.         Tamanho_fLinha = strlen(str);
  129.         strdel(str, Tamanho_fLinha -2, Tamanho_fLinha);
  130.     }
  131.     return true;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement