69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
#ifndef GAME_UTILS_HPP
|
|
#define GAME_UTILS_HPP
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
#include "../../eigen/Eigen/Dense"
|
|
|
|
using Vector3f = Eigen::Vector3f;
|
|
|
|
class FileUtils {
|
|
public:
|
|
static std::string readFile(const std::string& path) {
|
|
std::ifstream file(path);
|
|
if (!file.is_open()) {
|
|
std::cerr << "Failed to open file: " << path << std::endl;
|
|
return "{}"; // Return empty JSON obj on fail
|
|
}
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf();
|
|
return buffer.str();
|
|
}
|
|
};
|
|
|
|
class SimpleJsonParser {
|
|
public:
|
|
static std::map<std::string, std::string> parseDepth1(const std::string& raw) {
|
|
std::map<std::string, std::string> result;
|
|
std::string clean = raw;
|
|
// Remove braces and quotes
|
|
clean.erase(std::remove(clean.begin(), clean.end(), '{'), clean.end());
|
|
clean.erase(std::remove(clean.begin(), clean.end(), '}'), clean.end());
|
|
clean.erase(std::remove(clean.begin(), clean.end(), '\"'), clean.end());
|
|
|
|
std::stringstream ss(clean);
|
|
std::string segment;
|
|
while(std::getline(ss, segment, ',')) {
|
|
size_t colonPos = segment.find(':');
|
|
if(colonPos != std::string::npos) {
|
|
std::string key = segment.substr(0, colonPos);
|
|
std::string val = segment.substr(colonPos + 1);
|
|
|
|
// Trim key
|
|
size_t first = key.find_first_not_of(" \t\n\r");
|
|
size_t last = key.find_last_not_of(" \t\n\r");
|
|
if (first != std::string::npos) key = key.substr(first, (last - first + 1));
|
|
|
|
// Trim val
|
|
first = val.find_first_not_of(" \t\n\r");
|
|
last = val.find_last_not_of(" \t\n\r");
|
|
if (first != std::string::npos) val = val.substr(first, (last - first + 1));
|
|
|
|
result[key] = val;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|