Passwordfile library 5.0.12
C++ library to read/write passwords from/to encrypted files
Loading...
Searching...
No Matches
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
12namespace Io {
13
17enum 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
28class NodeEntry;
29
31 friend class NodeEntry;
32
33public:
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
54protected:
55 Entry(const std::string &label = std::string(), NodeEntry *parent = nullptr);
56 Entry(const Entry &other);
57
58private:
59 std::string m_label;
60 NodeEntry *m_parent;
61 int m_index;
62
63protected:
64 std::string m_extendedData;
65};
66
70inline const std::string &Entry::label() const
71{
72 return m_label;
73}
74
80inline void Entry::setLabel(const std::string &label)
81{
82 m_label = label;
84}
85
90inline NodeEntry *Entry::parent() const
91{
92 return m_parent;
93}
94
98inline int Entry::index() const
99{
100 return m_index;
101}
102
108{
109 EntryStatistics stats;
111 return stats;
112}
113
115 friend class Entry;
116
117public:
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
135private:
136 std::vector<Entry *> m_children;
137 bool m_expandedByDefault;
138};
139
141{
142 return EntryType::Node;
143}
144
145inline const std::vector<Entry *> &NodeEntry::children() const
146{
147 return m_children;
148}
149
151{
152 return m_expandedByDefault;
153}
154
155inline void NodeEntry::setExpandedByDefault(bool expandedByDefault)
156{
157 m_expandedByDefault = expandedByDefault;
158}
159
160inline bool Entry::denotesNodeEntry(std::uint8_t version)
161{
162 return (version & 0x80) == 0;
163}
164
165constexpr EntryType Entry::denotedEntryType(std::uint8_t version)
166{
167 return (version & 0x80) == 0 ? EntryType::Node : EntryType::Account;
168}
169
171public:
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
185private:
186 std::vector<Field> m_fields;
187};
188
190{
191 return EntryType::Account;
192}
193
194inline const std::vector<Field> &AccountEntry::fields() const
195{
196 return m_fields;
197}
198
199inline std::vector<Field> &AccountEntry::fields()
200{
201 return m_fields;
202}
203} // namespace Io
204
205#endif // PASSWORD_FILE_IO_ENTRY_H
The exception that is thrown when a parsing error occurs.
Definition entry.h:170
const std::vector< Field > & fields() const
Definition entry.h:194
EntryType type() const override
Returns the type of the entry.
Definition entry.h:189
Instances of the Entry class form a hierarchic data structure used to store account information.
Definition entry.h:30
virtual void accumulateStatistics(EntryStatistics &stats) const =0
int index() const
Returns the index of the entry within its parent.
Definition entry.h:98
static constexpr EntryType denotedEntryType(std::uint8_t version)
Definition entry.h:165
void makeLabelUnique()
Internally called to make the entry's label unique within the parent.
Definition entry.cpp:64
NodeEntry * parent() const
Returns the parent entry.
Definition entry.h:90
static bool denotesNodeEntry(std::uint8_t version)
Definition entry.h:160
void setLabel(const std::string &label)
Sets the label.
Definition entry.h:80
virtual void make(std::ostream &stream) const =0
Serializes the entry to the specified stream.
EntryStatistics computeStatistics() const
Computes statistics for this entry.
Definition entry.h:107
std::string m_extendedData
Definition entry.h:64
const std::string & label() const
Returns the label.
Definition entry.h:70
virtual Entry * clone() const =0
Clones the entry.
virtual EntryType type() const =0
Returns the type of the entry.
Entry & operator=(const Entry &other)=delete
The NodeEntry class acts as parent for other entries.
Definition entry.h:114
void setExpandedByDefault(bool expandedByDefault)
Definition entry.h:155
EntryType type() const override
Returns the type of the entry.
Definition entry.h:140
bool isExpandedByDefault() const
Definition entry.h:150
const std::vector< Entry * > & children() const
Definition entry.h:145
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
Definition global.h:14
Contains all IO related classes.
EntryType
Specifies the entry type.
Definition entry.h:17
std::size_t accountCount
Definition entry.h:24
std::size_t nodeCount
Definition entry.h:23
std::size_t fieldCount
Definition entry.h:25