Passwordfile library  4.0.0
C++ library to read/write passwords from/to encrypted files
entry.h
Go to the documentation of this file.
1 #ifndef PASSWORD_FILE_IO_ENTRY_H
2 #define PASSWORD_FILE_IO_ENTRY_H
3 
4 #include "./field.h"
5 
6 #include <c++utilities/conversion/types.h>
7 
8 #include <iostream>
9 #include <list>
10 #include <string>
11 #include <vector>
12 
13 namespace Io {
14 
18 enum class EntryType : int {
19  Node,
20  Account
21 };
22 
24  std::size_t nodeCount = 0;
25  std::size_t accountCount = 0;
26  std::size_t fieldCount = 0;
27 };
28 
29 class NodeEntry;
30 
32  friend class NodeEntry;
33 
34 public:
35  virtual ~Entry();
36  Entry &operator=(const Entry &other) = delete;
37  virtual EntryType type() const = 0;
38  const std::string &label() const;
39  void setLabel(const std::string &label);
40  void makeLabelUnique();
41  NodeEntry *parent() const;
42  void setParent(NodeEntry *parent, int index = -1);
43  int index() const;
44  bool isIndirectChildOf(const NodeEntry *entry) const;
45  std::list<std::string> path() const;
46  void path(std::list<std::string> &res) const;
47  virtual void make(std::ostream &stream) const = 0;
48  virtual Entry *clone() const = 0;
49  EntryStatistics computeStatistics() const;
50  virtual void accumulateStatistics(EntryStatistics &stats) const = 0;
51  static Entry *parse(std::istream &stream);
52  static bool denotesNodeEntry(byte version);
53  static constexpr EntryType denotedEntryType(byte version);
54 
55 protected:
56  Entry(const std::string &label = std::string(), NodeEntry *parent = nullptr);
57  Entry(const Entry &other);
58 
59 private:
60  std::string m_label;
61  NodeEntry *m_parent;
62  int m_index;
63 
64 protected:
65  std::string m_extendedData;
66 };
67 
71 inline const std::string &Entry::label() const
72 {
73  return m_label;
74 }
75 
81 inline void Entry::setLabel(const std::string &label)
82 {
83  m_label = label;
85 }
86 
91 inline NodeEntry *Entry::parent() const
92 {
93  return m_parent;
94 }
95 
99 inline int Entry::index() const
100 {
101  return m_index;
102 }
103 
109 {
110  EntryStatistics stats;
111  accumulateStatistics(stats);
112  return stats;
113 }
114 
116  friend class Entry;
117 
118 public:
119  NodeEntry();
120  NodeEntry(const std::string &label, NodeEntry *parent = nullptr);
121  NodeEntry(std::istream &stream);
122  NodeEntry(const NodeEntry &other);
123  ~NodeEntry() override;
124 
125  EntryType type() const override;
126  const std::vector<Entry *> &children() const;
127  void deleteChildren(int begin, int end);
128  void replaceChild(std::size_t at, Entry *newChild);
129  Entry *entryByPath(std::list<std::string> &path, bool includeThis = true, const EntryType *creationType = nullptr);
130  bool isExpandedByDefault() const;
131  void setExpandedByDefault(bool expandedByDefault);
132  void make(std::ostream &stream) const override;
133  NodeEntry *clone() const override;
134  void accumulateStatistics(EntryStatistics &stats) const override;
135 
136 private:
137  std::vector<Entry *> m_children;
138  bool m_expandedByDefault;
139 };
140 
142 {
143  return EntryType::Node;
144 }
145 
146 inline const std::vector<Entry *> &NodeEntry::children() const
147 {
148  return m_children;
149 }
150 
152 {
153  return m_expandedByDefault;
154 }
155 
156 inline void NodeEntry::setExpandedByDefault(bool expandedByDefault)
157 {
158  m_expandedByDefault = expandedByDefault;
159 }
160 
161 inline bool Entry::denotesNodeEntry(byte version)
162 {
163  return (version & 0x80) == 0;
164 }
165 
166 constexpr EntryType Entry::denotedEntryType(byte version)
167 {
168  return (version & 0x80) == 0 ? EntryType::Node : EntryType::Account;
169 }
170 
172 public:
173  AccountEntry();
174  AccountEntry(const std::string &label, NodeEntry *parent = nullptr);
175  AccountEntry(std::istream &stream);
176  AccountEntry(const AccountEntry &other);
177  ~AccountEntry() override;
178 
179  EntryType type() const override;
180  const std::vector<Field> &fields() const;
181  std::vector<Field> &fields();
182  void make(std::ostream &stream) const override;
183  AccountEntry *clone() const override;
184  void accumulateStatistics(EntryStatistics &stats) const override;
185 
186 private:
187  std::vector<Field> m_fields;
188 };
189 
191 {
192  return EntryType::Account;
193 }
194 
195 inline const std::vector<Field> &AccountEntry::fields() const
196 {
197  return m_fields;
198 }
199 
200 inline std::vector<Field> &AccountEntry::fields()
201 {
202  return m_fields;
203 }
204 } // namespace Io
205 
206 #endif // PASSWORD_FILE_IO_ENTRY_H
NodeEntry * parent() const
Returns the parent entry.
Definition: entry.h:91
The NodeEntry class acts as parent for other entries.
Definition: entry.h:115
const std::string & label() const
Returns the label.
Definition: entry.h:71
void setLabel(const std::string &label)
Sets the label.
Definition: entry.h:81
EntryStatistics computeStatistics() const
Computes statistics for this entry.
Definition: entry.h:108
static constexpr EntryType denotedEntryType(byte version)
Definition: entry.h:166
bool isExpandedByDefault() const
Definition: entry.h:151
Contains all IO related classes.
int index() const
Returns the index of the entry within its parent.
Definition: entry.h:99
std::size_t accountCount
Definition: entry.h:25
std::size_t fieldCount
Definition: entry.h:26
std::string m_extendedData
Definition: entry.h:65
The exception that is thrown when a parsing error occurs.
Definition: entry.h:171
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
virtual void accumulateStatistics(EntryStatistics &stats) const =0
EntryType type() const override
Returns the type of the entry.
Definition: entry.h:141
static bool denotesNodeEntry(byte version)
Definition: entry.h:161
const std::vector< Entry * > & children() const
Definition: entry.h:146
EntryType type() const override
Returns the type of the entry.
Definition: entry.h:190
std::size_t nodeCount
Definition: entry.h:24
void makeLabelUnique()
Internally called to make the entry&#39;s label unique within the parent.
Definition: entry.cpp:65
const std::vector< Field > & fields() const
Definition: entry.h:195
EntryType
Specifies the entry type.
Definition: entry.h:18
void setExpandedByDefault(bool expandedByDefault)
Definition: entry.h:156
Instances of the Entry class form a hierarchic data strucutre used to store account information...
Definition: entry.h:31