Inline file loading - Emmet_ - 28.02.2014
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
Re: Inline file loading -
Gnabry - 28.02.2014
amazing man...
U r just a genious
Re: Inline file loading -
Lordzy - 28.02.2014
Looks great! I'll give it a try when I get home, nice work EMMAT.
Re: Inline file loading -
ARamsey - 28.02.2014
this wll save loads of my time
Re: Inline file loading -
Sky™ - 28.02.2014
Congratulations for the work
Quote:
Originally Posted by Emmet_
FOR KILLING MY INDENTATION 
|
is good anyway
Re: Inline file loading -
iZN - 28.02.2014
Great work as always Emmet
Re: Inline file loading -
CriticalRP - 28.02.2014
It seem not working. Am I doing something wrong by testing it out with your test.ini's sample?
Re: Inline file loading -
PT - 28.02.2014
Very, very usefull nice job.
Re: Inline file loading -
Whitetiger - 28.02.2014
I can do all these things BETTER with sscanf!
Re: Inline file loading - Emmet_ - 28.02.2014
Thanks guys.
Quote:
Originally Posted by CriticalRP
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
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!
Re: Inline file loading -
SkittlesAreFalling - 28.02.2014
How would you save to the file?
Normal file functions or are you planning on making a similar inline macro?
Re: Inline file loading - Emmet_ - 28.02.2014
Quote:
Originally Posted by SkittlesAreFalling
How would you save to the file?
Normal file functions or are you planning on making a similar inline macro?
|
Normal file functions, for now. I suppose I can develop a syntax such as:
pawn Code:
new
value,
Float:fValue,
str[32];
append File("file.ini", "Field", value, "Float", fValue, "String", str)
{
print("File saved!");
}
But that's what y_ini is designed to do. This is designed to work with INI files and special syntax files (e.g. "files|like|this").
Re: Inline file loading -
SkittlesAreFalling - 28.02.2014
Okay.
I think it'll be a good idea to make an append, really helpful for lazy people! lol
Great work man.
Edit:
Looking over your function, I believe I can make the append for you if you want.
Re: Inline file loading -
Scaleta - 28.02.2014
Don't you mean eInline File Loading? Jokes.
This could be useful for small things like quick config files. Awesome!
Re: Inline file loading -
Ada32 - 28.02.2014
don't just have an idea and implement so loosely.. the concept itself is bad. and why is import a keyword?
Re: Inline file loading - Emmet_ - 28.02.2014
Quote:
Originally Posted by SkittlesAreFalling
Okay.
I think it'll be a good idea to make an append, really helpful for lazy people! lol
Great work man.
Edit:
Looking over your function, I believe I can make the append for you if you want.
|
Sure thing, but it might be a bit complicated (e.g. it might require a few assembly instructions).
Quote:
Originally Posted by Scaleta
Don't you mean eInline File Loading? Jokes.
This could be useful for small things like quick config files. Awesome!
|
Haha, thanks! I was thinking of adding the "e" in there, like I did with my previous libraries
Quote:
Originally Posted by Ada32
don't just have an idea and implement so loosely.. the concept itself is bad. and why is import a keyword?
|
I've actually been working on the idea for a while. At first it was a global keyword:
pawn Code:
#include <a_samp>
new
a,
Float:b,
c[24];
fload LoadFile["test.ini"](a, Float:b, c[24]);
public OnFilterScriptInit()
{
return 1;
}
But it just looked too tacky. I resorted to creating an inline "keyword" so it can all be controlled in a single compound block. I chose "import" because it was the only appropriate word for it (think of it as a way.. "importing" a file and read the data). Some other previous keywords were "Load_File", "File_Load" and "loadfile" but they all looked weird.
Re: Inline file loading -
CriticalRP - 28.02.2014
Quote:
Originally Posted by Emmet_
Works fine for me, what code are you trying to use?
|
I'm using your code. The one with test.ini
Re: Inline file loading - Emmet_ - 01.03.2014
Quote:
Originally Posted by CriticalRP
I'm using your code. The one with test.ini
|
I've updated the link, try now.
I also need to know which OS you're running.
Code:
Updates:
- The delimiter parser now detects the sscanf2 plugin and uses it accordingly!
- Fixed a few minor bugs with the parser.
Re: Inline file loading -
CriticalRP - 01.03.2014
Quote:
Originally Posted by Emmet_
I've updated the link, try now.
I also need to know which OS you're running.
Code:
Updates:
- The delimiter parser now detects the sscanf2 plugin and uses it accordingly!
- Fixed a few minor bugs with the parser.
|
Windows 7 Home Premium 64bit
Re: Inline file loading -
fiki574 - 02.03.2014
Jesus Christ, this is sick! Awesome work.