-
Notifications
You must be signed in to change notification settings - Fork 0
/
freqWin.cpp
361 lines (323 loc) · 12.1 KB
/
freqWin.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***************************************************************************
//
// Copyright (c) 1995, 1996, 1997, 1998, 1999 Gerhard W. Gross.
//
// THIS SOFTWARE IS PROVIDED BY GERHARD W. GROSS ''AS IS'' AND ANY
// EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GERHARD W. GROSS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
// PERMISSION TO USE, COPY, MODIFY, AND DISTRIBUTE THIS SOFTWARE AND ITS
// DOCUMENTATION FOR ANY PURPOSE AND WITHOUT FEE IS HEREBY GRANTED,
// PROVIDED THAT THE ABOVE COPYRIGHT NOTICE, THE ABOVE DISCLAIMER
// NOTICE, THIS PERMISSION NOTICE, AND THE FOLLOWING ATTRIBUTION
// NOTICE APPEAR IN ALL SOURCE CODE FILES AND IN SUPPORTING
// DOCUMENTATION AND THAT GERHARD W. GROSS BE GIVEN ATTRIBUTION
// AS THE MAIN AUTHOR OF THIS PROGRAM IN THE FORM OF A TEXTUAL
// MESSAGE AT PROGRAM STARTUP OR IN THE DISPLAY OF A USAGE MESSAGE,
// OR IN DOCUMENTATION (ONLINE OR TEXTUAL) PROVIDED WITH THIS PROGRAM.
//
// ALL OR PARTS OF THIS CODE WERE WRITTEN BY GERHARD W. GROSS, 1995-2003.
//
// Functions that are specific to Win32 are isolated in this file.
// There is an analogous file for Linux/Unice specific code. The
// reason that forces seperate code paths is subdirectory recursion. Unices
// perform variable expansion from the command line by the shell. Win32
// systems do not, but instead perform variable expansion within the
// "_findfirsti64" and "_findnexti64" functions.
***************************************************************************/
//#include <exception>
#include <windows.h>
#include <minwinbase.h>
#include <strsafe.h>
#include <stdio.h>
#include <wchar.h>
#include <cstdio>
#include <cwchar>
#include "freqwin.h"
#include "freq.h"
extern bool Verbose;
extern bool Prnt_Lines;
extern bool Prnt_Min;
extern bool File_Find;
extern bool Prnt_Some;
extern bool SuppressErrorsPrintout;
extern bool g_reverseSlashDir;
extern long g_numFilesMatchPattern;
extern long g_numDirsMatchPattern;
extern long g_numFilesAndDirsChecked;
extern long g_numDirsToOmit;
extern TCHAR g_dirsToOmit[MAX_DIRS_TO_OMIT][_MAX_PATH];
extern long g_numFilePtrnsToOmit;
extern TCHAR g_filePtrnsToOmit[MAX_FILE_PATTERNS_TO_OMIT][_MAX_PATH];
void PrintLastError(TCHAR* lpszFunction);
//TCHAR* strerror(int errNum)
//{
// const int errMsgSz = 2048;
// static TCHAR errMsg[errMsgSz];
// errMsg[0] = 0;
// _wcserror_s(errMsg, errMsgSz, errNum);
// return errMsg;
//}
bool ShouldIgnoreThisFile(WIN32_FIND_DATA ffd)
{
TCHAR lwrStr[_MAX_PATH];
CopyStringToLowerCaseRemoveAsterisk(ffd.cFileName, lwrStr);
for (int i = 0; i < g_numFilePtrnsToOmit; ++i)
{
unsigned int tmp = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
if ((tmp == 0) && wcsstr(lwrStr, g_filePtrnsToOmit[i]) != 0)
return true;
}
return false;
}
bool ShouldIgnoreThisDir(WIN32_FIND_DATA ffd)
{
for (int i = 0; i < g_numDirsToOmit; ++i)
{
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && _wcsicmp(g_dirsToOmit[i], ffd.cFileName) == 0)
return true;
}
return false;
}
/***************************************************************************
A directory is passed in here
***************************************************************************/
bool IsSensibleFileName(const TCHAR* dirName)
{
bool retVal = false;
int i;
size_t len = wcslen(dirName);
if (len > 0)
{
for (i = 0; i < len; ++i)
{
// Allow extended ASCII chars (>= 127) since have seen the copyright symbol used
if (dirName[i] > -1 && !isprint(dirName[i]))
break;
}
// This directory name is OK if all characaters were printable
// (loop did not end early).
if (i >= len)
retVal = true;
}
return retVal;
}
/***************************************************************************
This recursive function is called for every subdirectory in the search
path. When it finally encounters a directory with no subdirectories,
SearchCurrentDirectory() is called to do the actual file searching
dependent on the filename, with any file cards, supplied on the command
line.
***************************************************************************/
void SearchAllDirectories(const TCHAR *raw_in_file)
{
const int curPathSz = _MAX_PATH;
TCHAR current_path[curPathSz];
const TCHAR all_wildcard[] = L"*"; // search through for all directories
long fl_cnt = 0;
const int errStrSz = _MAX_PATH;
TCHAR errStr[errStrSz];
bool skipThisDir = false;
static int funcRecursionCnt = 0;
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
try
{
funcRecursionCnt++;
if(_wgetcwd(current_path, curPathSz) == NULL)
{
errStr[0] = 0;
if (Verbose == 1)
swprintf_s(errStr, errStrSz, L" Attempting to _getcwd to \"%s\", funcRecursionCnt = %d - ",
current_path, funcRecursionCnt);
throw errStr;
}
if ((hFind = FindFirstFileEx(all_wildcard, FindExInfoBasic, &ffd, FindExSearchNameMatch, nullptr, 0)) == INVALID_HANDLE_VALUE)
{
if (!SuppressErrorsPrintout)
wprintf(L"\n No \"%s\" files in %s", all_wildcard, current_path);
}
else
{
do
{
errStr[0] = 0;
if (ShouldIgnoreThisDir(ffd))
continue;
if(wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0 && wcscmp(ffd.cFileName, L"") != 0)
{
g_numFilesAndDirsChecked++;
if((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
continue;
try
{
if (IsSensibleFileName(ffd.cFileName))
{
if(_wchdir(ffd.cFileName) != 0)
{
if (Verbose == 1)
swprintf_s(errStr, errStrSz, L" Attempting to _chdir to \"%s\\%s\", file attrib: %d, funcRecursionCnt: %d - ",
current_path, ffd.cFileName, ffd.dwFileAttributes, funcRecursionCnt);
throw errStr;
}
}
}
catch (TCHAR * excp)
{
PrintLastError(excp);
continue;
}
SearchAllDirectories(raw_in_file);
try
{
if(_wchdir(current_path) != 0)
{
swprintf_s(errStr, errStrSz, L" Attempting to _chdir back to \"%s\" - ", current_path);
throw errStr;
}
}
catch (TCHAR* excp)
{
PrintLastError(excp);
continue;
}
}
} while ((hFind != INVALID_HANDLE_VALUE) && (FindNextFileW(hFind, &ffd) != 0));
}
if (hFind != INVALID_HANDLE_VALUE && hFind != 0)
FindClose(hFind);
SearchCurrentDirectory(raw_in_file, current_path);
}
catch (TCHAR* excp)
{
if (excp[0] != 0)
PrintLastError(excp);
}
funcRecursionCnt--;
}
/***************************************************************************
This function is called for every directory that has no subdirectories or
if the -R command line option was not supplied. The current directory is
searched for files that match the file name, with any wildcards, given at
the command line. For each of those files, ProcessFile() is called which
does the actual search and replace in the file contents of the command
line specified strings.
***************************************************************************/
void SearchCurrentDirectory(const TCHAR *raw_in_file, const TCHAR *current_path)
{
long fl_cnt = 0, freq_cntr = 0;
TCHAR cur_path_tmp[_MAX_PATH];
TCHAR buff[_MAX_PATH];
TCHAR errStr[_MAX_PATH];
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR fname[_MAX_PATH];
try
{
if (current_path[0] != 0)
wcscpy_s(cur_path_tmp, _MAX_PATH, current_path);
else
{
cur_path_tmp[0] = '.';
cur_path_tmp[1] = 0;
}
errStr[0] = 0;
if ((hFind = FindFirstFileEx(raw_in_file, FindExInfoBasic, &ffd, FindExSearchNameMatch, nullptr, 0)) == INVALID_HANDLE_VALUE)
{
if (!SuppressErrorsPrintout)
swprintf_s(errStr, _MAX_PATH, L" Error attempting to FindFirstFile in %s\\%s", current_path, raw_in_file);
throw errStr;
}
do
{
wcscpy_s(fname, _MAX_PATH, ffd.cFileName);
if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0 && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if (File_Find)
{
// No search string just print the files that match the file pattern
//sprintf_s(buff, _MAX_PATH, "%s\\%s\n", cur_path_tmp, ffd.cFileName);
//printf(buff);
wprintf_s(L"\n%s\\%s", cur_path_tmp, fname);
g_numFilesMatchPattern++;
}
else if (ShouldIgnoreThisFile(ffd))
continue;
freq_cntr = ProcessFile(fname);
if ((freq_cntr > 0 || Verbose == 1) && (!Prnt_Min || Prnt_Some))
{
//freq_cntr > 0 ? printf(" +") : printf(" ");
swprintf_s(buff, _MAX_PATH, L"\n%d occurrence(s) in: %s\\%s", freq_cntr, cur_path_tmp, fname);
if (g_reverseSlashDir == 1)
ReverseSlashDirInString(buff);
wprintf(buff);
if (Prnt_Lines || Verbose)
printf("\n ---------------------------------------------------------------------------------\n");
}
}
else if (File_Find && ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0)
{
if (!ShouldIgnoreThisDir(ffd))
{
wprintf(L"\n%s\\%s", cur_path_tmp, fname);
g_numDirsMatchPattern++;
}
}
} while ((hFind != INVALID_HANDLE_VALUE) && (FindNextFileW(hFind, &ffd) != 0));
}
catch (TCHAR* excp)
{
if (excp[0] != 0)
PrintLastError(excp);
}
if (hFind != INVALID_HANDLE_VALUE && hFind != 0)
FindClose(hFind);
}
void PrintLastError(TCHAR* msg)
{
if (SuppressErrorsPrintout)
return;
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen(msg)+40)*sizeof(TCHAR));
StringCchPrintfW((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), msg, dw, lpMsgBuf);
//MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
fwprintf(stderr, L"\n GetLastError: %s", (LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
//ExitProcess(dw);
}
/***************************************************************************
This function prints an error message and the system error message
using perror(). exit() is not called from here.
***************************************************************************/
/*
void SysError( char *fmt, ... )
{
char buff[256];
va_list va;
va_start(va, fmt);
wvsprintf( buff, fmt, va );
perror(buff);
}
*/