/[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 1726 - (show annotations) (download)
Sat Apr 26 09:27:24 2008 UTC (11 years, 9 months ago) by persson
File size: 5420 byte(s)
* more gcc 4.3 fixes

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

  ViewVC Help
Powered by ViewVC