core/smn_textparse.cpp
author David Anderson <dvander@alliedmods.net>
Wed Oct 31 05:04:07 2007 +0000 (2007-10-31)
changeset 1656 b0b86d57530c
parent 1406 f5c07ec3c052
child 1766 d30c252089be
permissions -rw-r--r--
fixed a number of memory complaints from valgrind. most of these were minor, but there was a rather disturbing memory over-read error in the SMC text parser. correcting this brought about a rewrite of its API. this change is BACKWARDS INCOMPATIBLE for C++ extensions, but it was sorely needed, and the API is now both future-extensible and much easier to work with. plugins didn't need any changes, but they will probably get the better API changes in a future release. as a special bonus, the RawLine() hook is now much less expensive since the entire stream buffer won't be shoved through it like before!
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include <ITextParsers.h>
    34 #include "HandleSys.h"
    35 
    36 HandleType_t g_TypeSMC = 0;
    37 
    38 class ParseInfo : public ITextListener_SMC
    39 {
    40 public:
    41 	ParseInfo()
    42 	{
    43 		parse_start = NULL;
    44 		parse_end = NULL;
    45 		new_section = NULL;
    46 		key_value = NULL;
    47 		end_section = NULL;
    48 		raw_line = NULL;
    49 		handle = 0;
    50 	}
    51 public:
    52 	void ReadSMC_ParseStart()
    53 	{
    54 		if (parse_start)
    55 		{
    56 			cell_t result;
    57 			parse_start->PushCell(handle);
    58 			parse_start->Execute(&result);
    59 		}
    60 	}
    61 
    62 	void ReadSMC_ParseEnd(bool halted, bool failed)
    63 	{
    64 		if (parse_end)
    65 		{
    66 			cell_t result;
    67 			parse_end->PushCell(handle);
    68 			parse_end->PushCell(halted ? 1 : 0);
    69 			parse_end->PushCell(failed ? 1 : 0);
    70 			parse_end->Execute(&result);
    71 		}
    72 	}
    73 
    74 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
    75 	{
    76 		cell_t result = SMCResult_Continue;
    77 
    78 		if (new_section)
    79 		{
    80 			new_section->PushCell(handle);
    81 			new_section->PushString(name);
    82 			new_section->PushCell(1);
    83 			new_section->Execute(&result);
    84 		}
    85 
    86 		return (SMCResult)result;
    87 	}
    88 
    89 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
    90 	{
    91 		cell_t result = SMCResult_Continue;
    92 
    93 		if (key_value)
    94 		{
    95 			key_value->PushCell(handle);
    96 			key_value->PushString(key);
    97 			key_value->PushString(value);
    98 			key_value->PushCell(1);
    99 			key_value->PushCell(1);
   100 			key_value->Execute(&result);
   101 		}
   102 
   103 		return (SMCResult)result;
   104 	}
   105 
   106 	SMCResult ReadSMC_LeavingSection(const SMCStates *states)
   107 	{
   108 		cell_t result = SMCResult_Continue;
   109 
   110 		if (end_section)
   111 		{
   112 			end_section->PushCell(handle);
   113 			end_section->Execute(&result);
   114 		}
   115 
   116 		return (SMCResult)result;
   117 	}
   118 
   119 	SMCResult ReadSMC_RawLine(const SMCStates *states, const char *line)
   120 	{
   121 		cell_t result = SMCResult_Continue;
   122 
   123 		if (raw_line)
   124 		{
   125 			raw_line->PushCell(handle);
   126 			raw_line->PushString(line);
   127 			raw_line->PushCell(states->line);
   128 			raw_line->Execute(&result);
   129 		}
   130 
   131 		return (SMCResult)result;
   132 	}
   133 public:
   134 	IPluginFunction *parse_start;
   135 	IPluginFunction *parse_end;
   136 	IPluginFunction *new_section;
   137 	IPluginFunction *key_value;
   138 	IPluginFunction *end_section;
   139 	IPluginFunction *raw_line;
   140 	Handle_t handle;
   141 };
   142 
   143 class TextParseGlobals : 
   144 	public SMGlobalClass,
   145 	public IHandleTypeDispatch
   146 {
   147 public:
   148 	void OnSourceModAllInitialized()
   149 	{
   150 		HandleAccess sec;
   151 
   152 		/* These cannot be cloned, because they are locked to a specific plugin.
   153 		 * However, we let anyone read them because we don't care.
   154 		 */
   155 		g_HandleSys.InitAccessDefaults(NULL, &sec);
   156 		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
   157 		sec.access[HandleAccess_Read] = 0;
   158 
   159 		g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
   160 	}
   161 
   162 	void OnSourceModShutdown()
   163 	{
   164 		g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
   165 	}
   166 
   167 	void OnHandleDestroy(HandleType_t type, void *object)
   168 	{
   169 		ParseInfo *parse = (ParseInfo *)object;
   170 		delete parse;
   171 	}
   172 };
   173 
   174 TextParseGlobals g_TextParseGlobals;
   175 
   176 static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	ParseInfo *pInfo = new ParseInfo();
   179 
   180 	Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
   181 
   182 	/* Should never happen */
   183 	if (!hndl)
   184 	{
   185 		delete pInfo;
   186 		return 0;
   187 	}
   188 
   189 	pInfo->handle = hndl;
   190 
   191 	return hndl;
   192 }
   193 
   194 static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
   195 {
   196 	Handle_t hndl = (Handle_t)params[1];
   197 	HandleError err;
   198 	ParseInfo *parse;
   199 
   200 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   201 		!= HandleError_None)
   202 	{
   203 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   204 	}
   205 
   206 	parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
   207 
   208 	return 1;
   209 }
   210 
   211 static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
   212 {
   213 	Handle_t hndl = (Handle_t)params[1];
   214 	HandleError err;
   215 	ParseInfo *parse;
   216 
   217 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   218 		!= HandleError_None)
   219 	{
   220 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   221 	}
   222 
   223 	parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
   224 
   225 	return 1;
   226 }
   227 
   228 static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
   229 {
   230 	Handle_t hndl = (Handle_t)params[1];
   231 	HandleError err;
   232 	ParseInfo *parse;
   233 
   234 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   235 		!= HandleError_None)
   236 	{
   237 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   238 	}
   239 
   240 	parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
   241 	parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
   242 	parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
   243 
   244 	return 1;
   245 }
   246 
   247 static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
   248 {
   249 	Handle_t hndl = (Handle_t)params[1];
   250 	HandleError err;
   251 	ParseInfo *parse;
   252 
   253 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   254 		!= HandleError_None)
   255 	{
   256 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   257 	}
   258 
   259 	parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
   260 
   261 	return 1;
   262 }
   263 
   264 static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
   265 {
   266 	Handle_t hndl = (Handle_t)params[1];
   267 	HandleError err;
   268 	ParseInfo *parse;
   269 
   270 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   271 		!= HandleError_None)
   272 	{
   273 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   274 	}
   275 
   276 	char *file;
   277 	pContext->LocalToString(params[2], &file);
   278 
   279 	char path[PLATFORM_MAX_PATH];
   280 	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
   281 
   282 	SMCStates states;
   283 	SMCError p_err = textparsers->ParseFile_SMC(path, parse, &states);
   284 
   285 	cell_t *c_line, *c_col;
   286 	pContext->LocalToPhysAddr(params[3], &c_line);
   287 	pContext->LocalToPhysAddr(params[4], &c_col);
   288 
   289 	*c_line = states.line;
   290 	*c_col = states.col;
   291 
   292 	return (cell_t)p_err;
   293 }
   294 
   295 static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
   296 {
   297 	const char *str = textparsers->GetSMCErrorString((SMCError)params[1]);
   298 
   299 	if (!str)
   300 	{
   301 		return 0;
   302 	}
   303 
   304 	pContext->StringToLocal(params[2], params[3], str);
   305 
   306 	return 1;
   307 }
   308 
   309 REGISTER_NATIVES(textNatives)
   310 {
   311 	{"SMC_CreateParser",			SMC_CreateParser},
   312 	{"SMC_ParseFile",				SMC_ParseFile},
   313 	{"SMC_GetErrorString",			SMC_GetErrorString},
   314 	{"SMC_SetParseStart",			SMC_SetParseStart},
   315 	{"SMC_SetParseEnd",				SMC_SetParseEnd},
   316 	{"SMC_SetReaders",				SMC_SetReaders},
   317 	{"SMC_SetRawLine",				SMC_SetRawLine},
   318 	{NULL,							NULL},
   319 };