From 3d4beaf41f3ebc4bb0ee71135486b0afbcc45644 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 14 Dec 2008 15:18:33 -0500 Subject: [PATCH] - Enhanced split algorithm to be non-destrutive, and therefore faster - Added autoconf testing to detect Solaris - Added Solaris-specific flock implementation --- configure.ac | 12 ++++++++++-- src/TDB.cpp | 12 ------------ src/task.h | 9 +++++++++ src/text.cpp | 34 ++++++++++++++++++++-------------- src/util.cpp | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 28 deletions(-) diff --git a/configure.ac b/configure.ac index 364500f72..a528345e1 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,6 @@ AC_PREREQ(2.61) AC_INIT(task, 1.5.0, bugs@beckingham.net) - CFLAGS="${CFLAGS=}" CXXFLAGS="${CXXFLAGS=}" # this macro is used to get the arguments supplied @@ -25,6 +24,15 @@ CXXFLAGS="$CFLAGS -O3" AC_MSG_RESULT(no) fi +# Check for OS. +OS=`uname|sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'` +if test "$OS" = "sunos"; then + AC_MSG_NOTICE([OS Solaris detected]) + AC_DEFINE([SOLARIS], [], [Compiling on Solaris]) +else + AC_MSG_NOTICE([OS Non-Solaris detected]) + AC_DEFINE([LINUX], [], [Compiling on Non-Solaris]) +fi AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR([src/task.cpp]) @@ -58,7 +66,7 @@ AC_STRUCT_TM AC_FUNC_MKTIME AC_FUNC_SELECT_ARGTYPES AC_CHECK_FUNCS([select]) -AC_CHECK_FUNC(flock, [AC_DEFINE([HAVE_FLOCK], [1], [Found flock])]) +#AC_CHECK_FUNC(flock, [AC_DEFINE([HAVE_FLOCK], [1], [Found flock])]) AC_CHECK_FUNC(uuid_unparse_lower, [AC_DEFINE([HAVE_UUID], [1], [Found uuid_unparse_lower])]) AC_CHECK_FUNC(random, [AC_DEFINE([HAVE_RANDOM], [1], [Found random])]) AC_CHECK_FUNC(srandom, [AC_DEFINE([HAVE_SRANDOM], [1], [Found srandom])]) diff --git a/src/TDB.cpp b/src/TDB.cpp index e531f10a1..ba7b4eb2b 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -289,11 +289,7 @@ bool TDB::modifyT (const T& t) //////////////////////////////////////////////////////////////////////////////// bool TDB::lock (FILE* file) const { -#ifdef HAVE_FLOCK return flock (fileno (file), LOCK_EX) ? false : true; -#else - return true; -#endif } //////////////////////////////////////////////////////////////////////////////// @@ -303,11 +299,9 @@ bool TDB::overwritePending (std::vector & all) FILE* out; if ((out = fopen (mPendingFile.c_str (), "w"))) { -#ifdef HAVE_FLOCK int retry = 0; while (flock (fileno (out), LOCK_EX) && ++retry <= 3) delay (0.25); -#endif std::vector ::iterator it; for (it = all.begin (); it != all.end (); ++it) @@ -328,11 +322,9 @@ bool TDB::writePending (const T& t) FILE* out; if ((out = fopen (mPendingFile.c_str (), "a"))) { -#ifdef HAVE_FLOCK int retry = 0; while (flock (fileno (out), LOCK_EX) && ++retry <= 3) delay (0.25); -#endif fputs (t.compose ().c_str (), out); @@ -351,11 +343,9 @@ bool TDB::writeCompleted (const T& t) FILE* out; if ((out = fopen (mCompletedFile.c_str (), "a"))) { -#ifdef HAVE_FLOCK int retry = 0; while (flock (fileno (out), LOCK_EX) && ++retry <= 3) delay (0.25); -#endif fputs (t.compose ().c_str (), out); @@ -380,11 +370,9 @@ bool TDB::readLockedFile ( FILE* in; if ((in = fopen (file.c_str (), "r"))) { -#ifdef HAVE_FLOCK int retry = 0; while (flock (fileno (in), LOCK_EX) && ++retry <= 3) delay (0.25); -#endif char line[T_LINE_MAX]; while (fgets (line, T_LINE_MAX, in)) diff --git a/src/task.h b/src/task.h index 79c79ddf9..c2f609d6d 100644 --- a/src/task.h +++ b/src/task.h @@ -130,6 +130,15 @@ const char* optionalBlankLine (Config&); int convertDuration (std::string&); std::string expandPath (const std::string&); +#ifdef SOLARIS + #define LOCK_SH 1 + #define LOCK_EX 2 + #define LOCK_NB 4 + #define LOCK_UN 8 + + int flock (int, int); +#endif + // rules.cpp void initializeColorRules (Config&); void autoColorize (T&, Text::color&, Text::color&); diff --git a/src/text.cpp b/src/text.cpp index 066d84f86..90fed8aa2 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -49,33 +49,39 @@ void wrapText ( } //////////////////////////////////////////////////////////////////////////////// -void split (std::vector& results, const std::string& input, const char delimiter) +void split ( + std::vector& results, + const std::string& input, + const char delimiter) { - std::string temp = input; + std::string::size_type start = 0; std::string::size_type i; - while ((i = temp.find (delimiter)) != std::string::npos) + while ((i = input.find (delimiter, start)) != std::string::npos) { - std::string token = temp.substr (0, i); - results.push_back (token); - temp.erase (0, i + 1); + results.push_back (input.substr (start, i - start)); + start = i + 1; } - if (temp.length ()) results.push_back (temp); + results.push_back (input.substr (start, std::string::npos)); } //////////////////////////////////////////////////////////////////////////////// -void split (std::vector& results, const std::string& input, const std::string& delimiter) +void split ( + std::vector& results, + const std::string& input, + const std::string& delimiter) { - std::string temp = input; + std::string::size_type length = delimiter.length (); + + std::string::size_type start = 0; std::string::size_type i; - while ((i = temp.find (delimiter)) != std::string::npos) + while ((i = input.find (delimiter, start)) != std::string::npos) { - std::string token = temp.substr (0, i); - results.push_back (token); - temp.erase (0, i + delimiter.length ()); + results.push_back (input.substr (start, i - start)); + start = i + length; } - if (temp.length ()) results.push_back (temp); + results.push_back (input.substr (start, std::string::npos)); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/util.cpp b/src/util.cpp index 8717619da..fc52bb7bf 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -331,3 +331,37 @@ std::string expandPath (const std::string& in) } //////////////////////////////////////////////////////////////////////////////// +// On Solaris no flock function exists. +#ifdef SOLARIS +int flock (int fd, int operation) +{ + struct flock flock; + + switch (operation & ~LOCK_NB) + { + case LOCK_SH: + flock.l_type = F_RDLCK; + break; + + case LOCK_EX: + flock.l_type = F_WRLCK; + break; + + case LOCK_UN: + flock.l_type = F_UNLCK; + break; + + default: + errno = EINVAL; + return -1; + } + + flock.l_whence = 0; + flock.l_start = 0; + flock.l_len = 0; + + return fcntl (fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock); +} +#endif + +////////////////////////////////////////////////////////////////////////////////