-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextParser.cpp
114 lines (81 loc) · 1.75 KB
/
TextParser.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
/*
* TextParser.cpp
*
* Created on: Jun 20, 2017
* Author: Brayan
*/
#include "TextParser.h"
using namespace std;
void log(string a) {
cout << a;
}
bool File::fileExists(const std::string& filename) {
struct stat buf;
if (stat(filename.c_str(), &buf) != -1) {
return true;
}
return false;
}
void Parser::config(std::fstream& inputDoc, std::string fieldName) {
string word;
if (inputDoc.is_open()) {
string word;
int fieldNum = 0;
while (getline(inputDoc, word)) {
if (word.find("#Fields:") == 0) {
int fieldPos = word.find(fieldName);
for (int i = 0; i <= fieldPos; i++) {
if (word[i] == ' ') {
fieldNum++;
}
}
setFieldNum(fieldNum);
break;
}
}
} else {
cout << "inputDoc is not opened. ";
}
}
string Parser::readColumn(std::string filename, std::string fieldName) {
File d;
if (d.fileExists(filename)) {
fstream inputDoc;
char* fileName = (char*) filename.c_str();
printf("Opening File. \n");
inputDoc.open(fileName);
config(inputDoc, fieldName);
int FieldNum = getFieldNum();
if(FieldNum == 0)
{
cout << "Field Not Found." << endl;
return "0";
}
string Line;
while (getline(inputDoc, Line)) {
if (FieldNum == 1) {
int test2 = Line.find(" ");
cout << Line.substr(0, test2) << endl;
}
else {
int currentField = 1;
for (int i = 0; i < Line.length(); i++) {
if (Line[i] == ' ') {
currentField++;
if (currentField == FieldNum) {
string test1 = Line.substr(i + 1);
int test2 = test1.find(" ");
cout << Line.substr(i + 1, test2) << endl;
i = Line.length();
}
}
}
}
}
inputDoc.close();
}
else {
cout << "File \"" << filename << "\" does not exist";
return "0";
}
}