- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
Latest commit
58 lines (39 loc) · 1.06 KB
/
Copy pathDatabase.cpp
File metadata and controls
58 lines (39 loc) · 1.06 KB
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
#include"Database.h"
#include<fstream>
Database::Database() {
incarcaDinFisier();
}
voidDatabase::salveazaInFisier() {
std::ofstream f("database.txt");
for (auto pair : db) {
f << pair.first << "" << pair.second << "\n";
}
}
voidDatabase::incarcaDinFisier() {
std::ifstream f("database.txt");
std::string cheie, valoare;
while (f >> cheie >> valoare) {
db[cheie] = valoare;
}
}
voidDatabase::set(const std::string& cheie, const std::string& valoare) {
std::lock_guard<std::mutex> lock(dbMutex);
db[cheie] = valoare;
salveazaInFisier();
}
std::string Database::get(const std::string& cheie) {
std::lock_guard<std::mutex> lock(dbMutex);
if (db.count(cheie) > 0){
return db[cheie];
}
return"";
}
boolDatabase::del(const std::string& cheie){
std::lock_guard<std::mutex> lock(dbMutex);
if (db.count(cheie) > 0) {
db.erase(cheie);
salveazaInFisier();
returntrue;
}
returnfalse;
}