forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileRepository.cpp
117 lines (97 loc) · 3.08 KB
/
FileRepository.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
//
// Created by Admin on 5/25/2023.
//
#include "FileRepository.h"
bool FileRepository::writeJsonArr(const QString &file_name_, const QJsonArray &data_, const bool clear_) {
QFile file(file_name_);
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
PLOGE << "File cannot be opened";
return false;
}
const QByteArray content = file.readAll();
file.resize(0); // Clear the file content
QJsonDocument doc = QJsonDocument::fromJson(content);
QJsonArray data_json;
if (!doc.isNull() && doc.isArray() && !clear_) {
data_json = doc.array();
}
for (const auto &data: data_) {
data_json.append(data);
}
doc.setArray(data_json);
file.write(doc.toJson());
file.close();
return true;
}
bool FileRepository::readJson(const QString &file_path_, QJsonObject &json_object_) {
QFile file(file_path_);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
PLOGE << "Failed to open file for reading:" << file.errorString();
return false;
}
const QByteArray json_data = file.readAll();
file.close();
QJsonParseError parse_error;
const QJsonDocument json_doc = QJsonDocument::fromJson(json_data, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
PLOGE << "Failed to parse JSON:" << parse_error.errorString();
return false;
}
json_object_ = json_doc.object();
return true;
}
bool FileRepository::readJsonArr(const QString &file_path_, QJsonArray &json_object_) {
QFile file(file_path_);
if (!file.exists()) {
PLOGE<< "File not found";
return false;
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
PLOGE << "Failed to open file for reading:" << file.errorString();
return false;
}
const QByteArray json_data = file.readAll();
file.close();
QJsonParseError parse_error;
const QJsonDocument json_doc = QJsonDocument::fromJson(json_data, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
PLOGE << "Failed to parse JSON:" << parse_error.errorString();
return false;
}
json_object_ = json_doc.array();
return true;
}
bool FileRepository::saveToBinFile(const QByteArray& data_, const QString& file_path_) {
QFile file(file_path_ + ".bin");
if (QFile::exists(file_path_)){
PLOGW << "Already existing id";
return false;
}
if (!file.open(QIODevice::WriteOnly)) {
PLOGE << "Failed to create file";
return false;
}
QDataStream out(&file);
out << data_;
file.close();
PLOGI << "File " << file_path_ << " was written";
return true;
}
QByteArray FileRepository::readFromBinFile(const QString& file_path_) {
QFile file(file_path_ + ".bin");
if (!file.open(QIODevice::ReadOnly)) {
PLOGE << "Failed to open file";
return QByteArray();
}
QDataStream in(&file);
QByteArray data;
in >> data;
file.close();
PLOGI << "File " << file_path_ << "was read";
return data;
}
FileRepository::FileRepository() = default;