GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
toml_file_exporter.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_TOML_FILE_EXPORTER_H_
9#define DESIGNLAB_TOML_FILE_EXPORTER_H_
10
11#include <filesystem>
12#include <fstream>
13#include <map>
14#include <string>
15#include <vector>
16
17#include "cmdio_util.h"
19#include "string_util.h"
20#include "toml11_define.h"
21
22namespace designlab {
23
25template <typename T>
26concept HasIntoToml =
27 impl::has_into_toml<T>::value && std::is_default_constructible_v<T>;
28
34template <HasIntoToml T>
35class TomlFileExporter final {
36 public:
40 void Export(const std::string& file_path, const T& data) {
41 const toml::basic_value<toml::preserve_comments, std::map> value(data);
42 std::string res_str = toml::format(value); // 設定を文字列に変換.
43
44 InsertNewLine(&res_str); // #をみたら,改行を挿入する.
45
46 IndentTable(&res_str); // Tableの中身をインデントする.
47
48 std::ofstream ofs;
49 ofs.open(file_path);
50
51 // ファイルが開けなかったら何もしない.
52 if (!ofs) {
53 cmdio::Output("Failed to output TOML file. file_path : " + file_path,
55 return;
56 }
57
58 ofs.write(res_str.c_str(), res_str.length()); // ファイルに書き込む.
59
60 ofs.close(); // ファイルを閉じる.
61
62 cmdio::Output("TOML files are output. file_path : " + file_path,
64 }
65
66 private:
68 void InsertNewLine(std::string* str) {
69 if (str == nullptr) {
70 return;
71 }
72
73 std::vector<std::string> split_str = string_util::Split((*str), "\n");
74
75 std::string res_str;
76 char past_first_char = ' ';
77
78 for (const auto& s : split_str) {
79 if (s.size() != 0 && s[0] == '#' && past_first_char != '#' &&
80 past_first_char != ' ') {
81 res_str += "\n";
82 }
83
84 past_first_char = s.size() != 0 ? s[0] : ' ';
85
86 res_str += s + "\n";
87 }
88
89 *str = res_str;
90 }
91
94 void IndentTable(std::string* str) {
95 if (str == nullptr) {
96 return;
97 }
98
99 std::vector<std::string> split_str = string_util::Split((*str), "\n");
100
101 std::string res_str;
102 bool do_indent = false;
103
104 for (size_t i = 0; i < split_str.size(); i++) {
105 std::string past_s = i != 0 ? split_str[i - 1] : "";
106 std::string next_s = i != split_str.size() - 1 ? split_str[i + 1] : "";
107 std::string s = split_str[i];
108
109 if (past_s.size() != 0 && past_s[0] == '[') {
110 do_indent = true;
111 }
112
113 if (next_s.size() != 0 && next_s[0] == '[') {
114 do_indent = false;
115 }
116
117 if (do_indent) {
118 res_str += " ";
119 }
120
121 res_str += s + "\n";
122 }
123
124 *str = res_str;
125 }
126};
127
128} // namespace designlab
129
130#endif // DESIGNLAB_TOML_FILE_EXPORTER_H_
TOMLファイルを出力するテンプレートクラス.
void Export(const std::string &file_path, const T &data)
TOMLファイルを出力する.
into<T>を持つか判定するコンセプト.
void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit 関数で設定した出力の許可範囲内であれば出力される.
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,...
@ kSystem
システムメッセージ,常に出力する.