core/smn_textparse.cpp
author David Anderson <dvander@alliedmods.net>
Mon Mar 12 00:06:20 2007 +0000 (2007-03-12)
changeset 599 3a098eff4890
parent 591 86e97f3eda1f
child 653 ef60870067ff
permissions -rw-r--r--
used a better error number
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "sm_globals.h"
    15 #include "TextParsers.h"
    16 #include "HandleSys.h"
    17 
    18 HandleType_t g_TypeSMC = 0;
    19 
    20 class ParseInfo : public ITextListener_SMC
    21 {
    22 public:
    23 	ParseInfo()
    24 	{
    25 		parse_start = NULL;
    26 		parse_end = NULL;
    27 		new_section = NULL;
    28 		key_value = NULL;
    29 		end_section = NULL;
    30 		raw_line = NULL;
    31 		handle = 0;
    32 	}
    33 public:
    34 	void ReadSMC_ParseStart()
    35 	{
    36 		if (parse_start)
    37 		{
    38 			cell_t result;
    39 			parse_start->PushCell(handle);
    40 			parse_start->Execute(&result);
    41 		}
    42 	}
    43 
    44 	void ReadSMC_ParseEnd(bool halted, bool failed)
    45 	{
    46 		if (parse_end)
    47 		{
    48 			cell_t result;
    49 			parse_start->PushCell(handle);
    50 			parse_end->PushCell(halted ? 1 : 0);
    51 			parse_end->PushCell(failed ? 1 : 0);
    52 			parse_end->Execute(&result);
    53 		}
    54 	}
    55 
    56 	SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
    57 	{
    58 		cell_t result = SMCParse_Continue;
    59 
    60 		if (new_section)
    61 		{
    62 			new_section->PushCell(handle);
    63 			new_section->PushString(name);
    64 			new_section->PushCell(opt_quotes ? 1 : 0);
    65 			new_section->Execute(&result);
    66 		}
    67 
    68 		return (SMCParseResult)result;
    69 	}
    70 
    71 	SMCParseResult ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
    72 	{
    73 		cell_t result = SMCParse_Continue;
    74 
    75 		if (key_value)
    76 		{
    77 			key_value->PushCell(handle);
    78 			key_value->PushString(key);
    79 			key_value->PushString(value);
    80 			key_value->PushCell(key_quotes ? 1 : 0);
    81 			key_value->PushCell(value_quotes ? 1 : 0);
    82 			key_value->Execute(&result);
    83 		}
    84 
    85 		return (SMCParseResult)result;
    86 	}
    87 
    88 	SMCParseResult ReadSMC_LeavingSection()
    89 	{
    90 		cell_t result = SMCParse_Continue;
    91 
    92 		if (end_section)
    93 		{
    94 			end_section->PushCell(handle);
    95 			end_section->Execute(&result);
    96 		}
    97 
    98 		return (SMCParseResult)result;
    99 	}
   100 
   101 	SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
   102 	{
   103 		cell_t result = SMCParse_Continue;
   104 
   105 		if (raw_line)
   106 		{
   107 			raw_line->PushCell(handle);
   108 			raw_line->PushString(line);
   109 			raw_line->PushCell(curline);
   110 			raw_line->Execute(&result);
   111 		}
   112 
   113 		return (SMCParseResult)result;
   114 	}
   115 public:
   116 	IPluginFunction *parse_start;
   117 	IPluginFunction *parse_end;
   118 	IPluginFunction *new_section;
   119 	IPluginFunction *key_value;
   120 	IPluginFunction *end_section;
   121 	IPluginFunction *raw_line;
   122 	Handle_t handle;
   123 };
   124 
   125 class TextParseGlobals : 
   126 	public SMGlobalClass,
   127 	public IHandleTypeDispatch
   128 {
   129 public:
   130 	void OnSourceModAllInitialized()
   131 	{
   132 		HandleAccess sec;
   133 
   134 		/* These cannot be cloned, because they are locked to a specific plugin.
   135 		 * However, we let anyone read them because we don't care.
   136 		 */
   137 		g_HandleSys.InitAccessDefaults(NULL, &sec);
   138 		sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
   139 		sec.access[HandleAccess_Read] = 0;
   140 
   141 		g_TypeSMC = g_HandleSys.CreateType("SMCParser", this, 0, NULL, &sec, g_pCoreIdent, NULL);
   142 	}
   143 
   144 	void OnSourceModShutdown()
   145 	{
   146 		g_HandleSys.RemoveType(g_TypeSMC, g_pCoreIdent);
   147 	}
   148 
   149 	void OnHandleDestroy(HandleType_t type, void *object)
   150 	{
   151 		ParseInfo *parse = (ParseInfo *)object;
   152 		delete parse;
   153 	}
   154 };
   155 
   156 TextParseGlobals g_TextParseGlobals;
   157 
   158 static cell_t SMC_CreateParser(IPluginContext *pContext, const cell_t *params)
   159 {
   160 	ParseInfo *pInfo = new ParseInfo();
   161 
   162 	Handle_t hndl = g_HandleSys.CreateHandle(g_TypeSMC, pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
   163 
   164 	/* Should never happen */
   165 	if (!hndl)
   166 	{
   167 		delete pInfo;
   168 		return 0;
   169 	}
   170 
   171 	pInfo->handle = hndl;
   172 
   173 	return hndl;
   174 }
   175 
   176 static cell_t SMC_SetParseStart(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	Handle_t hndl = (Handle_t)params[1];
   179 	HandleError err;
   180 	ParseInfo *parse;
   181 
   182 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   183 		!= HandleError_None)
   184 	{
   185 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   186 	}
   187 
   188 	parse->parse_start = pContext->GetFunctionById((funcid_t)params[2]);
   189 
   190 	return 1;
   191 }
   192 
   193 static cell_t SMC_SetParseEnd(IPluginContext *pContext, const cell_t *params)
   194 {
   195 	Handle_t hndl = (Handle_t)params[1];
   196 	HandleError err;
   197 	ParseInfo *parse;
   198 
   199 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   200 		!= HandleError_None)
   201 	{
   202 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   203 	}
   204 
   205 	parse->parse_end = pContext->GetFunctionById((funcid_t)params[2]);
   206 
   207 	return 1;
   208 }
   209 
   210 static cell_t SMC_SetReaders(IPluginContext *pContext, const cell_t *params)
   211 {
   212 	Handle_t hndl = (Handle_t)params[1];
   213 	HandleError err;
   214 	ParseInfo *parse;
   215 
   216 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   217 		!= HandleError_None)
   218 	{
   219 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   220 	}
   221 
   222 	parse->new_section = pContext->GetFunctionById((funcid_t)params[2]);
   223 	parse->key_value = pContext->GetFunctionById((funcid_t)params[3]);
   224 	parse->end_section = pContext->GetFunctionById((funcid_t)params[4]);
   225 
   226 	return 1;
   227 }
   228 
   229 static cell_t SMC_SetRawLine(IPluginContext *pContext, const cell_t *params)
   230 {
   231 	Handle_t hndl = (Handle_t)params[1];
   232 	HandleError err;
   233 	ParseInfo *parse;
   234 
   235 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   236 		!= HandleError_None)
   237 	{
   238 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   239 	}
   240 
   241 	parse->raw_line = pContext->GetFunctionById((funcid_t)params[2]);
   242 
   243 	return 1;
   244 }
   245 
   246 static cell_t SMC_ParseFile(IPluginContext *pContext, const cell_t *params)
   247 {
   248 	Handle_t hndl = (Handle_t)params[1];
   249 	HandleError err;
   250 	ParseInfo *parse;
   251 
   252 	if ((err=g_HandleSys.ReadHandle(hndl, g_TypeSMC, NULL, (void **)&parse))
   253 		!= HandleError_None)
   254 	{
   255 		return pContext->ThrowNativeError("Invalid SMC Parse Handle %x (error %d)", hndl, err);
   256 	}
   257 
   258 	char *file;
   259 	pContext->LocalToString(params[2], &file);
   260 
   261 	char path[PLATFORM_MAX_PATH];
   262 	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
   263 
   264 	unsigned int line = 0, col = 0;
   265 	SMCParseError p_err = g_TextParser.ParseFile_SMC(path, parse, &line, &col);
   266 
   267 	cell_t *c_line, *c_col;
   268 	pContext->LocalToPhysAddr(params[3], &c_line);
   269 	pContext->LocalToPhysAddr(params[4], &c_col);
   270 
   271 	*c_line = line;
   272 	*c_col = col;
   273 
   274 	return (cell_t)p_err;
   275 }
   276 
   277 static cell_t SMC_GetErrorString(IPluginContext *pContext, const cell_t *params)
   278 {
   279 	const char *str = g_TextParser.GetSMCErrorString((SMCParseError)params[1]);
   280 
   281 	if (!str)
   282 	{
   283 		return 0;
   284 	}
   285 
   286 	pContext->StringToLocal(params[2], params[3], str);
   287 
   288 	return 1;
   289 }
   290 
   291 REGISTER_NATIVES(textNatives)
   292 {
   293 	{"SMC_CreateParser",			SMC_CreateParser},
   294 	{"SMC_ParseFile",				SMC_ParseFile},
   295 	{"SMC_GetErrorString",			SMC_GetErrorString},
   296 	{"SMC_SetParseStart",			SMC_SetParseStart},
   297 	{"SMC_SetParseEnd",				SMC_SetParseEnd},
   298 	{"SMC_SetReaders",				SMC_SetReaders},
   299 	{"SMC_SetRawLine",				SMC_SetRawLine},
   300 	{NULL,							NULL},
   301 };