-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.h
More file actions
executable file
·186 lines (172 loc) · 4.68 KB
/
Copy pathFile.h
File metadata and controls
executable file
·186 lines (172 loc) · 4.68 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
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
181
182
183
184
185
186
#ifndef FILE_H
#define FILE_H
#include "arff_parser.h"
#include <exception>
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
//double distanceEuclidean(Point &A, Point &B);
std::string GetFileExtension(const std::string& FileName)
{
if(FileName.find_last_of(".") != std::string::npos)
return FileName.substr(FileName.find_last_of(".")+1);
return "";
}
/*
bool compareVertices(Edge<Point,double>p, Edge<Point,double>q)
{
return p.getData()>q.getData();
}
*/
class File
{
public:
std::string filename;
ios_base::openmode filemode;
fstream f;
File()
{
/*filename = NULL;
filemode = NULL;
f = NULL;*/
filemode = std::fstream::in | std::fstream::out;
}
File(std::string name)
{
filename = name;
this->Open(filename);
}
File(std::string name, ios_base::openmode mode)
{
filename = name;
filemode = mode;
}
void Open(std::string name)
{
filename = name;
filemode = std::fstream::in | std::fstream::out;
try
{
//this.f = new f(filename, filemode);
f.open(filename, filemode);
}
catch(exception &e)
{
std::cout << std::string(e.what()) << std::endl;
}
}
void Open(std::string name, ios_base::openmode mode)
{
filename = name;
filemode = mode;
try
{
//this.f = new f(filename, filemode);
f.open(filename, filemode);
}
catch(exception &e)
{
std::cout << std::string(e.what()) << std::endl;
}
}
//Function for base case of variadic function
template <typename T> void Write(T t)
{
f << t;
}
//recurcise function defiition for template type
template <typename T, typename... Args> void Write(T t, Args... args)
{
f << t;
Write(args...) ;
}
template <typename T> void Read(T *t)
{
f >> *t;
}
template <typename T, typename... Args> void Read(T *t, Args... args)
{
f >> *t;
Read(args...) ;
}
void Close()
{
f.close();
}
List<Point> convertPointFormat(ArffData* aData)
{
//std::cout << "number of attributes " << aData->num_attributes()<<endl;
//std::cout << "number of instances " << aData->num_instances()<<endl;
int i, j;
int32 val;
double f;
const char *ccc;
std::string str;
List<Point> pData;
long instanceCount = aData->num_instances();
long attribCount = aData->num_attributes();
for(i = 0; i < instanceCount; i++)
{
Point p;
for(j = 0; j < attribCount; j++)
{
switch(aData->get_instance(i)->get(j)->type())
{
case (INTEGER):
val = *(aData->get_instance(i)->get(j));
p.InsertInteger(val);
break;
case (FLOAT):
f = *(aData->get_instance(i)->get(j));
p.InsertFloat(f);
break;
case (NUMERIC):
f = *(aData->get_instance(i)->get(j));
p.InsertFloat(f);
break;
case (STRING):
str= (std::string)*(aData->get_instance(i)->get(j));
p.InsertString(str.c_str());
break;
case (NOMINAL):
str= (std::string)*(aData->get_instance(i)->get(j));
p.InsertString(str.c_str());
break;
case (DATE):
case (UNKNOWN_VAL):
default:
std::cerr << "Value of this type not supported. ";
break;
}
}
pData.AddEle(p);
// std::cout << endl;
}
return pData;
}
void readARFFDataset(string name, List<Point>* datalist)
{
ArffParser parser(name);
parser.parse(datalist);
}
void ReadDataset(std::string name, List<Point>* datalist)
{
filename = name;
filemode = std::fstream::in;
try
{
string extn = GetFileExtension(filename);
if(extn.compare("ARFF") == 0 || extn.compare("arff") == 0)
return (readARFFDataset(filename, datalist));
else
throw std::runtime_error("\nOnly ARFF File Format Supported for Dataset\n");
}
catch(exception &e)
{
std::cout << std::string(e.what()) << std::endl;
std::exit(EXIT_FAILURE);
}
}
};
#endif