mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 02:08:09 +08:00
@@ -1,7 +1,22 @@
|
|||||||
version: 2.1
|
version: 2.1
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test_suite:
|
||||||
|
environment:
|
||||||
|
- GOPATH: /home/circleci/go
|
||||||
|
docker:
|
||||||
|
- image: circleci/golang:1.9
|
||||||
|
steps:
|
||||||
|
- checkout
|
||||||
|
- run:
|
||||||
|
command: |
|
||||||
|
g++ --version
|
||||||
|
cd tests/
|
||||||
|
ls
|
||||||
|
g++ -std=c++11 -O2 -Wall -Wextra -Werror -I../ check_toml_test.cpp -o check_toml_test
|
||||||
|
go get github.com/BurntSushi/toml-test
|
||||||
|
$GOPATH/bin/toml-test ./check_toml_test
|
||||||
|
output_result:
|
||||||
docker:
|
docker:
|
||||||
- image: circleci/buildpack-deps:bionic
|
- image: circleci/buildpack-deps:bionic
|
||||||
steps:
|
steps:
|
||||||
@@ -42,4 +57,5 @@ workflows:
|
|||||||
version: 2.1
|
version: 2.1
|
||||||
test:
|
test:
|
||||||
jobs:
|
jobs:
|
||||||
- test
|
- test_suite
|
||||||
|
- output_result
|
||||||
|
12
README.md
12
README.md
@@ -1,8 +1,9 @@
|
|||||||
toml11
|
toml11
|
||||||
======
|
======
|
||||||
|
|
||||||
[](https://travis-ci.org/ToruNiina/toml11)
|
[](https://travis-ci.org/ToruNiina/toml11)
|
||||||
[](https://ci.appveyor.com/project/ToruNiina/toml11/branch/master)
|
[](https://ci.appveyor.com/project/ToruNiina/toml11/branch/master)
|
||||||
|
[](https://circleci.com/gh/ToruNiina/toml11/tree/master)
|
||||||
[](https://github.com/ToruNiina/toml11/releases)
|
[](https://github.com/ToruNiina/toml11/releases)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
[](https://doi.org/10.5281/zenodo.1209136)
|
[](https://doi.org/10.5281/zenodo.1209136)
|
||||||
@@ -13,9 +14,14 @@ compatible to the latest version of
|
|||||||
[TOML v0.5.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.5.0.md)
|
[TOML v0.5.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.5.0.md)
|
||||||
after version 2.0.0.
|
after version 2.0.0.
|
||||||
|
|
||||||
|
It passes [the language agnostic test suite for TOML parsers by BurntSushi](https://github.com/BurntSushi/toml-test).
|
||||||
|
Not only the test suite itself, tiny TOML reader/encoder also runs on [CircleCI](https://circleci.com/gh/ToruNiina/toml11).
|
||||||
|
You can see the error messages about invalid files and serialization results of valid files at
|
||||||
|
[CircleCI](https://circleci.com/gh/ToruNiina/toml11).
|
||||||
|
|
||||||
Are you looking for pre-C++11 compatible toml parser?
|
Are you looking for pre-C++11 compatible toml parser?
|
||||||
Try [Boost.toml](https://github.com/ToruNiina/Boost.toml)!
|
Try [Boost.toml](https://github.com/ToruNiina/Boost.toml)!
|
||||||
It has almost the same functionality as this library and works with C++98 & Boost.
|
It has a bit less functionality than this library but works with C++98 + Boost.
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
|
142
tests/check_toml_test.cpp
Normal file
142
tests/check_toml_test.cpp
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
#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)
|
||||||
|
{
|
||||||
|
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.
|
||||||
|
std::cout << "{\"type\":\"string\",\"value\":\""
|
||||||
|
<< this->escape_string(v.str) << "\"}";
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
void operator()(const toml::local_time& v)
|
||||||
|
{
|
||||||
|
std::cout << "{\"type\":\"local_time\",\"value\":\"" << toml::value(v) << "\"}";
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
void operator()(const toml::local_date& v)
|
||||||
|
{
|
||||||
|
std::cout << "{\"type\":\"local_date\",\"value\":\"" << toml::value(v) << "\"}";
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
void operator()(const toml::local_datetime& v)
|
||||||
|
{
|
||||||
|
std::cout << "{\"type\":\"local_datetime\",\"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\":[";
|
||||||
|
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;
|
||||||
|
std::cout << toml::format(toml::string(elem.first),
|
||||||
|
std::numeric_limits<std::size_t>::max());
|
||||||
|
std::cout << ':';
|
||||||
|
toml::visit(*this, elem.second);
|
||||||
|
}
|
||||||
|
std::cout << '}';
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string escape_string(const std::string& s) const
|
||||||
|
{
|
||||||
|
std::string retval;
|
||||||
|
for(const char c : s)
|
||||||
|
{
|
||||||
|
switch(c)
|
||||||
|
{
|
||||||
|
case '\\': {retval += "\\\\"; break;}
|
||||||
|
case '\"': {retval += "\\\""; break;}
|
||||||
|
case '\b': {retval += "\\b"; break;}
|
||||||
|
case '\t': {retval += "\\t"; break;}
|
||||||
|
case '\f': {retval += "\\f"; break;}
|
||||||
|
case '\n': {retval += "\\n"; break;}
|
||||||
|
case '\r': {retval += "\\r"; break;}
|
||||||
|
default : {retval += c; break;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user