00001 #include <errno.h>
00002 #include <sys/stat.h>
00003 #include "OSDependent/Directory.h"
00004
00005 Directory::Directory(const char *name) :
00006 _handle(NULL)
00007 {
00008 _name = name;
00009 }
00010
00011 Directory::~Directory()
00012 {
00013 close();
00014 }
00015
00016 int
00017 Directory::open()
00018 {
00019 _handle = ::opendir(_name.c_str());
00020 if (_handle == NULL)
00021 return -1;
00022 return 0;
00023 }
00024
00025 int
00026 Directory::close()
00027 {
00028 if (_handle == NULL)
00029 return 0;
00030 if (::closedir(_handle) < 0)
00031 return -1;
00032 return 0;
00033 }
00034
00035 int
00036 Directory::rewind()
00037 {
00038 if (_handle == NULL)
00039 return -1;
00040 ::rewinddir(_handle);
00041 return 0;
00042 }
00043
00044 DirectoryEntry *
00045 Directory::read()
00046 {
00047 static DirectoryEntry entry;
00048 struct dirent *dent;
00049 errno = 0;
00050 if ((dent = ::readdir(_handle)) == NULL) {
00051 if (errno == 0) {
00052 return NULL;
00053 } else {
00054 entry.name = NULL;
00055 return &entry;
00056 }
00057 }
00058 entry.name = dent->d_name;
00059 entry.size = dent->d_reclen;
00060
00061 entry.atime = 0;
00062 entry.ctime = 0;
00063 entry.mtime = 0;
00064 return &entry;
00065 }
00066
00067 int
00068 Directory::seek(int pos)
00069 {
00070 if (_handle == NULL)
00071 return -1;
00072 rewind();
00073 for (int i = 0; i < pos; i++) {
00074 if (::readdir(_handle) == NULL)
00075 return -1;
00076 }
00077 return 0;
00078 }
00079
00080 const char *
00081 Directory::name() const
00082 {
00083 return _name.c_str();
00084 }