diff --git a/pya2l/extensions/ifdata.hpp b/pya2l/extensions/ifdata.hpp index 47e7a9f..5936ef6 100644 --- a/pya2l/extensions/ifdata.hpp +++ b/pya2l/extensions/ifdata.hpp @@ -25,11 +25,11 @@ #if !defined(__IFDATA_HPP) #define __IFDATA_HPP - // #define __STDC_WANT_LIB_EXT1__ (1) +// #define __STDC_WANT_LIB_EXT1__ (1) - #include #include + #include #include #include "tokenizer.hpp" diff --git a/pya2l/extensions/preprocessor.hpp b/pya2l/extensions/preprocessor.hpp index f49c4ab..47e00d0 100644 --- a/pya2l/extensions/preprocessor.hpp +++ b/pya2l/extensions/preprocessor.hpp @@ -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) { } @@ -67,13 +67,14 @@ struct Filenames { using preprocessor_result_t = std::tuple; 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), @@ -81,8 +82,8 @@ class Preprocessor { 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(); } @@ -105,7 +106,7 @@ class Preprocessor { LineMap line_map{}; -protected: + protected: void skip_bom(auto& fs) { const unsigned char boms[]{ 0xef, 0xbb, 0xbf }; @@ -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 collected_tokens{}; @@ -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(), ' '); @@ -171,18 +171,16 @@ 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(); @@ -190,13 +188,11 @@ class Preprocessor { // 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; } @@ -204,13 +200,12 @@ class Preprocessor { } 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") { @@ -218,13 +213,12 @@ class Preprocessor { 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) { @@ -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 '" + @@ -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; @@ -260,20 +253,18 @@ 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") { @@ -281,9 +272,8 @@ class Preprocessor { 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); @@ -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; @@ -307,8 +296,7 @@ class Preprocessor { } if (a2ml == true) { tmp_aml() << token.m_payload; - } - else if (ifdata == true) { + } else if (ifdata == true) { ifdata_builder.add_token(token); } } @@ -316,8 +304,7 @@ class Preprocessor { 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() + "'"); } } @@ -325,8 +312,7 @@ class Preprocessor { 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(); } } @@ -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) { @@ -356,6 +345,7 @@ class Preprocessor { include_paths.push_back(ptr); ptr = strtok(NULL, &delimiter); } + #endif } std::optional locate_file(const std::string& file_name, const std::string& additional_path) { @@ -374,7 +364,7 @@ class Preprocessor { return std::nullopt; } -private: + private: std::string m_loglevel; TempFile tmp_a2l; @@ -385,7 +375,7 @@ class Preprocessor { Filenames m_filenames{}; std::vector include_paths{}; std::size_t line_offset{ 1 }; - bool a2ml {false}; + bool a2ml{ false }; }; #endif // __PREPROCESSOR_HPP diff --git a/pya2l/extensions/utils.hpp b/pya2l/extensions/utils.hpp index a36c4f2..52fe686 100644 --- a/pya2l/extensions/utils.hpp +++ b/pya2l/extensions/utils.hpp @@ -26,8 +26,10 @@ #define __UTILS_HPP #include - + #include #include + #include + #include enum class State : std::uint8_t { IDLE, @@ -126,6 +128,56 @@ inline auto split(const std::string &str, char delimiter) -> std::vector 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 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 split_path(std::optional pth) { + using std::operator""sv; + #if defined(_WIN32) + constexpr auto DELIM{ ";"sv }; + #else + constexpr auto DELIM{ ":"sv }; + #endif + + if (!pth) { + return {}; + } + + std::vector 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]); diff --git a/reformat.cmd b/reformat.cmd new file mode 100644 index 0000000..c262c57 --- /dev/null +++ b/reformat.cmd @@ -0,0 +1 @@ +clang-format -i pya2l/*.cpp pya2l/extensions/*.hpp pya2l/extensions/*.cpp