[Include] mFiles - File Writer/Reader System
#1

Introduction:

Recently I was struggling with different file systems, some were slow or some weren't convinient at all.
I decided to write my own system, and I've been working on it for a while.
This is the first system I am releasing, so please be gentle with your comments!

How To Use:

Simply download the file, and put it in your includes folder.
At the top of your filterscript/gamemode write this line:
pawn Код:
#include <mfiles>
after this line:
pawn Код:
#include <a_samp>
should look like this:
pawn Код:
#include <a_samp>
#include <mfiles>
// Other includes ...
Settings:

I have declared a few defines in the include itself, I advice to keep it as it is, but if you insist you may change it.
pawn Код:
#define MAX_FILE_READ_STRING            255
// The number of cells which will be used in the reading/writing array.
#define MAX_KEY_LEN                     32
// Maximum length of a key
#define MAX_VAL_LEN                     128
// Maximum length of a value.
#define KEY_IGNORECASE                  false
// Ignorecase:
// When set to false: when reading/writing to a file it will be case-sensetive.
// That means that VALUE and value won't be the same key.
// If set to true it does the opposite action.
Version:

• mFiles v1.0 - Initial Release

Bugs:

• None found yet, however if you find a bug please post here or PM me on forums with the details.

Download:

Mediafire: http://www.mediafire.com/?u4xcoh45wkrbqh5

Please do not make any mirrors without my permission.

Examples:

Writing:

Writing is done in a very simple way:
Open a file, write the data and close the file.

pawn Код:
new file[] = "settings.ini";

// Check if a file exists, if not: create it.
if(!File_Exists(file)) File_Create(file);

// Begin writing into a file.
File_Write(file);

// Writing the data...
File_WriteString("hostname", "gamemode_0");
File_WriteInt("maxplayers", 50);
File_WriteFloat("gravity", 0.08);
File_WriteBool("allow_connect", true);
File_WriteHex("spawn_color", 0xFFFFFF);
File_WriteBin("binary_info", 0b1110110);

// Finish writing, save and close.
File_WriteFinish(file);
Reading:

Reading is done pretty much the same way, open a file, read the data and close it.

pawn Код:
new hostname[11], maxplayers, Float:gravity, bool:allow_connect, file[] = "settings.ini";

// Check if file exists
if(File_Exists(file)) {

    // Open the file
    File_Open(file);
   
    // Read the data from it...
    File_GetStringEx("hostname", hostname);
    maxplayers = File_GetInt("maxplayers");
    gravity = File_GetFloat("gravity");
    allow_connect = File_GetBool("allow_connect");
   
    // Close the file
    File_Close();
}
Functions:

Natives:

pawn Код:
native File_Create(filename[]);
native File_Rename(filename[], newfile[]);
native File_Copy(filename[], copyname[], bool:remove_original = false, bool:content_only = false);
native File_Exists(filename[]);
native File_Write(filename[]);
native File_WriteInt(key[], value);
native File_WriteFloat(key[], Float:value, accuracy = 6);
native File_WriteString(key[], value[]);
native File_WriteHex(key[], value);
native File_WriteBool(key[], bool:value);
native File_WriteBin(key[], value);
native File_WriteFinish(filename[]);
native File_Open(filename[]);
native File_GetInt(key[]);
native File_GetFloat(key[]);
native File_GetString(key[]);
native File_GetStringEx(key[], dest[], size = sizeof(dest), bool:pack = false);
native File_GetHex(key[]);
native File_GetBool(key[]);
native File_GetBin(key[]);
native File_Close();
• File_Create(filename[])
Description: Create a new file.

Returns: 1 if succeded, 0 if failed (or file already exists).

Parameters:
- filename[] - The name of the file you want to create.
• File_Rename(filename[], newfile[])
Description: Rename a file.

