plugins/include/textparse.inc
author David Anderson <dvander@alliedmods.net>
Mon Mar 12 00:06:20 2007 +0000 (2007-03-12)
changeset 599 3a098eff4890
parent 475 bb11d7eaae0f
child 619 6934bf39171e
permissions -rw-r--r--
used a better error number
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _textparse_included
    17  #endinput
    18 #endif
    19 #define _textparse_included
    20 
    21 
    22 /********************************
    23  * Everything below describes the SMC Parse, or "SourceMod Configuration" format.
    24  * This parser is entirely event based.  You must hook events to receive data.
    25  * The file format itself is nearly identical to Valve's KeyValues format.
    26  ********************************/
    27 
    28 enum SMCResult
    29 {
    30 	SMCParse_Continue,			/**< Continue parsing */
    31 	SMCParse_Halt,				/**< Stop parsing here */
    32 	SMCParse_HaltFail			/**< Stop parsing and return failure */
    33 };
    34 
    35 enum SMCError
    36 {
    37 	SMCError_Okay = 0,			/**< No error */
    38 	SMCError_StreamOpen,		/**< Stream failed to open */
    39 	SMCError_StreamError,		/**< The stream died... somehow */
    40 	SMCError_Custom,			/**< A custom handler threw an error */
    41 	SMCError_InvalidSection1,	/**< A section was declared without quotes, and had extra tokens */
    42 	SMCError_InvalidSection2,	/**< A section was declared without any header */
    43 	SMCError_InvalidSection3,	/**< A section ending was declared with too many unknown tokens */
    44 	SMCError_InvalidSection4,	/**< A section ending has no matching beginning */
    45 	SMCError_InvalidSection5,	/**< A section beginning has no matching ending */
    46 	SMCError_InvalidTokens,		/**< There were too many unidentifiable strings on one line */
    47 	SMCError_TokenOverflow,		/**< The token buffer overflowed */
    48 	SMCError_InvalidProperty1,	/**< A property was declared outside of any section */
    49 };
    50 
    51 /**
    52  * Creates a new SMC file format parser.  This is used to set parse hooks.
    53  *
    54  * @return			A new Handle to an SMC Parse structure.
    55  */
    56 native Handle:SMC_CreateParser();
    57 
    58 /**
    59  * Parses an SMC file.
    60  *
    61  * @param smc			A Handle to an SMC Parse structure.
    62  * @param file			A string containing the file path.
    63  * @param line			An optional by reference cell to store the last line number read.
    64  * @param col			An optional by reference cell to store the last column number read.
    65  * @return				An SMCParseError result.
    66  * @error				Invalid or corrupt Handle. 
    67  */
    68 native SMCError:SMC_ParseFile(Handle:smc, const String:file[], &line=0, &col=0);
    69 
    70 /**
    71  * Gets an error string for an SMCError code.
    72  * @note SMCError_Okay returns false.
    73  * @note SMCError_Custom (which is thrown on SMCParse_HaltFail) returns false.
    74  *
    75  * @param error			The SMCParseError code.
    76  * @param buffer		A string buffer for the error (contents undefined on failure).
    77  * @param buf_max		The maximum size of the buffer.
    78  * @return				True on success, false otherwise.
    79  */
    80 native bool:SMC_GetErrorString(SMCError:error, String:buffer[], buf_max);
    81 
    82 /**
    83  * Called when parsing is started.
    84  *
    85  * @param smc			The SMC Parse Handle.
    86  * @noreturn
    87  */
    88 functag SMC_ParseStart public(Handle:smc);
    89 
    90 /**
    91  * Sets the SMC_ParseStart function of a parse Handle.
    92  *
    93  * @param smc			Handle to an SMC Parse.
    94  * @param func			SMC_ParseStart function.
    95  * @noreturn
    96  * @error				Invalid or corrupt Handle. 
    97  */
    98 native SMC_SetParseStart(Handle:smc, SMC_ParseStart:func);
    99 
   100 /**
   101  * Called when parsing is halted.
   102  *
   103  * @param smc			The SMC Parse Handle.
   104  * @param halted		True if abnormally halted, false otherwise.
   105  * @param failed		True if parsing failed, false otherwise.
   106  * @noreturn
   107  */
   108 functag SMC_ParseEnd public(Handle:smc, bool:halted, bool:failed);
   109 
   110 /**
   111  * Sets the SMC_ParseEnd of a parse handle.
   112  *
   113  * @param smc			Handle to an SMC Parse.
   114  * @param func			SMC_ParseEnd function.
   115  * @noreturn
   116  * @error				Invalid or corrupt Handle. 
   117  */
   118 native SMC_SetParseEnd(Handle:smc, SMC_ParseEnd:func);
   119 
   120 /**
   121  * Called when the parser is entering a new section or sub-section.
   122  * @note Enclosing quotes are always stripped.
   123  *
   124  * @param smc			The SMC Parse Handle.
   125  * @param name			String containing section name.
   126  * @param opt_quotes	True if the section name was quote-enclosed in the file.
   127  * @return				An SMCResult action to take.
   128  */
   129 functag SMC_NewSection SMCResult:public(Handle:smc, const String:name[], bool:opt_quotes);
   130 
   131 /**
   132  * Called when the parser finds a new key/value pair.
   133  * @note Enclosing quotes are always stripped.
   134  *
   135  * @param smc			The SMC Parse Handle.
   136  * @param key			String containing key name.
   137  * @param value			String containing value name.
   138  * @param key_quotes	Whether or not the key was enclosed in quotes.
   139  * @param value_quotes	Whether or not the value was enclosed in quotes.
   140  * @return				An SMCResult action to take.
   141  */
   142 functag SMC_KeyValue SMCResult:public(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes);
   143 
   144 /**
   145  * Called when the parser finds the end of the current section.
   146  *
   147  * @param smc			The SMC Parse Handle.
   148  * @return				An SMCResult action to take.
   149  */
   150 functag SMC_EndSection SMCResult:public(Handle:smc);
   151 
   152 /**
   153  * Sets the three main reader functions.
   154  *
   155  * @param smc			An SMC parse Handle.
   156  * @param ns			An SMC_NewSection function pointer.
   157  * @param kv			An SMC_KeyValue function pointer.
   158  * @param es			An SMC_EndSection function pointer.
   159  * @noreturn
   160  */
   161 native SMC_SetReaders(Handle:smc, SMC_NewSection:ns, SMC_KeyValue:kv, SMC_EndSection:es);
   162 
   163 /**
   164  * Called whenever a raw line is read.
   165  *
   166  * @param smc			The SMC Parse Handle.
   167  * @param line			A string containing the raw line from the file.
   168  * @param lineno		The line number it occurs on.
   169  * @return				An SMCResult action to take.
   170  */
   171 functag SMC_RawLine SMCResult:public(Handle:smc, const String:line[], lineno);
   172 
   173 /**
   174  * Sets a raw line reader on an SMC parser Handle.
   175  *
   176  * @param smc			Handle to an SMC Parse.
   177  * @param func			SMC_RawLine function.
   178  * @noreturn
   179  */
   180 native SMC_SetRawLine(Handle:smc, SMC_RawLine:func);