SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 28/02/2014, 06:03 AM   #1
Emmet_
Guest
 
Posts: n/a
Default Inline file loading

Inline file loading

Introduction
This is just a quick experimental include that I've came up with, that allows you to load files and save the data in local variables.

How does it work?
Imagine we have this file (test.ini):

Code:
// test.ini

Level = 5
Money = 50000
Score = 100
We can simply use this code to fetch the data:

pawn Code:
public OnFilterScriptInit()
{
    import File("test.ini", level, money, score)
    {
        printf("Level: %d", level);
        printf("Money: %d", money);
        printf("Score: %d", score);
    }
    return 1;
}

And the specifiers are automatically assumed. The variables are then destroyed after the code block ends.

It works for strings, too:

pawn Code:
public OnGameModeInit()
{
    import File("mysql.ini", host[32], user[32], database[32], password[32])
    {
        mysql_connect(host, user, database, password);
    }
    return 1;
}

Basically, you can have as many parameters as you would like! Do not exceed 128 parameters, though, since that is a PAWN limitation and I have no control over it.

Searching method
First, the file is opened and a common non-alphanumeric delimiter is searched for:

Code:
1000|2000|This|is|my|file
If there is no line after that one, then it is automatically assumed the only line in the file, and uses '|' as the delimiter. I figured it would fit in since most people save files using this method (don't ask me why, it's confusing!).

After that, it detects if sscanf2 is included, and if so, forwards the parameters and the specifiers over to the native function. This also works without sscanf, but it's recommended that you use it for more reliability.

If no delimiter is found, the file is automatically assumed to be an INI file.

Specifiers
The specifier system is very clever, and has been built to be reliable as much as possible. Say we have this file:

Code:
1000|2000|This|is|my|string|3000|4000
We can simply do this to fetch the entire string:

pawn Code:
import File("file.ini", str[64])
{
    // ...
}

Outputs:

Code:
1000|2000|This|is|my|string|3000|4000
This code:

pawn Code:
import File("file.ini", str[64], str2[64])
{
    // ...
}

Outputs:

Code:
1000
2000|This|is|my|string|3000|4000
This code:

pawn Code:
import File("file.ini", str[64], str2[64], str3[64])
{
    // ...
}

Outputs:

Code:
1000
2000
This|is|my|string|3000|4000
When the last parameter is a string, it will try to append any remaining file data into that string, depending if the size is sufficient or not.

Exporting
You can also use the "export" keyword, which is exactly the same as "import" but saves the values inside global variables, instead of creating and saving them locally:

pawn Code:
new
    a,
    Float:b,
    c[32];

public OnFilterScriptInit()
{
    export File("file.ini", a, Float:b, c[32])
    {
        printf("%d, %.4f, %s", a, b, c);
    }
    return 1;
}

Parsing notes
The delimiter search is not always that reliable. For example, we could have this file:

Code:
1000|2000|This|is|my|string|3000|4000
But what if the highlighted characters are PART of the string, and not delimiters? We can simply workaround this by escaping them:

Code:
1000|2000|This\|is\|my\|string|3000|4000
However, if you are using sscanf2 with this system then this is not a problem:

pawn Code:
#include <sscanf2>
#include <file-import>

Credits
******: Macro used to determine parameter types, and sscanf2 (took me 2 days of tweaking to write the "import" macro lol).
Pastebin: FOR KILLING MY INDENTATION

Downloads
Pastebin

Last edited by Emmet_; 01/03/2014 at 12:58 AM.
  Reply With Quote
Old 28/02/2014, 06:06 AM   #2
Gnabry
Little Clucker
 
Join Date: Feb 2014
Posts: 30
Reputation: 5
Default Re: Inline file loading

amazing man...
U r just a genious
Gnabry is offline   Reply With Quote
Old 28/02/2014, 06:08 AM   #3
Lordzy
High-roller
 
Lordzy's Avatar
 
Join Date: Mar 2012
Location: NetherRealm
Posts: 2,698
Reputation: 1177
Default Re: Inline file loading

Looks great! I'll give it a try when I get home, nice work EMMAT.
__________________
Currently inactive - I don't play at any SA-MP servers nor work on anything in PAWN for now. The projects that I've done so far in PAWN, which requires updates will be taking some time.
Lordzy is offline   Reply With Quote
Old 28/02/2014, 06:10 AM   #4
ARamsey
Little Clucker
 
Join Date: Feb 2014
Posts: 4
Reputation: 1
Default Re: Inline file loading

this wll save loads of my time
ARamsey is offline   Reply With Quote
Old 28/02/2014, 11:16 AM   #5
Sky™
High-roller
 
Sky™'s Avatar
 
Join Date: Aug 2012
Location: Apple Computer™
Posts: 1,782
Reputation: 351
Default Re: Inline file loading

Congratulations for the work

Quote:
Originally Posted by Emmet_ View Post
FOR KILLING MY INDENTATION
is good anyway
__________________
Sky™ is offline   Reply With Quote
Old 28/02/2014, 12:34 PM   #6
iZN
High-roller
 
Join Date: Jun 2010
Posts: 2,368
Reputation: 569
Default Re: Inline file loading

Great work as always Emmet
__________________
iZN is offline   Reply With Quote
Old 28/02/2014, 02:02 PM   #7
CriticalRP
Little Clucker
 
Join Date: Apr 2013
Posts: 38
Reputation: -7
Default Re: Inline file loading

It seem not working. Am I doing something wrong by testing it out with your test.ini's sample?
CriticalRP is offline   Reply With Quote
Old 28/02/2014, 02:19 PM   #8
PT
Godfather
 
PT's Avatar
 
Join Date: Nov 2012
Location: Europe
Posts: 7,185
Reputation: 803
Default Re: Inline file loading

Very, very usefull nice job.
PT is offline   Reply With Quote
Old 28/02/2014, 02:27 PM   #9
Whitetiger
Huge Clucker
 
Whitetiger's Avatar
 
Join Date: Nov 2007
Posts: 406
Reputation: 156
Default Re: Inline file loading

I can do all these things BETTER with sscanf!
__________________
GitHub

Author of SA-MP AC

Whitetiger is offline   Reply With Quote
Old 28/02/2014, 05:51 PM   #10
Emmet_
Guest
 
Posts: n/a
Default Re: Inline file loading

Thanks guys.

Quote:
Originally Posted by CriticalRP View Post
It seem not working. Am I doing something wrong by testing it out with your test.ini's sample?
Works fine for me, what code are you trying to use?

Quote:
Originally Posted by Whitetiger View Post
I can do all these things BETTER with sscanf!
As I said, this is an experimental include, just to test out if the concept would actually work. I was thinking about adding support for sscanf2, but I would need to pass referenced parameters to a native function, but for some reason I can't get it working!
  Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading File ? park4bmx Scripting Help 3 13/04/2012 10:55 AM
Loading the next file. milanosie Scripting Help 3 27/01/2012 05:23 PM
Loading Something From A File Tommy_Mandaz Help Archive 10 30/06/2011 04:07 PM
Re-loading a file? Skylar Paul Help Archive 1 29/05/2011 05:37 AM
File Loading (Jeff) Help Archive 1 21/10/2009 01:58 PM


All times are GMT. The time now is 04:42 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.