00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #include "ux.hpp" 00019 00020 using namespace Ux; 00021 00022 /** 00023 Calls malloc to allocate space for dirent. 00024 */ 00025 void DirStream::alloc(const char *path) 00026 { 00027 // use malloc instead of new so errno will be set 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 Calls free to free directory entry. 00035 */ 00036 void DirStream::free(void) 00037 { 00038 ::free(entry); 00039 entry = NULL; 00040 } 00041 00042 /** 00043 Calls pathconf to get _PC_NAME_MAX. 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 Calls opendir. 00054 */ 00055 void DirStream::open(const char *path) 00056 { 00057 if ((dirstream = ::opendir(path)) == NULL) 00058 throw Error(errno); 00059 } 00060 00061 /** 00062 Calls open and allocates space for directory entry. 00063 */ 00064 void DirStream::open_alloc(const char *path) 00065 { 00066 open(path); 00067 alloc(path); 00068 } 00069 00070 /** 00071 Calls closedir. 00072 */ 00073 void DirStream::close(void) 00074 { 00075 if (::closedir(dirstream) == -1) 00076 throw Error(errno); 00077 } 00078 00079 /** 00080 Calls readdir_r. 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 Calls rewinddir. 00094 */ 00095 void DirStream::rewind(void) 00096 { 00097 ::rewinddir(dirstream); 00098 } 00099 00100 /** 00101 Calls seekdir. 00102 */ 00103 void DirStream::seek(long loc) 00104 { 00105 ::seekdir(dirstream, loc); 00106 } 00107 00108 /** 00109 Calls telldir. 00110 */ 00111 long DirStream::tell(void) 00112 { 00113 return ::telldir(dirstream); 00114 }