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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1781 - (hide annotations) (download)
Mon Sep 29 18:21:21 2008 UTC (15 years, 6 months ago) by iliev
File size: 5975 byte(s)
* Implemented option for adding instruments in separate directories
  in the instruments database
  (patch by Chris Cherrett & Andrew Williams, a bit adjusted)

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     #include "File.h"
22    
23 persson 1726 #include <cstring>
24 iliev 1717 #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 iliev 1781 std::string File::basename(std::string path) {
173     size_t begin = 0;
174     size_t end = path.length();
175    
176     for(size_t i=0; i<path.length(); ++i){
177     if(path[i] == File::DirSeparator) begin = i+1;
178     }
179     return path.substr(begin, end).c_str();
180     }
181    
182     std::string File::basename(std::string path, std::string sep) {
183     std::string tmp = File::basename(path);
184     if(sep.empty())
185     sep = ".";
186     size_t begin = 0;
187     size_t end = tmp.length();
188     size_t lastdot = tmp.find_last_of(sep)-1;
189     if(lastdot > 0)
190     end = lastdot;
191     return tmp.substr(begin, end).c_str();
192     }
193 iliev 1717 }

  ViewVC Help
Powered by ViewVC