-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOp.cpp
168 lines (126 loc) · 3.26 KB
/
FileOp.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
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
#include "FileOp.h"
#include <string.h>
#include "Factory.h"
FileOp::FileOp(string name)
{
_fname = name;
memset(_buffer,0,sizeof(_buffer));
}
bool FileOp::FileOpen()
{
_ff.open(_fname, std::ios::in|std::ios::out);
return _ff.is_open();
}
string FileOp::GetDataType()
{
/*assert(_ff!= NULL)*/
char buf[128] = {0};
string out;
if(_ff.getline(buf, sizeof(buf)))
out = buf;
return out;
}
bool FileOp::ReadLine(string& str)
{
if(_ff.getline(_buffer,sizeof(_buffer)))
{
str = _buffer;
return true;
}
return false;
}
void FileOp::Write2File(SAVE_TYPE type,const string name,const char* context)
{
switch(type)
{
case SAVE_TYPE::TYPE_TXT:
break;
case SAVE_TYPE::TYPE_CSV:
break;
/*...*/
default:
break;
}
}
/*------------------------------------------------------------------------------------------*/
/*BELOEW AS CLIENT CODE*/
void Load_Default_Adapter(AdFactory& adp)
{
function<string(string)> fun0 = [](string s){return s;}; /*sring TO string*/
SET_FUNCTER1(adp,string,string,fun0);
function<int(string)> fun1 = [](string s){return stoi(s);}; /*sring TO Int*/
SET_FUNCTER1(adp,int,string,fun1);
function<double(string)> fun2 = [](string s){return stod(s);}; /*string TO Double*/
SET_FUNCTER1(adp,double,string,fun2);
function<int(int,int)> fun3 = [](int n1,int n2){return n1-n2;}; /*COMPARE 'Int' with 'Int' */
SET_FUNCTER2(adp,int,int,int,fun3);
/*MORE .....*/
}
int main(int argc,char* argv[])
{
if(argc < 3)
{
cout << "Pls input the source file!" << endl;
return 0;
}
AdFactory adp;
Load_Default_Adapter(adp); /* LOAD DEAULT ADAPTERS */
//FileOp txtFile("InputA1.txt");
FileOp txtFile1(argv[1]);
FileOp txtFile2(argv[2]);
if(!txtFile1.FileOpen() || !txtFile2.FileOpen())
return 0;
string sType1 = txtFile1.GetDataType();
string sType2 = txtFile2.GetDataType();
string strLine;
if(sType1.find("Int_Day") != string::npos && sType2.find("Int_Day") != string::npos)
{
cout << "INT" << endl;
string str1,str2;
auto Fun1 = GET_FUNCTER1(adp,int,string);
auto Fun2 = GET_FUNCTER2(adp,int,int,int);
while(txtFile1.ReadLine(str1) && txtFile2.ReadLine(str2))
{
try{
cout << Fun2(Fun1(str1),Fun1(str2)) << endl;
}
catch(const std::invalid_argument& ia) {
cout<< "Invalid argument!" << endl;
}
}
}
else if(sType1.find("Str_Word") != string::npos)
{
cout << "STR " << endl;
string str;
auto Fun1 = GET_FUNCTER1(adp,string,string);
while(txtFile1.ReadLine(str))
{
cout << Fun1(str) << endl;
}
}
else if(sType1.find("Double") != string::npos)
cout << "DOUBLE" << endl;
/*MORE WORKS BY USER DEFINE.... */
return 0;
}
/*TEST FOR DIFFERENT ADAPTER*/
void test()
{
AdFactory con;
function<int(string)> fun1 = [](string s){return stoi(s);};
SET_FUNCTER1(con,int,string,fun1);
function<double(string)> fun2 = [](string s){return stod(s);};
SET_FUNCTER1(con,double,string,fun2);
function<int(int,int)> fun3 = [](int n1,int n2){return n1-n2;};
SET_FUNCTER2(con,int,int,int,fun3);
auto Fun1 = GET_FUNCTER1(con,int,string);
auto nn = Fun1("9999");
cout << nn << endl;
auto Fun2 = GET_FUNCTER1(con,double,string);
auto dd = Fun2("3.1415927");
cout << dd << endl;
auto Fun3 = GET_FUNCTER2(con,int,int,int);
auto ee = Fun3(100,20);
cout << ee << endl;
}