yat/utility/OptionOutFile.cc

Code
Comments
Other
Rev Date Author Line
1014 01 Feb 08 peter 1 // $Id$
1014 01 Feb 08 peter 2
1014 01 Feb 08 peter 3 /*
2119 12 Dec 09 peter 4   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 5   Copyright (C) 2012 Peter Johansson
1014 01 Feb 08 peter 6
1437 25 Aug 08 peter 7   This file is part of the yat library, http://dev.thep.lu.se/yat
1014 01 Feb 08 peter 8
1014 01 Feb 08 peter 9   The yat library is free software; you can redistribute it and/or
1014 01 Feb 08 peter 10   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 11   published by the Free Software Foundation; either version 3 of the
1014 01 Feb 08 peter 12   License, or (at your option) any later version.
1014 01 Feb 08 peter 13
1014 01 Feb 08 peter 14   The yat library is distributed in the hope that it will be useful,
1014 01 Feb 08 peter 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1014 01 Feb 08 peter 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1014 01 Feb 08 peter 17   General Public License for more details.
1014 01 Feb 08 peter 18
1014 01 Feb 08 peter 19   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 20   along with yat. If not, see <http://www.gnu.org/licenses/>.
1014 01 Feb 08 peter 21 */
1014 01 Feb 08 peter 22
2881 18 Nov 12 peter 23 #include <config.h>
2881 18 Nov 12 peter 24
1014 01 Feb 08 peter 25 #include "OptionOutFile.h"
1014 01 Feb 08 peter 26
1014 01 Feb 08 peter 27 #include <cassert>
1014 01 Feb 08 peter 28 #include <fstream>
1014 01 Feb 08 peter 29 #include <iostream>
1014 01 Feb 08 peter 30 #include <string>
1014 01 Feb 08 peter 31
1014 01 Feb 08 peter 32 namespace theplu {
1014 01 Feb 08 peter 33 namespace yat {
1014 01 Feb 08 peter 34 namespace utility {
1014 01 Feb 08 peter 35
1014 01 Feb 08 peter 36
4200 19 Aug 22 peter 37   OptionOutFile::OptionOutFile(CommandLine& cmd, std::string flag,
1014 01 Feb 08 peter 38                                std::string desc, bool required)
1014 01 Feb 08 peter 39     : OptionFile(cmd, flag, desc, required, false, "w"), os_(NULL) {}
1014 01 Feb 08 peter 40
1014 01 Feb 08 peter 41
1014 01 Feb 08 peter 42   OptionOutFile::~OptionOutFile(void)
1014 01 Feb 08 peter 43   {
1014 01 Feb 08 peter 44     if (os_) {
1014 01 Feb 08 peter 45       os_->close();
1014 01 Feb 08 peter 46       delete os_;
1014 01 Feb 08 peter 47     }
1014 01 Feb 08 peter 48   }
1014 01 Feb 08 peter 49
1014 01 Feb 08 peter 50
1014 01 Feb 08 peter 51   std::ostream& OptionOutFile::ostream(void)
1014 01 Feb 08 peter 52   {
1014 01 Feb 08 peter 53     if (!present())
1014 01 Feb 08 peter 54       return std::cout;
1014 01 Feb 08 peter 55     if (!os_){
1014 01 Feb 08 peter 56       assert(value().size());
1014 01 Feb 08 peter 57       os_ = new std::ofstream(value().c_str());
1014 01 Feb 08 peter 58       assert(os_->good());
1014 01 Feb 08 peter 59     }
1014 01 Feb 08 peter 60     return *os_;
1014 01 Feb 08 peter 61   }
1014 01 Feb 08 peter 62
1014 01 Feb 08 peter 63
1014 01 Feb 08 peter 64 }}} // of namespace utility, yat, and theplu