18 #include "precompiled.h"
34 #pragma warning(disable:4512)
37 #include <boost/algorithm/string.hpp>
38 #include <boost/algorithm/string/join.hpp>
79 CStrW value = element.
GetText().FromUTF8();
81 bool hasSetValue =
false;
87 bool replacing =
false;
91 if (attr.Name == at_disable)
96 else if (attr.Name == at_replace)
106 if (attr.Name == at_datatype && std::wstring(attr.Value.begin(), attr.Value.end()) == L
"tokens")
111 std::vector<std::wstring> oldTokens;
112 std::vector<std::wstring> newTokens;
114 boost::algorithm::split(oldTokens, node.
m_Value, boost::algorithm::is_space());
115 boost::algorithm::split(newTokens, value, boost::algorithm::is_space());
118 oldTokens.erase(std::remove_if(oldTokens.begin(), oldTokens.end(), std::mem_fun_ref(&std::wstring::empty)), oldTokens.end());
119 newTokens.erase(std::remove_if(newTokens.begin(), newTokens.end(), std::mem_fun_ref(&std::wstring::empty)), newTokens.end());
122 std::vector<std::wstring> tokens = oldTokens;
123 for (
size_t i = 0; i < newTokens.size(); ++i)
125 if (newTokens[i][0] == L
'-')
127 std::vector<std::wstring>::iterator tokenIt = std::find(tokens.begin(), tokens.end(), newTokens[i].substr(1));
128 if (tokenIt != tokens.end())
129 tokens.erase(tokenIt);
131 LOGWARNING(L
"[ParamNode] Could not remove token '%ls' from node '%hs'%ls; not present in list nor inherited (possible typo?)",
132 newTokens[i].substr(1).c_str(), name.c_str(), sourceIdentifier ? (L
" in '" + std::wstring(sourceIdentifier) + L
"'").c_str() : L
"");
136 if (std::find(oldTokens.begin(), oldTokens.end(), newTokens[i]) == oldTokens.end())
137 tokens.push_back(newTokens[i]);
141 node.
m_Value = boost::algorithm::join(tokens, L
" ");
156 node.
ApplyLayer(xmb, child, sourceIdentifier);
163 if (attr.Name == at_replace)
continue;
166 node.
m_Childs[
"@" + attrName].m_Value = attr.Value.FromUTF8();
174 ChildrenMap::iterator dstChild =
m_Childs.find(name);
175 ChildrenMap::const_iterator srcChild = src.
m_Childs.find(name);
179 ChildrenMap::const_iterator it = srcChild->second.m_Childs.begin();
180 for (; it != srcChild->second.m_Childs.end(); ++it)
181 if (permitted.count(it->first))
182 dstChild->second.m_Childs[it->first] = it->second;
187 ChildrenMap::const_iterator it =
m_Childs.find(name);
216 std::wstringstream strm;
230 std::wstringstream strm;
252 ret.reserve(str.size());
253 for (
size_t i = 0; i < str.size(); ++i)
258 case '<': ret += L
"<";
break;
259 case '>': ret += L
">";
break;
260 case '&': ret += L
"&";
break;
261 case '"': ret += L
""";
break;
262 case '\t': ret += L
"	";
break;
263 case '\n': ret += L
" ";
break;
264 case '\r': ret += L
" ";
break;
266 if ((0x20 <= c && c <= 0xD7FF) || (0xE000 <= c && c <= 0xFFFD))
277 std::wstringstream strm;
286 ChildrenMap::const_iterator it =
m_Childs.begin();
290 if (it->first.length() && it->first[0] ==
'@')
293 std::wstring name (it->first.begin(), it->first.end());
295 strm << L
"<" << name;
298 ChildrenMap::const_iterator cit = it->second.m_Childs.begin();
299 for (; cit != it->second.m_Childs.end(); ++cit)
301 if (cit->first.length() && cit->first[0] ==
'@')
303 std::wstring attrname (cit->first.begin()+1, cit->first.end());
304 strm << L
" " << attrname << L
"=\"" <<
EscapeXMLString(cit->second.m_Value) << L
"\"";
310 it->second.ToXML(strm);
312 strm << L
"</" << name <<
">";
339 JSString* str = JS_InternUCStringN(cx, reinterpret_cast<const jschar*>(text.data()), text.length());
341 return STRING_TO_JSVAL(str);
348 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
352 for (std::map<std::string, CParamNode>::const_iterator it =
m_Childs.begin(); it !=
m_Childs.end(); ++it)
354 jsval childVal = it->second.ConstructJSVal(cx);
355 if (!JS_SetProperty(cx, obj, it->first.c_str(), &childVal))
363 JSString* str = JS_InternUCStringN(cx, reinterpret_cast<const jschar*>(text.data()), text.length());
366 jsval childVal = STRING_TO_JSVAL(str);
367 if (!JS_SetProperty(cx, obj,
"_string", &childVal))
371 return OBJECT_TO_JSVAL(obj);
An entity initialisation parameter node.
CParamNode(bool isOk=true)
Constructs a new, empty node.
A simple fixed-point number class.
jsval ConstructJSVal(JSContext *cx) const
bool uninitialised() const
Returns whether the value is uninitialised.
bool ToBool() const
Parses the content of this node as a boolean ("true" == true, anything else == false) ...
void ApplyLayer(const XMBFile &xmb, const XMBElement &element, const wchar_t *sourceIdentifier=NULL)
Overlays the specified data onto this node.
static std::wstring EscapeXMLString(const std::wstring &str)
Escapes a string so that it is well-formed XML content/attribute text.
const std::wstring & ToString() const
Returns the content of this node as a string.
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Load from an XML file (with invisible XMB caching).
#define XERO_ITER_ATTR(parent_element, attribute)
const std::string ToUTF8() const
Returns the content of this node as an 8-bit string.
const PSRETURN PSRETURN_OK
bool IsOk() const
Returns true if this is a valid CParamNode, false if it represents a non-existent node...
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
#define XERO_ITER_EL(parent_element, child_element)
int ToInt() const
Parses the content of this node as an integer.
const ChildrenMap & GetChildren() const
Returns the names/nodes of the children of this node, ordered by name.
static CParamNode g_NullNode(false)
fixed ToFixed() const
Parses the content of this node as a fixed-point number.
static void LoadXML(CParamNode &ret, const XMBFile &file, const wchar_t *sourceIdentifier=NULL)
Loads the XML data specified by file into the node ret.
const CParamNode & GetChild(const char *name) const
Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none...
float ToFloat() const
Parses the content of this node as a floating-point number.
static PSRETURN LoadXMLString(CParamNode &ret, const char *xml, const wchar_t *sourceIdentifier=NULL)
See LoadXML, but parses the XML string xml.
const String & string() const
PSRETURN LoadString(const char *xml)
Load from an in-memory XML string (with no caching).
static CFixed FromString(const CStr8 &s)
std::string GetElementString(const int ID) const
std::basic_string< utf16_t, utf16_traits > utf16string
XMBElement GetRoot() const
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
void CopyFilteredChildrenOfChild(const CParamNode &src, const char *name, const std::set< std::string > &permitted)
Finds the childs named name from src and from this, and copies the source child's children which are ...
int GetAttributeID(const char *Name) const
CScriptValRooted m_ScriptVal
Caches the ToJSVal script representation of this node.
std::string GetAttributeString(const int ID) const
const CStrIntern ToUTF8Intern() const
Returns the content of this node as an internalized 8-bit string.
std::wstring ToXML() const
Returns the content of this node and its children as an XML string.
std::map< std::string, CParamNode > ChildrenMap
jsval ToJSVal(JSContext *cx, bool cacheValue) const
Returns a jsval representation of this node and its children.