From: hama Date: Tue, 20 Jan 2015 23:07:46 +0000 (+0100) Subject: dir pattern for Traverser, win32 corrections X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=9f6fc7e7ecd844dbde7d1c5fec4fd69268e02845;p=crepublib dir pattern for Traverser, win32 corrections --- diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 4e18366..8c72be5 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -194,6 +194,7 @@ ReByteBuffer& ReByteBuffer::appendMilliSec(int time, int minLength){ appendInt(minutes, stdFormat).append(":"); appendInt(sec, sec > 10 || minimum > 5 ? stdFormat : "%d").append("."); appendInt(msec, "%03d"); + return *this; } /** Converts a subsequence into an integer. @@ -307,7 +308,7 @@ ReByteBuffer& ReByteBuffer::fill(Byte filler, int start, int end){ if (end == -1) end = m_length; if (start >= 0 && start < end){ - if (end > m_length) + if (end > (int) m_length) setLength(end); memset(m_buffer + start, filler, end - start); } diff --git a/base/ReSeqArray.cpp b/base/ReSeqArray.cpp index 5fc6c66..66fe1ec 100644 --- a/base/ReSeqArray.cpp +++ b/base/ReSeqArray.cpp @@ -60,7 +60,7 @@ enum RELOC_SEQARRAY { * @param deltaList If there is not enough space in the content buffer * this amount of bytes is the minimum to reserve. */ -ReSeqArray::ReSeqArray(size_t deltaList, int deltaBuffer) : +ReSeqArray::ReSeqArray(int deltaList, int deltaBuffer) : m_content(deltaBuffer), m_list(deltaList), m_lost(0), diff --git a/base/ReSeqArray.hpp b/base/ReSeqArray.hpp index b7a769d..fa460f5 100644 --- a/base/ReSeqArray.hpp +++ b/base/ReSeqArray.hpp @@ -30,7 +30,7 @@ public: Tag m_tag; } Sequence; public: - ReSeqArray(size_t deltaList = -1024*sizeof(size_t), int deltaBuffer = - 1024*1024); + ReSeqArray(int deltaList = -1024 * (int) sizeof(size_t), int deltaBuffer = - 1024*1024); virtual ~ReSeqArray(); ReSeqArray(const ReSeqArray& source); ReSeqArray& operator = (const ReSeqArray& source); diff --git a/base/ReTestUnit.cpp b/base/ReTestUnit.cpp index 7b6a241..232a104 100644 --- a/base/ReTestUnit.cpp +++ b/base/ReTestUnit.cpp @@ -252,6 +252,9 @@ int ReTestUnit::milliSecSince(int64_t start){ #if defined __linux__ int64_t diff = clock() - start; return diff * 1000 / CLOCKS_PER_SEC; +#elif defined __WIN32__ + int64_t diff = clock() - start; + return diff * 1000 / CLOCKS_PER_SEC; #else # error "timer not defined" #endif @@ -270,6 +273,13 @@ int ReTestUnit::milliSecSince(int64_t start){ int64_t ReTestUnit::timer(){ #if defined __linux__ return clock(); +#elif defined __WIN32__ + return clock(); +#if 0 + FILETIME timeBuffer; + GetSystemTimeAsFileTime(&timeBuffer); + return (int64_t) (((uint64_t) timeBuffer.dwHighDateTime << 32) + timeBuffer.dwLowDateTime); +#endif #else # error "timer not defined" #endif diff --git a/base/ReVarArgs.cpp b/base/ReVarArgs.cpp index 8f99580..969da46 100644 --- a/base/ReVarArgs.cpp +++ b/base/ReVarArgs.cpp @@ -179,6 +179,33 @@ ReVarArgs& ReVarArgs::arg(int value, const char* format){ return *this; } +/* @brief Defines an 64 bit integer argument. + * + * @param value The integer value of the argument. + * @param format The format (used with sprintf()) of the argument, e.g. "%lld". + * + * @return The instance itself. This is useful for chaining. + */ +ReVarArgs& ReVarArgs::arg(int64_t value, const char* format){ + char number[256]; + _snprintf(number, sizeof number, format, value); + store(number); + return *this; +} + +/** @brief Stores an long argument. + * + * @param value The argument value. + * @param format The format (used with sprintf()) of the argument, e.g. "%d". + * + * @return The instance itself. This is useful for chaining. + */ +ReVarArgs& ReVarArgs::arg(unsigned int value, const char* format){ + char number[256]; + _snprintf(number, sizeof number, format, value); + store(number); + return *this; +} /** @brief Stores an long argument. * * @param value The argument value. @@ -186,7 +213,7 @@ ReVarArgs& ReVarArgs::arg(int value, const char* format){ * * @return The instance itself. This is useful for chaining. */ -ReVarArgs& ReVarArgs::arg(long value, const char* format){ +ReVarArgs& ReVarArgs::arg(uint64_t value, const char* format){ char number[256]; _snprintf(number, sizeof number, format, value); store(number); diff --git a/base/ReVarArgs.hpp b/base/ReVarArgs.hpp index e4258a1..6239c4c 100644 --- a/base/ReVarArgs.hpp +++ b/base/ReVarArgs.hpp @@ -41,7 +41,9 @@ private: public: ReVarArgs& reset(const char* format); ReVarArgs& arg(int value, const char* format = "%d"); - ReVarArgs& arg(long arg, const char* format = "%ld"); + ReVarArgs& arg(int64_t value, const char* format = "%lld"); + ReVarArgs& arg(unsigned int arg, const char* format = "%u"); + ReVarArgs& arg(uint64_t value, const char* format = "%llu"); ReVarArgs& arg(const char* arg, int minWidth = 0, int maxWidth = 1024, bool alignRight = false); ReVarArgs& arg(double arg, const char* format = "%f"); const char* asCString(); diff --git a/cunit/cuReHashList.cpp b/cunit/cuReHashList.cpp index 33c7b86..fc21a66 100644 --- a/cunit/cuReHashList.cpp +++ b/cunit/cuReHashList.cpp @@ -13,7 +13,7 @@ private: testNext(); } void massTest(){ - const int maxKeys = 50*1000; + const int maxKeys = 20*1000; const int minLength = 3; const int maxLength = 10; const int countGet = maxKeys * 50; diff --git a/cunit/cuReTest.cpp b/cunit/cuReTest.cpp index 15bec42..48688ff 100644 --- a/cunit/cuReTest.cpp +++ b/cunit/cuReTest.cpp @@ -41,7 +41,7 @@ private: checkDirExists("/etc/"); checkFileExists("/etc/passwd"); #elif defined __WIN32__ - checkDirExists("c:\\windows") + checkDirExists("c:\\windows"); #endif log(false, "8 errors follow:"); checkT(false); diff --git a/cunit/cuReTraverser.cpp b/cunit/cuReTraverser.cpp index f64406f..3ef6366 100644 --- a/cunit/cuReTraverser.cpp +++ b/cunit/cuReTraverser.cpp @@ -2,7 +2,6 @@ * cuReTraverser.cpp * * Created on: 23.12.2014 - * Author: hm */ #include "base/rebase.hpp" @@ -48,16 +47,18 @@ private: makeDir("dir1/dir1_1"); makeDir("dir1/dir1_2"); makeDir("dir1/dir1_2/dir1_2_1"); + makeDir("dir1/cache"); makeFile("dir1/dir1_2/dir1_2_1/x1.txt"); makeFile("dir1/dir1_2/dir1_2_1/x2.txt"); makeFile("dir2/2.x"); + makeFile("dir1/cache/cache.txt"); } void run(){ - initTree(); - testDirOptions(); - checkSetFilterFromProgramArgs(); - testDirStatistic(); + initTree(); testBasic(); + testDirOptions(); + checkSetFilterFromProgramArgs(); + testDirStatistic(); testCopyFile(); testList(); } @@ -67,6 +68,7 @@ private: tools.list(2, argv); } void testCopyFile(){ +#if defined __linux__ ReByteBuffer src(m_base); src.append("dir1/dir1_2/dir1_2_1/x1.txt"); ReByteBuffer trg(testDir()); @@ -75,6 +77,9 @@ private: buffer.ensureSize(5); ReDirSync::copyFile(src.str(), 0, -1ll, trg.str(), buffer, ReLogger::globalLogger()); checkFileEqu(src.str(), trg.str()); +#else + log(false, "testCopyFile not implemented"); +#endif } void checkRelDate(time_t absTime, int relTime){ @@ -155,6 +160,13 @@ private: } void testBasic(){ ReTraverser traverser(m_base.str()); + RePatternList patterns; + // exclude */cache/* + ReByteBuffer buffer(";*;-*"); + buffer.append(ReTraverser::m_separatorStr).append("cache") + .append(ReTraverser::m_separatorStr); + patterns.set(buffer.str()); + traverser.setDirPattern(&patterns); int level = 0; ReDirStatus_t* entry; ReHashList hash; @@ -171,6 +183,7 @@ private: checkOneFile("dir1_1", "dir1", hash); checkOneFile("dir1_2", "dir1", hash); checkOneFile("dir1", m_base.str(), hash); + checkF(hash.get("cache.txt", buffer)); } void testDirStatistic(){ ReDirStatistic stat; @@ -192,7 +205,7 @@ private: log(false, list2.join("\n", buffer).str()); buffer.set(list.strOf(0), list.strLengthOf(0)); - checkT(buffer.startsWith(" 0.000054 MB 2 3\t")); + checkT(buffer.startsWith(" 0.000074 MB 3 4\t")); expected.set(m_base.str(), m_base.length()).append("dir1", -1) .append(ReTraverser::m_separatorStr); checkT(buffer.endsWith(expected.str())); @@ -204,7 +217,7 @@ private: checkT(buffer.endsWith(expected.str())); buffer.set(list.strOf(2), list.strLengthOf(2)); - checkT(buffer.startsWith(" 0.000067 MB 4 5\t")); + checkT(buffer.startsWith(" 0.000087 MB 5 6\t")); expected.set(m_base.str(), m_base.length()); } }; diff --git a/cunit/testall.cpp b/cunit/testall.cpp index f914f47..e5fe75d 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -11,11 +11,8 @@ #endif void testBase(){ - extern void testReHashList(void); - testReHashList(); - extern void testReTestUnit(); - testReTestUnit(); + //testReTestUnit(); extern void testReByteBuffer(); testReByteBuffer(); extern void testReSeqArray(void); @@ -66,8 +63,8 @@ void testMath(){ void testAll(){ try { - testBase(); testOs(); + testBase(); testMath(); testString(); } catch (ReException e){ diff --git a/math/ReRandomizer.cpp b/math/ReRandomizer.cpp index d646d92..f1d4ab9 100644 --- a/math/ReRandomizer.cpp +++ b/math/ReRandomizer.cpp @@ -56,9 +56,11 @@ ReRandomizer::seed_t ReRandomizer::nearTrueRandom(){ if (read(fh, buffer, sizeof buffer) > 0) rc ^= *(seed_t*) buffer; close(fh); +#elif defined __WIN32__ #else #error "no true random" #endif + return rc; } /** * @brief Returns the next random integer. @@ -102,7 +104,7 @@ int ReRandomizer::nextInt(int maxValue, int minValue){ * @return The next seed. */ int64_t ReRandomizer::nextInt64(int64_t maxValue, int64_t minValue){ - int rc; + seed_t rc; if (minValue > maxValue){ rc = minValue; minValue = maxValue; @@ -112,7 +114,7 @@ int64_t ReRandomizer::nextInt64(int64_t maxValue, int64_t minValue){ if (minValue == maxValue) rc = minValue; else if (minValue == 0 && maxValue == LLONG_MAX) - rc = abs(seed); + rc = abs((int64_t) seed); else if (uint64_t(maxValue - minValue) < LLONG_MAX) // no signed int64 overflow: rc = minValue + seed % (maxValue - minValue + 1); diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index a7def56..d87066f 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -384,10 +384,15 @@ ReTraverser::ReTraverser(const char* base) : m_level(-1), m_base(base), // m_dirs - m_passNoForDirSearch(2) + m_passNoForDirSearch(2), + m_dirPatterns(NULL) { memset(m_dirs, 0, sizeof m_dirs); m_dirs[0] = new ReDirStatus_t(); + // remove a preceeding "./". This simplifies the pattern expressions: + if (m_base.startsWith(ReByteBuffer(".").append(m_separatorStr, 1).str())){ + m_base.remove(0, 2); + } } /** @@ -444,8 +449,10 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level) } else { // we are interested only in true subdirectories: again = true; - if (current->isDirectory() && ! current->isDotDir() && ! current->isLink() - && m_level < m_maxLevel){ + if (m_level < m_maxLevel && current->isDirectory() + && ! current->isDotDir() && ! current->isLink() + && (m_dirPatterns == NULL || isAllowedDir(current->m_path, + current->node()))){ // open a new level alreadyRead = initEntry(current->m_path, current->node() , m_level + 1); } diff --git a/os/ReTraverser.hpp b/os/ReTraverser.hpp index c751c48..24e161b 100644 --- a/os/ReTraverser.hpp +++ b/os/ReTraverser.hpp @@ -104,19 +104,35 @@ public: public: ReDirStatus_t* rawNextFile(int& level); ReDirStatus_t* nextFile(int& level, ReDirEntryFilter_t* filter = NULL); + /** Sets directory filter (pattern list). + * @param pattern pattern list for the subdirs to be entered + */ + void setDirPattern(RePatternList* pattern) + { m_dirPatterns = pattern; } /** Sets the maximal depth. * @param value the value to set */ - void setMinLevel(int value) - { m_minLevel = value; } + void setMaxLevel(int value) + { m_maxLevel = value; } /** Sets the minimal depth. * @param value the value to set */ - void setMaxLevel(int value) - { m_maxLevel = value; } + void setMinLevel(int value) + { m_minLevel = value; } protected: bool initEntry(const ReByteBuffer& parent, const char* node, int level); void freeEntry(int level); + /** + * Tests whether a directory should be processed. + * @param path the path of the subdir + * @param node the base name of the subdir + * @return true: the subdir will be processed
+ * false: do not enter this subdir + */ + inline bool isAllowedDir(const ReByteBuffer& path, const char* node){ + bool rc = m_dirPatterns->match(ReByteBuffer(path).append(node).str()); + return rc; + } protected: int m_minLevel; int m_maxLevel; @@ -126,6 +142,9 @@ protected: /// each directory will be passed twice: for all files + for directories only /// 1: depth first 2: breadth first int m_passNoForDirSearch; + /// a subdirectory will be entered only if this pattern list matches + /// if NULL any directory will be entered + RePatternList* m_dirPatterns; public: static const char m_separator; static const char* const m_separatorStr;