summaryrefslogtreecommitdiff
path: root/src/util.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc112
1 files changed, 112 insertions, 0 deletions
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 <iostream>
+#include <fstream>
+
+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(fileSize);
+ ifs.read(bytes.data(), fileSize);
+
+ return std::string(bytes.data(), fileSize);
+}
+
+std::vector<std::string> readDeps(const std::string& depFile)
+{
+ if(!std::filesystem::exists(depFile))
+ {
+ return {};
+ }
+
+ 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);
+ }
+
+ 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 {};
+}