Merge pull request #101 from Arkshine/textparsers

Introduce TextParser API
This commit is contained in:
Vincent Herbet
2014-08-07 01:24:51 +02:00
19 changed files with 3235 additions and 2 deletions

View File

@ -27,6 +27,8 @@
#include <celltrie>
#include <datapack>
#include <newmenus>
#include <textparse_smc>
#include <textparse_ini>
/* Function is called just after server activation.
* Good place for configuration loading, commands and cvars registration. */

View File

@ -0,0 +1,206 @@
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
//
// INI Parser Functions
//
#if defined _textparse_ini_included
#endinput
#endif
#define _textparse_ini_included
/**
* This parser API is entirely event based.
* You must hook events to receive data.
*/
/**
* The INI file format is defined as:
* WHITESPACE: 0x20, \n, \t, \r
* IDENTIFIER: A-Z a-z 0-9 _ - , + . $ ? /
* STRING : Any set of symbols
*
* Basic syntax is comprised of SECTIONs.
* A SECTION is defined as:
* [SECTIONNAME]
* OPTION
* OPTION
* OPTION...
*
* SECTIONNAME is an IDENTIFIER.
* OPTION can be repeated any number of times, once per line.
* OPTION is defined as one of:
* KEY = "VALUE"
* KEY = VALUE
* KEY
* Where KEY is an IDENTIFIER and VALUE is a STRING.
*
* WHITESPACE should always be omitted.
* COMMENTS should be stripped, and are defined as text occurring in:
* ;<TEXT>
*
* Example file below. Note that the second line is technically invalid.
* The event handler must decide whether this should be allowed.
* --FILE BELOW--
* [gaben]
* hi = clams
* bye = "NO CLAMS"
*
* [valve]
* cannot
* maintain
* products
*/
/**
* Parser invalid code.
*/
enum INIParser
{
Invalid_INIParser = 0
};
/**
* Creates a new INI parser.
* This is used to set parse hooks.
*
* @return A new handle to an INI Parse structure.
*/
native INIParser:INI_CreateParser();
/**
* Disposes of an INI parser.
*
* @param handle Handle to an INI Parse structure.
*
* @return True if disposed, false otherwise.
*/
native INI_DestroyParser(&INIParser:handle);
/**
* Parses an INI config file.
*
* @param handle A handle to an INI Parse structure.
* @param file A string containing the file path.
* @param line An optional by reference cell to store the last line number read.
* @param col An optional by reference cell to store the last column number read.
* @return An SMCParseError result.
* @error Invalid or corrupt handle.
*/
native bool:INI_ParseFile(INIParser:handle, const file[], &line = 0, &col = 0);
/**
* Sets the INI_ParseStart function of a parse handle.
*
* @note Below is the prototype of callback:
* -
* Called when parsing is started.
*
* @param handle A handle to an INI Parse structure.
*
* @noreturn
*
* public OnParseStart(INIParser:handle)
* -
* @param handle Handle to an INI Parse structure.
* @param func A ParseStart callback.
*
* @noreturn
* @error Invalid or corrupt handle.
*/
native INI_SetParseStart(INIParser:handle, const func[]);
/**
* Sets the INI_ParseEnd function of a parse handle.
*
* @note Below is the prototype of callback:
* -
* Called when parsing is halted.
*
* @param handle A handle to an INI Parse structure.
* @param halted True if abnormally halted, false otherwise.
*
* @noreturn
*
* public OnParseEnd(INIParser:handle, bool:halted)
* -
* @param handle Handle to an INI Parse structure.
* @param func A ParseEnd callback.
*
* @noreturn
* @error Invalid or corrupt handle.
*/
native INI_SetParseEnd(INIParser:handle, const func[]);
/**
* Sets the two main reader functions.
*
* @note Below is the prototype of callback:
* -
* NewSection:
* Called when the parser finds a new section.
*
* @param handle Handle to an INI Parse structure.
* @param section Name of section in between the [ and ] characters.
* @param invalid_tokens True if invalid tokens were detected in the name.
* @param close_bracket True if a closing bracket was detected, false otherwise.
* @param extra_tokens True if extra tokens were detected on the line.
* @param curtok Contains current token in the line where the section name starts.
* You can add to this offset when failing to point to a token.
* @return True to keep parsing, false otherwise.
*
* public bool:OnNewSection(INIParser:handle, const section[], bool:invalid_tokens, bool:close_bracket, bool:extra_tokens, curtok)
*
* KeyValue:
* Called when the parser finds a new key/value pair.
*
* @param handle Handle to an INI Parse structure.
* @param key Name of key.
* @param value String containing value (with quotes stripped, if any).
* @param invalid_tokens Whether or not the key contained invalid tokens.
* @param equal_token There was an '=' sign present (in case the value is missing).
* @param quotes Whether value was enclosed in quotes.
* @param curtok Contains the token index of the start of the value string.
* This can be changed when returning false.
* @return True to keep parsing, false otherwise.
*
* public bool:OnKeyValue(INIParser:handle, const key[], const value[], bool:invalid_tokens, bool:equal_token, bool:quotes, curtok)
* -
* @param handle Handle to an INI Parse structure.
* @param kv A KeyValue callback.
* @param ns An optional NewSection callback.
*
* @noreturn
*/
native INI_SetReaders(INIParser:smc, const kvFunc[], const nsFunc[] = "" );
/**
* Sets a raw line reader on an INI parser handle.
*
* @note Below is the prototype of callback:
* -
* Called whenever a raw line is read.
*
* @param handle The INI Parse handle.
* @param line Contents of line.
* @param lineno The line number it occurs on.
* @param curtok Pointer to optionally store failed position in string.
*
* @return True to keep parsing, false otherwise.
*
* public bool:OnRawLine(INIParser:smc, const line[], lineno, curtok)
*
* @param handle Handle to an INI Parse structure.
* @param func A RawLine callback.
*
* @noreturn
*/
native INI_SetRawLine(INIParser:handle, const func[]);

