Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

FileAccessor.cc

Go to the documentation of this file.
00001 /*
00002  * FileAccessor.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Accessor to file stored on disc.
00006  *
00007  * Copyright (c) 2003 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 /* Class-accessing protocol */
00069 String *FileAccessor::className(void) const
00070 {
00071     return new String("FileAccessor");
00072 }
00073 
00074 /* Instance creation protocol */
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 /* Initialize-release protocol */
00086 void FileAccessor::close(void)
00087 {
00088     ::close(descriptor);
00089     descriptor = -1;
00090 }
00091 
00092 /* Accessing protocol */
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 /* Data transfer protocol */
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 /* Positioning protocol */
00132 void FileAccessor::seekTo(long position)
00133 {
00134     lseek(descriptor, position, SEEK_SET);
00135 }
00136 
00137 /* Stream redirection protocol */
00138 void FileAccessor::changeDescriptorTo(int fd)
00139 {
00140     dup2(descriptor, fd);
00141     close();
00142     descriptor = fd;
00143 }
00144 
00145 /* Testing protocol */
00146 bool FileAccessor::isSeekable(void)
00147 {
00148     return true;
00149 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2