fxos/lib/library.cpp

111 lines
2.0 KiB
C++

#include <fxos/library.h>
#include <fxos/load.h>
#include <fxos/errors.h>
#include <fxos/util.h>
#include <fxos/log.h>
#include <filesystem>
#include <ctime>
namespace fs = std::filesystem;
using namespace FxOS::Log;
namespace FxOS {
/* Add a new search path in the library. */
void Library::add_path(std::string path)
{
m_paths.push_back(path);
}
/* Recursively explore the fxos data file in a folder. */
void Library::explore(std::string path)
{
try
{
fs::recursive_directory_iterator it(path);
for(auto &file: it)
{
if(!fs::is_directory(file)) load(file.path());
}
}
catch(fs::filesystem_error &e)
{
if(e.code().value() == ENOENT)
{
log(WRN "directory %s does not exist", path);
}
else throw e;
}
}
/* Directly load an fxos data file into the library. */
void Library::load(std::string path)
{
Buffer file(path);
size_t offset;
int line;
log(LOG "Loading resource file '%s'...\\", path);
/* Time the loading */
auto start = timer_start();
Header h = load_header(file, offset, line);
if(h.find("type") == h.end())
{
log(ERR "no type set in the header of '%s'", path);
return;
}
std::string type = h["type"];
if(type == "assembly")
{
try {
int total = load_asm(file, offset, line);
m_asmtables.push_back(std::make_pair(h["name"],total));
}
catch(FxOS::SyntaxError &e) {
log(ERR "%s", e.str());
}
}
else if(type == "target")
{
if(!h.count("name"))
{
log(ERR "no target name set in '%s'", path);
return;
}
try {
m_targets[h["name"]] = load_target(file, offset, line);
}
catch(FxOS::SyntaxError &e) {
log(ERR "%s", e.str());
}
}
else if(type == "symbols")
{
try {
SymbolTable st = load_symbols(file, offset, line);
m_symtables.push_back(st);
}
catch(FxOS::SyntaxError &e) {
log(ERR "%s", e.str());
}
}
else
{
log(ERR "unknown file type '%s' in '%s'", type, path);
return;
}
long long ns = timer_end(start);
log(LOG "%s", timer_format(ns));
}
} /* namespace FxOS */