| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: unix/dir.cpp |
| 3 | // Purpose: wxDir implementation for Unix/POSIX systems |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 08.12.99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #ifndef WX_PRECOMP |
| 28 | #include "wx/intl.h" |
| 29 | #include "wx/log.h" |
| 30 | #endif // PCH |
| 31 | |
| 32 | #include "wx/dir.h" |
| 33 | #include "wx/filefn.h" // for wxMatchWild |
| 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | #include <dirent.h> |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // macros |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | #define M_DIR ((wxDirData *)m_data) |
| 46 | |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | // private classes |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
| 51 | // this class stores everything we need to enumerate the files |
| 52 | class wxDirData |
| 53 | { |
| 54 | public: |
| 55 | wxDirData(const wxString& dirname); |
| 56 | ~wxDirData(); |
| 57 | |
| 58 | bool IsOk() const { return m_dir != NULL; } |
| 59 | |
| 60 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } |
| 61 | void SetFlags(int flags) { m_flags = flags; } |
| 62 | |
| 63 | void Rewind() { rewinddir(m_dir); } |
| 64 | bool Read(wxString *filename); |
| 65 | |
| 66 | const wxString& GetName() const { return m_dirname; } |
| 67 | |
| 68 | private: |
| 69 | DIR *m_dir; |
| 70 | |
| 71 | wxString m_dirname; |
| 72 | wxString m_filespec; |
| 73 | |
| 74 | int m_flags; |
| 75 | }; |
| 76 | |
| 77 | // ============================================================================ |
| 78 | // implementation |
| 79 | // ============================================================================ |
| 80 | |
| 81 | // ---------------------------------------------------------------------------- |
| 82 | // wxDirData |
| 83 | // ---------------------------------------------------------------------------- |
| 84 | |
| 85 | #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 ) |
| 86 | |
| 87 | wxDirData::wxDirData(const wxString& dirname) |
| 88 | : m_dirname(dirname) |
| 89 | { |
| 90 | m_dir = NULL; |
| 91 | |
| 92 | // throw away the trailing slashes |
| 93 | size_t n = m_dirname.length(); |
| 94 | wxCHECK_RET( n, _T("empty dir name in wxDir") ); |
| 95 | |
| 96 | while ( n > 0 && m_dirname[--n] == '/' ) |
| 97 | ; |
| 98 | |
| 99 | m_dirname.Truncate(n + 1); |
| 100 | |
| 101 | // do open the dir |
| 102 | m_dir = opendir(m_dirname.fn_str()); |
| 103 | } |
| 104 | |
| 105 | wxDirData::~wxDirData() |
| 106 | { |
| 107 | if ( m_dir ) |
| 108 | { |
| 109 | if ( closedir(m_dir) != 0 ) |
| 110 | { |
| 111 | wxLogLastError(_T("closedir")); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | bool wxDirData::Read(wxString *filename) |
| 117 | { |
| 118 | dirent *de = (dirent *)NULL; // just to silence compiler warnings |
| 119 | bool matches = false; |
| 120 | |
| 121 | // speed up string concatenation in the loop a bit |
| 122 | wxString path = m_dirname; |
| 123 | path += _T('/'); |
| 124 | path.reserve(path.length() + 255); |
| 125 | |
| 126 | wxString de_d_name; |
| 127 | |
| 128 | while ( !matches ) |
| 129 | { |
| 130 | de = readdir(m_dir); |
| 131 | if ( !de ) |
| 132 | return false; |
| 133 | |
| 134 | #if wxUSE_UNICODE |
| 135 | de_d_name = wxConvFileName->cMB2WC( de->d_name ); |
| 136 | #else |
| 137 | de_d_name = de->d_name; |
| 138 | #endif |
| 139 | |
| 140 | // don't return "." and ".." unless asked for |
| 141 | if ( de->d_name[0] == '.' && |
| 142 | ((de->d_name[1] == '.' && de->d_name[2] == '\0') || |
| 143 | (de->d_name[1] == '\0')) ) |
| 144 | { |
| 145 | if ( !(m_flags & wxDIR_DOTDOT) ) |
| 146 | continue; |
| 147 | |
| 148 | // we found a valid match |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | // check the type now |
| 153 | if ( !(m_flags & wxDIR_FILES) && !wxDir::Exists(path + de_d_name) ) |
| 154 | { |
| 155 | // it's a file, but we don't want them |
| 156 | continue; |
| 157 | } |
| 158 | else if ( !(m_flags & wxDIR_DIRS) && wxDir::Exists(path + de_d_name) ) |
| 159 | { |
| 160 | // it's a dir, and we don't want it |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | // finally, check the name |
| 165 | if ( m_filespec.empty() ) |
| 166 | { |
| 167 | matches = m_flags & wxDIR_HIDDEN ? true : de->d_name[0] != '.'; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | // test against the pattern |
| 172 | matches = wxMatchWild(m_filespec, de_d_name, |
| 173 | !(m_flags & wxDIR_HIDDEN)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | *filename = de_d_name; |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | #else // old VMS (TODO) |
| 183 | |
| 184 | wxDirData::wxDirData(const wxString& WXUNUSED(dirname)) |
| 185 | { |
| 186 | wxFAIL_MSG(_T("not implemented")); |
| 187 | } |
| 188 | |
| 189 | wxDirData::~wxDirData() |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | bool wxDirData::Read(wxString * WXUNUSED(filename)) |
| 194 | { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | #endif // not or new VMS/old VMS |
| 199 | |
| 200 | // ---------------------------------------------------------------------------- |
| 201 | // wxDir helpers |
| 202 | // ---------------------------------------------------------------------------- |
| 203 | |
| 204 | /* static */ |
| 205 | bool wxDir::Exists(const wxString& dir) |
| 206 | { |
| 207 | return wxDirExists(dir); |
| 208 | } |
| 209 | |
| 210 | // ---------------------------------------------------------------------------- |
| 211 | // wxDir construction/destruction |
| 212 | // ---------------------------------------------------------------------------- |
| 213 | |
| 214 | wxDir::wxDir(const wxString& dirname) |
| 215 | { |
| 216 | m_data = NULL; |
| 217 | |
| 218 | (void)Open(dirname); |
| 219 | } |
| 220 | |
| 221 | bool wxDir::Open(const wxString& dirname) |
| 222 | { |
| 223 | delete M_DIR; |
| 224 | m_data = new wxDirData(dirname); |
| 225 | |
| 226 | if ( !M_DIR->IsOk() ) |
| 227 | { |
| 228 | wxLogSysError(_("Can not enumerate files in directory '%s'"), |
| 229 | dirname.c_str()); |
| 230 | |
| 231 | delete M_DIR; |
| 232 | m_data = NULL; |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | bool wxDir::IsOpened() const |
| 241 | { |
| 242 | return m_data != NULL; |
| 243 | } |
| 244 | |
| 245 | wxString wxDir::GetName() const |
| 246 | { |
| 247 | wxString name; |
| 248 | if ( m_data ) |
| 249 | { |
| 250 | name = M_DIR->GetName(); |
| 251 | if ( !name.empty() && (name.Last() == _T('/')) ) |
| 252 | { |
| 253 | // chop off the last (back)slash |
| 254 | name.Truncate(name.length() - 1); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return name; |
| 259 | } |
| 260 | |
| 261 | wxDir::~wxDir() |
| 262 | { |
| 263 | delete M_DIR; |
| 264 | } |
| 265 | |
| 266 | // ---------------------------------------------------------------------------- |
| 267 | // wxDir enumerating |
| 268 | // ---------------------------------------------------------------------------- |
| 269 | |
| 270 | bool wxDir::GetFirst(wxString *filename, |
| 271 | const wxString& filespec, |
| 272 | int flags) const |
| 273 | { |
| 274 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
| 275 | |
| 276 | M_DIR->Rewind(); |
| 277 | |
| 278 | M_DIR->SetFileSpec(filespec); |
| 279 | M_DIR->SetFlags(flags); |
| 280 | |
| 281 | return GetNext(filename); |
| 282 | } |
| 283 | |
| 284 | bool wxDir::GetNext(wxString *filename) const |
| 285 | { |
| 286 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
| 287 | |
| 288 | wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); |
| 289 | |
| 290 | return M_DIR->Read(filename); |
| 291 | } |
| 292 | |
| 293 | bool wxDir::HasSubDirs(const wxString& spec) |
| 294 | { |
| 295 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
| 296 | |
| 297 | if ( spec.empty() ) |
| 298 | { |
| 299 | // faster check for presence of any subdirectory: normally each subdir |
| 300 | // has a hard link to the parent directory and so, knowing that there |
| 301 | // are at least "." and "..", we have a subdirectory if and only if |
| 302 | // links number is > 2 - this is just a guess but it works fairly well |
| 303 | // in practice |
| 304 | // |
| 305 | // note that we may guess wrongly in one direction only: i.e. we may |
| 306 | // return true when there are no subdirectories but this is ok as the |
| 307 | // caller will learn it soon enough when it calls GetFirst(wxDIR) |
| 308 | // anyhow |
| 309 | wxStructStat stBuf; |
| 310 | if ( wxStat(M_DIR->GetName().c_str(), &stBuf) == 0 ) |
| 311 | { |
| 312 | switch ( stBuf.st_nlink ) |
| 313 | { |
| 314 | case 2: |
| 315 | // just "." and ".." |
| 316 | return false; |
| 317 | |
| 318 | case 0: |
| 319 | case 1: |
| 320 | // weird filesystem, don't try to guess for it, use dumb |
| 321 | // method below |
| 322 | break; |
| 323 | |
| 324 | default: |
| 325 | // assume we have subdirs - may turn out to be wrong if we |
| 326 | // have other hard links to this directory but it's not |
| 327 | // that bad as explained above |
| 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // just try to find first directory |
| 334 | wxString s; |
| 335 | return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN); |
| 336 | } |
| 337 | |