From 5b26bc470934f9b4811ba6dda9747b02a8704214 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 6 Jul 2016 17:46:17 -0600 Subject: [PATCH] light: add EntData_Write and EntData_Parse (unused) --- light/entities.cc | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/light/entities.cc b/light/entities.cc index 0c41f1a1..3828386c 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -831,6 +832,86 @@ SetupLightLeafnums(const bsp2_t *bsp) } } +using entdict_t = std::map; + +/* + * ================== + * EntData_Parse + * ================== + */ +std::vector +EntData_Parse(const char *entdata) +{ + std::vector result; + const char *data = entdata; + + /* go through all the entities */ + while (1) { + /* parse the opening brace */ + data = COM_Parse(data); + if (!data) + break; + if (com_token[0] != '{') + Error("%s: found %s when expecting {", __func__, com_token); + + /* Allocate a new entity */ + entdict_t entity; + + /* go through all the keys in this entity */ + while (1) { + /* parse key */ + data = COM_Parse(data); + if (!data) + Error("%s: EOF without closing brace", __func__); + + std::string keystr { com_token }; + + if (keystr == "}") + break; + if (keystr.length() > MAX_ENT_KEY - 1) + Error("%s: Key length > %i", __func__, MAX_ENT_KEY - 1); + + /* parse value */ + data = COM_Parse(data); + if (!data) + Error("%s: EOF without closing brace", __func__); + + std::string valstring { com_token }; + + if (valstring[0] == '}') + Error("%s: closing brace without data", __func__); + if (valstring.length() > MAX_ENT_VALUE - 1) + Error("%s: Value length > %i", __func__, MAX_ENT_VALUE - 1); + + entity[keystr] = valstring; + } + + result.push_back(entity); + } + + logprint("%d entities read", static_cast(result.size())); + return result; +} + +/* + * ================ + * EntData_Write + * ================ + */ +std::string +EntData_Write(const std::vector &ents) +{ + std::stringstream out; + for (const auto &ent : ents) { + out << "{\n"; + for (const auto &epair : ent) { + out << "\"" << epair.first << "\" \"" << epair.second << "\"\n"; + } + out << "}\n"; + } + return out.str(); +} + /* * ================== * LoadEntities