-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsedFile.cpp
61 lines (50 loc) · 1.39 KB
/
ParsedFile.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
/* Christian A. Rodriguez
CCOM 3034 Data Structures
ParsedFile class implementation for final project
*/
#include "ParsedFile.h"
vector<string> ParsedFile::readAndTokenize() {
string line;
ifstream myfile (name.c_str());
vector<string> result;
if (myfile.is_open()) {
while ( myfile.good() ) {
// for every line in the file, tokenize and store
// to a vector of strings.
getline (myfile,line);
vector<string> tokenized_line = tokenize(line, " ");
for(uint i=0; i<tokenized_line.size(); i++)
result.push_back(tokenized_line[i]);
}
myfile.close();
}
else cout << "readAndTokenize: Unable to open file, returning empty vector";
return result;
}
vector<string> tokenize(const string & str, const string & delim) {
vector<string> tokens;
size_t p0 = 0, p1 = string::npos;
while(p0 != string::npos) {
p1 = str.find_first_of(delim, p0);
if(p1 != p0) {
string token = str.substr(p0, p1 - p0);
tokens.push_back(token);
}
p0 = str.find_first_not_of(delim, p1);
}
return tokens;
}
int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}