s3rious

s3File v1.4

Mar 4th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.99 KB | None | 0 0
  1. /*
  2.     @author:    s3rious
  3.     @name:      sFile - Easy File reading & writing
  4.     @ver:       1.4
  5.  
  6.                                                                     in 2012
  7. */
  8.  
  9. // Includes -----------------------
  10. #include < A_SAMP >
  11.  
  12. // Macros -------------------------
  13.  
  14. // The maximum lenght of a line
  15. #define MAX_LINE_SIZE   (128)
  16.  
  17. #define MAX_VALUE_SIZE  (32)
  18.  
  19. #define MAX_KEY_SIZE    (32)
  20.  
  21. #define MAX_FILE_SIZE   (64)
  22.  
  23. #define MAX_LINES       (100)
  24.  
  25. // News ---------------------------
  26. new
  27.     INI_g_Value[ MAX_LINES ][ MAX_VALUE_SIZE ],     // Store value for each line.
  28.     INI_g_Key[ MAX_LINES ][ MAX_KEY_SIZE ],         // Store key for each line.
  29.     INI_g_FileN[ MAX_FILE_SIZE ],                   // Store filename o handled file.
  30.     File:INI_g_File,                                // Main variable for store file path.
  31.     INI_g_Line
  32.     ;
  33.    
  34. // Used to change mainfile to choosen
  35. stock INI_SetFile(targetfile[])
  36.     return strcpy(INI_g_FileN, targetfile);
  37.    
  38. // Return mainfile
  39. stock INI_GetFile()
  40.     return INI_g_FileN;
  41.  
  42. // Check if file exist
  43. stock INI_Exist(file[])
  44.     return fexist(file);
  45.  
  46. // Function for create a new file
  47. stock INI_Create(file[])
  48. {
  49.     if (!INI_Exist(file))
  50.     {
  51.         new File: f = fopen(file, io_append);
  52.  
  53.         if (fclose(f))
  54.         {
  55.             return 1;
  56.         }
  57.     }
  58.     return 0;
  59. }
  60.  
  61. /*
  62. *   Important function for load file
  63. *   This function must be called as firts of all
  64. *   other function (no for INI_Create function)
  65. */
  66.  
  67. stock INI_Load(file[])
  68. {
  69.     new
  70.         buf[ MAX_LINE_SIZE ],
  71.         line = -1,
  72.         pos
  73.     ;
  74.  
  75.     INI_g_File = fopen(file, io_read);
  76.     format(INI_g_FileN, MAX_FILE_SIZE, file);
  77.  
  78.     while(fread(INI_g_File, buf))
  79.     {
  80.         line++;
  81.         if(buf[ strlen(buf)-2 ] == '\r')
  82.             buf[ strlen(buf)-2 ] = EOS;
  83.  
  84.         if(buf[ strlen(buf)-1 ] == '\n')
  85.             buf[ strlen(buf)-1 ] = EOS;
  86.            
  87.         pos = charfind(buf, '=');
  88.        
  89.         new
  90.             start = 0,
  91.             key[ MAX_KEY_SIZE ],
  92.             value[ MAX_VALUE_SIZE ],
  93.             keylen, valuelen
  94.         ;
  95.             // Save key string
  96.         for (; start < pos; start++)
  97.         {
  98.             // pasting char
  99.             key [ start ] = buf[ start ];
  100.             if (!strcmp(buf[ start ], buf[ pos ], false))
  101.             keylen = buf[ start ];
  102.         }
  103.  
  104.         start = keylen + 1;
  105.  
  106.         for (; start < pos; start++)
  107.         {
  108.             value[ start ] = buf[ start ];
  109.             if (!strcmp(buf[ start ], buf[ pos ], false))
  110.                 valuelen = buf[ start ] - keylen;
  111.         }
  112.  
  113.         format(INI_g_Key[ line ], sizeof(INI_g_Key), key);
  114.         format(INI_g_Value[ line ], sizeof(INI_g_Value), value);
  115.  
  116.         // And increase lines value
  117.         INI_g_Line++;
  118.     }
  119.     return fclose(INI_g_File);
  120. }
  121.  
  122. // This remove file and erase global variable values
  123. stock INI_Remove(file[])
  124. {
  125.     if (file[ 0 ])
  126.     {
  127.         if (INI_g_FileN[ 0 ] && !strcmp(INI_g_FileN, file))
  128.             INI_g_FileN = EOS;
  129.            
  130.         return fremove(file);
  131.     }
  132.     return 0;
  133. }
  134.  
  135. // Used to read for boolean variables
  136. stock bool:INI_ReadBool(key[])
  137. {
  138.     new buf[ 16 ];
  139.     format(buf, sizeof(buf), "%s", INI_Read(key));
  140.     return (strval(buf) || (buf[ 0 ] && !strcmp(buf, "true", true)));
  141. }
  142.  
  143. // Used to read Hex strings
  144. stock INI_ReadHex(key[])
  145. {
  146.     new buf[ 16 ];
  147.     format(buf, sizeof(buf), "%d", s3_Hash(key));
  148.     return s3_StrToHex(buf);
  149. }
  150.  
  151. // Used to read for float type of variable
  152. stock Float:INI_ReadFloat(key[])
  153.     return floatstr(INI_Read(key));
  154.  
  155. // Used to read integer type of variable
  156. stock INI_ReadInt(key[])
  157.     return strval(INI_Read(key));
  158.  
  159. /*
  160. *   This function is called from each function and
  161. *   type of variable is converted to respective type of called function
  162. */
  163. stock INI_Read(key[])
  164. {
  165.     new
  166.         line = -1
  167.     ;
  168.  
  169.     while((line < INI_g_Line) && (line < MAX_LINES))
  170.     {
  171.         line++;
  172.  
  173.         if (!strcmp(key, INI_g_Key[ line ], false))
  174.         {
  175.             return INI_g_Value[ line ];
  176.         }
  177.     }
  178.     return INI_g_Value[ 0 ];
  179. }
  180.  
  181. /*
  182. *   This write function is used for each function, which write value.
  183. *   Function is used for save string variable too.
  184. */
  185. stock INI_Write(key[], value[])
  186. {
  187.     if (key[ 0 ] && value[ 0 ])
  188.     {
  189.         new
  190.             bool:INI_l_Pos = false,
  191.             line = -1
  192.         ;
  193.  
  194.         if (!INI_g_FileN[ 0 ])
  195.             return 0;
  196.  
  197.         while((line < INI_g_Line) && (line < MAX_LINES))
  198.         {
  199.             line++;
  200.            
  201.             if (!INI_g_Key[ line ][ 0 ]) continue;
  202.            
  203.             if (strcmp(key, INI_g_Key[ line ], false) == 0)
  204.             {
  205.                 format(INI_g_Value[ line ], MAX_VALUE_SIZE, value[ 0 ] ? value : ("null"));
  206.                 INI_l_Pos = true;
  207.                 break;
  208.             }
  209.         }
  210.  
  211.         if (!INI_l_Pos)
  212.         {
  213.             INI_g_Line++;
  214.             format(INI_g_Key[ line ], MAX_KEY_SIZE, key);
  215.             format(INI_g_Value[ line ], MAX_VALUE_SIZE, value);
  216.         }
  217.     }
  218.     return 1;
  219. }
  220.  
  221. // Used for write boolean type of variable to file
  222. stock INI_WriteBool(key[], bool:value)
  223.     return INI_Write(key, value ? ("true") : ("false"));
  224.  
  225. // Used for write integer type of variable to file
  226. stock INI_WriteInt(key[], value)
  227. {
  228.     new buf[ 16 ];
  229.     format(buf, MAX_FILE_SIZE, "%d", value);
  230.     return INI_Write(key, buf);
  231. }
  232.  
  233. stock INI_WriteHex(key[], value)
  234. {
  235.     new buf[ 16 ];
  236.     s3_HexToStr(value, buf);
  237.     return INI_Write(key, buf);
  238. }
  239.  
  240. // Used for write float type of variable to file
  241. stock INI_WriteFloat(key[], Float:value)
  242. {
  243.     new buf[ 16 ];
  244.     format(buf, MAX_FILE_SIZE, "%3.f", value);
  245.     return INI_Write(key, buf);
  246. }
  247.  
  248. // Saved all changes and close handle file.
  249. stock INI_Close()
  250. {
  251.     new
  252.         INI_l_Line[ MAX_LINE_SIZE ],
  253.         line = -1
  254.     ;
  255.  
  256.     INI_g_File = fopen(INI_g_FileN, io_write);
  257.  
  258.     if (INI_g_File)
  259.     {
  260.         while((line < INI_g_Line) && (line < MAX_LINES))
  261.         {
  262.             line++;
  263.  
  264.             if (!strlen(INI_g_Key[ line ]) || !strlen(INI_g_Value[ line ])) continue;
  265.             format(INI_l_Line, sizeof(INI_l_Line), "%s=%s\r\n", INI_g_Key[ line ], INI_g_Value[ line ]);
  266.  
  267.             fwrite(INI_g_File, INI_l_Line);
  268.         }
  269.         EraseVars();
  270.         INI_g_Line = 0;
  271.     }
  272.     fclose(INI_g_File);
  273. }
  274.  
  275. stock EraseVars()
  276. {
  277.     // Erasing variable values
  278.     for(new i = 0; i < INI_g_Line; i++)
  279.     {
  280.         INI_g_Key[ i ][ 0 ] = 0;
  281.         INI_g_Value[ i ][ 0 ] = 0;
  282.     }
  283.     return 1;
  284. }
  285.  
  286. stock s3_HexToStr(value, key[], size = sizeof(key))
  287. {
  288.     static const chars [] =
  289.     {
  290.         '0', '1', '2', '3',
  291.         '4', '5', '6', '7',
  292.         '8', '9', 'A', 'B',
  293.         'C', 'D', 'E', 'F'
  294.     };
  295.  
  296.     new
  297.         buf [8 + 3] = "0x"
  298.     ;
  299.  
  300.     for (new i = 0; i < 8; ++i){
  301.         buf [2 + i] = chars [(value >>> ((7 - i) << 2)) & 0x0F];
  302.     }
  303.    
  304.     key[ 0 ] = EOS;
  305.     strcat(key, buf, size);
  306. }
  307.  
  308. stock s3_StrToHex(key[])
  309. {
  310.     new
  311.         i,
  312.         value
  313.     ;
  314.  
  315.     if (key [0] == '0' && (key [1] == 'x' || key [1] == 'X'))
  316.         i = 2;
  317.  
  318.     while (key[i])
  319.     {
  320.         value <<= 4;
  321.         switch (key [i])
  322.         {
  323.             case '0' .. '9':
  324.                 value |= key [i] - '0';
  325.  
  326.             case 'A' .. 'F':
  327.                 value |= key [i] - 'A' + 10;
  328.  
  329.             case 'a' .. 'f':
  330.                 value |= key [i] - 'a' + 10;
  331.  
  332.             default:
  333.                 return 0;
  334.         }
  335.         ++i;
  336.     }
  337.     return value;
  338. }
  339.  
  340. static stock charfind (string [], c)
  341. {
  342.     for (new i, len = strlen (string); i < len; ++i)
  343.         if (string [i] == c)
  344.             return i;
  345.     return -1;
  346. }
  347.  
  348. static stock s3_Hash(key[])
  349. {
  350.     new h = -1, i, j;
  351.  
  352.     while ((j = key [i++]))
  353.         h = h * 33 + j;
  354.  
  355.     return h;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment