Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- @author: s3rious
- @name: sFile - Easy File reading & writing
- @ver: 1.4
- in 2012
- */
- // Includes -----------------------
- #include < A_SAMP >
- // Macros -------------------------
- // The maximum lenght of a line
- #define MAX_LINE_SIZE (128)
- #define MAX_VALUE_SIZE (32)
- #define MAX_KEY_SIZE (32)
- #define MAX_FILE_SIZE (64)
- #define MAX_LINES (100)
- // News ---------------------------
- new
- INI_g_Value[ MAX_LINES ][ MAX_VALUE_SIZE ], // Store value for each line.
- INI_g_Key[ MAX_LINES ][ MAX_KEY_SIZE ], // Store key for each line.
- INI_g_FileN[ MAX_FILE_SIZE ], // Store filename o handled file.
- File:INI_g_File, // Main variable for store file path.
- INI_g_Line
- ;
- // Used to change mainfile to choosen
- stock INI_SetFile(targetfile[])
- return strcpy(INI_g_FileN, targetfile);
- // Return mainfile
- stock INI_GetFile()
- return INI_g_FileN;
- // Check if file exist
- stock INI_Exist(file[])
- return fexist(file);
- // Function for create a new file
- stock INI_Create(file[])
- {
- if (!INI_Exist(file))
- {
- new File: f = fopen(file, io_append);
- if (fclose(f))
- {
- return 1;
- }
- }
- return 0;
- }
- /*
- * Important function for load file
- * This function must be called as firts of all
- * other function (no for INI_Create function)
- */
- stock INI_Load(file[])
- {
- new
- buf[ MAX_LINE_SIZE ],
- line = -1,
- pos
- ;
- INI_g_File = fopen(file, io_read);
- format(INI_g_FileN, MAX_FILE_SIZE, file);
- while(fread(INI_g_File, buf))
- {
- line++;
- if(buf[ strlen(buf)-2 ] == '\r')
- buf[ strlen(buf)-2 ] = EOS;
- if(buf[ strlen(buf)-1 ] == '\n')
- buf[ strlen(buf)-1 ] = EOS;
- pos = charfind(buf, '=');
- new
- start = 0,
- key[ MAX_KEY_SIZE ],
- value[ MAX_VALUE_SIZE ],
- keylen, valuelen
- ;
- // Save key string
- for (; start < pos; start++)
- {
- // pasting char
- key [ start ] = buf[ start ];
- if (!strcmp(buf[ start ], buf[ pos ], false))
- keylen = buf[ start ];
- }
- start = keylen + 1;
- for (; start < pos; start++)
- {
- value[ start ] = buf[ start ];
- if (!strcmp(buf[ start ], buf[ pos ], false))
- valuelen = buf[ start ] - keylen;
- }
- format(INI_g_Key[ line ], sizeof(INI_g_Key), key);
- format(INI_g_Value[ line ], sizeof(INI_g_Value), value);
- // And increase lines value
- INI_g_Line++;
- }
- return fclose(INI_g_File);
- }
- // This remove file and erase global variable values
- stock INI_Remove(file[])
- {
- if (file[ 0 ])
- {
- if (INI_g_FileN[ 0 ] && !strcmp(INI_g_FileN, file))
- INI_g_FileN = EOS;
- return fremove(file);
- }
- return 0;
- }
- // Used to read for boolean variables
- stock bool:INI_ReadBool(key[])
- {
- new buf[ 16 ];
- format(buf, sizeof(buf), "%s", INI_Read(key));
- return (strval(buf) || (buf[ 0 ] && !strcmp(buf, "true", true)));
- }
- // Used to read Hex strings
- stock INI_ReadHex(key[])
- {
- new buf[ 16 ];
- format(buf, sizeof(buf), "%d", s3_Hash(key));
- return s3_StrToHex(buf);
- }
- // Used to read for float type of variable
- stock Float:INI_ReadFloat(key[])
- return floatstr(INI_Read(key));
- // Used to read integer type of variable
- stock INI_ReadInt(key[])
- return strval(INI_Read(key));
- /*
- * This function is called from each function and
- * type of variable is converted to respective type of called function
- */
- stock INI_Read(key[])
- {
- new
- line = -1
- ;
- while((line < INI_g_Line) && (line < MAX_LINES))
- {
- line++;
- if (!strcmp(key, INI_g_Key[ line ], false))
- {
- return INI_g_Value[ line ];
- }
- }
- return INI_g_Value[ 0 ];
- }
- /*
- * This write function is used for each function, which write value.
- * Function is used for save string variable too.
- */
- stock INI_Write(key[], value[])
- {
- if (key[ 0 ] && value[ 0 ])
- {
- new
- bool:INI_l_Pos = false,
- line = -1
- ;
- if (!INI_g_FileN[ 0 ])
- return 0;
- while((line < INI_g_Line) && (line < MAX_LINES))
- {
- line++;
- if (!INI_g_Key[ line ][ 0 ]) continue;
- if (strcmp(key, INI_g_Key[ line ], false) == 0)
- {
- format(INI_g_Value[ line ], MAX_VALUE_SIZE, value[ 0 ] ? value : ("null"));
- INI_l_Pos = true;
- break;
- }
- }
- if (!INI_l_Pos)
- {
- INI_g_Line++;
- format(INI_g_Key[ line ], MAX_KEY_SIZE, key);
- format(INI_g_Value[ line ], MAX_VALUE_SIZE, value);
- }
- }
- return 1;
- }
- // Used for write boolean type of variable to file
- stock INI_WriteBool(key[], bool:value)
- return INI_Write(key, value ? ("true") : ("false"));
- // Used for write integer type of variable to file
- stock INI_WriteInt(key[], value)
- {
- new buf[ 16 ];
- format(buf, MAX_FILE_SIZE, "%d", value);
- return INI_Write(key, buf);
- }
- stock INI_WriteHex(key[], value)
- {
- new buf[ 16 ];
- s3_HexToStr(value, buf);
- return INI_Write(key, buf);
- }
- // Used for write float type of variable to file
- stock INI_WriteFloat(key[], Float:value)
- {
- new buf[ 16 ];
- format(buf, MAX_FILE_SIZE, "%3.f", value);
- return INI_Write(key, buf);
- }
- // Saved all changes and close handle file.
- stock INI_Close()
- {
- new
- INI_l_Line[ MAX_LINE_SIZE ],
- line = -1
- ;
- INI_g_File = fopen(INI_g_FileN, io_write);
- if (INI_g_File)
- {
- while((line < INI_g_Line) && (line < MAX_LINES))
- {
- line++;
- if (!strlen(INI_g_Key[ line ]) || !strlen(INI_g_Value[ line ])) continue;
- format(INI_l_Line, sizeof(INI_l_Line), "%s=%s\r\n", INI_g_Key[ line ], INI_g_Value[ line ]);
- fwrite(INI_g_File, INI_l_Line);
- }
- EraseVars();
- INI_g_Line = 0;
- }
- fclose(INI_g_File);
- }
- stock EraseVars()
- {
- // Erasing variable values
- for(new i = 0; i < INI_g_Line; i++)
- {
- INI_g_Key[ i ][ 0 ] = 0;
- INI_g_Value[ i ][ 0 ] = 0;
- }
- return 1;
- }
- stock s3_HexToStr(value, key[], size = sizeof(key))
- {
- static const chars [] =
- {
- '0', '1', '2', '3',
- '4', '5', '6', '7',
- '8', '9', 'A', 'B',
- 'C', 'D', 'E', 'F'
- };
- new
- buf [8 + 3] = "0x"
- ;
- for (new i = 0; i < 8; ++i){
- buf [2 + i] = chars [(value >>> ((7 - i) << 2)) & 0x0F];
- }
- key[ 0 ] = EOS;
- strcat(key, buf, size);
- }
- stock s3_StrToHex(key[])
- {
- new
- i,
- value
- ;
- if (key [0] == '0' && (key [1] == 'x' || key [1] == 'X'))
- i = 2;
- while (key[i])
- {
- value <<= 4;
- switch (key [i])
- {
- case '0' .. '9':
- value |= key [i] - '0';
- case 'A' .. 'F':
- value |= key [i] - 'A' + 10;
- case 'a' .. 'f':
- value |= key [i] - 'a' + 10;
- default:
- return 0;
- }
- ++i;
- }
- return value;
- }
- static stock charfind (string [], c)
- {
- for (new i, len = strlen (string); i < len; ++i)
- if (string [i] == c)
- return i;
- return -1;
- }
- static stock s3_Hash(key[])
- {
- new h = -1, i, j;
- while ((j = key [i++]))
- h = h * 33 + j;
- return h;
- }
Advertisement
Add Comment
Please, Sign In to add comment