-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_blue.cpp
More file actions
99 lines (91 loc) · 3 KB
/
parser_blue.cpp
File metadata and controls
99 lines (91 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright © 2014 CCP ehf.
#include "stdafx.h"
#include "parser.h"
#include "localization.h"
// -------------------------------------------------------------
// Description:
// Registers a property handler that is being used to retrieve the value for properties of a given variable type.
// Arguments:
// module - ignored
// args - argument tuple. Must contain the variable type and a python object that implements a GetProperty method.
// -------------------------------------------------------------
PyObject* PyRegisterPropertyHandler( PyObject* module, PyObject* args )
{
VariableType varType;
PyObject* handler;
if ( PyArg_ParseTuple( args, "iO", &varType, &handler ) )
{
if ( varType < 0 || varType >= VARIABLETYPE_MAX )
{
PyErr_SetString( PyExc_TypeError, "Unknown variable type" );
return NULL;
}
Py_XINCREF( handler );
PropertyHandlerMapIt it = g_settings.propertyHandlerMap.find( varType );
if ( it != g_settings.propertyHandlerMap.end() )
{
Py_XDECREF( it->second );
it->second = handler;
}
else
{
g_settings.propertyHandlerMap.insert( std::pair< VariableType, PyObject* >( varType, handler ) );
}
}
Py_RETURN_NONE;
}
MAP_FUNCTION( "RegisterPropertyHandler", PyRegisterPropertyHandler, "Registers a handler class instance for a specific variable type." );
// -------------------------------------------------------------
// Description:
// Parses the passed in string with provided language and tokens instead of fetching them from the messagedata storage.
// The language must have been loaded before, e.g. by loading messagedata for said language, otherwise a KeyError will be raised.
// Arguments:
// module - ignored
// args - argument tuple. Must contain sourceText, languageID and token dictionary
// kwargs - keyword argument dictionary.
// -------------------------------------------------------------
PyObject* PyParse( PyObject* module, PyObject* args, PyObject* kwargs )
{
std::wstringstream retVal;
TokenContainer tokens;
PyObject* sourceTextObject;
wchar_t* sourceText;
PyObject* tokenDict;
char* languageCode;
if ( !PyArg_ParseTuple( args, "UsO", &sourceTextObject, &languageCode, &tokenDict ) )
{
return nullptr;
}
sourceText = PyUnicode_AsWideCharString(sourceTextObject, nullptr);
LanguageID langID = CodeToLanguageID( languageCode );
LanguageMapCit lang = g_settings.languages.find( langID );
if ( lang == g_settings.languages.cend() )
{
char tmp[128];
sprintf_s(tmp, "Language %s does not exist.", languageCode );
PyErr_SetString( PyExc_KeyError, tmp );
return nullptr;
}
MessageData md;
if ( !LoadTokens( tokenDict, md ) )
{
return nullptr;
}
std::wstringstream text;
if ( md.tokens )
{
bool ret = Parse( sourceText, *(lang->second), *( md.tokens ), kwargs, text );
if ( ! ret )
{
// Actual exception object should be set in Parse.
return nullptr;
}
}
else
{
text << sourceText;
}
std::wstring tmp = text.str();
return PyUnicode_FromWideChar( tmp.c_str(), tmp.size() );
}
// Mapping from localization.cpp due to kwargs