- Enhanced split algorithm to be non-destrutive, and therefore faster

- Added autoconf testing to detect Solaris
- Added Solaris-specific flock implementation
This commit is contained in:
Paul Beckingham
2008-12-14 15:18:33 -05:00
parent 50ccb67185
commit 3d4beaf41f
5 changed files with 73 additions and 28 deletions

View File

@@ -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 <T>& 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 <T>::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))

View File

@@ -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&);

View File

@@ -49,33 +49,39 @@ void wrapText (
}
////////////////////////////////////////////////////////////////////////////////
void split (std::vector<std::string>& results, const std::string& input, const char delimiter)
void split (
std::vector<std::string>& 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<std::string>& results, const std::string& input, const std::string& delimiter)
void split (
std::vector<std::string>& 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));
}
////////////////////////////////////////////////////////////////////////////////

View File

@@ -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
////////////////////////////////////////////////////////////////////////////////