minor refactor
This commit is contained in:
+3
-27
@@ -1,5 +1,7 @@
|
|||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
|
|
||||||
|
#include "../util/stringutil.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -38,32 +40,6 @@ std::string parsing_error::errorLog() const {
|
|||||||
}
|
}
|
||||||
ss << "^";
|
ss << "^";
|
||||||
return ss.str();
|
return ss.str();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string escape_string(std::string s) {
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << '"';
|
|
||||||
for (char c : s) {
|
|
||||||
switch (c) {
|
|
||||||
case '\n': ss << "\\n"; break;
|
|
||||||
case '\r': ss << "\\r"; break;
|
|
||||||
case '\t': ss << "\\t"; break;
|
|
||||||
case '\f': ss << "\\f"; break;
|
|
||||||
case '\b': ss << "\\b"; break;
|
|
||||||
case '"': ss << "\\\""; break;
|
|
||||||
case '\\': ss << "\\\\"; break;
|
|
||||||
default:
|
|
||||||
if (c < ' ') {
|
|
||||||
ss << "\\" << std::oct << uint(ubyte(c));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ss << c;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ss << '"';
|
|
||||||
return ss.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicParser::BasicParser(std::string file, std::string source) : filename(file), source(source) {
|
BasicParser::BasicParser(std::string file, std::string source) : filename(file), source(source) {
|
||||||
@@ -151,7 +127,7 @@ void BasicParser::expect(const std::string& substring) {
|
|||||||
return;
|
return;
|
||||||
for (uint i = 0; i < substring.length(); i++) {
|
for (uint i = 0; i < substring.length(); i++) {
|
||||||
if (source.length() <= pos + i || source[pos+i] != substring[i]) {
|
if (source.length() <= pos + i || source[pos+i] != substring[i]) {
|
||||||
throw error(escape_string(substring)+" expected");
|
throw error(util::quote(substring)+" expected");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pos += substring.length();
|
pos += substring.length();
|
||||||
|
|||||||
@@ -49,8 +49,6 @@ inline int hexchar2int(int c) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern std::string escape_string(std::string s);
|
|
||||||
|
|
||||||
class parsing_error : public std::runtime_error {
|
class parsing_error : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
std::string filename;
|
std::string filename;
|
||||||
@@ -59,13 +57,14 @@ public:
|
|||||||
uint line;
|
uint line;
|
||||||
uint linestart;
|
uint linestart;
|
||||||
|
|
||||||
parsing_error(std::string message,
|
parsing_error(
|
||||||
std::string filename,
|
std::string message,
|
||||||
std::string source,
|
std::string filename,
|
||||||
uint pos,
|
std::string source,
|
||||||
uint line,
|
uint pos,
|
||||||
uint linestart);
|
uint line,
|
||||||
|
uint linestart
|
||||||
|
);
|
||||||
std::string errorLog() const;
|
std::string errorLog() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
#include "../data/dynamic.h"
|
#include "../data/dynamic.h"
|
||||||
|
#include "../util/stringutil.h"
|
||||||
|
|
||||||
using namespace json;
|
using namespace json;
|
||||||
using namespace dynamic;
|
using namespace dynamic;
|
||||||
@@ -74,7 +75,7 @@ void stringify(const Value* value,
|
|||||||
} else if (value->type == valtype::integer) {
|
} else if (value->type == valtype::integer) {
|
||||||
ss << std::get<integer_t>(value->value);
|
ss << std::get<integer_t>(value->value);
|
||||||
} else if (value->type == valtype::string) {
|
} else if (value->type == valtype::string) {
|
||||||
ss << escape_string(std::get<std::string>(value->value));
|
ss << util::escape(std::get<std::string>(value->value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +96,7 @@ void stringifyObj(const Map* obj,
|
|||||||
newline(ss, nice, indent, indentstr);
|
newline(ss, nice, indent, indentstr);
|
||||||
}
|
}
|
||||||
Value* value = entry.second.get();
|
Value* value = entry.second.get();
|
||||||
ss << escape_string(key) << ": ";
|
ss << util::escape(key) << ": ";
|
||||||
stringify(value, ss, indent+1, indentstr, nice);
|
stringify(value, ss, indent+1, indentstr, nice);
|
||||||
index++;
|
index++;
|
||||||
if (index < obj->values.size()) {
|
if (index < obj->values.size()) {
|
||||||
|
|||||||
+25
-25
@@ -1,5 +1,6 @@
|
|||||||
#include "toml.h"
|
#include "toml.h"
|
||||||
#include "commons.h"
|
#include "commons.h"
|
||||||
|
#include "../util/stringutil.h"
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -7,11 +8,9 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
using namespace toml;
|
using namespace toml;
|
||||||
|
|
||||||
Section::Section(string name) : name(name) {
|
Section::Section(std::string name) : name(name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(std::string name, Field field) {
|
void Section::add(std::string name, Field field) {
|
||||||
@@ -22,35 +21,35 @@ void Section::add(std::string name, Field field) {
|
|||||||
keyOrder.push_back(name);
|
keyOrder.push_back(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, bool* ptr) {
|
void Section::add(std::string name, bool* ptr) {
|
||||||
add(name, {fieldtype::ftbool, ptr});
|
add(name, {fieldtype::ftbool, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, int* ptr) {
|
void Section::add(std::string name, int* ptr) {
|
||||||
add(name, {fieldtype::ftint, ptr});
|
add(name, {fieldtype::ftint, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, uint* ptr) {
|
void Section::add(std::string name, uint* ptr) {
|
||||||
add(name, {fieldtype::ftuint, ptr});
|
add(name, {fieldtype::ftuint, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, float* ptr) {
|
void Section::add(std::string name, float* ptr) {
|
||||||
add(name, {fieldtype::ftfloat, ptr});
|
add(name, {fieldtype::ftfloat, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, double* ptr) {
|
void Section::add(std::string name, double* ptr) {
|
||||||
add(name, {fieldtype::ftdouble, ptr});
|
add(name, {fieldtype::ftdouble, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::add(string name, string* ptr) {
|
void Section::add(std::string name, std::string* ptr) {
|
||||||
add(name, {fieldtype::ftstring, ptr});
|
add(name, {fieldtype::ftstring, ptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
string Section::getName() const {
|
const std::string& Section::getName() const {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Field* Section::field(std::string name) const {
|
const Field* Section::field(const std::string& name) const {
|
||||||
auto found = fields.find(name);
|
auto found = fields.find(name);
|
||||||
if (found == fields.end()) {
|
if (found == fields.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -88,10 +87,10 @@ Section* Wrapper::section(std::string name) {
|
|||||||
|
|
||||||
std::string Wrapper::write() const {
|
std::string Wrapper::write() const {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
for (string key : keyOrder) {
|
for (const std::string& key : keyOrder) {
|
||||||
const Section* section = sections.at(key);
|
const Section* section = sections.at(key);
|
||||||
ss << "[" << key << "]\n";
|
ss << "[" << key << "]\n";
|
||||||
for (const string& key : section->keys()) {
|
for (const std::string& key : section->keys()) {
|
||||||
ss << key << " = ";
|
ss << key << " = ";
|
||||||
const Field* field = section->field(key);
|
const Field* field = section->field(key);
|
||||||
assert(field != nullptr);
|
assert(field != nullptr);
|
||||||
@@ -104,7 +103,7 @@ std::string Wrapper::write() const {
|
|||||||
case fieldtype::ftfloat: ss << *((float*)field->ptr); break;
|
case fieldtype::ftfloat: ss << *((float*)field->ptr); break;
|
||||||
case fieldtype::ftdouble: ss << *((double*)field->ptr); break;
|
case fieldtype::ftdouble: ss << *((double*)field->ptr); break;
|
||||||
case fieldtype::ftstring:
|
case fieldtype::ftstring:
|
||||||
ss << escape_string(*((const string*)field->ptr));
|
ss << util::escape(*((const std::string*)field->ptr));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ss << "\n";
|
ss << "\n";
|
||||||
@@ -114,7 +113,8 @@ std::string Wrapper::write() const {
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Reader::Reader(Wrapper* wrapper, string file, string source) : BasicParser(file, source), wrapper(wrapper) {
|
Reader::Reader(Wrapper* wrapper, std::string file, std::string source)
|
||||||
|
: BasicParser(file, source), wrapper(wrapper) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reader::skipWhitespace() {
|
void Reader::skipWhitespace() {
|
||||||
@@ -135,7 +135,7 @@ void Reader::read() {
|
|||||||
readSection(nullptr);
|
readSection(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::set(string name, double value) {
|
void Section::set(const std::string& name, double value) {
|
||||||
const Field* field = this->field(name);
|
const Field* field = this->field(name);
|
||||||
if (field == nullptr) {
|
if (field == nullptr) {
|
||||||
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
||||||
@@ -146,14 +146,14 @@ void Section::set(string name, double value) {
|
|||||||
case fieldtype::ftuint: *(uint*)(field->ptr) = value; break;
|
case fieldtype::ftuint: *(uint*)(field->ptr) = value; break;
|
||||||
case fieldtype::ftfloat: *(float*)(field->ptr) = value; break;
|
case fieldtype::ftfloat: *(float*)(field->ptr) = value; break;
|
||||||
case fieldtype::ftdouble: *(double*)(field->ptr) = value; break;
|
case fieldtype::ftdouble: *(double*)(field->ptr) = value; break;
|
||||||
case fieldtype::ftstring: *(string*)(field->ptr) = std::to_string(value); break;
|
case fieldtype::ftstring: *(std::string*)(field->ptr) = std::to_string(value); break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::set(std::string name, bool value) {
|
void Section::set(const std::string& name, bool value) {
|
||||||
const Field* field = this->field(name);
|
const Field* field = this->field(name);
|
||||||
if (field == nullptr) {
|
if (field == nullptr) {
|
||||||
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
||||||
@@ -164,20 +164,20 @@ void Section::set(std::string name, bool value) {
|
|||||||
case fieldtype::ftuint: *(uint*)(field->ptr) = (uint)value; break;
|
case fieldtype::ftuint: *(uint*)(field->ptr) = (uint)value; break;
|
||||||
case fieldtype::ftfloat: *(float*)(field->ptr) = (float)value; break;
|
case fieldtype::ftfloat: *(float*)(field->ptr) = (float)value; break;
|
||||||
case fieldtype::ftdouble: *(double*)(field->ptr) = (double)value; break;
|
case fieldtype::ftdouble: *(double*)(field->ptr) = (double)value; break;
|
||||||
case fieldtype::ftstring: *(string*)(field->ptr) = value ? "true" : "false"; break;
|
case fieldtype::ftstring: *(std::string*)(field->ptr) = value ? "true" : "false"; break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section::set(std::string name, std::string value) {
|
void Section::set(const std::string& name, std::string value) {
|
||||||
const Field* field = this->field(name);
|
const Field* field = this->field(name);
|
||||||
if (field == nullptr) {
|
if (field == nullptr) {
|
||||||
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
switch (field->type) {
|
switch (field->type) {
|
||||||
case fieldtype::ftstring: *(string*)(field->ptr) = value; break;
|
case fieldtype::ftstring: *(std::string*)(field->ptr) = value; break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
std::cerr << "error: type error for key '" << name << "'" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -192,14 +192,14 @@ void Reader::readSection(Section* section /*nullable*/) {
|
|||||||
}
|
}
|
||||||
char c = nextChar();
|
char c = nextChar();
|
||||||
if (c == '[') {
|
if (c == '[') {
|
||||||
string name = parseName();
|
std::string name = parseName();
|
||||||
Section* section = wrapper->section(name);
|
Section* section = wrapper->section(name);
|
||||||
pos++;
|
pos++;
|
||||||
readSection(section);
|
readSection(section);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pos--;
|
pos--;
|
||||||
string name = parseName();
|
std::string name = parseName();
|
||||||
expect('=');
|
expect('=');
|
||||||
c = peek();
|
c = peek();
|
||||||
if (is_digit(c)) {
|
if (is_digit(c)) {
|
||||||
@@ -223,7 +223,7 @@ void Reader::readSection(Section* section /*nullable*/) {
|
|||||||
section->set(name, std::get<number_t>(num));
|
section->set(name, std::get<number_t>(num));
|
||||||
}
|
}
|
||||||
} else if (is_identifier_start(c)) {
|
} else if (is_identifier_start(c)) {
|
||||||
string identifier = parseName();
|
std::string identifier = parseName();
|
||||||
if (identifier == "true" || identifier == "false") {
|
if (identifier == "true" || identifier == "false") {
|
||||||
bool flag = identifier == "true";
|
bool flag = identifier == "true";
|
||||||
if (section) {
|
if (section) {
|
||||||
@@ -240,7 +240,7 @@ void Reader::readSection(Section* section /*nullable*/) {
|
|||||||
}
|
}
|
||||||
} else if (c == '"' || c == '\'') {
|
} else if (c == '"' || c == '\'') {
|
||||||
pos++;
|
pos++;
|
||||||
string str = parseString(c);
|
std::string str = parseString(c);
|
||||||
if (section) {
|
if (section) {
|
||||||
section->set(name, str);
|
section->set(name, str);
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -36,13 +36,13 @@ namespace toml {
|
|||||||
void add(std::string name, double* ptr);
|
void add(std::string name, double* ptr);
|
||||||
void add(std::string name, std::string* ptr);
|
void add(std::string name, std::string* ptr);
|
||||||
|
|
||||||
const Field* field(std::string name) const;
|
const Field* field(const std::string& name) const;
|
||||||
|
|
||||||
void set(std::string name, double value);
|
void set(const std::string& name, double value);
|
||||||
void set(std::string name, bool value);
|
void set(const std::string& name, bool value);
|
||||||
void set(std::string name, std::string value);
|
void set(const std::string& name, std::string value);
|
||||||
|
|
||||||
std::string getName() const;
|
const std::string& getName() const;
|
||||||
const std::vector<std::string>& keys() const;
|
const std::vector<std::string>& keys() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -52,7 +52,7 @@ glm::vec3 Attribute::asVec3() const {
|
|||||||
}
|
}
|
||||||
size_t pos2 = text.find(',', pos1+1);
|
size_t pos2 = text.find(',', pos1+1);
|
||||||
if (pos2 == std::string::npos) {
|
if (pos2 == std::string::npos) {
|
||||||
throw std::runtime_error("invalid vec3 value "+escape_string(text));
|
throw std::runtime_error("invalid vec3 value "+util::quote(text));
|
||||||
}
|
}
|
||||||
return glm::vec3(
|
return glm::vec3(
|
||||||
util::parse_double(text, 0, pos1),
|
util::parse_double(text, 0, pos1),
|
||||||
@@ -69,11 +69,11 @@ glm::vec4 Attribute::asVec4() const {
|
|||||||
}
|
}
|
||||||
size_t pos2 = text.find(',', pos1+1);
|
size_t pos2 = text.find(',', pos1+1);
|
||||||
if (pos2 == std::string::npos) {
|
if (pos2 == std::string::npos) {
|
||||||
throw std::runtime_error("invalid vec4 value "+escape_string(text));
|
throw std::runtime_error("invalid vec4 value "+util::quote(text));
|
||||||
}
|
}
|
||||||
size_t pos3 = text.find(',', pos2+1);
|
size_t pos3 = text.find(',', pos2+1);
|
||||||
if (pos3 == std::string::npos) {
|
if (pos3 == std::string::npos) {
|
||||||
throw std::runtime_error("invalid vec4 value "+escape_string(text));
|
throw std::runtime_error("invalid vec4 value "+util::quote(text));
|
||||||
}
|
}
|
||||||
return glm::vec4(
|
return glm::vec4(
|
||||||
util::parse_double(text, 0, pos1),
|
util::parse_double(text, 0, pos1),
|
||||||
@@ -382,7 +382,7 @@ static void stringifyElement(
|
|||||||
auto attr = entry.second;
|
auto attr = entry.second;
|
||||||
ss << attr.getName();
|
ss << attr.getName();
|
||||||
if (!attr.getText().empty()) {
|
if (!attr.getText().empty()) {
|
||||||
ss << "=" << escape_string(attr.getText());
|
ss << "=" << util::escape(attr.getText());
|
||||||
}
|
}
|
||||||
if (count + 1 < int(attrs.size())) {
|
if (count + 1 < int(attrs.size())) {
|
||||||
ss << " ";
|
ss << " ";
|
||||||
|
|||||||
+31
-1
@@ -7,6 +7,36 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
// TODO: finish
|
||||||
|
std::string util::escape(const std::string& s) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << '"';
|
||||||
|
for (char c : s) {
|
||||||
|
switch (c) {
|
||||||
|
case '\n': ss << "\\n"; break;
|
||||||
|
case '\r': ss << "\\r"; break;
|
||||||
|
case '\t': ss << "\\t"; break;
|
||||||
|
case '\f': ss << "\\f"; break;
|
||||||
|
case '\b': ss << "\\b"; break;
|
||||||
|
case '"': ss << "\\\""; break;
|
||||||
|
case '\\': ss << "\\\\"; break;
|
||||||
|
default:
|
||||||
|
if (c < ' ') {
|
||||||
|
ss << "\\" << std::oct << uint(ubyte(c));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ss << c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ss << '"';
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string util::quote(const std::string& s) {
|
||||||
|
return escape(s);
|
||||||
|
}
|
||||||
|
|
||||||
std::wstring util::lfill(std::wstring s, uint length, wchar_t c) {
|
std::wstring util::lfill(std::wstring s, uint length, wchar_t c) {
|
||||||
if (s.length() >= length) {
|
if (s.length() >= length) {
|
||||||
return s;
|
return s;
|
||||||
@@ -62,7 +92,7 @@ struct utf_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const utf_t utf[] = {
|
const utf_t utf[] = {
|
||||||
/* mask lead beg end bits */
|
/* mask lead beg end bits */
|
||||||
{(char)0b00111111, (char)0b10000000, 0, 0, 6},
|
{(char)0b00111111, (char)0b10000000, 0, 0, 6},
|
||||||
{(char)0b01111111, (char)0b00000000, 0000, 0177, 7},
|
{(char)0b01111111, (char)0b00000000, 0000, 0177, 7},
|
||||||
{(char)0b00011111, (char)0b11000000, 0200, 03777, 5},
|
{(char)0b00011111, (char)0b11000000, 0200, 03777, 5},
|
||||||
|
|||||||
@@ -6,6 +6,12 @@
|
|||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
/// @brief Function used for string serialization in text formats
|
||||||
|
extern std::string escape(const std::string& s);
|
||||||
|
|
||||||
|
/// @brief Function used for error messages
|
||||||
|
extern std::string quote(const std::string& s);
|
||||||
|
|
||||||
extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
|
extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
|
||||||
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
|
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user