-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
147 lines (127 loc) · 4.31 KB
/
main.cpp
File metadata and controls
147 lines (127 loc) · 4.31 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
#include <filesystem>
#include <iostream>
#include "wingrep.h"
using namespace std;
//---------------------------------------------------------------------------------------------
// @name : Usage
//
// @description : Show command usage on console
//
// @returns : Nothing
//---------------------------------------------------------------------------------------------
static void Usage()
{
cout << "Usage:\n";
cout << "WinGrep.exe [options] <path> <search_string>\n";
cout << "Options:\n";
cout << " -r : Recursive search\n";
cout << " -i : Case insensitive search\n";
cout << " -n : Show line numbers in search result\n";
cout << " -w : Match whole word\n";
cout << " -j <num> : Uses <num> number of worker threads to search.\n"
" If nothing or an invalid value is specified then hardware_concurrency\n"
" is used to determine number of worker threads\n";
cout << "\n";
}
//---------------------------------------------------------------------------------------------
// @name : ProcessCommandLineArguments
//
// @description : Parses the command line arguments and populates the options
// structure required by the WinGrep application.
//
// @returns : true if valid arguments were passed, false otherwise
//---------------------------------------------------------------------------------------------
static bool ProcessCommandLineArguments(int argc, char* argv[], GrepOptions_t & options)
{
bool bValidArguments = true;
if (argc < 3)
{
return false;
}
string param;
int nextParamIndex = 1;
// Process options
for (int paramIndex = nextParamIndex; paramIndex < argc; paramIndex++)
{
param = argv[paramIndex];
if (param[0] == '-')
{
for (int j = 1; j < param.size(); j++)
{
if (param[j] == 'r')
{
options.bRecursiveSearch = true;
}
else if (param[j] == 'i')
{
options.bCaseInsensitive = true;
}
else if (param[j] == 'n')
{
options.bShowLineNumbers = true;
}
else if (param[j] == 'w')
{
options.bMatchWholeWord = true;
}
else if (param[j] == 'j')
{
nextParamIndex++;
string threadsStr(argv[nextParamIndex]);
if (threadsStr.empty())
{
bValidArguments = false;
}
else
{
try
{
options.threadsToUse = stoi(threadsStr);
}
catch (invalid_argument er)
{
options.threadsToUse = 0;
}
}
}
}
nextParamIndex++;
}
}
// Search Path
if (nextParamIndex < argc)
{
options.searchPath = argv[nextParamIndex];
nextParamIndex++;
}
// Search string
if (nextParamIndex < argc)
{
options.searchString = argv[nextParamIndex];
nextParamIndex++;
}
if (options.searchPath.empty() || options.searchString.empty())
{
bValidArguments = false;
}
return bValidArguments;
}
//---------------------------------------------------------------------------------------------
// M A I N
//---------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
GrepOptions_t options{};
bool bValidArguments = ProcessCommandLineArguments(argc, argv, options);
if (!bValidArguments)
{
Usage();
return 0;
}
//cout << "Search Path: " << options.searchPath << endl;
//cout << "Search String: " << options.searchString << endl;
long long results = 0;
results = GetSearchResults(options);
cout << " Found " << results << " result(s)\n";
return 0;
}