From b86000ddd55e27d9a6cbf0349bc932ef332c4ea1 Mon Sep 17 00:00:00 2001 From: hama Date: Fri, 30 Jan 2015 00:24:21 +0100 Subject: [PATCH] status() and dump() for some classes, ReDirSync works with path filter --- base/ReByteBuffer.cpp | 2 +- base/ReByteBuffer.hpp | 2 +- base/ReHashList.cpp | 4 +- base/ReHashList.hpp | 4 +- base/ReSeqArray.cpp | 9 +++-- base/ReSeqArray.hpp | 16 ++++---- base/ReStringList.cpp | 84 +++++++++++++++++++++------------------- base/ReStringList.hpp | 2 +- cunit/cuReStringList.cpp | 21 +++------- cunit/cuReTraverser.cpp | 9 ++--- os/ReDirTools.cpp | 6 ++- string/ReMatcher.cpp | 63 ++++++++++++++++++++++++++++++ string/ReMatcher.hpp | 6 ++- 13 files changed, 145 insertions(+), 83 deletions(-) diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 0e25832..acf4992 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -607,7 +607,7 @@ bool ReByteBuffer::startsWith(const Byte* head, size_t headLength, const bool ig * @param prefix NULL or a prefix of the output * @return buffer.str() (for chaining) */ -const char* ReByteBuffer::status(ReByteBuffer& buffer, const char* prefix){ +const char* ReByteBuffer::status(ReByteBuffer& buffer, const char* prefix) const{ if (prefix != NULL) buffer.append(prefix); buffer.append("capacity:").appendInt(m_capacity) diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index c8d5301..a3bcc5b 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -166,7 +166,7 @@ public: bool splice(size_t ix, size_t replacedLength, const Byte* source, size_t length); bool startsWith(const Byte* head, size_t headLength = -1, const bool ignoreCase = false) const; - const char* status(ReByteBuffer& buffer, const char* prefix = NULL); + const char* status(ReByteBuffer& buffer, const char* prefix = NULL) const; /** @brief Returns the buffer content as C string. * This is exactly the same result as from getBuffer(). * @return The internal used buffer. diff --git a/base/ReHashList.cpp b/base/ReHashList.cpp index c0bd295..123c3f9 100644 --- a/base/ReHashList.cpp +++ b/base/ReHashList.cpp @@ -67,7 +67,7 @@ void ReHashList::clear(){ * @param stream OUT: output stream * @param prefix NULL or a string which is written first */ -void ReHashList::dump(FILE* stream, const char* prefix){ +void ReHashList::dump(FILE* stream, const char* prefix) const{ if (prefix != NULL) fprintf(stream, "%s\n", prefix); ReArrayPosition position; @@ -214,7 +214,7 @@ void ReHashList::setCapacity(int maxKeys, int keySpace, int contentSpace){ * @param prefix NULL or a prefix of the output * @return buffer.str() (for chaining) */ -const char* ReHashList::status(ReByteBuffer& buffer, const char* prefix){ +const char* ReHashList::status(ReByteBuffer& buffer, const char* prefix) const{ if (prefix != NULL) buffer.append(prefix).append("\n", 1); m_keys.status(buffer, "keys: "); diff --git a/base/ReHashList.hpp b/base/ReHashList.hpp index f00f89b..bd0618a 100644 --- a/base/ReHashList.hpp +++ b/base/ReHashList.hpp @@ -36,7 +36,7 @@ public: virtual ~ReHashList(); public: void clear(); - void dump(FILE* stream, const char* prefix = NULL); + void dump(FILE* stream, const char* prefix = NULL) const; bool get(const Byte* key, size_t keyLength, ReByteBuffer& value) const; bool get(const ReByteBuffer& key, ReByteBuffer& value) const; bool next(ReArrayPosition& position, ReByteBuffer* key, ReByteBuffer* val) const; @@ -44,7 +44,7 @@ public: void put(const char* key, const char* value); void put(const ReByteBuffer& key, const ReByteBuffer& value); void setCapacity(int maxKeys, int keySpace, int contentSpace); - const char* status(ReByteBuffer& buffer, const char* prefix = NULL); + const char* status(ReByteBuffer& buffer, const char* prefix = NULL) const; protected: int find(const Byte* key, size_t length) const; diff --git a/base/ReSeqArray.cpp b/base/ReSeqArray.cpp index 4efc809..8b73fbb 100644 --- a/base/ReSeqArray.cpp +++ b/base/ReSeqArray.cpp @@ -243,11 +243,14 @@ int ReSeqArray::compare(Index index1, Index index2){ /** * Writes the content to a stream. * - * @param fp target file pointer + * @param fp target file pointer + * @param prefix NULL or a introduction */ -void ReSeqArray::dump(FILE* fp) const{ +void ReSeqArray::dump(FILE* fp, const char* prefix) const{ ReByteBuffer buffer; Tag tag; + if (prefix != NULL) + fprintf(fp, "%s\n", prefix); for (int ix = 0; ix < (int) count(); ix++){ get(ix, buffer, &tag); fprintf(fp, "%d: (%ld) [%d] %s\n", ix, (int64_t) tag, (int) buffer.length(), @@ -715,7 +718,7 @@ void ReSeqArray::sort(){ * @param prefix NULL or a prefix of the output * @return buffer.str() (for chaining) */ -const char* ReSeqArray::status(ReByteBuffer& buffer, const char* prefix){ +const char* ReSeqArray::status(ReByteBuffer& buffer, const char* prefix) const{ if (prefix != NULL) buffer.append(prefix); buffer.append("age: ").appendInt(m_age).append("\n"); diff --git a/base/ReSeqArray.hpp b/base/ReSeqArray.hpp index b23817a..4489de5 100644 --- a/base/ReSeqArray.hpp +++ b/base/ReSeqArray.hpp @@ -49,9 +49,15 @@ public: inline Index count() const { return m_list.length() / m_entrySize; } - void dump(FILE* fp) const; + void dump(FILE* fp, const char* prefix) const; Index find(const Byte* toFind, size_t length, Tag* tag = NULL) const; bool get(Index index, ReByteBuffer& value, Tag* tag = NULL) const; + /** Returns whether the comparisons inside the list are case insensitive. + * @return true the comparisons inside the list are case insensitive + */ + bool ignoreCase() const { + return m_ignoreCase; + } /** Returns whether the list is sorted automatically. * Note: comparison is controlled by m_ignoreCase. * @return true the byte sequences will be sorted @@ -59,12 +65,6 @@ public: bool isSorted() const { return m_sorted; } - /** Returns whether the comparisons inside the list are case insensitive. - * @return true the comparisons inside the list are case insensitive - */ - bool ignoreCase() const { - return m_ignoreCase; - } void remove(Index index); void set(Index index, const Byte* source, size_t sourceLength, Tag tag); void setCapacity(int maxIndices, int maxStringSpace); @@ -72,7 +72,7 @@ public: void setSorted(bool onNotOff); void setIgnoreCase(bool onNotOff); void sort(); - const char* status(ReByteBuffer& buffer, const char* prefix = NULL); + const char* status(ReByteBuffer& buffer, const char* prefix = NULL) const; protected: /** @brief Returns a pointer of the content buffer. * @return A pointer of the first byte of the content buffer. diff --git a/base/ReStringList.cpp b/base/ReStringList.cpp index acd9d7c..fab756a 100644 --- a/base/ReStringList.cpp +++ b/base/ReStringList.cpp @@ -58,6 +58,46 @@ ReStringList& ReStringList::append(const ReStringList& source){ add(-1, source.strOf(ii), source.sizeOf(ii), source.tagOf(ii)); return *this; } + +/** @brief Tests the equality with another instance. + * + * Compares the internal array of strings with another instance. + * Two instances are equal when the number of strings are equal + * and the n.th string is equal to the n.th string in the other instance. + * + * @param toCompare the other instance which will be compared + * + * @return true: the other instance is equal. false: Otherwise + */ +bool ReStringList::equal(const ReStringList& toCompare) const{ + bool rc = count() == toCompare.count() && firstDiff(toCompare) == -1; + return rc; +} + +/** @brief Returns the index of the first different string. + * + * Compares the internal array of strings with another instance + * + * @param toCompare the other instance which will be compared + * + * @return -1: the instances are equal. Otherwise: the index of the first different string + * + */ +int ReStringList::firstDiff(const ReStringList& toCompare) const{ + int rc = -1; + int count1 = count(); + int count2 = toCompare.count(); + int maxIx = count1 > count2 ? count2 : count1; + for (size_t ix = 0; rc == -1 && (int) ix < maxIx; ix++){ + if (sizeOf(ix) != toCompare.sizeOf(ix) + || strcmp(strOf(ix), toCompare.strOf(ix)) != 0) + rc = (int) ix; + } + if (rc == -1 && count1 != count2) + rc = maxIx; + return rc; +} + /** @brief Inserts a string at a given index. * * If the index exceeds the length of the array it will be appended. @@ -287,10 +327,11 @@ void ReStringList::split(const char* list, char separator, bool append){ * @param result OUT: the result buffer * @return buffer (for chaining) */ -ReByteBuffer& ReStringList::join(const char* separator, ReByteBuffer& result) const{ +ReByteBuffer& ReStringList::join(const char* separator, ReByteBuffer& result, + bool append) const{ size_t theCount = count(); - - result.setLength(0); + if (! append) + result.setLength(0); size_t lengthSep = strlen(separator); for (size_t ix = 0; ix < theCount; ix++){ @@ -352,40 +393,3 @@ bool ReStringList::readFromFile(const char* filename, bool cutNewline){ } return rc; } -/** @brief Returns the index of the first different string. - * - * Compares the internal array of strings with another instance - * - * @param toCompare the other instance which will be compared - * - * @return -1: the instances are equal. Otherwise: the index of the first different string - * - */ -int ReStringList::firstDiff(const ReStringList& toCompare) const{ - int rc = -1; - int count1 = count(); - int count2 = toCompare.count(); - int maxIx = count1 > count2 ? count2 : count1; - for (size_t ix = 0; rc == -1 && (int) ix < maxIx; ix++){ - if (sizeOf(ix) != toCompare.sizeOf(ix) - || strcmp(strOf(ix), toCompare.strOf(ix)) != 0) - rc = (int) ix; - } - if (rc == -1 && count1 != count2) - rc = maxIx; - return rc; -} -/** @brief Tests the equality with another instance. - * - * Compares the internal array of strings with another instance. - * Two instances are equal when the number of strings are equal - * and the n.th string is equal to the n.th string in the other instance. - * - * @param toCompare the other instance which will be compared - * - * @return true: the other instance is equal. false: Otherwise - */ -bool ReStringList::equal(const ReStringList& toCompare) const{ - bool rc = count() == toCompare.count() && firstDiff(toCompare) == -1; - return rc; -} diff --git a/base/ReStringList.hpp b/base/ReStringList.hpp index 08b1d82..d77a429 100644 --- a/base/ReStringList.hpp +++ b/base/ReStringList.hpp @@ -45,7 +45,7 @@ public: Index indexOf(const char* toFind, bool ignoreCase = false, Index start = 0) const; void insert(Index index, const char* source, Tag tag = 0); - ReByteBuffer& join(const char* separator, ReByteBuffer& result) const; + ReByteBuffer& join(const char* separator, ReByteBuffer& result, bool append = false) const; Index nextStartingWith(Index index, const char* prefix, bool ignoreCase = false); bool readFromFile(const char* filename, bool cutNewline = true); diff --git a/cunit/cuReStringList.cpp b/cunit/cuReStringList.cpp index a3199fc..6b7266c 100644 --- a/cunit/cuReStringList.cpp +++ b/cunit/cuReStringList.cpp @@ -134,25 +134,14 @@ private: checkEqu("4", list.strOf(3)); checkEqu("", list.strOf(4)); checkEqu("99", list.strOf(5)); - ReByteBuffer value; + ReByteBuffer value("x"); list.join(";", value); checkEqu(str, value.str()); - list.split("1\r\n2\n\r3", '\n'); - checkEqu(3U, list.count()); - checkEqu("1", list.strOf(0)); - checkEqu("2", list.strOf(1)); - checkEqu("3", list.strOf(2)); - - list.split("xyz\tXYZ", '\t', true); - checkEqu(5U, list.count()); - checkEqu("1", list.strOf(0)); - checkEqu("2", list.strOf(1)); - checkEqu("3", list.strOf(2)); - checkEqu("xyz", list.strOf(3)); - checkEqu("XYZ", list.strOf(4)); - - + list.split("1\n2", '\n'); + value.set("xxx", 3); + list.join(";", value, true); + checkEqu("xxx1;2", value.str()); } void testFile(){ if (true){ diff --git a/cunit/cuReTraverser.cpp b/cunit/cuReTraverser.cpp index 064cdee..c2f66c0 100644 --- a/cunit/cuReTraverser.cpp +++ b/cunit/cuReTraverser.cpp @@ -171,9 +171,7 @@ private: ReTraverser traverser(m_base.str()); RePatternList patterns; // exclude */cache/* - ReByteBuffer buffer(";*;-*"); - buffer.append(OS_SEPARATOR).append("cache") - .append(OS_SEPARATOR); + ReByteBuffer buffer(";*;-*/cache"); patterns.set(buffer.str()); traverser.setDirPattern(&patterns); int level = 0; @@ -191,7 +189,6 @@ private: checkOneFile("dir1_2_1", "dir1_2", hash); checkOneFile("dir1_1", "dir1", hash); checkOneFile("dir1_2", "dir1", hash); - checkOneFile("dir1", m_base.str(), hash); checkF(hash.get("cache.txt", buffer)); } void testDirStatistic(){ @@ -200,7 +197,7 @@ private: ReByteBuffer buffer; ReByteBuffer expected; log(false, list.join("\n", buffer).str()); - checkEqu(3u, list.count()); + checkEqu(4u, list.count()); // "1 t:\temp\winfried\2\retestunit\dir1\n" buffer.set(list.strOf(0), list.strLengthOf(0)); checkT(buffer.startsWith("1\t")); @@ -220,7 +217,7 @@ private: checkT(buffer.endsWith(expected.str())); buffer.set(list.strOf(1), list.strLengthOf(1)); - checkT(buffer.startsWith(" 0.000008 MB 1 0\t")); + checkT(buffer.startsWith(" 0.000054 MB 2 3\t")); expected.set(m_base.str(), m_base.length()).append("dir2", -1) .append(OS_SEPARATOR); checkT(buffer.endsWith(expected.str())); diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index f85e111..50ab77b 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -503,10 +503,12 @@ void ReDirOptions::optimizePathPattern(ReByteBuffer& buffer){ rootList.append(item.str(), 0); } } - if (rootList.count() > 0) + if (rootList.count() > 0){ list.append(rootList); + } item.set(buffer.str(), 1); - list.join(item.str(), buffer); + buffer.set(item); + list.join(item.str(), buffer, true); } /** * Sets the standard filter options given by the program arguments. diff --git a/string/ReMatcher.cpp b/string/ReMatcher.cpp index d76e047..e2b4940 100644 --- a/string/ReMatcher.cpp +++ b/string/ReMatcher.cpp @@ -78,6 +78,21 @@ bool ReSimpleMatcher::compile(const char* pattern){ } return rc; } + +/** + * Writes the content to a stream. + * + * @param fp target file pointer + * @param prefix NULL or a introduction + */ +void ReSimpleMatcher::dump(FILE* fp, const char* prefix) const{ + ReByteBuffer buffer; + if (prefix != NULL) + fprintf(fp, "%s\n", prefix); + fprintf(fp, "pattern: %s token (%d): %s\n", m_pattern.str(), + m_tokens.count(), m_tokens.join(" ", buffer).str()); +} + /** * Tests whether a name matches the pattern stored in the instance. * @@ -187,6 +202,20 @@ bool ReSimpleMatcher::search(const ReByteBuffer& toTest, ReHit* hit, bool greedy rc = ! rc; return rc; } +/** + * Appends the status data of the instance onto the buffer. + * + * @param buffer OUT: the space for the data report + * @param prefix NULL or a prefix of the output + * @return buffer.str() (for chaining) + */ +const char* ReSimpleMatcher::status(ReByteBuffer& buffer, const char* prefix) const{ + if (prefix != NULL) + buffer.append(prefix); + m_pattern.status(buffer, "pattern: "); + m_pattern.status(buffer, "tokens : "); + return buffer.str(); +} /** * Constructor. @@ -213,6 +242,23 @@ void RePatternList::destroy(){ delete[] m_patterns; m_patterns = NULL; } + +/** + * Writes the content to a stream. + * + * @param fp target file pointer + * @param prefix NULL or a introduction + */ +void RePatternList::dump(FILE* fp, const char* prefix) const{ + ReByteBuffer buffer; + if (prefix != NULL) + fprintf(fp, "%s\n", prefix); + for (int ix = 0; ix < m_count; ix++){ + fprintf(fp, "%d: ", ix); + m_patterns[ix]->dump(fp, NULL); + } +} + /** * Tests whether a name matches at least one of the patterns. * @@ -318,3 +364,20 @@ int RePatternList::setOne(int index, const char* pattern, size_t patternLength, m_patterns[ix]->setIgnoreCase(ignoreCase); return isNotPattern ? index: index + 1; } + +/** + * Appends the status data of the instance onto the buffer. + * + * @param buffer OUT: the space for the data report + * @param prefix NULL or a prefix of the output + * @return buffer.str() (for chaining) + */ +const char* RePatternList::status(ReByteBuffer& buffer, const char* prefix) const{ + if (prefix != NULL) + buffer.append(prefix); + for (int ix = 0; ix < m_count; ix++){ + buffer.appendInt(ix).append(": ", 2); + m_patterns[ix]->status(buffer, NULL); + } + return buffer.str(); +} diff --git a/string/ReMatcher.hpp b/string/ReMatcher.hpp index 6908f87..e12bd41 100644 --- a/string/ReMatcher.hpp +++ b/string/ReMatcher.hpp @@ -73,9 +73,11 @@ public: virtual ~ReSimpleMatcher(); public: virtual bool compile(const char* pattern); + void dump(FILE* fp, const char* prefix) const; virtual bool match(const ReByteBuffer& toTest, ReHit* hit = NULL) const; virtual bool search(const ReByteBuffer& toTest, ReHit* hit = NULL, bool greedy = false) const; + const char* status(ReByteBuffer& buffer, const char* prefix) const; protected: bool searchTokens(const ReByteBuffer& toTest, int from, int to, ReHit* hit, bool greedy) const; @@ -96,6 +98,7 @@ public: return m_count; } void destroy(); + void dump(FILE* fp, const char* prefix) const; bool match(const ReByteBuffer& name); /** @brief Tests whether a string matches the patterns. * @param name the string to Test @@ -110,7 +113,8 @@ public: const char* patternString() const { return m_patternString.str(); } void set(const char* patterns, bool ignoreCase = false, - const char* separator = NULL, const char* notPrefix = "-"); + const char* separator = NULL, const char* notPrefix = "-"); + const char* status(ReByteBuffer& buffer, const char* prefix) const; private: int setOne(int index, const char* pattern, size_t patternLength, bool ignoreCase, const ReByteBuffer& notPrefix); -- 2.39.5