-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDFReader.cpp
136 lines (112 loc) · 4.12 KB
/
GDFReader.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
#include "GDFReader.h"
GDFReader::GDFReader(){}
GDFReader::GDFReader(string inputfilepath,string outputfilepath){
this->setinputFilePath(inputfilepath);
this->setoutputFilePath(outputfilepath);
}
GDFReader::~GDFReader(){}
inline string GDFReader::trim(string s){
string str=s;
str.erase(0, str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ')+1);
return str;
}
//The input file can contain gaps and empty lines and also most of the other corner cases are handled by this code.
void GDFReader::ModifyFile(){
try{
int pos = this->getinputFilePath().find(".");
string t ;
t = this->getinputFilePath().substr(pos + 1, this->getinputFilePath().size());
if(t!="gdf"){
throw t;
}
fstream file;
file.open(this->getinputFilePath());
if(!file.is_open()){
throw 404;
}
Graph G;
stringstream ss;
string token;
int namecolumn=0,node1column=0,node2column=0,weighcolumn=0,directedcolumn=0,currColumn=0;
string line;getline(file,line);
while(line==""){
getline(file,line);
}
//Reading Nodedef
if((trim(line).substr(0,8))==NODEDEF){
ss<<trim(line).substr(8,line.length()-8);
currColumn=0;
while(getline(ss,token,',')){
if(token.find("name")!=string::npos)namecolumn=currColumn;
currColumn++;
}
ss.clear();
while(getline(file,line) and trim(line).substr(0,8)!=EDGEDEF){
ss<<line;
currColumn=0;
while(getline(ss,token,',')){
if(currColumn==namecolumn){
G.AddNode(trim(token));
}
currColumn++;
}
ss.clear();
}
G.Build();
}
//Reading Edgedef
if(trim(line).substr(0,8)==EDGEDEF){
ss<<trim(line).substr(8,line.length()-8);
currColumn=0;
while(getline(ss,token,',')){
if(token.find("node1")!=string::npos)node1column=currColumn;
if(token.find("node2")!=string::npos)node2column=currColumn;
if(token.find("weight")!=string::npos)weighcolumn=currColumn;
if(token.find("directed")!=string::npos)directedcolumn=currColumn;
currColumn++;
}
ss.clear();
while(getline(file,line)){
ss<<line;
currColumn=0;
string node1,node2;double weight;bool directed;
while(getline(ss,token,',')){
if(currColumn==node1column){
node1=trim(token);
}
else if(currColumn==node2column){
node2=trim(token);
}
else if(currColumn==weighcolumn){
weight=stod(trim(token));
}
else if(currColumn==directedcolumn){
directed=(trim(token)=="true");
}
currColumn++;
}
if(currColumn!=4)throw 504;
G.AddEdge(node1,node2,weight);
if(!directed){
G.AddEdge(node2,node1,weight);
}
ss.clear();
}
}
G.GetCSVRepresentation(this->getoutputFilePath(),";");
file.close();
cout<<"\u001B[32m"<<"Generated the .csv successfully!"<<"\u001B[0m"<<endl;
}
catch(string t){
cerr<<"\u001B[31m"<<"Error : "<<"\u001B[0m"<<"Input file extension should be .gdf : ."<<t<<" Provided"<<endl;
exit(0);
}
catch(int i){
if(i==404)
cerr<<"\u001B[31m"<<"Error : "<<"\u001B[0m"<<"Couldn't find the file at the specified input path"<<endl;
if(i==504)
cerr<<"\u001B[31m"<<"Error : "<<"\u001B[0m"<<"Found some ambiguity in the input file"<<endl;
exit(0);
}
}