Files
toml11/docs/features/parsing_files/index.html
ToruNiina e671b8e3bd Merge pull request #291 from franzpoeschel/namespace-versioning
Use an inline namespace to distinguish emitted symbols by version numbers e4051f77d7
2025-05-17 07:21:19 +00:00

144 lines
34 KiB
HTML

<!doctype html><html lang=en dir=ltr><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="
Parsing Files and Strings
#
In toml11, you can parse files, strings, and byte arrays using toml::parse or toml::try_parse.
Upon success, these functions return a toml::value.
Although the parsed file is always a table, the return type is not toml::table.
This is because toml::value contains metadata about the file, whereas toml::table is merely an alias for std::unordered_map<std::string, toml::value>.
To include metadata, a toml::value is returned instead of a toml::table.
The toml::value corresponding to the root of the file will always hold a table_type."><meta name=theme-color media="(prefers-color-scheme: light)" content="#ffffff"><meta name=theme-color media="(prefers-color-scheme: dark)" content="#343a40"><meta name=color-scheme content="light dark"><meta property="og:url" content="https://toruniina.github.io/toml11/docs/features/parsing_files/"><meta property="og:site_name" content="toml11"><meta property="og:title" content="parsing files"><meta property="og:description" content="Parsing Files and Strings # In toml11, you can parse files, strings, and byte arrays using toml::parse or toml::try_parse.
Upon success, these functions return a toml::value. Although the parsed file is always a table, the return type is not toml::table. This is because toml::value contains metadata about the file, whereas toml::table is merely an alias for std::unordered_map<std::string, toml::value>. To include metadata, a toml::value is returned instead of a toml::table. The toml::value corresponding to the root of the file will always hold a table_type."><meta property="og:locale" content="en"><meta property="og:type" content="article"><meta property="article:section" content="docs"><title>parsing files | toml11</title>
<link rel=manifest href=/toml11/manifest.json><link rel=icon href=/toml11/favicon.png><link rel=canonical href=https://toruniina.github.io/toml11/docs/features/parsing_files/><link rel=alternate hreflang=ja href=https://toruniina.github.io/toml11/ja/docs/features/parsing_files/ title="parsing files"><link rel=stylesheet href=/toml11/book.min.ddac3efe7ea4c1007ff53e5a8fd67f12517e5d2e99984d8c67a18668f7ecaa03.css integrity="sha256-3aw+/n6kwQB/9T5aj9Z/ElF+XS6ZmE2MZ6GGaPfsqgM=" crossorigin=anonymous><script defer src=/toml11/fuse.min.js></script><script defer src=/toml11/en.search.min.db4a558804ac41f081539588cec8e4a415587801da466728166bb5341dd62411.js integrity="sha256-20pViASsQfCBU5WIzsjkpBVYeAHaRmcoFmu1NB3WJBE=" crossorigin=anonymous></script></head><body dir=ltr><input type=checkbox class="hidden toggle" id=menu-control>
<input type=checkbox class="hidden toggle" id=toc-control><main class="container flex"><aside class=book-menu><div class=book-menu-content><nav><h2 class=book-brand><a class="flex align-center" href=/toml11/><span>toml11</span></a></h2><div class="book-search hidden"><input type=text id=book-search-input placeholder=Search aria-label=Search maxlength=64 data-hotkeys=s/><div class="book-search-spinner hidden"></div><ul id=book-search-results></ul></div><script>document.querySelector(".book-search").classList.remove("hidden")</script><ul class=book-languages><li><input type=checkbox id=languages class=toggle>
<label for=languages class="flex justify-between"><a role=button class="flex align-center"><img src=/toml11/svg/translate.svg class=book-icon alt=Languages>
English</a></label><ul><li><a href=https://toruniina.github.io/toml11/ja/docs/features/parsing_files/>日本語</a></li></ul></li></ul><ul><li><a href=/toml11/docs/installation/>installation</a><ul></ul></li><li><input type=checkbox id=section-291123c850af98d8b815ccdbb4c33657 class=toggle checked>
<label for=section-291123c850af98d8b815ccdbb4c33657 class="flex justify-between"><a href=/toml11/docs/features/>features</a></label><ul><li><a href=/toml11/docs/features/parsing_files/ class=active>parsing files</a></li><li><a href=/toml11/docs/features/value/>getting values</a></li><li><a href=/toml11/docs/features/error_message/>error message</a></li><li><a href=/toml11/docs/features/serialize/>serializing values</a></li><li><a href=/toml11/docs/features/configure_types/>configuring types</a></li><li><a href=/toml11/docs/features/literal/>toml literal</a></li><li><a href=/toml11/docs/features/toml_spec/>toml spec</a></li><li><a href=/toml11/docs/features/extension/>extension</a></li></ul></li><li><input type=checkbox id=section-e2e812e55021b82e114feaae98f1a02e class=toggle>
<label for=section-e2e812e55021b82e114feaae98f1a02e class="flex justify-between"><a href=/toml11/docs/reference/>reference</a></label><ul><li><a href=/toml11/docs/reference/color/>color.hpp</a></li><li><a href=/toml11/docs/reference/comments/>comments.hpp</a></li><li><a href=/toml11/docs/reference/conversion/>conversion.hpp</a></li><li><a href=/toml11/docs/reference/datetime/>datetime.hpp</a></li><li><a href=/toml11/docs/reference/error_info/>error_info.hpp</a></li><li><a href=/toml11/docs/reference/exception/>exception.hpp</a></li><li><a href=/toml11/docs/reference/find/>find.hpp</a></li><li><a href=/toml11/docs/reference/format/>format.hpp</a></li><li><a href=/toml11/docs/reference/from/>from.hpp</a></li><li><a href=/toml11/docs/reference/get/>get.hpp</a></li><li><a href=/toml11/docs/reference/into/>into.hpp</a></li><li><a href=/toml11/docs/reference/literal/>literal.hpp</a></li><li><a href=/toml11/docs/reference/ordered_map/>ordered_map.hpp</a></li><li><a href=/toml11/docs/reference/parser/>parser.hpp</a></li><li><a href=/toml11/docs/reference/result/>result.hpp</a></li><li><a href=/toml11/docs/reference/serializer/>serializer.hpp</a></li><li><a href=/toml11/docs/reference/source_location/>source_location.hpp</a></li><li><a href=/toml11/docs/reference/spec/>spec.hpp</a></li><li><a href=/toml11/docs/reference/toml_fwd/>toml_fwd.hpp</a></li><li><a href=/toml11/docs/reference/toml/>toml.hpp</a></li><li><a href=/toml11/docs/reference/types/>types.hpp</a></li><li><a href=/toml11/docs/reference/value_t/>value_t.hpp</a></li><li><a href=/toml11/docs/reference/value/>value.hpp</a></li><li><a href=/toml11/docs/reference/version/>version.hpp</a></li><li><a href=/toml11/docs/reference/visit/>visit.hpp</a></li></ul></li><li><a href=/toml11/docs/changelog/>changelog</a><ul></ul></li></ul><ul><li><a href=https://github.com/ToruNiina/toml11 target=_blank rel=noopener>GitHub</a></li></ul></nav><script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script></div></aside><div class=book-page><header class=book-header><div class="flex align-center justify-between"><label for=menu-control><img src=/toml11/svg/menu.svg class=book-icon alt=Menu>
</label><strong>parsing files</strong>
<label for=toc-control><img src=/toml11/svg/toc.svg class=book-icon alt="Table of Contents"></label></div><aside class="hidden clearfix"><nav id=TableOfContents><ul><li><a href=#parsing-files-and-strings>Parsing Files and Strings</a><ul><li><a href=#parsing-files>Parsing Files</a><ul><li><a href=#tomlparse><code>toml::parse</code></a></li><li><a href=#tomltry_parse><code>toml::try_parse</code></a></li></ul></li><li><a href=#parsing-strings>Parsing Strings</a><ul><li><a href=#tomlparse_str><code>toml::parse_str</code></a></li><li><a href=#tomltry_parse_str><code>toml::try_parse_str</code></a></li></ul></li><li><a href=#parsing-byte-arrays>Parsing Byte Arrays</a><ul><li><a href=#tomlparsestdvectorunsigned-char><code>toml::parse(std::vector&lt;unsigned char>)</code></a></li><li><a href=#tomltry_parsestdvectorunsigned-char><code>toml::try_parse(std::vector&lt;unsigned char>)</code></a></li></ul></li></ul></li></ul></nav></aside></header><article class="markdown book-article"><h1 id=parsing-files-and-strings>Parsing Files and Strings
<a class=anchor href=#parsing-files-and-strings>#</a></h1><p>In toml11, you can parse files, strings, and byte arrays using <code>toml::parse</code> or <code>toml::try_parse</code>.</p><p>Upon success, these functions return a <code>toml::value</code>.
Although the parsed file is always a table, the return type is not <code>toml::table</code>.
This is because <code>toml::value</code> contains metadata about the file, whereas <code>toml::table</code> is merely an alias for <code>std::unordered_map&lt;std::string, toml::value></code>.
To include metadata, a <code>toml::value</code> is returned instead of a <code>toml::table</code>.
The <code>toml::value</code> corresponding to the root of the file will always hold a <code>table_type</code>.</p><h2 id=parsing-files>Parsing Files
<a class=anchor href=#parsing-files>#</a></h2><p>To parse files, use either
<a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a>
or
<a href=https://toruniina.github.io/toml11/docs/reference/parser/#try_parse><code>toml::try_parse</code></a>.</p><h3 id=tomlparse><code>toml::parse</code>
<a class=anchor href=#tomlparse>#</a></h3><h4 id=specifying-the-filename-with-stdstring>Specifying the Filename with <code>std::string</code>
<a class=anchor href=#specifying-the-filename-with-stdstring>#</a></h4><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a>
accepts a filename as a string, opens the file, and parses it.</p><p>The following sample code parses a file named <code>input.toml</code>, extracts the <code>title</code> variable as a string, and prints it.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value input <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>parse(<span style=color:#e6db74>&#34;input.toml&#34;</span>);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> input.at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h4 id=specifying-a-file-with-stdfilesystempath>Specifying a File with <code>std::filesystem::path</code>
<a class=anchor href=#specifying-a-file-with-stdfilesystempath>#</a></h4><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a> can accept a <code>std::filesystem::path</code>.</p><p>This requires C++17 or later, as it relies on the <code>&lt;filesystem></code> support.</p><h4 id=specifying-an-input-stream-with-stdistream>Specifying an Input Stream with <code>std::istream</code>
<a class=anchor href=#specifying-an-input-stream-with-stdistream>#</a></h4><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a> can also accept an <code>std::istream</code>.</p><p>Open a stream in binary mode by passing <code>std::ios::binary</code> to avoid inconsistency between the file size and the number of characters due to automatic conversion of newline characters by the standard library.</p><p>Without the filename information, error messages will display <code>"unknown file"</code>. To avoid this, you can pass the filename as a <code>std::string</code> in the second argument when using <code>std::istream</code>.</p><p>You can use streams other than <code>std::ifstream</code>, such as <code>std::istringstream</code>. Note that the entire content is readable at the time of the call.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>string filename(<span style=color:#e6db74>&#34;input.toml&#34;</span>);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>ifstream ifs(filename);
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value input <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>parse(ifs, filename);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> input.at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h4 id=specifying-a-file-with-file>Specifying a File with <code>FILE*</code>
<a class=anchor href=#specifying-a-file-with-file>#</a></h4><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a> can also accept a <code>FILE*</code>.</p><p>Open a stream in binary mode by passing <code>"rb"</code> to avoid inconsistency between the file size and the number of characters due to automatic conversion of newline characters by the standard library.</p><p>As with <code>std::istream</code>, you need to provide the filename as a string in the second argument.</p><p>When passing a <code>FILE*</code>, if the file read fails, <code>errno</code> will be reported.</p><h4 id=error-handling>Error Handling
<a class=anchor href=#error-handling>#</a></h4><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a> throws a <a href=https://toruniina.github.io/toml11/docs/reference/parser/#syntax_error><code>toml::syntax_error</code></a> if it encounters a syntax error.</p><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#syntax_error><code>toml::syntax_error</code></a> is derived from <a href=https://toruniina.github.io/toml11/docs/reference/exception/><code>toml::exception</code></a>, which in turn is derived from <code>std::exception</code>.</p><p>Therefore, you can use the <code>what()</code> member function to retrieve the error message from a <a href=https://toruniina.github.io/toml11/docs/reference/parser/#syntax_error><code>toml::syntax_error</code></a>.</p><p>Additionally, <a href=https://toruniina.github.io/toml11/docs/reference/parser/#syntax_error><code>toml::syntax_error</code></a> contains a <a href=https://toruniina.github.io/toml11/docs/reference/error_info/><code>std::vector&lt;toml::error_info></code></a>, which can be accessed using the <code>errors()</code> member function.</p><p><code>toml::parse</code> attempts to recover from minor errors and report multiple errors whenever possible. While it can often recover from simple errors like number format issues, errors within arrays or tables might not be recoverable and may lead to multiple similar error reports. If you find the error messages redundant, you can use only the <code>front()</code> of the <code>std::vector&lt;toml::error_info></code> to get a message about the most critical issue.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> <span style=color:#66d9ef>try</span> {
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value input <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>parse(<span style=color:#e6db74>&#34;input.toml&#34;</span>);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> input.at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> } <span style=color:#66d9ef>catch</span>(<span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>syntax_error<span style=color:#f92672>&amp;</span> err) {
</span></span><span style=display:flex><span> <span style=color:#75715e>// report all the errors
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672>&lt;&lt;</span> err.what() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span> <span style=color:#75715e>// report the first error only
</span></span></span><span style=display:flex><span><span style=color:#75715e></span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672>&lt;&lt;</span> err.errors().front() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h3 id=tomltry_parse><code>toml::try_parse</code>
<a class=anchor href=#tomltry_parse>#</a></h3><p>While <code>toml::parse</code> throws an exception on failure, <code>toml::try_parse</code> does not throw exceptions when it fails.</p><p>Instead, its return type is <a href=https://toruniina.github.io/toml11/docs/reference/result/#result><code>toml::result&lt;toml::value, std::vector&lt;toml::error_info>></code></a>.</p><p>The <a href=https://toruniina.github.io/toml11/docs/reference/result/#result><code>result</code></a> type holds either a success value or a failure value, similar to Rust&rsquo;s <code>Result</code> or Haskell&rsquo;s <code>Either</code>.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> <span style=color:#66d9ef>auto</span> parse_result <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>try_parse(<span style=color:#e6db74>&#34;input.toml&#34;</span>);
</span></span><span style=display:flex><span> <span style=color:#66d9ef>if</span>(parse_result.is_ok())
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap().at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>else</span>
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap_err().at(<span style=color:#ae81ff>0</span>) <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><p>To check which value the <a href=https://toruniina.github.io/toml11/docs/reference/result/#result><code>result</code></a> type holds, use the <code>is_ok()</code> and <code>is_err()</code> functions. The success or failure value can be retrieved using <code>unwrap()</code> and <code>unwrap_err()</code>, respectively. If <code>unwrap</code> fails, it throws a <code>bad_result_access</code> exception. Using the <code>as_ok()</code> and <code>as_err()</code> functions does not throw exceptions on failure, but results in undefined behavior.</p><blockquote class="book-hint warning"><p>Although <code>try_parse</code> does not throw <code>syntax_error</code> or <code>file_io_error</code>, it returns the same <code>toml::error_info</code> as the failure type in the <code>result</code>. However, it is not entirely exception-free.</p><p>If an internal standard library error occurs, such as <code>std::bad_alloc</code> when a <code>vector</code> fails to allocate memory, <code>toml::try_parse</code> does not catch this and will let it propagate. Thus, exceptions originating from the standard library may still be thrown.</p></blockquote><h2 id=parsing-strings>Parsing Strings
<a class=anchor href=#parsing-strings>#</a></h2><h3 id=tomlparse_str><code>toml::parse_str</code>
<a class=anchor href=#tomlparse_str>#</a></h3><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse_str><code>toml::parse_str</code></a> accepts the string to be parsed directly, instead of a filename.</p><p>For the part of the error message that corresponds to the TOML file&rsquo;s name, if the <code>std::source_location</code> equivalent compiler extension is available, the name and line number of the C++ file that called <code>parse_str</code> will be used instead.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value input <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>parse_str(<span style=color:#e6db74>&#34;title = </span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>parse_str</span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>&#34;</span>);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> input.at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h3 id=tomltry_parse_str><code>toml::try_parse_str</code>
<a class=anchor href=#tomltry_parse_str>#</a></h3><p><a href=https://toruniina.github.io/toml11/docs/reference/parser/#try_parse_str><code>toml::try_parse_str</code></a> also takes the string to be parsed directly, similar to <code>parse_str</code>. Like <code>try_parse</code>, it uses <a href=https://toruniina.github.io/toml11/docs/reference/result/#result><code>toml::result</code></a> to report errors.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> <span style=color:#66d9ef>auto</span> parse_result <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>try_parse_str(<span style=color:#e6db74>&#34;title = </span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>parse_str</span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>&#34;</span>);
</span></span><span style=display:flex><span> <span style=color:#66d9ef>if</span>(parse_result.is_ok())
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap().at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>else</span>
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap_err().at(<span style=color:#ae81ff>0</span>) <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h2 id=parsing-byte-arrays>Parsing Byte Arrays
<a class=anchor href=#parsing-byte-arrays>#</a></h2><p>It is also possible to parse byte arrays instead of files.</p><p>Since the byte arrays must be encoded in UTF-8, <code>unsigned char</code> is used.</p><h3 id=tomlparsestdvectorunsigned-char><code>toml::parse(std::vector&lt;unsigned char>)</code>
<a class=anchor href=#tomlparsestdvectorunsigned-char>#</a></h3><p>The behavior is the same as <a href=https://toruniina.github.io/toml11/docs/reference/parser/#parse><code>toml::parse</code></a>.</p><p>When parsing byte arrays, a <code>filename</code> is required.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>vector<span style=color:#f92672>&lt;</span><span style=color:#66d9ef>unsigned</span> <span style=color:#66d9ef>char</span><span style=color:#f92672>&gt;</span> bytes{<span style=color:#75715e>/* ... */</span>};
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> toml<span style=color:#f92672>::</span>value input <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>parse(bytes, <span style=color:#e6db74>&#34;internal bytes&#34;</span>);
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> input.at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div><h3 id=tomltry_parsestdvectorunsigned-char><code>toml::try_parse(std::vector&lt;unsigned char>)</code>
<a class=anchor href=#tomltry_parsestdvectorunsigned-char>#</a></h3><p>The behavior is the same as <a href=https://toruniina.github.io/toml11/docs/reference/parser/#try_parse><code>toml::try_parse</code></a>.</p><p>When parsing byte arrays, a <code>filename</code> is required.</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-cpp data-lang=cpp><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;toml.hpp&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e>#include</span> <span style=color:#75715e>&lt;iostream&gt;</span><span style=color:#75715e>
</span></span></span><span style=display:flex><span><span style=color:#75715e></span>
</span></span><span style=display:flex><span><span style=color:#66d9ef>int</span> <span style=color:#a6e22e>main</span>()
</span></span><span style=display:flex><span>{
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>vector<span style=color:#f92672>&lt;</span><span style=color:#66d9ef>unsigned</span> <span style=color:#66d9ef>char</span><span style=color:#f92672>&gt;</span> bytes{<span style=color:#75715e>/* ... */</span>};
</span></span><span style=display:flex><span> <span style=color:#66d9ef>const</span> <span style=color:#66d9ef>auto</span> parse_result <span style=color:#f92672>=</span> toml<span style=color:#f92672>::</span>try_parse(bytes, <span style=color:#e6db74>&#34;internal bytes&#34;</span>);
</span></span><span style=display:flex><span> <span style=color:#66d9ef>if</span>(parse_result.is_ok())
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cout <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap().at(<span style=color:#e6db74>&#34;title&#34;</span>).as_string() <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>else</span>
</span></span><span style=display:flex><span> {
</span></span><span style=display:flex><span> std<span style=color:#f92672>::</span>cerr <span style=color:#f92672>&lt;&lt;</span> parse_result.unwrap_err().at(<span style=color:#ae81ff>0</span>) <span style=color:#f92672>&lt;&lt;</span> std<span style=color:#f92672>::</span>endl;
</span></span><span style=display:flex><span> }
</span></span><span style=display:flex><span> <span style=color:#66d9ef>return</span> <span style=color:#ae81ff>0</span>;
</span></span><span style=display:flex><span>}
</span></span></code></pre></div></article><footer class=book-footer><div class="flex flex-wrap justify-between"></div><script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script></footer><label for=menu-control class="hidden book-menu-overlay"></label></div><aside class=book-toc><div class=book-toc-content><nav id=TableOfContents><ul><li><a href=#parsing-files-and-strings>Parsing Files and Strings</a><ul><li><a href=#parsing-files>Parsing Files</a><ul><li><a href=#tomlparse><code>toml::parse</code></a></li><li><a href=#tomltry_parse><code>toml::try_parse</code></a></li></ul></li><li><a href=#parsing-strings>Parsing Strings</a><ul><li><a href=#tomlparse_str><code>toml::parse_str</code></a></li><li><a href=#tomltry_parse_str><code>toml::try_parse_str</code></a></li></ul></li><li><a href=#parsing-byte-arrays>Parsing Byte Arrays</a><ul><li><a href=#tomlparsestdvectorunsigned-char><code>toml::parse(std::vector&lt;unsigned char>)</code></a></li><li><a href=#tomltry_parsestdvectorunsigned-char><code>toml::try_parse(std::vector&lt;unsigned char>)</code></a></li></ul></li></ul></li></ul></nav></div></aside></main></body></html>