Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Hirsw0w File Save.
- * Author : Hirsw0w
- * Version : 1.0
- *
- * Explain:
- * I Have created a new file save, fast respone.
- * all the stocks helping you with the save
- *
- * Defines:
- *
- * MAX_VALUE_STRING is the max chars in the value. (default: 50 )
- * MAX_DATA_STRING is the max chars in the data. (default: 50 )
- * MAX_FILE_NAME is the max chars in the file name. (default: 100 )
- * MAX_DATA is the max data the include can contain. (default: 75 )
- *
- * Macors:
- *
- * HFile_Create(filename) that a macro for the functions fclose and fopen. ( thats create a new file )
- * HFile_Exists(filename) that a macro for the function file_exists. ( thats check if file exists )
- * HFile_Delete(filename) that a macro for the function delete_file. ( thats delete a file )
- *
- * Stocks:
- *
- * HFile_Copy(file1,file2) - Copy file for other location
- * HFile_Rename(file1,file2) - change file name for another
- * HFile_Open(filename) - Open a file ( need to write and read )
- * HFile_Close(filename) - Close a file after usage ( needed for save )
- * HFile_WriteString(Data[],Value[]) - Write string save on the file.
- * HFile_WriteInt(Data[],Value) - Write a Int( Number ) save on the file.
- * HFile_WriteFloat(Data[],Float:Value) - Write a Float save on the file.
- * HFile_WriteBool(Data[],bool:Value) - Write a boolean save on the file.
- * HFile_ReadString(Data[]) - Read a string from the file by the data.
- * HFile_ReadInt(Data[]) - Read a Int( Number ) from the file by the data.
- * HFile_ReadFloat(Data[]) - Read a Float from the file by the data.
- * HFile_ReadBool(Data[]) - Read a Boolean from the file by the data.
- * HFile_UnSet(Data[]) - Remove a data.
- * HFile_IsSet(Data[]) - Check if Data is exists
- * HFile_GetDataName(Value[]) - Get a data name by the value
- * HFIle_GetFileSize(filename[]) - Get the file size.
- *
- * Bugs: don't found yet.
- *
- * Enjoy!
- */
- #if defined _HFile_included
- #endinput
- #endif
- #define _HFile_included
- #define MAX_VALUE_STRING 50
- #define MAX_DATA_STRING 50
- #define MAX_FILE_NAME 100
- #define MAX_DATA 75
- #define HFile_Create(%0) fclose(fopen(%0, "wt"))
- #define HFile_Exists(%0) file_exists(%0)
- #define HFile_Delete(%0) delete_file(%0)
- new Data[MAX_DATA][MAX_DATA_STRING],Value[MAX_DATA][MAX_VALUE_STRING],File[MAX_FILE_NAME];
- new bool:FileOpen;
- stock HFile_Copy(file1[],file2[]) {
- if(!HFile_Exists(file1)) return 0;
- if(HFile_Exists(file2)) HFile_Delete(file2);
- new HFile1 = fopen(file1,"rt");
- new tmpres[128];
- while(!feof(HFile1))
- {
- fgets(HFile1, tmpres, sizeof tmpres - 1);
- replace_all(tmpres,128,"^n","");
- replace_all(tmpres,128,"^r","");
- write_file(file2,tmpres);
- }
- fclose(HFile1);
- return 1;
- }
- stock HFile_Rename(file1[],file2[]) {
- HFile_Copy(file1,file2);
- HFile_Delete(file1);
- return 1;
- }
- stock HFile_Open(filename[]) {
- if(!file_exists(filename)) {
- HFile_Create(filename);
- return 1;
- }
- new tmpres[128];
- new HFile = fopen(filename, "rt");
- new i;
- while(!feof(HFile))
- {
- fgets(HFile, tmpres, sizeof tmpres - 1);
- if(tmpres[0] == '/' && tmpres[1] == '/' || strlen(tmpres) < 3)
- continue;
- i++;
- strtok( tmpres,Data[i],MAX_DATA_STRING,Value[i],MAX_VALUE_STRING, '=');
- }
- FileOpen = true;
- formatex(File,MAX_FILE_NAME,filename);
- fclose(HFile);
- return 1;
- }
- stock HFile_Close() {
- if(!FileOpen) return 1;
- HFile_Delete(File);
- new string[150];
- for(new i; i <= sizeof Data-1;i++) {
- if(!strlen(Data[i])) continue;
- formatex(string,150,"%s=%s",Data[i],Value[i])
- write_file(File,string);
- Data[i] = "",Value[i] = "";
- }
- File = "";
- FileOpen = false;
- return 1;
- }
- stock HFile_WriteString(DataN[],ValueN[]) {
- if(!FileOpen) return 0;
- new lines;
- for(new i;i < sizeof Data;i++) {
- if(!strlen(Data[i])) continue;
- lines++;
- if(equali(DataN,Data[i])) {
- formatex(Value[i],MAX_VALUE_STRING,ValueN);
- return 1;
- }
- }
- formatex(Data[lines],MAX_DATA_STRING,DataN);
- formatex(Value[lines],MAX_VALUE_STRING,ValueN);
- return 1;
- }
- stock HFile_WriteInt(DataN[],ValueN) {
- new num[20];
- formatex(num,20,"%d",ValueN);
- return HFile_WriteString(DataN,num);
- }
- stock HFile_WriteFloat(DataN[],Float:ValueN) {
- new num[30];
- formatex(num,30,"%f",ValueN);
- return HFile_WriteString(DataN,num);
- }
- stock HFile_WriteBool(DataN[],bool:ValueN) return HFile_WriteString(DataN,ValueN ? "true":"false");
- stock HFile_ReadString(DataN[]) {
- if(FileOpen) {
- for(new i;i < sizeof Data;i++) {
- if(!strlen(Data[i])) continue;
- if(equali(Data[i],DataN)) return Value[i];
- }
- }
- return Value[MAX_DATA-1];
- }
- stock HFile_ReadInt(DataN[]) return str_to_num(HFile_ReadString(DataN));
- stock Float:HFile_ReadFloat(DataN[]) return str_to_float(HFile_ReadString(DataN));
- stock HFile_ReadBool(DataN[]) return (equali(HFile_ReadString(DataN),"true")) ? true:false;
- stock HFile_UnSet(DataN[]) {
- if(!FileOpen) return 0;
- for(new i;i < sizeof Data;i++) {
- if(!strlen(Data[i])) continue;
- if(equali(Data[i],DataN)) Data[i] = "",Value[i] = "";
- }
- return 1;
- }
- stock HFile_IsSet(DataN[]) {
- if(!FileOpen) return false;
- for(new i;i < sizeof Data;i++) {
- if(!strlen(Data[i])) continue;
- if(equali(Data[i],DataN)) return true;
- }
- return false;
- }
- stock HFile_GetDataName(ValueN[]) {
- if(FileOpen) {
- for(new i;i < sizeof Data;i++) {
- if(!strlen(Data[i])) continue;
- if(equali(Value[i],ValueN)) return Data[i];
- }
- }
- return Data[MAX_DATA-1];
- }
- stock HFile_GetFileSize(filename[]) {
- new tmpres[128];
- new HFile = fopen(filename, "rt");
- new i;
- while(!feof(HFile))
- {
- fgets(HFile, tmpres, sizeof tmpres - 1);
- if(tmpres[0] == '/' && tmpres[1] == '/' || strlen(tmpres) < 3)
- continue;
- i += strlen(tmpres);
- }
- fclose(HFile);
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement