Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8224 in orxonox.OLD


Ignore:
Timestamp:
Jun 8, 2006, 10:10:51 AM (18 years ago)
Author:
bensch
Message:

gui: much better directory, now the Directory is completly read in when opening it.

Location:
branches/gui/src/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/gui/src/lib/shell/shell_completion_plugin.cc

    r7661 r8224  
    100100      dir.setFileName(ResourceManager::getInstance()->getDataDir() + this->_subDir);
    101101      dir.open();
    102       while(dir)
     102      for(unsigned int i = 0; i < dir.fileCount(); i++ )
    103103      {
    104         completionList.push_back(this->_subDir + "/" + dir.next());
     104        completionList.push_back(this->_subDir + "/" + dir[i]);
    105105      }
    106106    }
     
    118118      std::string fileName;
    119119      std::string currFile;
    120       while(dir)
     120      for(unsigned int i = 0; i < dir.fileCount(); i++)
    121121      {
    122         currFile = dir.next();
     122        currFile = dir[i];
    123123        if (currFile[0] == '.')
    124124         continue;
  • branches/gui/src/lib/util/directory.cc

    r7661 r8224  
    3838
    3939#if not defined (__WIN32__)
    40  #include <sys/stat.h>
    41  #include <sys/types.h>
     40#include <sys/types.h>
     41#include <sys/stat.h>
     42#include <dirent.h>
     43#else
     44#include <windows.h>
     45#include <winbase.h>
    4246#endif
     47
     48#include <iostream>
     49
    4350Directory::Directory(const std::string& directoryName)
    44   : File(directoryName), willfail(false)
     51    : File(directoryName)
    4552{
    46   this->handle = 0;
    47   this->willfail = true;
     53  this->_opened = false;
    4854}
    4955
    5056Directory::~Directory()
    5157{
     58  this->close();
    5259}
     60
    5361
    5462bool Directory::open()
    5563{
     64  if (this->_opened)
     65    this->close();
     66
     67  // Openes the Directory for reading:
    5668#if not defined(__WIN32__)
    57   if (this->handle != NULL)
     69  DIR* handle;
     70  if (handle != NULL)
    5871    this->close();
    59   this->handle = opendir(this->name().c_str());
     72  handle = opendir(this->name().c_str());
    6073  if (!handle)
    61     willfail = true;
     74  {
     75    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
     76    return false;
     77  }
     78#else
     79  HANDLE handle;
     80  if (handle != INVALID_HANDLE_VALUE)
     81    this->close();
     82
     83  // First check the attributes trying to access a non-Directory with
     84  // FindFirstFile takes ages
     85  DWORD attrs = GetFileAttributes(this->name().c_str());
     86  if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
     87  {
     88    return false;
     89  }
     90  std::string Full(this->name());
     91  // Circumvent a problem in FindFirstFile with c:\\* as parameter
     92  if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
     93    Full += "\\";
     94  WIN32_FIND_DATA entry;
     95  handle = FindFirstFile( (Full+"*").c_str(), &entry);
     96  if (handle == INVALID_HANDLE_VALUE)
     97  {
     98    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
     99    return false;
     100  }
    62101  else
    63102  {
    64     willfail = false;
    65     dirent* entry = readdir(handle);
    66     if (entry)
    67       current = entry->d_name;
    68     else
    69       willfail = true;
     103    this->_fileNames.push_back(entry.cFileName);
    70104  }
    71 #else
    72     if (handle != INVALID_HANDLE_VALUE)
    73       this->close();
    74 
    75     // First check the attributes trying to access a non-Directory with
    76     // FindFirstFile takes ages
    77     DWORD attrs = GetFileAttributes(this->name().c_str());
    78     if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
    79     {
    80       willfail = true;
    81       return false;
    82     }
    83     std::string Full(this->name());
    84     // Circumvent a problem in FindFirstFile with c:\\* as parameter
    85     if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
    86       Full += "\\";
    87     WIN32_FIND_DATA entry;
    88     handle = FindFirstFile( (Full+"*").c_str(), &entry);
    89     if (handle == INVALID_HANDLE_VALUE)
    90     {
    91       willfail = true;
    92       return false;
    93     }
    94     else
    95     {
    96       willfail = false;
    97       current = entry.cFileName;
    98       return true;
    99     }
    100105#endif
    101106
     107  // BUILDING the list of contained Files. (only the names)
     108#if not defined(__WIN32__)
     109  while (dirent* entry = readdir(handle))
     110    this->_fileNames.push_back(entry->d_name);
     111  closedir(handle);
     112#else
     113  WIN32_FIND_DATA entry;
     114  while (int ok = FindNextFile(handle, &entry))
     115    this->_fileNames.push_back(entry.cFileName);
     116  FindClose(handle);
     117#endif
     118  this->_opened = true;
     119  return true;
    102120}
    103121
    104122bool Directory::close()
    105123{
    106 #if not defined(__WIN32__)
    107   if (this->handle != NULL)
    108     closedir(handle);
    109   this->handle = NULL;
    110 #else
    111   if (handle != INVALID_HANDLE_VALUE)
    112     FindClose(handle);
    113     handle = 0;
    114 #endif
    115   this->willfail = true;
    116   this->current = "";
     124  this->_opened = false;
     125  this->_fileNames.clear();
    117126  return true;
    118127}
    119128
    120 
    121 
    122 std::string Directory::next()
    123 {
    124 #if not defined(__WIN32__)
    125   std::string prev(current);
    126   dirent* entry = readdir(handle);
    127   if (entry)
    128     current = entry->d_name;
    129   else
    130     willfail = true;
    131   return prev;
    132 
    133 #else
    134   std::string prev = current;
    135   WIN32_FIND_DATA entry;
    136   int ok = FindNextFile(handle, &entry);
    137   if (!ok)
    138     willfail = true;
    139   else
    140     current = entry.cFileName;
    141   return current;
    142 #endif
    143 }
    144129
    145130
  • branches/gui/src/lib/util/directory.h

    r7661 r8224  
    2727
    2828#include "file.h"
    29 
    30 #if not defined (__WIN32__)
    31  #include <sys/types.h>
    32  #include <dirent.h>
    33 #else
    34  #include <windows.h>
    35  #include <winbase.h>
    36 #endif
     29#include <vector>
    3730
    3831class Directory : public File
     
    4437  virtual bool open();
    4538  virtual bool close();
    46   operator void*() const { return willfail ? (void*)0:(void*)(-1); }
    47 
    48   std::string next();
    4939
    5040  bool create();
    5141
     42  /** @returns the FileNames contained inside of the Directory */
     43  const std::vector<std::string>& fileNames() const { return this->_fileNames; };
     44  const std::string& operator[](unsigned int fileNumber) const { return this->_fileNames[fileNumber]; };
     45  unsigned int fileCount() const { return _fileNames.size(); };
     46
     47  std::string fileNameInDir(unsigned int fileNumber) const { return this->name() + "/" + _fileNames[fileNumber]; };
     48
     49  File getFile(unsigned int fileNumber) const { return File(fileNameInDir(fileNumber)); };
     50
    5251private:
    53 #if not defined(__WIN32__)
    54   DIR* handle;
    55 #else
    56   HANDLE    handle;
    57 #endif
    58   bool willfail;
    59   std::string current;
     52  bool                        _opened;
     53  std::vector<std::string>    _fileNames;
    6054};
    6155
Note: See TracChangeset for help on using the changeset viewer.