s3rious

s3File v1.5b

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