]> gitweb.hamatoma.de Git - crepublib/commitdiff
ReDirWhich, fix: owner in ReDirList
authorHamatoma <git.tortouse@hm.f-r-e-i.de>
Fri, 27 Feb 2015 00:10:39 +0000 (01:10 +0100)
committerHamatoma <git.tortouse@hm.f-r-e-i.de>
Fri, 27 Feb 2015 00:10:39 +0000 (01:10 +0100)
base/ReByteBuffer.hpp
cunit/cuReDirTools.cpp
os/ReDirTools.cpp
os/ReDirTools.hpp
os/ReTraverser.cpp

index 849c3f79e37180035665f0ab14385cb7573637af..ac2fd52439bf6dea330debaa1e9a6017fcebf4f7 100644 (file)
@@ -121,11 +121,28 @@ public:
                return *this;
        }
        /** @brief Tests whether another instance is equal to this instance.
-        * @param buffer        the buffer to compare
-        * @return                      <code>true</code>: the buffer's contents are equal
+        * @param data                  string to compare
+        * @param length                length of <code>data</code>. If -1: <code>strlen(data)</code>
+        * @param ignoreCase    <code>true</code>: case insensitive comparison
+        * @return                              <code>true</code>: the buffer's contents are equal
         */
-       inline bool equals(const ReByteBuffer& buffer) const{
-               bool rc = buffer.length() == m_length && _memcmp(buffer.str(), m_buffer, m_length) == 0;
+       inline bool equals(const char* data, size_t length, bool ignoreCase = false) const{
+               if (length == (size_t) -1)
+                       length = strlen(data);
+               bool rc = length == m_length 
+                       && (! ignoreCase && _memcmp(data, m_buffer, length) == 0
+                               || ignoreCase && _strnicmp(data, m_buffer, length) == 0);
+               return rc;
+       }
+       /** @brief Tests whether another instance is equal to this instance.
+        * @param buffer                the buffer to compare
+        * @param ignoreCase    <code>true</code>: case insensitive comparison
+        * @return                              <code>true</code>: the buffer's contents are equal
+        */
+       inline bool equals(const ReByteBuffer& buffer, bool ignoreCase = false) const{
+               bool rc = buffer.length() == m_length 
+                       && (! ignoreCase && _memcmp(buffer.str(), m_buffer, m_length) == 0
+                               || ignoreCase && _strnicmp(buffer.str(), m_buffer, m_length) == 0);
                return rc;
        }
        ReByteBuffer& fill(Byte filler = 0, int start = 0, int end = -1);
index 6f00be63d1ecdf5db9d9b7687094171c12a65d65..b846d32f549eedccbfa8259c594be4e76c3a0889 100644 (file)
@@ -73,9 +73,8 @@ private:
     }\r
        void run(){\r
                initTree();\r
-               testToolSync();\r
-               testList2();\r
                if (m_testAll){\r
+                       testList2();\r
                        testToolStatistic();\r
                        testBasic();\r
                        testDirOptions();\r
index 478852bbeba3b821622f9b6bf5fcfc5c3e7e13bd..a2e92da3f23db78d5cf716e02473c140f94e6528 100644 (file)
@@ -41,6 +41,7 @@ static const char* s_helpSummary[] = {
     "synchronize   copies only modified or new files from",\r
     "              from a source directory tre to a target",\r
        "touch         sets the filetimes",\r
+       "which         finds a file in a path list (like PATH)",\r
     NULL\r
 };\r
 \r
@@ -128,6 +129,16 @@ const char* s_touchExamples[] = {
     NULL\r
 };\r
 \r
+const char* s_whichUsage[] = {\r
+    "<command>: w(hicht) [<opts>] <file_or_pattern1> [<file_or_pattern2> ...]",\r
+    "   finds a file in a path list (like PATH)",\r
+    NULL\r
+};\r
+const char* s_whichExamples[] = {\r
+    "dirtool which --list=JAVA_CLASSPATH org.jdom.jar *.jdom*.jar",\r
+    "dirtool which find.exe",\r
+    NULL\r
+};\r
 /**\r
  * Constructor.\r
  *\r
@@ -656,6 +667,7 @@ ReTool::ReTool(const char* usage[], const char* example[],
        m_reservedFirst(reservedFirst),\r
        m_reservedLast(reservedLast),\r
        m_addCurrentDirIfNoArguments(addCurrentDirIfNoArguments),\r
+       m_hasStandardArgs(true),\r
        m_traverser(NULL, this, logger),\r
        m_filter(),\r
        m_start(time(NULL)),\r
@@ -700,8 +712,14 @@ void ReTool::run(int argc, const char** argv){
                m_programArgs.init(argc, argv);\r
                if (m_programArgs.getArgCount() < m_minArguments)\r
                        m_programArgs.help(i18n("too few arguments"), false, stdout);\r
-       setFilterFromProgramArgs(m_filter);\r
+               if (m_hasStandardArgs)\r
+               setFilterFromProgramArgs(m_filter);\r
                doIt();\r
+               if (m_output != stdout){\r
+                       fclose(m_output);\r
+                       m_output = stdout;\r
+               }\r
+                       \r
     } catch (ReOptionException& exc) {\r
         m_programArgs.help(exc.getMessage(), false, stdout);\r
     }\r
@@ -1902,6 +1920,83 @@ void ReDirSync::doIt(){
        }\r
 }\r
 \r
+/**\r
+ * Constructor.\r
+ *\r
+ * @param logger       logger for error handling\r
+ */\r
+ReDirWhich::ReDirWhich(ReLogger* logger) :\r
+    ReTool(s_batchUsage, s_batchExamples, 0, 0, 0, true, logger)\r
+{\r
+       // no standard options:\r
+    m_programArgs.addBool("all",\r
+        i18n("all files will be found, not only the first"),\r
+        'a', "all", false);\r
+    m_programArgs.addString("list",\r
+        i18n("a path list (separator see option 'separator'"),\r
+        'l', "list", false, NULL);\r
+       m_programArgs.addString("separator",\r
+               i18n("separator between the path elements"),\r
+               's', "separator", false,\r
+#if defined __linux__\r
+               ":"\r
+#elif defined __WIN32__\r
+               ";"\r
+#endif\r
+               );\r
+    m_programArgs.addString("variable",\r
+        i18n("variable with the path list"),\r
+        'v', "variable", false, "PATH");\r
+       m_hasStandardArgs = false;\r
+}\r
+\r
+/**\r
+ * Creates the batch file.\r
+ */\r
+void ReDirWhich::doIt(){\r
+       ReByteBuffer value, path;\r
+       bool all = false;\r
+       ReStringList items;
+       char sep = 0;\r
+       m_programArgs.getString("list", path);\r
+       if (path.length() == 0){\r
+               m_programArgs.getString("variable", value);\r
+               if (getenv(value.str()) == NULL)\r
+                       help("Umgebungsvariable nicht definiert: ", value.str());\r
+               else\r
+                       path = getenv(value.str());\r
+               m_programArgs.getString("separator", value);\r
+               sep = value.at(0);\r
+       }\r
+       items.split(path.str(), sep);\r
+       struct stat info;\r
+       ReByteBuffer full;\r
+       for (int ix = 0; ix < m_programArgs.getArgCount(); ix++){\r
+               bool found = false;\r
+\r
+               ReByteBuffer arg(m_programArgs.getArg(ix));\r
+               for (size_t ixItem = 0; ixItem < items.count(); ixItem++){\r
+                       full.set(items.strOf(ixItem));\r
+                       if (arg.indexOf('*') < 0){\r
+                               full.ensureLastChar(OS_SEPARATOR_CHAR);\r
+                               full.append(arg);\r
+                               found = stat(full.str(), &info) == 0;\r
+                               if (found)\r
+                                       printf("%s\n", full.str());\r
+                       } else {\r
+                               ReDirectory dir(full.str());\r
+                               if (dir.findFirst(arg.str(), false)){\r
+                                       do {\r
+                                               printf("%s%c%s\n", full.str(), OS_SEPARATOR_CHAR, dir.currentFile());\r
+                                       } while(dir.findNext());\r
+                               }\r
+                       }\r
+                       if (found && ! all)\r
+                               break;\r
+               }\r
+       }\r
+}\r
+\r
 /**\r
  * Tests whether a abrevation of an argument is given.\r
  * @param full      the full name\r
@@ -1933,14 +2028,23 @@ void ReDirTools::help(int argc, const char* argv[]){
         } else if (isArg("list", arg0)){\r
             ReDirList list(m_logger);\r
             list.help(NULL);\r
-        } else if (isArg("help", arg0))\r
+        } else if (isArg("help", arg0)){\r
             printField(s_helpSummary);\r
-        else if (isArg("statistic", arg0)){\r
+        } else if (isArg("md5", arg0)){\r
+            ReDirMD5 md5(m_logger);\r
+            md5.help(NULL);\r
+               } else if (isArg("statistic", arg0)){\r
             ReDirStatistic stat(m_logger);\r
             stat.help(NULL);\r
         } else if (isArg("test", arg0)){\r
             void testAll();\r
             testAll();\r
+        } else if (isArg("touch", arg0)){\r
+            ReDirTouch touch(m_logger);\r
+            touch.help(NULL);\r
+        } else if (isArg("which", arg0)){\r
+            ReDirWhich which(m_logger);\r
+            which.help(NULL);\r
         } else\r
             printf("+++ unknown sub command: %s\n", arg0);\r
     }\r
@@ -1987,6 +2091,8 @@ int ReDirTools::main(int argc, char* orgArgv[]){
         ReDirSync(m_logger).run(argc, argv);\r
     else if (isArg("touch", arg0))\r
         ReDirTouch(m_logger).run(argc, argv);\r
+    else if (isArg("which", arg0))\r
+        ReDirWhich(m_logger).run(argc, argv);\r
     else if (isArg("test", arg0)){\r
         void testAll();\r
         testAll();\r
index da857e271f0afaf8e5ec1208fd4272135be02cd8..0f1a5d8fdbc02aca033d814c395074a5e363f6fc 100644 (file)
@@ -120,6 +120,7 @@ protected:
        int m_reservedFirst;
        int m_reservedLast;
        bool m_addCurrentDirIfNoArguments;
+       bool m_hasStandardArgs;
        ReTraverser m_traverser;
        ReDirEntryFilter_t m_filter;
        int64_t m_start;
@@ -160,7 +161,7 @@ protected:
 };
 
 /**
- * List file attributes of files.
+ * Calculates the MD5 checksum of files.
  */
 class ReDirMD5 : public ReTool {
 public:
@@ -250,6 +251,15 @@ protected:
        ReFileTime_t m_accessed;
 };
 
+/**
+ * Find files in a list (like PATH).
+ */
+class ReDirWhich : public ReTool {
+public:
+    ReDirWhich(ReLogger* logger);
+protected:
+       virtual void doIt();
+};
 
 class ReDirTools {
 
index 655468b32f776e77fb90a9dc6556569869a24850..be3bf871928328abdacbbc4508f8d869b33208a4 100644 (file)
 #pragma comment(lib, "advapi32.lib")\r
 #endif\r
 \r
-enum RELOC_TRAVERSER {
-       LC_RIGHTS_AS_STRING_1   = LC_TRAVERSER + 1, // 50301
-       LC_RIGHTS_AS_STRING_2,  // 50302
-       LC_RIGHTS_AS_STRING_3,  // 50303
-       LC_GET_PRIVILEGE_1,             // 50304
-       LC_GET_PRIVILEGE_2,             // 50305
-       LC_GET_PRIVILEGE_3,             // 50306
-       LC_GET_FILE_OWNER_1,    // 50307
-       LC_GET_FILE_OWNER_2,    // 50308
-};
+enum RELOC_TRAVERSER {\r
+       LC_RIGHTS_AS_STRING_1   = LC_TRAVERSER + 1, // 50401\r
+       LC_RIGHTS_AS_STRING_2,  // 50402\r
+       LC_RIGHTS_AS_STRING_3,  // 50403\r
+       LC_GET_PRIVILEGE_1,             // 50404\r
+       LC_GET_PRIVILEGE_2,             // 50405\r
+       LC_GET_PRIVILEGE_3,             // 50406\r
+       LC_GET_FILE_OWNER_1,    // 50407\r
+       LC_GET_FILE_OWNER_2,    // 50408\r
+};\r
 \r
 /**\r
  * Constructor.\r
@@ -242,8 +242,8 @@ bool ReDirStatus_t::getFileOwner(HANDLE handle, const char* file,
 * @param privilege     the name of the privilege, e.g. "SeBackup"\r
 * @param logger                logger for error logging\r
 */\r
-bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){
-       bool rc = false;
+bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){\r
+       bool rc = false;\r
        LUID luidPrivilege;\r
        HANDLE hAccessToken;\r
     if (! OpenProcessToken (GetCurrentProcess(), \r
@@ -271,7 +271,7 @@ bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){
                }\r
     }\r
        return rc;\r
-}
+}\r
 #endif /* __WIN32__ */\r
 \r
 /**\r
@@ -441,7 +441,7 @@ const char* ReDirStatus_t::rightsAsString(ReByteBuffer& buffer, bool numerical,
        HANDLE handle = INVALID_HANDLE_VALUE;\r
        if (! isDirectory()){\r
                if ( (handle = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,\r
-                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)\r
+                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)\r
                        m_logger->sayF(LOG_ERROR | CAT_FILE, LC_RIGHTS_AS_STRING_1,\r
                                "CreateFile($1): $2").arg(name).arg((int) GetLastError()).end();\r
        } else if (m_getPrivilege){\r