plugins/include/textparse.inc
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 05:14:32 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2190 880e81f30b5e
parent 2168 c24a6fa957db
child 2289 44045ead61cd
permissions -rw-r--r--
GetContext() now returns 'this' as a hack (shouldn't+can't be accessed anyway)
dvander@2168
     1
/**
dvander@2168
     2
 * vim: set ts=4 :
dvander@2168
     3
 * =============================================================================
dvander@2168
     4
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
dvander@2168
     5
 * =============================================================================
dvander@2168
     6
 *
dvander@2168
     7
 * This file is part of the SourceMod/SourcePawn SDK.
dvander@2168
     8
 *
dvander@2168
     9
 * This program is free software; you can redistribute it and/or modify it under
dvander@2168
    10
 * the terms of the GNU General Public License, version 3.0, as published by the
dvander@2168
    11
 * Free Software Foundation.
dvander@2168
    12
 * 
dvander@2168
    13
 * This program is distributed in the hope that it will be useful, but WITHOUT
dvander@2168
    14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@2168
    15
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
dvander@2168
    16
 * details.
dvander@2168
    17
 *
dvander@2168
    18
 * You should have received a copy of the GNU General Public License along with
dvander@2168
    19
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@2168
    20
 *
dvander@2168
    21
 * As a special exception, AlliedModders LLC gives you permission to link the
dvander@2168
    22
 * code of this program (as well as its derivative works) to "Half-Life 2," the
dvander@2168
    23
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
dvander@2168
    24
 * by the Valve Corporation.  You must obey the GNU General Public License in
dvander@2168
    25
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
dvander@2168
    26
 * this exception to all derivative works.  AlliedModders LLC defines further
dvander@2168
    27
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
dvander@2168
    28
 * or <http://www.sourcemod.net/license.php>.
dvander@2168
    29
 *
dvander@2168
    30
 * Version: $Id$
dvander@2168
    31
 */
dvander@2168
    32
 
dvander@2168
    33
#if defined _textparse_included
dvander@2168
    34
 #endinput
dvander@2168
    35
#endif
dvander@2168
    36
#define _textparse_included
dvander@2168
    37
dvander@2168
    38
dvander@2168
    39
/********************************
dvander@2168
    40
 * Everything below describes the SMC Parse, or "SourceMod Configuration" format.
dvander@2168
    41
 * This parser is entirely event based.  You must hook events to receive data.
dvander@2168
    42
 * The file format itself is nearly identical to Valve's KeyValues format.
dvander@2168
    43
 ********************************/
dvander@2168
    44
dvander@2168
    45
/**
dvander@2168
    46
 * Parse result directive.
dvander@2168
    47
 */
dvander@2168
    48
enum SMCResult
dvander@2168
    49
{
dvander@2168
    50
	SMCParse_Continue,			/**< Continue parsing */
dvander@2168
    51
	SMCParse_Halt,				/**< Stop parsing here */
dvander@2168
    52
	SMCParse_HaltFail			/**< Stop parsing and return failure */
dvander@2168
    53
};
dvander@2168
    54
dvander@2168
    55
/**
dvander@2168
    56
 * Parse error codes.
dvander@2168
    57
 */
dvander@2168
    58
enum SMCError
dvander@2168
    59
{
dvander@2168
    60
	SMCError_Okay = 0,			/**< No error */
dvander@2168
    61
	SMCError_StreamOpen,		/**< Stream failed to open */
dvander@2168
    62
	SMCError_StreamError,		/**< The stream died... somehow */
dvander@2168
    63
	SMCError_Custom,			/**< A custom handler threw an error */
dvander@2168
    64
	SMCError_InvalidSection1,	/**< A section was declared without quotes, and had extra tokens */
dvander@2168
    65
	SMCError_InvalidSection2,	/**< A section was declared without any header */
dvander@2168
    66
	SMCError_InvalidSection3,	/**< A section ending was declared with too many unknown tokens */
dvander@2168
    67
	SMCError_InvalidSection4,	/**< A section ending has no matching beginning */
dvander@2168
    68
	SMCError_InvalidSection5,	/**< A section beginning has no matching ending */
dvander@2168
    69
	SMCError_InvalidTokens,		/**< There were too many unidentifiable strings on one line */
dvander@2168
    70
	SMCError_TokenOverflow,		/**< The token buffer overflowed */
dvander@2168
    71
	SMCError_InvalidProperty1,	/**< A property was declared outside of any section */
dvander@2168
    72
};
dvander@2168
    73
