-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
154 lines (133 loc) · 4.75 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
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
//
// The MIT License
//
// Copyright (c) 2010 James E Beveridge
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
// See ReadMe.txt for overview information.
#include "stdafx.h"
#include "ReadDirectoryChanges.h"
LPCWSTR ExplainAction( DWORD dwAction );
bool TryGetKeyboardInput( HANDLE hStdIn, bool &bTerminate, char* buf );
int start();
//
// When the application starts, it immediately starts monitoring your home
// directory, including children, as well as C:\, not including children.
// The application exits when you hit Esc.
// You can add a directory to the monitoring list by typing the directory
// name and hitting Enter. Notifications will pause while you type.
//
int start() {
const DWORD dwNotificationFlags =
FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_CREATION
| FILE_NOTIFY_CHANGE_FILE_NAME;
// Create the monitor and add two directories.
CReadDirectoryChanges changes;
//changes.AddDirectory(_tgetenv(_T("USERPROFILE")), true, dwNotificationFlags);
changes.AddDirectory(_T("S:\\KATAPULT\\Katapult Research\\FutureKidCPU\\photos"), false, dwNotificationFlags);
changes.AddDirectory(_T("S:\\KATAPULT\\Katapult Research\\FutureKidCPU\\photos_pc2"), false, dwNotificationFlags);
changes.AddDirectory(_T("S:\\KATAPULT\\Katapult Research\\FutureKidCPU\\photos_pc3"), false, dwNotificationFlags);
HANDLE hStdIn = ::GetStdHandle(STD_INPUT_HANDLE);
const HANDLE handles[] = { hStdIn, changes.GetWaitHandle() };
char buf[MAX_PATH];
bool bTerminate = false;
while (!bTerminate)
{
DWORD rc = ::WaitForMultipleObjectsEx(_countof(handles), handles, false, INFINITE, true);
switch (rc)
{
case WAIT_OBJECT_0 + 0:
// hStdIn was signaled. This can happen due to mouse input, focus change,
// Shift keys, and more. Delegate to TryGetKeyboardInput().
// TryGetKeyboardInput sets bTerminate to true if the user hits Esc.
if (TryGetKeyboardInput(hStdIn, bTerminate, buf))
changes.AddDirectory(CStringW(buf), false, dwNotificationFlags);
break;
case WAIT_OBJECT_0 + 1:
// We've received a notification in the queue.
{
DWORD dwAction;
CStringW wstrFilename;
if (changes.CheckOverflow())
wprintf(L"Queue overflowed.\n");
else
{
changes.Pop(dwAction, wstrFilename);
wprintf(L"%s %s\n", ExplainAction(dwAction), wstrFilename);
}
}
break;
case WAIT_IO_COMPLETION:
// Nothing to do.
break;
}
}
// Just for sample purposes. The destructor will
// call Terminate() automatically.
changes.Terminate();
return EXIT_SUCCESS;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
return start();
}
LPCWSTR ExplainAction( DWORD dwAction )
{
switch (dwAction)
{
case FILE_ACTION_ADDED :
return L"Added";
case FILE_ACTION_REMOVED :
return L"Deleted";
case FILE_ACTION_MODIFIED :
return L"Modified";
case FILE_ACTION_RENAMED_OLD_NAME :
return L"Renamed From";
case FILE_ACTION_RENAMED_NEW_NAME :
return L"Renamed To";
default:
return L"BAD DATA";
}
}
bool TryGetKeyboardInput( HANDLE hStdIn, bool &bTerminate, char* buf )
{
DWORD dwNumberOfEventsRead=0;
INPUT_RECORD rec = {0};
if (!::PeekConsoleInput(hStdIn, &rec, 1, &dwNumberOfEventsRead))
return false;
if (rec.EventType == KEY_EVENT)
{
if (rec.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
bTerminate = true;
else if (rec.Event.KeyEvent.wVirtualKeyCode > VK_HELP)
{
//if (!gets_s(buf)) // End of file, usually Ctrl-Z
if (!fgets(buf, sizeof(buf), stdin)) // End of file, usually Ctrl-Z
bTerminate = true;
else
return true;
}
}
::FlushConsoleInputBuffer(hStdIn);
return false;
}