-
Notifications
You must be signed in to change notification settings - Fork 1
/
IsValidFileName.h
346 lines (284 loc) · 8.63 KB
/
IsValidFileName.h
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#ifndef __ISVALIDFILENAME_H__
#define __ISVALIDFILENAME_H__
class FileName
{
public:
enum RES_CODE
{
OK = 0,
UNKNOWN,
INVALID_LENGTH,
FILENAME_CLOCK,
FILENAME_AUX,
FILENAME_CON,
FILENAME_NUL,
FILENAME_PRN,
FILENAME_COM1,
FILENAME_COM2,
FILENAME_COM3,
FILENAME_COM4,
FILENAME_COM5,
FILENAME_COM6,
FILENAME_COM7,
FILENAME_COM8,
FILENAME_COM9,
FILENAME_LPT1,
FILENAME_LPT2,
FILENAME_LPT3,
FILENAME_LPT4,
FILENAME_LPT5,
FILENAME_LPT6,
FILENAME_LPT7,
FILENAME_LPT8,
FILENAME_LPT9,
INVALID_CHARACTER,
};
class NameResult
{
friend class FileName;
RES_CODE m_code;
int m_errPos;
int m_lastDot;
public:
NameResult() : m_code(UNKNOWN), m_errPos(-1), m_lastDot(-1)
{}
RES_CODE code() const { return m_code; }
int errPos() const { return m_errPos; }
int lastDot() const { return m_lastDot; }
bool isOk() const { return OK == m_code; }
bool isDeviceName() const { return m_code >= FILENAME_CLOCK && m_code <= FILENAME_LPT9; }
};
class PathResult
{
friend class FileName;
RES_CODE m_code;
int m_errPos;
int m_lastDot;
int m_fileIdx; // èíäåêñ ïåðâîé áóêâû èìåíè ôàéëà
bool m_isServerPath;
bool m_canBeFile; // ïóòü ìîæåò îêàçàòñÿ ïóò¸ì äî ôàéëà. Ò.å., â êîíöå ïóòè íåò ñëåøà
public:
PathResult()
: m_code(UNKNOWN)
, m_errPos(-1)
, m_lastDot(-1)
, m_fileIdx(-1)
, m_isServerPath(false)
, m_canBeFile(false)
{}
RES_CODE code() const { return m_code; }
int errPos() const { return m_errPos; }
int lastDot() const { return m_lastDot; }
int fileIdx() const { return m_fileIdx; }
bool isServerPath() const { return m_isServerPath; }
bool canBeFile() const { return m_canBeFile; }
bool isOk() const { return OK == m_code; }
bool isDeviceName() const { return m_code >= FILENAME_CLOCK && m_code <= FILENAME_LPT9; }
};
private:
static RES_CODE IsStdDeviceName( const char* pFileName, int len );
static RES_CODE IsStdDeviceName( const wchar_t* pFileName, int len );
template< typename T >
static bool isSlash( T ch ) { return ch == T('\\') || ch == T('/'); }
// Ïðîâåðÿåò íà ïðàâèëüíîñòü èìÿ ôàéëà
template< typename T >
static bool testName_T( const T* pFileName, int len, NameResult& res )
{
res.m_code = UNKNOWN;
res.m_errPos = -1;
res.m_lastDot = -1;
if( !pFileName || !*pFileName || 0 == len || len > 255 )
{
res.m_code = INVALID_LENGTH;
res.m_errPos = 0;
return false;
}
RES_CODE deviceCode = IsStdDeviceName( pFileName, len );
if( OK != deviceCode )
{
res.m_code = deviceCode;
res.m_errPos = 0;
return false;
}
// Ïðîâåðêà íà îñîáûå îãðàíè÷åíèÿ äëÿ ïåðâîãî è ïîñëåäíåãî ñèìâîëîâ
switch( pFileName[0] )
{
case T(' '):
res.m_code = INVALID_CHARACTER;
res.m_errPos = 0;
return false;
}
switch( pFileName[len-1] )
{
case T('.'):
case T(' '):
res.m_code = INVALID_CHARACTER;
res.m_errPos = len-1;
return false;
}
// Îáùàÿ ïðîâåðêà ñèìâîëîâ íà ïðàâèëüíîñòü
for( int i = 0; i < len; i++ )
{
switch( pFileName[i] )
{
case T('\\'):
case T('/'):
case T('|'):
case T(':'):
case T('*'):
case T('?'):
case T('>'):
case T('<'):
case T('\"'):
case T('\t'):
res.m_code = INVALID_CHARACTER;
res.m_errPos = i;
return false;
case '.':
res.m_lastDot = i;
break;
}
}
res.m_code = OK;
return true;
}
// Ïðîâåðÿåò íà ïðàâèëüíîñòü ïóòü äî ôàéëà èëè ïàïêè
template< typename T >
static bool testPath_T( const T* pFilePath, PathResult& res )
{
res.m_code = UNKNOWN;
res.m_errPos = -1;
res.m_lastDot = -1;
res.m_fileIdx = -1;
res.m_isServerPath = false;
res.m_canBeFile = false;
// Äëèííà ïóòè äîëæíà áûòü, ïî êðàéíåé ìåðå 2 ñèìâîëà.
// íàïðèìåð "C:" èëè "\\"
if( !pFilePath || !pFilePath[0] || !pFilePath[1] )
{
res.m_code = INVALID_LENGTH;
res.m_errPos = 0;
return false;
}
int start = 2;
// Ïðîâåðêà áóêâû äèñêà
res.m_isServerPath = pFilePath[0] == T('\\') && pFilePath[1] == T('\\');
if( res.m_isServerPath )
{
// íå ìîæåò áûòü òðè ñëåøà ïîäðÿä
if( isSlash( pFilePath[2] ) )
{
res.m_code = INVALID_CHARACTER;
res.m_errPos = 2;
return false;
}
}
else
{
// âòîðîé ñèìâîë äîëæåí áûòü ":"
if( T(':') != pFilePath[1] )
{
res.m_code = INVALID_CHARACTER;
res.m_errPos = 1;
return false;
}
// òðåòèé ñèìâîë äîëæåí áûòü çàâåðøåíèåì ñëåøåì
if( T('0') != pFilePath[2] )
{
if( !isSlash( pFilePath[2] ) )
{
res.m_code = INVALID_CHARACTER;
res.m_errPos = 2;
return false;
}
start = 3;
}
}
// Òåïåðü "start" óêàçûâàåò íà áóêâó ñëåäóþùóþ çà îïðåäåëåíèåì äèñêà
// íàïðèìåð äëÿ "C:\some" start == 3, äëÿ "\\Some" start == 2.
int beginIdx = start; // èíäåêñ áóêâû ïîñëå ïîñëåäíåãî ñëåøà
NameResult fileRes;
int i = start; // èòåðàòîð
for( ; pFilePath[i]; i++ )
{
if( isSlash( pFilePath[i] ) )
{
int len = i - beginIdx;
if( 0 == len )
{
res.m_code = INVALID_CHARACTER;
res.m_errPos = i;
return false;
}
if( !testName_T( &pFilePath[beginIdx], len, fileRes ) )
{
res.m_code = fileRes.m_code;
res.m_errPos = beginIdx + fileRes.m_errPos;
return false;
}
beginIdx = i+1;
}
}
// Åñëè èìÿ ôàéëà ñëèøêîì äëèííîå
if( i > MAX_PATH )
{
res.m_code = INVALID_LENGTH;
res.m_errPos = i;
return false;
}
// Åñëè â êîíöå íå áûëî çàâåðøàþùåãî ñëåøà
if( T('\0') != pFilePath[beginIdx] )
{
res.m_canBeFile = true;
res.m_fileIdx = beginIdx;
int len = i - beginIdx - 1;
if( !testName_T( &pFilePath[beginIdx], len, fileRes ) )
{
res.m_code = fileRes.m_code;
res.m_errPos = beginIdx + fileRes.m_errPos;
return false;
}
res.m_lastDot = fileRes.m_lastDot != -1 ? beginIdx + fileRes.m_lastDot : -1;
}
res.m_code = OK;
return true;
}
public:
static bool testName( const char* pFileName, NameResult& res )
{
return testName_T( pFileName, (int)strlen( pFileName ), res );
}
static bool testName( const wchar_t* pFileName, NameResult& res )
{
return testName_T( pFileName, (int)wcslen( pFileName ), res );
}
static bool testPath( const char* pFilePath, PathResult& res )
{
return testPath_T( pFilePath, res );
}
static bool testPath( const wchar_t* pFilePath, PathResult& res )
{
return testPath_T( pFilePath, res );
}
static bool testName( const char* pFileName )
{
NameResult res;
return testName_T( pFileName, (int)strlen( pFileName ), res );
}
static bool testName( const wchar_t* pFileName )
{
NameResult res;
return testName_T( pFileName, (int)wcslen( pFileName ), res );
}
static bool testPath( const char* pFilePath )
{
PathResult res;
return testPath_T( pFilePath, res );
}
static bool testPath( const wchar_t* pFilePath )
{
PathResult res;
return testPath_T( pFilePath, res );
}
};
#endif