From: Fred T. Hamster Date: Tue, 29 May 2012 05:23:04 +0000 (-0400) Subject: dirtree brought back from oblivion. X-Git-Tag: 2.140.90~1333 X-Git-Url: https://feistymeow.org/gitweb/?p=feisty_meow.git;a=commitdiff_plain;h=80bc699743d6ff933712f95d67c0dd5801d068c3 dirtree brought back from oblivion. --- diff --git a/nucleus/applications/utilities/dirtree.cpp b/nucleus/applications/utilities/dirtree.cpp new file mode 100644 index 00000000..80156b38 --- /dev/null +++ b/nucleus/applications/utilities/dirtree.cpp @@ -0,0 +1,173 @@ +/* +* Name : dirtree +* Author : Chris Koeritz +* Purpose: +* A utility that shows the directory tree specified on the command line. + +* Copyright (c) 2004-$now By Author. This program is free software; you can +* redistribute it and/or modify it under the terms of the GNU General Public +* License as published by the Free Software Foundation; either version 2 of +* the License or (at your option) any later version. This is online at: +* http://www.fsf.org/copyleft/gpl.html +* Please send any updates to: fred@gruntose.com +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace application; +using namespace basis; +using namespace filesystem; +using namespace loggers; +using namespace structures; +using namespace textual; + +#define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), to_print) + +class dirtree : public application_shell +{ +public: + dirtree() : application_shell() {} + DEFINE_CLASS_NAME("dirtree"); + int execute(); + int print_instructions_and_exit() { + LOG(a_sprintf("\ +%s: This utility requires a directory name on the command line.\n\ +The subdirectories under that directory will be shown. If a second paramter\n\ +is provided, it is taken as a pattern that will be used to show the files in\n\ +those directories also. Otherwise, just the tree of directories is shown.\n\ +", filename(application::_global_argv[0]).basename().raw().s())); + return 23; + } +}; + +astring hier_prefix(int depth, int kids) +{ + astring indent = string_manipulation::indentation( (depth - 1) * 2); + if (!depth) return ""; + else if (!kids) return indent + "|--"; + else return indent + "+--"; +} + +int dirtree::execute() +{ + astring path; + + + + if (application::_global_argc < 2) { + return print_instructions_and_exit(); + } + + path = application::_global_argv[1]; + + // check if we should show any of the files. + bool show_files = false; + astring pattern; + if (application::_global_argc >= 3) + pattern = application::_global_argv[2]; + if (pattern.t()) { + show_files = true; + } + +// log(astring("Scanning directory tree at \"") + path + "\""); +// log(astring("Using pattern-match \"") + pattern + "\""); + + directory_tree dir(path, pattern.s(), !show_files); + if (!dir.good()) { + continuable_error(class_name(), "tree construction", + "the directory could not be read"); + return 82; + } + + dir_tree_iterator *ted = dir.start(directory_tree::prefix); + // create our iterator to traverse the tree in prefix order. + + filename curr; // the current path the iterator is at. + string_array files; // the filenames held at the iterator. + int depth; // current depth in tree. + int kids; // number of children below this node. + + while (directory_tree::current(*ted, curr, files)) { + // we have a good directory to show. + directory_tree::depth(*ted, depth); + directory_tree::children(*ted, kids); + astring name_to_log = curr.basename().raw(); + if (!depth) + name_to_log = curr.raw(); + LOG(hier_prefix(depth, kids) + name_to_log); + if (show_files) { + astring names; + for (int i = 0; i < files.length(); i++) names += files[i] + " "; + if (names.length()) { + astring split; + string_manipulation::split_lines(names, split, depth * 2 + 2); + LOG(split); + } + } + + // go to the next place. + directory_tree::next(*ted); + } + + directory_tree::throw_out(ted); + return 0; +} + +HOOPLE_MAIN(dirtree, ) + +#ifdef __BUILD_STATIC_APPLICATION__ + // static dependencies found by buildor_gen_deps.sh: + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include +#endif // __BUILD_STATIC_APPLICATION__ + diff --git a/nucleus/applications/utilities/makefile b/nucleus/applications/utilities/makefile index 2ba9d0eb..fee8e391 100644 --- a/nucleus/applications/utilities/makefile +++ b/nucleus/applications/utilities/makefile @@ -7,13 +7,9 @@ TYPE = application ifeq "$(OMIT_VERSIONS)" "" SOURCE += util_version.rc endif -LOCAL_LIBS_USED = application loggers mathematics processes textual timely configuration filesystem structures basis -TARGETS = await_app_exit.exe bytedump.exe checker.exe ini_edit.exe mdate.exe splitter.exe -#check_address.exe check_eol.exe create_guid.exe cstrip.exe \ - empty.exe find_non_ascii.exe find_text.exe insert_before.exe \ - lexcaser.exe machine_name.exe merge_inis.exe net_calc.exe pi.exe \ - replace_text.exe show_args.exe show_versions.exe to_lower.exe uucat.exe -#VCPP_USE_SOCK=t +LOCAL_LIBS_USED = application loggers mathematics nodes processes textual timely configuration filesystem structures basis +TARGETS = await_app_exit.exe bytedump.exe checker.exe dirtree.exe ini_edit.exe mdate.exe \ + splitter.exe include cpp/rules.def