Click here to Skip to main content
15,896,358 members
Articles / Programming Languages / C++11

Extending boost::filesystem for Windows and Linux: Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
19 Mar 2013CPOL16 min read 31.3K   977   33  
Extending boost::filesystem for Windows and Linux.
/****************************************************
 *                                                  *
 *          Written by Stanic Igor 2012/2013        *
 *      FileSystemWatcher basic implementation      *
 *                                                  *
 ***************************************************/


#include "FileSystemWatcher.h"

using namespace std;
using namespace boost;
using namespace boost::filesystem;

FileSystemWatcher::FileSystemWatcher(const std::wstring &path, OpResults& errors, ChangeKind ch_kind)
    : m_locationPath(path),
      m_changesToWatch(ch_kind),
      m_changed(),
      m_stopThread(false),
      m_wathcerThreadActive(false),
      m_pause(false),
      m_pause_lock(),
      m_results(errors),
      m_ResultsLock(),
      m_lock(),
      m_finalizer()
{
    m_alertableThreadState = true;

    //Handle for current location to be watched
    m_Directory = INVALID_HANDLE_VALUE;

    //for results of ReadDirectoryChangesW()
    m_buffer = std::array<BYTE, 4096>();
    ZeroMemory(&m_overlapped, sizeof(OVERLAPPED));

    //hEvent shouldn't be used by system when completion func. is defined
    //so it should be safe to use it.
    m_overlapped.hEvent = this;

    if(getHandle(path))
        m_listenerThread = boost::thread(&FileSystemWatcher::process_events, this);
    else
        m_results.AddResult(OperationResult(false, TO_STRING_TYPE("FileSystemWatcher"), TO_STRING_TYPE("Unsuccessful attempt to obtain handle to file system location")));

}

FileSystemWatcher::~FileSystemWatcher()
{
    if(m_wathcerThreadActive)
    {
        stopProcessing();
    }
}

void FileSystemWatcher::setPaused(bool val)
{
    boost::unique_lock<boost::mutex> lock(m_pause_lock);
    m_pause = val;
}

bool FileSystemWatcher::getPaused()
{
    return m_pause;
}

//Blocking func for terminating watcher thread
bool FileSystemWatcher::stopProcessing()
{
    //if thread is alive
    if(m_wathcerThreadActive)
    {
        m_stopThread = true;

        boost::unique_lock<boost::mutex> lock(m_lock);
        m_finalizer.wait(lock);
    }

    m_stopThread = false;
    m_changed.disconnect_all_slots();
    return true;
}



 /* FileSystemWatcher implementation END */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Serbia Serbia
Software developer with couple of years of experience mostly with .NET programming and MS SQL databases currently interested in expanding knowledge to C++ and other operating systems.

Comments and Discussions