00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <unistd.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <fcntl.h>
00029 #include <errno.h>
00030
00031 #include <stlib/FileAccessor.h>
00032 #include <stlib/ByteArray.h>
00033 #include <stlib/String.h>
00034 #include <stlib/FileOpenFailed.h>
00035
00036 FileAccessor::FileAccessor(int fd)
00037 {
00038 name = nil;
00039 descriptor = fd;
00040 }
00041
00042 FileAccessor::FileAccessor(String *pathname, int rwMode, int creationRule)
00043 {
00044 name = pathname;
00045 descriptor = open(pathname->asCString(), rwMode | creationRule,
00046 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
00047 if (descriptor < 0) {
00048 FileOpenFailed *error;
00049 error = new FileOpenFailed(new String(sys_errlist[errno]),
00050 __PRETTY_FUNCTION__, pathname);
00051 error->raise();
00052 }
00053 }
00054
00055 FileAccessor::FileAccessor(const char *pathname, int rwMode, int creationRule)
00056 {
00057 name = nil;
00058 descriptor = open(pathname, rwMode | creationRule,
00059 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
00060 if (descriptor < 0) {
00061 FileOpenFailed *error;
00062 error = new FileOpenFailed(new String(sys_errlist[errno]),
00063 __PRETTY_FUNCTION__, new String(pathname));
00064 error->raise();
00065 }
00066 }
00067
00068
00069 String *FileAccessor::className(void) const
00070 {
00071 return new String("FileAccessor");
00072 }
00073
00074
00075 FileAccessor *FileAccessor::openFileReadOnly(String *pathname)
00076 {
00077 return new FileAccessor(pathname, O_RDONLY, 0);
00078 }
00079
00080 FileAccessor *FileAccessor::openFileWriteOnly(String *pathname)
00081 {
00082 return new FileAccessor(pathname, O_WRONLY, O_CREAT | O_TRUNC);
00083 }
00084
00085
00086 void FileAccessor::close(void)
00087 {
00088 ::close(descriptor);
00089 descriptor = -1;
00090 }
00091
00092
00093 void FileAccessor::commit(void)
00094 {
00095 fsync(descriptor);
00096 }
00097
00098 long FileAccessor::dataSize(void)
00099 {
00100 struct stat buf;
00101 fstat(descriptor, &buf);
00102 return buf.st_size;
00103 }
00104
00105 LineEndConvention FileAccessor::lineEndConvention(void)
00106 {
00107 return LineEndLF;
00108 }
00109
00110
00111 int FileAccessor::readInto(ByteArray *buffer, long startIndex, long count)
00112 {
00113 unsigned char buf[count + 1];
00114 long size;
00115
00116 size = read(descriptor, buf, count);
00117 if (size < 0) {
00118 perror(__PRETTY_FUNCTION__);
00119 error(new String("File read error"), new String(__PRETTY_FUNCTION__));
00120 }
00121 buffer->replace(startIndex, startIndex+size, buf);
00122 return size;
00123 }
00124
00125 int FileAccessor::writeFrom(ByteArray *buffer, long startIndex, long count)
00126 {
00127 const unsigned char *buf = buffer->rawBytesReadOnly();
00128 return write(descriptor, &buf[startIndex], count);
00129 }
00130
00131
00132 void FileAccessor::seekTo(long position)
00133 {
00134 lseek(descriptor, position, SEEK_SET);
00135 }
00136
00137
00138 void FileAccessor::changeDescriptorTo(int fd)
00139 {
00140 dup2(descriptor, fd);
00141 close();
00142 descriptor = fd;
00143 }
00144
00145
00146 bool FileAccessor::isSeekable(void)
00147 {
00148 return true;
00149 }