2023-03-30 18:29:56 -07:00

76 lines
2.0 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/string.hpp"
namespace Dawn {
static inline std::string rawParser(std::string v, std::string *error) {
return v;
};
static inline std::string stringParser(std::string v, std::string *error) {
return "\"" + v + "\"";
};
static inline std::string floatParser(std::string v, std::string *error) {
v = stringTrim(v);
// Make sure number contains decimal
if(v.find(".") == std::string::npos) {
v += ".0";
}
// Make sure number contains a number before the decimal
if(v.find(".") == 0) {
v = "0" + v;
}
// Make sure ends with f
if(v.find("f") == std::string::npos) {
v += "f";
}
return v;
};
static inline std::string intParser(std::string v, std::string *error) {
v = stringTrim(v);
return v;
}
static inline std::string vec2Parser(std::string v, std::string *error) {
// Split string by comma into two strings that we pass into float
auto split = stringSplit(v, ",");
if(split.size() != 2) {
*error = "Invalid vec2 value: " + v;
return std::string("");
}
return std::string(
"glm::vec2(" +
floatParser(split[0], error) + ", " +
floatParser(split[1], error) +
")"
);
};
static inline std::string vec3Parser(std::string v, std::string *error) {
// Split string by comma into two strings that we pass into float
auto split = stringSplit(v, ",");
if(split.size() != 3) {
*error = "Invalid vec3 value: " + v;
return std::string("");
}
return std::string(
"glm::vec3(" +
floatParser(split[0], error) + ", " +
floatParser(split[1], error) + ", " +
floatParser(split[2], error) +
")"
);
};
static inline std::string colorParser(std::string v, std::string *error) {
return rawParser(v, error);
}
}