forked from KatrinaArnold/SigRecNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
165 lines (141 loc) · 3.65 KB
/
Database.cpp
File metadata and controls
165 lines (141 loc) · 3.65 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
#include "Database.h"
Database::Database()
{
}
Database::Database(string filePath)
{
load(filePath);
}
/*
* Get a list of files in a directory
* adapted from code written by jtshaw (http://www.linuxquestions.org)
*/
bool Database::getFileList(string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
string filename, extension;
int startpos, len;
if((dp = opendir(dir.c_str())) == NULL) {
cerr << "Error(" << errno << ") opening " << dir << endl;
return false;
}
while ((dirp = readdir(dp)) != NULL) {
filename = string(dirp->d_name); // convert from c_str to c++ string
startpos = filename.find_last_of('.');
startpos++; // skip the .
len = filename.size() - startpos;
extension = "";
extension.append(filename, startpos, len); // get extension
if (extension == "BMP" || extension == "bmp")
files.push_back(filename);
}
closedir(dp);
return true;
}
/*
* Extract name from filename
* The name is everything between the last / and -
* TODO: make this windows-compatible
*/
string Database::filenameToName(string filename)
{
string name = "";
int startpos, endpos;
startpos = filename.find_last_of('/');
startpos++; // skip the /
endpos = filename.find('-', startpos);
// get everything between the last / and -
name.append(filename, startpos, endpos-startpos);
return name;
}
/*
* Name Table lookup function - returns the position of name in the table
* adds name to the table if it isn't found
*/
int Database::tableLookup(string name)
{
unsigned int ii;
bool found = false;
for(ii = 0; ii < table.size(); ii++)
if (table[ii] == name) {
found = true;
break;
}
// if it isn't found, add it to the table
if (!found)
table.push_back(name);
return ii;
}
/*
* Name Table lookup function - returns name in the position
*/
string Database::tableLookup(unsigned int index)
{
return table[index];
}
/*
* Table size - returns the number of entries in the table
*/
int Database::tableSize()
{
return table.size();
}
/*
* Load Train Data (Signatures and respective names) from a directory
* Returns the number of signatures read
* TODO: make this windows compatible
*/
TrainData *Database::loadTrainDataFromDir(string dir, int *n)
{
Sig sig;
vector<string> files = vector<string>();
string fname, temp;
char buf[1024];
unsigned int ii;
TrainData *data;
getFileList(dir, files);
data = new TrainData [files.size()];
getcwd(buf, 1024); // save current dir
// cout << "Changing CWD to " << dir << "." << endl;
chdir(dir.c_str()); // change current dir to data dir
for (ii = 0; ii < files.size(); ii++) {
fname = files[ii];
// cout << "Reading " << fname << "." << endl;
sig.load(fname);
data[ii].name = filenameToName(files[ii]); // Get Name from sig file
data[ii].sigArr = sig.toArray(); // convert sig to array
data[ii].value = (double) tableLookup(data[ii].name); /* get value
* (table index)
*/
}
// cout << "Back to previous CWD." << endl;
chdir(buf); // change back to original working dir
// cout << "Read a total of " << ii << " files." << endl;
*n = ii;
return data;
}
bool Database::save(string filePath)
{
ofstream file(filePath.c_str());
unsigned int ii;
for(ii = 0; ii < table.size(); ii++)
file << table[ii] << endl;
file.close();
return true;
}
bool Database::load(string filePath)
{
ifstream file(filePath.c_str());
string temp;
table.clear();
while (!file.eof()) {
getline (file, temp);
table.push_back(temp);
}
file.close();
return true;
}
Database::~Database()
{
}