1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 - 2012 Grigor Iliev, Benno Senoner * |
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 2 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, write to the Free Software * |
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#ifndef LS_FILE_H |
22 |
#define LS_FILE_H |
23 |
|
24 |
#include <memory> |
25 |
#include <string> |
26 |
#include <vector> |
27 |
|
28 |
#include "Mutex.h" |
29 |
#include <sys/stat.h> |
30 |
|
31 |
namespace LinuxSampler { |
32 |
|
33 |
#if __cplusplus >= 201103L && !CONFIG_NO_CPP11STL |
34 |
typedef std::unique_ptr<std::vector<std::string>> FileListPtr; |
35 |
#else |
36 |
typedef std::auto_ptr<std::vector<std::string> > FileListPtr; |
37 |
#endif |
38 |
|
39 |
class File { |
40 |
public: |
41 |
class DirectoryWalker { |
42 |
public: |
43 |
virtual void DirectoryEntry(std::string Path) = 0; |
44 |
virtual void FileEntry(std::string Path) = 0; |
45 |
}; |
46 |
|
47 |
File(std::string FileName); |
48 |
|
49 |
/** |
50 |
* Tests whether the file exists. |
51 |
*/ |
52 |
bool Exist(); |
53 |
|
54 |
/** |
55 |
* Provides appropriate error message if failed to retrieve |
56 |
* information about the specified file, in which case Exist() returns false. |
57 |
*/ |
58 |
std::string GetErrorMsg(); |
59 |
|
60 |
/** |
61 |
* Tests whether it's a regular file. |
62 |
*/ |
63 |
bool IsFile(); |
64 |
|
65 |
/** |
66 |
* Tests whether it's a directory. |
67 |
*/ |
68 |
bool IsDirectory(); |
69 |
|
70 |
/** |
71 |
* Returns the size of the file in bytes. |
72 |
*/ |
73 |
unsigned long GetSize(); |
74 |
|
75 |
/** |
76 |
* Returns the names of the regular files in the specified directory. |
77 |
* @throws Exception If failed to list the directory content. |
78 |
*/ |
79 |
static FileListPtr GetFiles(std::string Dir); |
80 |
|
81 |
/** |
82 |
* Walks through the directory tree that is located under the |
83 |
* directory <b>Dir</b>, and calls <b>pWalker->DirectoryEntry()</b> |
84 |
* once for each directory in the tree and <b>pWalker->FileEntry()</b> |
85 |
* once for each file in the tree. Note that this method can be |
86 |
* recursively invoked from within the callback functions |
87 |
* <b>pWalker->DirectoryEntry()</b> and <b>pWalker->FileEntry()</b>. |
88 |
* @throws Exception If the specified path is missing or is not a directory. |
89 |
* Exception is also thrown, and the directory tree walk is stopped, |
90 |
* if <b>pWalker->DirectoryEntry()</b> or <b>pWalker->FileEntry()</b> |
91 |
* throws Exception. |
92 |
*/ |
93 |
static void WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker); |
94 |
|
95 |
static const char DirSeparator; |
96 |
static const char PathSeparator; |
97 |
|
98 |
private: |
99 |
bool bExist; |
100 |
std::string ErrorMsg; |
101 |
static Mutex DirectoryWalkerMutex; |
102 |
static std::vector<DirectoryWalker*> DirectoryWalkers; |
103 |
static std::string DWErrorMsg; |
104 |
|
105 |
struct stat Status; |
106 |
#ifdef WIN32 |
107 |
static void WalkDirectoryTreeSub(std::string Dir, DirectoryWalker* pWalker); |
108 |
#else |
109 |
static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag); |
110 |
#endif |
111 |
}; |
112 |
} |
113 |
|
114 |
#endif |