00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <basis/function.h>
00020 #include <basis/guards.h>
00021 #include <basis/string_array.h>
00022 #include <opsystem/application_shell.h>
00023 #include <loggers/console_logger.h>
00024 #include <opsystem/directory_tree.h>
00025 #include <opsystem/filename.h>
00026 #include <opsystem/filename_list.h>
00027 #include <data_struct/static_memory_gremlin.h>
00028 #include <textual/string_manipulation.h>
00029
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031
00032 class test_directory_tree : public application_shell
00033 {
00034 public:
00035 test_directory_tree() : application_shell(static_class_name()) {}
00036 IMPLEMENT_CLASS_NAME("test_directory_tree");
00037 int execute();
00038 };
00039
00040 int test_directory_tree::execute()
00041 {
00042 FUNCDEF("execute");
00043
00044
00045 uint16 a = 23;
00046 uint16 b = 7;
00047 if ((a ^ b) != 16) deadly_error("check", "check", "not working as expect");
00048 a = 0;
00049 b = 19;
00050 if ((a ^ b) != 19) deadly_error("check", "check2", "not working as expect");
00051
00052
00053
00054
00055 istring path = "/usr/local";
00056 #ifdef __WIN32__
00057
00058 path = portable::env_string("COMMONPROGRAMFILES");
00059 #endif
00060 if (__argc >= 2)
00061 path = __argv[1];
00062
00063 istring pattern = "*";
00064 if (__argc >= 3)
00065 pattern = __argv[2];
00066
00067 {
00068 log(istring("Scanning directory tree at \"") + path + "\"");
00069 log(istring("Using pattern-match \"") + pattern + "\"");
00070
00071 directory_tree dir(path, pattern.s());
00072 if (!dir.good())
00073 deadly_error(class_name(), "directory_tree construction",
00074 "the directory could not be read");
00075
00076 dir_tree_iterator *ted = dir.start(directory_tree::prefix);
00077
00078
00079 int depth;
00080 filename curr;
00081 string_array files;
00082
00083 while (directory_tree::current(*ted, curr, files)) {
00084
00085 directory_tree::depth(*ted, depth);
00086 log(string_manipulation::indentation(depth * 2) + istring("[")
00087 + curr.raw() + "]");
00088 istring names;
00089 for (int i = 0; i < files.length(); i++) names += files[i] + " ";
00090 if (names.length()) {
00091 istring split;
00092 string_manipulation::split_lines(names, split, depth * 2 + 2);
00093 log(split);
00094 }
00095
00096
00097 directory_tree::next(*ted);
00098 }
00099
00100 directory_tree::throw_out(ted);
00101
00102 }
00103
00104 {
00105
00106
00107
00108
00109 }
00110
00111 {
00112
00113 log(istring("Self-comparing directory tree at \"") + path + "\"");
00114 log(istring("Using pattern-match \"") + pattern + "\"");
00115
00116 LOG("reading tree 1.");
00117 directory_tree dir(path, pattern.s());
00118 if (!dir.good())
00119 deadly_error(class_name(), "directory_tree construction",
00120 "the directory could not be read");
00121
00122
00123 LOG("reading tree 2.");
00124 directory_tree dir2(path, pattern.s());
00125 if (!dir2.good())
00126 deadly_error(class_name(), "directory_tree construction",
00127 "the directory could not be read the second time?!");
00128
00129 LOG("comparing the two trees.");
00130 filename_list diffs;
00131 directory_tree::compare_trees(dir, dir2, diffs);
00132 LOG(diffs.text_form());
00133
00134 if (diffs.elements())
00135 deadly_error(class_name(), "directory_tree comparison",
00136 "there were differences when comparing identical directories!");
00137 }
00138
00139 {
00140
00141 log(istring("Calculating sums for tree at \"") + path + "\"");
00142 log(istring("Using pattern-match \"") + pattern + "\"");
00143
00144 LOG("reading tree 1.");
00145 directory_tree dir(path, pattern.s());
00146 if (!dir.good())
00147 deadly_error(class_name(), "directory_tree construction",
00148 "the directory could not be read");
00149
00150
00151 LOG("reading tree 2.");
00152 directory_tree dir2(path, pattern.s());
00153 if (!dir2.good())
00154 deadly_error(class_name(), "directory_tree construction",
00155 "the directory could not be read the second time?!");
00156
00157 LOG("calculating checksums for tree 1.");
00158 if (!dir.calculate())
00159 deadly_error(class_name(), "directory_tree calculation",
00160 "the first tree could not be calculated");
00161
00162 LOG("calculating checksums for tree 2.");
00163 if (!dir2.calculate())
00164 deadly_error(class_name(), "directory_tree calculation",
00165 "the second tree could not be calculated");
00166
00167 LOG("comparing the two trees.");
00168 filename_list diffs;
00169 directory_tree::compare_trees(dir, dir2, diffs);
00170 LOG(diffs.text_form());
00171
00172 if (diffs.elements())
00173 deadly_error(class_name(), "directory_tree comparison",
00174 "there were differences when comparing identical directories!");
00175 }
00176
00177 {
00178
00179 log(istring("Reading tree for packing at \"") + path + "\"");
00180 log(istring("Using pattern-match \"") + pattern + "\"");
00181
00182 LOG("reading tree.");
00183 directory_tree dir(path, pattern.s());
00184 if (!dir.good())
00185 deadly_error(class_name(), "directory_tree construction",
00186 "the directory could not be read");
00187
00188 LOG("calculating checksums for tree.");
00189 if (!dir.calculate())
00190 deadly_error(class_name(), "directory_tree calculation",
00191 "the first tree could not be calculated");
00192
00193 byte_array packed_form;
00194 dir.pack(packed_form);
00195 LOG(isprintf("tree became %d byte array", packed_form.length()));
00196
00197 directory_tree dir2;
00198 if (!dir2.unpack(packed_form))
00199 deadly_error(class_name(), "directory_tree calculation",
00200 "the second tree could not be unpacked from the first");
00201
00202 LOG("comparing the two trees.");
00203 filename_list diffs;
00204 directory_tree::compare_trees(dir, dir2, diffs);
00205 LOG(diffs.text_form());
00206
00207 if (diffs.elements())
00208 deadly_error(class_name(), "directory_tree comparison",
00209 "there were differences when comparing identical directories!");
00210
00211 directory_tree::compare_trees(dir2, dir, diffs);
00212 if (diffs.elements())
00213 deadly_error(class_name(), "directory_tree comparison",
00214 "there were differences when reverse comparing identical dirs!");
00215
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 guards::alert_message("directory_tree:: works for those functions tested.");
00229 return 0;
00230 }
00231
00232 HOOPLE_MAIN(test_directory_tree, )
00233