[Include] tfile 1.0 - file system based on file lines
#1

This include allow you to write and get data from each line of file by number of this line
It's using my plugin directory (click) what it's more faster then without it, but you can still use just include, without plugin, just before you add to the script #include write a macro:
pawn Code:
#define DO_NOT_USE_PLUGIN
#include <tfile>
(it's not recommended)

How i can use it? in normal file system you use a key name to find data in file, here it is more like create an array, you assign each data a id, is faster then other because file system don't need to search key name, just skipping lines to the right

Functions:

GetFileLine(const filename[], dest[], line, len = sizeof dest)
Gets the data from line of file
  • const filename[] - file name
  • dest[] - array to save the data
  • line - number of the line in file
  • len = sizeof dest - max length of data
Return true if succeeded get data


SetFileLine(const filename[], const substr[], line)
Save/change data in file line
  • const filename[] - file name
  • const substr[] - string/text to save in file line
  • line - number of the line in file
Return true if succeeded save data


DeleteFileLine(const filename[], line)
Deletes full line from file
  • const filename[] - file name
  • line - number of the line in file
Return true if succeeded delete line



ClearFileLine(const filename[], line)
Clears the line in file (not deletes)
  • const filename[] - file name
  • line - number of the line in file
Return true if succeeded clear line



AddFileLine(const filename[], const strline[], line = -1)
Adds line to file,if we add something in line 1, line 1, 2, 3 etc. will be moved forward
  • const filename[] - file name
  • const strline[] - string/text to add to the file
  • line = -1 - number of the line in file (-1 means no line, data will added at the end of file)
Return true if succeeded add line


LinesInFile(const filename[])
Returns the number of lines in the file
  • const filename[] - file name

Additional functions

denter(string[])
Deletes special chars from the string/text ( \r \n )
  • string[] - string/text to modify
Returns nothing

Attention:
1. If you manually modify the file, just remember to set last line in file empty, example:
Quote:

1000
mypass
true
//empty line!

2. If you want to use this include to save players data, remember that's functions 'DeleteFileLine' & 'AddFileLine' can change numbers of file lines
3. Lines in file begins from 0



DOWNLOAD; Mirror


example how to save player data to file:
pawn Code:
#define MONEY 0
#define SCORE 1
#define PASS 2
#define VIP 3
pawn Code:
//creating new player file:
new str[64]
format(str, sizeof str, "%d\r\n%d\r\n%s\r\n%d\r\n", GetPlayerMoney(playerid), GetPlayerScore(playerid), inputtext, true);
new File:x = fopen(PlayerName(playerid), io_write);
fwrite(x, str);
fclose(x);
pawn Code:
//changing the data in the file:
new str[30];
valstr(str, GetPlayerMoney(playerid));
SetFileLine(PlayerName(playerid), str, MONEY);
valstr(str, GetPlayerScore(playerid));
SetFileLine(PlayerName(playerid), str, SCORE);
str[0] = EOS;
strcat(str, inputtext); // inputtext as password?
SetFileLine(PlayerName(playerid), str, PASS);
SetFileLine(PlayerName(playerid), "1", VIP);
pawn Code:
//gets data from file
new str[30];
GetFileLine(PlayerName(playerid), str, MONEY);
GivePlayerMoney(playerid, strval(str));
GetFileLine(PlayerName(playerid), str, SCORE);
SetPlayerScore(playerid, strval(str));
GetFileLine(PlayerName(playerid), str, VIP);
if(str[0] != '0')
SetPVarInt(playerid, "VIP", true); // for example
Reply
#2

Not Bad.
Reply
#3

You better use enumerators instead of defines for that.
Reply
#4

Quote:
Originally Posted by RyDeR`
View Post
You better use enumerators instead of defines for that.
you mean
pawn Code:
enum info
{
money,
score,
pass,
vip
};

SetFileLine(PlayerName(playerid), "1", info:vip);
?
Honestly? I do not see the difference
and I prefer to use a macro
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)