1 using System;
2 using System.Collections;
3
4 void PrintHelp()
5 {
6 Console.Out() << "Directory list" << endl();
7 Console.Out() << "Usage: DirList [options] { FILE_PATH_WITH_WILD_CARDS }" << endl();
8 Console.Out() << "Prints list of files matching FILE_PATH_WITH_WILD_CARDS \'*\' and \'?\' such as \'*.cm\' or \'C:/programs/*.exe\'." << endl();
9 Console.Out() << "--help | h" << endl();
10 Console.Out() << " Print help and exit." << endl();
11 Console.Out() << "--debug-parsing | -d" << endl();
12 Console.Out() << " Print XML parsing log to stderr for regular expression parsing." << endl();
13 }
14
15 int main(int argc, const char** argv)
16 {
17 List<string> filePaths;
18 for (int i = 1; i < argc; ++i;)
19 {
20 string arg = argv[i];
21 if (arg.StartsWith("--"))
22 {
23 if (arg == "--help")
24 {
25 PrintHelp();
26 return 0;
27 }
28 else if (arg == "--debug-parsing")
29 {
30 System.RegularExpressions.SetDebugParsing(true);
31 }
32 else
33 {
34 Console.Error() << "unknown option \'" << arg << "\'" << endl();
35 return 1;
36 }
37 }
38 else if (arg.StartsWith("-"))
39 {
40 string options = arg.Substring(1);
41 for (char o : options)
42 {
43 switch (o)
44 {
45 case 'h':
46 {
47 PrintHelp();
48 return 0;
49 }
50 case 'd':
51 {
52 System.RegularExpressions.SetDebugParsing(true);
53 break;
54 }
55 default:
56 {
57 Console.Error() << "unknown option \'-" << string(o) << "\'" << endl();
58 return 1;
59 }
60 }
61 }
62 }
63 else
64 {
65 auto result = System.IO.GetFullPath(arg);
66 if (result.Error())
67 {
68 Console.Error() << result.GetErrorMessage() << endl();
69 return 1;
70 }
71 filePaths.Add(result.Value());
72 }
73 System.RegularExpressions.Context context;
74 for (const auto& filePath : filePaths)
75 {
76 string dir = System.IO.Path.GetDirectoryName(filePath);
77 string pattern = System.IO.Path.GetFileName(filePath);
78 auto utf32Result = ToUtf32(pattern);
79 if (utf32Result.Error())
80 {
81 Console.Error() << utf32Result.GetErrorMessage() << endl();
82 return 1;
83 }
84 const ustring& u32pattern = utf32Result.Value();
85 auto nfaResult = System.RegularExpressions.CompileFilePattern(context, u32pattern);
86 if (nfaResult.Error())
87 {
88 Console.Error() << nfaResult.GetErrorMessage() << endl();
89 return 1;
90 }
91 System.RegularExpressions.Nfa& nfa = nfaResult.Value();
92 List<string> files = Directory.GetFiles(dir);
93 for (const string& fp : files)
94 {
95 string fileName = System.IO.Path.GetFileName(fp);
96 auto utf32Result = ToUtf32(fileName);
97 if (utf32Result.Error())
98 {
99 Console.Error() << utf32Result.GetErrorMessage() << endl();
100 return 1;
101 }
102 const ustring& u32fileName = utf32Result.Value();
103 if (System.RegularExpressions.PatternMatch(u32fileName, nfa))
104 {
105 Console.Out() << fp << endl();
106 }
107 }
108 }
109 }
110 return 0;
111 }