2016-06-15 05:32:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-11 03:07:28 +01:00
|
|
|
#include <streambuf>
|
|
|
|
|
2016-06-15 05:32:35 +02:00
|
|
|
#include "common.hpp"
|
2016-12-20 05:05:43 +01:00
|
|
|
#include "utils/factory.hpp"
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-11-19 06:22:44 +01:00
|
|
|
POLYBAR_NS
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
class file_ptr {
|
|
|
|
public:
|
|
|
|
explicit file_ptr(const string& path, const string& mode = "a+");
|
|
|
|
~file_ptr();
|
|
|
|
|
2016-12-26 10:29:32 +01:00
|
|
|
explicit operator bool();
|
|
|
|
operator bool() const;
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-26 10:29:32 +01:00
|
|
|
explicit operator FILE*();
|
|
|
|
operator FILE*() const;
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-26 10:29:32 +01:00
|
|
|
explicit operator int();
|
|
|
|
operator int() const;
|
2016-12-15 03:30:41 +01:00
|
|
|
|
2016-12-26 10:29:32 +01:00
|
|
|
private:
|
2016-12-20 05:05:43 +01:00
|
|
|
FILE* m_ptr = nullptr;
|
|
|
|
string m_path;
|
|
|
|
string m_mode;
|
|
|
|
};
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
class file_descriptor {
|
|
|
|
public:
|
|
|
|
explicit file_descriptor(const string& path, int flags = 0);
|
2017-01-01 08:58:33 +01:00
|
|
|
explicit file_descriptor(int fd, bool autoclose = true);
|
2016-12-20 05:05:43 +01:00
|
|
|
~file_descriptor();
|
2016-12-26 10:29:32 +01: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 08:58:33 +01:00
|
|
|
bool m_autoclose{true};
|
2016-12-26 10:29:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class fd_streambuf : public std::streambuf {
|
|
|
|
public:
|
|
|
|
using traits_type = std::streambuf::traits_type;
|
|
|
|
|
2017-01-01 08:58:33 +01:00
|
|
|
template <typename... Args>
|
|
|
|
explicit fd_streambuf(Args&&... args) : m_fd(forward<Args>(args)...) {
|
|
|
|
setg(m_in, m_in, m_in);
|
2017-01-14 09:42:46 +01:00
|
|
|
setp(m_out, m_out + bufsize - 1);
|
2017-01-01 08:58:33 +01:00
|
|
|
}
|
2016-12-26 10:29:32 +01:00
|
|
|
~fd_streambuf();
|
|
|
|
|
|
|
|
explicit operator int();
|
|
|
|
operator int() const;
|
|
|
|
|
|
|
|
void open(int fd);
|
|
|
|
void close();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int sync();
|
|
|
|
int overflow(int c);
|
|
|
|
int underflow();
|
|
|
|
|
|
|
|
private:
|
|
|
|
file_descriptor m_fd;
|
2017-01-14 09:42:46 +01:00
|
|
|
enum { bufsize = 1024 };
|
|
|
|
char m_out[bufsize];
|
|
|
|
char m_in[bufsize - 1];
|
2016-12-26 10:29:32 +01:00
|
|
|
};
|
|
|
|
|
2017-01-11 03:07:28 +01:00
|
|
|
template <typename StreamType>
|
2016-12-26 10:29:32 +01:00
|
|
|
class fd_stream : public StreamType {
|
|
|
|
public:
|
|
|
|
using type = fd_stream<StreamType>;
|
|
|
|
|
2017-01-01 08:58:33 +01:00
|
|
|
template <typename... Args>
|
2017-01-09 20:48:53 +01:00
|
|
|
explicit fd_stream(Args&&... args) : StreamType(nullptr), m_buf(forward<Args>(args)...) {
|
2016-12-26 10:29:32 +01:00
|
|
|
StreamType::rdbuf(&m_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator int() {
|
|
|
|
return static_cast<const type&>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int() const {
|
|
|
|
return m_buf;
|
|
|
|
}
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
protected:
|
2016-12-26 10:29:32 +01:00
|
|
|
fd_streambuf m_buf;
|
2016-12-20 05:05:43 +01:00
|
|
|
};
|
2016-06-15 05:32:35 +02:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
namespace file_util {
|
2016-11-25 13:55:15 +01:00
|
|
|
bool exists(const string& filename);
|
2016-12-31 04:20:46 +01:00
|
|
|
string pick(const vector<string>& filenames);
|
|
|
|
string contents(const string& filename);
|
2017-01-01 08:58:33 +01:00
|
|
|
bool is_fifo(const string& filename);
|
2017-01-25 17:07:55 +01:00
|
|
|
vector<string> glob(string pattern);
|
2017-06-02 18:29:55 +02:00
|
|
|
const string expand(const string& path);
|
2016-12-20 05:05:43 +01:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
decltype(auto) make_file_descriptor(Args&&... args) {
|
2016-12-23 01:07:00 +01:00
|
|
|
return factory_util::unique<file_descriptor>(forward<Args>(args)...);
|
2016-12-20 05:05:43 +01:00
|
|
|
}
|
2016-06-15 05:32:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-19 06:22:44 +01:00
|
|
|
POLYBAR_NS_END
|