/*
ri File - Version 1.0.0
© 2012 by RFT
*/
#include <a_samp>
/*
native ri_Create(File[]);
native ri_Open(File[]);
native ri_Close();
native ri_Save();
native ri_ReadString(Var[], str[]);
native ri_ReadInt(Var[]);
native ri_ReadFloat(Var[]);
native ri_WriteString(Var[], str[]);
native ri_WriteInt(Var[], inte);
native ri_WriteFloat(Var[], Float:f);
native ri_Remove(File[]);
*/
new ri_path[32];
new ri_Cache[100][128];
new ri_line[100];
new ri_line_counter;
new ri_readed=0;
stock ri_Create(File[])
{
new File:w_file=fopen(File, io_write);
fclose(w_file);
return true;
}
stock ri_Remove(File[])
{
fremove(File);
return true;
}
stock ri_Open(File[])
{
if(!fexist(File) || ri_readed==1)return 0;
new File:r_file=fopen(File, io_read);
ri_readed=1;
strmid(ri_path, File, 0, strlen(File), 64);
new c=0, load[128];
while(fread(r_file, load))
{
new len=strlen(load);
strmid(ri_Cache[c], load, 0, len-2, 128);
ri_line[c]=1;
ri_line_counter++;
c++;
}
fclose(r_file);
return 1;
}
stock ri_Close()
{
for(new i = 0; i < sizeof(ri_Cache); i++)
{
if(ri_line[i]==0)continue;
ri_Cache[i]="\n";
ri_line[i]=0;
ri_line_counter=0;
}
return true;
}
stock ri_Save()
{
if(ri_readed==0)return 0;
new File:w_file=fopen(ri_path, io_write);
new str[128];
for(new i = 0; i < sizeof(ri_Cache); i++)
{
if(ri_line[i]==0)continue;
format(str, sizeof(str), "%s\r\n", ri_Cache[i]);
fwrite(w_file, str);
}
fclose(w_file);
return true;
}
stock ri_ReadString(Var[], str[])
{
for(new i = 0; i < sizeof(ri_Cache); i++)
{
if(ri_line[i]==0 || ri_readed==0)continue;
if(strfind(ri_Cache[i], Var, true)!=-1)
{
new s=strfind(ri_Cache[i], "=", true);
if(s!=-1)
{
s++;
new o=strlen(ri_Cache[i][s]);
strmid(str, ri_Cache[i], s, s+o, 128);
return true;
}
}
}
return false;
}
stock ri_ReadInt(Var[])
{
new str[128];
ri_ReadString(Var, str);
return strval(str);
}
stock ri_ReadFloat(Var[])
{
new str[128];
ri_ReadString(Var, str);
return float:floatstr(str);
}
stock ri_WriteString(Var[], str[])
{
for(new i = 0; i < sizeof(ri_Cache); i++)
{
if(ri_line[i]==0)continue;
if(strfind(ri_Cache[i], Var, true)!=-1)
{
new s=strfind(ri_Cache[i], "=", true);
s++;
strdel(ri_Cache[i], s, 128);
strins(ri_Cache[i], str, s, 128);
return true;
}
}
ri_line_counter++;
format(ri_Cache[ri_line_counter], 128, "%s=%s", Var, str);
ri_line[ri_line_counter]=1;
return true;
}
stock ri_WriteInt(Var[], inte)
{
new str[32];
format(str, sizeof(str), "%d", inte);
ri_WriteString(Var, str);
return true;
}
stock ri_WriteFloat(Var[], Float:f)
{
new str[32];
format(str, sizeof(str), "%f", f);
ri_WriteString(Var, str);
return true;
}