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