-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputTable.cpp
More file actions
133 lines (103 loc) · 3.3 KB
/
Copy pathInputTable.cpp
File metadata and controls
133 lines (103 loc) · 3.3 KB
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
/*
Copyright 2014 Wouter Spekkink
Authors : Wouter Spekkink <wouter.spekkink@gmail.com>
Website : http://www.wouterspekkink.org
This file is part of Dynamic Networks.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2014 Wouter Spekkink. All rights reserved.
Dynamic Networks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dynamic Networks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "InputTable.h"
InputTable::InputTable(QWidget *parent)
: nrow(0), ncol(0)
{
}
void InputTable::readData(const QString &fileName, const QString &sep)
{
const std::string file = fileName.toStdString();
const std::string delimiter = sep.toStdString();
// Let's make suer that the data vector is emptied, before making a new one
rowData.clear();
header.clear();
rowNames.clear();
nrow = 0;
ncol = 0;
std::istringstream convert(delimiter.c_str());
char del;
convert >> del;
ReadFile(file, del);
}
void InputTable::ReadFile(const std::string &inputFile, const char &del)
{
std::vector <std::vector <std::string> > dataVector;
std::ifstream myFile (inputFile.c_str());
while(myFile) {
std::string buffer;
if(!getline(myFile, buffer)) break;
std::istringstream stringStream(buffer);
std::vector <std::string> record;
while (stringStream) {
std::string s;
if(!getline(stringStream, s, del)) break;
record.push_back(s);
}
dataVector.push_back(record);
}
std::vector<std::vector <std::string> >::iterator it;
for(it = dataVector.begin(); it != dataVector.end(); it++) {
if(it == dataVector.begin()) {// The first line is always the header
std::vector<std::string> tempVector = *it;
std::vector<std::string>::iterator it2;
for(it2 = tempVector.begin() + 1; it2 != tempVector.end(); it2++)
header.push_back(*it2);
ncol = header.size(); // - 1 for empty space;
} else {
std::vector<std::string> tempVector = *it;
rowNames.push_back(tempVector[0]); // This makes the vector of row names
std::vector <std::string>::iterator it2;
std::vector <short> numbers;
for(it2 = tempVector.begin() + 1; it2 != tempVector.end(); it2++) {
short tempNumber = 0;
std::istringstream convert(*it2);
convert >> tempNumber;
numbers.push_back(tempNumber);
}
rowData.push_back(numbers);
}
}
nrow = rowNames.size();
emit importFinished();
}
int InputTable::GetRows()
{
return nrow;
}
int InputTable::GetCols()
{
return ncol;
}
const std::vector<std::string> InputTable::GetHeader()
{
return header;
}
const std::vector<std::string> InputTable::GetRowNames()
{
return rowNames;
}
const std::vector<std::vector <short> > InputTable::GetRowData()
{
return rowData;
}