s3rious

s3File v1.2

Feb 28th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.96 KB | None | 0 0
  1. /*
  2.     @author:    s3rious
  3.     @name:      s3File - Easy File reading & writing
  4.     @ver:       1.2
  5.  
  6.                                                                     in 2012
  7. */
  8.  
  9. // Includes -----------------------
  10. #include < A_SAMP >
  11.  
  12. // Macros -------------------------
  13. #define MAX_FILENAME    32
  14. #define MAX_VALUES      128
  15. #define MAX_KEYS        64
  16. #define MAX_LINES       64
  17.  
  18. // News ---------------------------
  19. new
  20.     INI_g_Value[ MAX_LINES ][ MAX_VALUES ],  // Store value for each line.
  21.     INI_g_Key[ MAX_LINES ][ MAX_KEYS ],      // Store key for each line.
  22.     INI_g_FileN[ MAX_FILENAME ],             // Store filename o handled file.
  23.     File:INI_g_File,                         // Main variable for store file path.
  24.     INI_g_Line
  25.     ;
  26.  
  27. // Check if file exist
  28. stock INI_Exist(file[])
  29. {
  30.     return fexist(file);
  31. }
  32.  
  33. // Function for create a new file
  34. stock INI_Create(file[])
  35. {
  36.     new
  37.         File:FCreate = fopen(file, io_write);
  38.     if (FCreate)
  39.     {
  40.         fclose(FCreate);
  41.         return true;
  42.     }
  43.     return false;
  44. }
  45.  
  46. /*
  47. *   Important function for load file
  48. *   This function must be called as firts of all
  49. *   other function (no for INI_Create function)
  50. */
  51. stock INI_Load(file[])
  52. {
  53.     new
  54.         INI_l_String[ 168 ],
  55.         line = -1,
  56.         delim
  57.     ;
  58.  
  59.     INI_g_File = fopen(file, io_read);
  60.     format(INI_g_FileN, MAX_FILENAME, file);
  61.  
  62.     while(fread(INI_g_File, INI_l_String))
  63.     {
  64.         line++;
  65.         EscCharacter(INI_l_String);
  66.         delim = chrfind('=', INI_l_String);
  67.  
  68.         // Save key string
  69.         strmid(INI_g_Key[ line ], INI_l_String, 0, delim);
  70.  
  71.         // Save Value string
  72.         strmid(INI_g_Value[ line ], INI_l_String, delim+1, strlen(INI_l_String));
  73.  
  74.         // And increase lines value
  75.         INI_g_Line++;
  76.  
  77.     }
  78.     return fclose(INI_g_File);
  79. }
  80.  
  81. // This remove file and erase global variable values
  82. stock INI_Remove(file[])
  83. {
  84.     if (!fexist(file))  return false;
  85.     else
  86.     {
  87.         fremove(file);
  88.         EraseVars();
  89.     }
  90.     return 1;
  91. }
  92.  
  93. // Used to read for boolean variables
  94. stock bool:INI_ReadBool(key[])
  95. {
  96.     return bool:strval(INI_Read(key));
  97. }
  98.  
  99. // Used to read for float type of variable
  100. Float:INI_ReadFloat(key[])
  101. {
  102.     return floatstr(INI_Read(key));
  103. }
  104.  
  105. // Used to read integer type of variable
  106. INI_ReadInt(key[])
  107. {
  108.     return strval(INI_Read(key));
  109. }
  110.  
  111. /*
  112. *   This function is called from each function and
  113. *   type of variable is converted to respective type of called function
  114. */
  115. INI_Read(key[])
  116. {
  117.     new
  118.         line = -1
  119.     ;
  120.  
  121.     while((line < INI_g_Line) && (line < MAX_LINES))
  122.     {
  123.         line++;
  124.  
  125.         if (!strcmp(key, INI_g_Key[ line ], false))
  126.         {
  127.             return INI_g_Value[ line ];
  128.         }
  129.     }
  130.     return INI_g_Value[ 0 ];
  131. }
  132.  
  133. /*
  134. *   This write function is used for each function, which write value.
  135. *   Function is used for save string variable too.
  136. */
  137. stock INI_Write(key[], value[])
  138. {
  139.     new
  140.         bool:INI_l_Pos = false,
  141.         line = -1
  142.     ;
  143.  
  144.     while((line < INI_g_Line) && (line < MAX_LINES))
  145.     {
  146.         line++;
  147.  
  148.         if (!INI_g_Key[ line ][ 0 ]) continue;
  149.  
  150.         if (strcmp(key, INI_g_Key[ line ], false) == 0)
  151.         {
  152.             format(INI_g_Value[ line ], MAX_VALUES, value);
  153.             INI_l_Pos = true;
  154.             break;
  155.         }
  156.     }
  157.  
  158.     if (!INI_l_Pos)
  159.     {
  160.         INI_g_Line++;
  161.         format(INI_g_Key[ line ], MAX_KEYS, key);
  162.         format(INI_g_Value[ line ], MAX_VALUES, value);
  163.     }
  164. }
  165.  
  166. // Used for write boolean type of variable to file
  167. stock INI_WriteBool(key[], value)
  168. {
  169.     new
  170.         INI_l_String[ 128 ]
  171.     ;
  172.  
  173.     format(INI_l_String, MAX_FILENAME, "%d", value);
  174.     INI_Write(key, INI_l_String);
  175. }
  176.  
  177. // Used for write integer type of variable to file
  178. stock INI_WriteInt(key[], value)
  179. {
  180.     new
  181.         INI_l_String[ 128 ]
  182.     ;
  183.  
  184.     format(INI_l_String, MAX_FILENAME, "%d", value);
  185.     INI_Write(key, INI_l_String);
  186. }
  187.  
  188. // Used for write float type of variable to file
  189. stock INI_WriteFloat(key[], Float:value)
  190. {
  191.     new
  192.         INI_l_String[ 128 ]
  193.     ;
  194.  
  195.     format(INI_l_String, MAX_FILENAME, "%2.f", value);
  196.     INI_Write(key, INI_l_String);
  197. }
  198.  
  199. // Saved all changes and close handle file.
  200. stock INI_Close()
  201. {
  202.     new
  203.         INI_l_String[ 128 ],
  204.         line = -1
  205.     ;
  206.  
  207.     INI_g_File = fopen(INI_g_FileN, io_write);
  208.  
  209.     while((line < INI_g_Line) && (line < MAX_LINES))
  210.     {
  211.         line++;
  212.  
  213.         if (!strlen(INI_g_Key[ line ]) || !strlen(INI_g_Value[ line ])) continue;
  214.         format(INI_l_String, sizeof(INI_l_String), "%s=%s\r\n", INI_g_Key[ line ], INI_g_Value[ line ]);
  215.  
  216.         fwrite(INI_g_File, INI_l_String);
  217.     }
  218.     EraseVars();
  219.  
  220.     INI_g_Line = 0;
  221.     fclose(INI_g_File);
  222. }
  223.  
  224. stock EscCharacter(INI_l_StrEsc[])
  225. {
  226.     new
  227.         len = strlen(INI_l_StrEsc)
  228.     ;
  229.    
  230.     if(INI_l_StrEsc[ len-2 ] == '\r') INI_l_StrEsc[ len-2 ] = '\0';
  231.     if(INI_l_StrEsc[ len-1 ] == '\n')       INI_l_StrEsc[ len-1 ] = '\0';
  232. }
  233.  
  234. stock EraseVars()
  235. {
  236.     // Erasing variable values
  237.     for(new i = 0; i < INI_g_Line; i++)
  238.     {
  239.         INI_g_Key[ i ][ 0 ] = 0;
  240.         INI_g_Value[ i ][ 0 ] = 0;
  241.     }
  242.     return 1;
  243. }
  244.  
  245. chrfind(n, str[], pos = 0)  // Function by Y_Less
  246. {
  247.     new len = strlen(str);
  248.     while(pos < len)
  249.     {
  250.         if(str[ pos ] == n) return pos;pos++;
  251.     }
  252.     return -1;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment