FileWatcher.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/core/filesystem/FileWatcher.cpp
24 
25  \brief This class is designed to watch a file.
26 
27  \author Matheus Cavassan Zaglia
28  */
29 
30 // TerraLib
31 #include "FileWatcher.h"
32 #include "../Exception.h"
33 #include "FileSystem.h"
34 
35 // STL
36 #include <chrono>
37 
38 te::core::FileWatcher::FileWatcher(const std::string &path,
39  const std::function<void()> &callback)
40  : m_file(path), m_callback(callback)
41 {
42 }
43 
45 {
46  m_watcher.join();
47 }
48 
50 {
51  m_watcher = std::thread([this]() {
52 
53  bool exists = false;
55 
56  uintmax_t fileSize = 0;
57  if(exists)
59 
60  m_running = true;
61 
62  while(m_running)
63  {
64  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
65 
66  bool tempExists = te::core::FileSystem::exists(m_file);
67  if(tempExists != exists)
68  {
69  exists = tempExists;
70  std::lock_guard<std::mutex> lock(m_mutex);
71  m_callback();
72  continue;
73  }
74 
75  if(!exists)
76  continue;
77 
78  uintmax_t tempSize = te::core::FileSystem::fileSize(m_file);
79  if(fileSize != tempSize)
80  {
81  fileSize = tempSize;
82  std::lock_guard<std::mutex> lock(m_mutex);
83  m_callback();
84  continue;
85  }
86  }
87 
88  });
89 }
90 
92 {
93  m_running = false;
94 }
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
~FileWatcher()
Destructor.
Definition: FileWatcher.cpp:44
void watch()
Starts a thread that will monitor the given file and call the previously given callback function...
Definition: FileWatcher.cpp:49
std::function< void()> m_callback
Definition: FileWatcher.h:84
A class for handling system files and paths.
This class is designed to watch a file.
std::thread m_watcher
Definition: FileWatcher.h:85
std::string m_file
Definition: FileWatcher.h:83
void unwatch()
Stops the thread watching the file.
Definition: FileWatcher.cpp:91
static uintmax_t fileSize(const std::string &path)
Returns the file size of a FILE in UTF-8.
Definition: FileSystem.cpp:195
FileWatcher(const std::string &path, const std::function< void()> &callback)
It initializes a new FileWatcher from a given path and callback function.
Definition: FileWatcher.cpp:38