00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "ux.hpp"
00019
00020 using namespace Ux;
00021
00022
00023
00024
00025 void DirStream::alloc(const char *path)
00026 {
00027
00028 if ((entry = (struct dirent *)::malloc(offsetof(struct dirent, d_name) +
00029 get_max_name(path) + 1)) == NULL)
00030 throw Error(errno);
00031 }
00032
00033
00034
00035
00036 void DirStream::free(void)
00037 {
00038 ::free(entry);
00039 entry = NULL;
00040 }
00041
00042
00043
00044
00045 size_t DirStream::get_max_name(const char *path)
00046 {
00047 return (size_t)File(path).pathconf(_PC_NAME_MAX, 512);
00048 }
00049
00050
00051
00052
00053
00054
00055 void DirStream::open(const char *path)
00056 {
00057 if ((dirstream = ::opendir(path)) == NULL)
00058 throw Error(errno);
00059 }
00060
00061
00062
00063
00064 void DirStream::open_alloc(const char *path)
00065 {
00066 open(path);
00067 alloc(path);
00068 }
00069
00070
00071
00072
00073 void DirStream::close(void)
00074 {
00075 if (::closedir(dirstream) == -1)
00076 throw Error(errno);
00077 }
00078
00079
00080
00081
00082 bool DirStream::read(void)
00083 {
00084 struct dirent *result;
00085 int r;
00086
00087 if ((r = ::readdir_r(dirstream, entry, &result)) != 0)
00088 throw(r);
00089 return result != NULL;
00090 }
00091
00092
00093
00094
00095 void DirStream::rewind(void)
00096 {
00097 ::rewinddir(dirstream);
00098 }
00099
00100
00101
00102
00103 void DirStream::seek(long loc)
00104 {
00105 ::seekdir(dirstream, loc);
00106 }
00107
00108
00109
00110
00111 long DirStream::tell(void)
00112 {
00113 return ::telldir(dirstream);
00114 }