[Include] mFiles - File Writer/Reader System
#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


Messages In This Thread
mFiles - File Writer/Reader System - by Maxips2 - 26.07.2012, 21:31
Re: mFiles - File Writer/Reader System - by yan0804 - 26.07.2012, 21:56
Re: mFiles - File Writer/Reader System - by Maxips2 - 26.07.2012, 22:26
Re: mFiles - File Writer/Reader System - by FireCat - 26.07.2012, 22:29
Re: mFiles - File Writer/Reader System - by Maxips2 - 26.07.2012, 22:37
Re: mFiles - File Writer/Reader System - by Glint - 26.07.2012, 23:27
Re: mFiles - File Writer/Reader System - by Maxips2 - 26.07.2012, 23:36
Re: mFiles - File Writer/Reader System - by Basssiiie - 26.07.2012, 23:45
Re: mFiles - File Writer/Reader System - by Glint - 26.07.2012, 23:48
Re: mFiles - File Writer/Reader System - by Maxips2 - 26.07.2012, 23:49
Re: mFiles - File Writer/Reader System - by Basssiiie - 27.07.2012, 00:11
Re: mFiles - File Writer/Reader System - by Maxips2 - 27.07.2012, 00:31
Re: mFiles - File Writer/Reader System - by Amit_B - 27.07.2012, 07:32
Re: mFiles - File Writer/Reader System - by Maxips2 - 27.07.2012, 07:35
Re: mFiles - File Writer/Reader System - by Maxips2 - 29.07.2012, 16:28
Re: mFiles - File Writer/Reader System - by [MK]Man_Deep - 29.07.2012, 16:41
Re: mFiles - File Writer/Reader System - by Maxips2 - 29.07.2012, 16:44
Re: mFiles - File Writer/Reader System - by Jochemd - 29.07.2012, 23:17
Re: mFiles - File Writer/Reader System - by Maxips2 - 30.07.2012, 05:15
Re: mFiles - File Writer/Reader System - by Snir_sofer - 03.08.2012, 16:26
Re: mFiles - File Writer/Reader System - by Maxips2 - 03.08.2012, 17:14
Re: mFiles - File Writer/Reader System - by Kaperstone - 03.08.2012, 23:17
Re: mFiles - File Writer/Reader System - by Maxips2 - 04.08.2012, 01:01
Re: mFiles - File Writer/Reader System - by Arca - 04.08.2012, 01:06
Re: mFiles - File Writer/Reader System - by Kaperstone - 04.08.2012, 01:32
Re: mFiles - File Writer/Reader System - by Maxips2 - 04.08.2012, 01:39
Re: mFiles - File Writer/Reader System - by Kaperstone - 04.08.2012, 01:56
Re: mFiles - File Writer/Reader System - by Maxips2 - 04.08.2012, 02:05
Re: mFiles - File Writer/Reader System - by Kaperstone - 04.08.2012, 02:15
Re: mFiles - File Writer/Reader System - by Maxips2 - 04.08.2012, 05:50

Forum Jump:


Users browsing this thread: 3 Guest(s)