Skip to content

Commit

Permalink
today()
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Jun 13, 2024
1 parent 232ac72 commit c78b70d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pya2l/extensions/ifdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#if !defined(__IFDATA_HPP)
#define __IFDATA_HPP

// #define __STDC_WANT_LIB_EXT1__ (1)
// #define __STDC_WANT_LIB_EXT1__ (1)

#include <bit>
#include <sys/stat.h>

#include <bit>
#include <cstdio>

#include "tokenizer.hpp"
Expand Down
108 changes: 49 additions & 59 deletions pya2l/extensions/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ namespace fs = std::filesystem;
#include "utils.hpp"

struct Filenames {
Filenames() = default;
Filenames(const Filenames &) = default;
Filenames(Filenames &&) = default;
Filenames() = default;
Filenames(const Filenames&) = default;
Filenames(Filenames&&) = default;

Filenames(const std::string &_a2l, const std::string &_aml, const std::string &_ifdata) :
Filenames(const std::string& _a2l, const std::string& _aml, const std::string& _ifdata) :
a2l(_a2l), aml(_aml), ifdata(_ifdata) {
}

Expand All @@ -67,22 +67,23 @@ struct Filenames {
using preprocessor_result_t = std::tuple<Filenames, LineMap, IfDataReader>;

class Preprocessor {
public:
public:

const std::string A2L_TMP = "A2L.tmp";
const std::string AML_TMP = "AML.tmp";
const std::string A2L_TMP = "A2L.tmp";
const std::string AML_TMP = "AML.tmp";
const std::string IFDATA_TMP = "IFDATA.tmp";

Preprocessor(const std::string& loglevel) : m_loglevel(loglevel),
Preprocessor(const std::string& loglevel) :
m_loglevel(loglevel),
tmp_a2l(A2L_TMP, true),
tmp_aml(AML_TMP),
tmp_ifdata(IFDATA_TMP, true),
a2l_token_writer(tmp_a2l),
ifdata_builder{ tmp_ifdata.handle() } {
get_include_paths_from_env();
// tmp_a2l.to_stdout();
m_filenames.a2l = tmp_a2l.abs_path();
m_filenames.aml = tmp_aml.abs_path();
m_filenames.a2l = tmp_a2l.abs_path();
m_filenames.aml = tmp_aml.abs_path();
m_filenames.ifdata = tmp_ifdata.abs_path();
}

Expand All @@ -105,7 +106,7 @@ class Preprocessor {

LineMap line_map{};

protected:
protected:

void skip_bom(auto& fs) {
const unsigned char boms[]{ 0xef, 0xbb, 0xbf };
Expand All @@ -123,12 +124,12 @@ class Preprocessor {
fs::path path{ filename };
auto abs_pth = fs::absolute(path);
std::ifstream file(abs_pth, std::ios::binary);
bool begin = false;
bool end = false;
bool ifdata = false;
bool begin = false;
bool end = false;
bool ifdata = false;
bool ifdata_name = false;
bool collect{ false };
bool include = false;
bool include = false;
std::uint8_t skip_tokens = 0;
std::vector<Token> collected_tokens{};

Expand All @@ -152,12 +153,11 @@ class Preprocessor {
if (token.m_token_class != TokenClass::COMMENT) {
skip_tokens--;
}
}
else {
} else {
end_line = token.m_line_numbers.end_line;
}
if (token.m_token_class == TokenClass::COMMENT) {
auto lines = split(token.m_payload, '\n');
auto lines = split(token.m_payload, '\n');
auto line_count = lines.size();
for (const auto& line : lines) {
// tmp_a2l() << std::string(line.length(), ' ');
Expand All @@ -171,60 +171,54 @@ class Preprocessor {
}
}
}
}
else if ((token.m_token_class == TokenClass::REGULAR) || (token.m_token_class == TokenClass::STRING)) {
} else if ((token.m_token_class == TokenClass::REGULAR) || (token.m_token_class == TokenClass::STRING)) {
if (end == true) {
if (token.m_payload == "A2ML") {
a2ml = false;
// tmp_aml() << token.m_payload;
// tmp_aml() << token.m_payload;
for (const auto& item : collected_tokens) {
// tmp_a2l() << item.m_payload;
a2l_token_writer << item;
}
}
else if (token.m_payload == "IF_DATA") {
} else if (token.m_payload == "IF_DATA") {
ifdata = false;
ifdata_builder.add_token(token);
ifdata_builder.finalize();
for (const auto& item : collected_tokens) {
// tmp_a2l() << item.m_payload;
a2l_token_writer << item;
}
}
else {
} else {
for (const auto& item : collected_tokens) {
if (item.m_token_class == TokenClass::REGULAR) {
// tmp_a2l() << std::string(item.m_payload.length(), ' ');
}
else if (item.m_token_class == TokenClass::WHITESPACE) {
} else if (item.m_token_class == TokenClass::WHITESPACE) {
// tmp_a2l() << item.m_payload;
// a2l_token_writer << item;
}
}
}
collected_tokens.clear();
collect = false;
end = false;
end = false;
}
if (a2ml == true) {
if (token.m_token_class == TokenClass::STRING) {
tmp_aml() << "\"" << token.m_payload << "\"";
}
else {
} else {
tmp_aml() << token.m_payload;
}
if (token.m_payload == "/end") {
end = true;
collected_tokens.push_back(token);
collect = true;
}
}
else if (ifdata == true) {
} else if (ifdata == true) {
ifdata_builder.add_token(token);
if (token.m_payload == "/end") {
collected_tokens.push_back(token);
collect = true;
end = true;
end = true;
}
}
if (include == true) {
Expand All @@ -236,10 +230,9 @@ class Preprocessor {
line_offset += length;

std::cout << "[INFO (pya2l.Preprocessor)]: Including '" + incl_file.value().string() + "'."
<< std::endl;
<< std::endl;
_process_file(incl_file.value().string());
}
else {
} else {
throw std::runtime_error(
"[ERROR (pya2l.Preprocessor)]: Could "
"not locate include file '" +
Expand All @@ -249,7 +242,7 @@ class Preprocessor {
include = false;
line_offset++;
start_line_number = token.m_line_numbers.end_line + 1;
skip_tokens = 2;
skip_tokens = 2;
}
if (token.m_payload == "/include") {
include = true;
Expand All @@ -260,30 +253,27 @@ class Preprocessor {
// tmp_a2l() << token.m_payload;
a2l_token_writer << token;
ifdata_name = false;
}
else {
} else {
// tmp_a2l() << std::string(token.m_payload.length(), ' ');
}
}
}
else {
} else {
if ((include == false) && (skip_tokens == 0)) {
// tmp_a2l() << token.m_payload;
a2l_token_writer << token;
}
}
if (begin) {
begin = false;
begin = false;
collect = false;
collected_tokens.push_back(token);
if (token.m_payload == "A2ML") {
a2ml = true;
for (const auto& item : collected_tokens) {
tmp_aml() << item.m_payload;
}
}
else if (token.m_payload == "IF_DATA") {
ifdata = true;
} else if (token.m_payload == "IF_DATA") {
ifdata = true;
ifdata_name = true;
for (const auto& item : collected_tokens) {
ifdata_builder.add_token(item);
Expand All @@ -292,12 +282,11 @@ class Preprocessor {
collected_tokens.clear();
}
if ((token.m_payload == "/begin") && (ifdata == false)) {
begin = true;
begin = true;
collect = true;
collected_tokens.push_back(token);
}
}
else if (token.m_token_class == TokenClass::WHITESPACE) {
} else if (token.m_token_class == TokenClass::WHITESPACE) {
if ((end == false) && (include == false) && (skip_tokens == 0)) {
// tmp_a2l() << token.m_payload;
// a2l_token_writer << token;
Expand All @@ -307,26 +296,23 @@ class Preprocessor {
}
if (a2ml == true) {
tmp_aml() << token.m_payload;
}
else if (ifdata == true) {
} else if (ifdata == true) {
ifdata_builder.add_token(token);
}
}
}
auto length = (end_line - start_line_number);
update_line_map(abs_pth, line_offset, line_offset + length, start_line_number, end_line);
line_offset += length;
}
else {
} else {
throw std::runtime_error("Could not open file: '" + abs_pth.string() + "'");
}
}

std::string shorten_file_name(fs::path file_name) {
if (file_name.parent_path() == fs::current_path()) {
return file_name.filename().string();
}
else {
} else {
return file_name.string();
}
}
Expand All @@ -339,11 +325,14 @@ class Preprocessor {
}

void get_include_paths_from_env() {
#if defined(_WIN32)
auto var = get_env_var("ASAP_INCLUDE");
include_paths = split_path(var);
#if 0
#if defined(_WIN32)
const char delimiter = ';';
#else
#else
const char delimiter = ':';
#endif
#endif
char* asap_include = std::getenv("ASAP_INCLUDE");

if (asap_include == NULL) {
Expand All @@ -356,6 +345,7 @@ class Preprocessor {
include_paths.push_back(ptr);
ptr = strtok(NULL, &delimiter);
}
#endif
}

std::optional<fs::path> locate_file(const std::string& file_name, const std::string& additional_path) {
Expand All @@ -374,7 +364,7 @@ class Preprocessor {
return std::nullopt;
}

private:
private:

std::string m_loglevel;
TempFile tmp_a2l;
Expand All @@ -385,7 +375,7 @@ class Preprocessor {
Filenames m_filenames{};
std::vector<std::string> include_paths{};
std::size_t line_offset{ 1 };
bool a2ml {false};
bool a2ml{ false };
};

#endif // __PREPROCESSOR_HPP
54 changes: 53 additions & 1 deletion pya2l/extensions/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#define __UTILS_HPP

#include <algorithm>

#include <optional>
#include <ranges>
#include <string>
#include <vector>

enum class State : std::uint8_t {
IDLE,
Expand Down Expand Up @@ -126,6 +128,56 @@ inline auto split(const std::string &str, char delimiter) -> std::vector<std::st
return { std::ranges::begin(range), std::ranges::end(range) };
}

#if defined(_MSC_VER)
inline std::optional<std::string> get_env_var(const std::string &var) {
char *c_ptr;
std::size_t len;

::errno_t err = _dupenv_s(&c_ptr, &len, var.c_str());
if ((err == -1) || (len == 0)) {
return std::nullopt;
}
std::string result{ c_ptr };

::free(c_ptr);
return result;
}
#else
inline std::optional<std::string> get_env_var(const std::string &var) {
auto res = secure_getenv(var.c_str());

if (res == nullptr) {
return std::nullopt;
}
return res;
}
#endif

inline std::vector<std::string> split_path(std::optional<std::string> pth) {
using std::operator""sv;
#if defined(_WIN32)
constexpr auto DELIM{ ";"sv };
#else
constexpr auto DELIM{ ":"sv };
#endif

if (!pth) {
return {};
}

std::vector<std::string> result{};

std::size_t pos = 0;
std::string token;
std::string str{ *pth };
while ((pos = str.find(DELIM)) != std::string::npos) {
token = str.substr(0, pos);
result.emplace_back(token);
str.erase(0, pos + DELIM.length());
}
return result;
}

inline void hex_dump(const char *p, std::size_t n) {
for (std::size_t idx = 0; idx < n; ++idx) {
printf("%02X ", p[idx]);
Expand Down
1 change: 1 addition & 0 deletions reformat.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clang-format -i pya2l/*.cpp pya2l/extensions/*.hpp pya2l/extensions/*.cpp

0 comments on commit c78b70d

Please sign in to comment.