summaryrefslogtreecommitdiffstats
path: root/src/spreadsheet/dumper_global.cpp
blob: f500f3cd7d723f49733e3ec167706a3dd13cef07 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- 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 "dumper_global.hpp"
#include "number_format.hpp"

#include <ixion/formula_name_resolver.hpp>
#include <ixion/formula_result.hpp>
#include <ixion/cell.hpp>

namespace orcus { namespace spreadsheet { namespace detail {

void dump_cell_value(
    std::ostream& os, const ixion::model_context& cxt, const ixion::model_iterator::cell& cell,
    func_str_handler str_handler,
    func_empty_handler empty_handler)
{
    switch (cell.type)
    {
        case ixion::celltype_t::empty:
            empty_handler(os);
            break;
        case ixion::celltype_t::boolean:
        {
            os << (std::get<bool>(cell.value) ? "true" : "false");
            break;
        }
        case ixion::celltype_t::numeric:
        {
            format_to_file_output(os, std::get<double>(cell.value));
            break;
        }
        case ixion::celltype_t::string:
        {
            const std::string* p = cxt.get_string(std::get<ixion::string_id_t>(cell.value));
            assert(p);
            str_handler(os, *p);
            break;
        }
        case ixion::celltype_t::formula:
        {
            const ixion::formula_cell* fc = std::get<const ixion::formula_cell*>(cell.value);
            assert(fc);
            ixion::formula_result res;

            try
            {
                res = fc->get_result_cache(
                    ixion::formula_result_wait_policy_t::throw_exception);
            }
            catch (const std::exception&)
            {
                os << "\"#RES!\"";
                break;
            }

            switch (res.get_type())
            {
                case ixion::formula_result::result_type::value:
                    format_to_file_output(os, res.get_value());
                break;
                case ixion::formula_result::result_type::string:
                {
                    const std::string& s = res.get_string();
                    str_handler(os, s);
                }
                break;
                case ixion::formula_result::result_type::error:
                    os << "\"#ERR!\"";
                break;
                default:
                    ;
            }
            break;
        }
        default:
            ;
    }
}

}}}

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