-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (56 loc) · 1.77 KB
/
main.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
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <atlcomcli.h>
#include "LogReader.h"
int wmain(const int argc, const wchar_t* const argv[])
{
if (argc <= 2)
{
fwprintf(stderr, L"Error! Not enough command line arguments!\n");
fwprintf(stderr, L"Usage:\n");
fwprintf(stderr, L"LogReader.exe <filename> <pattern>\n");
fwprintf(stderr, L"Pattern is similar to fnmatch and supports symbols '*' and '?'.\n");
fwprintf(stderr, L"Example:\n");
fwprintf(stderr, L"LogReader.exe 20190102.log \"*bbb*\"\n");
return 1;
}
CLogReader reader;
const wchar_t* const fileName = argv[1];
const wchar_t* const lineFilter = argv[2];
const bool openedOk = reader.Open(fileName);
if (!openedOk)
{
fwprintf(stderr, L"Error! Failed to open file: \"%ws\"\n", fileName);
return 2;
}
const bool filterSetOk = reader.SetFilter(CW2A(lineFilter));
if (!filterSetOk)
{
fwprintf(stderr, L"Error! Failed to set filter: \"%ws\"\n", lineFilter);
return 3;
}
// prevent printf from changing LF to CRLF
// so we act the same way as grep does
_setmode(_fileno(stdout), O_BINARY);
while (true)
{
const auto line = reader.GetNextLine();
if (!line)
{
break;
}
fwrite(line->data(), line->size(), 1, stdout);
// Add optionally missing EOL after the last line of the file:
// CLineReader gives guarantee line is never empty, but it is better to check to be safe:
//if (line->size() > 0)
//{
// if (line->data()[line->size() - 1] != '\n')
// {
// fwrite("\n", 1, 1, stdout);
// }
//}
}
reader.Close();
return 0;
}