dvander@2168
    74
/**
dvander@2168
    75
 * Creates a new SMC file format parser.  This is used to set parse hooks.
dvander@2168
    76
 *
dvander@2168
    77
 * @return			A new Handle to an SMC Parse structure.
dvander@2168
    78
 */
dvander@2168
    79
native Handle:SMC_CreateParser();
dvander@2168
    80
dvander@2168
    81
/**
dvander@2168
    82
 * Parses an SMC file.
dvander@2168
    83
 *
dvander@2168
    84
 * @param smc			A Handle to an SMC Parse structure.
dvander@2168
    85
 * @param file			A string containing the file path.
dvander@2168
    86
 * @param line			An optional by reference cell to store the last line number read.
dvander@2168
    87
 * @param col			An optional by reference cell to store the last column number read.
dvander@2168
    88
 * @return				An SMCParseError result.
dvander@2168
    89
 * @error				Invalid or corrupt Handle. 
dvander@2168
    90
 */
dvander@2168
    91
native SMCError:SMC_ParseFile(Handle:smc, const String:file[], &line=0, &col=0);
dvander@2168
    92
dvander@2168
    93
/**
dvander@2168
    94
 * Gets an error string for an SMCError code.
dvander@2168
    95
 * @note SMCError_Okay returns false.
dvander@2168
    96
 * @note SMCError_Custom (which is thrown on SMCParse_HaltFail) returns false.
dvander@2168
    97
 *
dvander@2168
    98
 * @param error			The SMCParseError code.
dvander@2168
    99
 * @param buffer		A string buffer for the error (contents undefined on failure).
dvander@2168
   100
 * @param buf_max		The maximum size of the buffer.
dvander@2168
   101
 * @return				True on success, false otherwise.
dvander@2168
   102
 */
dvander@2168
   103
native bool:SMC_GetErrorString(SMCError:error, String:buffer[], buf_max);
dvander@2168
   104
dvander@2168
   105
/**
dvander@2168
   106
 * Called when parsing is started.
dvander@2168
   107
 *
dvander@2168
   108
 * @param smc			The SMC Parse Handle.
dvander@2168
   109
 * @noreturn
dvander@2168
   110
 */
dvander@2168
   111
functag SMC_ParseStart public(Handle:smc);
dvander@2168
   112
dvander@2168
   113
/**
dvander@2168
   114
 * Sets the SMC_ParseStart function of a parse Handle.
dvander@2168
   115
 *
dvander@2168
   116
 * @param smc			Handle to an SMC Parse.
dvander@2168
   117
 * @param func			SMC_ParseStart function.
dvander@2168
   118
 * @noreturn
dvander@2168
   119
 * @error				Invalid or corrupt Handle. 
dvander@2168
   120
 */
dvander@2168
   121
native SMC_SetParseStart(Handle:smc, SMC_ParseStart:func);
dvander@2168
   122
dvander@2168
   123
/**
dvander@2168
   124
 * Called when parsing is halted.
dvander@2168
   125
 *
dvander@2168
   126
 * @param smc			The SMC Parse Handle.
dvander@2168
   127
 * @param halted		True if abnormally halted, false otherwise.
dvander@2168
   128
 * @param failed		True if parsing failed, false otherwise.
dvander@2168
   129
 * @noreturn
dvander@2168
   130
 */
dvander@2168
   131
functag SMC_ParseEnd public(Handle:smc, bool:halted, bool:failed);
dvander@2168
   132
dvander@2168
   133
