https://github.com/toruniina/tomlparser
c++ header only TOML parser
https://github.com/toruniina/tomlparser
toml toml-parser
Last synced: 5 months ago
JSON representation
c++ header only TOML parser
- Host: GitHub
- URL: https://github.com/toruniina/tomlparser
- Owner: ToruNiina
- License: mit
- Created: 2016-08-18T04:07:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-06-01T04:04:41.000Z (over 7 years ago)
- Last Synced: 2025-04-23T19:25:39.816Z (9 months ago)
- Topics: toml, toml-parser
- Language: C++
- Homepage:
- Size: 115 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
TOMLParser
====
[](https://travis-ci.org/ToruNiina/TOMLParser)
[](LICENSE)
C++ header-only TOML Parser.
__NOTE__: New c++11 toml library named [toml11](https://github.com/ToruNiina/toml11) is ready. If you can use c++11, use it.
__NOTE__: New c++(98|11|14|17) toml library with Boost named [Boost.toml](https://github.com/ToruNiina/Boost.toml) is ready. If you can use Boost C++ Library, use it.
__TOMLParser__ supports [TOML v0.4.0](http://github.com/toml-lang/toml/blob/master/README.md).
If you can use c++11 features, this parser depends only on the STL.
If not so, it depends on the Boost C++ library.
To use this parser with c++98 & boost, it is better that download the boost library
from the official site.
[日本語版README](README_ja.md)
## Usage
Because this is header-only library, you can use __TOMLParser__ without build.
You can easily see how to use it if you read the following sample codes.
To parse a TOML-file, use the `toml::Data toml::parse(std::basic_istream&)` function.
```cpp
std::ifstream ifs("sample.toml");
toml::Data data = toml::parse(ifs);
```
You can get a TOML-value using the `T toml::get` function.
```toml
# toml file
title = "title"
```
```cpp
std::string title = toml::get(data.at("title")); // "title"
```
You can also use the `T toml::get` function for an array.
```toml
# toml file
array = [3, 3.1, 3.14, 3.141, 3.1415]
```
```cpp
// {3, 3.1, 3.14, 3.141, 3.1415}
std::vector array = toml::get>(data.at("array"));
```
If you have a TOML-file like the following,
```toml
# sample.toml
title = "this is sample"
[table]
number = +100_000
reals = [-1.1e+2, 2.0, 3.0]
nested_array = [[true, false], # this is a comment.
[true],
[false]]
date = 1979-05-27
inline_table = {name = "inline table"}
array_of_inline_table = [
{key = "array"},
{key = "of"},
{key = "inline"},
{key = "table"},
]
[[array_of_table]]
foobar = 1
[[array_of_table]]
foobar = 2
[[array_of_table]]
foobar = 3
```
you can read and parse it using the c++11 code described below.
```cpp
#include "toml.hpp"
#include
int main()
{
std::ifstream file("sample.toml");
if(not file.good()) return 1;
toml::Data data = toml::parse(file);
std::string title = toml::get(data.at("title"));
toml::Table table = toml::get(data.at("table"));
std::int_least64_t number = toml::get(table.at("number");
std::vector reals =
toml::get>(table.at("reals"));
std::vector> nested_array =
toml::get>>(table.at("nested_array"));
std::chrono::system_clock::time_point date =
toml::get(table.at("date"));
toml::Table inline_table = toml::get(table.at("inline_table"));
std::string name = toml::get(inline_table.at("name"));
std::vector array_of_inline_table =
toml::get>(table.at("array_of_inline_table"));
std::string key0 = toml::get(array_of_inline_table.at(0).at("key"));
std::string key1 = toml::get(array_of_inline_table.at(1).at("key"));
std::string key2 = toml::get(array_of_inline_table.at(2).at("key"));
std::string key3 = toml::get(array_of_inline_table.at(3).at("key"));
std::vector array_of_table =
toml::get>(table.at("array_of_table"));
std::int_least64_t foobar0 = toml::get(array_of_table.at(0).at("foobar"));
std::int_least64_t foobar1 = toml::get(array_of_table.at(1).at("foobar"));
std::int_least64_t foobar2 = toml::get(array_of_table.at(2).at("foobar"));
return 0;
}
```
Even though the function `toml::get` is useful,
you cannot use it straightforwardly for a value like this.
```toml
array_of_array = [[1.0, 2.0, 3.0], ["a", "b", "c"]]
```
In this case, `toml::ValueBase` will help you.
```cpp
std::vector array_of_array =
toml::get>(data.at("array_of_array"));
std::vector first_array =
toml::get>(array_of_array.at(0));
std::vector second_array =
toml::get>(array_of_array.at(1));
```
In C++98, some of the types and methods are different.
For more detail, see Documentation.
## Documentation
In this parser, all the TOML types are the subclass of `toml::value_base`.
Actually, `toml::ValueBase` is just a `typedef` of `toml::shared_ptr`.
All the supported TOML types are listed below.
| TOML type | type names in this parser | actual type in c++11 |
|:----------|:--------------------------|:---------------------------------------|
| Boolean | `toml::Boolean` | `bool` |
| Integer | `toml::Integer` | `std::int_least64_t` |
| Float | `toml::Float` | `double` |
| String | `toml::String` | `std::string` |
| Datetime | `toml::Datetime` | `std::chrono::system_clock::time_point`|
| Array | `toml::Array` | `std::vector` |
| Table | `toml::Table` | `std::map>` |
Also, `toml::Data` is just a typedef of `toml::Table`.
In C++98, `toml::Array` become a struct that is a `type generator`
because there are no `template using alias`.
And there are no `at` method in `std::map`,
so you should use `operator[]` to access the values.
Additionally, there are no `std::chrono`, `std::int_least64_t`
and `std::shared_ptr`, so `boost::chrono`, `boost::int_least64_t`
and `boost::shared_ptr` are used instead.
| TOML type | type names in this parser | actual type in c++98 |
|:----------|:--------------------------|:-----------------------------------------|
| Boolean | `toml::Boolean` | `bool` |
| Integer | `toml::Integer` | `boost::int_least64_t` |
| Float | `toml::Float` | `double` |
| String | `toml::String` | `std::string` |
| Datetime | `toml::Datetime` | `boost::chrono::system_clock::time_point`|
| Array | `toml::Array::type` | `std::vector` |
| Table | `toml::Table` | `std::map>` |
So, the different part of the sample code in C++98 is below.
```cpp
boost::int_least64_t number = toml::get(table.at("number");
boost::chrono::system_clock::time_point date =
toml::get(table.at("date"));
std::vector reals =
toml::get::type>(table.at("reals"));
// If you do not want to write "::type", you can use std::vector instead of toml::Array.
// std::vector reals =
// toml::get>(table.at("reals"));
```
__Note__: When you install boost using apt or some other package manager
that provides binaries already built, some source files possibly does not exist.
If you fail to build your own project that includes this parser using boost and c++98
with an error message like "boost/../libs/something not found", please download
boost library from the official site.
The reason of this problem is you and this parser are forcing to use
```boost/chrono``` as header-only.
### Excpetions
If the syntax is invalid or some sort of an internal error occurs,
exception ```toml::syntax_error``` or ```toml::internal_error``` is thrown by
the function ```toml::parse```, respectively.
If an invalid type is set as a template parameter of the ```toml::get``` function,
exception ```toml::type_error``` is thrown.
All the exception classes are the subclass of ```toml::exception``` which is
the subclass of ```std::exception```.
## Testing
Using Boost Unit Test Framework and CMake CTest.
```
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make test
```
## Licensing terms
This project is licensed under the terms of the MIT License.
See LICENSE for the project license.
- Copyright (c) 2016 Toru Niina
All rights reserved.