From 922412f5dc975b423757c1e248eac2813e48acb2 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 10 Oct 2021 18:57:13 +0200 Subject: Move some common functions to util.cc --- ctor.cc | 1 + src/task_ar.cc | 18 +--------- src/task_cc.cc | 108 +------------------------------------------------------ src/task_so.cc | 18 +--------- src/util.cc | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 13 +++++++ 6 files changed, 129 insertions(+), 141 deletions(-) create mode 100644 src/util.cc create mode 100644 src/util.h diff --git a/ctor.cc b/ctor.cc index 2e5c763..81f771e 100644 --- a/ctor.cc +++ b/ctor.cc @@ -24,6 +24,7 @@ BuildConfigurations ctorConfigs() "src/task_ld.cc", "src/task_so.cc", "src/tasks.cc", + "src/util.cc", "src/unittest.cc", }, .cxxflags = { diff --git a/src/task_ar.cc b/src/task_ar.cc index 84cda1b..06504f6 100644 --- a/src/task_ar.cc +++ b/src/task_ar.cc @@ -9,23 +9,7 @@ #include "libctor.h" #include "settings.h" #include "execute.h" - -namespace -{ -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 bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return std::string(bytes.data(), fileSize); -} -} // namespace :: +#include "util.h" TaskAR::TaskAR(const BuildConfiguration& config, const Settings& settings, diff --git a/src/task_cc.cc b/src/task_cc.cc index dfda75d..73297f2 100644 --- a/src/task_cc.cc +++ b/src/task_cc.cc @@ -10,113 +10,7 @@ #include "libctor.h" #include "settings.h" #include "execute.h" - -namespace -{ -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 bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return std::string(bytes.data(), fileSize); -} - -std::vector readDeps(const std::string& depFile) -{ - if(!std::filesystem::exists(depFile)) - { - return {}; - } - - auto str = readFile(depFile); - - std::vector 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); - } - - return output; -} - -Language languageFromExtension(const std::filesystem::path& file) -{ - auto ext = file.extension().string(); - if(ext == ".c") - { - return Language::C; - } - - if(ext == ".C" || - ext == ".cc" || - ext == ".cpp" || - ext == ".CPP" || - ext == ".c++" || - ext == ".cp" || - ext == ".cxx") - { - return Language::Cpp; - } - - if(ext == ".s" || - ext == ".S" || - ext == ".asm") - { - return Language::Asm; - } - - std::cerr << "Could not deduce language from " << file.string() << "\n"; - exit(1); - return {}; -} -} // namespace :: +#include "util.h" TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, const std::string& sourceDir, const Source& source) diff --git a/src/task_so.cc b/src/task_so.cc index ce6e868..f3e1937 100644 --- a/src/task_so.cc +++ b/src/task_so.cc @@ -9,23 +9,7 @@ #include "libctor.h" #include "settings.h" #include "execute.h" - -namespace -{ -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 bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return std::string(bytes.data(), fileSize); -} -} // namespace :: +#include "util.h" TaskSO::TaskSO(const BuildConfiguration& config, const Settings& settings, diff --git a/src/util.cc b/src/util.cc new file mode 100644 index 0000000..9bf83cc --- /dev/null +++ b/src/util.cc @@ -0,0 +1,112 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "util.h" + +#include +#include + +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 bytes(fileSize); + ifs.read(bytes.data(), fileSize); + + return std::string(bytes.data(), fileSize); +} + +std::vector readDeps(const std::string& depFile) +{ + if(!std::filesystem::exists(depFile)) + { + return {}; + } + + auto str = readFile(depFile); + + std::vector 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); + } + + return output; +} + +Language languageFromExtension(const std::filesystem::path& file) +{ + auto ext = file.extension().string(); + if(ext == ".c") + { + return Language::C; + } + + if(ext == ".C" || + ext == ".cc" || + ext == ".cpp" || + ext == ".CPP" || + ext == ".c++" || + ext == ".cp" || + ext == ".cxx") + { + return Language::Cpp; + } + + if(ext == ".s" || + ext == ".S" || + ext == ".asm") + { + return Language::Asm; + } + + std::cerr << "Could not deduce language from " << file.string() << "\n"; + exit(1); + return {}; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..dc9cd41 --- /dev/null +++ b/src/util.h @@ -0,0 +1,13 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include "libctor.h" + +#include +#include + +std::string readFile(const std::string& fileName); +std::vector readDeps(const std::string& depFile); +Language languageFromExtension(const std::filesystem::path& file); -- cgit v1.2.3