2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
2016-12-20 04:05:43 +00:00
|
|
|
#include "utils/factory.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
class file_ptr {
|
|
|
|
public:
|
|
|
|
explicit file_ptr(const string& path, const string& mode = "a+");
|
|
|
|
~file_ptr();
|
|
|
|
|
2016-12-26 09:29:32 +00:00
|
|
|
explicit operator bool();
|
|
|
|
operator bool() const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-26 09:29:32 +00:00
|
|
|
explicit operator FILE*();
|
|
|
|
operator FILE*() const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-26 09:29:32 +00:00
|
|
|
explicit operator int();
|
|
|
|
operator int() const;
|
2016-12-15 02:30:41 +00:00
|
|
|
|
2016-12-26 09:29:32 +00:00
|
|
|
private:
|
2016-12-20 04:05:43 +00:00
|
|
|
FILE* m_ptr = nullptr;
|
|
|
|
string m_path;
|
|
|
|
string m_mode;
|
|
|
|
};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
class file_descriptor {
|
|
|
|
public:
|
|
|
|
explicit file_descriptor(const string& path, int flags = 0);
|
|
|
|
explicit file_descriptor(int fd);
|
|
|
|
~file_descriptor();
|
2016-12-26 09:29:32 +00: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};
|
|
|
|
};
|
|
|
|
|
|
|
|
class fd_streambuf : public std::streambuf {
|
|
|
|
public:
|
|
|
|
using traits_type = std::streambuf::traits_type;
|
|
|
|
|
|
|
|
explicit fd_streambuf(int fd);
|
|
|
|
~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;
|
2016-12-31 03:32:11 +00:00
|
|
|
char m_out[BUFSIZ]{'\0'};
|
|
|
|
char m_in[BUFSIZ - 1]{'\0'};
|
2016-12-26 09:29:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename StreamType = std::ostream>
|
|
|
|
class fd_stream : public StreamType {
|
|
|
|
public:
|
|
|
|
using type = fd_stream<StreamType>;
|
|
|
|
|
|
|
|
explicit fd_stream(int fd) : m_buf(fd) {
|
|
|
|
StreamType::rdbuf(&m_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator int() {
|
|
|
|
return static_cast<const type&>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int() const {
|
|
|
|
return m_buf;
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
protected:
|
2016-12-26 09:29:32 +00:00
|
|
|
fd_streambuf m_buf;
|
2016-12-20 04:05:43 +00:00
|
|
|
};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
namespace file_util {
|
2016-11-25 12:55:15 +00:00
|
|
|
bool exists(const string& filename);
|
2016-12-31 03:20:46 +00:00
|
|
|
string pick(const vector<string>& filenames);
|
|
|
|
string contents(const string& filename);
|
2016-11-02 19:22:45 +00:00
|
|
|
bool is_fifo(string filename);
|
2016-12-20 04:05:43 +00:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
decltype(auto) make_file_descriptor(Args&&... args) {
|
2016-12-23 00:07:00 +00:00
|
|
|
return factory_util::unique<file_descriptor>(forward<Args>(args)...);
|
2016-12-20 04:05:43 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|