/**
dvander@2168
   134
 * Sets the SMC_ParseEnd of a parse handle.
dvander@2168
   135
 *
dvander@2168
   136
 * @param smc			Handle to an SMC Parse.
dvander@2168
   137
 * @param func			SMC_ParseEnd function.
dvander@2168
   138
 * @noreturn
dvander@2168
   139
 * @error				Invalid or corrupt Handle. 
dvander@2168
   140
 */
dvander@2168
   141
native SMC_SetParseEnd(Handle:smc, SMC_ParseEnd:func);
dvander@2168
   142
dvander@2168
   143
/**
dvander@2168
   144
 * Called when the parser is entering a new section or sub-section.
dvander@2168
   145
 * @note Enclosing quotes are always stripped.
dvander@2168
   146
 *
dvander@2168
   147
 * @param smc			The SMC Parse Handle.
dvander@2168
   148
 * @param name			String containing section name.
dvander@2168
   149
 * @param opt_quotes	True if the section name was quote-enclosed in the file.
dvander@2168
   150
 * @return				An SMCResult action to take.
dvander@2168
   151
 */
dvander@2168
   152
functag SMC_NewSection SMCResult:public(Handle:smc, const String:name[], bool:opt_quotes);
dvander@2168
   153
dvander@2168
   154
/**
dvander@2168
   155
 * Called when the parser finds a new key/value pair.
dvander@2168
   156
 * @note Enclosing quotes are always stripped.
dvander@2168
   157
 *
dvander@2168
   158
 * @param smc			The SMC Parse Handle.
dvander@2168
   159
 * @param key			String containing key name.
dvander@2168
   160
 * @param value			String containing value name.
dvander@2168
   161
 * @param key_quotes	Whether or not the key was enclosed in quotes.
dvander@2168
   162
 * @param value_quotes	Whether or not the value was enclosed in quotes.
dvander@2168
   163
 * @return				An SMCResult action to take.
dvander@2168
   164
 */
dvander@2168
   165
functag SMC_KeyValue SMCResult:public(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes);
dvander@2168
   166
dvander@2168
   167
/**
dvander@2168
   168
 * Called when the parser finds the end of the current section.
dvander@2168
   169
 *
dvander@2168
   170
 * @param smc			The SMC Parse Handle.
dvander@2168
   171
 * @return				An SMCResult action to take.
dvander@2168
   172
 */
dvander@2168
   173
functag SMC_EndSection SMCResult:public(Handle:smc);
dvander@2168
   174
dvander@2168
   175
/**
dvander@2168
   176
 * Sets the three main reader functions.
dvander@2168
   177
 *
dvander@2168
   178
 * @param smc			An SMC parse Handle.
dvander@2168
   179
 * @param ns			An SMC_NewSection function pointer.
dvander@2168
   180
 * @param kv			An SMC_KeyValue function pointer.
dvander@2168
   181
 * @param es			An SMC_EndSection function pointer.
dvander@2168
   182
 * @noreturn
dvander@2168
   183
 */
dvander@2168
   184
native SMC_SetReaders(Handle:smc, SMC_NewSection:ns, SMC_KeyValue:kv, SMC_EndSection:es);
dvander@2168
   185
dvander@2168
   186
/**
dvander@2168
   187
 * Called whenever a raw line is read.
dvander@2168
   188
 *
dvander@2168
   189
 * @param smc			The SMC Parse Handle.
dvander@2168
   190
 * @param line			A string containing the raw line from the file.
dvander@2168
   191
 * @param lineno		The line number it occurs on.
dvander@2168
   192
 * @return				An SMCResult action to take.
dvander@2168
   193
 */
dvander@2168
   194
functag SMC_RawLine SMCResult:public(Handle:smc, const String:line[], lineno);
dvander@2168
   195
dvander@2168
   196
/**
dvander@2168
   197
 * Sets a raw line reader on an SMC parser Handle.
dvander@2168
   198
 *
dvander@2168
   199
 * @param smc			Handle to an SMC Parse.
dvander@2168
   200
 * @param func			SMC_RawLine function.
dvander@2168
   201
 * @noreturn
dvander@2168
   202
 */
dvander@2168
   203
native SMC_SetRawLine(Handle:smc, SMC_RawLine:func);