libfilezilla
registry.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_GLUE_REGISTRY_HEADER
2 #define LIBFILEZILLA_GLUE_REGISTRY_HEADER
3 
8 #include "../libfilezilla.hpp"
9 
10 #ifdef FZ_WINDOWS
11 
12 #include "windows.hpp"
13 
14 #include <optional>
15 #include <string>
16 
17 namespace fz {
18 
25 class FZ_PUBLIC_SYMBOL regkey final
26 {
27 public:
28  regkey() = default;
29  ~regkey();
30 
31  enum regview {
32  regview_native,
33  regview_32,
34  regview_64
35  };
36 
37 
39  explicit regkey(HKEY const root, std::wstring const& subkey, bool readonly, regview v = regview_native);
40 
41  regkey(regkey const&) = delete;
42  regkey& operator=(regkey const&) = delete;
43 
44  void close();
45 
51  bool open(HKEY const root, std::wstring const& subkey, bool readonly, regview v = regview_native);
52 
53  bool has_value(std::wstring const& name) const;
54 
56  std::wstring value(std::wstring const& name) const;
57 
59  uint64_t int_value(std::wstring const& name) const;
60 
61  bool set_value(std::wstring const& name, std::wstring const& value);
62  bool set_value(std::wstring const& name, uint64_t value);
63 
64  explicit operator bool() const {
65  return key_.has_value();
66  }
67 
68  bool delete_value(std::wstring const& name);
69 
70  struct iterator final
71  {
72  struct value final
73  {
74  std::wstring name;
75  DWORD type{};
76  };
77 
78  iterator() = default;
79 
80  iterator &operator++()
81  {
82  if (key_ && key_->key_) {
83  DWORD len{16383};
84  v_.name.resize(len);
85 
86  DWORD res = RegEnumValueW(*key_->key_, ++index_, v_.name.data(), &len, nullptr, &v_.type, nullptr, nullptr);
87  if (res != ERROR_SUCCESS || !len) {
88  index_ = DWORD(-1);
89  }
90  else {
91  v_.name.resize(len);
92  }
93  }
94  return *this;
95  }
96 
97  bool operator==(iterator const& op) const
98  {
99  return index_ == op.index_;
100  }
101 
102  bool operator!=(iterator const& op) const
103  {
104  return !(*this == op);
105  }
106 
107  value const& operator*() const
108  {
109  return v_;
110  }
111 
112  value const* operator->() const
113  {
114  return &v_;
115  }
116 
117  private:
118  friend regkey;
119 
120  iterator(regkey const* key)
121  : key_(key)
122  {
123  operator++();
124  }
125 
126  regkey const* key_{};
127  DWORD index_{DWORD(-1)};
128  value v_;
129  };
130  using const_iterator = iterator;
131 
132  iterator begin() const
133  {
134  return { this };
135  }
136 
137  iterator end() const
138  {
139  return {};
140  }
141 
142  const_iterator cbegin() const
143  {
144  return { this };
145  }
146 
147  const_iterator cend() const
148  {
149  return {};
150  }
151 
152 private:
153  mutable std::optional<HKEY> key_;
154 };
155 }
156 
157 #else
158 #error This file is for Windows only
159 #endif
160 
161 #endif
Prepresents a key in the Windows registry.
Definition: registry.hpp:25
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
The namespace used by libfilezilla.
Definition: apply.hpp:17
Definition: registry.hpp:70