3 * ===============================================================
4 * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC.
6 * ===============================================================
8 * This file is part of the SourceMod/SourcePawn SDK. This file may only be
9 * used or modified under the Terms and Conditions of its License Agreement,
10 * which is found in public/licenses/LICENSE.txt. As of this notice, derivative
11 * works must be licensed under the GNU General Public License (version 2 or
12 * greater). A copy of the GPL is included under public/licenses/GPL.txt.
14 * To view the latest information, see: http://www.sourcemod.net/license.php
19 #ifndef _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
20 #define _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
23 * @file ITextParsers.h
24 * @brief Defines various text/file parsing functions, as well as UTF-8 support code.
27 #include <IShareSys.h>
32 * The INI file format is defined as:
33 * WHITESPACE: 0x20, \n, \t, \r
34 * IDENTIFIER: A-Z a-z 0-9 _ - , + . $ ? /
35 * STRING: Any set of symbols
37 * Basic syntax is comprised of SECTIONs.
38 * A SECTION is defined as:
44 * SECTIONNAME is an IDENTIFIER.
45 * OPTION can be repeated any number of times, once per line.
46 * OPTION is defined as one of:
50 * Where KEY is an IDENTIFIER and VALUE is a STRING.
52 * WHITESPACE should always be omitted.
53 * COMMENTS should be stripped, and are defined as text occuring in:
56 * Example file below. Note that
57 * The second line is technically invalid. The event handler
58 * must decide whether this should be allowed.
71 * @brief Contains parse events for INI files.
73 class ITextListener_INI
77 * @brief Called when a new section is encountered in an INI file.
79 * @param section Name of section in between the [ and ] characters.
80 * @param invalid_tokens True if invalid tokens were detected in the name.
81 * @param close_bracket True if a closing bracket was detected, false otherwise.
82 * @param extra_tokens True if extra tokens were detected on the line.
83 * @param curtok Contains current token in the line where the section name starts.
84 * You can add to this offset when failing to point to a token.
85 * @return True to keep parsing, false otherwise.
87 virtual bool ReadINI_NewSection(const char *section,
97 * @brief Called when encountering a key/value pair in an INI file.
99 * @param key Name of key.
100 * @param value String containing value (with quotes stripped, if any).
101 * @param invalid_tokens Whether or not the key contained invalid tokens.
102 * @param equal_token There was an '=' sign present (in case the value is missing).
103 * @param quotes Whether value was enclosed in quotes.
104 * @param curtok Contains the token index of the start of the value string.
105 * This can be changed when returning false.
106 * @return True to keep parsing, false otherwise.
108 virtual bool ReadINI_KeyValue(const char *key,
113 unsigned int *curtok)
119 * @brief Called after a line has been preprocessed, if it has text.
121 * @param line Contents of line.
122 * @param curtok Pointer to optionally store failed position in string.
123 * @return True to keep parsing, false otherwise.
125 virtual bool ReadINI_RawLine(const char *line, unsigned int *curtok)
132 * :TODO: write this in CFG (context free grammar) format so it makes sense
134 * The SMC file format is defined as:
135 * WHITESPACE: 0x20, \n, \t, \r
136 * IDENTIFIER: Any ASCII character EXCLUDING ", {, }, ;, //, / *, or WHITESPACE.
137 * STRING: Any set of symbols enclosed in quotes.
138 * Note: if a STRING does not have quotes, it is parsed as an IDENTIFIER.
140 * Basic syntax is comprised of SECTIONBLOCKs.
141 * A SECTIONBLOCK defined as:
148 * OPTION can be repeated any number of times inside a SECTIONBLOCK.
149 * A new line will terminate an OPTION, but there can be more than one OPTION per line.
150 * OPTION is defined any of:
154 * SECTIONNAME, KEY, VALUE, and SINGLEKEY are strings
155 * SECTIONNAME cannot have trailing characters if quoted, but the quotes can be optionally removed.
156 * If SECTIONNAME is not enclosed in quotes, the entire sectionname string is used (minus surrounding whitespace).
157 * If KEY is not enclosed in quotes, the key is terminated at first whitespace.
158 * If VALUE is not properly enclosed in quotes, the entire value string is used (minus surrounding whitespace).
159 * The VALUE may have inner quotes, but the key string may not.
161 * For an example, see configs/permissions.cfg
163 * WHITESPACE should be ignored.
164 * Comments are text occuring inside the following tokens, and should be stripped
165 * unless they are inside literal strings:
171 * @brief Lists actions to take when an SMC parse hook is done.
175 SMCParse_Continue, /**< Continue parsing */
176 SMCParse_Halt, /**< Stop parsing here */
177 SMCParse_HaltFail /**< Stop parsing and return SMCParseError_Custom */
181 * @brief Lists error codes possible from parsing an SMC file.
185 SMCParse_Okay = 0, /**< No error */
186 SMCParse_StreamOpen, /**< Stream failed to open */
187 SMCParse_StreamError, /**< The stream died... somehow */
188 SMCParse_Custom, /**< A custom handler threw an error */
189 SMCParse_InvalidSection1, /**< A section was declared without quotes, and had extra tokens */
190 SMCParse_InvalidSection2, /**< A section was declared without any header */
191 SMCParse_InvalidSection3, /**< A section ending was declared with too many unknown tokens */
192 SMCParse_InvalidSection4, /**< A section ending has no matching beginning */
193 SMCParse_InvalidSection5, /**< A section beginning has no matching ending */
194 SMCParse_InvalidTokens, /**< There were too many unidentifiable strings on one line */
195 SMCParse_TokenOverflow, /**< The token buffer overflowed */
196 SMCParse_InvalidProperty1, /**< A property was declared outside of any section */
200 * @brief Describes the events available for reading an SMC stream.
202 class ITextListener_SMC
206 * @brief Called when starting parsing.
208 virtual void ReadSMC_ParseStart()
213 * @brief Called when ending parsing.
215 * @param halted True if abnormally halted, false otherwise.
216 * @param failed True if parsing failed, false otherwise.
218 virtual void ReadSMC_ParseEnd(bool halted, bool failed)
223 * @brief Called when a warning occurs.
224 * @param error By-reference variable containing the error message of the warning.
225 * @param tokens Pointer to the token stream causing the error.
226 * @return SMCParseResult directive.
228 virtual SMCParseResult ReadSMC_OnWarning(SMCParseError &error, const char *tokens)
230 return SMCParse_HaltFail;
234 * @brief Called when entering a new section
236 * @param name Name of section, with the colon omitted.
237 * @param opt_quotes Whether or not the option string was enclosed in quotes.
238 * @return SMCParseResult directive.
240 virtual SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
242 return SMCParse_Continue;
246 * @brief Called when encountering a key/value pair in a section.
248 * @param key Key string.
249 * @param value Value string. If no quotes were specified, this will be NULL,
250 and key will contain the entire string.
251 * @param key_quotes Whether or not the key was in quotation marks.
252 * @param value_quotes Whether or not the value was in quotation marks.
253 * @return SMCParseResult directive.
255 virtual SMCParseResult ReadSMC_KeyValue(const char *key,
260 return SMCParse_Continue;
264 * @brief Called when leaving the current section.
266 * @return SMCParseResult directive.
268 virtual SMCParseResult ReadSMC_LeavingSection()
270 return SMCParse_Continue;
274 * @brief Called after an input line has been preprocessed.
276 * @param line String containing line input.
277 * @param curline Number of line in file.
278 * @return SMCParseResult directive.
280 virtual SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
282 return SMCParse_Continue;
286 #define SMINTERFACE_TEXTPARSERS_NAME "ITextParsers"
287 #define SMINTERFACE_TEXTPARSERS_VERSION 1
290 * @brief Contains various text stream parsing functions.
292 class ITextParsers : public SMInterface
295 virtual const char *GetInterfaceName()
297 return SMINTERFACE_TEXTPARSERS_NAME;
299 virtual unsigned int GetInterfaceVersion()
301 return SMINTERFACE_TEXTPARSERS_VERSION;
305 * @brief Parses an INI-format file.
307 * @param file Path to file.
308 * @param ini_listener Event handler for reading file.
309 * @param line If non-NULL, will contain last line parsed (0 if file could not be opened).
310 * @param col If non-NULL, will contain last column parsed (undefined if file could not be opened).
311 * @return True if parsing succeded, false if file couldn't be opened or there was a syntax error.
313 virtual bool ParseFile_INI(const char *file,
314 ITextListener_INI *ini_listener,
316 unsigned int *col) =0;
319 * @brief Parses an SMC-format text file.
320 * Note that the parser makes every effort to obey broken syntax.
321 * For example, if an open brace is missing, but the section name has a colon,
322 * it will let you know. It is up to the event handlers to decide whether to be strict or not.
324 * @param file Path to file.
325 * @param smc_listener Event handler for reading file.
326 * @param line If non-NULL, will contain last line parsed (0 if file could not be opened).
327 * @param col If non-NULL, will contain last column parsed (undefined if file could not be opened).
328 * @return An SMCParseError result code.
330 virtual SMCParseError ParseFile_SMC(const char *file,
331 ITextListener_SMC *smc_listener,
333 unsigned int *col) =0;
336 * @brief Converts an SMCParseError to a stirng.
338 * @param err SMCParseError.
339 * @return String error message, or NULL if none.
341 virtual const char *GetSMCErrorString(SMCParseError err) =0;
345 * @brief Returns the number of bytes that a multi-byte character contains in a UTF-8 stream.
346 * If the current character is not multi-byte, the function returns 1.
348 * @param stream Pointer to multi-byte ANSI character string.
349 * @return Number of bytes in current character.
351 virtual unsigned int GetUTF8CharBytes(const char *stream) =0;
355 #endif //_INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_