passwordfile/io/entry.h

206 lines
5.0 KiB
C
Raw Normal View History

#ifndef PASSWORD_FILE_IO_ENTRY_H
#define PASSWORD_FILE_IO_ENTRY_H
2015-04-22 19:06:29 +02:00
2015-09-06 20:33:27 +02:00
#include "./field.h"
2015-04-22 19:06:29 +02:00
2019-03-13 19:08:30 +01:00
#include <cstdint>
2015-04-22 19:06:29 +02:00
#include <iostream>
2017-05-01 03:25:30 +02:00
#include <list>
2015-04-22 19:06:29 +02:00
#include <string>
#include <vector>
namespace Io {
/*!
* \brief Specifies the entry type.
*/
2017-05-01 03:25:30 +02:00
enum class EntryType : int {
2015-04-22 19:06:29 +02:00
Node, /**< denotes a NodeEntry */
Account /**< denotes an AccountEntry */
};
2018-12-21 01:12:53 +01:00
struct EntryStatistics {
std::size_t nodeCount = 0;
std::size_t accountCount = 0;
std::size_t fieldCount = 0;
};
2015-04-22 19:06:29 +02:00
class NodeEntry;
2017-05-01 03:25:30 +02:00
class PASSWORD_FILE_EXPORT Entry {
2015-04-22 19:06:29 +02:00
friend class NodeEntry;
2017-05-01 03:25:30 +02:00
2015-04-22 19:06:29 +02:00
public:
virtual ~Entry();
Entry &operator=(const Entry &other) = delete;
virtual EntryType type() const = 0;
const std::string &label() const;
void setLabel(const std::string &label);
void makeLabelUnique();
NodeEntry *parent() const;
void setParent(NodeEntry *parent, int index = -1);
int index() const;
2018-12-23 18:57:03 +01:00
bool isIndirectChildOf(const NodeEntry *entry) const;
2015-04-22 19:06:29 +02:00
std::list<std::string> path() const;
void path(std::list<std::string> &res) const;
virtual void make(std::ostream &stream) const = 0;
virtual Entry *clone() const = 0;
2018-12-21 01:12:53 +01:00
EntryStatistics computeStatistics() const;
virtual void accumulateStatistics(EntryStatistics &stats) const = 0;
2015-04-22 19:06:29 +02:00
static Entry *parse(std::istream &stream);
2019-03-13 19:08:30 +01:00
static bool denotesNodeEntry(std::uint8_t version);
static constexpr EntryType denotedEntryType(std::uint8_t version);
2015-04-22 19:06:29 +02:00
protected:
Entry(const std::string &label = std::string(), NodeEntry *parent = nullptr);
Entry(const Entry &other);
private:
std::string m_label;
NodeEntry *m_parent;
int m_index;
protected:
std::string m_extendedData;
2015-04-22 19:06:29 +02:00
};
/*!
* \brief Returns the label.
*/
inline const std::string &Entry::label() const
{
return m_label;
}
/*!
* \brief Sets the label.
* \remarks The label might be modified to ensure that each child entry within a certain parent
* has a unique label.
*/
inline void Entry::setLabel(const std::string &label)
{
m_label = label;
makeLabelUnique();
}
/*!
* \brief Returns the parent entry.
* \remarks Returns nullptr for top-level entries.
*/
inline NodeEntry *Entry::parent() const
{
return m_parent;
}
/*!
* \brief Returns the index of the entry within its parent. Returns -1 for parentless entries.
*/
inline int Entry::index() const
{
return m_index;
}
2018-12-21 01:12:53 +01:00
/*!
* \brief Computes statistics for this entry.
* \remarks Takes the current instance and children into account but not parents.
*/
inline EntryStatistics Entry::computeStatistics() const
{
EntryStatistics stats;
accumulateStatistics(stats);
return stats;
}
2017-05-01 03:25:30 +02:00
class PASSWORD_FILE_EXPORT NodeEntry : public Entry {
2015-04-22 19:06:29 +02:00
friend class Entry;
2017-05-01 03:25:30 +02:00
2015-04-22 19:06:29 +02:00
public:
NodeEntry();
NodeEntry(const std::string &label, NodeEntry *parent = nullptr);
NodeEntry(std::istream &stream);
NodeEntry(const NodeEntry &other);
2018-12-21 17:31:50 +01:00
~NodeEntry() override;
2015-04-22 19:06:29 +02:00
2018-12-21 17:31:50 +01:00
EntryType type() const override;
2015-04-22 19:06:29 +02:00
const std::vector<Entry *> &children() const;
void deleteChildren(int begin, int end);
2018-06-09 21:17:01 +02:00
void replaceChild(std::size_t at, Entry *newChild);
2018-12-23 18:57:03 +01:00
Entry *entryByPath(std::list<std::string> &path, bool includeThis = true, const EntryType *creationType = nullptr);
2015-04-22 19:06:29 +02:00
bool isExpandedByDefault() const;
void setExpandedByDefault(bool expandedByDefault);
2018-12-21 17:31:50 +01:00
void make(std::ostream &stream) const override;
NodeEntry *clone() const override;
void accumulateStatistics(EntryStatistics &stats) const override;
2015-04-22 19:06:29 +02:00
private:
std::vector<Entry *> m_children;
bool m_expandedByDefault;
};
inline EntryType NodeEntry::type() const
{
return EntryType::Node;
}
inline const std::vector<Entry *> &NodeEntry::children() const
{
return m_children;
}
inline bool NodeEntry::isExpandedByDefault() const
{
return m_expandedByDefault;
}
inline void NodeEntry::setExpandedByDefault(bool expandedByDefault)
{
m_expandedByDefault = expandedByDefault;
}
2019-03-13 19:08:30 +01:00
inline bool Entry::denotesNodeEntry(std::uint8_t version)
2015-04-22 19:06:29 +02:00
{
return (version & 0x80) == 0;
}
2019-03-13 19:08:30 +01:00
constexpr EntryType Entry::denotedEntryType(std::uint8_t version)
2018-06-09 21:17:01 +02:00
{
return (version & 0x80) == 0 ? EntryType::Node : EntryType::Account;
}
2017-05-01 03:25:30 +02:00
class PASSWORD_FILE_EXPORT AccountEntry : public Entry {
2015-04-22 19:06:29 +02:00
public:
AccountEntry();
AccountEntry(const std::string &label, NodeEntry *parent = nullptr);
AccountEntry(std::istream &stream);
AccountEntry(const AccountEntry &other);
2018-12-21 17:31:50 +01:00
~AccountEntry() override;
2015-04-22 19:06:29 +02:00
2018-12-21 17:31:50 +01:00
EntryType type() const override;
2015-04-22 19:06:29 +02:00
const std::vector<Field> &fields() const;
std::vector<Field> &fields();
2018-12-21 17:31:50 +01:00
void make(std::ostream &stream) const override;
AccountEntry *clone() const override;
void accumulateStatistics(EntryStatistics &stats) const override;
2017-05-01 03:25:30 +02:00
2015-04-22 19:06:29 +02:00
private:
std::vector<Field> m_fields;
};
inline EntryType AccountEntry::type() const
{
return EntryType::Account;
}
inline const std::vector<Field> &AccountEntry::fields() const
{
return m_fields;
}
inline std::vector<Field> &AccountEntry::fields()
{
return m_fields;
}
2018-03-20 20:11:31 +01:00
} // namespace Io
2015-04-22 19:06:29 +02:00
#endif // PASSWORD_FILE_IO_ENTRY_H