dirtree brought back from oblivion.
authorFred T. Hamster <fred@gruntose.com>
Tue, 29 May 2012 05:23:04 +0000 (01:23 -0400)
committerFred T. Hamster <fred@gruntose.com>
Tue, 29 May 2012 05:23:04 +0000 (01:23 -0400)
nucleus/applications/utilities/dirtree.cpp [new file with mode: 0644]
nucleus/applications/utilities/makefile

diff --git a/nucleus/applications/utilities/dirtree.cpp b/nucleus/applications/utilities/dirtree.cpp
new file mode 100644 (file)
index 0000000..80156b3
--- /dev/null
@@ -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 <application/hoople_main.h>
+#include <basis/guards.h>
+#include <filesystem/directory_tree.h>
+#include <filesystem/filename.h>
+#include <loggers/console_logger.h>
+#include <structures/static_memory_gremlin.h>
+#include <structures/string_array.h>
+#include <textual/string_manipulation.h>
+
+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 <application/application_shell.cpp>
+  #include <application/command_line.cpp>
+  #include <application/windoze_helper.cpp>
+  #include <basis/astring.cpp>
+  #include <basis/common_outcomes.cpp>
+  #include <basis/environment.cpp>
+  #include <basis/guards.cpp>
+  #include <basis/mutex.cpp>
+  #include <basis/utf_conversion.cpp>
+  #include <configuration/application_configuration.cpp>
+  #include <configuration/configurator.cpp>
+  #include <configuration/ini_configurator.cpp>
+  #include <configuration/ini_parser.cpp>
+  #include <configuration/table_configurator.cpp>
+  #include <configuration/variable_tokenizer.cpp>
+  #include <filesystem/byte_filer.cpp>
+  #include <filesystem/directory.cpp>
+  #include <filesystem/directory_tree.cpp>
+  #include <filesystem/file_info.cpp>
+  #include <filesystem/file_time.cpp>
+  #include <filesystem/filename.cpp>
+  #include <filesystem/filename_list.cpp>
+  #include <filesystem/filename_tree.cpp>
+  #include <filesystem/huge_file.cpp>
+  #include <loggers/combo_logger.cpp>
+  #include <loggers/console_logger.cpp>
+  #include <loggers/critical_events.cpp>
+  #include <loggers/file_logger.cpp>
+  #include <loggers/program_wide_logger.cpp>
+  #include <nodes/node.cpp>
+  #include <nodes/packable_tree.cpp>
+  #include <nodes/path.cpp>
+  #include <nodes/tree.cpp>
+  #include <structures/bit_vector.cpp>
+  #include <structures/checksums.cpp>
+  #include <structures/object_packers.cpp>
+  #include <structures/static_memory_gremlin.cpp>
+  #include <structures/string_hasher.cpp>
+  #include <structures/string_table.cpp>
+  #include <structures/version_record.cpp>
+  #include <textual/byte_formatter.cpp>
+  #include <textual/parser_bits.cpp>
+  #include <textual/string_manipulation.cpp>
+  #include <timely/earth_time.cpp>
+  #include <timely/time_stamp.cpp>
+#endif // __BUILD_STATIC_APPLICATION__
+
index 2ba9d0eb0f96211e64db86774565648f9f8afcff..fee8e39154c20d7b91f02b96d314ea303861d1a8 100644 (file)
@@ -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