From ec347ac73a010ec3b535078162324e6f2d13da90 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Wed, 25 Feb 2015 12:20:06 +0100 Subject: [PATCH] Refactoring. Bug: missing FindClose() --- base/ReByteBuffer.cpp | 48 ++++++++++++++ base/ReByteBuffer.hpp | 2 + base/ReDirectory.cpp | 14 +++- base/ReDirectory.hpp | 1 + base/ReTestUnit.cpp | 19 +++++- base/ReTestUnit.hpp | 2 + cunit/cuReByteBuffer.cpp | 130 ++++++++++++++++++++++++-------------- cunit/cuReDirTools.cpp | 35 ++++++---- cunit/cuReHashList.cpp | 14 ++-- cunit/cuReMD5.cpp | 8 +-- cunit/cuReSeqArray.cpp | 40 ++++++------ cunit/cuReStringList.cpp | 4 +- cunit/cuReStringUtils.cpp | 102 +++++++++++++++--------------- cunit/cuReconfig.cpp | 4 +- cunit/testall.cpp | 33 +++++----- os/ReDirTools.cpp | 3 +- os/ReDirTools.hpp | 2 + os/ReTraverser.cpp | 71 ++++++++++++++++++++- 18 files changed, 364 insertions(+), 168 deletions(-) diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 06e6c3e..4f28020 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -161,6 +161,53 @@ ReByteBuffer& ReByteBuffer::append(double value, const char* format){ return *this; } +/** + * Appends a string with minimal/maximal length. + * + * If the length is greater than the maximal length the string will be cutted + * (in the middle) and a separator will set at the cut. + * + *
Example:
+ * buffer.appendFix("sammy2", -1, 5, 5, "*").appendFix("x", 1, 99, -3);
+ * 
+ * Buffer contains: "sa*y2  x"
+ * 
+ * + * @param data data to append + * @param length length of data. -1: strlen(data) + * @param maxLength the stored length is at most this value + * @param minLength if the length is smaller padding characters will be added
+ * < 0: padding is done at the top + * @param separator NULL or the string used at the separaration point + * @param padding character used for padding (see minLength) + * @return *this (for chaining) + */ +ReByteBuffer& ReByteBuffer::appendFix(const char* data, size_t length, int maxLength, + int minLength, const char* separator, char padding){ + if (length == (size_t) -1) + length = strlen(data); + if (length < abs(minLength)){ + ensureSize(m_length + abs(minLength)); + if (minLength < 0) + appendChar(padding, - minLength - length); + append(data, length); + if (minLength > 0) + appendChar(padding, minLength - length); + } else if (length > maxLength){ + ensureSize(m_length + maxLength); + int sepLength = separator == NULL ? 0 : strlen(separator); + int lengthPart1 = (maxLength - sepLength + 1) / 2; + int lengthPart2 = maxLength - sepLength - lengthPart1; + append(data, lengthPart1); + if (sepLength > 0) + append(separator, sepLength); + append(data + length - lengthPart2, lengthPart2); + } else { + append(data, length); + } + return *this; +} + /** * Appends a hexadecimal dump of a memory array. * @@ -183,6 +230,7 @@ ReByteBuffer& ReByteBuffer::append(double value, const char* format){ * @param gapBehind behind gapBehind hex bytes a ' ' is appended
* -1: bytePerLine / 2 * @param separator NULL or a string between hex area and ASCII area + * @return *this (for chaining) */ ReByteBuffer& ReByteBuffer::appendHexDump(const char* data, size_t length, int offset, int bytesPerLine, const char* offsetFormat, diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index 0769f06..849c3f7 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -69,6 +69,8 @@ public: } return *this; } + ReByteBuffer& appendFix(const char* data, size_t length, int maxLength, + int minLength = 0, const char* separator = "*", char padding = ' '); ReByteBuffer& appendHexDump(const char* data, size_t length = -1, int offset = 0, int bytePerLine = 16, const char* offsetFormat = "%04x: ", bool withAscii = true, diff --git a/base/ReDirectory.cpp b/base/ReDirectory.cpp index 64fe2e0..d9daca8 100644 --- a/base/ReDirectory.cpp +++ b/base/ReDirectory.cpp @@ -23,7 +23,7 @@ ReDirectory::ReDirectory() : m_regExprIsInitialized(false), m_isRegExpr(false) #elif defined __WIN32__ - m_handle(0) + m_handle(INVALID_HANDLE_VALUE) //m_data() #endif { @@ -56,6 +56,11 @@ ReDirectory::ReDirectory(const char* path) : /** @brief Destructor. */ ReDirectory::~ReDirectory(){ + close(); +} +/** @brief Frees the resources. +*/ +void ReDirectory::close(){ #if defined __linux__ if (m_dir != NULL){ closedir(m_dir); @@ -65,6 +70,10 @@ ReDirectory::~ReDirectory(){ if (m_regExprIsInitialized) regfree(&m_regExpr); #elif defined __WIN32__ + if (m_handle != INVALID_HANDLE_VALUE){ + FindClose(m_handle); + m_handle = INVALID_HANDLE_VALUE; + } #endif } /** @brief Returns the name of the current file found by the last findFirst() @@ -90,6 +99,7 @@ const char* ReDirectory::currentFile(){ * false: only the files and subdirs in the dir will be deleted */ void ReDirectory::deleteTree(const char* base, bool deleteBaseToo){ + if (true){ ReDirectory dir(base); if (dir.findFirst("*", false)){ ReByteBuffer full; @@ -105,10 +115,12 @@ void ReDirectory::deleteTree(const char* base, bool deleteBaseToo){ } } while(dir.findNext()); } + dir.close(); if (deleteBaseToo){ _rmdir(base); } } +} /** @brief Returns the name of the directory. * diff --git a/base/ReDirectory.hpp b/base/ReDirectory.hpp index 0739fac..315dbda 100644 --- a/base/ReDirectory.hpp +++ b/base/ReDirectory.hpp @@ -17,6 +17,7 @@ public: ReDirectory(const char* path); ~ReDirectory(); public: + void close(); const char* currentFile(); bool findFirst(const char* pattern, bool isRegExpr); bool findNext(); diff --git a/base/ReTestUnit.cpp b/base/ReTestUnit.cpp index b78bcb4..24ab0c5 100644 --- a/base/ReTestUnit.cpp +++ b/base/ReTestUnit.cpp @@ -133,7 +133,24 @@ void ReTestUnit::assertEqual(const char* expected, const char* current, int line expected, current == NULL ? "" : current); } } - +/** @brief Compares a string and a ReByteBuffer. If not equal this will be logged. + * + * @param expected the expected value + * @param current the current value + * @param lineNo the line number of the test (for the error message) + */ +void ReTestUnit::assertEqual(const char* expected, const ReByteBuffer& current, int lineNo){ + assertEqual(expected, current.str(), lineNo); +} +/** @brief Compares two ReByteBuffer objects. If not equal this will be logged. + * + * @param expected the expected value + * @param current the current value + * @param lineNo the line number of the test (for the error message) + */ +void ReTestUnit::assertEqual(const ReByteBuffer& expected, const ReByteBuffer& current, int lineNo){ + assertEqual(expected.str(), current.str(), lineNo); +} /** @brief Compares two string values. If not equal this will be logged. * * @param expected the expected value diff --git a/base/ReTestUnit.hpp b/base/ReTestUnit.hpp index e515d8a..568aebd 100644 --- a/base/ReTestUnit.hpp +++ b/base/ReTestUnit.hpp @@ -25,6 +25,8 @@ public: void assertEqual(int expected, int current, int lineNo); void assertEqual(unsigned int expected, unsigned int current, int lineNo); void assertEqual(const char* expected, const char* current, int lineNo); + void assertEqual(const char* expected, const ReByteBuffer& current, int lineNo); + void assertEqual(const ReByteBuffer& expected, const ReByteBuffer& current, int lineNo); void assertEqualIgnoreCase(const char* expected, const char* current, int lineNo); void assertEqualFiles(const char* name1, const char* name2, int lineNo); void assertFileExists(const char* name, int lineNo); diff --git a/cunit/cuReByteBuffer.cpp b/cunit/cuReByteBuffer.cpp index 1315480..c261103 100644 --- a/cunit/cuReByteBuffer.cpp +++ b/cunit/cuReByteBuffer.cpp @@ -15,6 +15,7 @@ public: } private: void run(){ + testAppendFix(); testEnsureLastChar(); testLastChar(); testReduceLength(); @@ -45,6 +46,41 @@ private: testSplice(); testReplace(); } + void testAppendFix(){ + ReByteBuffer buffer; + // maxLength exceeded + // odd maxlength: + buffer.appendFix("123456", -1, 5); + checkEqu("12*56", buffer); + // given separator, even maxLength + buffer.appendFix("abcdefg", -1, 4, 4, "."); + checkEqu("12*56ab.g", buffer); + // given separator with more than one char / no separator + buffer.appendFix("ABCDEF", -1, 5, 5, "...").appendFix("xyz", -1, 2, 0, NULL); + checkEqu("12*56ab.gA...Fxz", buffer); + + buffer = "x"; + // minlength not reached: + // data length given, minLength == length + buffer.appendFix("123", 2U, 99, 2, NULL, ':'); + checkEqu("x12", buffer); + // positive minLength + buffer.appendFix("abc", -1, 99, 4, NULL, ':'); + checkEqu("x12abc:", buffer); + // negative minLength + buffer.appendFix("AB", -1, 99, -4, NULL, '.'); + checkEqu("x12abc:..AB", buffer); + + buffer = "y"; + // minLength and maxLength not reached: + // minLength equal, maxLength equal: + buffer.appendFix("1234", 4, 4, 4); + checkEqu("y1234", buffer); + // minLength smaller, maxLength larger: + buffer.appendFix("a", 1, 2, 1); + checkEqu("y1234a", buffer); + + } void testEnsureLastChar(){ ReByteBuffer buffer("1/"); buffer.ensureLastChar('/'); @@ -64,13 +100,13 @@ private: ReByteBuffer buffer("12345"); buffer.reduceLength(); checkEqu(4U, buffer.length()); - checkEqu("1234", buffer.str()); + checkEqu("1234", buffer); buffer.reduceLength(2); checkEqu(2U, buffer.length()); - checkEqu("12", buffer.str()); + checkEqu("12", buffer); buffer.reduceLength(0).reduceLength(-1); checkEqu(2U, buffer.length()); - checkEqu("12", buffer.str()); + checkEqu("12", buffer); buffer.reduceLength(99); checkEqu(0U, buffer.length()); } @@ -78,48 +114,48 @@ private: ReByteBuffer buffer("123"); buffer.removeLastChar('x').removeLastChar('3'); checkEqu(2U, buffer.length()); - checkEqu("12", buffer.str()); + checkEqu("12", buffer); } void testAppendChar(){ ReByteBuffer buffer; buffer.appendChar('1'); checkEqu(1U, buffer.length()); - checkEqu("1", buffer.str()); + checkEqu("1", buffer); buffer.appendChar('2'); checkEqu(2U, buffer.length()); - checkEqu("12", buffer.str()); + checkEqu("12", buffer); buffer.appendChar('x', 1); checkEqu(3U, buffer.length()); - checkEqu("12x", buffer.str()); + checkEqu("12x", buffer); buffer.appendChar('y', 3); checkEqu(6U, buffer.length()); - checkEqu("12xyyy", buffer.str()); + checkEqu("12xyyy", buffer); buffer.appendChar('x', 0); checkEqu(6U, buffer.length()); - checkEqu("12xyyy", buffer.str()); + checkEqu("12xyyy", buffer); buffer.appendChar('x', -1); checkEqu(6U, buffer.length()); - checkEqu("12xyyy", buffer.str()); + checkEqu("12xyyy", buffer); } void testAppendFloat(){ ReByteBuffer buffer; buffer.append(125 / 100.0); - checkEqu("1.250000", buffer.str()); + checkEqu("1.250000", buffer); buffer.append(0.333, "%.2f"); - checkEqu("1.2500000.33", buffer.str()); + checkEqu("1.2500000.33", buffer); } void testAppendInt64(){ ReByteBuffer buffer; buffer.appendInt((int64_t) 12345678901ll); - checkEqu("12345678901", buffer.str()); + checkEqu("12345678901", buffer); buffer.appendInt((uint64_t) 0x123456789ll, "%llx"); - checkEqu("12345678901123456789", buffer.str()); + checkEqu("12345678901123456789", buffer); } void testEnsureSize2(){ @@ -139,33 +175,33 @@ private: buf.appendHexDump("abcdefghijklmnopq"); checkEqu("0000: 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 | abcdefgh ijklmnop\n" "0010: 71 | q \n", - buf.str()); + buf); buf.setLength(0).appendHexDump("abcdefghijklmnopq", 16); checkEqu("0000: 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 | abcdefgh ijklmnop\n", - buf.str()); + buf); buf.setLength(0).appendHexDump("\t\nü23456789x123456", -1, 20, 8, "%3d: ", true, 4, 0, NULL); checkEqu(" 20: 090ac3bc 32333435....2345\n" " 28: 36373839 783132336789x123\n" - " 36: 343536 456 \n", buf.str()); + " 36: 343536 456 \n", buf); buf.setLength(0).appendHexDump("abcdefghijk", -1, 0, 8, "%3d: ", true, 2, 4); checkEqu(" 0: 6162 6364 6566 6768 | abcd efgh\n" - " 8: 696a 6b | ijk \n", buf.str()); + " 8: 696a 6b | ijk \n", buf); } void testFill(){ ReByteBuffer buf; buf.fill('=', 0, 3); - checkEqu("===", buf.str()); + checkEqu("===", buf); buf.fill('x', 1, 2); - checkEqu("=x=", buf.str()); + checkEqu("=x=", buf); } void testAppendMilliSec(){ ReByteBuffer buf; - checkEqu("5.123", buf.appendMilliSec(5123).str()); + checkEqu("5.123", buf.appendMilliSec(5123)); checkEqu("32:45.789", buf.setLength(0) - .appendMilliSec(32*60*1000+45789).str()); + .appendMilliSec(32*60*1000+45789)); checkEqu("17:04:06.976", buf.setLength(0) - .appendMilliSec(17*3600*1000 + 4*60*1000 + 6976).str()); + .appendMilliSec(17*3600*1000 + 4*60*1000 + 6976)); } void testSetDelta(){ ReByteBuffer buf("abcd"); @@ -349,7 +385,7 @@ private: buffer.append((Byte*)"123", 3); checkEqu("123", buffer.buffer()); - checkEqu("123", buffer.str()); + checkEqu("123", buffer); checkEqu(3u, buffer.length()); buffer.append((Byte*)"ab", 2); checkEqu("123ab", buffer.buffer()); @@ -374,22 +410,22 @@ private: checkEqu(3u, buffer.length()); buffer.append(buffer2); checkEqu("abcxyz", buffer.buffer()); - checkEqu("abcxyz", buffer.str()); + checkEqu("abcxyz", buffer); checkEqu(6u, buffer.length()); buffer.setLength(0); buffer.appendInt(-1); - checkEqu("-1", buffer.str()); + checkEqu("-1", buffer); checkEqu(2u, buffer.length()); buffer.appendInt(9, "%03d"); - checkEqu("-1009", buffer.str()); + checkEqu("-1009", buffer); checkEqu(5u, buffer.length()); buffer.setLength(0).appendInt((unsigned int) 123); - checkEqu("123", buffer.str()); + checkEqu("123", buffer); buffer.appendInt((unsigned int) 0x87654321, "%x"); - checkEqu("12387654321", buffer.str()); + checkEqu("12387654321", buffer); } void testOpAssignCopyConstructor() { @@ -398,12 +434,12 @@ private: ReByteBuffer buf3; { ReByteBuffer buf2(buf1); - checkEqu("abc", buf2.str()); + checkEqu("abc", buf2); buf3 = buf2; buf2.append("123", 3); } - checkEqu("abc", buf3.str()); - checkEqu("abc", buf1.str()); + checkEqu("abc", buf3); + checkEqu("abc", buf1); } void testAt(){ ReByteBuffer buf1; @@ -477,11 +513,11 @@ private: checkEqu(10u, buf1.length()); buf1.setLength(5); - checkEqu("01234", buf1.str()); + checkEqu("01234", buf1); checkEqu(5u, buf1.length()); buf1.setLengthAndFillOut(8, 'X'); - checkEqu("01234XXX", buf1.str()); + checkEqu("01234XXX", buf1); checkEqu(8u, buf1.length()); checkEqu(2000u, buf1.capacity()); } @@ -526,19 +562,19 @@ private: void testInsert(){ ReByteBuffer buf1; checkT(buf1.insert(0, "123", 2)); - checkEqu("12", buf1.str()); + checkEqu("12", buf1); checkEqu(2u, buf1.length()); checkT(buf1.insert(0, "abc", 1)); - checkEqu("a12", buf1.str()); + checkEqu("a12", buf1); checkEqu(3u, buf1.length()); checkT(buf1.insert(1, "x", 1)); - checkEqu("ax12", buf1.str()); + checkEqu("ax12", buf1); checkEqu(4u, buf1.length()); checkT(buf1.insert(4, "yz", 2)); - checkEqu("ax12yz", buf1.str()); + checkEqu("ax12yz", buf1); checkEqu(6u, buf1.length()); checkF(buf1.insert(-1, "-", 1)); @@ -549,27 +585,27 @@ private: ReByteBuffer buf1; buf1.set("1234567890", 10); checkT(buf1.remove(0, 2)); - checkEqu("34567890", buf1.str()); + checkEqu("34567890", buf1); checkEqu(8u, buf1.length()); checkF(buf1.remove(-1, 2)); - checkEqu("34567890", buf1.str()); + checkEqu("34567890", buf1); checkEqu(8u, buf1.length()); checkF(buf1.remove(9, 2)); - checkEqu("34567890", buf1.str()); + checkEqu("34567890", buf1); checkEqu(8u, buf1.length()); checkT(buf1.remove(7, 2)); - checkEqu("3456789", buf1.str()); + checkEqu("3456789", buf1); checkEqu(7u, buf1.length()); checkT(buf1.remove(5, 2)); - checkEqu("34567", buf1.str()); + checkEqu("34567", buf1); checkEqu(5u, buf1.length()); checkT(buf1.remove(0, 99)); - checkEqu("", buf1.str()); + checkEqu("", buf1); checkEqu(0u, buf1.length()); } void testReplace(){ @@ -577,14 +613,14 @@ private: buffer.set("1234", 4); buffer.replaceAll("12", 2, "abc", 3); - checkEqu("abc34", buffer.str()); + checkEqu("abc34", buffer); buffer.replaceAll("c34", 2, "uv", 3); - checkEqu("abuv", buffer.str()); + checkEqu("abuv", buffer); buffer.set("$$x$$", -1); buffer.replaceAll("$", 1, "XX", 2); - checkEqu("XXXXxXXXX", buffer.str()); + checkEqu("XXXXxXXXX", buffer); } void testSplice(){ ReByteBuffer buffer; diff --git a/cunit/cuReDirTools.cpp b/cunit/cuReDirTools.cpp index 6c5da85..ba878d3 100644 --- a/cunit/cuReDirTools.cpp +++ b/cunit/cuReDirTools.cpp @@ -22,12 +22,14 @@ public: m_base.append("dirtool"); _mkdir(m_base.str(), ALLPERMS); m_base.append(OS_SEPARATOR, -1); + m_testAll = true; run(); ReDirectory::deleteTree(m_base.str(), true); } private: ReByteBuffer m_base; ReByteBuffer m_buffer; + bool m_testAll; private: const char* makeDir(const char* relPath){ m_buffer = m_base; @@ -65,18 +67,25 @@ private: } void run(){ initTree(); - - testBatch(); - - testToolStatistic(); - testBasic(); - testDirOptions(); - checkSetFilterFromProgramArgs(); - testDirStatistic(); - testCopyFile(); - testList(); + testList2(); testToolSync(); - testBatch(); + if (m_testAll){ + testToolStatistic(); + testBasic(); + testDirOptions(); + checkSetFilterFromProgramArgs(); + testDirStatistic(); + testCopyFile(); + testList(); + testToolSync(); + testBatch(); + } + } + // F:\temp\retestunit\dirtool + void testList2(){ + const char* argv[] = { "dt", "list", m_base.str(), NULL }; + ReDirTools tools; + tools.main(3, (char**) argv); } void testList(){ const char* argv[] = { "list", m_base.str(), NULL }; @@ -263,10 +272,10 @@ private: cols.split(expected.strOf(ix), '*'); const char* col1 = cols.strOf(0); if (! line.startsWith(col1)) - checkEqu(col1, line.str()); + checkEqu(col1, line); const char* col2 = cols.strOf(1); if (! line.endsWith(col2)){ - checkEqu(col2, line.str()); + checkEqu(col2, line); } } } diff --git a/cunit/cuReHashList.cpp b/cunit/cuReHashList.cpp index f6820ba..4dbca67 100644 --- a/cunit/cuReHashList.cpp +++ b/cunit/cuReHashList.cpp @@ -65,28 +65,28 @@ private: hash.put("abc", "123"); checkT(hash.get("abc", -1, value)); - checkEqu("123", value.str()); + checkEqu("123", value); hash.put("ab", "999"); checkT(hash.get("ab", -1, value)); - checkEqu("999", value.str()); + checkEqu("999", value); checkT(hash.get("abc", -1, value)); - checkEqu("123", value.str()); + checkEqu("123", value); hash.put("abc", "!!!"); checkT(hash.get("abc", -1, value)); - checkEqu("!!!", value.str()); + checkEqu("!!!", value); checkT(hash.get("ab", -1, value)); - checkEqu("999", value.str()); + checkEqu("999", value); hash.put("abc", "longer"); checkT(hash.get("abc", -1, value)); - checkEqu("longer", value.str()); + checkEqu("longer", value); checkT(hash.get("ab", -1, value)); - checkEqu("999", value.str()); + checkEqu("999", value); } void testNext(){ ReHashList hash; diff --git a/cunit/cuReMD5.cpp b/cunit/cuReMD5.cpp index f17df7c..aa484e4 100644 --- a/cunit/cuReMD5.cpp +++ b/cunit/cuReMD5.cpp @@ -43,16 +43,16 @@ private: const char* text = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ01234567890\n"; md5.update((const uint8_t*)text, strlen(text)); checkEqu("c0a278c051a6898f6ad05da7fa80a1c4", - md5.hexDigest().str()); + md5.hexDigest()); md5.reset(); text = "The quick brown fox jumps over the lazy dog"; md5.update((const uint8_t*) text, -1); checkEqu("9e107d9d372bb6826bd81d3542a419d6", - md5.hexDigest().str()); + md5.hexDigest()); md5.reset(); md5.update((const uint8_t*)"", 0); checkEqu("d41d8cd98f00b204e9800998ecf8427e", - md5.hexDigest().str()); + md5.hexDigest()); } int testOneLong(ReByteBuffer& text, int seed2 = 0){ ReMD5 md5; @@ -74,7 +74,7 @@ private: text.remove(0, part); rc++; } - checkEqu(digest.str(), md5.hexDigest().str()); + checkEqu(digest, md5.hexDigest()); return rc; } void testLong(){ diff --git a/cunit/cuReSeqArray.cpp b/cunit/cuReSeqArray.cpp index 8d5fe54..942aa75 100644 --- a/cunit/cuReSeqArray.cpp +++ b/cunit/cuReSeqArray.cpp @@ -151,7 +151,7 @@ private: list.add(-1, expectedValue.str(), -1, expectedTag); checkEqu(ix + 1, list.count()); checkT(list.get(ix, value, &tag)); - checkEqu(expectedValue.str(), value.str()); + checkEqu(expectedValue, value); if (expectedTag != tag) checkEqu(expectedTag, tag); } @@ -160,7 +160,7 @@ private: expectedTag = (1ll << ix); expectedValue.appendChar('x'); checkT(list.get(ix, value, &tag)); - checkEqu(expectedValue.str(), value.str()); + checkEqu(expectedValue, value); checkEqu(expectedTag, tag); } @@ -177,7 +177,7 @@ private: list.add(-1, expectedValue.str(), -1, expectedTag); checkEqu(ix + 1, list.count()); checkT(list.get(ix, value, &tag)); - checkEqu(expectedValue.str(), value.str()); + checkEqu(expectedValue, value); if (expectedTag != tag) checkEqu(expectedTag, tag); } @@ -200,43 +200,43 @@ private: list.add(-1, "xxx", -1, ReSeqArray::Tag(1l) << 31); checkT(list.get(0, value, &tag)); checkEqu(1ll << 31, tag); - checkEqu("xxx", value.str()); + checkEqu("xxx", value); list.clear(); list.add(-1, "123", -1, 0x3344556633445566ll); checkEqu(1u, list.count()); checkT(list.get(0, value, &tag)); - checkEqu("123", value.str()); + checkEqu("123", value); checkEqu((int64_t) 0x3344556633445566ll, tag); list.add(-1, "ab", -1, 0x345678abcdef3344ll); checkEqu(2u, list.count()); checkT(list.get(0, value)); - checkEqu("123", value.str()); + checkEqu("123", value); checkT(list.get(1, value, &tag)); - checkEqu("ab", value.str()); + checkEqu("ab", value); checkEqu(0x345678abcdef3344ll, tag); list.add(0, "xyz", -1, 300); checkEqu(3u, list.count()); checkT(list.get(0, value, &tag)); - checkEqu("xyz", value.str()); + checkEqu("xyz", value); checkT(list.get(1, value)); - checkEqu("123", value.str()); + checkEqu("123", value); checkT(list.get(2, value)); - checkEqu("ab", value.str()); + checkEqu("ab", value); checkEqu(300ll, tag); list.add(1, "vw", -1, 400); checkEqu(4u, list.count()); checkT(list.get(0, value)); - checkEqu("xyz", value.str()); + checkEqu("xyz", value); checkT(list.get(1, value, &tag)); - checkEqu("vw", value.str()); + checkEqu("vw", value); checkT(list.get(2, value)); - checkEqu("123", value.str()); + checkEqu("123", value); checkT(list.get(3, value)); - checkEqu("ab", value.str()); + checkEqu("ab", value); checkEqu(400ll, tag); list.clear(); @@ -256,29 +256,29 @@ private: list.remove(3); checkEqu(3u, list.count()); list.get(0, value, &tag); - checkEqu("abc", value.str()); + checkEqu("abc", value); checkEqu(100ll, tag); list.get(1, value, &tag); - checkEqu("def12", value.str()); + checkEqu("def12", value); checkEqu(200ll, tag); list.get(2, value, &tag); - checkEqu("ghi", value.str()); + checkEqu("ghi", value); checkEqu(300ll, tag); list.remove(1); checkEqu(2u, list.count()); list.get(0, value, &tag); - checkEqu("abc", value.str()); + checkEqu("abc", value); checkEqu(100ll, tag); list.get(1, value, &tag); - checkEqu("ghi", value.str()); + checkEqu("ghi", value); checkEqu(300ll, tag); list.remove(0); checkEqu(1u, list.count()); list.get(0, value, &tag); - checkEqu("ghi", value.str()); + checkEqu("ghi", value); checkEqu(300ll, tag); } diff --git a/cunit/cuReStringList.cpp b/cunit/cuReStringList.cpp index 3471f55..09095c4 100644 --- a/cunit/cuReStringList.cpp +++ b/cunit/cuReStringList.cpp @@ -139,12 +139,12 @@ private: checkEqu("99", list.strOf(5)); ReByteBuffer value("x"); list.join(";", value); - checkEqu(str, value.str()); + checkEqu(str, value); list.split("1\n2", '\n'); value.set("xxx", 3); list.join(";", value, true); - checkEqu("xxx1;2", value.str()); + checkEqu("xxx1;2", value); } void testFile(){ if (true){ diff --git a/cunit/cuReStringUtils.cpp b/cunit/cuReStringUtils.cpp index 6839381..22ab490 100644 --- a/cunit/cuReStringUtils.cpp +++ b/cunit/cuReStringUtils.cpp @@ -77,95 +77,95 @@ private: const char* fn = "file:/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("file:", protocol.str()); - checkEqu("/etc/samba/", path.str()); - checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("file:", protocol); + checkEqu("/etc/samba/", path); + checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("", protocol.str()); - checkEqu("/etc/samba/", path.str()); - checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("", protocol); + checkEqu("/etc/samba/", path); + checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "smb.cnf"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("", protocol.str()); - checkEqu("", path.str()); - checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("", protocol); + checkEqu("", path); + checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "smb"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("", protocol.str()); - checkEqu("", path.str()); - checkEqu("smb", name.str()); - checkEqu("", ext.str()); + checkEqu("", protocol); + checkEqu("", path); + checkEqu("smb", name); + checkEqu("", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "file:smb.003.cnf"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("file:", protocol.str()); - checkEqu("", path.str()); - checkEqu("smb.003", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("file:", protocol); + checkEqu("", path); + checkEqu("smb.003", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "file:/etc.bak/smb"; ReStringUtils::splitPath(fn, &protocol, &path, &name, &ext); - checkEqu("file:", protocol.str()); - checkEqu("/etc.bak/", path.str()); - checkEqu("smb", name.str()); - checkEqu("", ext.str()); + checkEqu("file:", protocol); + checkEqu("/etc.bak/", path); + checkEqu("smb", name); + checkEqu("", ext); ReStringUtils::joinPath(fullname, &protocol, &path, &name, &ext); - checkEqu(fn, fullname.str()); + checkEqu(fn, fullname); fn = "file:/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, NULL, &path, &name, &ext); - checkEqu("file:", protocol.str()); - checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("file:", protocol); + checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, &protocol, NULL, &name, &ext); - checkEqu("file:smb.cnf", fullname.str()); + checkEqu("file:smb.cnf", fullname); fn = "file:/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, NULL, NULL, &name, &ext); - checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, NULL, NULL, &name, &ext); - checkEqu("smb.cnf", fullname.str()); + checkEqu("smb.cnf", fullname); fn = "file:/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, NULL, NULL, &name, NULL); - //checkEqu("", protocol.str()); - //checkEqu("/etc/samba/", path.str()); - checkEqu("smb", name.str()); - //checkEqu(".cnf", ext.str()); + //checkEqu("", protocol); + //checkEqu("/etc/samba/", path); + checkEqu("smb", name); + //checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, NULL, NULL, &name, NULL); checkEqu("smb", fullname.str()); @@ -173,28 +173,28 @@ private: fn = "file:/etc/samba/smb.cnf"; ReStringUtils::splitPath(fn, NULL, &path, NULL, &ext); - //checkEqu("", protocol.str()); - checkEqu("/etc/samba/", path.str()); - //checkEqu("smb", name.str()); - checkEqu(".cnf", ext.str()); + //checkEqu("", protocol); + checkEqu("/etc/samba/", path); + //checkEqu("smb", name); + checkEqu(".cnf", ext); ReStringUtils::joinPath(fullname, NULL, &path, NULL, &ext); - checkEqu("/etc/samba/.cnf", fullname.str()); + checkEqu("/etc/samba/.cnf", fullname); ReStringUtils::joinPath(fullname, "http:", "//any.de/", "name", ".ext"); - checkEqu("http://any.de/name.ext", fullname.str()); + checkEqu("http://any.de/name.ext", fullname); ReStringUtils::joinPath(fullname, NULL, "/any.de/", "name", ".ext"); - checkEqu("/any.de/name.ext", fullname.str()); + checkEqu("/any.de/name.ext", fullname); ReStringUtils::joinPath(fullname, NULL, NULL, "name", ".ext"); - checkEqu("name.ext", fullname.str()); + checkEqu("name.ext", fullname); ReStringUtils::joinPath(fullname, NULL, NULL, "name", NULL); - checkEqu("name", fullname.str()); + checkEqu("name", fullname); ReStringUtils::joinPath(fullname, "file:", "/", NULL, NULL); - checkEqu("file:/", fullname.str()); + checkEqu("file:/", fullname); } }; extern void testReStringUtils(void); diff --git a/cunit/cuReconfig.cpp b/cunit/cuReconfig.cpp index 798e884..5b54e21 100644 --- a/cunit/cuReconfig.cpp +++ b/cunit/cuReconfig.cpp @@ -37,9 +37,9 @@ private: ReByteBuffer buffer; config.getString("string", buffer, "x"); - checkEqu("abc", buffer.str()); + checkEqu("abc", buffer); config.getString("string1", buffer, "x"); - checkEqu("x", buffer.str()); + checkEqu("x", buffer); } }; extern void testReConfigFile(void); diff --git a/cunit/testall.cpp b/cunit/testall.cpp index 3ef40e8..b2c941e 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -12,6 +12,8 @@ #include "net/renet.hpp" #endif +static bool s_testAll = true; + void testBase(){ extern void testReSeqArray(void); testReSeqArray(); @@ -59,10 +61,12 @@ void testOs(){ void testReDirTools(); testReDirTools(); - void testReTraverser(); - testReTraverser(); - void testReDirTools(); - //testReDirTools(); + if (s_testAll){ + void testReTraverser(); + testReTraverser(); + void testReDirTools(); + testReDirTools(); + } } void testMath(){ extern void testReMD5(); @@ -73,23 +77,16 @@ void testMath(){ void testAll(){ try { - testOs(); - testBase(); - testString(); - testMath(); - testOs(); + //testOs(); + if (s_testAll){ + testBase(); + testString(); + testMath(); + testOs(); + } } catch (ReException e){ fprintf(stderr, "testBase.cpp: unexpected exception: %s\n", e.getMessage()); } } -void testCurrent(){ - try - { - void testReByteBuffer(); - testReByteBuffer(); - } catch (ReException e){ - fprintf(stderr, "testBase.cpp: unexpected exception: %s\n", e.getMessage()); - } -} diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index c0a33e8..edaca86 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -643,7 +643,8 @@ ReTool::ReTool(const char* usage[], const char* example[], m_addCurrentDirIfNoArguments(addCurrentDirIfNoArguments), m_traverser(NULL, this), m_filter(), - m_start(time(NULL)) + m_start(time(NULL)), + m_logger(ReLogger::globalLogger()) { } diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index 4bbac2e..913f1a3 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -124,6 +124,8 @@ protected: ReDirEntryFilter_t m_filter; int64_t m_start; struct stat m_statInfo; + ReLogger* m_logger; + }; class ReDirBatch : public ReTool { diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 6c96df6..1e68133 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -9,7 +9,11 @@ #include "base/rebase.hpp" #include "os/reos.hpp" - +#if defined __WIN32__ +#include "accctrl.h" +#include "aclapi.h" +#pragma comment(lib, "advapi32.lib") +#endif /** * Constructor. */ @@ -339,6 +343,71 @@ const char* ReDirStatus_t::rightsAsString(ReByteBuffer& buffer, bool numerical) buffer.appendChar(' '); } #elif defined __WIN32__ + const char* name = fullName(); + DWORD access = isDirectory() ? 0 : GENERIC_READ; + DWORD shareFlag = isDirectory() ? 0 : FILE_SHARE_READ; + DWORD flag = isDirectory() ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL; + HANDLE handle; + if (! isDirectory()){ + handle = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_EXISTING, flag, NULL); + } else { + LUID luidPrivilege; + DWORD dwErrorCode; + HANDLE hAccessToken; + if (! OpenProcessToken (GetCurrentProcess(), + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hAccessToken)){ + printf("OpenProcessToken(): %d\n", GetLastError()); + } + + if (! LookupPrivilegeValue (NULL, SE_BACKUP_NAME, &luidPrivilege)) { + printf("LookupPrivilegeValue(): %d\n", GetLastError()); + } else { + TOKEN_PRIVILEGES tpPrivileges; + tpPrivileges.PrivilegeCount = 1; + tpPrivileges.Privileges[0].Luid = luidPrivilege; + tpPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + if (AdjustTokenPrivileges (hAccessToken, FALSE, &tpPrivileges, + 0, NULL, NULL) != 0){ + printf("AdjustTokenPrivileges(): %d\n", GetLastError()); + } + } + handle = CreateFile(name, 0, 0, NULL, OPEN_EXISTING, flag, NULL); + } + if (handle == INVALID_HANDLE_VALUE){ + int error = GetLastError(); + printf("CreateFile(%s): %d\n", name, error); + } else { + PSID pSidOwner = NULL; + PSECURITY_DESCRIPTOR pSD = NULL; + DWORD dwRtnCode = GetSecurityInfo(handle, SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION, &pSidOwner, NULL, NULL, NULL, &pSD); + if (dwRtnCode != ERROR_SUCCESS) { + int error = GetLastError(); + printf("GetSecurityInfo(%s): %d\n", name, error); + } else { + if (numerical){ + buffer.appendInt((int) pSidOwner, " %08x"); + } else { + char accountName[128]; + char domainName[128]; + DWORD dwAcctName = sizeof accountName; + DWORD dwDomainName = sizeof domainName; + SID_NAME_USE eUse = SidTypeUnknown; + BOOL bRtnBool = LookupAccountSid(NULL, // local computer + pSidOwner, accountName, &dwAcctName, domainName, + &dwDomainName, &eUse); + if (! bRtnBool){ + int error = GetLastError(); + printf("LookupAccountSid(%s): %d\n", name, error); + } else { + if (domainName[0] != '\0') + buffer.append(domainName, -1).appendChar('/'); + } + buffer.append(accountName, -1); + } + } + } #endif return buffer.str(); } -- 2.39.5