Repository navigation
Expand file tree
/
Copy pathConfigFile.cpp
More file actions
311 lines (249 loc) · 7.05 KB
/
Copy pathConfigFile.cpp
File metadata and controls
311 lines (249 loc) · 7.05 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "ConfigFile.h"
#include "Common.h"
#include <assert.h>
#include <string.h>
#include <iostream>
using namespace std;
#define LINE_SIZE 100
ConfigFile::ConfigFile(TCHAR *pFullFile) : m_strConfigFile(pFullFile), CXMLParser(pFullFile), m_pRejectFolder(NULL), m_pRejectFolderType(NULL), m_pAcceptFileType(NULL), m_pRejectFileType(NULL)
{
if(!m_pRootElement)
throw "Error config file!";
MSXML2::IXMLDOMElementPtr pElement;
// target file, name and path
pElement = m_pRootElement->selectSingleNode("TargetFile");
m_strTargetFile = (char*)(_bstr_t)pElement->getAttribute("value");
sepPathFile(m_strConfigFile, m_strConfigPath, m_strConfigName);
if(m_strConfigPath.GetLength() == 0)
{
char buf[1000];
GetCurrentDirectory(1000, buf); //得到当前工作路径
m_strConfigPath = buf;
// cout << m_strConfigPath << endl;
}
sepPathFile(m_strTargetFile, m_strTargetPath, m_strTargetName);
if(m_strTargetPath.GetLength() == 0)
{
m_strTargetPath = m_strConfigPath;
m_strTargetFile = m_strTargetPath + '\\' + m_strTargetName;
}
cout << "Target file path is: " << m_strTargetPath << endl;
cout << "Target file name is: " << m_strTargetName << endl;
// Reject folder type: “., +, @” 等开头的文件夹
m_pRejectFolderType = new UNIT;
CString strTemp;
pElement = m_pRootElement->selectSingleNode("RejectFolder");
if(pElement)
{
m_pRejectFolder = new UNIT;
strTemp = (char*)(_bstr_t)pElement->getAttribute("value");
add2List(m_pRejectFolder, strTemp);
}
pElement = m_pRootElement->selectSingleNode("AcceptFileType");
if(pElement)
{
m_pAcceptFileType = new UNIT;
strTemp = (char*)pElement->Gettext();
add2List(m_pAcceptFileType, strTemp, ".");
}
pElement = m_pRootElement->selectSingleNode("RejectFileType");
if(pElement)
{
m_pRejectFileType = new UNIT;
strTemp = (char*)pElement->Gettext();
//pElement->get_attributes
add2List(m_pRejectFileType, strTemp, ".");
}
m_pAcceptFile = new UNIT;
add2List(m_pAcceptFile, m_pRootElement, "AcceptFile");
m_pAcceptPath = new ACCEPT_PATH;
add2List(m_pAcceptPath, m_pRootElement, "AcceptPath");
}
void deleteLink(UNIT* p)
{
UNIT* h;
while(p)
{
h = p->pNext;
delete p;
p = h;
}
}
void deleteLink(ACCEPT_PATH* p)
{
ACCEPT_PATH* h;
while(p)
{
h = p->pNext;
delete p;
p = h;
}
}
ConfigFile::~ConfigFile()
{
deleteLink(m_pRejectFolder);
deleteLink(m_pRejectFolderType);
deleteLink(m_pAcceptFileType);
deleteLink(m_pRejectFileType);
deleteLink(m_pAcceptFile);
deleteLink(m_pAcceptPath);
}
void ConfigFile::add2RejectFolderList(CString strTokens)
{
add2List(m_pRejectFolder, strTokens);
}
void ConfigFile::add2RejectFolderTypeList(CString strTokens)
{
add2List(m_pRejectFolderType, strTokens);
}
void ConfigFile::add2List(UNIT* pUnit, MSXML2::IXMLDOMElementPtr pParentElement, CString strNodeName)
{
bool bPath = false;
if(strNodeName.Right(4) == "Path")
bPath = true;
MSXML2::IXMLDOMNodeListPtr pNodeList = pParentElement->selectNodes(strNodeName.GetString());
for(int index = 0; index < pNodeList->Getlength(); index++)
{
MSXML2::IXMLDOMElementPtr pElem = (MSXML2::IXMLDOMElementPtr) pNodeList->Getitem(index);
pUnit->strData = (char*)(_bstr_t)pElem->getAttribute("value");
if(bPath) addPathSep(pUnit->strData);
pUnit->pNext = new UNIT;
pUnit = pUnit->pNext;
}
}
void ConfigFile::add2AcceptPathList(int iLen, TCHAR** pPathList)
{
// Mordecai Lee, 2010/09/25, Sat., 11:26:35
ACCEPT_PATH* pPath = m_pAcceptPath;
for(int i = 0; i < iLen; i++)
{
pPath->strPath = pPathList[i];
pPath->pNext = new ACCEPT_PATH;
pPath = pPath->pNext;
}
}
void ConfigFile::add2List(ACCEPT_PATH* pPath, MSXML2::IXMLDOMElementPtr pParentElement, CString strNodeName)
{
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap = NULL;
MSXML2::IXMLDOMNodePtr pAttr;
MSXML2::IXMLDOMNodeListPtr pNodeList = pParentElement->selectNodes(strNodeName.GetString());
CString strName, strValue;
for(int index = 0; index < pNodeList->Getlength(); index++)
{
MSXML2::IXMLDOMElementPtr pElem = (MSXML2::IXMLDOMElementPtr) pNodeList->Getitem(index);
pElem->get_attributes(&pAttrMap);
for(int idx = 0; idx < pAttrMap->Getlength(); idx++) //属性值的循环
{
pAttrMap->get_item(idx,&pAttr);
strName = (char*)(_bstr_t)pAttr->nodeName;
strValue = (char*)(_bstr_t)pAttr->nodeValue;
if(strName == "value")
{
pPath->strPath = strValue;
addPathSep(pPath->strPath);
}else if(strName == "order")
{
add2List(pPath->pOrder, strValue);
}
}
add2List(pPath->pRejectFile, pElem, "RejectFile");
add2List(pPath->pRejectPath, pElem, "RejectPath");
pPath->pNext = new ACCEPT_PATH;
pPath = pPath->pNext;
}
}
void ConfigFile::add2List(UNIT* pUnit, CString strTokens, CString strPre)
{
if(strTokens == "")
return;
if(pUnit->strData == ".*")
return;
CString resToken;
int curPos = 0;
while(pUnit->pNext) pUnit = pUnit->pNext;
if(pUnit->strData != "")
{
pUnit->pNext = new UNIT;
pUnit = pUnit->pNext;
}
resToken = strTokens.Tokenize(", ", curPos);
while (true)
{
pUnit->strData = strPre + resToken;
resToken = strTokens.Tokenize(_T(", "), curPos);
if(resToken == "")
break;
pUnit->pNext = new UNIT;
pUnit = pUnit->pNext;
}
}
/**************************** check *********************************/
bool ConfigFile::checkFolder(UNIT* ptr, const CString strFolder)
{
while(ptr && ptr->strData != "")
{
if( ptr->strData.CompareNoCase(strFolder) == 0 )
return true;
ptr = ptr->pNext;
}
return false;
}
bool ConfigFile::isOneRejectFolder(const CString strFolder)
{
return checkFolder(m_pRejectFolderType, strFolder[0]) || checkFolder(m_pRejectFolder, strFolder);
}
bool ConfigFile::isOneOrderFolder(ACCEPT_PATH* pAcceptPath, const CString strCurRootPath, const CString strFolder)
{
if( pAcceptPath->strPath == strCurRootPath )
return checkFolder(pAcceptPath->pOrder, strFolder);
return false;
}
bool ConfigFile::checkFileType(const CString strFileName, UNIT* ptr)
{
while(ptr && ptr->strData != "")
{
if(checkFileType(strFileName, ptr->strData))
return true;
ptr = ptr->pNext;
}
return false;
}
bool ConfigFile::checkFileType(CString strFileName, CString strFileType)
{
strFileName.Trim();
strFileType.Trim();
if( strFileType == ".*" || strFileType.CompareNoCase(strFileName.Right(strFileType.GetLength())) == 0 )
return true;
return false;
}
bool ConfigFile::checkFile(ACCEPT_PATH* pAcceptPath, const CString strPath, CString strFileName)
{
CString strFullFile = strPath;
addPathSep(strFullFile);
strFullFile += strFileName;
if(strFileName.Left(1) == '.')
return false;
UNIT* ptr = pAcceptPath->pRejectFile;
while(ptr)
{
if( strFullFile.CompareNoCase(ptr->strData) == 0 )
return false;
ptr = ptr->pNext;
}
//strFileName.MakeLower();
if(checkFileType(strFileName, m_pAcceptFileType))
return true;
if(checkFileType(strFileName, m_pRejectFileType))
return false;
return true;
}
/* 检查strPath是否为“拒绝路径” */
bool ConfigFile::checkPath(ACCEPT_PATH* pAcceptPath, const CString strPath)
{
UNIT* ptr = pAcceptPath->pRejectPath;
do{
if(strPath.CompareNoCase(ptr->strData) == 0)
return false;
}while(ptr = ptr->pNext);
return true;
}