unixdirectory.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // unixdirectory.cpp
  2. // 
  3. // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <dirent.h>
  13. #include <wordexp.h>
  14. #include "directory.h"
  15. using namespace std;
  16. #ifdef TARGET_OS_MAC
  17. #ifdef QT_CORE_LIB
  18. // Crash on Mac OS X / Qt4 version when calling wordfree.
  19. // This seems to happen only with Leopard.
  20. #define WORDEXP_PROBLEM
  21. #endif
  22. #endif
  23. class UnixDirectory : public Directory
  24. {
  25. public:
  26.     UnixDirectory(const std::string&);
  27.     virtual ~UnixDirectory();
  28.     virtual bool nextFile(std::string&);
  29.     enum {
  30.         DirGood = 0,
  31.         DirBad = 1
  32.     };
  33. private:
  34.     string dirname;
  35.     int status;
  36.     DIR* dir;
  37. };
  38. UnixDirectory::UnixDirectory(const std::string& _dirname) :
  39.     dirname(_dirname),
  40.     status(DirGood),
  41.     dir(NULL)
  42. {
  43. }
  44. UnixDirectory::~UnixDirectory()
  45. {
  46.     if (dir != NULL)
  47.     {
  48.         closedir(dir);
  49.         dir = NULL;
  50.     }
  51. }
  52. bool UnixDirectory::nextFile(std::string& filename)
  53. {
  54.     if (status != DirGood)
  55.         return false;
  56.     if (dir == NULL)
  57.     {
  58.         dir = opendir(dirname.c_str());
  59.         if (dir == NULL)
  60.         {
  61.             status = DirBad;
  62.             return false;
  63.         }
  64.     }
  65.     struct dirent* ent = readdir(dir);
  66.     if (ent == NULL)
  67.     {
  68.         status = DirBad;
  69.         return false;
  70.     }
  71.     else
  72.     {
  73.         filename = ent->d_name;
  74.         return true;
  75.     }
  76. }
  77. Directory* OpenDirectory(const std::string& dirname)
  78. {
  79.     return new UnixDirectory(dirname);
  80. }
  81. bool IsDirectory(const std::string& filename)
  82. {
  83.     struct stat buf;
  84.     stat(filename.c_str(), &buf);
  85.     return S_ISDIR(buf.st_mode);
  86. }
  87. std::string WordExp(const std::string& filename) 
  88. {
  89. #ifndef WORDEXP_PROBLEM   
  90.     wordexp_t result;
  91.     std::string expanded;
  92.     switch(wordexp(filename.c_str(), &result, WRDE_NOCMD)) {
  93.     case 0: // successful
  94.         break;
  95.     case WRDE_NOSPACE:
  96.             // If the error was `WRDE_NOSPACE',
  97.             // then perhaps part of the result was allocated.
  98.         wordfree(&result);
  99.     default: // some other error
  100.         return filename;
  101.     }
  102.     if (result.we_wordc != 1) {
  103.         wordfree(&result);
  104.         return filename;
  105.     }
  106.     expanded = result.we_wordv[0];
  107.     wordfree(&result);
  108. #else
  109.     std::string expanded = filename;
  110. #endif
  111.     return expanded;
  112. }