Parser (JSON Reader)
Download
View CPP File
This module will allow you to parse files more easily, like listed below.
This is very recommended when you want to precache files, enumerate swears and set entity variables such viewmodel.
Linux version will be done ASAP.
configuration.ini file.
PHP Code:
{
"encoding" : "UTF8",
"latitude" : -21.512312,
"maxplayers" : 128,
"plugins" : [
"This1",
"This2"
]
}
parser.sma file.
PHP Code:
#include <amxmodx>
#include <parser>
public plugin_init()
{
new parserFile:fileId;
new gameName[16], configsDir[128], fileName[256];
new encodingString[32], Float:cityLatitude, maxPlayers;
get_modname(gameName, charsmax(gameName));
get_localinfo("amxx_configsdir", configsDir, charsmax(configsDir));
formatex(fileName, charsmax(fileName), "%s/%s/configuration.ini", gameName, configsDir);
/** If file does not exist, plugin_init forward will stop here.
*/
fileId = parserLoadFile(fileName);
/** Reads all plugins.
*/
parserEnumData(fileId, "plugins");
parserGetString(fileId, "encoding", encodingString, charsmax(encodingString));
cityLatitude = parserGetFloat(fileId, "latitude");
maxPlayers = parserGetInt(fileId, "maxplayers");
server_print("Encoding = %s, Latitude = %f, MaxPlayers = %d", encodingString, cityLatitude, maxPlayers);
}
public parserEnumForward(parserFile:fileIndex, keyName[], bufferSet[])
{
/** Prints to server all plugins.
*/
server_print(bufferSet);
}
parser.inc file.
PHP Code:
#if !defined PARSER_INC
#define PARSER_INC
#include <amxmodx>
#if AMXX_VERSION_NUM >= 175
#pragma reqlib parser
#if !defined AMXMODX_NOAUTOLOAD
#pragma loadlib parser
#endif
#else
#pragma library parser
#endif
/** Example of file to be parsed.
*
* {
* "encoding" : "utf-8",
* "maxPlayers" : 32,
* "latitude" : 51.213341,
*
* "plugins" : [
* "admin.amxx",
* "admincmd.amxx",
* "adminvote.amxx"
* ]
* }
*/
/** Prints all items from a named category.
*
* @param fileIndex The file that the information is taken from.
* @param keyName The key.
* @param bufferSet The buffer taken.
*
* @return Does not matter.
*/
forward parserEnumForward(parserFile:fileIndex, keyName[], bufferSet[]);
/** Parses a file.
*
* @param fileName The file. If you need a path, the game name should be added too.
*
* @return Does not matter.
*/
native parserFile:parserLoadFile(const fileName[]);
/** Enumerates all data from a parsed file regarding the key name.
*
* @param fileIndex The file that the information is taken from.
* @param keyName The key.
*
* @return Does not matter.
*/
native parserEnumData(const parserFile:fileIndex, const keyName[]);
/** Retrieves a string by key.
*
* @param fileIndex The file that the information is taken from.
* @param keyName The key.
* @param stringBuffer The buffer to be filled.
* @param stringSize The size of buffer
*
* @return Does not matter.
*/
native parserGetString(const parserFile:fileIndex, const keyName[], stringBuffer[], const stringSize);
/** Retrieves a numerical value by key.
*
* @param fileIndex The file that the information is taken from.
* @param keyName The key.
*
* @return The numerical value.
*/
native parserGetInt(const parserFile:fileIndex, const keyName[]);
/** Retrieves a float value by key.
*
* @param fileIndex The file that the information is taken from.
* @param keyName The key.
*
* @return The float value.
*/
native Float:parserGetFloat(const parserFile:fileIndex, const keyName[]);
#endif
__________________