s3rious

s3File v1.1a

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