2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-10 21:07:28 -05:00
|
|
|
#include <streambuf>
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "common.hpp"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
class file_descriptor {
|
|
|
|
public:
|
2021-03-01 18:00:24 -05:00
|
|
|
explicit file_descriptor(const string& path, int flags = 0, bool autoclose = true);
|
2017-01-01 02:58:33 -05:00
|
|
|
explicit file_descriptor(int fd, bool autoclose = true);
|
2016-12-19 23:05:43 -05:00
|
|
|
~file_descriptor();
|
2016-12-26 04:29:32 -05:00
|
|
|
|
|
|
|
file_descriptor& operator=(const int);
|
|
|
|
|
|
|
|
explicit operator bool();
|
|
|
|
operator bool() const;
|
|
|
|
|
|
|
|
explicit operator int();
|
|
|
|
operator int() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void close();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd{-1};
|
2017-01-01 02:58:33 -05:00
|
|
|
bool m_autoclose{true};
|
2016-12-26 04:29:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class fd_streambuf : public std::streambuf {
|
|
|
|
public:
|
|
|
|
using traits_type = std::streambuf::traits_type;
|
|
|
|
|
2017-01-01 02:58:33 -05:00
|
|
|
template <typename... Args>
|
|
|
|
explicit fd_streambuf(Args&&... args) : m_fd(forward<Args>(args)...) {
|
2021-10-02 19:27:11 -04:00
|
|
|
setg(m_in, m_in + BUFSIZE_IN, m_in + BUFSIZE_IN);
|
|
|
|
setp(m_out, m_out + BUFSIZE_OUT - 1);
|
2017-01-01 02:58:33 -05:00
|
|
|
}
|
2016-12-26 04:29:32 -05:00
|
|
|
~fd_streambuf();
|
|
|
|
|
|
|
|
explicit operator int();
|
|
|
|
operator int() const;
|
|
|
|
|
|
|
|
void open(int fd);
|
|
|
|
void close();
|
|
|
|
|
|
|
|
protected:
|
2021-01-04 04:38:43 -05:00
|
|
|
int sync() override;
|
|
|
|
int overflow(int c) override;
|
|
|
|
int underflow() override;
|
2016-12-26 04:29:32 -05:00
|
|
|
|
|
|
|
private:
|
2021-10-02 19:27:11 -04:00
|
|
|
static constexpr int BUFSIZE_OUT = 1024;
|
|
|
|
static constexpr int BUFSIZE_IN = 1024;
|
2016-12-26 04:29:32 -05:00
|
|
|
file_descriptor m_fd;
|
2021-10-02 19:27:11 -04:00
|
|
|
char m_out[BUFSIZE_OUT];
|
|
|
|
char m_in[BUFSIZE_IN];
|
2016-12-26 04:29:32 -05:00
|
|
|
};
|
|
|
|
|
2017-01-10 21:07:28 -05:00
|
|
|
template <typename StreamType>
|
2016-12-26 04:29:32 -05:00
|
|
|
class fd_stream : public StreamType {
|
|
|
|
public:
|
|
|
|
using type = fd_stream<StreamType>;
|
|
|
|
|
2017-01-01 02:58:33 -05:00
|
|
|
template <typename... Args>
|
2017-01-09 14:48:53 -05:00
|
|
|
explicit fd_stream(Args&&... args) : StreamType(nullptr), m_buf(forward<Args>(args)...) {
|
2016-12-26 04:29:32 -05:00
|
|
|
StreamType::rdbuf(&m_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator int() {
|
|
|
|
return static_cast<const type&>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int() const {
|
|
|
|
return m_buf;
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
protected:
|
2016-12-26 04:29:32 -05:00
|
|
|
fd_streambuf m_buf;
|
2016-12-19 23:05:43 -05:00
|
|
|
};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
namespace file_util {
|
2016-11-25 07:55:15 -05:00
|
|
|
bool exists(const string& filename);
|
2020-11-24 19:35:38 -05:00
|
|
|
bool is_file(const string& filename);
|
2021-10-24 05:25:05 -04:00
|
|
|
bool is_dir(const string& filename);
|
2016-12-30 22:20:46 -05:00
|
|
|
string pick(const vector<string>& filenames);
|
|
|
|
string contents(const string& filename);
|
2020-01-15 10:32:17 -05:00
|
|
|
void write_contents(const string& filename, const string& contents);
|
2017-01-01 02:58:33 -05:00
|
|
|
bool is_fifo(const string& filename);
|
2017-01-25 11:07:55 -05:00
|
|
|
vector<string> glob(string pattern);
|
2021-10-20 06:31:15 -04:00
|
|
|
string expand(const string& path, const string& relative_to = {});
|
2020-03-01 16:03:17 -05:00
|
|
|
string get_config_path();
|
2020-10-08 11:44:29 -04:00
|
|
|
vector<string> list_files(const string& dirname);
|
2021-10-20 06:31:15 -04:00
|
|
|
string dirname(const string& path);
|
2016-12-19 23:05:43 -05:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
decltype(auto) make_file_descriptor(Args&&... args) {
|
2021-09-21 15:15:49 -04:00
|
|
|
return std::make_unique<file_descriptor>(forward<Args>(args)...);
|
2016-12-19 23:05:43 -05:00
|
|
|
}
|
2020-01-15 10:32:17 -05:00
|
|
|
} // namespace file_util
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|