Confdump Agent  1.4.0
DataOutput.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
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.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <stdexcept>
00019 
00020 #include "QTextStream"
00021 #include "QFile"
00022 
00023 #include "Confdump/TextFormatter.hpp"
00024 #include "Confdump/XmlFormatter.hpp"
00025 #include "Confdump/DataOutput.hpp"
00026 
00027 Confdump::DataOutput::DataOutput()
00028 {
00029 }
00030 
00031 Confdump::DataOutput::~DataOutput()
00032 {
00033 }
00034 
00035 boost::shared_ptr<Confdump::Formatter> Confdump::DataOutput::createFormatter( Options::OutputFormat format ) const
00036 {
00037     boost::shared_ptr<Formatter> fmt;
00038 
00039     switch ( format )
00040     {
00041     case Options::TextFormat:
00042         fmt.reset( new TextFormatter );
00043         break;
00044 
00045     case Options::XmlFormat:
00046         fmt.reset( new XmlFormatter );
00047         break;
00048 
00049     }
00050 
00051     if ( !fmt )
00052         throw std::invalid_argument( "unhandled output format" );
00053 
00054     fmt->setDevice( device_ );
00055 
00056     return fmt;
00057 }
00058 
00059 QTextStream &Confdump::DataOutput::stream() const
00060 {
00061     return *stream_;
00062 }
00063 
00064 void Confdump::DataOutput::setSink( FILE *file )
00065 {
00066     boost::shared_ptr<QFile> device( new QFile );
00067     if ( !device->open( file, QIODevice::WriteOnly | QIODevice::Text ) )
00068         throw std::runtime_error( "unable to open data output stream" );
00069     setSink( device );
00070 }
00071 
00072 void Confdump::DataOutput::setSink( QString file )
00073 {
00074     boost::shared_ptr<QFile> device( new QFile( file ) );
00075     if ( !device->open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
00076         throw std::runtime_error( "unable to open output file" );
00077     setSink( device );
00078 }
00079 
00080 void Confdump::DataOutput::setSink( boost::shared_ptr<QIODevice> device )
00081 {
00082     device_ = device;
00083     stream_.reset( new QTextStream( device.get() ) );
00084 }
00085 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends