/*
| cessil's ini system (cini)
| version: 2.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_DeleteAccount(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_FAdd(filen[],typedefs[],{Float,_}:...); similar to cini_Add but first arg requires a filename not playerid
| 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
| cini_FDelete(filen[]); deletes a file
|
| Version: 2.0
| speed improved
| escapes all saved strings
| doesn't stop on undefined typedefs
| changed cini_Delete to cini_DeleteAccount
| added cini_FDelete(filen[])
*/
#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
#define getstrarg(%1,%2) for(new x = 0; getarg(%1,x) != '\0'; x++) %2[x] = getarg(%1,x)
#define setstrarg(%1,%2) for(new x = 0; %2[x] != '\0'; x++) setarg(%1,x,%2[x])
#define cini_ES cini_EscapeString
/*==================/
/ for user accounts /
/==================*/
stock cini_GetFileName(playerid,filename[])
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
strcat(filename,cini_file_location,cini_max_filename);
strcat(filename,name,cini_max_filename);
strcat(filename,".ini",cini_max_filename);
}
stock cini_Register(playerid, pass[])
{
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
new string[cini_max_string];
if(fexist(filename)) return false;
new File:file = fopen(filename,io_write);
string = "Password=";
cini_ES(pass);
strcat(string,pass,sizeof(string));
strcat(string,"\n",sizeof(string));
fwrite(file,string);
fclose(file);
if(fexist(filename)) return true;
return false;
}
stock cini_CheckAccount(playerid)
{
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename)) return false;
return true;
}
stock cini_CheckLogin(playerid,pass[])
{
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename)) return false;
new string[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(!strfind(string,"Password=",true))
{
strdel(string,0,strfind(string,"=",true)+1);
strdel(string,strlen(string)-1,strlen(string));
if(!strcmp(pass,string,false))
{
fclose(file);
return true;
}
}
}
fclose(file);
return false;
}
stock cini_Save(playerid,typedefs[],{Float,_}:...)
{
new string[cini_max_string];
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename))
{
printf("cini error: cini_Save account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != (numargs() - 2) / 2)
{
printf("cini error: cini_Save type definitions expected: %d got: %d",strlen(typedefs),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(!strfind(string,"Password=",false)) 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,j=strlen(typedefs);i<j;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
strcat(string,tstr,sizeof(string));
strcat(string,"=",sizeof(string));
switch(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
strcat(tsrt,"\n",sizeof(tsrt));
strcat(string,tsrt,sizeof(string));
}
default:
{
printf("cini error: cini_Save unknown type passed in type definitions: %c",typedefs[i]);
strcat(string,"Error Adding Data\n",sizeof(string));
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_Add(playerid,typedefs[],{Float,_}:...)
{
new string[cini_max_string];
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename))
{
printf("cini error: cini_Add account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != (numargs() - 2) / 2)
{
printf("cini error: cini_Add type definitions expected: %d got: %d",strlen(typedefs),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
if(strfind(string,"Password=",true) != -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,j=strlen(typedefs);i<j;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
strcat(string,tstr,sizeof(string));
strcat(string,"=",sizeof(string));
switch(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
strcat(string,tsrt,sizeof(string));
strcat(string,"\n",sizeof(string));
}
default:
{
printf("cini error: cini_Add unknown type passed in type definitions: %c",typedefs[i]);
strcat(string,"Error Adding Data\n",sizeof(string));
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_Load(playerid,typedefs[],{Float,_}:...)
{
new args = numargs() - 2;
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename))
{
printf("cini error: cini_Load account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != args / 2)
{
printf("cini error: cini_Load type definitions expected: %d got: %d",strlen(typedefs),args / 2);
return false;
}
new string[cini_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0,l=strlen(typedefs);i<l;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
if(strfind(string,tstr,true) || !strlen(tstr)) continue;
strdel(string,0,strfind(string,"=",true)+1);
strdel(string,strlen(string)-1,strlen(string));
switch(typedefs[i])
{
case 'd', 'i':
{
setarg(3 + (i * 2), 0, strval(string));
}
case 'f':
{
setarg(3 + (i * 2),0,_:floatstr(string));
}
case 's':
{
setstrarg(3 + (i * 2),string);
}
default:
{
printf("cini error: cini_FLoad unknown type passed in type definitions: %c",typedefs[i]);
}
}
}
}
fclose(file);
return true;
}
stock cini_Update(playerid,typedefs[],{Float,_}:...)
{
new args = numargs() - 2, updated;
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename))
{
printf("cini error: cini_Update account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != args / 2)
{
printf("cini error: cini_Update type definitions expected: %d got: %d",strlen(typedefs),args / 2);
return false;
}
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0,l=strlen(typedefs);i<l;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
if(strfind(string,tstr,true) || !strlen(tstr)) continue;
updated++;
strdel(string,strfind(string,"=",true)+1,strlen(string));
switch(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
strcat(tsrt,"\n",sizeof(tsrt));
strcat(string,tsrt,sizeof(string));
}
default:
{
printf("cini error: cini_Update unknown type passed in type definitions: %c",typedefs[i]);
}
}
}
strcat(store,string,sizeof(store));
}
if(strlen(typedefs) != updated) printf("cini warning: cini_Update attempted to update %d keys, updated %d",strlen(typedefs),updated);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
stock cini_DelKey(playerid,{Float,_}:...)
{
new args = numargs() - 1, deleted;
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(!fexist(filename))
{
printf("cini error: cini_DelKey account %s does not exist",filename);
return false;
}
new string[cini_string];
new store[cini_max_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
new skip;
for(new i=0;i<args;i++)
{
new tstr[cini_string];
getstrarg(1 + i,tstr);
if(!strfind(string,tstr,true) || !strlen(tstr))
{
deleted++;
skip=1;
continue;
}
}
if(!skip) 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_DeleteAccount(playerid)
{
new filename[cini_max_filename];
cini_GetFileName(playerid,filename);
if(fremove(filename)) return true;
return false;
}
stock cini_EscapeString(text[])
{
new pos;
new counter;
while(strfind(text,"\n",false,pos) != -1)
{
pos = strfind(text,"\n",false,pos);
strdel(text,pos,pos+1);
counter++;
pos++;
}
pos = 0;
counter = 0;
while(strfind(text,"\r",false,pos) != -1)
{
pos = strfind(text,"\r",false,pos);
strdel(text,pos,pos+1);
counter++;
pos++;
}
}
/*==================/
/ for other files /
/==================*/
stock cini_Create(filename[])
{
if(fexist(filename)) return false;
new File:file = fopen(filename,io_write);
fclose(file);
if(!fexist(filename)) return false;
return true;
}
stock cini_FSave(filename[],typedefs[],{Float,_}:...)
{
new string[cini_max_string];
if(strlen(typedefs) != (numargs() - 2) / 2)
{
printf("cini error: cini_FSave type definitions expected: %d got: %d",strlen(typedefs),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_write);
for(new i=0,j=strlen(typedefs);i<j;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
format(string,sizeof(string),"%s%s=",string,tstr);
switch(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
format(string,sizeof(string),"%s%s\n",string,tsrt);
}
default:
{
printf("cini error: cini_FSave unknown type passed in type definitions: %c",typedefs[i]);
strcat(string,"Error Adding Data\n",sizeof(string));
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_FAdd(filename[],typedefs[],{Float,_}:...)
{
new string[cini_max_string];
if(!fexist(filename))
{
printf("cini error: cini_FAdd account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != (numargs() - 2) / 2)
{
printf("cini error: cini_Add type definitions expected: %d got: %d",strlen(typedefs),(numargs() - 2) / 2);
return false;
}
new File:file = fopen(filename,io_append);
for(new i=0,j=strlen(typedefs);i<j;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
strcat(string,tstr,sizeof(string));
strcat(string,"=",sizeof(string));
switch(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
strcat(string,tsrt,sizeof(string));
strcat(string,"\n",sizeof(string));
}
default:
{
printf("cini error: cini_FAdd unknown type passed in type definitions: %c",typedefs[i]);
strcat(string,"Error Adding Data\n",sizeof(string));
}
}
}
fwrite(file,string);
fclose(file);
return true;
}
stock cini_FLoad(filename[],typedefs[],{Float,_}:...)
{
new args = numargs() - 2;
if(!fexist(filename))
{
printf("cini error: cini_FLoad account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != args / 2)
{
printf("cini error: cini_FLoad type definitions expected: %d got: %d",strlen(typedefs),args / 2);
return false;
}
new string[cini_string];
new File:file = fopen(filename,io_read);
while(fread(file,string))
{
for(new i=0,l=strlen(typedefs);i<l;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
if(strfind(string,tstr,true) || !strlen(tstr)) continue;
strdel(string,0,strfind(string,"=",true)+1);
cini_ES(string);
//strdel(string,strlen(string)-1,strlen(string));
switch(typedefs[i])
{
case 'd', 'i':
{
setarg(3 + (i * 2), 0, strval(string));
}
case 'f':
{
setarg(3 + (i * 2),0,_:floatstr(string));
}
case 's':
{
setstrarg(3 + (i * 2),string);
}
default:
{
printf("cini error: cini_FLoad unknown type passed in type definitions: %c",typedefs[i]);
}
}
}
}
fclose(file);
return true;
}
stock cini_FUpdate(filename[],typedefs[],{Float,_}:...)
{
new args = numargs() - 2, updated;
if(!fexist(filename))
{
printf("cini error: cini_FUpdate account %s does not exist",filename);
return false;
}
if(strlen(typedefs) != args / 2)
{
printf("cini error: cini_FUpdate type definitions expected: %d got: %d",strlen(typedefs),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,l=strlen(typedefs);i<l;i++)
{
new tstr[cini_string];
getstrarg(2 + (i * 2),tstr);
strcat(key,string,sizeof(key));
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(typedefs[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];
getstrarg(3 + (i * 2),tsrt);
cini_ES(tsrt);
strcat(tsrt,"\n",sizeof(tsrt));
strcat(string,tsrt,sizeof(string));
}
default:
{
printf("cini error: cini_FUpdate unknown type passed in type definitions: %c",typedefs[i]);
}
}
}
strcat(store,string,sizeof(store));
}
if(strlen(typedefs) != updated) printf("cini warning: cini_FUpdate attempted to update %d keys, updated %d",strlen(typedefs),updated);
fclose(file);
file = fopen(filename,io_write);
fwrite(file,store);
fclose(file);
return true;
}
stock cini_FDelKey(filename[],{Float,_}:...)
{
new args = numargs() - 1, deleted = 0;
if(!fexist(filename))
{
printf("cini error: cini_FDelKey account %s does not exist",filename);
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];
getstrarg(1 + i,tstr);
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;
}
stock cini_FDelete(filen[])
{
if(fremove(filen)) return true;
return false;
}
#define FILTERSCRIPT
#include <a_samp>
#include <cini>
#if defined FILTERSCRIPT
public OnFilterScriptInit()
{
print("\n /------------------------/");
print(" / cini test filterscript /");
print("/------------------------/\n");
return 1;
}
public OnFilterScriptExit()
{
print("goodbye my lover, goodbye my friend...");
return 1;
}
#endif
public OnPlayerConnect(playerid)
{
if(!cini_CheckAccount(playerid))
{
cini_Register(playerid,"default_pass");
cini_Save(playerid,"ssssdf",
"Props1","Slice",
"Props2","Grim_",
"Props3","Blacklite",
"Props4","Rachael",
"digit1",18,
"float1",1.24
);
cini_Add(playerid,"d","digit2",20);
}
if(!cini_CheckLogin(playerid,"wrong_pass")) print("wrong password");
if(cini_CheckLogin(playerid,"default_pass"))
{
print("correct password!");
new name[cini_string], Float:fl, digit2;
cini_Load(playerid,"sfd",
"Props3",name,
"float1",fl,
"digit2",digit2
);
printf("name = %s, fl = %.02f, digit2 = %d",name,fl,digit2);
cini_Update(playerid,"sf",
"Props3","Potassium",
"float1",floatmul(fl,2.0)
);
cini_Load(playerid,"sf","Props3",name,"float1",fl);
printf("name = %s, float = %.02f",name,fl);
}
print(cini_EscapeString("Password=pass\n\rAdminLevel=1337"));
return 1;
}
public OnPlayerDisconnect(playerid,reason)
{
if(cini_Delete(playerid)) print("deleted account!");
}
cini_Save(playerid,"sdif", //here "s" string, "d" decimal, "i" integer, "f", float. These must be in order
"name","cessil", //string
"score",1000, //decimal
"kills",12, //int
"coolness",87.99 //float
);
/*type defs must be in order and match the amount of values you want to set.
the above code will save a string like.
name=cessil
score=1000
kills=12
coolness=87.99
*/
write times: cini write time 4772 | 2.09 Per ms 4723 | 2.11 Per ms 4801 | 2.08 Per ms cini2 write time 4570 | 2.18 Per ms 4368 | 2.28 Per ms 4469 | 2.23 Per ms bini2 write time 5708 | 1.75 Per ms 5696 | 1.75 Per ms 5478 | 1.82 Per ms fini write time 10918 | 0.91 Per ms 10733 | 0.93 Per ms 11034 | 0.90 Per ms sii write time 6747 | 1.48 Per ms 6664 | 1.50 Per ms 6889 | 1.45 Per ms read times: cini read time 2811 | 3.55 Per ms 2794 | 3.57 Per ms 2786 | 3.58 Per ms cini2 read time 2008 | 4.98 Per ms 1994 | 5.01 Per ms 2040 | 4.90 Per ms bini2 read time 1237 | 8.08 Per ms 1237 | 8.08 Per ms 1262 | 7.92 Per ms fini read time 1320 | 7.57 Per ms 1319 | 7.58 Per ms 1353 | 7.39 Per ms sii read time 2669 | 3.74 Per ms 2638 | 3.79 Per ms 2677 | 3.73 Per ms
#define FILTERSCRIPT
#include <a_samp>
#include <cini>
#include <cini2>
#include <fini>
#include <dini>
#include <sii>
#include <bini2>
#if defined FILTERSCRIPT
public OnFilterScriptInit()
{
print("\n-----------------------------");
print(" ini file system speed tests");
print("-----------------------------\n");
cini_Create("cini.ini");
cini2_Create("cini2.ini");
dini_Create("dini.ini");
Fini_Create("Fini.ini");
bINI_Create("bini.ini");
new super[20];
format(super,sizeof(super),"super dooper string");
print("write times:");
new ticks = GetTickCount();
new Float:average;
for(new i=0;i<10000;i++)
{
cini_FSave("cini.ini","sdf",
"cessil",super,
"digit",200,
"float",1.1
);
}
average = 10000.0 / (GetTickCount() - ticks);
printf("cini write time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
cini2_FSave("cini2.ini","sdf",
"cessil",super,
"digit",200,
"float",1.1
);
}
average = 10000.0 / (GetTickCount() - ticks);
printf("cini2 write time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
bINI_Open("bini.ini");
bINI_WriteString("cessil", super);
bINI_WriteInt("digit", 200);
bINI_WriteFloat("float",1.1);
bINI_Save();
bINI_Close();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("bini2 write time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
Fini_OpenFile("Fini.ini");
Fini_SetStr("cessil",super);
Fini_SetVal("digit",200);
Fini_SetFloat("float",1.1);
Fini_SaveFile();
Fini_CloseFile();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("fini write time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
INI_Open("sii.ini");
INI_WriteString("cessil",super);
INI_WriteInt("digit",200);
INI_WriteFloat("float",1.1);
INI_Save();
INI_Close();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("sii write time %d | %.2f Per ms",GetTickCount() - ticks,average);
print("read times:");
new string[256], digit, Float:fl;
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
cini_FLoad("cini.ini","sdf",
"cessil",string,
"digit",digit,
"float",fl
);
}
average = 10000.0 / (GetTickCount() - ticks);
printf("cini read time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
cini2_FLoad("cini2.ini","sdf",
"cessil",string,
"digit",digit,
"float",fl
);
}
average = 10000.0 / (GetTickCount() - ticks);
printf("cini2 read time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
bINI_Open("bini.ini");
string = bINI_ReadString("cessil");
digit = bINI_ReadInt("digit");
fl = bINI_ReadFloat("float");
bINI_Close();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("bini2 read time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
Fini_OpenFile("Fini.ini");
string = Fini_GetStr("cessil");
digit = Fini_GetValue("digit");
fl = Fini_GetFloat("float");
Fini_CloseFile();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("fini read time %d | %.2f Per ms",GetTickCount() - ticks,average);
ticks = GetTickCount();
for(new i=0;i<10000;i++)
{
INI_Open("sii.ini");
INI_ReadString("cessil",string,sizeof(string));
digit = INI_ReadInt("digit");
fl = INI_ReadFloat("float");
INI_Close();
}
average = 10000.0 / (GetTickCount() - ticks);
printf("sii read time %d | %.2f Per ms",GetTickCount() - ticks,average);
return 1;
}
public OnFilterScriptExit()
{
return 1;
}
#endif
#include <a_samp>
#include <dini>
#define FILTERSCRIPT
public OnFilterScriptInit()
{
print("\n-----------------------------");
print(" ini file system speed tests");
print("-----------------------------\n");
cini_Create("cini.ini");
dini_Create("dini.ini");
Fini_Create("Fini.ini");
new super[20];
format(super,sizeof(super),"super dooper string");
print("write times:");
new ticks = GetTickCount();
for(new i=0;i<1000;i++)
{
cini_FSave("cini.ini","sdf",
"cessil",super,
"digit",200,
"float",1.1
);
}
printf("cini write time %d",GetTickCount() - ticks);
ticks = GetTickCount();
for(new i=0;i<1000;i++)
{
dini_Set("dini.ini","cessil",super);
dini_IntSet("dini.ini","digit",200);
dini_FloatSet("dini.ini","float",1.1);
}
printf("dini write time %d",GetTickCount() - ticks);
ticks = GetTickCount();
Fini_OpenFile("Fini.ini");
for(new i=0;i<1000;i++)
{
Fini_SetStr("cessil","super");
Fini_SetVal("digit",200);
Fini_SetFloat("float",1.1);
}
Fini_SaveFile();
Fini_CloseFile();
printf("fini write time %d",GetTickCount() - ticks);
print("read times:");
#define Fini_GetFloat(%0) floatstr(Fini_GetStr(%0))
ticks = GetTickCount();
Fini_OpenFile("Fini.ini");
for(new i=0;i<1000;i++)
{
Fini_GetStr("cessil");
Fini_GetValue("digit");
Fini_GetFloat("float");
}
Fini_CloseFile();
printf("fini read time %d",GetTickCount() - ticks);
ticks = GetTickCount();
for(new i=0;i<1000;i++)
{
dini_Get("dini.ini","cessil");
dini_Int("dini.ini","digit");
dini_Float("dini.ini","float");
}
printf("dini read time %d",GetTickCount() - ticks);
new string[128], digit, Float:fl;
ticks = GetTickCount();
for(new i=0;i<1000;i++)
{
cini_FLoad("cini.ini","sdf",
"cessil",string,
"digit",digit,
"float",fl
);
}
printf("cini 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;
}
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;
}
[03:37:23] cini write time 222 [03:37:26] dini write time 2426 [03:37:26] fini write time 39 [03:37:26] read times: [03:37:26] fini read time 60 [03:37:26] dini read time 105 [03:37:26] cini read time 141
I should note that in a test on Cache Files OPEN / SAVE / CLOSE should stay out of the loop because it will run only in general 1x
|
Open File.ini Set Get Set Save File.ini Close File.ini
Open File.ini Set Get Save Close Open Set Get Save Close Open Set Get Save File.ini Close File.ini
if(dialogid == Register && response == 1) { if(cini_Register(playerid, inputtext)) ShowPlayerDialog(playerid, Msg, DIALOG_STYLE_MSGBOX, "Succes!", "You have been succesfully registered!", "K thnx bye", ""); ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Now login", "Confirm your password by logging in!", "Login", "Cancel"); cini_Save(playerid, "dii", "AdminLvl", 0, "Money", 0, "Score", 0); //PlayerInfo[playerid][AdminLvl], PlayerInfo[playerid][Money], PlayerInfo[playerid][Score]); cini_Load(playerid,"dii", "AdminLvl", PlayerInfo[playerid][AdminLvl], "Money", PlayerInfo[playerid][Money], "Score", PlayerInfo[playerid][Score]); return 1; }
if(!cini_CheckAccount(playerid)) // --> this way i get the "You are new here!" dialog { ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "You have been here before!", "Please enter your password to login", "Login", "Cancel"); } else { ShowPlayerDialog(playerid, Register, DIALOG_STYLE_INPUT, "You are new here!", "Please enter your preferred password for your account to register", "Register", "Cancel"); }
cini_FSave("hello.ini", "si", "break", "hi\nwoop=11", "woop", 42);