View File

@ -0,0 +1,254 @@
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
//
// SMC Parser Functions
//
#if defined _textparse_smc_included
#endinput
#endif
#define _textparse_smc_included
/**
* Everything below describes the SMC Parse, or "SourceMod Configuration" format.
* This parser is entirely event based. You must hook events to receive data.
* The file format itself is nearly identical to Valve's KeyValues format (also known as VDF).
*/
/**
* The SMC file format is defined as:
* WHITESPACE: 0x20, \n, \t, \r
* IDENTIFIER: Any ASCII character EXCLUDING ", {, }, ;, //, / *, or WHITESPACE.
* STRING : Any set of symbols enclosed in quotes.
*
* Note: if a STRING does not have quotes, it is parsed as an IDENTIFIER.
*
* Basic syntax is comprised of SECTIONBLOCKs.
* A SECTIONBLOCK defined as:
*
* SECTIONNAME
* {
* OPTION
* }
*
* OPTION can be repeated any number of times inside a SECTIONBLOCK.
* A new line will terminate an OPTION, but there can be more than one OPTION per line.
* OPTION is defined any of:
* "KEY" "VALUE"
* SECTIONBLOCK
*
* SECTIONNAME, KEY, VALUE, and SINGLEKEY are strings
* SECTIONNAME cannot have trailing characters if quoted, but the quotes can be optionally removed.
* If SECTIONNAME is not enclosed in quotes, the entire sectionname string is used (minus surrounding whitespace).
* If KEY is not enclosed in quotes, the key is terminated at first whitespace.
* If VALUE is not properly enclosed in quotes, the entire value string is used (minus surrounding whitespace).
* The VALUE may have inner quotes, but the key string may not.
*
* For an example, see scripting/testsuite/textparse_test.cfg
*
* WHITESPACE should be ignored.
* Comments are text occurring inside the following tokens, and should be stripped
* unless they are inside literal strings:
* ;<TEXT>
* //<TEXT>
* / *<TEXT> * /
*/
/**
* Parser invalid code.
*/
enum SMCParser
{
Invalid_SMCParser = 0
};
/**
* Parse result directive.
*/
enum SMCResult
{
SMCParse_Continue, /* Continue parsing */
SMCParse_Halt, /* Stop parsing here */
SMCParse_HaltFail /* Stop parsing and return failure */
};
/**
* Parse error codes.
*/
enum SMCError
{
SMCError_Okay = 0, /* No error */
SMCError_StreamOpen, /* Stream failed to open */
SMCError_StreamError, /* The stream died... somehow */
SMCError_Custom, /* A custom handler threw an error */
SMCError_InvalidSection1, /* A section was declared without quotes, and had extra tokens */
SMCError_InvalidSection2, /* A section was declared without any header */
SMCError_InvalidSection3, /* A section ending was declared with too many unknown tokens */
SMCError_InvalidSection4, /* A section ending has no matching beginning */
SMCError_InvalidSection5, /* A section beginning has no matching ending */
SMCError_InvalidTokens, /* There were too many unidentifiable strings on one line */
SMCError_TokenOverflow, /* The token buffer overflowed */
SMCError_InvalidProperty1, /* A property was declared outside of any section */
};
/**
* Creates a new SMC parser.
* This is used to set parse hooks.
*
* @return A new handle to an SMC Parse structure.
*/
native SMCParser:SMC_CreateParser();
/**
* Disposes of an SMC parser.
*
* @param handle Handle to an SMC Parse structure.
*
* @return True if disposed, false otherwise.
*/
native SMC_DestroyParser(&SMCParser:handle);
/**
* Parses a config file.
*
* @param handle A handle to an SMC Parse structure.
* @param file A string containing the file path.
* @param line An optional by reference cell to store the last line number read.
* @param col An optional by reference cell to store the last column number read.
*
* @return An SMCParseError result.
* @error Invalid or corrupt handle.
*/
native SMCError:SMC_ParseFile(SMCParser:handle, const file[], &line = 0, &col = 0);
/**
* Sets the SMC_ParseStart function of a parse handle.
*
* @note Below is the prototype of callback:
* -
* Called when parsing is started.
*
* @param handle Handle to an SMC Parse structure.
*
* @noreturn
*
* public OnParseStart(SMCParser:handle)
* -
* @param handle Handle to an SMC Parse structure.
* @param func A ParseStart callback.
*
* @noreturn
* @error Invalid or corrupt handle.
*/
native SMC_SetParseStart(SMCParser:handle, const func[]);
/**
* Sets the SMC_ParseEnd function of a parse handle.
*
* @note Below is the prototype of callback:
* -
* Called when parsing is halted.
*
* @param handle Handle to an SMC Parse structure.
* @param halted True if abnormally halted, false otherwise.
* @param failed True if parsing failed, false otherwise.
*
* @noreturn
*
* public OnParseEnd(SMCParser:handle, bool:halted, bool:failed)
* -
* @param handle Handle to an SMC Parse structure.
* @param func A ParseEnd callback.
*
* @noreturn
* @error Invalid or corrupt handle.
*/
native SMC_SetParseEnd(SMCParser:handle, const func[]);
/**
* Sets the three main reader functions.
*
* @note Enclosing quotes are always stripped.
* @note Below is the prototype of callbacks:
* -
* NewSection:
* Called when the parser finds a new section or sub-section.
*
* @param handle Handle to an SMC Parse structure.
* @param name String containing section name.
*
* @return An SMCResult action to take.
*
* public SMCResult:OnNewSection(SMCParser:handle, const name[])
*
* KeyValue:
* Called when the parser finds a new key/value pair.
*
* @param handle Handle to an SMC Parse structure.
* @param key String containing key name.
* @param value String containing value name.
*
* @return An SMCResult action to take.
*
* public SMCResult:OnKeyValue(SMCParser:handle, const key[], const value[])
*
* EndSection:
* Called when the parser finds the end of the current section.
*
* @param handle Handle to an SMC Parse structure.
*
* @return An SMCResult action to take.
*
* public SMCResult:OnEndSection(SMCParser:handle)
* -
* @param handle Handle to an SMC Parse structure.
* @param kv A KeyValue callback.
* @param ns An optional NewSection callback.
* @param es An optional EndSection callback.
*
* @noreturn
*/
native SMC_SetReaders(SMCParser:smc, const kvFunc[], const nsFunc[] = "", const esFunc[] = "");
/**
* Sets a raw line reader on an text parser handle.
*
* @note Below is the prototype of callbacks:
* -
* Called whenever a raw line is read.
*
* @param handle Handle to an SMC Parse structure.
* @param line A string containing the raw line from the file.
* @param lineno The line number it occurs on.
*
* @return An SMCResult action to take.
*
* public SMCResult:SMC_RawLine(SMCParser:handle, const line[], lineno)
* -
* @param handle Handle to an SMC Parse structure.
* @param func A RawLine callback.
*
* @noreturn
*/
native SMC_SetRawLine(SMCParser:handle, const func[]);
/**
* Gets an error string for an SMCError code.
*
* @note SMCError_Okay returns false.
* @note SMCError_Custom (which is thrown on SMCParse_HaltFail) returns false.
*
* @param error The SMCParseError code.
* @param buffer A string buffer for the error (contents undefined on failure).
* @param buf_max The maximum size of the buffer.
*
* @return True on success, false otherwise.
*/
native bool:SMC_GetErrorString(SMCError:error, buffer[], buf_max);

