GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
file_tree.cpp
[詳解]
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#include "file_tree.h"
9
10#include <filesystem>
11
12#include "cassert_define.h"
13#include "cmdio_util.h"
14
15namespace designlab {
16
17void FileTree::DisplayFileTree(const std::string& path, int max_depth) const {
18 FileTreeData tree = MakeFileTree(path, max_depth, "", "sim");
19
20 int count = 0;
21 OutputFileTree(tree, 0, true, &count);
22}
23
24bool FileTree::SelectFile(const std::string& path, int max_depth,
25 const std::string& extension,
26 const std::string keyword,
27 std::string* output) const {
28 assert(output != nullptr);
29
30 // ファイルツリーを作成
31 FileTreeData tree = MakeFileTree(path, max_depth, extension, keyword);
32
33 // ファイルツリーを表示
35
36 int count = 0;
37 OutputFileTree(tree, 0, true, &count);
41
42 // ファイルを選択
43 std::vector<std::string> file_list = MakeFileList(tree);
44
45 if (file_list.empty()) {
46 cmdio::Output("The file did not exist.", OutputDetail::kSystem);
47 return false;
48 }
49
50 while (true) {
51 int select_index =
52 cmdio::InputInt(0, static_cast<int>(file_list.size()) - 1, 0,
53 "Please select a file. Enter an integer.");
54
57 "The selected file is " + file_list[static_cast<size_t>(select_index)],
59
60 if (cmdio::InputYesNo("Are you sure?")) {
61 *output = file_list[static_cast<size_t>(select_index)];
62 break;
63 }
64
66 }
67
68 return true;
69}
70
71FileTree::FileTreeData FileTree::MakeFileTree(const std::string& path,
72 int max_depth,
73 const std::string& extension,
74 const std::string keyword) const {
75 FileTreeData tree;
76
77 tree.path = path;
78
79 assert(std::filesystem::exists(tree.path));
80
81 for (const auto& entry : std::filesystem::directory_iterator(path)) {
82 if (entry.is_directory()) {
83 // ディレクトリの場合、再帰的にファイルツリーを作成
84 if (max_depth == 0) {
85 tree.directory.push_back(FileTreeData{entry.path().string(), {}, {}});
86 } else {
87 tree.directory.push_back(MakeFileTree(
88 entry.path().string(), max_depth - 1, extension, keyword));
89 }
90 } else if (entry.is_regular_file()) {
91 // ファイルの場合、ファイル名を追加
92
93 if (!extension.empty()) {
94 if (!(entry.path().extension().string() == extension ||
95 entry.path().extension().string() == "." + extension)) {
96 continue;
97 }
98 }
99
100 if (!keyword.empty()) {
101 if (entry.path().filename().string().find(keyword) ==
102 std::string::npos) {
103 continue;
104 }
105 }
106
107 tree.file.push_back(entry.path().filename().string());
108 }
109 }
110
111 return tree;
112}
113
114void FileTree::OutputFileTree(const FileTreeData& tree, int depth,
115 bool not_display_empty, int* file_count) const {
116 assert(file_count != nullptr);
117
118 std::string indent = "";
119 for (int i = 0; i < depth; ++i) {
120 indent += "| ";
121 }
122
123 // 空 かつ 空のディレクトリを表示しない設定 でないならば表示する
124 if (!(not_display_empty && tree.file.empty() && tree.directory.empty())) {
125 // ディレクトリ名を出力する際に,パスの階層を削除する
126
128
129 std::string::size_type pos = tree.path.find_last_of("/\\");
130 std::string dir_name = ((depth == 0) ? "" : "- ");
131 dir_name +=
132 std::string("[ ") + tree.path.substr(pos + 1) + std::string(" ]");
133
134 cmdio::Output(indent + dir_name, OutputDetail::kSystem);
135 }
136
137 for (const auto& directory : tree.directory) {
138 OutputFileTree(directory, depth + 1, not_display_empty, file_count);
139 }
140
141 if (!tree.file.empty()) {
143
144 for (const auto& file : tree.file) {
146 indent + "|- " + file + " [-" + std::to_string(*file_count) + "-]",
148
149 (*file_count)++;
150 }
151 }
152}
153
154std::vector<std::string> FileTree::MakeFileList(
155 const FileTreeData& tree) const {
156 std::vector<std::string> file_list;
157
158 for (const auto& directory : tree.directory) {
159 std::vector<std::string> tmp = MakeFileList(directory);
160
161 file_list.insert(file_list.end(), tmp.begin(), tmp.end());
162 }
163
164 for (const auto& file : tree.file) {
165 file_list.push_back(tree.path + "\\" + file);
166 }
167
168 return file_list;
169}
170
171} // namespace designlab
void DisplayFileTree(const std::string &path, int max_depth) const
ファイルツリーを表示する.
Definition file_tree.cpp:17
bool SelectFile(const std::string &path, int max_depth, const std::string &extension, const std::string keyword, std::string *output) const
ディレクトリの中から,ファイルを一つ選択する.
Definition file_tree.cpp:24
void OutputHorizontalLine(const std::string &line_visual, OutputDetail detail)
コマンドラインに水平線を出力する関数.
int InputInt(int min, int max, int default_num, const std::string &str="Please enter an integer.")
整数を入力させる関数. 出力される文字列は, 必ず OutputDetail::kSystem で出力される.
void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit 関数で設定した出力の許可範囲内であれば出力される.
bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true, no なら false を返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
@ kSystem
システムメッセージ,常に出力する.
Definition com_type.h:21