1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 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 |
|
30 |
#if WIN32 |
31 |
|
32 |
/* |
33 |
dirent.h |
34 |
POSIX WIN32 Directory Browsing parts |
35 |
Copyright (C) 2005 OpenAsthra |
36 |
blogs.openasthra AT gmail.com |
37 |
modifications copyright 2009 by Benno Senoner |
38 |
Licence: LGPL |
39 |
*/ |
40 |
|
41 |
#ifndef _DIRENT_H_ |
42 |
#define _DIRENT_H_ |
43 |
|
44 |
#ifdef __cplusplus |
45 |
extern "C" |
46 |
{ |
47 |
#endif |
48 |
|
49 |
typedef struct DIR DIR; |
50 |
|
51 |
struct dirent{ |
52 |
long d_ino; |
53 |
long d_off; |
54 |
unsigned short d_reclen; |
55 |
unsigned char d_type; |
56 |
unsigned short d_namlen; |
57 |
char d_name[1]; |
58 |
}; |
59 |
|
60 |
DIR *opendir(const char *); |
61 |
int closedir(DIR *); |
62 |
struct dirent *readdir(DIR *); |
63 |
void rewinddir(DIR *); |
64 |
|
65 |
void seekdir (DIR *dirp, long int pos); |
66 |
long int telldir (DIR *dirp); |
67 |
|
68 |
int scandir(const char *dir, struct dirent ***namelist, |
69 |
int (*filter)(const struct dirent *), |
70 |
int (*compar)(const struct dirent **, const struct dirent **)); |
71 |
|
72 |
int ftw(const char *dirpath, |
73 |
int (*fn) (const char *fpath, const struct stat *sb, |
74 |
int typeflag), |
75 |
int nopenfd); |
76 |
|
77 |
|
78 |
int dirfd(DIR *dirp); |
79 |
|
80 |
#define DT_DIR 4 |
81 |
#define DT_REG 8 |
82 |
|
83 |
#define FTW_F 0x01 |
84 |
#define FTW_D 0x02 |
85 |
#define FTW_DNR 0x03 |
86 |
#define FTW_NS 0x04 |
87 |
#define FTW_SL 0x05 |
88 |
|
89 |
#ifdef __cplusplus |
90 |
} |
91 |
#endif |
92 |
|
93 |
#endif |
94 |
/* end of POSIX WIN32 Directory Browsing implementation */ |
95 |
|
96 |
#else |
97 |
#include <sys/stat.h> |
98 |
#endif |
99 |
|
100 |
namespace LinuxSampler { |
101 |
|
102 |
typedef std::auto_ptr<std::vector<std::string> > FileListPtr; |
103 |
|
104 |
class File { |
105 |
public: |
106 |
class DirectoryWalker { |
107 |
public: |
108 |
virtual void DirectoryEntry(std::string Path) = 0; |
109 |
virtual void FileEntry(std::string Path) = 0; |
110 |
}; |
111 |
|
112 |
File(std::string FileName); |
113 |
|
114 |
/** |
115 |
* Tests whether the file exists. |
116 |
*/ |
117 |
bool Exist(); |
118 |
|
119 |
/** |
120 |
* Provides appropriate error message if failed to retrieve |
121 |
* information about the specified file, in which case Exist() returns false. |
122 |
*/ |
123 |
std::string GetErrorMsg(); |
124 |
|
125 |
/** |
126 |
* Tests whether it's a regular file. |
127 |
*/ |
128 |
bool IsFile(); |
129 |
|
130 |
/** |
131 |
* Tests whether it's a directory. |
132 |
*/ |
133 |
bool IsDirectory(); |
134 |
|
135 |
/** |
136 |
* Returns the size of the file in bytes. |
137 |
*/ |
138 |
unsigned long GetSize(); |
139 |
|
140 |
/** |
141 |
* Returns the names of the regular files in the specified directory. |
142 |
* @throws Exception If failed to list the directory content. |
143 |
*/ |
144 |
static FileListPtr GetFiles(std::string Dir); |
145 |
|
146 |
/** |
147 |
* Walks through the directory tree that is located under the |
148 |
* directory <b>Dir</b>, and calls <b>pWalker->DirectoryEntry()</b> |
149 |
* once for each directory in the tree and <b>pWalker->FileEntry()</b> |
150 |
* once for each file in the tree. Note that this method can be |
151 |
* recursively invoked from within the callback functions |
152 |
* <b>pWalker->DirectoryEntry()</b> and <b>pWalker->FileEntry()</b>. |
153 |
* @throws Exception If the specified path is missing or is not a directory. |
154 |
* Exception is also thrown, and the directory tree walk is stopped, |
155 |
* if <b>pWalker->DirectoryEntry()</b> or <b>pWalker->FileEntry()</b> |
156 |
* throws Exception. |
157 |
*/ |
158 |
static void WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker); |
159 |
|
160 |
static char DirSeparator; |
161 |
|
162 |
private: |
163 |
bool bExist; |
164 |
std::string ErrorMsg; |
165 |
static Mutex DirectoryWalkerMutex; |
166 |
static std::vector<DirectoryWalker*> DirectoryWalkers; |
167 |
static std::string DWErrorMsg; |
168 |
|
169 |
struct stat Status; |
170 |
static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag); |
171 |
}; |
172 |
} |
173 |
|
174 |
#endif |