-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileRW.cpp
180 lines (148 loc) · 4.23 KB
/
FileRW.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "FileRW.h"
#include "boost/algorithm/string.hpp"
FileRW::FileRW()
{
}
FileRW::~FileRW()
{
}
//used to check if data loffer file has a header or not
bool FileRW::CheckHeader(std::string dir) {
std::ifstream file;
file.open(dir);
std::string line;
bool atLeastOneLine = false;
if (file.is_open()) {
while (std::getline(file, line)) { //while there is a line available
atLeastOneLine = true;
break;
}
}
return atLeastOneLine;
}
void FileRW::ArchiveLogFile(std::string fileName) {
//get time
time_t timeNow = time(0);
tm *ltm = localtime(&timeNow);
std::vector<std::string> dirs = split(fileName);
std::string name = dirs[dirs.size() - 1];
std::string fullDir;
for (uint8_t i = 0; i < dirs.size() - 1; i++) {
if (dirs[i] == name)
break;
if (dirs[i] != "")
fullDir += '/' + dirs[i];
}
fullDir += "/archive"; //add archive folder to dir
//printf("%s\n", fullDir.c_str());
//Check if the archive folder exists
struct stat info;
boost::filesystem::path p{ fullDir };
if (stat(fullDir.c_str(), &info) != 0)
boost::filesystem::create_directory(p);
//append the current time to the log file name
char t[80];
strftime(t, 80, "%Y_%m_%d__%H:%M:%S", ltm);
std::string time(t);
std::string newLogFileName = (fullDir + "/" + time + "__" + name).c_str();
//printf("%s\n", newLogFileName.c_str());
std::rename(fileName.c_str(), newLogFileName.c_str()); //rename to archive folder
}
bool FileRW::append(std::string str, std::string dir) {
std::ofstream file;
file.open(dir, std::ios_base::app);
if (file.is_open()) {
file << str; //copy str into new file
file.close(); //close new file
return true;
}
else {
//printf("Failed to open file!");
return false;
}
}
std::vector<std::string> FileRW::split(const std::string s) {
std::vector<std::string> results;
boost::split(results, s, [](char c) {return c == '/'; });
return results;
}
/*
void FileRW::readLine(std::string fileName, Data &d) {
//open file in read mode
std::ifstream file;
std::string line;
file.open(fileName);
//read in a line
std::vector<std::string> l;
if (file.is_open()) {
while (std::getline(file, line)) { //while there is a line available (but we'll break after reading first line)
l.push_back(line);
//for (size_t i = 0; i < l.size(); i++)
// d.cMessage[i] = l[i];
//break;
}
}
//close file
file.close();
}
*/
void FileRW::rmLine(std::string fileName) {
return;
}
bool FileRW::GetDownlinkMessage(std::string fileName, std::string &d, char dMess[]) {
//open files
std::ifstream inFile; //read
std::ofstream tempFile; //write
//get dir and filename
char* ts1 = strdup(fileName.c_str());
char* dir = dirname(ts1);
//char* filename = basename(ts2);
//_logger.log("dir: %s, %s\n", dir, filename);
std::string tempFileName = "/temp.txt";
//open files
inFile.open(fileName);
//read in a line
//std::vector<std::string> l;
std::string linesForNewFile;
std::string line;
bool available = false;
int lineCount = 0; //only used for first line
if (inFile.is_open()) {
while (std::getline(inFile, line)) { //while there is a line available
//_logger.log("line: %s\n", line.c_str());
available = true; //at least one line is available
if (lineCount < 1) { //get the first line and copy into cMessage
//l.push_back(line);
lineCount++;
d = line;
for (size_t i = 0; i < line.size(); i++) {
//_logger.log("l: %s\n", l[i].c_str());
dMess[i] = line[i];
if (line[i] == Definitions::BP)
break;
//_logger.log("cm: %c\n", d.cMessage[i]);
}
}
else { //add all but first line to the string
linesForNewFile += line + "\n";
}
}
linesForNewFile.erase(linesForNewFile.end() - 1); //erase last char ******** do we need this??
//_logger.log("for temp: %s\n", linesForNewFile.c_str());
}
inFile.close(); //close inFile
//create temp file
std::string dd = dir + tempFileName;
tempFile.open(dd);
//_logger.log("dir: %s\n", (dir + tempFileName).c_str());
if (tempFile.is_open()) {
//_logger.log("Writing new lines...\n");
tempFile << linesForNewFile; //copy str into new file
tempFile.close(); //close new file
}
else {
return false;
}
std::rename(dd.c_str(), fileName.c_str()); //rename new file to old files name (erases old file)
return available;
}