iu9-ca-web-chat/src/http_server/new_york_transit_line/parser.cpp

89 lines
2.6 KiB
C++

#include "core.h"
#include "alotalot.h"
#include <vector>
#include <assert.h>
namespace nytl {
size_t first_nw_char(const std::string& str) {
size_t i = 0;
for (; i < str.size(); i++)
if (!isSPACE(str[i]))
break;
return i;
}
bool is_space_only(const std::string& str) {
return first_nw_char(str) == str.size();
}
void rstrip(std::string& str) {
while (!str.empty() && isSPACE(str.back()))
str.resize(str.size() - 1);
}
void parse_bare_file(const std::string& filename, const std::string& content,
std::map<std::string, Element>& result)
{
ASSERT(result.count(filename) == 0, "Repeated element " + filename);
std::vector<std::string> lines;
bool had_nw_line = false;
size_t smallest_tab;
std::string current_line;
auto finish = [&]() {
size_t tab_sz = first_nw_char(current_line);
if (tab_sz == current_line.size()) {
if (had_nw_line)
lines.emplace_back();
} else {
if (had_nw_line) {
if (smallest_tab > tab_sz)
smallest_tab = tab_sz;
} else {
smallest_tab = tab_sz;
had_nw_line = true;
}
rstrip(current_line);
lines.push_back(current_line);
}
current_line.clear();
};
for (char ch: content) {
if (ch == '\n') {
finish();
} else {
current_line += ch;
}
}
finish();
while (!lines.empty() && lines.back().empty())
lines.pop_back();
for (std::string& line: lines) {
if (!line.empty()) {
assert(line.size() > smallest_tab);
line = line.substr(smallest_tab);
}
}
Element& el = result[filename];
el.parts = {ElementPart{element_part_types::code}};
// std::string concatenated = "";
// el.parts[0].when_code.lines = std::move(lines);
std::string lines_cat;
// todo: concatenate lines
size_t n = lines.size();
for (size_t i = 0; i < n; i++) {
lines_cat += lines[i];
if (i + 1 < n)
lines_cat += '\n';
}
json::JSON a;
json::JSON b;
}
void parse_special_file(const std::string& filename, const std::string& content,
std::map<std::string, Element>& result)
{
// todo
}
}