basic db save
This commit is contained in:
parent
a397347001
commit
60afa6126f
4 changed files with 25548 additions and 5 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
||||||
a.out
|
a.out
|
||||||
flake.lock
|
flake.lock
|
||||||
|
db.json
|
||||||
/.ccls-cache
|
/.ccls-cache
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@ And also my first serious C++ project.
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
- [X] Basic K/V store
|
- [X] Basic K/V store
|
||||||
- [ ] Remove/add entries
|
- [X] Remove/add entries
|
||||||
- [ ] Serialisation and saving
|
- [ ] Serialisation and saving
|
||||||
- [ ] Networking
|
- [ ] Networking
|
||||||
|
|
|
||||||
24
main.cpp
24
main.cpp
|
|
@ -1,12 +1,24 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include "json.hpp"
|
||||||
|
|
||||||
void add_entry(std::vector<std::vector<std::string>>& database, std::string key, std::string value) {
|
using Db = std::vector<std::vector<std::string>>;
|
||||||
|
|
||||||
|
void save_database(Db& database, std::string file_name) {
|
||||||
|
nlohmann::json json_data = database;
|
||||||
|
std::string serialised_json_data = json_data.dump();
|
||||||
|
std::ofstream db_file(file_name);
|
||||||
|
db_file << serialised_json_data;
|
||||||
|
db_file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_entry(Db& database, std::string key, std::string value) {
|
||||||
database.push_back({key, value});
|
database.push_back({key, value});
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove_entry(std::vector<std::vector<std::string>>& database, std::string key) {
|
void remove_entry(Db& database, std::string key) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (std::vector<std::string>& entry : database) {
|
for (std::vector<std::string>& entry : database) {
|
||||||
index++;
|
index++;
|
||||||
|
|
@ -16,7 +28,7 @@ void remove_entry(std::vector<std::vector<std::string>>& database, std::string k
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void prompt_user(std::vector<std::vector<std::string>>& database) {
|
void prompt_user(Db& database) {
|
||||||
std::string mode;
|
std::string mode;
|
||||||
std::cin >> mode;
|
std::cin >> mode;
|
||||||
if (mode == "add") {
|
if (mode == "add") {
|
||||||
|
|
@ -29,13 +41,17 @@ void prompt_user(std::vector<std::vector<std::string>>& database) {
|
||||||
std::string key;
|
std::string key;
|
||||||
std::cin >> key;
|
std::cin >> key;
|
||||||
remove_entry(database, key);
|
remove_entry(database, key);
|
||||||
|
} else if (mode == "save") {
|
||||||
|
std::string file;
|
||||||
|
std::cin >> file;
|
||||||
|
save_database(database, file);
|
||||||
} else {
|
} else {
|
||||||
prompt_user(database);
|
prompt_user(database);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::vector<std::vector<std::string>> database;
|
Db database;
|
||||||
|
|
||||||
database = {
|
database = {
|
||||||
{ "name", "username" },
|
{ "name", "username" },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue