# libtoml **Repository Path**: shadowyuan/libtoml ## Basic Information - **Project Name**: libtoml - **Description**: C++11 implementation of TOML Parser - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-09-07 - **Last Updated**: 2022-10-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LIBTOML LIBTOML Is A TOML Reader And Writer In C++11 ## TODO List 1. Writer ## Usage ### Compile ```bash git clone https://gitee.com/shadowyuan/libtoml.git cd libtoml mkdir build cd build ``` If Linux ``` cmake .. -DCMAKE_BUILD_TYPE=Release make ``` If Windows ``` cmake .. -G "Visual Studio 15 2017 Win64" cmake --build . --config Release ``` ### Example There is a toml file: ``` [server] host = "example.com" port = [ 8080, 8181, 8282 ] ``` TOML Parser: ```c++ #include #include #include int main(int argc, char *argv[]) { char buff[] = R"([server] host = "example.com" port = [ 8080, 8181, 8282 ] )"; std::string error_desc; // TOML::Node root = TOML::LoadFromString(buff, strlen(buff), &error_desc); std::cout << "error desc:" << error_desc << std::endl; // check key exists bool r = root.AsTable()->Exists("server"); std::cout << "server exists result:" << r << std::endl; // or just call 'Get' TOML::Node server = root.AsTable()->Get("server"); if (!server) { std::cout << "server does not exists" << std::endl; return 0; } std::cout << "server exists" << std::endl; // get host TOML::Node host = server.As()->Get("host"); std::cout << "host type: " << host.TypeString() << std::endl; if (host.Type() == TOML::kString) { std::string host_str = host.As()->Value(); std::cout << "host value: " << host_str << std::endl; } // get port TOML::Node port = server.As()->Get("port"); std::cout << "port type: " << port.TypeString() << std::endl; if (port.Type() == TOML::kArray) { int count = static_cast(port.AsArray()->size()); for (int i = 0; i < count; i++) { TOML::Node value = port.AsArray()->At(i); std::cout << "value type: " << value.TypeString() << std::endl; if (value.Type() == TOML::kInteger) { int64_t num = value.As()->Value(); std::cout << "port[" << i << "] is " << num << std::endl; } // or just get string std::string s = value.ToString(); std::cout << "port[" << i << "] is " << s << std::endl; } } return 0; } ```