29.01.2011, 13:50
Lol.
It is a Good Script But look my speed tests (READ/WRITE):
TOTAL
TESTS
Fini is Slow in a Multiple Keys,but is much faster in keys normals (example: 15 keys in max)
EDIT
I Include SII
It is a Good Script But look my speed tests (READ/WRITE):
TOTAL
Код:
Cini = 3115 Dini = 9595 Fini = 194 dFile = 283
pawn Код:
#include <a_samp>
#include <dini>
#define FILTERSCRIPT
public OnFilterScriptInit()
{
print("\n-----------------------------");
print(" ini file system speed tests write");
print("-----------------------------\n");
#define maxtests 10000
cini_Create("cini.ini");
dini_Create("dini.ini");
Fini_Create("Fini.ini");
Fini_Create("Dfile.ini");
//write
new super[20];
format(super,sizeof(super),"super dooper string");
print("write times:");
new ticks = GetTickCount();
for(new i=0;i<maxtests;i++)
{
cini_FSave("cini.ini","s",
"cessil",super
);
}
printf("cini write time %d",GetTickCount() - ticks);
ticks = GetTickCount();
for(new i=0;i<maxtests;i++)
{
dini_Set("dini.ini","cessil",super);
}
printf("dini write time %d",GetTickCount() - ticks);
ticks = GetTickCount();
Fini_OpenFile("Fini.ini");
for(new i=0;i<maxtests;i++)
{
Fini_SetStr("cessil",super);
}
Fini_SaveFile();
Fini_CloseFile();
printf("fini write time %d",GetTickCount() - ticks);
ticks = GetTickCount();
dfile_Open("Dfile.ini");
for(new i=0;i<maxtests;i++)
{
dfile_WriteString ("cessil",super);
}
dfile_SaveFile();
printf("dfile write time %d",GetTickCount() - ticks);
//read
print("\n-----------------------------");
print(" ini file system speed tests read");
print("-----------------------------\n");
print("read times:");
ticks = GetTickCount();
for(new i=0;i<maxtests;i++)
{
new string[64];
cini_FLoad("cini.ini","s","cessil",string);
}
printf("cini read time %d",GetTickCount() - ticks);
ticks = GetTickCount();
for(new i=0;i<maxtests;i++)
{
dini_Get("dini.ini","cessil");
}
printf("dini read time %d",GetTickCount() - ticks);
ticks = GetTickCount();
Fini_OpenFile("Fini.ini");
for(new i=0;i<maxtests;i++)
{
Fini_GetStr("cessil");
}
Fini_SaveFile();
Fini_CloseFile();
printf("fini read time %d",GetTickCount() - ticks);
ticks = GetTickCount();
dfile_Open("Dfile.ini");
for(new i=0;i<maxtests;i++)
{
new string[64];
dfile_ReadString ("cessil",string);
}
dfile_SaveFile();
printf("dfile read time %d",GetTickCount() - ticks);
return 1;
}
public OnFilterScriptExit()
{
return 1;
}
#define maxtag (00244)
#define maxfile (99999)
#define maxname (00064)
#define Fini_Exists fexist
#define Fini_GetFloat(%0) floatstr(Fini_GetStr(%0))
static
zNormal1,
zNormal2,
fStr[maxtag],
fTag[maxtag],
fFile[maxfile],
File:zFopenFile,
fName[maxname],
iSource
;
stock
Fini_OpenFile(fname[])
{
zFopenFile = fopen(fname,io_read);
while(fread(zFopenFile,fStr))
strins(fFile,fStr,strlen(fFile),maxfile);
fclose(zFopenFile);
format(fName,maxname,"%s",fname);
return true;
}
stock
Fini_SaveFile()
{
fremove(fName);
zFopenFile = fopen(fName,io_write);
fwrite(zFopenFile,fFile);
fclose(zFopenFile);
return true;
}
stock
Fini_CloseFile()
{
fFile[0] = '\0';
fName[0] = '\0';
return true;
}
stock
Fini_GetStr(ftag[])
{
zNormal1 = strfind(fFile,ftag,true);
zNormal2 = (zNormal1 + (strlen(ftag)));
strmid(fTag,fFile,zNormal2,(zNormal2 + maxtag),maxtag);
zNormal2 = strfind(fTag,"\n",true);
strmid(fStr,fTag,1,zNormal2 ,maxtag);
return fStr;
}
stock
Fini_SetStr(ftag[],fstr[])
{
zNormal1 = strfind(fFile,ftag,true);
if(zNormal1 != -1)
{
format(fTag,maxtag,"%s=%s",ftag,Fini_GetStr(ftag));
iSource = strlen( fTag ) + zNormal1;
format(fStr,maxtag,"%s=%s\r",ftag,fstr);
strdel(fFile,zNormal1 ,iSource);
format(fStr,128,"%s",fStr);
strins(fFile,fStr,zNormal1 ,128);
}
else
{
format(fStr,maxtag,"%s=%s\r\n",ftag,fstr);
strins(fFile,fStr,strlen(fFile));
}
return true;
}
stock
Fini_Create(nFile[])
{
if (Fini_Exists(nFile)) return false;
new File:cFile = fopen(nFile,io_write);
return fclose(cFile);
}
stock
Fini_SetVal(ftag[],fval)
{
static Seting[24];
format(Seting,maxtag,"%d",fval);
Fini_SetStr(ftag,Seting);
return true;
}
stock
Fini_SetFloat(ftag[],Float:fval)
{
static Seting[24];
format(Seting,maxtag,"%f",fval);
Fini_SetStr(ftag,Seting);
return true;
}
stock
Fini_SetBool(ftag[],bool:fval)
{
static Seting[24];
format(Seting,maxtag,"%d",fval);
Fini_SetStr(ftag,Seting);
return true;
}
stock
Fini_IsSet(ftag[])
{
format(fTag,maxtag,"%s=%s",ftag,Fini_GetStr(ftag));
zNormal1 = strfind(fFile,ftag,true);
if(zNormal1 != -1)
return true;
return false;
}
stock Fini_GetBool(ftag[])
{
new Seting = strval(Fini_GetStr(ftag));
return Seting;
}
native file_move(file[],destination[]);
native file_delete(file[]);
native file_create(file[]);
native file_write(file[],text[],mode[] = "a");
native file_read(file[],storage[], size = sizeof(storage));
native file_log();
native file_exists(file[]);
native dir_create(directory[]);
native dir_delete(directory[]);
native dir_exists(directory[]);
stock Fini_GetValue(ftag[])
{
new Seting = strval(Fini_GetStr(ftag));
return Seting;
}
/*
| cessil's ini system (cini)
| version: 1.0
| date: 26/1/2010
| stocks:
| cini_Register(playerid,pass[]); creates a user file with a password
| cini_CheckAccount(playerid,pass[]); checks if an account exists
| cini_Save(playerid,typedefs[],{Float,_}:...); clears an account file and writes specified stats
| cini_Add(playerid,typedefs[],{Float,_}:...); similar to cini_Save but it adds to an existing file and doesn't clear it
| cini_Load(playerid,typedefs[],{Float,_}:...); returns data from a user file
| cini_Update(playerid,typedefs[],{Float,_}:...); replaces key values with new ones
| cini_DelKey(playerid,typedefs[],{Float,_}:...); deletes a key from a user file
| cini_Delete(playerid) deletes a user file
| cini_EscapeString(text[]); cancels out \n and \r, it's not needed if you don't save then load after registration
| cini_Create(filen[],typedefs[],{Float,_}:...); creates an empty file
| cini_FSave(filen[],typedefs[],{Float,_}:...); similar to cini_Save but first arg requires a filename not playerid and does not check if file exists first
| cini_FLoad(filen[],typedefs[],{Float,_}:...); similar to cini_Load but first arg requires a filename not playerid
| cini_FUpdate(filen[],typedefs[],{Float,_}:...); similar to cini_Update but first arg requires a filename not playerid
| cini_FDelKey(filen[],typedefs[],{Float,_}:...); similar to cini_DelKey but first arg requires a filename not playerid
*/
#include <a_samp>
#define cini_max_filename 40 //max size of /folder/name.ini
#define cini_max_string 256 //max string for saving total data
#define cini_max_password_length 20 //maximum password length, use longer if you're encrypting especially whirlpool
#define cini_string 48 //this is like "Password=security" strings
#define cini_file_location "/accounts/" //folder to save accounts to
/*==================/
/ for user accounts /
/==================*/
stock cini_Register(playerid, pass[])
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new filename[cini_max_filename];
new string[cini_max_string];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(fexist(filename)) return false;
new File:file = fopen(filename,io_write);
format(string,sizeof(string),"Password=%s\n",pass);
fwrite(file,string);
fclose(file);
if(fexist(filename)) return true;
return false;
}
stock cini_CheckAccount(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename)) return false;
return true;
}
stock cini_CheckLogin(playerid,pass[])
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new filename[cini_max_filename];
new string[cini_max_string];
new password[cini_max_password_length];
format(password,sizeof(password),"%s",pass);
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename)) return false;
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(strfind(string,"Password=",false,0) == 0)
{
strdel(string,0,strfind(string,"=",true)+1);
strdel(string,strlen(string)-1,strlen(string));
if(!strcmp(password,string,false))
{
fclose(file);
return true;
}
}
}
fclose(file);
return false;
}
stock cini_Save(playerid,typedefs[],{Float,_}:...)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new types[64];
new string[cini_max_string];
new filename[cini_max_filename];
format(types,sizeof(types),"%s",typedefs);
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_Save account %s does not exist",filename);
return false;
}
if(strlen(types) != (numargs() - 2) / 2)
{
printf("cini error: cini_Save type definitions expected: %d got: %d",strlen(types),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(strfind(string,"Password=",false) != -1) break;
}
if(!strlen(string)) //checks the account has a password
{
fclose(file);
printf("cini error: cini_Save, Password not found for %s",filename);
return false;
}
fclose(file);
file = fopen(filename,io_write);
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(string,sizeof(string),"%s%s=",string,tstr);
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
new tsrt[cini_string];
for(new l=0;l<cini_string;l++)
{
tsrt[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tsrt);
}
default:
{
fclose(file);
print("cini error: cini_Save unknown type passed in type definitions");
return false;
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_Add(playerid,typedefs[],{Float,_}:...)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new types[64];
new string[cini_max_string];
new filename[cini_max_filename];
format(types,sizeof(types),"%s",typedefs);
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_Add account %s does not exist",filename);
return false;
}
if(strlen(types) != (numargs() - 2) / 2)
{
printf("cini error: cini_Add type definitions expected: %d got: %d",strlen(types),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(strfind(string,"Password=",false) != -1) break;
}
if(!strlen(string)) //checks the account has a password
{
fclose(file);
printf("cini error: cini_Add, Password not found for %s",filename);
return false;
}
fclose(file);
string = "";
file = fopen(filename,io_append);
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string+2];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(string,sizeof(string),"%s%s=",string,tstr);
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
tstr = "";
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tstr);
}
default:
{
fclose(file);
print("cini error: cini_Add unknown type passed in type definitions");
return false;
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_Load(playerid,typedefs[],{Float,_}:...)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new types[64];
new args = numargs() - 2;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_Load account %s does not exist",filename);
return false;
}
format(types,sizeof(types),"%s",typedefs);
if(!strlen(types) || args <= 0)
{
print("cini error: cini_Load missing parameters");
return false;
}
if(strlen(types) != args / 2)
{
printf("cini error: cini_Load type definitions expected: %d got: %d",strlen(types),args / 2);
return false;
}
new key[cini_string];
new string[cini_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(strcmp(tstr,key,false) || !strlen(tstr)) continue;
strdel(string,0,strfind(string,"=",true)+1);
strdel(string,strlen(string)-1,strlen(string));
switch(types[i])
{
case 'd', 'i':
{
setarg(3 + (i * 2), 0, strval(string));
}
case 'f':
{
setarg(3 + (i * 2),0,_:floatstr(string));
}
case 's':
{
new strin[cini_string];
format(strin,sizeof(strin),"%s",string);
for(new l=0;l<cini_string;l++)
{
setarg(3 + (i * 2),l,strin[l]);
}
}
default:
{
fclose(file);
print("cini error: cini_Load unknown type passed in type definitions");
return false;
}
}
}
}
fclose(file);
return true;
}
stock cini_Update(playerid,typedefs[],{Float,_}:...)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new types[64];
new args = numargs() - 2, updated = 0;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_Update account %s does not exist",filename);
return false;
}
format(types,sizeof(types),"%s",typedefs);
if(!strlen(types) || args <= 0)
{
print("cini error: cini_Update missing parameters");
return false;
}
if(strlen(types) != args / 2)
{
printf("cini error: cini_Update type definitions expected: %d got: %d",strlen(types),args / 2);
return false;
}
new key[cini_string];
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(strcmp(tstr,key,false) || !strlen(tstr)) continue;
updated++;
strdel(string,strfind(string,"=",true)+1,strlen(string));
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
new tsrt[cini_string];
for(new l=0;l<cini_string;l++)
{
tsrt[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tsrt);
}
default:
{
fclose(file);
print("cini error: cini_Update unknown type passed in type definitions");
return false;
}
}
}
strcat(store,string,sizeof(store));
}
if(strlen(types) != updated) printf("cini warning: cini_Update attempted to update %d keys, updated %d",strlen(types),updated);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
stock cini_DelKey(playerid,{Float,_}:...)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new args = numargs() - 1, deleted = 0;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_DelKey account %s does not exist",filename);
return false;
}
if(args <= 0)
{
print("cini error: cini_DelKey missing parameters");
return false;
}
new key[cini_string];
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<args;i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(1 + i , l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(!strcmp(tstr,key,false) || !strlen(tstr))
{
deleted++;
continue;
}
strcat(store,string,sizeof(store));
}
}
if(args != deleted) printf("cini warning: cini_DelKey attempted to delete %d keys, deleted %d",args,deleted);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
stock cini_Delete(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s%s.ini",cini_file_location,name);
print(filename);
if(fremove(filename)) return true;
return false;
}
stock cini_EscapeString(text[])
{
new string[cini_string];
format(string,sizeof(string),"%s",text);
for(new i=0;i<strlen(string);i++)
{
if(string[i] == '\n') strdel(string,i,i+1);
if(string[i] == '\r') strdel(string,i,i+1);
}
return string;
}
/*==================/
/ for other files /
/==================*/
stock cini_Create(filen[])
{
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s",filen);
if(fexist(filename)) return false;
new File:file = fopen(filename,io_write);
fclose(file);
if(!fexist(filename)) return false;
return true;
}
stock cini_FSave(filen[],typedefs[],{Float,_}:...)
{
new types[64];
new string[cini_max_string];
new filename[cini_max_filename];
format(types,sizeof(types),"%s",typedefs);
format(filename,sizeof(filename),"%s",filen);
if(strlen(types) != (numargs() - 2) / 2)
{
printf("cini error: cini_FSave type definitions expected: %d got: %d",strlen(types),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_write);
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(string,sizeof(string),"%s%s=",string,tstr);
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
new tsrt[cini_string];
for(new l=0;l<cini_string;l++)
{
tsrt[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tsrt);
}
default:
{
fclose(file);
print("cini error: cini_FSave unknown type passed in type definitions");
return false;
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_FAdd(filen[],typedefs[],{Float,_}:...)
{
new types[64];
new string[cini_max_string];
new filename[cini_max_filename];
format(types,sizeof(types),"%s",typedefs);
format(filename,sizeof(filename),"%s",filen);
if(!fexist(filename))
{
printf("cini error: cini_FAdd file %s does not exist",filename);
return false;
}
if(strlen(types) != (numargs() - 2) / 2)
{
printf("cini error: cini_FAdd type definitions expected: %d got: %d",strlen(types),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(strfind(string,"Password=",false) != -1) break;
}
if(!strlen(string)) //checks the account has a password
{
fclose(file);
printf("cini error: cini_FAdd, Password not found for %s",filename);
return false;
}
fclose(file);
string = "";
file = fopen(filename,io_append);
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string+2];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(string,sizeof(string),"%s%s=",string,tstr);
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
tstr = "";
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tstr);
}
default:
{
fclose(file);
print("cini error: cini_FAdd unknown type passed in type definitions");
return false;
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_FLoad(filen[],typedefs[],{Float,_}:...)
{
new types[64];
new args = numargs() - 2;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s",filen);
if(!fexist(filename))
{
printf("cini error: cini_FLoad file %s does not exist",filename);
return false;
}
format(types,sizeof(types),"%s",typedefs);
if(!strlen(types) || args <= 0)
{
print("cini error: cini_FLoad missing parameters");
return false;
}
if(strlen(types) != args / 2)
{
printf("cini error: cini_FLoad type definitions expected: %d got: %d",strlen(types),args / 2);
return false;
}
new key[cini_string];
new string[cini_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(strcmp(tstr,key,false) || !strlen(tstr)) continue;
strdel(string,0,strfind(string,"=",true)+1);
strdel(string,strlen(string)-1,strlen(string));
switch(types[i])
{
case 'd', 'i':
{
setarg(3 + (i * 2), 0, strval(string));
}
case 'f':
{
setarg(3 + (i * 2),0,_:floatstr(string));
}
case 's':
{
new strin[cini_string];
format(strin,sizeof(strin),"%s",string);
for(new l=0;l<cini_string;l++)
{
setarg(3 + (i * 2),l,strin[l]);
}
}
default:
{
fclose(file);
print("cini error: cini_FLoad unknown type passed in type definitions");
return false;
}
}
}
}
fclose(file);
return true;
}
stock cini_FUpdate(filen[],typedefs[],{Float,_}:...)
{
new types[64];
new args = numargs() - 2, updated = 0;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s",cini_file_location,name);
if(!fexist(filename))
{
printf("cini error: cini_FUpdate file %s does not exist",filename);
return false;
}
format(types,sizeof(types),"%s",typedefs);
if(!strlen(types) || args <= 0)
{
print("cini error: cini_FUpdate missing parameters");
return false;
}
if(strlen(types) != args / 2)
{
printf("cini error: cini_FUpdate type definitions expected: %d got: %d",strlen(types),args / 2);
return false;
}
new key[cini_string];
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<strlen(types);i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(2 + (i * 2), l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(strcmp(tstr,key,false) || !strlen(tstr)) continue;
updated++;
strdel(string,strfind(string,"=",true)+1,strlen(string));
switch(types[i])
{
case 'd', 'i':
{
format(string,sizeof(string),"%s%d\n",string,getarg(3 + (i * 2)));
}
case 'f':
{
format(string,sizeof(string), "%s%f\n",string,Float:getarg(3 + (i * 2)));
}
case 's':
{
new tsrt[cini_string];
for(new l=0;l<cini_string;l++)
{
tsrt[l] = getarg(3 + (i * 2), l);
}
format(string,sizeof(string),"%s%s\n",string,tsrt);
}
default:
{
fclose(file);
print("cini error: cini_FUpdate unknown type passed in type definitions");
return false;
}
}
}
strcat(store,string,sizeof(store));
}
if(strlen(types) != updated) printf("cini warning: cini_FUpdate attempted to update %d keys, updated %d",strlen(types),updated);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
stock cini_FDelKey(filen[],{Float,_}:...)
{
new args = numargs() - 1, deleted = 0;
new filename[cini_max_filename];
format(filename,sizeof(filename),"%s",cini_file_location,filen);
if(!fexist(filename))
{
printf("cini error: cini_FDelKey file %s does not exist",filename);
return false;
}
if(args <= 0)
{
print("cini error: cini_FDelKey missing parameters");
return false;
}
new key[cini_string];
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0;i<args;i++)
{
new tstr[cini_string];
for(new l=0;l<cini_string;l++)
{
tstr[l] = getarg(1 + i , l);
}
format(key,cini_string,"%s",string);
strdel(key,strfind(key,"=",true),strlen(key));
if(!strcmp(tstr,key,false) || !strlen(tstr))
{
deleted++;
continue;
}
strcat(store,string,sizeof(store));
}
}
if(args != deleted) printf("cini warning: cini_FDelKey attempted to delete %d keys, deleted %d",args,deleted);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
#define dfile_ReadFloat(%1,%2) floatstr(dfile_ReadString(%1,%2))
#define dfile_ReadInt(%1,%2) strval(dfile_ReadString(%1,%2))
#define dfile_ReadBool(%1,%2) boolstr2(dfile_ReadString(%1,%2))
#define dfile_FileExists(%1) file_exists(%1)
#define dfile_DirectoryExists(%1) dir_exists(%1)
#define MAX_STRING 256
#define FILE_SIZE (99999)
#define FILE_NAME (00064)
static tarolo[FILE_SIZE],
fajlnev[FILE_NAME],
olvasas[FILE_SIZE];
stock dfile_Delete(fajl[]){
if(!file_exists(fajl)) return false;
file_delete(fajl);
return true;
}
stock dfile_ReadString(fajl[],parameter[]){
new ertek[MAX_STRING],
eredmeny[MAX_STRING],
talalat;
eredmeny[0]=EOS;
olvasas[0]=EOS;
if(!file_exists(fajl)) return eredmeny;
file_read(fajl, olvasas);
format(ertek, sizeof(ertek), "%s=", parameter);
talalat = strfind(olvasas, ertek, false);
if(talalat != -1){strmid(eredmeny, olvasas, talalat+strlen(ertek), strfind(olvasas, "\n", false, talalat+1)); return eredmeny;}
else return eredmeny;
}
stock dfile_Open(fajl[]){
if(!fexist(fajl)) return false;
format(fajlnev, FILE_NAME, "%s", fajl);
return true;
}
stock dfile_WriteString(parameter[], ertek[])
{
new string[128];
if(fajlnev[0] == 0) return false;
format(string,sizeof(string), "%s=", parameter);
new talalat = strfind(tarolo, string, false, 0);
if(talalat == -1)
{
format(string, sizeof(string), "%s=%s\n", parameter, ertek);
strins(tarolo, string, strlen(tarolo));
}else{
strdel(tarolo, talalat-1, strfind(tarolo, "\n", false, talalat));
format(string, sizeof(string), "%s=%s\n", parameter, ertek);
strins(tarolo, string, strlen(tarolo));
}
return true;
}
stock dfile_SaveFile()
{
if(strlen(tarolo) == 0) return false;
file_delete(fajlnev);
file_create(fajlnev);
file_write(fajlnev, tarolo);
tarolo[0] = EOS;
fajlnev[0] = EOS;
return true;
}
stock dfile_WriteInt(parameter[], ertek){
new intvalue[MAX_STRING];
format(intvalue, sizeof(intvalue), "%d", ertek);
return dfile_WriteString(parameter, intvalue);
}
stock dfile_WriteBool(parameter[], bool:ertek){
if(ertek) dfile_WriteString(parameter, "true");
else dfile_WriteString(parameter, "false");
return true;
}
stock dfile_WriteFloat(parameter[], Float:ertek){
new floatvalue[MAX_STRING];
format(floatvalue, sizeof(floatvalue), "%f", ertek);
return dfile_WriteString(parameter, floatvalue);
}
stock bool:boolstr2(string[]){
if (!string[0] || string[0] == '0' || !strcmp(string, "false", true)) return false;
return true;
}
stock dfile_CreateDirectory(mappa[]){
if(dir_exists(string)) return false;
dir_create(mappa);
return true;
}
stock dfile_DeleteDirectory(mappa[]){
if(!dir_exists(mappa)) return false;
dir_delete(mappa);
return true;
}
stock dfile_MoveFile(fajl[], cel[]){
if(!file_exists(fajl)) return false;
file_move(fajl, cel);
return true;
}
stock dfile_ParseString(fajl[], parameter[], ertek[])
{
new string[128], string2[128];
format(string, sizeof(string), "%s", dfile_ReadString(fajl, parameter));
format(string2, sizeof(string), "%s", ertek);
if(!strcmp(string, string2, false)) return true;
return false;
}
stock dfile_ParseInt(fajl[], parameter[], ertek)
{
new intvalue[MAX_STRING];
format(intvalue, sizeof(intvalue), "%d", ertek);
return dfile_ParseString(fajl, parameter, intvalue);
}
stock dfile_ParseFloat(fajl[], parameter[], Float:ertek)
{
new floatvalue[MAX_STRING];
format(floatvalue, sizeof(floatvalue), "%f", ertek);
return dfile_ParseString(fajl, parameter, floatvalue);
}
stock dfile_ParseBool(fajl[], parameter[], bool:ertek)
{
new string[128], string2[128];
format(string, sizeof(string), "%s", boolstr2(dfile_ReadString(fajl, parameter)));
format(string2, sizeof(string), "%s", ertek);
if(strcmp(string, string2, false)) return true;
return false;
}
EDIT
I Include SII
Код:
----------------------------- [13:02:10] ini file system speed tests write [13:02:10] ----------------------------- [13:02:10] write times: [13:02:12] cini write time 2100 [13:02:21] dini write time 9087 [13:02:21] sii write time 46 [13:02:21] fini write time 98 [13:02:21] dfile write time 27 [13:02:21] ----------------------------- [13:02:21] ini file system speed tests read [13:02:21] ----------------------------- [13:02:21] read times: [13:02:22] cini read time 696 [13:02:22] dini read time 3760 [13:02:22] sii write time 420 [13:02:22] fini read time 69 [13:02:22] dfile read time 172