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