From c34781afb65dcc92ba32352eaf668c814a2527a6 Mon Sep 17 00:00:00 2001 From: hama Date: Tue, 6 Jan 2015 01:17:04 +0100 Subject: [PATCH] linux corrections --- base/ReByteBuffer.cpp | 3 ++- base/ReByteBuffer.hpp | 7 ++++++ base/ReCString.cpp | 2 +- base/ReCString.hpp | 2 +- base/ReProgramArgs.cpp | 2 +- base/ReProgramArgs.hpp | 2 +- cunit/cuReProgramArgs.cpp | 6 ++--- cunit/cuReTraverser.cpp | 2 +- os/ReDirTools.cpp | 48 ++++++++++++++++++++------------------- os/ReDirTools.hpp | 16 ++++++------- os/ReTraverser.cpp | 15 +++++++----- os/ReTraverser.hpp | 1 + 12 files changed, 60 insertions(+), 46 deletions(-) diff --git a/base/ReByteBuffer.cpp b/base/ReByteBuffer.cpp index 0b2e274..6e6a393 100644 --- a/base/ReByteBuffer.cpp +++ b/base/ReByteBuffer.cpp @@ -294,7 +294,8 @@ int ReByteBuffer::indexOf(const Byte* toFind, size_t toFindLength, int start, if (start >= 0 && start <= int(m_length - toFindLength) && end >= (int) toFindLength && end <= (int) m_length){ while(rc < 0 && start <= int(end - toFindLength)){ - if (ignoreCase ? memicmp(toFind, m_buffer + start, toFindLength) == 0 + if (ignoreCase ? memicmp((void*) toFind, (void*) (m_buffer + start), + toFindLength) == 0 : memcmp(toFind, m_buffer + start, toFindLength) == 0) rc = start; else diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index f863a6d..ecd4f07 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -148,6 +148,13 @@ public: inline ReByteBuffer& set(const Byte* source, size_t length){ return setLength(0).append(source, length); } + /** @brief Sets the content of the buffer by copying a byte sequence. + * @param source the source to copy. + * @return *this (for chaining). + */ + inline ReByteBuffer& set(const ReByteBuffer& source){ + return setLength(0).append(source); + } ReByteBuffer& setLength(size_t length); ReByteBuffer& setLengthAndFill(size_t length, Byte filler = 0); diff --git a/base/ReCString.cpp b/base/ReCString.cpp index 99f6475..9b5bee1 100644 --- a/base/ReCString.cpp +++ b/base/ReCString.cpp @@ -53,7 +53,7 @@ void replaceSubstring(char* start, size_t bufferSize, size_t lengthReplaced, * < 0: region1 is alphabetically less than region2
* > 0: region1 is alphabetically greater than region2 */ -int memicmp(void* region1, void* region2, int size) +int memicmp(const void* region1, const void* region2, int size) { unsigned char* ptr1 = (unsigned char*) region1; unsigned char* ptr2 = (unsigned char*) region2; diff --git a/base/ReCString.hpp b/base/ReCString.hpp index 03d31a9..fdf7d7e 100644 --- a/base/ReCString.hpp +++ b/base/ReCString.hpp @@ -11,6 +11,6 @@ void replaceSubstring(char* start, size_t bufferSize, size_t lengthReplaced, const char* newString); #if defined __linux__ -int memicmp(void* region1, void* region2, int size); +int memicmp(const void* region1, const void* region2, int size); #endif #endif /* RESTRING_H_ */ diff --git a/base/ReProgramArgs.cpp b/base/ReProgramArgs.cpp index 038c838..d40573b 100644 --- a/base/ReProgramArgs.cpp +++ b/base/ReProgramArgs.cpp @@ -513,7 +513,7 @@ void ReProgramArgs::analyseLong(const char* opt){ * * @throws ReException */ -void ReProgramArgs::init(int argc, char* argv[]){ +void ReProgramArgs::init(int argc, const char* argv[]){ m_program = argv[0]; argv++; argc--; diff --git a/base/ReProgramArgs.hpp b/base/ReProgramArgs.hpp index 97e1018..d7c22b7 100644 --- a/base/ReProgramArgs.hpp +++ b/base/ReProgramArgs.hpp @@ -68,7 +68,7 @@ public: const char* getArg(size_t index) const; const char* getProgramName() const; - void init(int argc, char* argv[]); + void init(int argc, const char* argv[]); void setLastError(const char* message); void help(const char* message, bool issueLastError, ReStringList& lines); diff --git a/cunit/cuReProgramArgs.cpp b/cunit/cuReProgramArgs.cpp index eb05e1f..4681084 100644 --- a/cunit/cuReProgramArgs.cpp +++ b/cunit/cuReProgramArgs.cpp @@ -19,7 +19,7 @@ private: ReProgramArgs args("test","example"); args.addBool("boolarg", "This is a boolean arg", 'b', "boolval", false); try { - char* argv[] = { "test", "-v" }; + const char* argv[] = { "test", "-v" }; args.init(2, argv); checkF(true); } catch (ReOptionException& exc) { @@ -50,7 +50,7 @@ private: const char* vector[] = { "testprog", "-bB+i123", "-S", "-x-", "-Y+", "-s", "2nd string", "arg1", "arg2" }; - args.init(sizeof vector / sizeof vector[0], (char**) vector); + args.init(sizeof vector / sizeof vector[0], vector); checkEqu("testprog", args.getProgramName()); checkT(args.getBool("boolarg")); @@ -111,7 +111,7 @@ private: "--string=x y", "--estring=", "--estring2=not empty", "arg1", "arg2" }; - args.init(sizeof vector / sizeof vector[0], (char**) vector); + args.init(sizeof vector / sizeof vector[0], vector); checkEqu("testprog", args.getProgramName()); checkT(args.getBool("boolarg")); diff --git a/cunit/cuReTraverser.cpp b/cunit/cuReTraverser.cpp index 13a9185..81e76da 100644 --- a/cunit/cuReTraverser.cpp +++ b/cunit/cuReTraverser.cpp @@ -110,7 +110,7 @@ private: void checkSetFilterFromProgramArgs(){ ReDirOptions opts(s_empty, s_empty); opts.addStandardFilterOptions(); - char* argv[] = { "x", "-y1970.01.02", "-o1970.01.03", + const char* argv[] = { "x", "-y1970.01.02", "-o1970.01.03", "-D5", "-d1", "-z1k", "-Z2M", "-p;*;-*~" }; ReDirEntryFilter_t filter; diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 3c746da..7137fb3 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -10,8 +10,6 @@ const char* ReDirTools::m_version = "2015.01.05"; -static const char* s_empty[] = { NULL }; - static const char* s_helpSummary[] = { "dirtool or dt ", " Useful commands around directory trees.", @@ -287,7 +285,7 @@ time_t ReDirOptions::checkDate(const char* value){ time_t ReDirOptions::checkSize(const char* value){ int64_t rc = 0; char unit = 'b'; - switch (sscanf(value, "%lld%c", &rc, &unit)){ + switch (sscanf(value, "%lld%c", (long long int*) &rc, &unit)){ case 1: case 2: switch(unit){ @@ -334,7 +332,8 @@ time_t ReDirOptions::checkSize(const char* value){ * @throws ReOptionExecption */ const char* ReDirOptions::checkPatternList(const char* value){ - if (iscsym(*value) || *value == '*' || *value == '.' || *value == '-') + if (isalnum(*value) || *value == '_' || *value == '*' + || *value == '.' || *value == '-') throw ReOptionException(&m_programArgs, i18n("invalid separator (first character): $1 use ';' instead"), value); @@ -552,7 +551,7 @@ ReDirStatistic::~ReDirStatistic(){ * @param arc count of arguments in argv * @param argv the program arguments. */ -void ReDirStatistic::run(int argc, char* argv[]){ +void ReDirStatistic::run(int argc, const char* argv[]){ time_t start = time(NULL); try { m_programArgs.init(argc, argv); @@ -631,6 +630,10 @@ const ReStringList& ReDirStatistic::calculate(const char* base, int level, // set up a new stack entry: if (currentDepth != topOfStack + 1) assert(currentDepth == topOfStack + 1); + //@todo + if (strstr("dir2", entry->node()) != NULL || strstr("dir2", entry->m_path.str())) + ReByteBuffer breaker; + topOfStack++; if (dataStack[topOfStack] == NULL) dataStack[topOfStack] = new ReDirStatisticData(); @@ -763,7 +766,7 @@ ReDirList::ReDirList() : * @param argc the number of arguments * @param argav the argument vector */ -void ReDirList::list(int argc, char* argv[]){ +void ReDirList::list(int argc, const char* argv[]){ ReDirEntryFilter_t filter; try { time_t start = time(NULL); @@ -900,7 +903,7 @@ static void replaceMakros(const char* arguments, ReDirStatus_t* entry, const cha * @param argc the number of arguments * @param argav the argument vector */ -void ReDirBatch::createBatch(int argc, char* argv[]){ +void ReDirBatch::createBatch(int argc, const char* argv[]){ ReDirEntryFilter_t filter; try { time_t start = time(NULL); @@ -1000,7 +1003,7 @@ ReDirSync::ReDirSync() : * @param argc the number of arguments * @param argav the argument vector */ -void ReDirSync::synchronize(int argc, char* argv[]){ +void ReDirSync::synchronize(int argc, const char* argv[]){ ReDirEntryFilter_t filter; const char* sep = ReTraverser::m_separatorStr; struct stat info; @@ -1020,6 +1023,7 @@ void ReDirSync::synchronize(int argc, char* argv[]){ size_t lengthTargetBase = target.length(); bool verbose = ! m_programArgs.getBool("quiet"); bool chatterMode = true; + bool copyExistingOnly = false; setFilterFromProgramArgs(filter); int64_t sumSizes = 0; int files = 0; @@ -1057,14 +1061,16 @@ void ReDirSync::synchronize(int argc, char* argv[]){ makeDirWithParents(target, ixTargetRelative); targetFile.set(target).append(entry->node(), -1); bool exists = stat(targetFile.str(), &info) == 0; - if (! exists && ! copyExistingOnly + if ( (! exists && ! copyExistingOnly) || entry->filetimeToTime(entry->modified()) - info.st_mtime > maxFileTimeDiff) { if (chatterMode) - printf("%s%s same time\n", entry->m_path.str(), entry->node); + printf("%s%s same time\n", entry->m_path.str(), entry->node()); continue; } else { + } + if (entry->isDirectory()) dirs++; else{ @@ -1077,13 +1083,8 @@ void ReDirSync::synchronize(int argc, char* argv[]){ } if (verbose){ int duration = int(time(NULL) - start); -#if defined __linux__ - const char* prefix = "#"; -#elif defined __WIN32__ - const char* prefix = "rem"; -#endif - printf ("%s %d dir(s) and %d file(s) with %.6f MByte in %02d:%02d sec\n", - prefix, dirs, files, sumSizes / 1E6, duration / 60, duration % 60); + printf ("=== %d dir(s) and %d file(s) with %.6f MByte in %02d:%02d sec\n", + dirs, files, sumSizes / 1E6, duration / 60, duration % 60); } } catch(ReOptionException& exc){ help(exc.getMessage()); @@ -1096,7 +1097,7 @@ void ReDirSync::synchronize(int argc, char* argv[]){ * @param argc the number of arguments * @param argav the argument vector */ -void ReDirTools::list(int argc, char* argv[]){ +void ReDirTools::list(int argc, const char* argv[]){ ReDirList lister; lister.list(argc, argv); } @@ -1107,7 +1108,7 @@ void ReDirTools::list(int argc, char* argv[]){ * @param argc the number of arguments * @param argav the argument vector */ -void ReDirTools::batch(int argc, char* argv[]){ +void ReDirTools::batch(int argc, const char* argv[]){ ReDirBatch batch; batch.createBatch(argc, argv); } @@ -1130,7 +1131,7 @@ static bool isArg(const char* full, const char* part){ * @param argc the number of arguments * @param argav the argument vector */ -void ReDirTools::help(int argc, char* argv[]){ +void ReDirTools::help(int argc, const char* argv[]){ if (argc <= 1) printField(s_helpSummary); else { @@ -1162,7 +1163,7 @@ void ReDirTools::help(int argc, char* argv[]){ * @param argc the number of arguments * @param argav the argument vector */ -void ReDirTools::statistic(int argc, char* argv[]){ +void ReDirTools::statistic(int argc, const char* argv[]){ ReDirStatistic statistic; statistic.run(argc, argv); } @@ -1171,10 +1172,11 @@ void ReDirTools::statistic(int argc, char* argv[]){ * Gets the arguments for any command and execute this. * * @param argc the number of arguments - * @param argav the argument vector + * @param orgArgv the argument vector */ -int ReDirTools::main(int argc, char* argv[]){ +int ReDirTools::main(int argc, char* orgArgv[]){ ReDirTools tools; + const char** argv = (const char**) orgArgv; if (argc < 2) tools.help(0, argv); argc--; diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index 7914afd..5929171 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -39,7 +39,7 @@ class ReDirBatch : public ReDirOptions { public: ReDirBatch(); public: - void createBatch(int argc, char* argv[]); + void createBatch(int argc, const char* argv[]); virtual bool printOneFile(ReDirStatus_t* entry) { return false; } }; @@ -48,14 +48,14 @@ class ReDirSync : public ReDirOptions { public: ReDirSync(); public: - void synchronize(int argc, char* argv[]); + void synchronize(int argc, const char* argv[]); }; class ReDirList : public ReDirOptions { public: ReDirList(); public: - void list(int argc, char* argv[]); + void list(int argc, const char* argv[]); virtual bool printOneFile(ReDirStatus_t* entry) { return false; } }; @@ -93,7 +93,7 @@ public: const ReStringList& calculate(const char* base, int depth, void (*format)(const ReDirStatisticData& data, ReDirStatistic& parent, ReByteBuffer& line) = formatLikeDu); - void run(int argc, char* argv[]); + void run(int argc, const char* argv[]); void setTraceInterval(int interval){ m_traceInterval = interval; } @@ -108,11 +108,11 @@ class ReDirTools { public: virtual void usage(const char* msg, const char* msg2 = NULL); void dirListUsage(); - void batch(int argc, char* argv[]); - void help(int argc, char* argv[]); - void list(int argc, char* argv[]); + void batch(int argc, const char* argv[]); + void help(int argc, const char* argv[]); + void list(int argc, const char* argv[]); void statisticUsage(); - void statistic(int argc, char* argv[]); + void statistic(int argc, const char* argv[]); public: static int main(int argc, char* argv[]); public: diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 590585c..e690258 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -20,10 +20,13 @@ const char* const ReTraverser::m_separatorStr = "\\"; * Constructor. */ ReDirStatus_t::ReDirStatus_t() : + m_path(), + m_passNo(0), #ifdef __linux__ m_handle(NULL), - m_data(NULL) + m_data(NULL), //m_status; + m_fullName() #elif defined WIN32 m_handle(INVALID_HANDLE_VALUE) //m_data; @@ -344,9 +347,11 @@ bool ReDirEntryFilter_t::match(ReDirStatus_t& entry){ * @return the status of the current file */ struct stat* ReDirStatus_t::getStatus() { - if (m_status.st_ino == 0) - if (stat(m_data->d_name, &m_status) != 0) + if (m_status.st_ino == 0){ + m_fullName.set(m_path).append(m_data->d_name, -1); + if (stat(m_fullName.str(), &m_status) != 0) memset((void*) &m_status, 0, sizeof m_status); + } return &m_status; } #endif @@ -357,9 +362,9 @@ struct stat* ReDirStatus_t::getStatus() { * @param base the base directory. The traversal starts at this point */ ReTraverser::ReTraverser(const char* base) : - m_level(-1), m_minLevel(0), m_maxLevel(512), + m_level(-1), m_base(base), // m_dirs m_passNoForDirSearch(2) @@ -433,8 +438,6 @@ ReDirStatus_t* ReTraverser::rawNextFile(int& level) if (current->m_passNo == 1){ // we start the second pass: alreadyRead = initEntry(current->m_path, NULL, -1); - if (strcmp(current->m_path.str(), "E:\\user\\winfried\\") == 0) - current->m_passNo = 2; current->m_passNo = 2; again = true; } else { diff --git a/os/ReTraverser.hpp b/os/ReTraverser.hpp index 272ec2d..be66239 100644 --- a/os/ReTraverser.hpp +++ b/os/ReTraverser.hpp @@ -64,6 +64,7 @@ public: DIR* m_handle; struct dirent* m_data; struct stat m_status; + ReByteBuffer m_fullName; public: struct stat* getStatus(); #elif defined WIN32 -- 2.39.5