23 #include <boost/filesystem.hpp> 27 namespace fs = boost::filesystem;
32 type_(SourceTypeFile),
35 fs::path fspath{this->path_};
36 for (
const auto& pathPart : fspath.relative_path())
37 this->pathParts_.push_back(pathPart.string());
41 Source::Source(
const std::string& program,
const std::vector<std::string>& arguments) noexcept :
42 type_(SourceTypeCommand)
44 this->path_ = program;
45 for (
const auto& arg : arguments)
46 this->path_ +=
" " + arg;
52 std::vector<std::string> paths;
55 int r = glob(pattern.c_str(), 0, NULL, &globbuf);
57 for (
size_t i = 0; i < globbuf.gl_pathc; i++) {
58 if (fs::is_regular_file(fs::status(globbuf.gl_pathv[i])))
59 paths.emplace_back(globbuf.gl_pathv[i]);
100 this->
file_ = std::ifstream(this->
path_, std::ios::binary);
102 perror(std::string(this->
path_).append(
": Error opening file").c_str());
108 this->
file_.seekg(0);
111 perror(std::string(this->
path_).append(
": Error reading file").c_str());
115 return this->
file_.gcount();
123 this->
process_.open(this->
path_, redi::pstreambuf::pstdout | redi::pstreambuf::pstderr);
134 this->
buffer_.resize(1024,
'\0');
136 switch (this->
type_) {
138 if (this->
readFile(
true) == ((
size_t)-1))
140 while (!this->
file_.eof()) {
142 if (this->
readFile(
true) == ((size_t)-1))
163 switch (this->
type_) {
171 if (size == ((
size_t)-1)) {
178 this->
timestamp_ = std::chrono::system_clock::now();
190 std::string_view::const_iterator
Source::getLine(std::string_view::const_iterator begin)
const noexcept {
192 while (end != this->end() && *end !=
'\n')
std::string_view contents_
Read-only view on the buffer_.
std::ifstream file_
For file sources, the file descriptor.
const std::string & path() const noexcept
Returns the file path or command to execute.
bool reset() noexcept
Resets the source: attempts to open the file or execute the command, allocates enough space for conte...
SourceType
Enum of existing source type.
std::string_view::const_iterator getLine(std::string_view::const_iterator begin) const noexcept
Returns an iterator to the end of the specified line.
std::vector< char > buffer_
Buffer for file contents or command output.
std::string_view::const_iterator begin() const noexcept
Returns the iterator to the beginning of the source's contents.
std::chrono::system_clock::time_point timestamp() const noexcept
Returns the timestamp of stored contents.
bool update() noexcept
Updates the source (read the file or execute command) and store the results.
redi::ipstream process_
For command sources, the child process.
std::vector< std::shared_ptr< Expression > > expressions_
Array of expressions used on the source's contents.
std::string_view::const_iterator end() const noexcept
Returns the iterator to the end of the source's contents.
static std::vector< std::string > filePathsMatchingGlobbingPattern(const std::string &pattern) noexcept
Returns all file paths that match a pattern as defined by POSIX.
const std::vector< std::string > & pathParts() const noexcept
For file sources, returns the different parts of the file's path.
const std::string_view & contents() const noexcept
Returns the stored contents (file contents or command output)
SourceType type_
The type of the source.
std::chrono::system_clock::time_point timestamp_
Last contents or output fetching time.
std::vector< std::string > pathParts_
For file sources, the parts of the file's path.
Source(const std::string &filePath) noexcept
Construct a new Source object of file type.
std::string path_
Path of the file or command to execute.
size_t readFile(bool firstTime=false)
For file sources, put the file contents into the buffer_.
SourceType type()
Returns the source type.
std::vector< std::shared_ptr< Expression > > & expressions() noexcept
Returns the array of the receiver's expressions.
size_t executeCommand(bool firstTime=false)
For command sources, put the command output into the buffer_.