Don't like ads? PRO users don't see any ads ;-)
Guest

ri_File

By: a guest on May 2nd, 2012  |  syntax: PAWN  |  size: 2.92 KB  |  hits: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  
  3.                 ri File    -    Version 1.0.0
  4.                 © 2012 by RFT
  5.  
  6. */
  7. #include <a_samp>
  8.  
  9.  
  10.  
  11. /*
  12. native ri_Create(File[]);
  13. native ri_Open(File[]);
  14. native ri_Close();
  15. native ri_Save();
  16. native ri_ReadString(Var[], str[]);
  17. native ri_ReadInt(Var[]);
  18. native ri_ReadFloat(Var[]);
  19. native ri_WriteString(Var[], str[]);
  20. native ri_WriteInt(Var[], inte);
  21. native ri_WriteFloat(Var[], Float:f);
  22. native ri_Remove(File[]);
  23. */
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. new ri_path[32];
  31. new ri_Cache[100][128];
  32. new ri_line[100];
  33. new ri_line_counter;
  34. new ri_readed=0;
  35.  
  36.  
  37. stock ri_Create(File[])
  38. {
  39.         new File:w_file=fopen(File, io_write);
  40.         fclose(w_file);
  41.         return true;
  42. }
  43.  
  44. stock ri_Remove(File[])
  45. {
  46.         fremove(File);
  47.         return true;
  48. }
  49.  
  50. stock ri_Open(File[])
  51. {
  52.     if(!fexist(File) || ri_readed==1)return 0;
  53.         new File:r_file=fopen(File, io_read);
  54.         ri_readed=1;
  55.         strmid(ri_path, File, 0, strlen(File), 64);
  56.         new c=0, load[128];
  57.         while(fread(r_file, load))
  58.         {
  59.             new len=strlen(load);
  60.             strmid(ri_Cache[c], load, 0, len-2, 128);
  61.             ri_line[c]=1;
  62.             ri_line_counter++;
  63.                 c++;
  64.         }
  65.         fclose(r_file);
  66.         return 1;
  67. }
  68.  
  69. stock ri_Close()
  70. {
  71.         for(new i = 0; i < sizeof(ri_Cache); i++)
  72.         {
  73.             if(ri_line[i]==0)continue;
  74.             ri_Cache[i]="\n";
  75.             ri_line[i]=0;
  76.             ri_line_counter=0;
  77.         }
  78.         return true;
  79. }
  80.  
  81. stock ri_Save()
  82. {
  83.         if(ri_readed==0)return 0;
  84.         new File:w_file=fopen(ri_path, io_write);
  85.         new str[128];
  86.         for(new i = 0; i < sizeof(ri_Cache); i++)
  87.         {
  88.                 if(ri_line[i]==0)continue;
  89.                 format(str, sizeof(str), "%s\r\n", ri_Cache[i]);
  90.                 fwrite(w_file, str);
  91.         }
  92.         fclose(w_file);
  93.         return true;
  94. }
  95.  
  96. stock ri_ReadString(Var[], str[])
  97. {
  98.         for(new i = 0; i < sizeof(ri_Cache); i++)
  99.         {
  100.             if(ri_line[i]==0 || ri_readed==0)continue;
  101.             if(strfind(ri_Cache[i], Var, true)!=-1)
  102.             {
  103.                     new s=strfind(ri_Cache[i], "=", true);
  104.                     if(s!=-1)
  105.                     {
  106.                         s++;
  107.                         new o=strlen(ri_Cache[i][s]);
  108.                         strmid(str, ri_Cache[i], s, s+o, 128);
  109.                         return true;
  110.                     }
  111.                 }
  112.         }
  113.         return false;
  114. }
  115.  
  116. stock ri_ReadInt(Var[])
  117. {
  118.         new str[128];
  119.         ri_ReadString(Var, str);
  120.         return strval(str);
  121. }
  122.  
  123. stock ri_ReadFloat(Var[])
  124. {
  125.         new str[128];
  126.         ri_ReadString(Var, str);
  127.         return float:floatstr(str);
  128. }
  129.  
  130. stock ri_WriteString(Var[], str[])
  131. {
  132.         for(new i = 0; i < sizeof(ri_Cache); i++)
  133.         {
  134.             if(ri_line[i]==0)continue;
  135.             if(strfind(ri_Cache[i], Var, true)!=-1)
  136.             {
  137.                     new s=strfind(ri_Cache[i], "=", true);
  138.                     s++;
  139.                     strdel(ri_Cache[i], s, 128);
  140.                     strins(ri_Cache[i], str, s, 128);
  141.                     return true;
  142.                 }
  143.         }
  144.         ri_line_counter++;
  145.         format(ri_Cache[ri_line_counter], 128, "%s=%s", Var, str);
  146.         ri_line[ri_line_counter]=1;
  147.         return true;
  148. }
  149.  
  150. stock ri_WriteInt(Var[], inte)
  151. {
  152.         new str[32];
  153.         format(str, sizeof(str), "%d", inte);
  154.         ri_WriteString(Var, str);
  155.         return true;
  156. }
  157.  
  158. stock ri_WriteFloat(Var[], Float:f)
  159. {
  160.         new str[32];
  161.         format(str, sizeof(str), "%f", f);
  162.         ri_WriteString(Var, str);
  163.         return true;
  164. }