| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/dir.cpp |
| 3 | // Purpose: wxDir implementation for Mac |
| 4 | // Author: Stefan Csomor |
| 5 | // Modified by: |
| 6 | // Created: 08.12.99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch> |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "dir.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/intl.h" |
| 33 | #include "wx/log.h" |
| 34 | #endif // PCH |
| 35 | |
| 36 | #include "wx/dir.h" |
| 37 | #include "wx/filefn.h" // for wxPathExists() |
| 38 | |
| 39 | #ifndef __WXMAC_X__ |
| 40 | #include <windows.h> |
| 41 | #endif |
| 42 | |
| 43 | #if defined(__WXMAC__) && !defined(__UNIX__) |
| 44 | #include "morefile.h" |
| 45 | #include "moreextr.h" |
| 46 | #include "fullpath.h" |
| 47 | #include "fspcompa.h" |
| 48 | #endif |
| 49 | |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | // constants |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | |
| 54 | #ifndef MAX_PATH |
| 55 | #define MAX_PATH 260 // from VC++ headers |
| 56 | #endif |
| 57 | |
| 58 | // ---------------------------------------------------------------------------- |
| 59 | // macros |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | #define M_DIR ((wxDirData *)m_data) |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | // private classes |
| 66 | // ---------------------------------------------------------------------------- |
| 67 | |
| 68 | // this class stores everything we need to enumerate the files |
| 69 | class wxDirData |
| 70 | { |
| 71 | public: |
| 72 | wxDirData(const wxString& dirname); |
| 73 | ~wxDirData(); |
| 74 | |
| 75 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } |
| 76 | void SetFlags(int flags) { m_flags = flags; } |
| 77 | |
| 78 | bool Read(wxString *filename); // reads the next |
| 79 | void Rewind() ; |
| 80 | |
| 81 | private: |
| 82 | CInfoPBRec m_CPB ; |
| 83 | wxInt16 m_index ; |
| 84 | long m_dirId ; |
| 85 | Str255 m_name ; |
| 86 | Boolean m_isDir ; |
| 87 | |
| 88 | wxString m_dirname; |
| 89 | wxString m_filespec; |
| 90 | |
| 91 | int m_flags; |
| 92 | }; |
| 93 | |
| 94 | // ============================================================================ |
| 95 | // implementation |
| 96 | // ============================================================================ |
| 97 | |
| 98 | // ---------------------------------------------------------------------------- |
| 99 | // wxDirData |
| 100 | // ---------------------------------------------------------------------------- |
| 101 | |
| 102 | wxDirData::wxDirData(const wxString& dirname) |
| 103 | : m_dirname(dirname) |
| 104 | { |
| 105 | // throw away the trailing slashes |
| 106 | size_t n = m_dirname.length(); |
| 107 | wxCHECK_RET( n, _T("empty dir name in wxDir") ); |
| 108 | |
| 109 | while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) ) |
| 110 | ; |
| 111 | |
| 112 | m_dirname.Truncate(n + 1); |
| 113 | |
| 114 | FSSpec fsspec ; |
| 115 | |
| 116 | wxUnixFilename2FSSpec( m_dirname , &fsspec ) ; |
| 117 | m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ; |
| 118 | m_CPB.hFileInfo.ioNamePtr = m_name ; |
| 119 | m_index = 0 ; |
| 120 | |
| 121 | #ifdef __WXMAC_X__ |
| 122 | // TODO: what are we supposed to do for Mac OS X |
| 123 | #else |
| 124 | FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ; |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | wxDirData::~wxDirData() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | void wxDirData::Rewind() |
| 133 | { |
| 134 | m_index = 0 ; |
| 135 | } |
| 136 | |
| 137 | bool wxDirData::Read(wxString *filename) |
| 138 | { |
| 139 | if ( !m_isDir ) |
| 140 | return FALSE ; |
| 141 | |
| 142 | #if TARGET_CARBON |
| 143 | char c_name[256] ; |
| 144 | #endif |
| 145 | wxString result; |
| 146 | |
| 147 | short err = noErr ; |
| 148 | |
| 149 | while ( err == noErr ) |
| 150 | { |
| 151 | m_index++ ; |
| 152 | m_CPB.dirInfo.ioFDirIndex = m_index; |
| 153 | m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */ |
| 154 | err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB); |
| 155 | if ( err != noErr ) |
| 156 | break ; |
| 157 | |
| 158 | #if TARGET_CARBON |
| 159 | p2cstrcpy( c_name, m_name ) ; |
| 160 | strcpy( (char *)m_name, c_name); |
| 161 | #else |
| 162 | p2cstr( m_name ) ; |
| 163 | #endif |
| 164 | if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory |
| 165 | break ; |
| 166 | |
| 167 | if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it |
| 168 | continue ; |
| 169 | |
| 170 | if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it |
| 171 | continue ; |
| 172 | |
| 173 | wxString file( m_name ) ; |
| 174 | if ( m_filespec.IsEmpty() || m_filespec == "*.*" ) |
| 175 | { |
| 176 | } |
| 177 | else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" ) |
| 178 | { |
| 179 | if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() ) |
| 180 | { |
| 181 | continue ; |
| 182 | } |
| 183 | } |
| 184 | else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" ) |
| 185 | { |
| 186 | if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() ) |
| 187 | { |
| 188 | continue ; |
| 189 | } |
| 190 | } |
| 191 | else if ( file.Upper() != m_filespec.Upper() ) |
| 192 | { |
| 193 | continue ; |
| 194 | } |
| 195 | |
| 196 | break ; |
| 197 | } |
| 198 | if ( err != noErr ) |
| 199 | { |
| 200 | return FALSE ; |
| 201 | } |
| 202 | |
| 203 | *filename = (char*) m_name ; |
| 204 | |
| 205 | return TRUE; |
| 206 | } |
| 207 | |
| 208 | // ---------------------------------------------------------------------------- |
| 209 | // wxDir helpers |
| 210 | // ---------------------------------------------------------------------------- |
| 211 | |
| 212 | /* static */ |
| 213 | bool wxDir::Exists(const wxString& dir) |
| 214 | { |
| 215 | return wxPathExists(dir); |
| 216 | } |
| 217 | |
| 218 | // ---------------------------------------------------------------------------- |
| 219 | // wxDir construction/destruction |
| 220 | // ---------------------------------------------------------------------------- |
| 221 | |
| 222 | wxDir::wxDir(const wxString& dirname) |
| 223 | { |
| 224 | m_data = NULL; |
| 225 | |
| 226 | (void)Open(dirname); |
| 227 | } |
| 228 | |
| 229 | bool wxDir::Open(const wxString& dirname) |
| 230 | { |
| 231 | delete M_DIR; |
| 232 | m_data = new wxDirData(dirname); |
| 233 | |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | bool wxDir::IsOpened() const |
| 238 | { |
| 239 | return m_data != NULL; |
| 240 | } |
| 241 | |
| 242 | wxDir::~wxDir() |
| 243 | { |
| 244 | delete M_DIR; |
| 245 | } |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // wxDir enumerating |
| 249 | // ---------------------------------------------------------------------------- |
| 250 | |
| 251 | bool wxDir::GetFirst(wxString *filename, |
| 252 | const wxString& filespec, |
| 253 | int flags) const |
| 254 | { |
| 255 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); |
| 256 | |
| 257 | M_DIR->Rewind(); |
| 258 | |
| 259 | M_DIR->SetFileSpec(filespec); |
| 260 | M_DIR->SetFlags(flags); |
| 261 | |
| 262 | return GetNext(filename); |
| 263 | } |
| 264 | |
| 265 | bool wxDir::GetNext(wxString *filename) const |
| 266 | { |
| 267 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); |
| 268 | |
| 269 | wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") ); |
| 270 | |
| 271 | return M_DIR->Read(filename); |
| 272 | } |