[go: up one dir, main page]

File: check_toml_test.cpp

package info (click to toggle)
toml11 3.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,120 kB
  • sloc: cpp: 17,500; sh: 6; makefile: 4
file content (138 lines) | stat: -rw-r--r-- 4,077 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "toml.hpp"
#include <iostream>
#include <iomanip>

struct json_serializer
{
    void operator()(toml::boolean v)
    {
        std::cout << "{\"type\":\"bool\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(toml::integer v)
    {
        std::cout << "{\"type\":\"integer\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(toml::floating v)
    {
        if(std::isnan(v) && std::signbit(v))
        {
            // toml-test does not allow negative NaN represented in "-nan" because
            // there are languages that does not distinguish nan and -nan.
            // But toml11 keeps sign from input. To resolve this difference,
            // we convert -nan to nan here.
            v = std::numeric_limits<toml::floating>::quiet_NaN();
        }
        std::cout << "{\"type\":\"float\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(const toml::string& v)
    {
        // since toml11 automatically convert string to multiline string that is
        // valid only in TOML, we need to format the string to make it valid in
        // JSON.
        toml::serializer<toml::value> ser(std::numeric_limits<std::size_t>::max());
        std::cout << "{\"type\":\"string\",\"value\":"
                  << ser(v.str) << "}";
        return ;
    }
    void operator()(const toml::local_time& v)
    {
        std::cout << "{\"type\":\"time-local\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(const toml::local_date& v)
    {
        std::cout << "{\"type\":\"date-local\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(const toml::local_datetime& v)
    {
        std::cout << "{\"type\":\"datetime-local\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(const toml::offset_datetime& v)
    {
        std::cout << "{\"type\":\"datetime\",\"value\":\"" << toml::value(v) << "\"}";
        return ;
    }
    void operator()(const toml::array& v)
    {
        if(!v.empty() && v.front().is_table())
        {
            std::cout << '[';
            bool is_first = true;
            for(const auto& elem : v)
            {
                if(!is_first) {std::cout << ", ";}
                is_first = false;
                toml::visit(*this, elem);
            }
            std::cout << ']';
        }
        else
        {
//             std::cout << "{\"type\":\"array\",\"value\":[";
            std::cout << "[";
            bool is_first = true;
            for(const auto& elem : v)
            {
                if(!is_first) {std::cout << ", ";}
                is_first = false;
                toml::visit(*this, elem);
            }
            std::cout << "]";
        }
        return ;
    }
    void operator()(const toml::table& v)
    {
        std::cout << '{';
        bool is_first = true;
        for(const auto& elem : v)
        {
            if(!is_first) {std::cout << ", ";}
            is_first = false;
            const auto k = toml::format_key(elem.first);
            if(k.at(0) == '"')
            {
                std::cout << k << ":";
            }
            else // bare key
            {
                std::cout << '\"' << k << "\":";
            }
            toml::visit(*this, elem.second);
        }
        std::cout << '}';
        return ;
    }
};

int main()
{
    try
    {
        std::vector<char> buf;
        std::cin.peek();
        while(!std::cin.eof())
        {
            buf.push_back(std::cin.get());
            std::cin.peek();
        }
        std::string bufstr(buf.begin(), buf.end());

        std::istringstream ss(bufstr);

        const auto data = toml::parse(ss);
        std::cout << std::setprecision(std::numeric_limits<double>::max_digits10);
        toml::visit(json_serializer(), data);
        return 0;
    }
    catch(const toml::syntax_error& err)
    {
        std::cout << "what(): " << err.what() << std::endl;
        return 1;
    }
}