Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

filedevice.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     filedevice.cpp  -  Definitions of the FileDevice class methods.
00003                              -------------------
00004     begin                : June 21 2002
00005     copyright            : (C) 2003 by Vojtìch Toman
00006     email                : vtoman@lit.cz
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00026 #ifdef __GNUG__
00027 # pragma implementation
00028 #endif
00029 
00030 
00031 #include "filedevice.h"
00032 
00033 
00034 
00038 FileDevice::FileDevice(void)
00039   : IODevice()
00040 {
00041   bWritten = 0;
00042   bRead = 0;
00043 }
00044 
00045 
00046 
00050 FileDevice::~FileDevice(void)
00051 {
00052   if (!isPrepared())
00053     finish();
00054 }
00055 
00056 
00057 
00064 void FileDevice::prepare(const char *fileName, int mode) throw (ExaltIOException)
00065 {
00066   //if fileName is NULL, use stdout or stdin
00067   if (!fileName)
00068     {
00069       if (mode == ios::in)
00070         attach(0);
00071       else
00072         if (mode == ios::out)
00073           attach(1);
00074         else
00075           throw ExaltPrepareDeviceIOException();
00076     }
00077   else
00078     open(fileName, mode);
00079 
00080   prepared = !fail();
00081 
00082   if (!prepared)
00083     throw ExaltPrepareDeviceIOException();
00084 }
00085 
00086 
00090 void FileDevice::flush(void) throw (ExaltIOException)
00091 {
00092   fstream::flush();
00093 
00094   if (errorOccurred())
00095     throw ExaltFlushDeviceIOException();
00096 }
00097 
00098 
00102 void FileDevice::finish(void) throw (ExaltIOException)
00103 {
00104   prepared = false;
00105   bWritten = 0;
00106   bRead = 0;
00107 
00108   flush();
00109   close();
00110 
00111   if (errorOccurred())
00112     throw ExaltFinishDeviceIOException();
00113 }
00114 
00115 
00124 IOState FileDevice::readData(char *buf, IOSize length) throw (ExaltIOException)
00125 {
00126   if (isPrepared())
00127     {
00128       if (read(buf, length))
00129         {
00130           bRead += gcount();
00131           return ReadOk;
00132         }
00133       else
00134         {
00135           bRead += gcount();
00136 
00137           if (eof())
00138             return EndOfFile;
00139           else
00140             throw ExaltDeviceReadIOException();
00141         }
00142     }
00143   else
00144     throw ExaltDeviceNotPreparedIOException();
00145 }
00146 
00147 
00155 IOState FileDevice::getChar(int *c) throw (ExaltIOException)
00156 {
00157   if (isPrepared())
00158     {
00159       *c = 0;
00160       if (get((unsigned char &)*c))
00161         {
00162           bRead += gcount();
00163           return ReadOk;
00164         }
00165       else
00166         {
00167           bRead += gcount();
00168           if (eof())
00169             return EndOfFile;
00170           else
00171             throw ExaltDeviceWriteIOException();
00172         }
00173     }
00174   else
00175     throw ExaltDeviceNotPreparedIOException();
00176 }
00177 
00178 
00187 IOState FileDevice::writeData(const char *buf, IOSize length) throw (ExaltIOException)
00188 {
00189   if (isPrepared())
00190     {
00191       if (write(buf, length))
00192         {
00193           bWritten += length;
00194           return WriteOk;
00195         }
00196       else
00197         throw ExaltDeviceWriteIOException();
00198     }
00199   else
00200     throw ExaltDeviceNotPreparedIOException();
00201 }
00202 
00203 
00211 IOState FileDevice::putChar(int c) throw (ExaltIOException)
00212 {
00213   if (isPrepared())
00214     {
00215       if (put((unsigned char)c))
00216         {
00217           bWritten++;
00218           return WriteOk;
00219         }
00220       else
00221         throw ExaltDeviceWriteIOException();
00222     }
00223   else
00224     throw ExaltDeviceNotPreparedIOException();
00225 }
00226 
00227 
00233 IOSize FileDevice::bytesReadTotal(void)
00234 {
00235   return bRead;
00236 }
00237 
00238 
00244 IOSize FileDevice::bytesRead(void)
00245 {
00246   return gcount();
00247 }
00248 
00249 
00255 IOSize FileDevice::bytesWritten(void)
00256 {
00257   return bWritten;
00258 }
00259 
00260 
00267 bool FileDevice::errorOccurred(void)
00268 {
00269   return bad();
00270 }
00271 
00278 bool FileDevice::eof(void)
00279 {
00280   return fstream::eof();
00281 }
00282 
00283 

Generated on Wed Feb 5 10:43:01 2003 for Exalt by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002