Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Directory.cpp

Go to the documentation of this file.
00001 #include "OSDependent/Directory.h"
00002 
00003 Directory::Directory(const char *name) :
00004 _handle(-1L),
00005 _hasFirst(false),
00006 _isRewound(false),
00007 _name(name)
00008 {
00009 }
00010 
00011 Directory::~Directory()
00012 {
00013   close();
00014 }
00015 
00016 int
00017 Directory::open()
00018 {
00019   if (_handle != -1L)
00020     return 0;
00021   std::string name = _name;
00022   name += "\\*";
00023   _handle = ::_findfirst(name.c_str(), &_data);
00024         if (_handle == -1L)
00025     return -1;
00026   _hasFirst = true;
00027   _isRewound = false;
00028   return 0;
00029 }
00030 
00031 int
00032 Directory::close()
00033 {
00034   if (_handle == -1L)
00035     return 0;
00036   if (::_findclose(_handle) != 0)
00037     return -1;
00038   _handle = -1L;
00039   return 0;
00040 }
00041 
00042 int
00043 Directory::rewind()
00044 {
00045         if (_handle == -1L)
00046     return -1;
00047         if (close() < 0)
00048     return -1;
00049   _isRewound = true;
00050   return 0;
00051 }
00052 
00053 DirectoryEntry *
00054 Directory::read()
00055 {
00056   static DirectoryEntry entry;
00057   if (_isRewound) {
00058     if (open() < 0)
00059       return NULL;
00060   }
00061   if (_handle == -1L)
00062     return NULL;
00063   if (_hasFirst) {
00064     _hasFirst = false;
00065   } else if (::_findnext(_handle, &_data) != 0) {
00066                         return NULL;    // end of entries
00067         }
00068         entry.name      = _data.name;
00069         entry.size      = _data.size;
00070         entry.atime     = _data.time_access;
00071         entry.ctime     = _data.time_create;
00072         entry.mtime     = _data.time_write;
00073         entry.directory = (_data.attrib & _A_SUBDIR) ? true : false;
00074 
00075         return &entry;
00076 }
00077 
00078 int
00079 Directory::seek(int pos)
00080 {
00081   if (rewind() < 0)
00082     return -1;
00083   if (open() < 0)
00084     return -1;
00085   for (int i = 0; i < pos; i++) {
00086     if (::_findnext(_handle, &_data) == 0)
00087       return -1;
00088   }
00089         return 0;
00090 }
00091 
00092 const char *
00093 Directory::name() const
00094 {
00095   return _name.c_str();
00096 }

Generated on Mon Oct 7 09:33:29 2002 for Gaia by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001