21 FileTreeData tree = MakeFileTree(path, max_depth,
"",
"sim");
24 OutputFileTree(tree, 0,
true, &count);
28 const std::string& extension,
const std::string keyword,
29 std::string* output)
const
31 assert(output !=
nullptr);
34 FileTreeData tree = MakeFileTree(path, max_depth, extension, keyword);
40 OutputFileTree(tree, 0,
true, &count);
46 std::vector<std::string> file_list = MakeFileList(tree);
48 if (file_list.empty())
58 "Please select a file. Enter an integer.");
61 CmdIOUtil::Output(
"The selected file is " + file_list[
static_cast<size_t>(select_index)],
66 *output = file_list[
static_cast<size_t>(select_index)];
76FileTree::FileTreeData FileTree::MakeFileTree(
const std::string& path,
int max_depth,
77 const std::string& extension,
78 const std::string keyword)
const
84 assert(std::filesystem::exists(tree.path));
86 for (
const auto& entry :
std::filesystem::directory_iterator(path))
88 if (entry.is_directory())
93 tree.directory.push_back(
94 FileTreeData{ entry.path().
string(), {}, {} });
98 tree.directory.push_back(
99 MakeFileTree(entry.path().string(), max_depth - 1, extension, keyword));
102 else if (entry.is_regular_file())
106 if (!extension.empty())
108 if (!(entry.path().extension().string() == extension ||
109 entry.path().extension().string() ==
"." + extension))
115 if (!keyword.empty())
117 if (entry.path().filename().string().find(keyword) == std::string::npos)
123 tree.file.push_back(entry.path().filename().string());
130void FileTree::OutputFileTree(
const FileTreeData& tree,
int depth,
131 bool not_display_empty,
int* file_count)
const
133 assert(file_count !=
nullptr);
135 std::string indent =
"";
136 for (
int i = 0; i < depth; ++i)
142 if (!(not_display_empty && tree.file.empty() && tree.directory.empty()))
148 std::string::size_type pos = tree.path.find_last_of(
"/\\");
149 std::string dir_name = ((depth == 0) ?
"" :
"- ");
150 dir_name += std::string(
"[ ") + tree.path.substr(pos + 1) + std::string(
" ]");
155 for (
const auto& directory : tree.directory)
157 OutputFileTree(directory, depth + 1, not_display_empty, file_count);
160 if (!tree.file.empty())
164 for (
const auto& file : tree.file)
166 CmdIOUtil::Output(indent +
"|- " + file +
" [-" + std::to_string(*file_count) +
"-]",
174std::vector<std::string> FileTree::MakeFileList(
const FileTreeData& tree)
const
176 std::vector<std::string> file_list;
178 for (
const auto& directory : tree.directory)
180 std::vector<std::string> tmp = MakeFileList(directory);
182 file_list.insert(file_list.end(), tmp.begin(), tmp.end());
185 for (
const auto& file : tree.file)
187 file_list.push_back(tree.path +
"\\" + file);
static void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
static int InputInt(int min, int max, int default_num, const std::string &str="Please enter an integer.")
整数を入力させる関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
static void OutputHorizontalLine(const std::string &line_visual, OutputDetail detail)
コマンドラインに水平線を出力する関数.
static bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true,noなら falseを返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
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
ディレクトリの中から,ファイルを一つ選択する.
@ kSystem
システムメッセージ,常に出力する.