[Include] [INC] For speedy data save and read from files.
#1

This script can be used to save player data to a file and then read it when needed. It has proven to be a lot faster than most of other tools (which do the same) when a massive ammount of data has to be saved (let's say... over 400 values). If you, however don't need such a powerful tool, you can always use dini, it's more user-friendly and somewhat more extendable.

Since the whole script is very small, I'll just post it here:

pawn Code:
#if defined _filefunc_included
 #endinput
#endif

#define _filefunc_included

stock RefreshSFile(document_name[])
{
    new bool:neednew = false;
    if(fexist(document_name)) neednew=true;
    new password[128];
    if(neednew)
    {
        new File:ftmp;
        ReadSFile(document_name,ftmp);
        SFileRead(ftmp,"password",password);
        CloseSFile(ftmp);
    }
    new File:ftmp=fopen(document_name,io_write);
    if(neednew)
    {
        fwrite(ftmp,"password=");
        fwrite(ftmp,password);
        fwrite(ftmp," \r\n");
    }
    fclose(ftmp);
}

stock WriteSFile(document_name[],&File:stream)
{
    stream = fopen(document_name,io_append);
}

stock ReadSFile(document_name[],&File:stream)
{
    stream = fopen(document_name,io_read);
}

stock CloseSFile(File:document)
{
    fclose(document);
}

stock SFileWrite(File:document,Value[],Data[])
{
    new ftmp[128];
    format(ftmp,sizeof(ftmp),"%s=%s \r\n",Value,Data);
    fwrite(document,ftmp);
}

stock SFileRead(File:document,Value[],result[])
{
    fseek(document);
    new stmp[256],newvalue[256];
    format(newvalue,sizeof(newvalue),"%s=",Value);
    while(fread(document, stmp))
    {
        if(!strcmp(stmp,newvalue,false,strlen(Value)))
        {
            new index=strfind(stmp,"=",true)+1;
            strmid(result,stmp,index,strlen(stmp)-3,index+128);
            return;
        }
    }

}

stock IntString(integer)
{
    new str[12];
    format(str,12,"%i",integer);
    return str;
}

stock FloatString(Float:integer)
{
    new str[12];
    format(str,12,"%f",integer);
    return str;
}
Here is an example script to test it:

pawn Code:
#include <a_samp>
#include "data/filefunc.txt" // This is where I include the script above.
new filename[]="test.ini";

main()
{
    print("\n----------------------------------");
    print(" A test script");
    print("----------------------------------\n");
}


public OnGameModeInit()
{
    new File:handle;
   
    //Write some data to a file
    RefreshSFile(filename); //Create or wipe file (this function creates a file if it does not exist or wipes an existing file. Pretty useful.) This function also moves the 'password' value over to a wiped file.
    WriteSFile(filename,handle); // Open the file for writing
    SFileWrite(handle,"password","Wootage"); // Example password saving
    SFileWrite(handle,"TestString","Woot"); // Save data to the file;
    SFileWrite(handle,"TestString2","Woot2"); // Save some other data to the file;
    CloseSFile(handle); // Close the file
   
    new value[128];
   
    //Print some data from a file
   
    //you CAN also be read in backwards
   
    ReadSFile(filename,handle); //Open the file for reading
    SFileRead(handle,"TestString2",value); // Read another string
    printf("TestString2=%s",value); // Print the results
    SFileRead(handle,"TestString",value); //Read it
    printf("TestString=%s",value); // Print the results
    CloseSFile(handle); // Close the file
   
    return 1;
}
It has no name or copyright, just releasing some nice stuff for ya. Have fun.

Greetz,
Garf.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)