Passwordfile library  3.2.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 
23 class NodeEntry;
24 
26  friend class NodeEntry;
27 
28 public:
29  virtual ~Entry();
30  Entry &operator=(const Entry &other) = delete;
31  virtual EntryType type() const = 0;
32  const std::string &label() const;
33  void setLabel(const std::string &label);
34  void makeLabelUnique();
35  NodeEntry *parent() const;
36  void setParent(NodeEntry *parent, int index = -1);
37  int index() const;
38  bool isIndirectChildOf(NodeEntry *entry) const;
39  std::list<std::string> path() const;
40  void path(std::list<std::string> &res) const;
41  virtual void make(std::ostream &stream) const = 0;
42  virtual Entry *clone() const = 0;
43  static Entry *parse(std::istream &stream);
44  static bool denotesNodeEntry(byte version);
45  static constexpr EntryType denotedEntryType(byte version);
46 
47 protected:
48  Entry(const std::string &label = std::string(), NodeEntry *parent = nullptr);
49  Entry(const Entry &other);
50 
51 private:
52  std::string m_label;
53  NodeEntry *m_parent;
54  int m_index;
55 
56 protected:
57  std::string m_extendedData;
58 };
59 
63 inline const std::string &Entry::label() const
64 {
65  return m_label;
66 }
67 
73 inline void Entry::setLabel(const std::string &label)
74 {
75  m_label = label;
77 }
78 
83 inline NodeEntry *Entry::parent() const
84 {
85  return m_parent;
86 }
87 
91 inline int Entry::index() const
92 {
93  return m_index;
94 }
95 
97  friend class Entry;
98 
99 public:
100  NodeEntry();
101  NodeEntry(const std::string &label, NodeEntry *parent = nullptr);
102  NodeEntry(std::istream &stream);
103  NodeEntry(const NodeEntry &other);
104  ~NodeEntry();
105 
106  virtual EntryType type() const;
107  const std::vector<Entry *> &children() const;
108  void deleteChildren(int begin, int end);
109  void replaceChild(std::size_t at, Entry *newChild);
110  Entry *entryByPath(std::list<std::string> &path, bool includeThis = true, EntryType *creationType = nullptr);
111  bool isExpandedByDefault() const;
112  void setExpandedByDefault(bool expandedByDefault);
113  virtual void make(std::ostream &stream) const;
114  virtual NodeEntry *clone() const;
115 
116 private:
117  std::vector<Entry *> m_children;
118  bool m_expandedByDefault;
119 };
120 
122 {
123  return EntryType::Node;
124 }
125 
126 inline const std::vector<Entry *> &NodeEntry::children() const
127 {
128  return m_children;
129 }
130 
132 {
133  return m_expandedByDefault;
134 }
135 
136 inline void NodeEntry::setExpandedByDefault(bool expandedByDefault)
137 {
138  m_expandedByDefault = expandedByDefault;
139 }
140 
141 inline bool Entry::denotesNodeEntry(byte version)
142 {
143  return (version & 0x80) == 0;
144 }
145 
146 constexpr EntryType Entry::denotedEntryType(byte version)
147 {
148  return (version & 0x80) == 0 ? EntryType::Node : EntryType::Account;
149 }
150 
152 public:
153  AccountEntry();
154  AccountEntry(const std::string &label, NodeEntry *parent = nullptr);
155  AccountEntry(std::istream &stream);
156  AccountEntry(const AccountEntry &other);
157  ~AccountEntry();
158 
159  virtual EntryType type() const;
160  const std::vector<Field> &fields() const;
161  std::vector<Field> &fields();
162  virtual void make(std::ostream &stream) const;
163  virtual AccountEntry *clone() const;
164 
165 private:
166  std::vector<Field> m_fields;
167 };
168 
170 {
171  return EntryType::Account;
172 }
173 
174 inline const std::vector<Field> &AccountEntry::fields() const
175 {
176  return m_fields;
177 }
178 
179 inline std::vector<Field> &AccountEntry::fields()
180 {
181  return m_fields;
182 }
183 } // namespace Io
184 
185 #endif // PASSWORD_FILE_IO_ENTRY_H
NodeEntry * parent() const
Returns the parent entry.
Definition: entry.h:83
The NodeEntry class acts as parent for other entries.
Definition: entry.h:96
const std::string & label() const
Returns the label.
Definition: entry.h:63
void setLabel(const std::string &label)
Sets the label.
Definition: entry.h:73
static constexpr EntryType denotedEntryType(byte version)
Definition: entry.h:146
bool isExpandedByDefault() const
Definition: entry.h:131
virtual EntryType type() const
Returns the type of the entry.
Definition: entry.h:169
Contains all IO related classes.
int index() const
Returns the index of the entry within its parent.
Definition: entry.h:91
std::string m_extendedData
Definition: entry.h:57
The exception that is thrown when a parsing error occurs.
Definition: entry.h:151
#define PASSWORD_FILE_EXPORT
Marks the symbol to be exported by the passwordfile library.
static bool denotesNodeEntry(byte version)
Definition: entry.h:141
const std::vector< Entry * > & children() const
Definition: entry.h:126
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:174
EntryType
Specifies the entry type.
Definition: entry.h:18
void setExpandedByDefault(bool expandedByDefault)
Definition: entry.h:136
virtual EntryType type() const
Returns the type of the entry.
Definition: entry.h:121
Instances of the Entry class form a hierarchic data strucutre used to store account information...
Definition: entry.h:25