View File

@ -0,0 +1,135 @@
/**
* This file is used to set various options that are important to SourceMod's core.
* If this file is missing or an option in this file is missing, then the default values will be used.
*/
"Core"
{
/**
* This option determines if SourceMod logging is enabled.
*
* "on" - Logging is enabled (default)
* "off" - Logging is disabled
*/
"Logging" "on"
/**
* This option determines how SourceMod logging should be handled.
*
* "daily" - New log file is created for each day (default)
* "map" - New log file is created for each map change
* "game" - Use game's log files
*/
"LogMode" "daily"
/**
* Language that multilingual enabled plugins and extensions will use to print messages.
* Only languages listed in languages.cfg are valid.
*
* The default value is "en"
*/
"ServerLang" "en"
/**
* String to use as the public chat trigger. Set an empty string to disable.
*/
"PublicChatTrigger" "!"
/**
* String to use as the silent chat trigger. Set an empty string to disable.
*/
"SilentChatTrigger" "/"
/**
* If a say command is a silent chat trigger, and is used by an admin,
* but it does not evaluate to an actual command, it will be displayed
* publicly. This setting allows you to suppress accidental typings.
*
* The default value is "no". A value of "yes" will supress.
*/
"SilentFailSuppress" "no"
/**
* Password setinfo key that clients must set. You must change this in order for
* passwords to work, for security reasons.
*/
"PassInfoVar" "_password"
/**
* Specifies the sound that gets played when an item is selected from a menu.
*/
"MenuItemSound" "buttons/button14.wav"
/**
* Specifies the sound that gets played when an "Exit" button is selected
* from a menu.
*/
"MenuExitSound" "buttons/combine_button7.wav"
/**
* Specifies the sound that gets played when an "Exit Back" button is selected
* from a menu. This is the special "Back" button that is intended to roll back
* to a previous menu.
*/
"MenuExitBackSound" "buttons/combine_button7.wav"
/**
* Enables or disables whether SourceMod reads a client's cl_language cvar to set
* their language for server-side phrase translation.
*
* "on" - Translate using the client's language (default)
* "off" - Translate using default server's language
*/
"AllowClLanguageVar" "On"
/**
* Enables or Disables SourceMod's automatic gamedata updating.
*
* The default value is "no". A value of "yes" will block the Auto Updater.
*/
"DisableAutoUpdate" "no"
/**
* If set to yes, a successful gamedata update will attempt to restart SourceMod.
* SourceMod is unloaded and reloaded, and the map is changed to the current map.
* Since gamedata updates occur when the server loads, impact should be minimal.
* But to be safe, this option is disabled by default.
*/
"ForceRestartAfterUpdate" "no"
/**
* URL to use for retrieving update information.
* SSL is not yet supported.
*/
"AutoUpdateURL" "http://update.sourcemod.net/update/"
/**
* Whether to show debug spew.
* Currently this will log details about the gamedata updating process.
*/
"DebugSpew" "no"
/**
* If set to yes, SourceMod will validate steamid auth strings with the Steam backend before giving out admin access.
* This can prevent malicious users from impersonating admins with stolen Steam apptickets.
* If Steam is down, admins will not be authenticated until Steam comes back up.
* This option increases the security of your server, but is still experimental.
*/
"SteamAuthstringValidation" "yes"
/**
* Enables or disables whether SourceMod blocks known or potentially malicious plugins from loading.
* It is STRONGLY advised that this is left enabled, there have been cases in the past with plugins that
* allow anyone to delete files on the server, gain full rcon control, etc.
*
* "yes" - Block malware or illegal plugins from loading (default)
* "no" - Warn about malware or illegal plugins loading
*/
"BlockBadPlugins" "yes"
/**
* If a plugin takes too long to execute, hanging or freezing the game server in the process,
* SourceMod will attempt to terminate that plugin after the specified timeout length has
* passed. You can disable this feature by setting the value to "0".
*/
"SlowScriptTimeout" "8"
}

