28.02.2014, 06:03
(
Last edited by Emmet_; 01/03/2014 at 12:58 AM.
)
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):
We can simply use this code to fetch the data:
And the specifiers are automatically assumed. The variables are then destroyed after the code block ends.
It works for strings, too:
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:
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:
We can simply do this to fetch the entire string:
Outputs:
This code:
Outputs:
This code:
Outputs:
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:
Parsing notes
The delimiter search is not always that reliable. For example, we could have this file:
But what if the highlighted characters are PART of the string, and not delimiters? We can simply workaround this by escaping them:
However, if you are using sscanf2 with this system then this is not a problem:
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
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
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;
}
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;
}
Searching method
First, the file is opened and a common non-alphanumeric delimiter is searched for:
Code:
1000|2000|This|is|my|file
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
pawn Code:
import File("file.ini", str[64])
{
// ...
}
Code:
1000|2000|This|is|my|string|3000|4000
pawn Code:
import File("file.ini", str[64], str2[64])
{
// ...
}
Code:
1000 2000|This|is|my|string|3000|4000
pawn Code:
import File("file.ini", str[64], str2[64], str3[64])
{
// ...
}
Code:
1000 2000 This|is|my|string|3000|4000
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;
}
The delimiter search is not always that reliable. For example, we could have this file:
Code:
1000|2000|This|is|my|string|3000|4000
Code:
1000|2000|This\|is\|my\|string|3000|4000
pawn Code:
#include <sscanf2>
#include <file-import>
******: 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