-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathftokenizer.h
More file actions
174 lines (123 loc) · 3.32 KB
/
ftokenizer.h
File metadata and controls
174 lines (123 loc) · 3.32 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
#ifndef FTOKENIZER
#define FTOKENIZER
#include "Stokenizer.h"
#include <fstream>
class ftokenize{
public:
const int MAX=10;
ftokenize(string _file);
//postcondition: number of paragraph is set to 1 and the number
//of file is set to 0. The array is copy to the private member _f
ftokenize (string _file[]);
//precondition: the last element of the array must be string()
//Ex. array[]={file1.txt,file2,string()}
//postcondition: number of paragraph is set to 1 and the number
//of file is set to 0. The array is copy to the private member _f
//Accessor
int getNumPara();
//postcondition: return number of paragraph
int getNumFile();
//postcondition: return number of file in the array
bool more();
//postcondition: return true if exist more token in the file
Token nextToken();
//Postcondition: return the next token
friend Token & operator>>(ftokenize& s,Token &t);
//Postcondition: Return the next token
int getTotalFiles();
private:
bool getNewParagraph();//get the new paragraph
bool getNewFile();//get the new file
string* _f=new string[MAX];//array of file
ifstream file;
STokenizer _stk;
int total_files;
int num_file;
int num_paragraph;
void init();//Initialize each element of _f to string()
};
ftokenize::ftokenize(string _file):num_file(0),num_paragraph(0){
init();
_f[0]=_file;
getNewFile();
getNewParagraph();
total_files = 1;
}
void ftokenize::init(){
for(int i=0;i<MAX;i++)
_f[i]=string();
}
ftokenize::ftokenize (string _file[]):num_file(0),num_paragraph(0){
int i;
init();
total_files = TOTAL_FILES;
for (i=0;/*_file[i]!=string()*/ i < total_files;++i){
_f[i]=_file[i];
}
getNewFile();
getNewParagraph();
}
//Accessor
int ftokenize::getNumPara(){
return num_paragraph;
}
int ftokenize::getNumFile(){
return num_file;
}
int ftokenize::getTotalFiles(){
return total_files;
}
bool ftokenize::more(){
if(_f[num_file]!=string())
return true;
return false;
}
Token ftokenize::nextToken(){
Token t;
bool New=true;
if (!_stk.more()){
if (!getNewParagraph()){
file.close();
num_file++;
New=getNewFile();
getNewParagraph();
}
}
if (New && _stk.more()){
_stk>>t;
return t;
}
}
bool ftokenize::getNewParagraph(){
string text,line;
if (!file.eof()){
do{
getline(file,line);
int len=line.length();
if(len>0)
line.insert(len," ");
text+= line;
} while(line != ""&& !file.eof());
_stk=text;
num_paragraph++;
return true;
}
return false;
}
bool ftokenize::getNewFile(){
if (/*_f[num_file]!=string()*/num_file < total_files){
file.open(_f[num_file]);
if(file.fail()){
cout<<"File Opening failed\n";
exit(0);
}
num_paragraph=0;
return true;
}
return false;
}
Token & operator>>(ftokenize& s,Token &t){
t=s.nextToken();
return t;
}
#endif // FTOKENIZER