18 FileTreeData tree = MakeFileTree(path, max_depth,
"",
"sim");
21 OutputFileTree(tree, 0,
true, &count);
25 const std::string& extension,
26 const std::string keyword,
27 std::string* output)
const {
28 assert(output !=
nullptr);
31 FileTreeData tree = MakeFileTree(path, max_depth, extension, keyword);
37 OutputFileTree(tree, 0,
true, &count);
43 std::vector<std::string> file_list = MakeFileList(tree);
45 if (file_list.empty()) {
53 "Please select a file. Enter an integer.");
57 "The selected file is " + file_list[
static_cast<size_t>(select_index)],
61 *output = file_list[
static_cast<size_t>(select_index)];
71FileTree::FileTreeData FileTree::MakeFileTree(
const std::string& path,
73 const std::string& extension,
74 const std::string keyword)
const {
79 assert(std::filesystem::exists(tree.path));
81 for (
const auto& entry :
std::filesystem::directory_iterator(path)) {
82 if (entry.is_directory()) {
85 tree.directory.push_back(FileTreeData{entry.path().
string(), {}, {}});
87 tree.directory.push_back(MakeFileTree(
88 entry.path().string(), max_depth - 1, extension, keyword));
90 }
else if (entry.is_regular_file()) {
93 if (!extension.empty()) {
94 if (!(entry.path().extension().string() == extension ||
95 entry.path().extension().string() ==
"." + extension)) {
100 if (!keyword.empty()) {
101 if (entry.path().filename().string().find(keyword) ==
107 tree.file.push_back(entry.path().filename().string());
114void FileTree::OutputFileTree(
const FileTreeData& tree,
int depth,
115 bool not_display_empty,
int* file_count)
const {
116 assert(file_count !=
nullptr);
118 std::string indent =
"";
119 for (
int i = 0; i < depth; ++i) {
124 if (!(not_display_empty && tree.file.empty() && tree.directory.empty())) {
129 std::string::size_type pos = tree.path.find_last_of(
"/\\");
130 std::string dir_name = ((depth == 0) ?
"" :
"- ");
132 std::string(
"[ ") + tree.path.substr(pos + 1) + std::string(
" ]");
137 for (
const auto& directory : tree.directory) {
138 OutputFileTree(directory, depth + 1, not_display_empty, file_count);
141 if (!tree.file.empty()) {
144 for (
const auto& file : tree.file) {
146 indent +
"|- " + file +
" [-" + std::to_string(*file_count) +
"-]",
154std::vector<std::string> FileTree::MakeFileList(
155 const FileTreeData& tree)
const {
156 std::vector<std::string> file_list;
158 for (
const auto& directory : tree.directory) {
159 std::vector<std::string> tmp = MakeFileList(directory);
161 file_list.insert(file_list.end(), tmp.begin(), tmp.end());
164 for (
const auto& file : tree.file) {
165 file_list.push_back(tree.path +
"\\" + file);
void DisplayFileTree(const std::string &path, int max_depth) const
ファイルツリーを表示する.
bool SelectFile(const std::string &path, int max_depth, const std::string &extension, const std::string keyword, std::string *output) const
ディレクトリの中から,ファイルを一つ選択する.
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
システムメッセージ,常に出力する.