diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 107 |
1 files changed, 40 insertions, 67 deletions
diff --git a/src/util.cc b/src/util.cc index 73b158d..f13a4a4 100644 --- a/src/util.cc +++ b/src/util.cc @@ -7,10 +7,17 @@ #include <fstream> #include <algorithm> +namespace { +char to_lower_c(char ch) +{ + return static_cast<char>(::tolower(ch)); +} +} + std::string to_lower(const std::string& str) { std::string out{str}; - std::transform(out.begin(), out.end(), out.begin(), ::tolower); + std::transform(out.begin(), out.end(), out.begin(), to_lower_c); return out; } @@ -19,73 +26,18 @@ std::string readFile(const std::string& fileName) std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); - std::ifstream::pos_type fileSize = ifs.tellg(); - ifs.seekg(0, std::ios::beg); - - std::vector<char> bytes(static_cast<std::size_t>(fileSize)); - ifs.read(bytes.data(), fileSize); - - return {bytes.data(), static_cast<std::size_t>(fileSize)}; -} - -std::vector<std::string> readDeps(const std::string& depFile) -{ - if(!std::filesystem::exists(depFile)) + auto tell = ifs.tellg(); + if(tell < 0) { return {}; } + auto fileSize = static_cast<std::size_t>(tell); + ifs.seekg(0, std::ios::beg); - auto str = readFile(depFile); - - std::vector<std::string> output; - std::string tmp; - bool start{false}; - bool in_whitespace{false}; - for(const auto& c : str) - { - if(c == '\\' || c == '\n') - { - continue; - } - - if(c == ':') - { - start = true; - continue; - } - - if(!start) - { - continue; - } - - if(c == ' ' || c == '\t') - { - if(in_whitespace) - { - continue; - } - - if(!tmp.empty()) - { - output.push_back(tmp); - } - tmp.clear(); - in_whitespace = true; - } - else - { - in_whitespace = false; - tmp += c; - } - } - - if(!tmp.empty()) - { - output.push_back(tmp); - } + std::vector<char> bytes(fileSize); + ifs.read(bytes.data(), static_cast<long>(bytes.size())); - return output; + return { bytes.data(), bytes.size() }; } ctor::language languageFromExtension(const std::filesystem::path& file) @@ -229,15 +181,36 @@ std::string locate(const std::string& prog, } } + if(std::filesystem::exists(program + ".exe")) + { + if(check_executable(program + ".exe")) + { + return program + ".exe"; + } + } + for(const auto& path_str : paths) { std::filesystem::path path(path_str); - auto prog_path = path / program; - if(std::filesystem::exists(prog_path)) { - if(check_executable(prog_path)) + auto prog_path = path / program; + if(std::filesystem::exists(prog_path)) + { + if(check_executable(prog_path)) + { + return prog_path.string(); + } + } + } + + { + auto prog_path = path / (program + ".exe"); + if(std::filesystem::exists(prog_path)) { - return prog_path.string(); + if(check_executable(prog_path)) + { + return prog_path.string(); + } } } } |