/[svn]/linuxsampler/trunk/src/common/File.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/File.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1717 - (show annotations) (download)
Sun Mar 16 17:43:20 2008 UTC (16 years, 1 month ago) by iliev
File size: 5401 byte(s)
* moved all OS dependent file operation to File class

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 #include "File.h"
22
23 #include <errno.h>
24 #include <sstream>
25
26 #include "Exception.h"
27 #include "global_private.h"
28
29 #if WIN32
30
31 #else
32 #include <dirent.h>
33 #include <ftw.h>
34 #endif
35
36 namespace LinuxSampler {
37
38 #if WIN32
39 char File::DirSeparator = '\\';
40 #else
41 char File::DirSeparator = '/';
42 #endif
43
44 Mutex File::DirectoryWalkerMutex = Mutex();
45 std::vector<File::DirectoryWalker*> File::DirectoryWalkers;
46 std::string File::DWErrorMsg;
47
48 File::File(std::string Path) {
49 #if WIN32
50
51 #else
52 bExist = !stat(Path.c_str(), &Status);
53 if(!bExist) ErrorMsg = strerror(errno);
54 #endif
55 }
56
57 bool File::Exist() {
58 return bExist;
59 }
60
61 std::string File::GetErrorMsg() {
62 return ErrorMsg;
63 }
64
65 bool File::IsFile() {
66 if(!Exist()) return false;
67
68 #if WIN32
69
70 #else
71 return S_ISREG(Status.st_mode);
72 #endif
73 }
74
75 bool File::IsDirectory() {
76 if(!Exist()) return false;
77
78 #if WIN32
79
80 #else
81 return S_ISDIR(Status.st_mode);
82 #endif
83 }
84
85 unsigned long File::GetSize() {
86 if(!Exist()) return 0;
87
88 #if WIN32
89
90 #else
91 return Status.st_size;
92 #endif
93 }
94
95 FileListPtr File::GetFiles(std::string Dir) {
96 #if WIN32
97
98 #else
99 DIR* pDir = opendir(Dir.c_str());
100 if (pDir == NULL) {
101 std::stringstream ss;
102 ss << "Failed to list the directory content of `";
103 ss << Dir << "`: " << strerror(errno);
104 throw Exception(ss.str());
105 }
106
107 FileListPtr fileList(new std::vector<std::string>);
108
109 struct dirent* pEnt = readdir(pDir);
110 while (pEnt != NULL) {
111 if (pEnt->d_type != DT_REG) {
112 pEnt = readdir(pDir);
113 continue;
114 }
115
116 fileList->push_back(std::string(pEnt->d_name));
117 pEnt = readdir(pDir);
118 }
119
120 if (closedir(pDir)) {
121 std::stringstream ss;
122 ss << "Failed to close directory `" << Dir << "`: ";
123 ss << strerror(errno);
124 throw Exception(ss.str());
125 }
126
127 return fileList;
128 #endif
129 }
130
131 void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) {
132 dmsg(2,("File: WalkDirectoryTree(Dir=%s)\n", Dir.c_str()));
133 File f = File(Dir);
134 if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg());
135 if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir);
136
137 DirectoryWalkerMutex.Lock();
138 DirectoryWalkers.push_back(pWalker);
139 DWErrorMsg = "Failed to process directory tree: " + Dir;
140
141 #if WIN32
142
143 #else
144 if (ftw(Dir.c_str(), FtwCallback, 10)) {
145 DirectoryWalkers.pop_back();
146 if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();
147 throw Exception(DWErrorMsg);
148 }
149 #endif
150 DirectoryWalkers.pop_back();
151 if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();
152 }
153
154 #if WIN32
155
156 #else
157 int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) {
158 dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath));
159 try {
160 if (typeflag == FTW_D) DirectoryWalkers.back()->DirectoryEntry(std::string(fpath));
161 else if (typeflag == FTW_F) DirectoryWalkers.back()->FileEntry(std::string(fpath));
162 } catch(Exception e) {
163 e.PrintMessage();
164 DWErrorMsg = e.Message();
165 return -1;
166 }
167
168 return 0;
169 };
170 #endif
171 }

  ViewVC Help
Powered by ViewVC