Returns: 1 if succeded, 0 if failed (or original doesn't exist/new name already exist).

Parameters:
- filename[] - The name of the file you want to rename.
- newfile[] - The new name of the file.
• File_Copy(filename[], copyname[], bool:remove_original = false, bool:content_only = false)
Description: Copy a file.

Returns: 1 if succeded, 0 if failed

Parameters:
- filename[] - The name of the file you want to copy.
- copyname[] - The name of the copy file.
- bool:remove_original - Remove original file (set to false by default).
- bool:content_only - Copy content only (set to false by default).
• File_Exists(filename[])
Description: Check if a file exists.

Returns: 1 if exists, 0 if not.

Parameters:
- filename[] - Name of the file you want to check if exists.
• File_Write(filename[])
Description: Begin writing into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- filename[] - The name of the file you want to write into.
• File_WriteString(key[], value[])
Description: Write a string into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value[] - The string value you want to write.
• File_WriteInt(key[], value)
Description: Write an integer into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The integer value you want to write.
• File_WriteFloat(key[], Float:value, accuracy = 6)

(accuracy idea was originally by ****** as you can see in y_ini)
Description: Write a float into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- Float:value - The float value you want to write.
- accuracy - The number of digits after the dot.
• File_WriteBool(key[], bool:value)
Description: Write a boolean into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- bool:value - The boolean value you want to write.
• File_WriteHex(key[], value)
Description: Write an hexadecimal into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The hexadecimal value you want to write.
• File_WriteBin(key[], value)
Description: Write a binary into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The binary value you want to write.
• File_WriteFinish(filename[])
Description: Write the data into the originally file and close it (must be used when finishing writing).

Returns: None.

Parameters:
- filename[] - The name of the file you finish writing to.
• File_Open(filename[])
Description: Open a file to begin reading from it.

Returns: 1 if succeded, 0 if failed (or file doesn't exist).

Parameters:
- filename[] - The name of the file you want to read from.
• File_GetString(key[])
Description: Read a string value from a file.

Returns: The string it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetStringEx(key[], dest[], size = sizeof(dest), bool: pack = false)
Description: Read a string value from a file (with extra parameters).

Returns: Nothing if failed.

Parameters:
- key[] - The key you want to read from.
- dest[] - The array you want to save the string to.
- size - Size of the array.
- bool: pack - Pack the string or not (set to false by default).
• File_GetInt(key[])
Description: Read an integer value from a file.

Returns: The integer it read from a file.

Parameters:
- key[] - The key you want to read from.
• Float: File_GetFloat(key[])
Description: Read a float value from a file.

Returns: The float it read from a file.

Parameters:
- key[] - The key you want to read from.
• bool: File_GetBool(key[])
Description: Read a boolean value from a file.

Returns: The boolean value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetHex(key[])
Description: Read an hexadecimal value from a file.

Returns: The hexadecimal value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetBin(key[])
Description: Read a binary value from a file.

Returns: The binary value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_Close()
Description: Finish reading from a file and close it.

Returns: None.
Timings:

Running this code: http://pastebin.com/J5X7n9Xz

Writing:

Writing 100 strings into a blank file:
Код:
4 (ms)
Writing (replacing) 100 strings in a file:
Код:
50-60 (ms)
Reading:

Reading 100 strings from a file:
Код:
20-40 (ms)
Reply
#2

Awesome releasses! i'll use it for sure!
Reply
#3

Quote:
Originally Posted by yan0804
Посмотреть сообщение
Awesome releasses! i'll use it for sure!
Thanks!

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Nice to see you released it!

I got your PM about the replacement, I've been busy recently but if you still want me to look into it, I will!
I'll check out the code tomorrow
Sure!
Reply
#4

pawn Код:
stock File_Rename(filename[], newfile[]) {
    if(!fexist(filename) || fexist(newfile)) return 0;
    if(!File_Copy(filename, newfile)) return 0;
    fremove(filename);
    return 1;
}
Seems legit
Reply
#5

I have just noticed that I forgot to change something in the include, I have updated the link.

Quote:
Originally Posted by FireCat
Посмотреть сообщение
pawn Код:
stock File_Rename(filename[], newfile[]) {
    if(!fexist(filename) || fexist(newfile)) return 0;
    if(!File_Copy(filename, newfile)) return 0;
    fremove(filename);
    return 1;
}
Seems legit
That works just fine.
Reply
#6

good job mate this seems nice any speed tests ?
Reply
#7

Quote:
Originally Posted by Lexi'
Посмотреть сообщение
good job mate this seems nice any speed tests ?
Thanks.
Yes there are, go to the end of the topic, or just hit CTRL + F and type "Timings" and your browser will do the rest of the job for ya.
Reply
#8

Quote:
Originally Posted by Maxips2
Посмотреть сообщение
Thanks.
Yes there are, go to the end of the topic, or just hit CTRL + F and type "Timings" and your browser will do the rest of the job for ya.
Aren't the timings for comparing with other file handlers? It's nice that your include can write 100 strings into a file in 4 ms on your PC/server, but the speed of my PC/server is most likely different. It would be nice if you could run a speed test with for example Dini, Y_ini (and maybe some other file handlers) with the same technique and show us the differences.
Reply
#9

Quote:
Originally Posted by Maxips2
Посмотреть сообщение
Thanks.
Yes there are, go to the end of the topic, or just hit CTRL + F and type "Timings" and your browser will do the rest of the job for ya.
Fail! Sorry sorry about that however nice job again.
Reply
#10

Quote:
Originally Posted by Basssiiie
Посмотреть сообщение
Aren't the timings for comparing with other file handlers? It's nice that your include can write 100 strings into a file in 4 ms on your PC/server, but the speed of my PC/server is most likely different. It would be nice if you could run a speed test with for example Dini, Y_ini (and maybe some other file handlers) with the same technique and show us the differences.
Well you can see different timings in different file writers/readers threads.

But for your quesiton, its much faster than dini for sure.
I haven't comapred timings with y_ini but I believe its faster, but way more complicated.

This system is very easy to use, and its relativley fast.
Reply
#11

Quote:
Originally Posted by Maxips2
Посмотреть сообщение
Well you can see different timings in different file writers/readers threads.

But for your quesiton, its much faster than dini for sure.
I haven't comapred timings with y_ini but I believe its faster, but way more complicated.

This system is very easy to use, and its relativley fast.
I understand that I can see timings in, but the speed of the script isn't the same on every PC/server. mFiles might just do 100 strings in under 4 ms on your computer, but if I try it on my old laptop, it might take 100 ms. Same goes for other handlers like Y_ini. On ******' PC it might take 100 ms to write 10.000 integers (= example), while on other PC's it might take 250 ms to do the same amount of integers. No offense to your work, I'm just curious how quick your system is in comparison with the other systems while tested on the same PC.
Reply
#12

Alright, well I made a timing comparsion with sii and here are the results:

Writing 100 strings into a blank file:

mFiles: 4 (ms)
sii: 5 (ms)

Reading 100 strings:

mFiles: 32 (ms)
sii: 7 (ms)

Writing (replacing) 100 strings into a file:

mFiles: 53 (ms)
sii: 6 (ms)

As you can see sii is slightly faster in some ways, but both systems work in a different way!
sii is based on cache while mFiles is directly writing into a file (well, not exactly directly)

Only one downside which I see with sii is that you are limited to an amount of keys (255 by default).
Cache system takes more memory (correct me if im wrong) which means; increasing the amount of keys takes more memory and also takes longer to write!
Reply
#13

Great release! I'm sure that people will find it useful
Reply
#14

Quote:
Originally Posted by Amit_B
Посмотреть сообщение
Great release! I'm sure that people will find it useful
Thanks!
Reply
#15

Update: a new version is about to be released.
However, I need some people to help me test it so I can release a stable version, anyone interested?
Reply
#16

Quote:
Originally Posted by Maxips2
Посмотреть сообщение
Introduction:

Recently I was struggling with different file systems, some were slow or some weren't convinient at all.
I decided to write my own system, and I've been working on it for a while.
This is the first system I am releasing, so please be gentle with your comments!

How To Use:

Simply download the file, and put it in your includes folder.
At the top of your filterscript/gamemode write this line:
pawn Код:
#include <mfiles>
after this line:
pawn Код:
#include <a_samp>
should look like this:
pawn Код:
#include <a_samp>
#include <mfiles>
// Other includes ...
Settings:

I have declared a few defines in the include itself, I advice to keep it as it is, but if you insist you may change it.
pawn Код:
#define MAX_FILE_READ_STRING            255
// The number of cells which will be used in the reading/writing array.
#define MAX_KEY_LEN                     32
// Maximum length of a key
#define MAX_VAL_LEN                     128
// Maximum length of a value.
#define KEY_IGNORECASE                  false
// Ignorecase:
// When set to false: when reading/writing to a file it will be case-sensetive.
// That means that VALUE and value won't be the same key.
// If set to true it does the opposite action.
Version:

• mFiles v1.0 - Initial Release

Bugs:

• None found yet, however if you find a bug please post here or PM me on forums with the details.

Download:

Mediafire: http://www.mediafire.com/?u4xcoh45wkrbqh5

Please do not make any mirrors without my permission.

Examples:

Writing:

Writing is done in a very simple way:
Open a file, write the data and close the file.

pawn Код:
new file[] = "settings.ini";

// Check if a file exists, if not: create it.
if(!File_Exists(file)) File_Create(file);

// Begin writing into a file.
File_Write(file);

// Writing the data...
File_WriteString("hostname", "gamemode_0");
File_WriteInt("maxplayers", 50);
File_WriteFloat("gravity", 0.08);
File_WriteBool("allow_connect", true);
File_WriteHex("spawn_color", 0xFFFFFF);
File_WriteBin("binary_info", 0b1110110);

// Finish writing, save and close.
File_WriteFinish(file);
Reading:

Reading is done pretty much the same way, open a file, read the data and close it.

pawn Код:
new hostname[11], maxplayers, Float:gravity, bool:allow_connect, file[] = "settings.ini";

// Check if file exists
if(File_Exists(file)) {

    // Open the file
    File_Open(file);
   
    // Read the data from it...
    File_GetStringEx("hostname", hostname);
    maxplayers = File_GetInt("maxplayers");
    gravity = File_GetFloat("gravity");
    allow_connect = File_GetBool("allow_connect");
   
    // Close the file
    File_Close();
}
Functions:

Natives:

pawn Код:
native File_Create(filename[]);
native File_Rename(filename[], newfile[]);
native File_Copy(filename[], copyname[], bool:remove_original = false, bool:content_only = false);
native File_Exists(filename[]);
native File_Write(filename[]);
native File_WriteInt(key[], value);
native File_WriteFloat(key[], Float:value, accuracy = 6);
native File_WriteString(key[], value[]);
native File_WriteHex(key[], value);
native File_WriteBool(key[], bool:value);
native File_WriteBin(key[], value);
native File_WriteFinish(filename[]);
native File_Open(filename[]);
native File_GetInt(key[]);
native File_GetFloat(key[]);
native File_GetString(key[]);
native File_GetStringEx(key[], dest[], size = sizeof(dest), bool:pack = false);
native File_GetHex(key[]);
native File_GetBool(key[]);
native File_GetBin(key[]);
native File_Close();
• File_Create(filename[])
Description: Create a new file.

Returns: 1 if succeded, 0 if failed (or file already exists).

Parameters:
- filename[] - The name of the file you want to create.
• File_Rename(filename[], newfile[])
Description: Rename a file.

Returns: 1 if succeded, 0 if failed (or original doesn't exist/new name already exist).

Parameters:
- filename[] - The name of the file you want to rename.
- newfile[] - The new name of the file.
• File_Copy(filename[], copyname[], bool:remove_original = false, bool:content_only = false)
Description: Copy a file.

Returns: 1 if succeded, 0 if failed

Parameters:
- filename[] - The name of the file you want to copy.
- copyname[] - The name of the copy file.
- bool:remove_original - Remove original file (set to false by default).
- bool:content_only - Copy content only (set to false by default).
• File_Exists(filename[])
Description: Check if a file exists.

Returns: 1 if exists, 0 if not.

Parameters:
- filename[] - Name of the file you want to check if exists.
• File_Write(filename[])
Description: Begin writing into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- filename[] - The name of the file you want to write into.
• File_WriteString(key[], value[])
Description: Write a string into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value[] - The string value you want to write.
• File_WriteInt(key[], value)
Description: Write an integer into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The integer value you want to write.
• File_WriteFloat(key[], Float:value, accuracy = 6)

(accuracy idea was originally by ****** as you can see in y_ini)
Description: Write a float into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- Float:value - The float value you want to write.
- accuracy - The number of digits after the dot.
• File_WriteBool(key[], bool:value)
Description: Write a boolean into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- bool:value - The boolean value you want to write.
• File_WriteHex(key[], value)
Description: Write an hexadecimal into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The hexadecimal value you want to write.
• File_WriteBin(key[], value)
Description: Write a binary into a file.

Returns: 1 if succeded, 0 if failed.

Parameters:
- key[] - The name of the file you want to write into.
- value - The binary value you want to write.
• File_WriteFinish(filename[])
Description: Write the data into the originally file and close it (must be used when finishing writing).

Returns: None.

Parameters:
- filename[] - The name of the file you finish writing to.
• File_Open(filename[])
Description: Open a file to begin reading from it.

Returns: 1 if succeded, 0 if failed (or file doesn't exist).

Parameters:
- filename[] - The name of the file you want to read from.
• File_GetString(key[])
Description: Read a string value from a file.

Returns: The string it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetStringEx(key[], dest[], size = sizeof(dest), bool: pack = false)
Description: Read a string value from a file (with extra parameters).

Returns: Nothing if failed.

Parameters:
- key[] - The key you want to read from.
- dest[] - The array you want to save the string to.
- size - Size of the array.
- bool: pack - Pack the string or not (set to false by default).
• File_GetInt(key[])
Description: Read an integer value from a file.

Returns: The integer it read from a file.

Parameters:
- key[] - The key you want to read from.
• Float: File_GetFloat(key[])
Description: Read a float value from a file.

Returns: The float it read from a file.

Parameters:
- key[] - The key you want to read from.
• bool: File_GetBool(key[])
Description: Read a boolean value from a file.

Returns: The boolean value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetHex(key[])
Description: Read an hexadecimal value from a file.

Returns: The hexadecimal value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_GetBin(key[])
Description: Read a binary value from a file.

Returns: The binary value it read from a file.

Parameters:
- key[] - The key you want to read from.
• File_Close()
Description: Finish reading from a file and close it.

Returns: None.
Timings:

Running this code: http://pastebin.com/J5X7n9Xz

Writing:

Writing 100 strings into a blank file:
Код:
4 (ms)
Writing (replacing) 100 strings in a file:
Код:
50-60 (ms)
Reading:

Reading 100 strings from a file:
Код:
20-40 (ms)
Ok Thats Akward
Reply
#17

Quote:
Originally Posted by [MK]Man_Deep
Посмотреть сообщение
Ok Thats Akward
What's akward?
Reply
#18

Just a quick hint; these aren't natives but stocks.

Nice, can yoh do benchmarks with y_ini too?
Reply
#19

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
Just a quick hint; these aren't natives but stocks.

Nice, can yoh do benchmarks with y_ini too?
In version 1.0 I believe mFiles could be faster in some cases but I can't tell you for sure as I have not compared it with y_ini.
However with the new verison (1.1) mFiles is the fastest writer/reader you can find on the net (if you use it right of course).
With 1.1 I have compared it to sii, DOF2 and y_ini with writing 1000 files and also replacing 1000 fields (255 fields on the cache based systems) and mFiles was the fastest of all (including reading!).

Version 1.1 is not released just yet, but I think I might release it as a beta version.
The reason it was not released to public yet is because I am a little bit worried about the repalcment part as its a new method which I came up with.
Reply
#20

thanks
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)