-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileIO.cpp
151 lines (124 loc) · 3.3 KB
/
fileIO.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
#include "fileIO.h"
FileIOHandler::FileIOHandler()
{
return;
}
void FileIOHandler::init()
{
// Serial.println("Initializing SD card...");
if(!SD.begin())
{
// Serial.println("SD card initialisation failed!");
while(1);
}
// Serial.println("Initialisation done.");
}
void FileIOHandler::closeFile()
{
if(myFile)
{
myFile.close();
}
}
char* FileIOHandler::readFile(const char* fileName) // this memory needs to be freed after use
{
std::string newString = "";
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
this->fileName = fileName;
if(SD.exists(this->fileName.c_str()))
{
myFile = SD.open(this->fileName.c_str());
while(myFile.available())
{
newString.push_back(myFile.read());
}
myFile.close();
}
}
// Allocate memory for the C-string
char* cString = (char*)malloc(newString.length() + 1); // +1 for the null terminator
if (cString == nullptr) {
// Allocation failed
return nullptr;
}
// Copy the contents of the std::string to the newly allocated C-string
strcpy(cString, newString.c_str());
// Return the dynamically allocated copy
return cString;
}
void FileIOHandler::writeFile(const char* fileName, const char* content) // this will not overwrite the file, only append to it
{
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
this->fileName = fileName;
myFile = SD.open(fileName, FILE_WRITE);
if(myFile)
{
myFile.println(content);
myFile.close();
}
}
}
JsonDocument FileIOHandler::readJson(const char* fileName)
{
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
this->fileName = fileName;
char* jsonMSG = readFile(fileName);
// Deserialize the JSON
DeserializationError error = deserializeJson(jsonObject, jsonMSG);
// Test if parsing succeeds
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
JsonDocument tempJson;
deserializeJson(tempJson, "{}");
return tempJson;
}
}
else
{
JsonDocument tempJson;
deserializeJson(tempJson, "{}");
return tempJson;
}
return jsonObject;
}
const char* FileIOHandler::getFileName()
{
return this->fileName.c_str();
}
void FileIOHandler::deleteFile(const char* fileName)
{
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
this->fileName = fileName;
if(SD.exists(fileName))
{
SD.remove(fileName);
}
}
}
void FileIOHandler::cleanFile(const char* fileName) // delete the old one and create a new one with the same name
{
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
this->fileName = fileName;
if(SD.exists(fileName))
{
SD.remove(fileName);
myFile = SD.open(fileName, FILE_WRITE);
myFile.close();
}
}
}
bool FileIOHandler::exists(const char* fileName)
{
if((fileName != nullptr)&&(fileName[0] != '\0'))
{
return SD.exists(fileName);
}
return false;
}