View File

@ -0,0 +1,68 @@
;CSDM Configuration File
; Default settings by BAILOPAN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;You must be running the Main plugin for this section
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[settings]
;Sets whether CSDM is enabled or not.
enabled = 1
;Sets whether or not players should be stripped of weapons on round start
; (excludes knife)
strip_weapons = 1
;Sets how long weapons should stay on the ground for after being dropped
;in seconds. note that enabling this can create lots of lag for clients
; AND server. 0 is immediate, -1 is infinite.
weapons_stay = 0
;Sets the spawn mode.
; "none" - users spawn at normal map spawn points
; "preset" - csdm_spawn_preset.amxx required, uses predefined spawns in config files
; -- others may be supplied by 3rd party plugins
spawnmode = preset
;Sets whether the bomb is removed
remove_bomb = 1
;Sets the spawn waiting time
spawn_wait_time = 0.75
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;You must be running the protection plugin for this section
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[protection] ; some comment
;Is spawn protection enabled?
enabled = 1
;Colors of glow shell, leave this in quotes
;The digits are R, G, B, A where A is the alpha transparency
; (as A gets higher, the glow shell is thicker)
colors = "0 255 0 200"
;Number of seconds someone is respawned for.
time = 2
;;;;;;;;;;;;;;;;
;;WEAPON MENUS;;
;;;;;;;;;;;;;;;;
;Format for weapon menus is:
;shortname "Display Name" menupage
;Change the '1' to a '0' to block the weapon
;Removing or moving things
; from the list will change the order of the menus!
[secondary] gabe // < just to test param.
usp USP 1
glock18 Glock 1
deagle Deagle 1
;List weapons here the bots can randomly have
;The short name must match one in the list above
[botsecondary
deagle
usp

View File

@ -0,0 +1,253 @@
/**
* vim: set ts=4 sw=4 tw=99 noet:
*
* AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
* Copyright (C) The AMX Mod X Development Team.
*
* This software is licensed under the GNU General Public License, version 3 or higher.
* Additional exceptions apply. For full license details, see LICENSE.txt or visit:
* https://alliedmods.net/amxmodx-license
*/
#include <amxmodx>
new SuccessCount;
new Trie:ExpectedKVData;
new bool:Debug;
public plugin_init()
{
register_concmd("textparse", "ConsoleCommand_TextParse");
Debug = !!(plugin_flags() & AMX_FLAG_DEBUG);
}
public ConsoleCommand_TextParse()
{
InitializeTextParseSMC();
InitializeTextParseINI();
}
/**
* SMC Config format
*/
InitializeTextParseSMC()
{
SuccessCount = 0;
server_print("Testing text parser with SMC config file format...");
new const configFile[] = "addons/amxmodx/scripting/testsuite/textparse_test.cfg";
ExpectedKVData = TrieCreate();
TrieSetString(ExpectedKVData, "Logging", "on");
TrieSetString(ExpectedKVData, "LogMode", "daily");
TrieSetString(ExpectedKVData, "ServerLang", "en");
TrieSetString(ExpectedKVData, "PublicChatTrigger", "!");
TrieSetString(ExpectedKVData, "SilentChatTrigger", "/");
TrieSetString(ExpectedKVData, "SilentFailSuppress", "no");
TrieSetString(ExpectedKVData, "PassInfoVar", "_password");
TrieSetString(ExpectedKVData, "MenuItemSound", "buttons/button14.wav");
TrieSetString(ExpectedKVData, "MenuExitSound", "buttons/combine_button7.wav");
TrieSetString(ExpectedKVData, "MenuExitBackSound", "buttons/combine_button7.wav");
TrieSetString(ExpectedKVData, "AllowClLanguageVar", "On");
TrieSetString(ExpectedKVData, "DisableAutoUpdate", "no");
TrieSetString(ExpectedKVData, "ForceRestartAfterUpdate", "no");
TrieSetString(ExpectedKVData, "AutoUpdateURL", "http://update.sourcemod.net/update/");
TrieSetString(ExpectedKVData, "DebugSpew", "no");
TrieSetString(ExpectedKVData, "SteamAuthstringValidation", "yes");
TrieSetString(ExpectedKVData, "BlockBadPlugins", "yes");
TrieSetString(ExpectedKVData, "SlowScriptTimeout", "8");
new const expectedSectionCount = 2; // Include start and end.
new const expectedStartEndCount = 2;
new const expectedKeyValueCount = TrieGetSize(ExpectedKVData);
new const expectedLineCount = file_size(configFile, .flag = 1) - 1;
new SMCParser:parser = SMC_CreateParser();
SMC_SetReaders(parser, "ReadCore_KeyValue", "ReadCore_NewSection", "ReadCore_EndSection");
SMC_SetParseStart(parser, "ReadCore_ParseStart");
SMC_SetRawLine(parser, "ReadCore_CurrentLine");
SMC_SetParseEnd(parser, "ReadCore_ParseEnd");
new line, col;
new SMCError:err = SMC_ParseFile(parser, configFile, line, col);
if (err != SMCError_Okay)
{
new buffer[64];
server_print("%s", SMC_GetErrorString(err, buffer, charsmax(buffer)) ? buffer : "Fatal parse error");
}
if (line == expectedLineCount + 1 && col == 2)
{
++SuccessCount;
}
server_print("^tTests successful: %d/%d", SuccessCount, expectedStartEndCount + expectedSectionCount + expectedKeyValueCount + expectedLineCount + 1);
SMC_DestroyParser(parser);
TrieDestroy(ExpectedKVData);
SuccessCount = 0;
}
public ReadCore_ParseStart(SMCParser:handle)
{
Debug && server_print("ReadCore_ParseStart");
++SuccessCount;
}
public ReadCore_NewSection(SMCParser:handle, const name[])
{
Debug && server_print("^tReadCore_NewSection - %s", name);
++SuccessCount;
}
public ReadCore_KeyValue(SMCParser:handle, const key[], const value[])
{
Debug && server_print("^t^tReadCore_KeyValue - %-32s %s", key, value);
new buffer[128];
if (TrieGetString(ExpectedKVData, key, buffer, charsmax(buffer)) && equal(value, buffer))
{
++SuccessCount;
}
}
public ReadCore_EndSection(SMCParser:handle)
{
Debug && server_print("^tReadCore_EndSection");
++SuccessCount;
}
public ReadCore_CurrentLine(SMCParser:handle, const line[], lineno)
{
//Debug && server_print("^t^tReadCore_CurrentLine - %s", line);
++SuccessCount;
}
public ReadCore_ParseEnd(SMCParser:handle, bool:halted, bool:failed)
{
Debug &&server_print("ReadCore_ParseEnd - halted: %s, failed: %s", halted ? "yes" : "no", failed ? "yes" : "no");
++SuccessCount;
}
/**
* INI Config format
*/
public InitializeTextParseINI()
{
SuccessCount = 0;
server_print("Testing text parser with INI config file format...");
new const configFile[] = "addons/amxmodx/scripting/testsuite/textparse_test.ini";
ExpectedKVData = TrieCreate();
TrieSetString(ExpectedKVData, "settings", "");
TrieSetString(ExpectedKVData, "enabled", "1");
TrieSetString(ExpectedKVData, "strip_weapons", "1");
TrieSetString(ExpectedKVData, "weapons_stay", "0");
TrieSetString(ExpectedKVData, "spawnmode", "preset");
TrieSetString(ExpectedKVData, "remove_bomb", "1");
TrieSetString(ExpectedKVData, "spawn_wait_time", "0.75");
TrieSetString(ExpectedKVData, "protection", "");
TrieSetString(ExpectedKVData, "colors", "0 255 0 200");
TrieSetString(ExpectedKVData, "time", "time");
TrieSetString(ExpectedKVData, "secondary", "");
TrieSetString(ExpectedKVData, "usp USP 1", "");
TrieSetString(ExpectedKVData, "glock18 Glock 1", "");
TrieSetString(ExpectedKVData, "deagle Deagle 1", "");
TrieSetString(ExpectedKVData, "botsecondary", "");
TrieSetString(ExpectedKVData, "deagle", "");
TrieSetString(ExpectedKVData, "usp", "");
new const expectedSectionCount = 4;
new const expectedStartEndCount = 2;
new const expectedKeyValueCount = TrieGetSize(ExpectedKVData) - expectedSectionCount;
new const expectedLineCount = TrieGetSize(ExpectedKVData); // This doesn't include blanck/comments line.
new INIParser:parser = INI_CreateParser();
INI_SetReaders(parser, "ReadCSDM_KeyValue", "ReadCSDM_NewSection");
INI_SetParseStart(parser, "ReadCSDM_ParseStart");
INI_SetRawLine(parser, "ReadCSDM_CurrentLine");
INI_SetParseEnd(parser, "ReadCSDM_ParseEnd");
new line, col;
new bool:result = INI_ParseFile(parser, configFile, line, col);
if (!result)
{
server_print("^tFatal parse error");
}
if (line == expectedLineCount + 1)
{
++SuccessCount;
}
server_print("^tTests successful: %d/%d", SuccessCount, expectedStartEndCount + expectedSectionCount + expectedKeyValueCount + expectedLineCount + 1);
INI_DestroyParser(parser);
TrieDestroy(ExpectedKVData);
}
public ReadCSDM_ParseStart(INIParser:handle)
{
Debug && server_print("ReadCSDM_ParseStart");
++SuccessCount;
}
public ReadCSDM_NewSection(INIParser:handle, const section[], bool:invalid_tokens, bool:close_bracket, bool:extra_tokens, curtok)
{
Debug && server_print("^tReadCSDM_NewSection - [%s] (invalid_tokens: '%s', close_bracked: '%s', extra_tokens: '%s')", section, invalid_tokens ? "yes" : "no", close_bracket ? "yes" : "no", extra_tokens ? "yes" : "no");
if (TrieKeyExists(ExpectedKVData, section))
{
if ((equal(section, "secondary") && !extra_tokens) ||
(equal(section, "botsecondary") && close_bracket))
{
return true;
}
++SuccessCount;
}
return true;
}
public bool:ReadCSDM_KeyValue(INIParser:handle, const key[], const value[], bool:invalid_tokens, bool:equal_token, bool:quotes, curtok)
{
Debug && server_print("^t^tReadCSDM_KeyValue - %-32s %s", key, value);
new buffer[128];
if (TrieGetString(ExpectedKVData, key, buffer, charsmax(buffer)) && equal(value, buffer))
{
if (equal(key, "colors") && !quotes)
{
return true;
}
++SuccessCount;
}
return true;
}
public bool:ReadCSDM_CurrentLine(INIParser:handle, const line[], curtok)
{
//Debug && server_print("^t^tReadCSDM_CurrentLine - %s", line);
++SuccessCount;
return true;
}
public ReadCSDM_ParseEnd(INIParser:handle, bool:halted, bool:failed)
{
Debug && server_print("ReadCSDM_ParseStart");
++SuccessCount;
}