-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.cpp
146 lines (136 loc) · 4.69 KB
/
file_utils.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
#include "file_utils.h"
#include "qt_utils.h"
#include <QtCore/QTextStream>
#include <QObject>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <QIODevice>
#include <QDebug>
#include <QString>
#include <istream>
#include <fstream>
#include <windows.h>
void file_utils::constructCsvFileVector(std::string filePath, std::vector<std::vector<std::string>>& lineData, std::vector<std::string>& headers)
{
// Constructs a multi-vector out of a csv file
// std::ifstream file(filePath); // declare file stream
std::ifstream file(filePath.c_str()); // declare file stream
std::string line;
int numOfColumns = 0;
int numOfLines= 0;
// Using safeGetLine function to handle LF, CR, and CRLF
while(!safeGetline(file, line).eof())
{
numOfColumns = 0;
++numOfLines;
const char *mystart = line.c_str();
bool instring{ false };
for (const char* p = mystart; *p; ++p) // Iterate through each character
{
if (*p == '"') // toggle flag if we're btw double quote
{
instring = !instring;
}
// We need to check for an empty string and a comma. The empty string is the last value in our mystart variable
else if ((*p == ',' && !instring))
{
++numOfColumns;
if (numOfLines == 1)
{
// On each field delimiter "," we add a new column indicating a new field
// Only needs doing once hence if line == 1
lineData.resize(numOfColumns);
headers.push_back(std::string(mystart, p - mystart));
}
else
{
lineData[numOfColumns - 1].push_back(std::string(mystart, p - mystart)); // If comma is outside double quote keep the field
}
mystart = p + 1;
}
}
// End of the mystart char array delimited by end of line instead of , so we need to account for that
++numOfColumns;
if(numOfLines == 1)
{
lineData.resize(numOfColumns);
headers.push_back(std::string(mystart));
}
else
{
lineData[numOfColumns - 1].push_back(std::string(mystart));
}
}
}
std::string file_utils::openFile()
{
QString fileName = QFileDialog::getOpenFileName(NULL, QObject::tr("Open CSV file"),
"C:/Users/Ben/Desktop/sample_csv_data",
QObject::tr("text files (*.csv *.txt)"));
if (fileName.isEmpty())
{
return "";
}
else
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(NULL, QObject::tr("Unable to open file"), file.errorString());
//const char* errorMessage = "Error biatch";
//qt_utils::createInfoBox(QString::fromStdString("Unable to open file"), file.errorString());
//qt_utils::createInfoBox(errorMessage, file.errorString());
return "";
}
return fileName.toStdString();
}
}
void file_utils::saveFile(QString& inData)
{
QString fileName = QFileDialog::getSaveFileName(NULL, QObject::tr("Save File"), "", QObject::tr("CSV data (*.csv)"));
QFile newFile( fileName );
if (fileName.isEmpty())
{
return;
}
else if(newFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QTextStream out(&newFile);
out << inData;
newFile.close();
qDebug() << "Closed and saved file";
}
}
std::istream& file_utils::safeGetline(std::istream& is, std::string& t)
{
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf* sb = is.rdbuf();
for(;;) // ;; Is the same as while(true)
{
int c = sb->sbumpc();
switch (c)
{
case '\n':
return is;
case '\r':
if(sb->sgetc() == '\n')
sb->sbumpc();
return is;
case EOF:
// Also handle the case when the last line has no line ending
if(t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}