-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
203 lines (199 loc) · 7.76 KB
/
Program.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace LOCCounter
{
class Program
{
private static void Main()
{
Console.WriteLine
(
"Format: <paths>; <extensions>; <recursive>; <exclude-extension>\n" +
"paths: Pipe-separated list of targeted paths. Place \"?\" in front to exclude.\n" +
"extensions: Comma-separated list of targeted file extensions. Leave blank for all extensions.\n" +
"recursive: \"true\" or \"false\". Whether to recursively search for files in the specified paths.\n" +
"exclude-extension: \"true\" or \"false\". Whether to exclude the extensions specified in \"extensions\" and search for all other extensions.\n" +
"Example: C:\\Code | D:\\Programming | ?D:\\Programming\\Secret; .cs, .cpp, .c; true; false\n"
);
while (true)
{
string command = Console.ReadLine();
string errorText = CheckError(command);
if (!string.IsNullOrEmpty(errorText))
{
Console.WriteLine(errorText);
continue;
}
string[] arguments = command.Split(';', StringSplitOptions.None);
string[] paths = arguments[0].Split('|', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < paths.Length; i++)
{
paths[i] = paths[i].Trim();
}
string[] extensions = arguments[1].Split(',', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < extensions.Length; i++)
{
extensions[i] = extensions[i].Trim();
}
if (Array.TrueForAll(extensions, extension => string.IsNullOrWhiteSpace(extension)))
{
extensions = new string[1] { "" };
}
bool isRecursive;
try
{
isRecursive = bool.Parse(arguments[2]);
}
catch when (string.IsNullOrWhiteSpace(arguments[2]))
{
isRecursive = true;
}
bool isExcludeExtension;
try
{
isExcludeExtension = bool.Parse(arguments[3]);
}
catch when (string.IsNullOrWhiteSpace(arguments[3]))
{
isExcludeExtension = false;
}
List<string> includedPaths = new List<string>();
List<string> excludedPaths = new List<string>();
string LOCs = "";
long totalLines = 0;
foreach (string path in paths)
{
if (path.StartsWith('?'))
{
excludedPaths.Add(path.Remove(0, 1));
}
else
{
includedPaths.Add(path);
}
}
foreach (string path in includedPaths)
{
List<string> pathsToSearch = new List<string>();
if (File.GetAttributes(path).HasFlag(FileAttributes.Directory))
{
ScanDirectory(path, excludedPaths, extensions, isExcludeExtension, ref LOCs, ref totalLines, isRecursive);
}
else
{
if (!excludedPaths.Contains(path))
{
ScanFile(path, excludedPaths, extensions, isExcludeExtension, ref LOCs, ref totalLines);
}
}
}
Console.WriteLine($"{LOCs}\nTotal: {totalLines} lines\n");
}
}
private static string CheckError(string command)
{
string errorText = "";
string[] arguments = command.Split(';', StringSplitOptions.None);
string[] paths = arguments[0].Split('|', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < paths.Length; i++)
{
paths[i] = paths[i].Trim();
}
if (arguments.Length != 4)
{
errorText += $"Incorrect argument count({arguments.Length} provided, should be 4)\n";
return errorText;
}
if (paths.Length == 0 || Array.TrueForAll(paths, path => string.IsNullOrWhiteSpace(path)))
{
errorText += "paths: No paths provided\n";
}
try
{
bool.Parse(arguments[2]);
}
catch (Exception e)
{
if (e is ArgumentNullException)
{
errorText += "recursive: Argument is null\n";
}
if (e is FormatException)
{
errorText += "recursive: Wrong format(must be \"true\" or \"false\")\n";
}
}
try
{
bool.Parse(arguments[3]);
}
catch (Exception e)
{
if (e is ArgumentNullException)
{
errorText += "exclude-extension: Argument is null\n";
}
if (e is FormatException)
{
errorText += "exclude-extension: Wrong format(must be \"true\" or \"false\")\n";
}
}
foreach (string path in paths)
{
string trimmed;
if (path.StartsWith('?'))
{
trimmed = path.Remove(0, 1);
}
else
{
trimmed = path;
}
if (!File.Exists(trimmed) && !Directory.Exists(trimmed) && !string.IsNullOrWhiteSpace(trimmed) && !trimmed.StartsWith('?'))
{
errorText += $"{trimmed}: No such file or directory\n";
}
}
return errorText;
}
private static void ScanDirectory(string path, List<string> excludedPaths, string[] extensions, bool isExcludeExtension, ref string LOCs, ref long totalLines, bool isRecursive)
{
if (excludedPaths.ContainsIgnoreCase(path))
{
return;
}
excludedPaths.Add(path);
foreach (string file in Directory.EnumerateFiles(path))
{
ScanFile(file, excludedPaths, extensions, isExcludeExtension, ref LOCs, ref totalLines);
excludedPaths.Add(file);
}
if (isRecursive)
{
foreach (string directory in Directory.EnumerateDirectories(path))
{
ScanDirectory(directory, excludedPaths, extensions, isExcludeExtension, ref LOCs, ref totalLines, isRecursive);
}
}
}
private static void ScanFile(string file, List<string> excludedPaths, string[] extensions, bool isExcludeExtension, ref string LOCs, ref long totalLines)
{
if (!excludedPaths.ContainsIgnoreCase(file) && extensions.Any(s => file.EndsWith(s)) == !isExcludeExtension)
{
long lines = 0;
try
{
lines = File.ReadLines(file).Count();
}
catch (UnauthorizedAccessException)
{
return;
}
LOCs += $"{file}: {lines} lines\n";
totalLines += lines;
}
}
}
}