00001 00005 00006 // 00007 // Copyright (c) 2003-2004 California Institute of Technology 00008 // Copyright (c) 2004-2007 University of Southern California 00009 // Rob Peters <rjpeters at usc dot edu> 00010 // 00011 // created: Fri May 23 10:03:19 2003 00012 // commit: $Id: outputfile.cc 10065 2007-04-12 05:54:56Z rjpeters $ 00013 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/io/outputfile.cc $ 00014 // 00015 // -------------------------------------------------------------------- 00016 // 00017 // This file is part of GroovX. 00018 // [http://ilab.usc.edu/rjpeters/groovx/] 00019 // 00020 // GroovX is free software; you can redistribute it and/or modify it 00021 // under the terms of the GNU General Public License as published by 00022 // the Free Software Foundation; either version 2 of the License, or 00023 // (at your option) any later version. 00024 // 00025 // GroovX is distributed in the hope that it will be useful, but 00026 // WITHOUT ANY WARRANTY; without even the implied warranty of 00027 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00028 // General Public License for more details. 00029 // 00030 // You should have received a copy of the GNU General Public License 00031 // along with GroovX; if not, write to the Free Software Foundation, 00032 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00033 // 00035 00036 #ifndef GROOVX_IO_OUTPUTFILE_CC_UTC20050626084021_DEFINED 00037 #define GROOVX_IO_OUTPUTFILE_CC_UTC20050626084021_DEFINED 00038 00039 #include "outputfile.h" 00040 00041 #include "io/reader.h" 00042 #include "io/writer.h" 00043 00044 #include "rutz/error.h" 00045 #include "rutz/sfmt.h" 00046 00047 #include <fstream> 00048 00049 #include "rutz/trace.h" 00050 00051 using rutz::shared_ptr; 00052 00053 output_file::output_file() : 00054 m_filename(""), 00055 m_stream() 00056 { 00057 } 00058 00059 output_file::~output_file() throw() {} 00060 00061 void output_file::read_from(io::reader& reader) 00062 { 00063 rutz::fstring fname; 00064 reader.read_value("filename", fname); 00065 00066 set_filename(fname); 00067 } 00068 00069 void output_file::write_to(io::writer& writer) const 00070 { 00071 writer.write_value("filename", m_filename); 00072 } 00073 00074 rutz::fstring output_file::get_filename() const 00075 { 00076 return m_filename; 00077 } 00078 00079 void output_file::set_filename(rutz::fstring fname) 00080 { 00081 if (fname.is_empty()) 00082 { 00083 m_stream.reset(); 00084 return; 00085 } 00086 00087 shared_ptr<std::ostream> s(new std::ofstream(fname.c_str())); 00088 00089 if (s->fail()) 00090 throw rutz::error(rutz::sfmt("couldn't open '%s' for writing", 00091 fname.c_str()), SRC_POS); 00092 00093 m_stream.swap(s); 00094 m_filename = fname; 00095 } 00096 00097 bool output_file::has_stream() const 00098 { 00099 return (m_stream.get() != 0 && !m_stream->fail()); 00100 } 00101 00102 std::ostream& output_file::stream() 00103 { 00104 if (!has_stream()) 00105 throw rutz::error("output_file object's stream is invalid", SRC_POS); 00106 00107 return *m_stream; 00108 } 00109 00110 static const char __attribute__((used)) vcid_groovx_io_outputfile_cc_utc20050626084021[] = "$Id: outputfile.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file: 00111 #endif // !GROOVX_IO_OUTPUTFILE_CC_UTC20050626084021_DEFINED