It compiles!

This commit is contained in:
dpeter99 2022-05-27 00:47:26 +02:00
parent defbb33ce8
commit b1523d13a6
5 changed files with 148 additions and 154 deletions

View File

@ -1,37 +1,15 @@
module;
import <string>;
import shadow_utils;
/*
module;
module Shadow.FileFormat:SFFElement;
import <string>;
import shadow_utils;
namespace Shadow::SFF {
SFFElement* SFFElement::GetFirstChild()
{
return children.size() > 0 ? children.begin()->second : nullptr;
}
SFFElement* SFFElement::GetChildByIndex(int index)
{
ChildrenMap::iterator it = children.begin();
for (size_t i = 0; i < index; i++)
{
it++;
}
return it->second;
}
SFFElement* SFFElement::GetChildByName(std::string name)
{
ChildrenMap::iterator it = children.find(name);
if (it != children.end()) {
return it->second;
}
return nullptr;
}
}
*/

View File

@ -1,15 +1,14 @@
module;
export module Shadow.FileFormat:SFFElement;
import <string>;
import <map>;
import <list>;
export module Shadow.FileFormat:SFFElement;
namespace Shadow::SFF {
export namespace Shadow::SFF {
class SFFElement
export class SFFElement
{
public:
SFFElement* parent;
@ -26,12 +25,29 @@ export namespace Shadow::SFF {
std::string GetStringProperty(std::string name);
SFFElement* GetFirstChild()
{
return children.size() > 0 ? children.begin()->second : nullptr;
}
SFFElement* GetFirstChild();
SFFElement* GetChildByIndex(int index)
{
ChildrenMap::iterator it = children.begin();
for (size_t i = 0; i < index; i++)
{
it++;
}
return it->second;
}
SFFElement* GetChildByIndex(int index);
SFFElement* GetChildByName(std::string name);
SFFElement* GetChildByName(std::string name)
{
ChildrenMap::iterator it = children.find(name);
if (it != children.end()) {
return it->second;
}
return nullptr;
}
~SFFElement();

View File

@ -1,3 +1,4 @@
/*
module;
import <string>;
import <iostream>;
@ -13,112 +14,5 @@ import :SFFVersion;
namespace Shadow::SFF {
SFFElement* SFFParser::ReadFromStream(std::istream& stream)
{
auto version = ReadVersionFromHeader(stream);
if (version.invalid) {
//SH_CORE_WARN("Shadow File is invalid");
return nullptr;
}
//The current node that we are building
auto* context = new SFFElement;
//Top level Element
SFFElement* base = context;
//The new node that will be a child of the context
auto* current = new SFFElement;
std::string buffer;
char c;
while (!stream.eof())
{
stream.get(c);
if (c == ':')
{
//The stuff in the buffer is a parameter name
std::cout << "Name: " << buffer;
current->name = buffer;
buffer = "";
}
else if (c == '{')
{
//Start of a new block
current->isBlock = true;
current->parent = context;
context->children[current->name] = current;
context = current;
current = new SFFElement;
}
else if (c == ',')
{
// End of a property
//The stuff is the value
std::cout << "Value: " << buffer << std::endl;
current->value = buffer;
current->parent = context;
current->isBlock = false;
buffer = "";
context->children[current->name] = current;
current = new SFFElement();
}
else if (c == '}')
{
// End of a block
context = context->parent;
}
else
{
if (std::isspace(c) == 0)
{
buffer += c;
}
}
}
std::cout << "END" << std::endl;
return base;
}
SFFVersion SFFParser::ReadVersionFromHeader(std::istream& stream) {
std::string line;
std::getline(stream, line);
auto parts = explode(line, '_');
if (parts[0] != "ShadowFileFormat") {
return SFFVersion(-1,-1,-1);
}
else {
int mayor = std::stoi(parts[1]);
int minor = std::stoi(parts[2]);
int patch = std::stoi(parts[3]);
return SFFVersion(mayor, minor, patch);
}
}
SFFElement* SFFParser::ReadFromFile(std::string path)
{
std::ifstream inputFileStream(path);
if (errno)
{
//SH_CORE_ERROR("Error: {0} | File: {1}", strerror(errno), path);
//std::cerr << "Error: " << strerror(errno) << std::endl;
//std::cerr << "File: " << path << std::endl;
return nullptr;
}
return ReadFromStream(inputFileStream);
}
}
*/

View File

@ -1,24 +1,130 @@
module;
export module Shadow.FileFormat:SFFParser;
import <string>;
import <iostream>;
export module Shadow.FileFormat:SFFParser;
import <fstream>;
import <string>;
import shadow_utils;
import :SFFElement;
import :SFFVersion;
export namespace Shadow::SFF {
namespace Shadow::SFF {
class SFFParser
export class SFFParser
{
public:
static SFFElement* ReadFromStream(std::istream& stream);
static SFFElement* ReadFromStream(std::istream& stream)
{
auto version = ReadVersionFromHeader(stream);
if (version.invalid) {
//SH_CORE_WARN("Shadow File is invalid");
return nullptr;
}
static SFFVersion ReadVersionFromHeader(std::istream& stream);
static SFFElement* ReadFromFile(std::string path);
//The current node that we are building
auto* context = new SFFElement;
//Top level Element
SFFElement* base = context;
//The new node that will be a child of the context
auto* current = new SFFElement;
std::string buffer;
char c;
while (!stream.eof())
{
stream.get(c);
if (c == ':')
{
//The stuff in the buffer is a parameter name
std::cout << "Name: " << buffer;
current->name = buffer;
buffer = "";
}
else if (c == '{')
{
//Start of a new block
current->isBlock = true;
current->parent = context;
context->children[current->name] = current;
context = current;
current = new SFFElement;
}
else if (c == ',')
{
// End of a property
//The stuff is the value
std::cout << "Value: " << buffer << std::endl;
current->value = buffer;
current->parent = context;
current->isBlock = false;
buffer = "";
context->children[current->name] = current;
current = new SFFElement();
}
else if (c == '}')
{
// End of a block
context = context->parent;
}
else
{
if (std::isspace(c) == 0)
{
buffer += c;
}
}
}
std::cout << "END" << std::endl;
return base;
}
static SFFVersion ReadVersionFromHeader(std::istream& stream) {
std::string line;
std::getline(stream, line);
auto parts = explode(line, '_');
if (parts[0] != "ShadowFileFormat") {
return SFFVersion(-1, -1, -1);
}
else {
int mayor = std::stoi(parts[1]);
int minor = std::stoi(parts[2]);
int patch = std::stoi(parts[3]);
return SFFVersion(mayor, minor, patch);
}
}
static SFFElement* ReadFromFile(std::string path)
{
std::ifstream inputFileStream(path);
if (errno)
{
//SH_CORE_ERROR("Error: {0} | File: {1}", strerror(errno), path);
//std::cerr << "Error: " << strerror(errno) << std::endl;
//std::cerr << "File: " << path << std::endl;
return nullptr;
}
return ReadFromStream(inputFileStream);
}
};
}

View File

@ -1,10 +1,10 @@
module;
module shadow_utils;
import <string>;
import <vector>;
module shadow_utils;
inline std::vector<std::string> explode(const std::string& s, const char& c)
{
std::string buff;