From: kawi Date: Fri, 2 Jan 2015 09:03:33 +0000 (+0100) Subject: + class ReDirTools X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=dc019144ea912102f641f9595e688ed8d2cbd174;p=crepublib + class ReDirTools ReDirTools works --- diff --git a/os/DirTools.hpp b/os/DirTools.hpp deleted file mode 100644 index 9f210b8..0000000 --- a/os/DirTools.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * DirTools.hpp - * - * Created on: 30.12.2014 - * Author: hm - */ - -#ifndef OS_DIRTOOLS_HPP_ -#define OS_DIRTOOLS_HPP_ - -class ReDirStatisticData{ -public: - ReDirStatisticData(); - ReDirStatisticData(const ReDirStatisticData& source); - ReDirStatisticData& operator =(const ReDirStatisticData& source); -public: - void clear(); - ReDirStatisticData& add(const ReDirStatisticData& source); -public: - int64_t m_sizes; - int m_files; - int m_dirs; - ReByteBuffer m_path; -}; - -class ReDirStatistic; -extern void formatLikeDu(const ReDirStatisticData& data, ReDirStatistic& parent, - ReByteBuffer& line); -extern void formatWithSizeFilesAndDirs(const ReDirStatisticData& data, - ReDirStatistic& parent, ReByteBuffer& line); - -/** - * Calculates a statistic of a directory tree. - */ -class ReDirStatistic { -public: - ReDirStatistic(int deltaList = 512, int deltaBuffer = 0x10000); - ~ReDirStatistic(); -public: - const ReStringList& calculate(const char* base, int depth, - void (*format)(const ReDirStatisticData& data, ReDirStatistic& parent, - ReByteBuffer& line) = formatLikeDu); - void setTraceInterval(int interval){ - m_traceInterval = interval; - } -private: - ReStringList m_list; - int m_traceInterval; - time_t m_lastTrace; -}; - - -#endif /* OS_DIRTOOLS_HPP_ */ diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index f395fa6..497f397 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -8,6 +8,8 @@ #include "base/rebase.hpp" #include "os/reos.hpp" +const char* ReDirTools::m_version = "2015.01.02"; + /** * Constructor. */ @@ -210,3 +212,77 @@ void formatWithSizeFilesAndDirs(const ReDirStatisticData& data, data.m_sizes / 1E6, data.m_files, data.m_dirs); line.append(buffer, -1).append(data.m_path); } +/** + * Prints an message how to use the program and exits. + * + * @param msg an error message + * @param msg2 an addition to the error message or NULL + */ +void ReDirTools::usage(const char* msg, const char* msg2){ + printf ("Version: %s\n", m_version); + printf ("usage: dirtool \n"); + statisticUsage(); + if (msg != NULL) + printf ("+++ %s%s\n", msg, msg2 == NULL ? "" : msg2); + exit(1); +} + +/** + * Prints an message how to use the statistic module and exits. + */ +void ReDirTools::statisticUsage(){ + printf (":\nstatistic [] []\n"); + printf (":\n -v verbose\n -t trace interval\n"); + printf ("example: dirtool statistic c:\\windows 2 -v -t60\n"); +} + +/** + * Gets the arguments for the "statistic" command and execute this. + * + * @param argc the number of arguments + * @param argav the argument vector + */ +void ReDirTools::dirStatistic(int argc, char* argv[]){ + time_t start = time(NULL); + bool verbose = true; + int traceInterval = 60; + if (argc < 1) + usage("statistic: missing path"); + int depth = 1; + if (argc > 1) + depth = atol(argv[1]); + ReDirStatistic statistic; + statistic.setTraceInterval(traceInterval); + const ReStringList& list = statistic.calculate(argv[0], depth, formatWithSizeFilesAndDirs); + ReByteBuffer buffer; + for (size_t ix = 0; ix < list.count(); ix++){ + buffer.set(list.strOf(ix), list.strLengthOf(ix)); + printf("%s\n", buffer.str()); + } + if (verbose){ + int duration = int(time(NULL) - start); + printf("Duration: "); + if (duration >= 3600) + printf("%d:", duration / 3600); + printf("%02d:%02d\n", duration % 3600 / 60, duration % 60); + } +} + +/** + * Gets the arguments for any command and execute this. + * + * @param argc the number of arguments + * @param argav the argument vector + */ +int ReDirTools::main(int argc, char* argv[]){ + ReDirTools tools; + if (argc < 2) + tools.usage("missing arguments"); + if (strcmp(argv[1], "statistic") == 0) + tools.dirStatistic(argc - 2, argv + 2); + else + tools.usage("command not implemented: ", argv[1]); + //testOs(); + ReLogger::freeGlobalLogger(); + return 0; +} diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 0ff17b3..271021b 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -107,7 +107,7 @@ bool ReDirStatus_t::findFirst(){ if (m_handle != INVALID_HANDLE_VALUE) FindClose(m_handle); ReByteBuffer thePath(m_path); - thePath.append("\\*"); + thePath.append(m_path.str()[m_path.length() - 1] == '\\' ? "*" : "\\*"); m_handle = FindFirstFileA(thePath.str(), &m_data); rc = m_handle != INVALID_HANDLE_VALUE; #endif @@ -328,12 +328,17 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level) do{ again = false; if (m_level < 0){ + // Not yet initialized? + if (m_dirs[0]->m_passNo == 2) + rc = NULL; + else { // first call: - if (initEntry(m_base.str(), NULL, 0)){ - if (1 != m_passNoForDirSearch) - rc = m_dirs[0]; - else - again = alreadyRead = true; + if (initEntry(m_base.str(), NULL, 0)){ + if (1 != m_passNoForDirSearch) + rc = m_dirs[0]; + else + again = alreadyRead = true; + } } } else { ReDirStatus_t* current = m_dirs[m_level]; @@ -356,6 +361,8 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level) if (current->m_passNo == 1){ // we start the second pass: alreadyRead = initEntry(current->m_path, NULL, -1); + if (strcmp(current->m_path.str(), "E:\\user\\winfried\\") == 0) + current->m_passNo = 2; current->m_passNo = 2; again = true; } else { diff --git a/os/reos.hpp b/os/reos.hpp index e8ccc68..3b513a9 100644 --- a/os/reos.hpp +++ b/os/reos.hpp @@ -20,6 +20,6 @@ #endif #include "os/ReTraverser.hpp" -#include "os/DirTools.hpp" +#include "os/ReDirTools.hpp" #endif /* OS_REOS_HPP_ */