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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1782 by iliev, Tue Sep 30 02:16:41 2008 UTC revision 1943 by persson, Tue Jul 14 18:25:11 2009 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2008 Grigor Iliev, Benno Senoner                        *   *   Copyright (C) 2008 - 2009 Grigor Iliev, Benno Senoner                 *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 23  Line 23 
23  #include <cstring>  #include <cstring>
24  #include <errno.h>  #include <errno.h>
25  #include <sstream>  #include <sstream>
26    #include <sys/stat.h>
27    #include <dirent.h>
28    
29  #include "Exception.h"  #include "Exception.h"
30  #include "global_private.h"  #include "global_private.h"
31    
32  #if WIN32  #if WIN32
33    #include <windows.h>
34  #else  #else
 #include <dirent.h>  
35  #include <ftw.h>  #include <ftw.h>
36  #endif  #endif
37    
38  namespace LinuxSampler {  namespace LinuxSampler {
39    #ifdef WIN32
 #if WIN32  
40      char File::DirSeparator = '\\';      char File::DirSeparator = '\\';
41  #else  #else
42      char File::DirSeparator = '/';      char File::DirSeparator = '/';
43  #endif  #endif
44        Mutex File::DirectoryWalkerMutex;
     Mutex File::DirectoryWalkerMutex = Mutex();  
45      std::vector<File::DirectoryWalker*> File::DirectoryWalkers;      std::vector<File::DirectoryWalker*> File::DirectoryWalkers;
46      std::string File::DWErrorMsg;      std::string File::DWErrorMsg;
47    
48      File::File(std::string Path) {      File::File(std::string Path) {
         #if WIN32  
           
         #else  
49              bExist = !stat(Path.c_str(), &Status);              bExist = !stat(Path.c_str(), &Status);
50              if(!bExist) ErrorMsg = strerror(errno);              if(!bExist) ErrorMsg = strerror(errno);
         #endif  
51      }      }
52            
53      bool File::Exist() {      bool File::Exist() {
# Line 66  namespace LinuxSampler { Line 61  namespace LinuxSampler {
61      bool File::IsFile() {      bool File::IsFile() {
62          if(!Exist()) return false;          if(!Exist()) return false;
63                    
         #if WIN32  
           
         #else  
64              return S_ISREG(Status.st_mode);              return S_ISREG(Status.st_mode);
         #endif  
65      }      }
66            
67      bool File::IsDirectory() {      bool File::IsDirectory() {
68          if(!Exist()) return false;          if(!Exist()) return false;
69                    
         #if WIN32  
           
         #else  
70              return S_ISDIR(Status.st_mode);              return S_ISDIR(Status.st_mode);
         #endif  
71      }      }
72            
73      unsigned long File::GetSize() {      unsigned long File::GetSize() {
74          if(!Exist()) return 0;          if(!Exist()) return 0;
75                    
76          #if WIN32              return Status.st_size;      
           
         #else  
             return Status.st_size;  
         #endif        
77      }      }
78            
79      FileListPtr File::GetFiles(std::string Dir) {      FileListPtr File::GetFiles(std::string Dir) {
         #if WIN32  
           
         #else  
80              DIR* pDir = opendir(Dir.c_str());              DIR* pDir = opendir(Dir.c_str());
81              if (pDir == NULL) {              if (pDir == NULL) {
82                  std::stringstream ss;                  std::stringstream ss;
# Line 109  namespace LinuxSampler { Line 89  namespace LinuxSampler {
89                            
90              struct dirent* pEnt = readdir(pDir);              struct dirent* pEnt = readdir(pDir);
91              while (pEnt != NULL) {              while (pEnt != NULL) {
92                  if (pEnt->d_type != DT_REG) {  #ifdef _DIRENT_HAVE_D_TYPE
93                      pEnt = readdir(pDir);              if (pEnt->d_type == DT_REG) {
94                      continue;                  fileList->push_back(std::string(pEnt->d_name));
95                  }              }
96    #else
97                struct stat s;
98                if (stat((Dir + DirSeparator + pEnt->d_name).c_str(), &s) == 0 && S_ISREG(s.st_mode)) {
99                  fileList->push_back(std::string(pEnt->d_name));                  fileList->push_back(std::string(pEnt->d_name));
100                }
101    #endif
102                  pEnt = readdir(pDir);                  pEnt = readdir(pDir);
103              }              }
104                            
# Line 126  namespace LinuxSampler { Line 110  namespace LinuxSampler {
110              }              }
111                            
112              return fileList;              return fileList;
         #endif  
113      }      }
114            
115      void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) {      void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) {
# Line 134  namespace LinuxSampler { Line 117  namespace LinuxSampler {
117          File f = File(Dir);          File f = File(Dir);
118          if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg());          if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg());
119          if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir);          if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir);
120            #ifdef WIN32
121            WalkDirectoryTreeSub(Dir, pWalker);
122    #else
123          DirectoryWalkerMutex.Lock();          DirectoryWalkerMutex.Lock();
124          DirectoryWalkers.push_back(pWalker);          DirectoryWalkers.push_back(pWalker);
125          DWErrorMsg = "Failed to process directory tree: " + Dir;          DWErrorMsg = "Failed to process directory tree: " + Dir;
126                    
         #if WIN32  
           
         #else  
127              if (ftw(Dir.c_str(), FtwCallback, 10)) {              if (ftw(Dir.c_str(), FtwCallback, 10)) {
128                  DirectoryWalkers.pop_back();                  DirectoryWalkers.pop_back();
129                  if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();                  if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();
130                  throw Exception(DWErrorMsg);                  throw Exception(DWErrorMsg);
131              }              }
         #endif  
132          DirectoryWalkers.pop_back();          DirectoryWalkers.pop_back();
133          if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();          if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock();
134    #endif
135      }      }
136    
137  #if WIN32  #ifdef WIN32
138        void File::WalkDirectoryTreeSub(String Dir, DirectoryWalker* pWalker) {
139            dmsg(2,("File: WalkDirectoryTreeSub(Dir=%s)\n", Dir.c_str()));
140            DWORD attrs = GetFileAttributes(Dir.c_str());
141            if (attrs == INVALID_FILE_ATTRIBUTES) return;
142    
143            if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
144                pWalker->DirectoryEntry(Dir);
145    
146                std::string::size_type l = Dir.length() - 1;
147                if (Dir[l] == '/') Dir[l] = '\\';
148                else if (Dir[l] != '\\') Dir += '\\';
149    
150                WIN32_FIND_DATA fd;
151                HANDLE h = FindFirstFile((Dir + "*").c_str(), &fd);
152                if (h == INVALID_HANDLE_VALUE) return;
153                do {
154                    if (strcmp(fd.cFileName, ".") != 0 &&
155                        strcmp(fd.cFileName, "..") != 0) {
156                        WalkDirectoryTreeSub(Dir + fd.cFileName, pWalker);
157                    }
158                } while (FindNextFile(h, &fd));
159                FindClose(h);
160            } else {
161                pWalker->FileEntry(Dir);
162            }
163        }
164  #else  #else
165      int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) {      int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) {
166          dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath));          dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath));
# Line 167  namespace LinuxSampler { Line 174  namespace LinuxSampler {
174          }          }
175                    
176          return 0;          return 0;
177      };      }
178  #endif  #endif
179  }  }

Legend:
Removed from v.1782  
changed lines
  Added in v.1943

  ViewVC Help
Powered by ViewVC