AALanguage
The best language for those who have nothing to do
Loading...
Searching...
No Matches
LibParser.cpp
Go to the documentation of this file.
1#include "LibParser.h"
2
3LibParser::LibParser(std::string libs_) {
4 libs = libs_;
5}
6bool LibParser::add_lib(std::string name) {
7 if (used.count(name)) return false;
8 used.insert(name);
9
10 std::string path = name_to_path(name);
11 std::ifstream file;
12 file.open(path, std::ios::in);
13 if (file.is_open()) {
14 file.close();
15 return true;
16 }
17 throw std::exception(("Unknown library: " + name).c_str());
18}
19std::string LibParser::parse_lib(std::string name) {
20 std::string path = name_to_path(name);
21 std::ifstream file;
22 file.open(path, std::ios::in);
23 if (file.is_open()) {
24 std::string res = slurp(file);
25 file.close();
26 return res;
27 }
28 throw std::exception(("Unknown library: " + name).c_str());
29}
30std::string LibParser::name_to_path(std::string name) {
31 std::string path = libs;
32 if (*path.rbegin() != '\\')
33 path.push_back('\\');
34 auto dirs = split(name, '.');
35 for (int i = 0; i < dirs.size() - 1; ++i) {
36 path += dirs[i] + "\\";
37 }
38 path += *dirs.rbegin() + ".aa";
39 return path;
40}
41
42std::vector<std::string> LibParser::split(const std::string& s, char delim) {
43 std::stringstream ss(s);
44 std::string item;
45 std::vector<std::string> elems;
46 while (std::getline(ss, item, delim)) {
47 elems.push_back(item);
48 }
49 return elems;
50}
51std::string LibParser::slurp(std::ifstream& in) {
52 std::ostringstream sstr;
53 sstr << in.rdbuf();
54 return sstr.str();
55}