Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
FileSystem.h
Go to the documentation of this file.
1 
10 #ifndef SUPPORTLIB_FILESYSTEM_H
11 #define SUPPORTLIB_FILESYSTEM_H
12 
13 #include "Exception.h"
14 #include <vector>
15 #include <fstream>
16 #include <filesystem>
17 #include <future>
18 #include <iostream>
19 
20 namespace giri {
21 
25  namespace FileSystem {
26 
30  class FileSystemException final : public ExceptionBase
31  {
32  public:
33  FileSystemException(const std::string &msg) : ExceptionBase(msg) {};
34  using SPtr = std::shared_ptr<FileSystemException>;
35  using UPtr = std::unique_ptr<FileSystemException>;
36  using WPtr = std::weak_ptr<FileSystemException>;
37  };
38 
44  inline std::vector<char> LoadFile(const std::filesystem::path& file) {
45  if(!std::filesystem::exists(file))
46  throw FileSystemException("File does not exist: " + file.string());
47 
48  std::ifstream in;
49  in.open(file, std::ios::in | std::ios::binary);
50  if (!in.good())
51  throw FileSystemException("Could not open file: " + file.string());
52 
53  in.seekg(0, in.end); size_t len = in.tellg(); in.seekg(0, in.beg);
54  std::vector<char> b(len);
55  in.read((char*)&b[0], len);
56  in.close();
57  return b;
58  }
59 
68  inline bool WriteFile(const std::filesystem::path& file, const std::vector<char>& data)
69  {
70  std::ofstream out;
71  out.open(file, std::ios::out | std::ios::binary);
72  if (!out.good())
73  return false;
74  out.write((char*)&data[0], data.size() * sizeof(char));
75  out.close();
76  return true;
77  }
83  inline std::filesystem::path FindExecutableInPath(const std::string &executable)
84  {
85  std::string path = getenv("PATH");
86  #if defined(_WIN32)
87  char delim = ';';
88  #else
89  char delim = ':';
90  #endif
91  std::vector<std::string> tokens;
92  std::string token;
93  std::istringstream tokenStream(path);
94  while (std::getline(tokenStream, token, delim))
95  {
96  std::filesystem::path exec(token);
97  exec.append(executable);
98  if(std::filesystem::exists(exec))
99  return exec;
100  exec.replace_extension(".exe");
101  if(std::filesystem::exists(exec))
102  return exec;
103  }
104  return "";
105  }
111  inline std::string ExecuteSync(const std::string& cmd) {
112  std::array<char, 128> buffer;
113  std::string result;
114  #if defined(_WIN32)
115  std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
116  #else
117  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
118  #endif
119  if (!pipe) {
120  throw FileSystemException("popen() failed!");
121  }
122  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
123  result += buffer.data();
124  }
125  return result;
126  }
133  inline std::future<void> ExecuteAsync(const std::string& cmd, std::ostream& ostr = std::cout) {
134  auto hdl = std::async(std::launch::async, [&] {
135  #if defined(_WIN32)
136  std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
137  #else
138  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
139  #endif
140  if (!pipe) {
141  throw FileSystemException("popen() failed!");
142  }
143  std::array<char, 128> buffer;
144  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
145  ostr << buffer.data();
146  }
147  return;
148  });
149  return hdl;
150  }
151  }
152 }
153 #endif //SUPPORTLIB_FILESYSTEM_H
Base exception to inherit custom exceptions from.
Base exception to inherit custom exceptions from.
Definition: Exception.h:48
ExceptionBase(const std::string &msg="")
Definition: Exception.h:54
Exception to be thrown on FileSystem errors.
Definition: FileSystem.h:31
std::vector< char > LoadFile(const std::filesystem::path &file)
Definition: FileSystem.h:44
std::string ExecuteSync(const std::string &cmd)
Definition: FileSystem.h:111
std::future< void > ExecuteAsync(const std::string &cmd, std::ostream &ostr=std::cout)
Definition: FileSystem.h:133
bool WriteFile(const std::filesystem::path &file, const std::vector< char > &data)
Definition: FileSystem.h:68
std::filesystem::path FindExecutableInPath(const std::string &executable)
Definition: FileSystem.h:83
Namespace for giri's C++ support library.
Definition: Base64.h:47