00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 }