1 |
iliev |
1717 |
/*************************************************************************** |
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 |
persson |
1726 |
#include <memory> |
25 |
iliev |
1717 |
#include <string> |
26 |
|
|
#include <vector> |
27 |
|
|
|
28 |
|
|
#include "Mutex.h" |
29 |
|
|
|
30 |
|
|
#if WIN32 |
31 |
|
|
|
32 |
senoner |
1910 |
/* |
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 S_IFMT 0170000 |
81 |
|
|
#define S_IFDIR 0040000 |
82 |
|
|
#define S_IFREG 0100000 |
83 |
|
|
|
84 |
|
|
#define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask)) |
85 |
|
|
#define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR) |
86 |
|
|
#define S_ISREG(mode) __S_ISTYPE((mode), S_IFREG) |
87 |
|
|
|
88 |
|
|
#define DT_DIR 4 |
89 |
|
|
#define DT_REG 8 |
90 |
|
|
|
91 |
|
|
int stat(const char *path, struct stat *buf); |
92 |
|
|
|
93 |
|
|
#define FTW_F 0x01 |
94 |
|
|
#define FTW_D 0x02 |
95 |
|
|
#define FTW_DNR 0x03 |
96 |
|
|
#define FTW_NS 0x04 |
97 |
|
|
#define FTW_SL 0x05 |
98 |
|
|
|
99 |
|
|
#ifdef __cplusplus |
100 |
|
|
} |
101 |
|
|
#endif |
102 |
|
|
|
103 |
|
|
#endif |
104 |
|
|
/* end of POSIX WIN32 Directory Browsing implementation */ |
105 |
|
|
|
106 |
iliev |
1717 |
#else |
107 |
|
|
#include <sys/stat.h> |
108 |
|
|
#endif |
109 |
|
|
|
110 |
|
|
namespace LinuxSampler { |
111 |
|
|
|
112 |
|
|
typedef std::auto_ptr<std::vector<std::string> > FileListPtr; |
113 |
|
|
|
114 |
|
|
class File { |
115 |
|
|
public: |
116 |
|
|
class DirectoryWalker { |
117 |
|
|
public: |
118 |
|
|
virtual void DirectoryEntry(std::string Path) = 0; |
119 |
|
|
virtual void FileEntry(std::string Path) = 0; |
120 |
|
|
}; |
121 |
|
|
|
122 |
|
|
File(std::string FileName); |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* Tests whether the file exists. |
126 |
|
|
*/ |
127 |
|
|
bool Exist(); |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* Provides appropriate error message if failed to retrieve |
131 |
|
|
* information about the specified file, in which case Exist() returns false. |
132 |
|
|
*/ |
133 |
|
|
std::string GetErrorMsg(); |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* Tests whether it's a regular file. |
137 |
|
|
*/ |
138 |
|
|
bool IsFile(); |
139 |
|
|
|
140 |
|
|
/** |
141 |
|
|
* Tests whether it's a directory. |
142 |
|
|
*/ |
143 |
|
|
bool IsDirectory(); |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Returns the size of the file in bytes. |
147 |
|
|
*/ |
148 |
|
|
unsigned long GetSize(); |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* Returns the names of the regular files in the specified directory. |
152 |
|
|
* @throws Exception If failed to list the directory content. |
153 |
|
|
*/ |
154 |
|
|
static FileListPtr GetFiles(std::string Dir); |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* Walks through the directory tree that is located under the |
158 |
|
|
* directory <b>Dir</b>, and calls <b>pWalker->DirectoryEntry()</b> |
159 |
|
|
* once for each directory in the tree and <b>pWalker->FileEntry()</b> |
160 |
|
|
* once for each file in the tree. Note that this method can be |
161 |
|
|
* recursively invoked from within the callback functions |
162 |
|
|
* <b>pWalker->DirectoryEntry()</b> and <b>pWalker->FileEntry()</b>. |
163 |
|
|
* @throws Exception If the specified path is missing or is not a directory. |
164 |
|
|
* Exception is also thrown, and the directory tree walk is stopped, |
165 |
|
|
* if <b>pWalker->DirectoryEntry()</b> or <b>pWalker->FileEntry()</b> |
166 |
|
|
* throws Exception. |
167 |
|
|
*/ |
168 |
|
|
static void WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker); |
169 |
iliev |
1781 |
|
170 |
iliev |
1717 |
static char DirSeparator; |
171 |
|
|
|
172 |
|
|
private: |
173 |
|
|
bool bExist; |
174 |
|
|
std::string ErrorMsg; |
175 |
|
|
static Mutex DirectoryWalkerMutex; |
176 |
|
|
static std::vector<DirectoryWalker*> DirectoryWalkers; |
177 |
|
|
static std::string DWErrorMsg; |
178 |
|
|
|
179 |
|
|
struct stat Status; |
180 |
|
|
static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag); |
181 |
|
|
}; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
#endif |