]> gitweb.hamatoma.de Git - crepublib/commitdiff
+ class ReDirTools
authorkawi <winfriedkappeler@atron.de>
Fri, 2 Jan 2015 09:03:33 +0000 (10:03 +0100)
committerkawi <winfriedkappeler@atron.de>
Fri, 2 Jan 2015 09:03:33 +0000 (10:03 +0100)
ReDirTools works

os/DirTools.hpp [deleted file]
os/ReDirTools.cpp
os/ReTraverser.cpp
os/reos.hpp

diff --git a/os/DirTools.hpp b/os/DirTools.hpp
deleted file mode 100644 (file)
index 9f210b8..0000000
+++ /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_ */
index f395fa64b8c7f93d58ba10c3cd68ad7f8f62f910..497f39706cc40ac0b59106b1e6bdbe72043d8e11 100644 (file)
@@ -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 <command> <opt>\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 ("<command>:\nstatistic <path> [<depth>] [<opts_stat>]\n");
+    printf ("<opts_stat>:\n  -v   verbose\n -t<n>  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;
+}
index 0ff17b3a93deb9631817301b3c9703d5dcddab5c..271021b361ec662e02fc6d1a76b22b204be16f44 100644 (file)
@@ -107,7 +107,7 @@ bool ReDirStatus_t::findFirst(){
        if (m_handle != INVALID_HANDLE_VALUE)\r
                FindClose(m_handle);\r
        ReByteBuffer thePath(m_path);\r
-       thePath.append("\\*");\r
+       thePath.append(m_path.str()[m_path.length() - 1] == '\\' ? "*" : "\\*");\r
        m_handle = FindFirstFileA(thePath.str(), &m_data);\r
        rc = m_handle != INVALID_HANDLE_VALUE;\r
 #endif\r
@@ -328,12 +328,17 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level)
        do{\r
         again = false;\r
                if (m_level < 0){\r
+            // Not yet initialized?\r
+            if (m_dirs[0]->m_passNo == 2)\r
+                rc = NULL;\r
+            else {\r
                        // first call:\r
-                       if (initEntry(m_base.str(), NULL, 0)){\r
-                if (1 != m_passNoForDirSearch)\r
-                                   rc = m_dirs[0];\r
-                else\r
-                    again = alreadyRead = true;\r
+                           if (initEntry(m_base.str(), NULL, 0)){\r
+                    if (1 != m_passNoForDirSearch)\r
+                                       rc = m_dirs[0];\r
+                    else\r
+                        again = alreadyRead = true;\r
+                }\r
             }\r
                } else {\r
                        ReDirStatus_t* current = m_dirs[m_level];\r
@@ -356,6 +361,8 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level)
                                if (current->m_passNo == 1){\r
                                        // we start the second pass:\r
                                        alreadyRead = initEntry(current->m_path, NULL, -1);\r
+                    if (strcmp(current->m_path.str(), "E:\\user\\winfried\\") == 0)\r
+                        current->m_passNo = 2;\r
                                        current->m_passNo = 2;\r
                                        again = true;\r
                                } else {\r
index e8ccc68268ba095da1b1f8cfab234f1e56c96a9a..3b513a9f53af713a7005c3a4fc7ace9fc59f96a3 100644 (file)
@@ -20,6 +20,6 @@
 #endif
 
 #include "os/ReTraverser.hpp"
-#include "os/DirTools.hpp"
+#include "os/ReDirTools.hpp"
 
 #endif /* OS_REOS_HPP_ */