/
folderdb.cpp
executable file
·161 lines (133 loc) · 3.58 KB
/
folderdb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "folderdb.h"
#include "serialize.h"
#include "util/pathtools.h"
#include "settings.h"
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
FolderDB::FolderDB(const string &path)
: file{path}
{
load();
}
FolderDB::~FolderDB()
{
save();
}
void FolderDB::save() const
{
file.overwrite(serialize());
}
vector<char> FolderDB::serialize() const
{
vector<char> data;
vector<vector<char>> archivesData;
for (const Archive& f : archives)
archivesData.push_back(f.serialize());
vector<vector<char>> sourcesData;
for (const Source& f : sources)
sourcesData.push_back(::serialize(f.getPath()));
serializeAppend(data, archivesData);
serializeAppend(data, sourcesData);
return data;
}
void FolderDB::load()
{
deserialize(file.readAll());
}
void FolderDB::deserialize(const std::vector<char> &data)
{
createPathTo("/", dataPath()+"archive/");
if (data.empty())
return;
auto it = begin(data);
vector<vector<char>> archivesData = deserializeConsume<decltype(archivesData)>(it);
for (const vector<char>& vec : archivesData)
archives.emplace_back(vec);
vector<vector<char>> sourcesData = deserializeConsume<decltype(sourcesData)>(it);
for (const vector<char>& vec : sourcesData)
{
auto it = vec.begin();
sources.emplace_back(::dataToString(it));
}
}
const std::vector<Source> &FolderDB::getSources() const
{
return sources;
}
const std::vector<Archive>& FolderDB::getArchives() const
{
return archives;
}
Source *FolderDB::getSource(const string &path)
{
auto it = find_if(begin(sources), end(sources), [&path](const Source& s)
{
return s.getPath() == path;
});
if (it == end(sources))
return nullptr;
return &*it;
}
Archive *FolderDB::getArchive(const PathHash &pathHash)
{
auto it = find_if(begin(archives), end(archives), [&pathHash](const Archive& a)
{
return a.getPathHash() == pathHash;
});
if (it == end(archives))
return nullptr;
return &*it;
}
void FolderDB::addArchive(PathHash pathHash)
{
if (any_of(begin(archives), end(archives), [&pathHash](const Archive& a){return a.getPathHash() == pathHash;}))
return;
archives.emplace_back(pathHash);
}
void FolderDB::addSource(const std::string& path)
{
if (any_of(begin(sources), end(sources), [&path](const Source& s){return s.getPath() == path;}))
return;
sources.emplace_back(path);
}
bool FolderDB::removeArchive(const PathHash& pathHash)
{
auto it = find_if(begin(archives), end(archives), [&pathHash](const Archive& a)
{
return a.getPathHash() == pathHash;
});
if (it == end(archives))
{
cout << "FolderDB::removeArchive: Archive "<<pathHash.toBase64()<<" not found"<<endl;
return false;
}
it->removeData();
archives.erase(it);
return true;
}
bool FolderDB::removeArchive(const string &pathHashStr)
{
auto it = find_if(begin(archives), end(archives), [&pathHashStr](const Archive& a)
{
return a.getPathHash().toBase64() == pathHashStr;
});
if (it == end(archives))
{
cout << "FolderDB::removeArchive: Archive "<<pathHashStr<<" not found"<<endl;
return false;
}
it->removeData();
archives.erase(it);
return true;
}
bool FolderDB::removeSource(const std::string& path)
{
size_t size = sources.size();
sources.erase(remove_if(begin(sources), end(sources), [&path](const Source& s)
{
return s.getPath() == path;
}), end(sources));
return size != sources.size();
}