summaryrefslogtreecommitdiffstats
path: root/src/cli_global.cpp
blob: 85e37c67892633ef7750ffbc2d23469900c5e30d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "cli_global.hpp"

#include <iostream>
#include <fstream>

#include "filesystem_env.hpp"

namespace po = boost::program_options;

namespace orcus {

output_stream::output_stream(const boost::program_options::variables_map& vm) :
    m_os(&std::cout)
{
    if (!vm.count("output"))
        // No output parameter given. Output to stdout.
        return;

    std::string output_path = vm["output"].as<std::string>();

    if (output_path.empty())
        // Specified output path is empty.
        return;

    // Check to make sure the output path doesn't point to an existing
    // directory.
    if (fs::is_directory(output_path))
    {
        std::ostringstream os;
        os << "Output file path points to an existing directory.";
        throw std::invalid_argument(os.str());
    }

    // Output to stdout when output path is not given.
    m_ofs = std::make_unique<std::ofstream>(output_path.data());
    m_os = m_ofs.get();
}

output_stream::output_stream(output_stream&& other) :
    m_ofs(std::move(other.m_ofs)),
    m_os(other.m_os)
{
    if (m_ofs)
        m_os = m_ofs.get();

    other.m_os = nullptr;
}

std::ostream& output_stream::get()
{
    return *m_os;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */