minor refactor
This commit is contained in:
parent
8001a8bf86
commit
9d9c9f5570
@ -7,11 +7,9 @@
|
|||||||
#include "../files/files.h"
|
#include "../files/files.h"
|
||||||
#include "../files/engine_paths.h"
|
#include "../files/engine_paths.h"
|
||||||
|
|
||||||
using std::string;
|
|
||||||
using std::filesystem::path;
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
void GLSLExtension::setVersion(string version) {
|
void GLSLExtension::setVersion(std::string version) {
|
||||||
this->version = version;
|
this->version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,21 +17,21 @@ void GLSLExtension::setPaths(const ResPaths* paths) {
|
|||||||
this->paths = paths;
|
this->paths = paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSLExtension::loadHeader(string name) {
|
void GLSLExtension::loadHeader(std::string name) {
|
||||||
path file = paths->find("shaders/lib/"+name+".glsl");
|
fs::path file = paths->find("shaders/lib/"+name+".glsl");
|
||||||
string source = files::read_string(file);
|
std::string source = files::read_string(file);
|
||||||
addHeader(name, source);
|
addHeader(name, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSLExtension::addHeader(string name, string source) {
|
void GLSLExtension::addHeader(std::string name, std::string source) {
|
||||||
headers[name] = source;
|
headers[name] = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSLExtension::define(string name, string value) {
|
void GLSLExtension::define(std::string name, std::string value) {
|
||||||
defines[name] = value;
|
defines[name] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string& GLSLExtension::getHeader(const string name) const {
|
const std::string& GLSLExtension::getHeader(const std::string& name) const {
|
||||||
auto found = headers.find(name);
|
auto found = headers.find(name);
|
||||||
if (found == headers.end()) {
|
if (found == headers.end()) {
|
||||||
throw std::runtime_error("no header '"+name+"' loaded");
|
throw std::runtime_error("no header '"+name+"' loaded");
|
||||||
@ -41,7 +39,7 @@ const string& GLSLExtension::getHeader(const string name) const {
|
|||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string GLSLExtension::getDefine(const string name) const {
|
const std::string GLSLExtension::getDefine(const std::string& name) const {
|
||||||
auto found = defines.find(name);
|
auto found = defines.find(name);
|
||||||
if (found == defines.end()) {
|
if (found == defines.end()) {
|
||||||
return "";
|
return "";
|
||||||
@ -49,41 +47,41 @@ const string GLSLExtension::getDefine(const string name) const {
|
|||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GLSLExtension::hasDefine(const string name) const {
|
bool GLSLExtension::hasDefine(const std::string& name) const {
|
||||||
return defines.find(name) != defines.end();
|
return defines.find(name) != defines.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GLSLExtension::hasHeader(const string name) const {
|
bool GLSLExtension::hasHeader(const std::string& name) const {
|
||||||
return headers.find(name) != headers.end();
|
return headers.find(name) != headers.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSLExtension::undefine(string name) {
|
void GLSLExtension::undefine(std::string name) {
|
||||||
if (hasDefine(name)) {
|
if (hasDefine(name)) {
|
||||||
defines.erase(name);
|
defines.erase(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::runtime_error parsing_error(
|
inline std::runtime_error parsing_error(
|
||||||
const path& file,
|
const fs::path& file,
|
||||||
uint linenum,
|
uint linenum,
|
||||||
const string message) {
|
const std::string message) {
|
||||||
return std::runtime_error("file "+file.string()+": "+message+
|
return std::runtime_error("file "+file.string()+": "+message+
|
||||||
" at line "+std::to_string(linenum));
|
" at line "+std::to_string(linenum));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void parsing_warning(
|
inline void parsing_warning(
|
||||||
const path& file,
|
const fs::path& file,
|
||||||
uint linenum, const
|
uint linenum, const
|
||||||
string message) {
|
std::string message) {
|
||||||
std::cerr << "file "+file.string()+": warning: "+message+
|
std::cerr << "file "+file.string()+": warning: "+message+
|
||||||
" at line "+std::to_string(linenum) << std::endl;
|
" at line "+std::to_string(linenum) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void source_line(std::stringstream& ss, uint linenum) {
|
inline void source_line(std::stringstream& ss, uint linenum) {
|
||||||
ss << "#line " << linenum << "\n";
|
ss << "#line " << linenum << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
const string GLSLExtension::process(const path file, const string& source) {
|
const std::string GLSLExtension::process(const fs::path file, const std::string& source) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
uint linenum = 1;
|
uint linenum = 1;
|
||||||
@ -94,15 +92,15 @@ const string GLSLExtension::process(const path file, const string& source) {
|
|||||||
source_line(ss, linenum);
|
source_line(ss, linenum);
|
||||||
while (pos < source.length()) {
|
while (pos < source.length()) {
|
||||||
size_t endline = source.find('\n', pos);
|
size_t endline = source.find('\n', pos);
|
||||||
if (endline == string::npos) {
|
if (endline == std::string::npos) {
|
||||||
endline = source.length();
|
endline = source.length();
|
||||||
}
|
}
|
||||||
// parsing preprocessor directives
|
// parsing preprocessor directives
|
||||||
if (source[pos] == '#') {
|
if (source[pos] == '#') {
|
||||||
string line = source.substr(pos+1, endline-pos);
|
std::string line = source.substr(pos+1, endline-pos);
|
||||||
util::trim(line);
|
util::trim(line);
|
||||||
// parsing 'include' directive
|
// parsing 'include' directive
|
||||||
if (line.find("include") != string::npos) {
|
if (line.find("include") != std::string::npos) {
|
||||||
line = line.substr(7);
|
line = line.substr(7);
|
||||||
util::trim(line);
|
util::trim(line);
|
||||||
if (line.length() < 3) {
|
if (line.length() < 3) {
|
||||||
@ -113,7 +111,7 @@ const string GLSLExtension::process(const path file, const string& source) {
|
|||||||
throw parsing_error(file, linenum,
|
throw parsing_error(file, linenum,
|
||||||
"expected '#include <filename>' syntax");
|
"expected '#include <filename>' syntax");
|
||||||
}
|
}
|
||||||
string name = line.substr(1, line.length()-2);
|
std::string name = line.substr(1, line.length()-2);
|
||||||
if (!hasHeader(name)) {
|
if (!hasHeader(name)) {
|
||||||
loadHeader(name);
|
loadHeader(name);
|
||||||
}
|
}
|
||||||
@ -125,7 +123,7 @@ const string GLSLExtension::process(const path file, const string& source) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// removing extra 'include' directives
|
// removing extra 'include' directives
|
||||||
else if (line.find("version") != string::npos) {
|
else if (line.find("version") != std::string::npos) {
|
||||||
parsing_warning(file, linenum, "removed #version directive");
|
parsing_warning(file, linenum, "removed #version directive");
|
||||||
pos = endline+1;
|
pos = endline+1;
|
||||||
linenum++;
|
linenum++;
|
||||||
@ -138,4 +136,4 @@ const string GLSLExtension::process(const path file, const string& source) {
|
|||||||
pos = endline+1;
|
pos = endline+1;
|
||||||
}
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,11 +23,11 @@ public:
|
|||||||
void undefine(std::string name);
|
void undefine(std::string name);
|
||||||
void addHeader(std::string name, std::string source);
|
void addHeader(std::string name, std::string source);
|
||||||
|
|
||||||
const std::string& getHeader(const std::string name) const;
|
const std::string& getHeader(const std::string& name) const;
|
||||||
const std::string getDefine(const std::string name) const;
|
const std::string getDefine(const std::string& name) const;
|
||||||
|
|
||||||
bool hasHeader(const std::string name) const;
|
bool hasHeader(const std::string& name) const;
|
||||||
bool hasDefine(const std::string name) const;
|
bool hasDefine(const std::string& name) const;
|
||||||
|
|
||||||
const std::string process(const std::filesystem::path file, const std::string& source);
|
const std::string process(const std::filesystem::path file, const std::string& source);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -14,10 +14,10 @@ namespace xml {
|
|||||||
class Attribute;
|
class Attribute;
|
||||||
class Document;
|
class Document;
|
||||||
|
|
||||||
typedef Attribute xmlattribute;
|
using xmlattribute = Attribute;
|
||||||
typedef std::shared_ptr<Node> xmlelement;
|
using xmlelement = std::shared_ptr<Node>;
|
||||||
typedef std::shared_ptr<Document> xmldocument;
|
using xmldocument = std::shared_ptr<Document>;
|
||||||
typedef std::unordered_map<std::string, xmlattribute> xmlelements_map;
|
using xmlelements_map = std::unordered_map<std::string, xmlattribute>;
|
||||||
|
|
||||||
class Attribute {
|
class Attribute {
|
||||||
std::string name;
|
std::string name;
|
||||||
@ -37,7 +37,7 @@ namespace xml {
|
|||||||
glm::vec4 asColor() const;
|
glm::vec4 asColor() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* XML element class. Text element has tag 'text' and attribute 'text' */
|
/// @brief XML element class. Text element has tag 'text' and attribute 'text'
|
||||||
class Node {
|
class Node {
|
||||||
std::string tag;
|
std::string tag;
|
||||||
std::unordered_map<std::string, xmlattribute> attrs;
|
std::unordered_map<std::string, xmlattribute> attrs;
|
||||||
@ -45,13 +45,15 @@ namespace xml {
|
|||||||
public:
|
public:
|
||||||
Node(std::string tag);
|
Node(std::string tag);
|
||||||
|
|
||||||
/* Add sub-element */
|
/// @brief Add sub-element
|
||||||
void add(xmlelement element);
|
void add(xmlelement element);
|
||||||
|
|
||||||
/* Set attribute value. Creates attribute if does not exists */
|
|
||||||
void set(std::string name, std::string text);
|
|
||||||
|
|
||||||
/* Get element tag */
|
/// @brief Set attribute value. Creates attribute if does not exists
|
||||||
|
/// @param name attribute name
|
||||||
|
/// @param text attribute value
|
||||||
|
void set(std::string name, std::string text);
|
||||||
|
|
||||||
|
/// @brief Get element tag
|
||||||
const std::string& getTag() const;
|
const std::string& getTag() const;
|
||||||
|
|
||||||
inline bool isText() const {
|
inline bool isText() const {
|
||||||
@ -62,27 +64,30 @@ namespace xml {
|
|||||||
return attr("#").getText();
|
return attr("#").getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get attribute by name
|
/// @brief Get attribute by name
|
||||||
@param name attribute name
|
/// @param name attribute name
|
||||||
@throws std::runtime_error if element has no attribute
|
/// @throws std::runtime_error if element has no attribute
|
||||||
@return xmlattribute - {name, value} */
|
/// @return xmlattribute - {name, value}
|
||||||
const xmlattribute attr(const std::string& name) const;
|
const xmlattribute attr(const std::string& name) const;
|
||||||
/* Get attribute by name
|
|
||||||
@param name name
|
/// @brief Get attribute by name
|
||||||
@param def default value will be returned wrapped in xmlattribute
|
/// @param name attribute name
|
||||||
if element has no attribute
|
/// @param def default value will be returned wrapped in xmlattribute
|
||||||
@return xmlattribute - {name, value} or {name, def} if not found*/
|
/// if element has no attribute
|
||||||
|
/// @return xmlattribute - {name, value} or {name, def} if not found*/
|
||||||
const xmlattribute attr(const std::string& name, const std::string& def) const;
|
const xmlattribute attr(const std::string& name, const std::string& def) const;
|
||||||
|
|
||||||
/* Check if element has attribute
|
/// @brief Check if element has attribute
|
||||||
@param name attribute name */
|
/// @param name attribute name
|
||||||
bool has(const std::string& name) const;
|
bool has(const std::string& name) const;
|
||||||
|
|
||||||
/* Get sub-element by index
|
/// @brief Get sub-element by index
|
||||||
@throws std::out_of_range if an invalid index given */
|
/// @param index sub-element index
|
||||||
|
/// @throws std::out_of_range if an invalid index given
|
||||||
|
/// @return sub-element
|
||||||
xmlelement sub(size_t index);
|
xmlelement sub(size_t index);
|
||||||
|
|
||||||
/* Get number of sub-elements */
|
/// @brief Get number of sub-elements
|
||||||
size_t size() const;
|
size_t size() const;
|
||||||
|
|
||||||
const std::vector<xmlelement>& getElements() const;
|
const std::vector<xmlelement>& getElements() const;
|
||||||
@ -118,21 +123,21 @@ namespace xml {
|
|||||||
xmldocument parse();
|
xmldocument parse();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Serialize XML Document to string
|
/// @brief Serialize XML Document to string
|
||||||
@param document serializing document
|
/// @param document serializing document
|
||||||
@param nice use human readable format
|
/// @param nice use human readable format (with indents and line-separators)
|
||||||
(with indents and line-separators)
|
/// @param indentStr indentation characters sequence (default - 4 spaces)
|
||||||
@param indentStr indentation characters sequence
|
/// @return XML string
|
||||||
(default - 4 spaces)*/
|
|
||||||
extern std::string stringify(
|
extern std::string stringify(
|
||||||
const xmldocument document,
|
const xmldocument document,
|
||||||
bool nice=true,
|
bool nice=true,
|
||||||
const std::string& indentStr=" "
|
const std::string& indentStr=" "
|
||||||
);
|
);
|
||||||
|
|
||||||
/* Read XML Document from string
|
/// @brief Read XML Document from string
|
||||||
@param filename file name will be shown in error messages
|
/// @param filename file name will be shown in error messages
|
||||||
@param source xml source code string */
|
/// @param source xml source code string
|
||||||
|
/// @return xml document
|
||||||
extern xmldocument parse(std::string filename, std::string source);
|
extern xmldocument parse(std::string filename, std::string source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user