Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- @author: s3rious
- @name: s3File - Easy File reading & writing
- @ver: 1.2
- in 2012
- */
- // Includes -----------------------
- #include < A_SAMP >
- // Macros -------------------------
- #define MAX_FILENAME 32
- #define MAX_VALUES 128
- #define MAX_KEYS 64
- #define MAX_LINES 64
- // News ---------------------------
- new
- INI_g_Value[ MAX_LINES ][ MAX_VALUES ], // Store value for each line.
- INI_g_Key[ MAX_LINES ][ MAX_KEYS ], // Store key for each line.
- INI_g_FileN[ MAX_FILENAME ], // Store filename o handled file.
- File:INI_g_File, // Main variable for store file path.
- INI_g_Line
- ;
- // Check if file exist
- stock INI_Exist(file[])
- {
- return fexist(file);
- }
- // Function for create a new file
- stock INI_Create(file[])
- {
- new
- File:FCreate = fopen(file, io_write);
- if (FCreate)
- {
- fclose(FCreate);
- return true;
- }
- return false;
- }
- /*
- * 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
- INI_l_String[ 168 ],
- line = -1,
- delim
- ;
- INI_g_File = fopen(file, io_read);
- format(INI_g_FileN, MAX_FILENAME, file);
- while(fread(INI_g_File, INI_l_String))
- {
- line++;
- EscCharacter(INI_l_String);
- delim = chrfind('=', INI_l_String);
- // Save key string
- strmid(INI_g_Key[ line ], INI_l_String, 0, delim);
- // Save Value string
- strmid(INI_g_Value[ line ], INI_l_String, delim+1, strlen(INI_l_String));
- // 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 (!fexist(file)) return false;
- else
- {
- fremove(file);
- EraseVars();
- }
- return 1;
- }
- // Used to read for boolean variables
- stock bool:INI_ReadBool(key[])
- {
- return bool:strval(INI_Read(key));
- }
- // Used to read for float type of variable
- Float:INI_ReadFloat(key[])
- {
- return floatstr(INI_Read(key));
- }
- // Used to read integer type of variable
- 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
- */
- 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[])
- {
- new
- bool:INI_l_Pos = false,
- line = -1
- ;
- 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_VALUES, value);
- INI_l_Pos = true;
- break;
- }
- }
- if (!INI_l_Pos)
- {
- INI_g_Line++;
- format(INI_g_Key[ line ], MAX_KEYS, key);
- format(INI_g_Value[ line ], MAX_VALUES, value);
- }
- }
- // Used for write boolean type of variable to file
- stock INI_WriteBool(key[], value)
- {
- new
- INI_l_String[ 128 ]
- ;
- format(INI_l_String, MAX_FILENAME, "%d", value);
- INI_Write(key, INI_l_String);
- }
- // Used for write integer type of variable to file
- stock INI_WriteInt(key[], value)
- {
- new
- INI_l_String[ 128 ]
- ;
- format(INI_l_String, MAX_FILENAME, "%d", value);
- INI_Write(key, INI_l_String);
- }
- // Used for write float type of variable to file
- stock INI_WriteFloat(key[], Float:value)
- {
- new
- INI_l_String[ 128 ]
- ;
- format(INI_l_String, MAX_FILENAME, "%2.f", value);
- INI_Write(key, INI_l_String);
- }
- // Saved all changes and close handle file.
- stock INI_Close()
- {
- new
- INI_l_String[ 128 ],
- line = -1
- ;
- INI_g_File = fopen(INI_g_FileN, io_write);
- while((line < INI_g_Line) && (line < MAX_LINES))
- {
- line++;
- if (!strlen(INI_g_Key[ line ]) || !strlen(INI_g_Value[ line ])) continue;
- format(INI_l_String, sizeof(INI_l_String), "%s=%s\r\n", INI_g_Key[ line ], INI_g_Value[ line ]);
- fwrite(INI_g_File, INI_l_String);
- }
- EraseVars();
- INI_g_Line = 0;
- fclose(INI_g_File);
- }
- stock EscCharacter(INI_l_StrEsc[])
- {
- new
- len = strlen(INI_l_StrEsc)
- ;
- if(INI_l_StrEsc[ len-2 ] == '\r') INI_l_StrEsc[ len-2 ] = '\0';
- if(INI_l_StrEsc[ len-1 ] == '\n') INI_l_StrEsc[ len-1 ] = '\0';
- }
- 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;
- }
- chrfind(n, str[], pos = 0) // Function by Y_Less
- {
- new len = strlen(str);
- while(pos < len)
- {
- if(str[ pos ] == n) return pos;pos++;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment