Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/File.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: File.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/File>
00026 
00027 using base::File;
00028 using base::VFile;
00029 using base::PathName;
00030 
00031 
00032 File::File(const PathName& pathname) throw(path_not_found, io_error)
00033   : VFile(pathname), opened(false)
00034 {
00035 }
00036 
00037 
00038 File::File(const String& pathname) throw(path_not_found, io_error, std::invalid_argument)
00039   : VFile(PathName(pathname)), opened(false)
00040 {
00041 }
00042     
00043 
00044 File::~File()
00045 {
00046   close();
00047 }
00048 
00049 
00050 std::istream& File::istream() const throw(io_error)
00051 {
00052   return iostream(std::iostream::in);
00053 }
00054 
00055 
00056 std::ostream& File::ostream() const throw(io_error)
00057 {
00058   return iostream(std::iostream::out);
00059 }
00060 
00061 
00062 std::iostream& File::iostream(std::iostream::openmode mode) const throw(io_error)
00063 {
00064   if (!opened) 
00065     open(mode);
00066   else 
00067     if ((this->mode & mode) != mode) {
00068       close();
00069       open(mode);
00070     }
00071   return *fileStream;
00072 }
00073 
00074 
00075 void File::close() const throw(io_error)
00076 {
00077   if (opened) {
00078     if (mode & std::iostream::out) (*fileStream) << std::flush;
00079     fileStream->close();
00080     delete fileStream;
00081     opened=false;
00082   }
00083 }
00084 
00085 
00086 void File::open(std::iostream::openmode mode) const throw(path_not_found, io_error)
00087 {
00088   if (!opened) {
00089     fileStream = new std::fstream( pathname.str().c_str(), mode);
00090     if (!(*fileStream))
00091       throw io_error(Exception(String("can't open file ")+pathname.str()));
00092     opened=true;
00093     this->mode = mode;
00094   }
00095 }

Generated on Thu Jul 29 15:56:06 2004 for OpenSim by doxygen 1.3.6