File: file.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (103 lines) | stat: -rw-r--r-- 2,404 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once

#include <streambuf>

#include "common.hpp"

POLYBAR_NS

class file_descriptor {
 public:
  explicit file_descriptor(const string& path, int flags = 0, bool autoclose = true);
  explicit file_descriptor(int fd, bool autoclose = true);
  ~file_descriptor();

  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};
  bool m_autoclose{true};
};

class fd_streambuf : public std::streambuf {
 public:
  using traits_type = std::streambuf::traits_type;

  template <typename... Args>
  explicit fd_streambuf(Args&&... args) : m_fd(forward<Args>(args)...) {
    setg(m_in, m_in + BUFSIZE_IN, m_in + BUFSIZE_IN);
    setp(m_out, m_out + BUFSIZE_OUT - 1);
  }
  ~fd_streambuf();

  explicit operator int();
  operator int() const;

  void open(int fd);
  void close();

 protected:
  int sync() override;
  int overflow(int c) override;
  int underflow() override;

 private:
  static constexpr int BUFSIZE_OUT = 1024;
  static constexpr int BUFSIZE_IN = 1024;
  file_descriptor m_fd;
  char m_out[BUFSIZE_OUT];
  char m_in[BUFSIZE_IN];
};

template <typename StreamType>
class fd_stream : public StreamType {
 public:
  using type = fd_stream<StreamType>;

  template <typename... Args>
  explicit fd_stream(Args&&... args) : StreamType(nullptr), m_buf(forward<Args>(args)...) {
    StreamType::rdbuf(&m_buf);
  }

  explicit operator int() {
    return static_cast<const type&>(*this);
  }

  operator int() const {
    return m_buf;
  }

 protected:
  fd_streambuf m_buf;
};

namespace file_util {
  bool exists(const string& filename);
  bool is_file(const string& filename);
  bool is_dir(const string& filename);
  string pick(const vector<string>& filenames);
  string contents(const string& filename);
  void write_contents(const string& filename, const string& contents);
  bool is_fifo(const string& filename);
  vector<string> glob(string pattern);
  string expand(const string& path, const string& relative_to = {});
  string get_config_path();
  vector<string> list_files(const string& dirname);
  string dirname(const string& path);

  template <typename... Args>
  decltype(auto) make_file_descriptor(Args&&... args) {
    return std::make_unique<file_descriptor>(forward<Args>(args)...);
  }
}  // namespace file_util

POLYBAR_NS_END