NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
filewatcher.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2017 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "filewatcher.hpp"
20std::string replacePathSeparator(const std::string&);
21
22
23bool Filewatcher::isDefaultPath(const wxFileName& path)
24{
25 std::string sPath = replacePathSeparator(path.GetPath().ToStdString());
26 for (size_t i = 2; i < vDefaultPaths.size(); i++)
27 {
28 if (sPath.find(vDefaultPaths[i]) != std::string::npos)
29 return true;
30 }
31 return false;
32}
33
34bool Filewatcher::SetDefaultPaths(const std::vector<std::string>& vPaths)
35{
36 vDefaultPaths = vPaths;
37 if (this->GetWatchedPathsCount())
38 this->RemoveAll();
39
40 for (size_t i = 2; i < vDefaultPaths.size(); i++)
41 this->AddTree(wxFileName(vDefaultPaths[i] + "/"), wxFSW_EVENT_ALL, "*");
42
43 for (auto iter = mWatchedFiles.begin(); iter != mWatchedFiles.end(); ++iter)
44 {
45 wxFileSystemWatcher::Add(wxFileName(iter->second + "/"));
46 }
47 return true;
48}
49
50bool Filewatcher::Add(const wxFileName& path, int events)
51{
52 if (isDefaultPath(path))
53 return false;
54 if (mWatchedFiles.find(path.GetFullPath()) != mWatchedFiles.end())
55 return false;
56 for (auto iter = mWatchedFiles.begin(); iter != mWatchedFiles.end(); ++iter)
57 {
58 if (iter->second == path.GetPath())
59 {
60 mWatchedFiles[path.GetFullPath()] = path.GetPath();
61 return true;
62 }
63 }
64 mWatchedFiles[path.GetFullPath()] = path.GetPath();
65 return wxFileSystemWatcher::Add(wxFileName(path.GetPath() + "/"), events);
66}
67
68bool Filewatcher::Remove(const wxFileName& path)
69{
70 if (isDefaultPath(path))
71 return false;
72 if (mWatchedFiles.find(path.GetFullPath()) == mWatchedFiles.end())
73 return false;
74 auto iter_found = mWatchedFiles.find(path.GetFullPath());
75 for (auto iter = mWatchedFiles.begin(); iter != mWatchedFiles.end(); ++iter)
76 {
77 if (iter == iter_found)
78 continue;
79 if (iter->second == path.GetPath())
80 {
81 mWatchedFiles.erase(path.GetFullPath());
82 return true;
83 }
84 }
85 mWatchedFiles.erase(path.GetFullPath());
86 return wxFileSystemWatcher::Remove(wxFileName(path.GetPath() + "/"));
87}
88
std::vector< std::string > vDefaultPaths
Definition: filewatcher.hpp:32
std::map< wxString, wxString > mWatchedFiles
Definition: filewatcher.hpp:33
bool isDefaultPath(const wxFileName &path)
Definition: filewatcher.cpp:23
bool Remove(const wxFileName &path)
Definition: filewatcher.cpp:68
bool Add(const wxFileName &path, int events=wxFSW_EVENT_ALL)
Definition: filewatcher.cpp:50
bool SetDefaultPaths(const std::vector< std::string > &vPaths)
Definition: filewatcher.cpp:34
std::string replacePathSeparator(const std::string &)
This function replaces the Windows style path sparators to UNIX style.