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

Core::FileAccessor Class Reference

#include <FileAccessor.h>

Inheritance diagram for Core::FileAccessor:

Inheritance graph
[legend]
Collaboration diagram for Core::FileAccessor:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 FileAccessor (int fd)
 Creates FileAccessor instance on existing and openned file descriptor.
 FileAccessor (String *pathname, int rwMode, int creationRule)
 Creates FileAccessor instance on file specified by path and name.
 FileAccessor (const char *pathname, int rwMode, int creationRule)
virtual StringclassName (void) const
 Answer receiver class name.
virtual void close (void)
virtual void commit (void)
virtual long dataSize (void)
virtual LineEndConvention lineEndConvention (void)
virtual int readInto (ByteArray *buffer)
virtual int readInto (ByteArray *buffer, long startIndex, long count)
virtual int writeFrom (ByteArray *buffer)
virtual int writeFrom (ByteArray *buffer, long startIndex, long count)
virtual void seekTo (long position)
virtual void changeDescriptorTo (int fd)
 Duplicates file descriptor to given number and then closes original one.
virtual bool isSeekable (void)

Static Public Member Functions

static FileAccessoropenFileReadOnly (String *pathname)
static FileAccessoropenFileWriteOnly (String *pathname)

Protected Attributes

Stringname
 Name (and optionally path) of the file.
int descriptor
 File descriptor of openned file.

Constructor & Destructor Documentation

FileAccessor::FileAccessor int  fd  ) 
 

Creates FileAccessor instance on existing and openned file descriptor.

This constructor is provided for creating accessors to standard input (fd = 0), standard output (fd = 1) and standard error output (fd = 2). It should not be used in any application.

  • fd - file descriptor to create accessor on

Definition at line 36 of file FileAccessor.cc.

References descriptor, name, and nil.

Referenced by openFileReadOnly(), and openFileWriteOnly().

00037 {
00038     name = nil;
00039     descriptor = fd;
00040 }

FileAccessor::FileAccessor String pathname,
int  rwMode,
int  creationRule
 

Creates FileAccessor instance on file specified by path and name.

  • pathname - path and name of the file
  • rwMode - data direction to open file for
  • creationRule - says if the file should or shouldn't be create

Definition at line 42 of file FileAccessor.cc.

References Core::String::asCString(), descriptor, Core::Object::error(), name, and Core::GenericException::raise().

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 }

FileAccessor::FileAccessor const char *  pathname,
int  rwMode,
int  creationRule
 

Definition at line 55 of file FileAccessor.cc.

References descriptor, Core::Object::error(), name, nil, and Core::GenericException::raise().

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 }


Member Function Documentation

void FileAccessor::changeDescriptorTo int  fd  )  [virtual]
 

Duplicates file descriptor to given number and then closes original one.

In fact it calls dup2 and close functions. Use with caution.

Reimplemented from Core::IOAccessor.

Definition at line 138 of file FileAccessor.cc.

References close(), and descriptor.

Referenced by OS::Process::privateExecuteJob().

00139 {
00140     dup2(descriptor, fd);
00141     close();
00142     descriptor = fd;
00143 }

String * FileAccessor::className void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::IOAccessor.

Definition at line 69 of file FileAccessor.cc.

00070 {
00071     return new String("FileAccessor");
00072 }

void FileAccessor::close void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 86 of file FileAccessor.cc.

References descriptor.

Referenced by changeDescriptorTo(), OS::UnixPipe::closeInput(), OS::UnixPipe::closeOutput(), and Tools::Filename::privateCopy().

00087 {
00088     ::close(descriptor);
00089     descriptor = -1;
00090 }

void FileAccessor::commit void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 93 of file FileAccessor.cc.

References descriptor.

00094 {
00095     fsync(descriptor);
00096 }

long FileAccessor::dataSize void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 98 of file FileAccessor.cc.

References descriptor.

00099 {
00100     struct stat buf;
00101     fstat(descriptor, &buf);
00102     return buf.st_size;
00103 }

bool FileAccessor::isSeekable void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 146 of file FileAccessor.cc.

00147 {
00148     return true;
00149 }

LineEndConvention FileAccessor::lineEndConvention void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 105 of file FileAccessor.cc.

References Core::LineEndLF.

00106 {
00107     return LineEndLF;
00108 }

FileAccessor * FileAccessor::openFileReadOnly String pathname  )  [static]
 

Definition at line 75 of file FileAccessor.cc.

References FileAccessor().

00076 {
00077     return new FileAccessor(pathname, O_RDONLY, 0);
00078 }

FileAccessor * FileAccessor::openFileWriteOnly String pathname  )  [static]
 

Definition at line 80 of file FileAccessor.cc.

References FileAccessor().

00081 {
00082     return new FileAccessor(pathname, O_WRONLY, O_CREAT | O_TRUNC);
00083 }

int FileAccessor::readInto ByteArray buffer,
long  startIndex,
long  count
[virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 111 of file FileAccessor.cc.

References descriptor, Core::Object::error(), and Core::ByteArray::replace().

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 }

virtual int Core::FileAccessor::readInto ByteArray buffer  )  [inline, virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 78 of file FileAccessor.h.

References Core::IOAccessor::readInto().

Referenced by Tools::Filename::privateCopy(), and OS::UnixPipe::readInto().

00079       { return IOAccessor::readInto(buffer); }

void FileAccessor::seekTo long  position  )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 132 of file FileAccessor.cc.

References descriptor.

00133 {
00134     lseek(descriptor, position, SEEK_SET);
00135 }

int FileAccessor::writeFrom ByteArray buffer,
long  startIndex,
long  count
[virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 125 of file FileAccessor.cc.

References descriptor.

00126 {
00127     const unsigned char *buf = buffer->rawBytesReadOnly();
00128     return write(descriptor, &buf[startIndex], count);
00129 }

virtual int Core::FileAccessor::writeFrom ByteArray buffer  )  [inline, virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 82 of file FileAccessor.h.

References Core::IOAccessor::writeFrom().

Referenced by Tools::Filename::privateCopy(), and OS::UnixPipe::writeFrom().

00083       { return IOAccessor::writeFrom(buffer); }


Member Data Documentation

int Core::FileAccessor::descriptor [protected]
 

File descriptor of openned file.

Definition at line 41 of file FileAccessor.h.

Referenced by changeDescriptorTo(), close(), commit(), dataSize(), FileAccessor(), readInto(), seekTo(), and writeFrom().

String* Core::FileAccessor::name [protected]
 

Name (and optionally path) of the file.

Definition at line 39 of file FileAccessor.h.

Referenced by FileAccessor().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:51:30 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2