Passwordfile library: io/entry.h Source File

Passwordfile library 5.0.13 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
17 enum class EntryType : int {
18 Node,
19 Account
20};
21
22 struct EntryStatistics {
23 std::size_t nodeCount = 0;
24 std::size_t accountCount = 0;
25 std::size_t fieldCount = 0;
26};
27
28class NodeEntry;
29
30 class PASSWORD_FILE_EXPORT Entry {
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
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;
83 makeLabelUnique();
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
107 inline EntryStatistics Entry::computeStatistics() const
108{
109 EntryStatistics stats;
110 accumulateStatistics(stats);
111 return stats;
112}
113
114 class PASSWORD_FILE_EXPORT NodeEntry : public Entry {
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
140 inline EntryType NodeEntry::type() const
141{
142 return EntryType::Node;
143}
144
145 inline const std::vector<Entry *> &NodeEntry::children() const
146{
147 return m_children;
148}
149
150 inline bool NodeEntry::isExpandedByDefault() const
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
170 class PASSWORD_FILE_EXPORT AccountEntry : public Entry {
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
189 inline EntryType AccountEntry::type() const
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::AccountEntry::fields
const std::vector< Field > & fields() const
Definition entry.h:194
Io::AccountEntry::accumulateStatistics
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this account entry and its fields.
Definition entry.cpp:513
Io::AccountEntry::clone
AccountEntry * clone() const override
Clones the entry.
Definition entry.cpp:505
Io::AccountEntry::AccountEntry
AccountEntry()
Definition entry.cpp:433
Io::AccountEntry::make
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
Definition entry.cpp:490
Io::AccountEntry::type
EntryType type() const override
Returns the type of the entry.
Definition entry.h:189
Io::Entry::accumulateStatistics
virtual void accumulateStatistics(EntryStatistics &stats) const =0
Io::Entry::index
int index() const
Returns the index of the entry within its parent.
Definition entry.h:98
Io::Entry::parse
static Entry * parse(std::istream &stream)
Parses an entry from the specified stream.
Definition entry.cpp:173
Io::Entry::Entry
Entry(const std::string &label=std::string(), NodeEntry *parent=nullptr)
Constructs a new entry with the specified label and parent.
Definition entry.cpp:29
Io::Entry::denotedEntryType
static constexpr EntryType denotedEntryType(std::uint8_t version)
Definition entry.h:165
Io::Entry::makeLabelUnique
void makeLabelUnique()
Internally called to make the entry's label unique within the parent.
Definition entry.cpp:61
Io::Entry::NodeEntry
friend class NodeEntry
Definition entry.h:31
Io::Entry::isIndirectChildOf
bool isIndirectChildOf(const NodeEntry *entry) const
Returns an indication whether the instance is an indirect child of the specified entry.
Definition entry.cpp:136
Io::Entry::setParent
void setParent(NodeEntry *parent, int index=-1)
Sets the parent for the entry.
Definition entry.cpp:96
Io::Entry::parent
NodeEntry * parent() const
Returns the parent entry.
Definition entry.h:90
Io::Entry::denotesNodeEntry
static bool denotesNodeEntry(std::uint8_t version)
Definition entry.h:160
Io::Entry::setLabel
void setLabel(const std::string &label)
Sets the label.
Definition entry.h:80
Io::Entry::make
virtual void make(std::ostream &stream) const =0
Serializes the entry to the specified stream.
Io::Entry::computeStatistics
EntryStatistics computeStatistics() const
Computes statistics for this entry.
Definition entry.h:107
Io::Entry::m_extendedData
std::string m_extendedData
Definition entry.h:64
Io::Entry::label
const std::string & label() const
Returns the label.
Definition entry.h:70
Io::Entry::clone
virtual Entry * clone() const =0
Clones the entry.
Io::Entry::type
virtual EntryType type() const =0
Returns the type of the entry.
Io::Entry::path
std::list< std::string > path() const
Returns the path of the entry.
Definition entry.cpp:151
Io::Entry::operator=
Entry & operator=(const Entry &other)=delete
Io::NodeEntry
The NodeEntry class acts as parent for other entries.
Definition entry.h:114
Io::NodeEntry::entryByPath
Entry * entryByPath(std::list< std::string > &path, bool includeThis=true, const EntryType *creationType=nullptr)
Returns an entry specified by the provided path.
Definition entry.cpp:347
Io::NodeEntry::replaceChild
void replaceChild(std::size_t at, Entry *newChild)
Replaces the child at the specified index with the specified newChild.
Definition entry.cpp:314
Io::NodeEntry::setExpandedByDefault
void setExpandedByDefault(bool expandedByDefault)
Definition entry.h:155
Io::NodeEntry::type
EntryType type() const override
Returns the type of the entry.
Definition entry.h:140
Io::NodeEntry::isExpandedByDefault
bool isExpandedByDefault() const
Definition entry.h:150
Io::NodeEntry::accumulateStatistics
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this node entry and its children.
Definition entry.cpp:420
Io::NodeEntry::clone
NodeEntry * clone() const override
Clones the entry.
Definition entry.cpp:412
Io::NodeEntry::Entry
friend class Entry
Definition entry.h:115
Io::NodeEntry::NodeEntry
NodeEntry()
Constructs a new node entry.
Definition entry.cpp:208
Io::NodeEntry::deleteChildren
void deleteChildren(int begin, int end)
Deletes children from the node entry.
Definition entry.cpp:287
Io::NodeEntry::children
const std::vector< Entry * > & children() const
Definition entry.h:145
Io::NodeEntry::make
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
Definition entry.cpp:392
field.h
PASSWORD_FILE_EXPORT
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
Definition global.h:14
Io
Contains all IO related classes.
Definition cryptoexception.h:9
Io::EntryType
EntryType
Specifies the entry type.
Definition entry.h:17
Io::EntryType::Account
@ Account
Definition entry.h:19
Io::EntryType::Node
@ Node
Definition entry.h:18
Io::EntryStatistics
Definition entry.h:22
Io::EntryStatistics::accountCount
std::size_t accountCount
Definition entry.h:24
Io::EntryStatistics::nodeCount
std::size_t nodeCount
Definition entry.h:23
Io::EntryStatistics::fieldCount
std::size_t fieldCount
Definition entry.h:25