diff --git a/src/parser.cpp b/src/parser.cpp index 2a5246a..f32efa5 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -320,12 +320,13 @@ namespace /* internal */ } -Tree parseTftFile(MappedFile file) +Tree parseTftFile(fd_t file) { Tree result("name"); cur_tree = &result; - alloced preprocessed = preprocess(file); + MappedFile mapedfile = mapfile(file); + alloced preprocessed = preprocess(mapedfile); state_neutral(preprocessed.data); diff --git a/src/parser.hpp b/src/parser.hpp index 7ce9e19..dcbc83c 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -1,3 +1,3 @@ #include "tree.hpp" -Tree parseTftFile(MappedFile file); +Tree parseTftFile(fd_t); diff --git a/src/tft.cpp b/src/tft.cpp index 0225f7d..5f73ad2 100644 --- a/src/tft.cpp +++ b/src/tft.cpp @@ -3,16 +3,35 @@ #include "tree.hpp" #include "utils.hpp" #include "parser.hpp" +#include - +using std::string_view; /* using std::cin; */ /* using std::cout; */ +using std::cerr; +using namespace std::literals; + int main(int argc, char* argv[]) { init(); - auto f = mapfile("example_tree.tft"); - parseTftFile(f).display(); + if(argc < 2) + { + cerr << "Usage: tft FILES...\nTry 'tft --help' for more information"; + return 1; + } + + for(int i = 1; i < argc; ++i) + { + if(string_view(argv[i]) == "--help"sv) /* sv worth it? */ + { + /* todo */ + return 0; /* todo exit curses */ + } + + fd_t file = openOrDie(argv[i]); + parseTftFile(file).display(); + } Tree test1("Dechkovi"); /* test1.loadFromFile(); */ diff --git a/src/utils.cpp b/src/utils.cpp index 9317196..02abff1 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -4,6 +4,7 @@ #include #include #include +#include void init() { @@ -25,10 +26,8 @@ void init() wnoutrefresh(stdscr); /* if not present the first real refresh is missed */ } -MappedFile mapfile(const char* path) +MappedFile mapfile(fd_t file) { - int file = open(path, O_RDONLY); - MappedFile res; res.len = lseek(file, 0, SEEK_END); res.data = (char*)mmap(nullptr, res.len, PROT_READ, MAP_PRIVATE, file, 0); @@ -37,6 +36,18 @@ MappedFile mapfile(const char* path) return res; } +fd_t openOrDie(const char* path) +{ + fd_t file = open(path, O_RDONLY); + if(file == -1) + { + std::cerr << "Can't open: " << path << '\n'; + exit(1); + } + return file; +} + + bool willBeVisible(I16 drawY, I16 drawX, U16 height, U16 width) { return (drawY + height > 0) && (drawX + width > 0) && diff --git a/src/utils.hpp b/src/utils.hpp index be8785c..4d40c5b 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -23,15 +23,18 @@ /* return lhs; */ /* } */ -void init(); - +using fd_t = int; /* file descriptor */ struct MappedFile { char* data; unsigned len; }; -MappedFile mapfile(const char* path); +void init(); + + +MappedFile mapfile(fd_t); +fd_t openOrDie(const char* path); template void remove(Vec& vec, size_t pos) /* Remove from vector by moving the last element in its place */