-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (93 loc) · 2.05 KB
/
main.cpp
File metadata and controls
118 lines (93 loc) · 2.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
// Filename matching.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
bool testName(const char * name);
void fixBackSlash(char * path);
bool isHard(char * temp);
bool testElement(char * text, char * templ);
enum ResultType
{
eGoodResult = 0, // template == filename
eBadResult = 1, // template != filename
eInvalidParameters = 2 // error in params
};
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cout << "invalid parameters";
return eInvalidParameters; // invalid size of arguments
}
char * name= argv[1];
char * templ = argv[2];
fixBackSlash(name);
fixBackSlash(templ);
if (!testName(name))
{
std::cout << "Invalid parameters";
return eInvalidParameters; // unresolved character
}
if (!testElement(name, templ))
{
std::cout << "File name does not match pattern";
return eBadResult; // template != filename
}
std::cout << "The file name matches the pattern";
return eGoodResult;
}
bool testElement(char * text, char * templ)
{
bool result = false;
switch (*templ)
{
case '?':
result = (*text) && (*text) != '/' && testElement(text + 1, templ + 1);
break;
case '*':
if (isHard(templ + 1))
{
if (((*text) && (*text) == '/'))
{
result = false;
break;
}
result = testElement(text, templ + 1) || *text && testElement(text + 1, templ);
}
else
result = testElement(text, templ + 2) || *text && testElement(text + 1, templ);
break;
case '\0':
result = !(*text);
break;
default:
result = (templ[0] == text[0] && testElement(text + 1, templ + 1));
}
return result;
}
bool isHard(char * temp)
{
return !(*temp) || *temp != '*';
}
bool testName(const char * name)
{
const char * symbols = "*? ";
while (*symbols != '\0')
{
if (strchr(name, symbols[0]))
return false;
++symbols;
}
return true;
}
void fixBackSlash(char * path)
{
while (*path != '\0')
{
if (*path == '\\')
*path = '/';
++path;
}
}