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 <string>
14#include <map>
15#include <vector>
16
17#include "cmdio_util.h"
19#include "string_util.h"
20#include "toml11_define.h"
21
22
23namespace designlab
24{
25
27template <typename T>
28concept HasIntoToml = impl::has_into_toml<T>::value && std::is_default_constructible_v<T>;
29
30
36template <HasIntoToml T>
38{
39public:
43 void Export(const std::string& file_path, const T& data)
44 {
45 const toml::basic_value<toml::preserve_comments, std::map> value(data);
46 std::string res_str = toml::format(value); // 設定を文字列に変換.
47
48 InsertNewLine(&res_str); // #をみたら,改行を挿入する.
49
50 IndentTable(&res_str); // Tableの中身をインデントする.
51
52 std::ofstream ofs;
53 ofs.open(file_path);
54
55 // ファイルが開けなかったら何もしない.
56 if (!ofs)
57 {
58 CmdIOUtil::Output("Failed to output TOML file. file_path : " + file_path, OutputDetail::kSystem);
59 return;
60 }
61
62 ofs.write(res_str.c_str(), res_str.length()); // ファイルに書き込む.
63
64 ofs.close(); // ファイルを閉じる.
65
66 CmdIOUtil::Output("TOML files are output. file_path : " + file_path, OutputDetail::kSystem);
67 }
68
69private:
71 void InsertNewLine(std::string* str)
72 {
73 if (str == nullptr)
74 {
75 return;
76 }
77
78 std::vector<std::string> split_str = string_util::Split((*str), "\n");
79
80 std::string res_str;
81 char past_first_char = ' ';
82
83 for (const auto& s : split_str)
84 {
85 if (s.size() != 0 &&
86 s[0] == '#' &&
87 past_first_char != '#' &&
88 past_first_char != ' ')
89 {
90 res_str += "\n";
91 }
92
93 past_first_char = s.size() != 0 ? s[0] : ' ';
94
95 res_str += s + "\n";
96 }
97
98 *str = res_str;
99 }
100
103 void IndentTable(std::string* str)
104 {
105 if (str == nullptr)
106 {
107 return;
108 }
109
110 std::vector<std::string> split_str = string_util::Split((*str), "\n");
111
112 std::string res_str;
113 bool do_indent = false;
114
115 for (size_t i = 0; i < split_str.size(); i++)
116 {
117 std::string past_s = i != 0 ? split_str[i - 1] : "";
118 std::string next_s = i != split_str.size() - 1 ? split_str[i + 1] : "";
119 std::string s = split_str[i];
120
121 if (past_s.size() != 0 && past_s[0] == '[')
122 {
123 do_indent = true;
124 }
125
126 if (next_s.size() != 0 && next_s[0] == '[')
127 {
128 do_indent = false;
129 }
130
131 if (do_indent)
132 {
133 res_str += " ";
134 }
135
136 res_str += s + "\n";
137 }
138
139 *str = res_str;
140 }
141};
142
143} // namespace designlab
144
145
146#endif // DESIGNLAB_TOML_FILE_EXPORTER_H_
static void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
TOMLファイルを出力するテンプレートクラス.
void Export(const std::string &file_path, const T &data)
TOMLファイルを出力する.
into<T>を持つか判定するコンセプト.
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,それを無視する.
@ kSystem
システムメッセージ,常に出力する.