Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jrocha committed Feb 22, 2020
1 parent 4d5abdf commit 4d66a0a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
13 changes: 11 additions & 2 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "VerticalFileSwitcher.h"
#include "functionListPanel.h"
#include "ReadDirectoryChanges.h"
#include "ReadFileChanges.h"
#include <tchar.h>
#include <unordered_set>

Expand All @@ -60,13 +61,16 @@ DWORD WINAPI Notepad_plus::monitorFileOnChange(void * params)
CReadDirectoryChanges changes;
changes.AddDirectory(folderToMonitor, true, dwNotificationFlags);

CReadFileChanges fChanges;
fChanges.AddFile(fullFileName, FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE);

HANDLE changeHandles[] = { buf->getMonitoringEvent(), changes.GetWaitHandle() };

bool toBeContinued = true;

while (toBeContinued)
{
DWORD waitStatus = ::WaitForMultipleObjects(_countof(changeHandles), changeHandles, FALSE, INFINITE);
DWORD waitStatus = ::WaitForMultipleObjects(_countof(changeHandles), changeHandles, FALSE, 250);
switch (waitStatus)
{
case WAIT_OBJECT_0 + 0:
Expand Down Expand Up @@ -102,7 +106,11 @@ DWORD WINAPI Notepad_plus::monitorFileOnChange(void * params)
}
}
break;

case WAIT_TIMEOUT:
if (fChanges.DetectChanges()) {
::PostMessage(h, NPPM_INTERNAL_RELOADSCROLLTOEND, reinterpret_cast<WPARAM>(buf), 0);
}
break;
case WAIT_IO_COMPLETION:
// Nothing to do.
break;
Expand All @@ -112,6 +120,7 @@ DWORD WINAPI Notepad_plus::monitorFileOnChange(void * params)
// Just for sample purposes. The destructor will
// call Terminate() automatically.
changes.Terminate();
fChanges.Terminate();
delete monitorInfo;
return EXIT_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "ReadFileChanges.h"



CReadFileChanges::CReadFileChanges()
{
_szFile = NULL;
_dwNotifyFilter = 0;
}


CReadFileChanges::~CReadFileChanges()
{
}


BOOL CReadFileChanges::DetectChanges() {

WIN32_FILE_ATTRIBUTE_DATA fInfo;
BOOL rValue = FALSE;
::GetFileAttributesEx(_szFile, GetFileExInfoStandard, &fInfo);

if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_SIZE) && (fInfo.nFileSizeHigh != _lastFileInfo.nFileSizeHigh || fInfo.nFileSizeLow != _lastFileInfo.nFileSizeLow)) {
rValue = TRUE;
}

if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_LAST_WRITE) && (fInfo.ftLastWriteTime.dwHighDateTime != _lastFileInfo.ftLastWriteTime.dwHighDateTime || fInfo.ftLastWriteTime.dwLowDateTime != _lastFileInfo.ftLastWriteTime.dwLowDateTime)) {
rValue = TRUE;
}

_lastFileInfo = fInfo;
return rValue;
}

void CReadFileChanges::AddFile(LPCTSTR szFile, DWORD dwNotifyFilter)
{
_szFile = szFile;
_dwNotifyFilter = dwNotifyFilter;
::GetFileAttributesEx(szFile, GetFileExInfoStandard, &_lastFileInfo);
}


void CReadFileChanges::Terminate()
{
_szFile = NULL;
_dwNotifyFilter = 0;
}
29 changes: 29 additions & 0 deletions PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once


#include <stdio.h>

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif


#include <windows.h>


class CReadFileChanges
{
public:
CReadFileChanges();
~CReadFileChanges();
void AddFile(LPCTSTR szDirectory, DWORD dwNotifyFilter);
BOOL DetectChanges();
void Terminate();

private:
LPCTSTR _szFile;
DWORD _dwNotifyFilter;
WIN32_FILE_ATTRIBUTE_DATA _lastFileInfo;

};

3 changes: 3 additions & 0 deletions PowerEditor/visual.net/notepadPlus.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ copy ..\src\contextMenu.xml ..\bin64\contextMenu.xml
<ClCompile Include="..\src\WinControls\FileBrowser\fileBrowser.cpp" />
<ClCompile Include="..\src\WinControls\ReadDirectoryChanges\ReadDirectoryChanges.cpp" />
<ClCompile Include="..\src\WinControls\ReadDirectoryChanges\ReadDirectoryChangesPrivate.cpp" />
<ClCompile Include="..\src\WinControls\ReadDirectoryChanges\ReadFileChanges.cpp" />
</ItemGroup>
<ItemGroup>
<Image Include="..\src\icons\allChars_off.ico" />
Expand Down Expand Up @@ -673,6 +674,8 @@ copy ..\src\contextMenu.xml ..\bin64\contextMenu.xml
<ClInclude Include="..\src\WinControls\ReadDirectoryChanges\ReadDirectoryChanges.h" />
<ClInclude Include="..\src\WinControls\ReadDirectoryChanges\ReadDirectoryChangesPrivate.h" />
<ClInclude Include="..\src\WinControls\ReadDirectoryChanges\ThreadSafeQueue.h" />
<ClInclude Include="..\src\WinControls\ReadDirectoryChanges\ReadFileChanges.h" />

</ItemGroup>
<ItemGroup>
<Manifest Include="..\src\notepad++.exe.manifest" />
Expand Down

0 comments on commit 4d66a0a

Please sign in to comment.