dirtree.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : dirtree                                                           *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    A utility that shows the directory tree specified on the command line.   *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 2004-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
00017 \*****************************************************************************/
00018 
00019 #include <basis/guards.h>
00020 #include <basis/string_array.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <opsystem/directory_tree.h>
00024 #include <opsystem/filename.h>
00025 #include <data_struct/static_memory_gremlin.h>
00026 #include <textual/string_manipulation.h>
00027 
00028 class dirtree : public application_shell
00029 {
00030 public:
00031   dirtree() : application_shell(static_class_name()) {}
00032   IMPLEMENT_CLASS_NAME("dirtree");
00033   int execute();
00034   int print_instructions_and_exit() {
00035     log(isprintf("\
00036 %s: This utility requires a directory name on the command line.\n\
00037 The subdirectories under that directory will be shown.  If a second paramter\n\
00038 is provided, it is taken as a pattern that will be used to show the files in\n\
00039 those directories also.  Otherwise, just the tree of directories is shown.\n\
00040 ", filename(__argv[0]).basename().raw().s()));
00041     return 23;
00042   }
00043 };
00044 
00045 istring hier_prefix(int depth, int kids)
00046 {
00047   istring indent = string_manipulation::indentation( (depth - 1) * 2);
00048   if (!depth) return "";
00049   else if (!kids) return indent + "|--";
00050   else return indent + "+--";
00051 }
00052 
00053 int dirtree::execute()
00054 {
00055   istring path;
00056   if (__argc < 2) {
00057     return print_instructions_and_exit();
00058   }
00059 
00060   path = __argv[1];
00061 
00062   // check if we should show any of the files.
00063   bool show_files = false;
00064   istring pattern;
00065   if (__argc >= 3)
00066     pattern = __argv[2];
00067   if (pattern.t()) {
00068     show_files = true;
00069   }
00070 
00071 //  log(istring("Scanning directory tree at \"") + path + "\"");
00072 //  log(istring("Using pattern-match \"") + pattern + "\"");
00073 
00074   directory_tree dir(path, pattern.s(), !show_files);
00075   if (!dir.good()) {
00076     continuable_error(class_name(), "tree construction",
00077         "the directory could not be read");
00078     return 82;
00079   }
00080 
00081   dir_tree_iterator *ted = dir.start(directory_tree::prefix);
00082     // create our iterator to traverse the tree in prefix order.
00083 
00084   filename curr;  // the current path the iterator is at.
00085   string_array files;  // the filenames held at the iterator.
00086   int depth;  // current depth in tree.
00087   int kids;  // number of children below this node.
00088 
00089   while (directory_tree::current(*ted, curr, files)) {
00090     // we have a good directory to show.
00091     directory_tree::depth(*ted, depth);
00092     directory_tree::children(*ted, kids); 
00093     istring name_to_log = curr.basename().raw();
00094     if (!depth)
00095       name_to_log = curr.raw();
00096     log(hier_prefix(depth, kids) + name_to_log);
00097     if (show_files) {
00098       istring names;
00099       for (int i = 0; i < files.length(); i++) names += files[i] + " ";
00100       if (names.length()) {
00101         istring split;
00102         string_manipulation::split_lines(names, split, depth * 2 + 2);
00103         log(split);
00104       }
00105     }
00106 
00107     // go to the next place.
00108     directory_tree::next(*ted);
00109   }
00110 
00111   directory_tree::throw_out(ted);
00112   return 0;
00113 }
00114 
00115 HOOPLE_MAIN(dirtree, )
00116 
00117 #ifdef __BUILD_STATIC_APPLICATION__
00118   // static dependencies found by buildor_gen_deps.sh:
00119   #include <basis/array.cpp>
00120   #include <basis/byte_array.cpp>
00121   #include <basis/callstack_tracker.cpp>
00122   #include <basis/chaos.cpp>
00123   #include <basis/convert_utf.cpp>
00124   #include <basis/definitions.cpp>
00125   #include <basis/earth_time.cpp>
00126   #include <basis/guards.cpp>
00127   #include <basis/istring.cpp>
00128   #include <basis/log_base.cpp>
00129   #include <basis/memory_checker.cpp>
00130   #include <basis/mutex.cpp>
00131   #include <basis/object_base.cpp>
00132   #include <basis/outcome.cpp>
00133   #include <basis/packable.cpp>
00134   #include <basis/portable.cpp>
00135   #include <basis/sequence.cpp>
00136   #include <basis/set.cpp>
00137   #include <basis/utility.cpp>
00138   #include <basis/version_record.cpp>
00139   #include <data_struct/amorph.cpp>
00140   #include <data_struct/bit_vector.cpp>
00141   #include <data_struct/byte_hasher.cpp>
00142   #include <data_struct/configurator.cpp>
00143   #include <data_struct/hash_table.cpp>
00144   #include <data_struct/pointer_hash.cpp>
00145   #include <data_struct/stack.cpp>
00146   #include <data_struct/static_memory_gremlin.cpp>
00147   #include <data_struct/string_hash.cpp>
00148   #include <data_struct/string_hasher.cpp>
00149   #include <data_struct/string_table.cpp>
00150   #include <data_struct/symbol_table.cpp>
00151   #include <data_struct/table_configurator.cpp>
00152   #include <loggers/console_logger.cpp>
00153   #include <loggers/file_logger.cpp>
00154   #include <loggers/locked_logger.cpp>
00155   #include <loggers/null_logger.cpp>
00156   #include <loggers/program_wide_logger.cpp>
00157   #include <nodes/node.cpp>
00158   #include <nodes/packable_tree.cpp>
00159   #include <nodes/path.cpp>
00160   #include <nodes/tree.cpp>
00161   #include <opsystem/application_base.cpp>
00162   #include <opsystem/application_shell.cpp>
00163   #include <opsystem/byte_filer.cpp>
00164   #include <opsystem/command_line.cpp>
00165   #include <opsystem/critical_events.cpp>
00166   #include <opsystem/directory.cpp>
00167   #include <opsystem/directory_tree.cpp>
00168   #include <opsystem/file_info.cpp>
00169   #include <opsystem/filename.cpp>
00170   #include <opsystem/filename_list.cpp>
00171   #include <opsystem/filename_tree.cpp>
00172   #include <opsystem/huge_file.cpp>
00173   #include <opsystem/ini_config.cpp>
00174   #include <opsystem/ini_parser.cpp>
00175   #include <opsystem/path_configuration.cpp>
00176   #include <opsystem/rendezvous.cpp>
00177   #include <textual/byte_format.cpp>
00178   #include <textual/parser_bits.cpp>
00179   #include <textual/string_manipulation.cpp>
00180   #include <textual/tokenizer.cpp>
00181 #endif // __BUILD_STATIC_APPLICATION__
00182 

Generated on Thu Nov 20 04:28:45 2008 for HOOPLE Libraries by  doxygen 1.5.1