diff options
author | Bernard Normier <bernard@zeroc.com> | 2007-02-01 17:09:49 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2007-02-01 17:09:49 +0000 |
commit | abada90e3f84dc703b8ddc9efcbed8a946fadead (patch) | |
tree | 2c6f9dccd510ea97cb927a7bd635422efaae547a /cpp/src/IceUtil | |
parent | removing trace message (diff) | |
download | ice-abada90e3f84dc703b8ddc9efcbed8a946fadead.tar.bz2 ice-abada90e3f84dc703b8ddc9efcbed8a946fadead.tar.xz ice-abada90e3f84dc703b8ddc9efcbed8a946fadead.zip |
Expanded tabs into spaces
Diffstat (limited to 'cpp/src/IceUtil')
-rw-r--r-- | cpp/src/IceUtil/Cond.cpp | 96 | ||||
-rwxr-xr-x | cpp/src/IceUtil/ConvertUTF.cpp | 478 | ||||
-rwxr-xr-x | cpp/src/IceUtil/ConvertUTF.h | 34 | ||||
-rw-r--r-- | cpp/src/IceUtil/CountDownLatch.cpp | 70 | ||||
-rw-r--r-- | cpp/src/IceUtil/CtrlCHandler.cpp | 120 | ||||
-rw-r--r-- | cpp/src/IceUtil/Exception.cpp | 4 | ||||
-rw-r--r-- | cpp/src/IceUtil/InputUtil.cpp | 130 | ||||
-rw-r--r-- | cpp/src/IceUtil/MD5I.cpp | 128 | ||||
-rw-r--r-- | cpp/src/IceUtil/MD5I.h | 16 | ||||
-rwxr-xr-x | cpp/src/IceUtil/Options.cpp | 1210 | ||||
-rw-r--r-- | cpp/src/IceUtil/OutputUtil.cpp | 146 | ||||
-rw-r--r-- | cpp/src/IceUtil/RWRecMutex.cpp | 318 | ||||
-rw-r--r-- | cpp/src/IceUtil/Random.cpp | 126 | ||||
-rw-r--r-- | cpp/src/IceUtil/RecMutex.cpp | 116 | ||||
-rw-r--r-- | cpp/src/IceUtil/StaticMutex.cpp | 40 | ||||
-rw-r--r-- | cpp/src/IceUtil/StringUtil.cpp | 358 | ||||
-rw-r--r-- | cpp/src/IceUtil/Thread.cpp | 132 | ||||
-rw-r--r-- | cpp/src/IceUtil/ThreadException.cpp | 46 | ||||
-rw-r--r-- | cpp/src/IceUtil/Time.cpp | 2 | ||||
-rw-r--r-- | cpp/src/IceUtil/UUID.cpp | 26 | ||||
-rw-r--r-- | cpp/src/IceUtil/Unicode.cpp | 116 |
21 files changed, 1856 insertions, 1856 deletions
diff --git a/cpp/src/IceUtil/Cond.cpp b/cpp/src/IceUtil/Cond.cpp index ab59e63958e..0f895980c9a 100644 --- a/cpp/src/IceUtil/Cond.cpp +++ b/cpp/src/IceUtil/Cond.cpp @@ -20,7 +20,7 @@ IceUtil::Semaphore::Semaphore(long initial) _sem = CreateSemaphore(0, initial, 0x7fffffff, 0); if(_sem == INVALID_HANDLE_VALUE) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -35,7 +35,7 @@ IceUtil::Semaphore::wait() const int rc = WaitForSingleObject(_sem, INFINITE); if(rc != WAIT_OBJECT_0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -45,7 +45,7 @@ IceUtil::Semaphore::timedWait(const Time& timeout) const int rc = WaitForSingleObject(_sem, static_cast<long>(timeout.toMilliSeconds())); if(rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } return rc != WAIT_TIMEOUT; } @@ -56,7 +56,7 @@ IceUtil::Semaphore::post(int count) const int rc = ReleaseSemaphore(_sem, count, 0); if(rc == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -95,26 +95,26 @@ IceUtil::Cond::wake(bool broadcast) if(_unblocked != 0) { - _blocked -= _unblocked; - _unblocked = 0; + _blocked -= _unblocked; + _unblocked = 0; } if(_blocked > 0) { - // - // Unblock some number of waiters. - // - _toUnblock = (broadcast) ? _blocked : 1; - _internal.unlock(); - _queue.post(); + // + // Unblock some number of waiters. + // + _toUnblock = (broadcast) ? _blocked : 1; + _internal.unlock(); + _queue.post(); } else { - // - // Otherwise no blocked waiters, release gate & mutex. - // - _gate.post(); - _internal.unlock(); + // + // Otherwise no blocked waiters, release gate & mutex. + // + _gate.post(); + _internal.unlock(); } } @@ -134,26 +134,26 @@ IceUtil::Cond::postWait(bool timedOut) const if(_toUnblock != 0) { - bool last = --_toUnblock == 0; - _internal.unlock(); - - if(timedOut) - { - _queue.wait(); - } - - if(last) - { - _gate.post(); - } - else - { - _queue.post(); - } + bool last = --_toUnblock == 0; + _internal.unlock(); + + if(timedOut) + { + _queue.wait(); + } + + if(last) + { + _gate.post(); + } + else + { + _queue.post(); + } } else { - _internal.unlock(); + _internal.unlock(); } } @@ -162,13 +162,13 @@ IceUtil::Cond::dowait() const { try { - _queue.wait(); - postWait(false); + _queue.wait(); + postWait(false); } catch(...) { - postWait(false); - throw; + postWait(false); + throw; } } @@ -177,14 +177,14 @@ IceUtil::Cond::timedDowait(const Time& timeout) const { try { - bool rc = _queue.timedWait(timeout); - postWait(!rc); - return rc; + bool rc = _queue.timedWait(timeout); + postWait(!rc); + return rc; } catch(...) { - postWait(false); - throw; + postWait(false); + throw; } } @@ -199,19 +199,19 @@ IceUtil::Cond::Cond() rc = pthread_condattr_init(&attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_cond_init(&_cond, &attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_condattr_destroy(&attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } @@ -228,7 +228,7 @@ IceUtil::Cond::signal() int rc = pthread_cond_signal(&_cond); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } @@ -238,7 +238,7 @@ IceUtil::Cond::broadcast() int rc = pthread_cond_broadcast(&_cond); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } diff --git a/cpp/src/IceUtil/ConvertUTF.cpp b/cpp/src/IceUtil/ConvertUTF.cpp index 34a827163f9..f44aea0f0b5 100755 --- a/cpp/src/IceUtil/ConvertUTF.cpp +++ b/cpp/src/IceUtil/ConvertUTF.cpp @@ -36,10 +36,10 @@ Author: Mark E. Davis, 1994. Rev History: Rick McGowan, fixes & updates May 2001. Sept 2001: fixed const & error conditions per - mods suggested by S. Parent & A. Lillich. + mods suggested by S. Parent & A. Lillich. June 2002: Tim Dodd added detection and handling of incomplete - source sequences, enhanced error detection, added casts - to eliminate compiler warnings. + source sequences, enhanced error detection, added casts + to eliminate compiler warnings. July 2003: slight mods to back out aggressive FFFE detection. Jan 2004: updated switches in from-UTF8 conversions. Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions. @@ -67,8 +67,8 @@ static const UTF32 halfMask = 0x3FFUL; #define UNI_SUR_HIGH_END (UTF32)0xDBFF #define UNI_SUR_LOW_START (UTF32)0xDC00 #define UNI_SUR_LOW_END (UTF32)0xDFFF -// #define false 0 -// #define true 1 +// #define false 0 +// #define true 1 /* --------------------------------------------------------------------- */ @@ -97,7 +97,7 @@ static const char trailingBytesForUTF8[256] = { * in a UTF-8 sequence. */ static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, - 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; + 0x03C82080UL, 0xFA082080UL, 0x82082080UL }; /* * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed @@ -121,67 +121,67 @@ static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC /* --------------------------------------------------------------------- */ ConversionResult ConvertUTF16toUTF8 ( - const UTF16** sourceStart, const UTF16* sourceEnd, - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { + const UTF16** sourceStart, const UTF16* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { ConversionResult result = conversionOK; const UTF16* source = *sourceStart; UTF8* target = *targetStart; while (source < sourceEnd) { - UTF32 ch; - unsigned short bytesToWrite = 0; - const UTF32 byteMask = 0xBF; - const UTF32 byteMark = 0x80; - const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ - ch = *source++; - /* If we have a surrogate pair, convert to UTF32 first. */ - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { - /* If the 16 bits following the high surrogate are in the source buffer... */ - if (source < sourceEnd) { - UTF32 ch2 = *source; - /* If it's a low surrogate, convert to UTF32. */ - if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { - ch = ((ch - UNI_SUR_HIGH_START) << halfShift) - + (ch2 - UNI_SUR_LOW_START) + halfBase; - ++source; - } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ - --source; /* return to the illegal value itself */ - result = sourceIllegal; - break; - } - } else { /* We don't have the 16 bits following the high surrogate. */ - --source; /* return to the high surrogate */ - result = sourceExhausted; - break; - } - } else if (flags == strictConversion) { - /* UTF-16 surrogate values are illegal in UTF-32 */ - if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { - --source; /* return to the illegal value itself */ - result = sourceIllegal; - break; - } - } - /* Figure out how many bytes the result will require */ - if (ch < (UTF32)0x80) { bytesToWrite = 1; - } else if (ch < (UTF32)0x800) { bytesToWrite = 2; - } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; - } else if (ch < (UTF32)0x110000) { bytesToWrite = 4; - } else { bytesToWrite = 3; - ch = UNI_REPLACEMENT_CHAR; - } - - target += bytesToWrite; - if (target > targetEnd) { - source = oldSource; /* Back up source pointer! */ - target -= bytesToWrite; result = targetExhausted; break; - } - switch (bytesToWrite) { /* note: everything falls through. */ - case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]); - } - target += bytesToWrite; + UTF32 ch; + unsigned short bytesToWrite = 0; + const UTF32 byteMask = 0xBF; + const UTF32 byteMark = 0x80; + const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */ + ch = *source++; + /* If we have a surrogate pair, convert to UTF32 first. */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) { + /* If the 16 bits following the high surrogate are in the source buffer... */ + if (source < sourceEnd) { + UTF32 ch2 = *source; + /* If it's a low surrogate, convert to UTF32. */ + if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) { + ch = ((ch - UNI_SUR_HIGH_START) << halfShift) + + (ch2 - UNI_SUR_LOW_START) + halfBase; + ++source; + } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } else { /* We don't have the 16 bits following the high surrogate. */ + --source; /* return to the high surrogate */ + result = sourceExhausted; + break; + } + } else if (flags == strictConversion) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + /* Figure out how many bytes the result will require */ + if (ch < (UTF32)0x80) { bytesToWrite = 1; + } else if (ch < (UTF32)0x800) { bytesToWrite = 2; + } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; + } else if (ch < (UTF32)0x110000) { bytesToWrite = 4; + } else { bytesToWrite = 3; + ch = UNI_REPLACEMENT_CHAR; + } + + target += bytesToWrite; + if (target > targetEnd) { + source = oldSource; /* Back up source pointer! */ + target -= bytesToWrite; result = targetExhausted; break; + } + switch (bytesToWrite) { /* note: everything falls through. */ + case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]); + } + target += bytesToWrite; } *sourceStart = source; *targetStart = target; @@ -206,19 +206,19 @@ static Boolean isLegalUTF8(const UTF8 *source, int length) { const UTF8 *srcptr = source+length; switch (length) { default: return false; - /* Everything else falls through when "true"... */ + /* Everything else falls through when "true"... */ case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false; case 2: if ((a = (*--srcptr)) > 0xBF) return false; - switch (*source) { - /* no fall-through in this inner switch */ - case 0xE0: if (a < 0xA0) return false; break; - case 0xED: if (a > 0x9F) return false; break; - case 0xF0: if (a < 0x90) return false; break; - case 0xF4: if (a > 0x8F) return false; break; - default: if (a < 0x80) return false; - } + switch (*source) { + /* no fall-through in this inner switch */ + case 0xE0: if (a < 0xA0) return false; break; + case 0xED: if (a > 0x9F) return false; break; + case 0xF0: if (a < 0x90) return false; break; + case 0xF4: if (a > 0x8F) return false; break; + default: if (a < 0x80) return false; + } case 1: if (*source >= 0x80 && *source < 0xC2) return false; } @@ -238,89 +238,89 @@ Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) { } while(true) { int length = trailingBytesForUTF8[*source]+1; - // Is buffer big enough to contain character? + // Is buffer big enough to contain character? if (source+length > sourceEnd) { - return false; + return false; } - // Is character legal UTF8? + // Is character legal UTF8? if(!isLegalUTF8(source, length)) { - return false; - } - // Are we at end of buffer? - source += length; - if(source == sourceEnd) { - return true; - } + return false; + } + // Are we at end of buffer? + source += length; + if(source == sourceEnd) { + return true; + } } } /* --------------------------------------------------------------------- */ ConversionResult ConvertUTF8toUTF16 ( - const UTF8** sourceStart, const UTF8* sourceEnd, - UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) { ConversionResult result = conversionOK; const UTF8* source = *sourceStart; UTF16* target = *targetStart; while (source < sourceEnd) { - UTF32 ch = 0; - unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; - if (source + extraBytesToRead >= sourceEnd) { - result = sourceExhausted; break; - } - /* Do this check whether lenient or strict */ - if (! isLegalUTF8(source, extraBytesToRead+1)) { - result = sourceIllegal; - break; - } - /* - * The cases all fall through. See "Note A" below. - */ - switch (extraBytesToRead) { - case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ - case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ - case 3: ch += *source++; ch <<= 6; - case 2: ch += *source++; ch <<= 6; - case 1: ch += *source++; ch <<= 6; - case 0: ch += *source++; - } - ch -= offsetsFromUTF8[extraBytesToRead]; - - if (target >= targetEnd) { - source -= (extraBytesToRead+1); /* Back up source pointer! */ - result = targetExhausted; break; - } - if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ - /* UTF-16 surrogate values are illegal in UTF-32 */ - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { - if (flags == strictConversion) { - source -= (extraBytesToRead+1); /* return to the illegal value itself */ - result = sourceIllegal; - break; - } else { - *target++ = UNI_REPLACEMENT_CHAR; - } - } else { - *target++ = (UTF16)ch; /* normal case */ - } - } else if (ch > UNI_MAX_UTF16) { - if (flags == strictConversion) { - result = sourceIllegal; - source -= (extraBytesToRead+1); /* return to the start */ - break; /* Bail out; shouldn't continue */ - } else { - *target++ = UNI_REPLACEMENT_CHAR; - } - } else { - /* target is a character in range 0xFFFF - 0x10FFFF. */ - if (target + 1 >= targetEnd) { - source -= (extraBytesToRead+1); /* Back up source pointer! */ - result = targetExhausted; break; - } - ch -= halfBase; - *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); - *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); - } + UTF32 ch = 0; + unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; + if (source + extraBytesToRead >= sourceEnd) { + result = sourceExhausted; break; + } + /* Do this check whether lenient or strict */ + if (! isLegalUTF8(source, extraBytesToRead+1)) { + result = sourceIllegal; + break; + } + /* + * The cases all fall through. See "Note A" below. + */ + switch (extraBytesToRead) { + case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */ + case 3: ch += *source++; ch <<= 6; + case 2: ch += *source++; ch <<= 6; + case 1: ch += *source++; ch <<= 6; + case 0: ch += *source++; + } + ch -= offsetsFromUTF8[extraBytesToRead]; + + if (target >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */ + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + source -= (extraBytesToRead+1); /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = (UTF16)ch; /* normal case */ + } + } else if (ch > UNI_MAX_UTF16) { + if (flags == strictConversion) { + result = sourceIllegal; + source -= (extraBytesToRead+1); /* return to the start */ + break; /* Bail out; shouldn't continue */ + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + /* target is a character in range 0xFFFF - 0x10FFFF. */ + if (target + 1 >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up source pointer! */ + result = targetExhausted; break; + } + ch -= halfBase; + *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START); + *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START); + } } *sourceStart = source; *targetStart = target; @@ -330,50 +330,50 @@ ConversionResult ConvertUTF8toUTF16 ( /* --------------------------------------------------------------------- */ ConversionResult ConvertUTF32toUTF8 ( - const UTF32** sourceStart, const UTF32* sourceEnd, - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { + const UTF32** sourceStart, const UTF32* sourceEnd, + UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) { ConversionResult result = conversionOK; const UTF32* source = *sourceStart; UTF8* target = *targetStart; while (source < sourceEnd) { - UTF32 ch; - unsigned short bytesToWrite = 0; - const UTF32 byteMask = 0xBF; - const UTF32 byteMark = 0x80; - ch = *source++; - if (flags == strictConversion ) { - /* UTF-16 surrogate values are illegal in UTF-32 */ - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { - --source; /* return to the illegal value itself */ - result = sourceIllegal; - break; - } - } - /* - * Figure out how many bytes the result will require. Turn any - * illegally large UTF32 things (> Plane 17) into replacement chars. - */ - if (ch < (UTF32)0x80) { bytesToWrite = 1; - } else if (ch < (UTF32)0x800) { bytesToWrite = 2; - } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; - } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4; - } else { bytesToWrite = 3; - ch = UNI_REPLACEMENT_CHAR; - result = sourceIllegal; - } - - target += bytesToWrite; - if (target > targetEnd) { - --source; /* Back up source pointer! */ - target -= bytesToWrite; result = targetExhausted; break; - } - switch (bytesToWrite) { /* note: everything falls through. */ - case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; - case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]); - } - target += bytesToWrite; + UTF32 ch; + unsigned short bytesToWrite = 0; + const UTF32 byteMask = 0xBF; + const UTF32 byteMark = 0x80; + ch = *source++; + if (flags == strictConversion ) { + /* UTF-16 surrogate values are illegal in UTF-32 */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + --source; /* return to the illegal value itself */ + result = sourceIllegal; + break; + } + } + /* + * Figure out how many bytes the result will require. Turn any + * illegally large UTF32 things (> Plane 17) into replacement chars. + */ + if (ch < (UTF32)0x80) { bytesToWrite = 1; + } else if (ch < (UTF32)0x800) { bytesToWrite = 2; + } else if (ch < (UTF32)0x10000) { bytesToWrite = 3; + } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4; + } else { bytesToWrite = 3; + ch = UNI_REPLACEMENT_CHAR; + result = sourceIllegal; + } + + target += bytesToWrite; + if (target > targetEnd) { + --source; /* Back up source pointer! */ + target -= bytesToWrite; result = targetExhausted; break; + } + switch (bytesToWrite) { /* note: everything falls through. */ + case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6; + case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]); + } + target += bytesToWrite; } *sourceStart = source; *targetStart = target; @@ -383,59 +383,59 @@ ConversionResult ConvertUTF32toUTF8 ( /* --------------------------------------------------------------------- */ ConversionResult ConvertUTF8toUTF32 ( - const UTF8** sourceStart, const UTF8* sourceEnd, - UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { + const UTF8** sourceStart, const UTF8* sourceEnd, + UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) { ConversionResult result = conversionOK; const UTF8* source = *sourceStart; UTF32* target = *targetStart; while (source < sourceEnd) { - UTF32 ch = 0; - unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; - if (source + extraBytesToRead >= sourceEnd) { - result = sourceExhausted; break; - } - /* Do this check whether lenient or strict */ - if (! isLegalUTF8(source, extraBytesToRead+1)) { - result = sourceIllegal; - break; - } - /* - * The cases all fall through. See "Note A" below. - */ - switch (extraBytesToRead) { - case 5: ch += *source++; ch <<= 6; - case 4: ch += *source++; ch <<= 6; - case 3: ch += *source++; ch <<= 6; - case 2: ch += *source++; ch <<= 6; - case 1: ch += *source++; ch <<= 6; - case 0: ch += *source++; - } - ch -= offsetsFromUTF8[extraBytesToRead]; - - if (target >= targetEnd) { - source -= (extraBytesToRead+1); /* Back up the source pointer! */ - result = targetExhausted; break; - } - if (ch <= UNI_MAX_LEGAL_UTF32) { - /* - * UTF-16 surrogate values are illegal in UTF-32, and anything - * over Plane 17 (> 0x10FFFF) is illegal. - */ - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { - if (flags == strictConversion) { - source -= (extraBytesToRead+1); /* return to the illegal value itself */ - result = sourceIllegal; - break; - } else { - *target++ = UNI_REPLACEMENT_CHAR; - } - } else { - *target++ = ch; - } - } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */ - result = sourceIllegal; - *target++ = UNI_REPLACEMENT_CHAR; - } + UTF32 ch = 0; + unsigned short extraBytesToRead = trailingBytesForUTF8[*source]; + if (source + extraBytesToRead >= sourceEnd) { + result = sourceExhausted; break; + } + /* Do this check whether lenient or strict */ + if (! isLegalUTF8(source, extraBytesToRead+1)) { + result = sourceIllegal; + break; + } + /* + * The cases all fall through. See "Note A" below. + */ + switch (extraBytesToRead) { + case 5: ch += *source++; ch <<= 6; + case 4: ch += *source++; ch <<= 6; + case 3: ch += *source++; ch <<= 6; + case 2: ch += *source++; ch <<= 6; + case 1: ch += *source++; ch <<= 6; + case 0: ch += *source++; + } + ch -= offsetsFromUTF8[extraBytesToRead]; + + if (target >= targetEnd) { + source -= (extraBytesToRead+1); /* Back up the source pointer! */ + result = targetExhausted; break; + } + if (ch <= UNI_MAX_LEGAL_UTF32) { + /* + * UTF-16 surrogate values are illegal in UTF-32, and anything + * over Plane 17 (> 0x10FFFF) is illegal. + */ + if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) { + if (flags == strictConversion) { + source -= (extraBytesToRead+1); /* return to the illegal value itself */ + result = sourceIllegal; + break; + } else { + *target++ = UNI_REPLACEMENT_CHAR; + } + } else { + *target++ = ch; + } + } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */ + result = sourceIllegal; + *target++ = UNI_REPLACEMENT_CHAR; + } } *sourceStart = source; *targetStart = target; @@ -448,14 +448,14 @@ ConversionResult ConvertUTF8toUTF32 ( The fall-through switches in UTF-8 reading code save a temp variable, some decrements & conditionals. The switches are equivalent to the following loop: - { - int tmpBytesToRead = extraBytesToRead+1; - do { - ch += *source++; - --tmpBytesToRead; - if (tmpBytesToRead) ch <<= 6; - } while (tmpBytesToRead > 0); - } + { + int tmpBytesToRead = extraBytesToRead+1; + do { + ch += *source++; + --tmpBytesToRead; + if (tmpBytesToRead) ch <<= 6; + } while (tmpBytesToRead > 0); + } In UTF-8 writing code, the switches on "bytesToWrite" are similarly unrolled loops. diff --git a/cpp/src/IceUtil/ConvertUTF.h b/cpp/src/IceUtil/ConvertUTF.h index daa2e98db52..a70e62fd1a9 100755 --- a/cpp/src/IceUtil/ConvertUTF.h +++ b/cpp/src/IceUtil/ConvertUTF.h @@ -60,12 +60,12 @@ the respective buffers. Input parameters: - sourceStart - pointer to a pointer to the source buffer. - The contents of this are modified on return so that - it points at the next thing to be converted. - targetStart - similarly, pointer to pointer to the target buffer. - sourceEnd, targetEnd - respectively pointers to the ends of the - two buffers, for overflow checking only. + sourceStart - pointer to a pointer to the source buffer. + The contents of this are modified on return so that + it points at the next thing to be converted. + targetStart - similarly, pointer to pointer to the target buffer. + sourceEnd, targetEnd - respectively pointers to the ends of the + two buffers, for overflow checking only. These conversion functions take a ConversionFlags argument. When this flag is set to strict, both irregular sequences and isolated surrogates @@ -82,15 +82,15 @@ they constitute an error. Output parameters: - The value "sourceIllegal" is returned from some routines if the input - sequence is malformed. When "sourceIllegal" is returned, the source - value will point to the illegal value that caused the problem. E.g., - in UTF-8 when a sequence is malformed, it points to the start of the - malformed sequence. + The value "sourceIllegal" is returned from some routines if the input + sequence is malformed. When "sourceIllegal" is returned, the source + value will point to the illegal value that caused the problem. E.g., + in UTF-8 when a sequence is malformed, it points to the start of the + malformed sequence. Author: Mark E. Davis, 1994. Rev History: Rick McGowan, fixes & updates May 2001. - Fixes & updates, Sept 2001. + Fixes & updates, Sept 2001. ------------------------------------------------------------------------ */ @@ -105,10 +105,10 @@ namespace IceUtil { -typedef unsigned int UTF32; /* at least 32 bits */ -typedef unsigned short UTF16; /* at least 16 bits */ -typedef unsigned char UTF8; /* typically 8 bits */ -typedef bool Boolean; /* 0 or 1 */ +typedef unsigned int UTF32; /* at least 32 bits */ +typedef unsigned short UTF16; /* at least 16 bits */ +typedef unsigned char UTF8; /* typically 8 bits */ +typedef bool Boolean; /* 0 or 1 */ /* Some fundamental constants */ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD @@ -125,7 +125,7 @@ ConversionResult ConvertUTF8toUTF16( ConversionResult ConvertUTF16toUTF8 ( const UTF16** sourceStart, const UTF16* sourceEnd, UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags); - + ConversionResult ConvertUTF8toUTF32( const UTF8** sourceStart, const UTF8* sourceEnd, UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags); diff --git a/cpp/src/IceUtil/CountDownLatch.cpp b/cpp/src/IceUtil/CountDownLatch.cpp index b30fe718242..c54cc51240d 100644 --- a/cpp/src/IceUtil/CountDownLatch.cpp +++ b/cpp/src/IceUtil/CountDownLatch.cpp @@ -15,26 +15,26 @@ IceUtil::CountDownLatch::CountDownLatch(int count) : { if(count < 0) { - throw Exception(__FILE__, __LINE__); + throw Exception(__FILE__, __LINE__); } #ifdef _WIN32 _event = CreateEvent(0, TRUE, FALSE, 0); if(_event == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } #else int rc = pthread_mutex_init(&_mutex, 0); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_cond_init(&_cond, 0); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } #endif } @@ -58,24 +58,24 @@ IceUtil::CountDownLatch::await() const #ifdef _WIN32 while(InterlockedExchangeAdd(&_count, 0) > 0) { - DWORD rc = WaitForSingleObject(_event, INFINITE); - assert(rc == WAIT_OBJECT_0 || rc == WAIT_FAILED); - - if(rc == WAIT_FAILED) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + DWORD rc = WaitForSingleObject(_event, INFINITE); + assert(rc == WAIT_OBJECT_0 || rc == WAIT_FAILED); + + if(rc == WAIT_FAILED) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } } #else lock(); while(_count > 0) { - int rc = pthread_cond_wait(&_cond, &_mutex); - if(rc != 0) - { - pthread_mutex_unlock(&_mutex); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + int rc = pthread_cond_wait(&_cond, &_mutex); + if(rc != 0) + { + pthread_mutex_unlock(&_mutex); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } unlock(); @@ -88,10 +88,10 @@ IceUtil::CountDownLatch::countDown() #ifdef _WIN32 if(InterlockedDecrement(&_count) == 0) { - if(SetEvent(_event) == 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + if(SetEvent(_event) == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } } #else bool broadcast = false; @@ -99,7 +99,7 @@ IceUtil::CountDownLatch::countDown() lock(); if(_count > 0 && --_count == 0) { - broadcast = true; + broadcast = true; } #if defined(__APPLE__) // @@ -108,12 +108,12 @@ IceUtil::CountDownLatch::countDown() // if(broadcast) { - int rc = pthread_cond_broadcast(&_cond); - if(rc != 0) - { - pthread_mutex_unlock(&_mutex); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + int rc = pthread_cond_broadcast(&_cond); + if(rc != 0) + { + pthread_mutex_unlock(&_mutex); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } unlock(); @@ -122,11 +122,11 @@ IceUtil::CountDownLatch::countDown() if(broadcast) { - int rc = pthread_cond_broadcast(&_cond); - if(rc != 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + int rc = pthread_cond_broadcast(&_cond); + if(rc != 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } #endif @@ -154,7 +154,7 @@ IceUtil::CountDownLatch::lock() const int rc = pthread_mutex_lock(&_mutex); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } @@ -164,7 +164,7 @@ IceUtil::CountDownLatch::unlock() const int rc = pthread_mutex_unlock(&_mutex); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } diff --git a/cpp/src/IceUtil/CtrlCHandler.cpp b/cpp/src/IceUtil/CtrlCHandler.cpp index a11f36f09a6..21338923e4b 100644 --- a/cpp/src/IceUtil/CtrlCHandler.cpp +++ b/cpp/src/IceUtil/CtrlCHandler.cpp @@ -70,7 +70,7 @@ static BOOL WINAPI handlerRoutine(DWORD dwCtrlType) CtrlCHandlerCallback callback = _handler->getCallback(); if(callback != 0) { - callback(dwCtrlType); + callback(dwCtrlType); } return TRUE; } @@ -81,14 +81,14 @@ CtrlCHandler::CtrlCHandler(CtrlCHandlerCallback callback) StaticMutex::Lock lock(globalMutex); if(_handler != 0) { - throw CtrlCHandlerException(__FILE__, __LINE__); + throw CtrlCHandlerException(__FILE__, __LINE__); } else { - _callback = callback; - _handler = this; - lock.release(); - SetConsoleCtrlHandler(handlerRoutine, TRUE); + _callback = callback; + _handler = this; + lock.release(); + SetConsoleCtrlHandler(handlerRoutine, TRUE); } } @@ -96,8 +96,8 @@ CtrlCHandler::~CtrlCHandler() { SetConsoleCtrlHandler(handlerRoutine, FALSE); { - StaticMutex::Lock lock(globalMutex); - _handler = 0; + StaticMutex::Lock lock(globalMutex); + _handler = 0; } } @@ -120,38 +120,38 @@ sigwaitThread(void*) // for(;;) { - int signal = 0; - int rc = sigwait(&ctrlCLikeSignals, &signal); + int signal = 0; + int rc = sigwait(&ctrlCLikeSignals, &signal); #if defined(__APPLE__) - // - // WORKAROUND: sigwait is not a cancelation point on MacOS X. To cancel this thread, the - // destructor cancels the thread and send a signal to the thread to unblock sigwait, then - // we explicitly test for cancellation. - // - pthread_testcancel(); + // + // WORKAROUND: sigwait is not a cancelation point on MacOS X. To cancel this thread, the + // destructor cancels the thread and send a signal to the thread to unblock sigwait, then + // we explicitly test for cancellation. + // + pthread_testcancel(); #endif - // - // Some sigwait() implementations incorrectly return EINTR - // when interrupted by an unblocked caught signal - // - if(rc == EINTR) - { - continue; - } - assert(rc == 0); - - rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); - assert(rc == 0); - - CtrlCHandlerCallback callback = _handler->getCallback(); - - if(callback != 0) - { - callback(signal); - } - - rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0); - assert(rc == 0); + // + // Some sigwait() implementations incorrectly return EINTR + // when interrupted by an unblocked caught signal + // + if(rc == EINTR) + { + continue; + } + assert(rc == 0); + + rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); + assert(rc == 0); + + CtrlCHandlerCallback callback = _handler->getCallback(); + + if(callback != 0) + { + callback(signal); + } + + rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0); + assert(rc == 0); } return 0; } @@ -164,29 +164,29 @@ CtrlCHandler::CtrlCHandler(CtrlCHandlerCallback callback) StaticMutex::Lock lock(globalMutex); if(_handler != 0) { - throw CtrlCHandlerException(__FILE__, __LINE__); + throw CtrlCHandlerException(__FILE__, __LINE__); } else { - _callback = callback; - _handler = this; - lock.release(); - - // We block these CTRL+C like signals in the main thread, - // and by default all other threads will inherit this signal - // disposition. - - sigset_t ctrlCLikeSignals; - sigemptyset(&ctrlCLikeSignals); - sigaddset(&ctrlCLikeSignals, SIGHUP); - sigaddset(&ctrlCLikeSignals, SIGINT); - sigaddset(&ctrlCLikeSignals, SIGTERM); - int rc = pthread_sigmask(SIG_BLOCK, &ctrlCLikeSignals, 0); - assert(rc == 0); - - // Joinable thread - rc = pthread_create(&_tid, 0, sigwaitThread, 0); - assert(rc == 0); + _callback = callback; + _handler = this; + lock.release(); + + // We block these CTRL+C like signals in the main thread, + // and by default all other threads will inherit this signal + // disposition. + + sigset_t ctrlCLikeSignals; + sigemptyset(&ctrlCLikeSignals); + sigaddset(&ctrlCLikeSignals, SIGHUP); + sigaddset(&ctrlCLikeSignals, SIGINT); + sigaddset(&ctrlCLikeSignals, SIGTERM); + int rc = pthread_sigmask(SIG_BLOCK, &ctrlCLikeSignals, 0); + assert(rc == 0); + + // Joinable thread + rc = pthread_create(&_tid, 0, sigwaitThread, 0); + assert(rc == 0); } } @@ -209,8 +209,8 @@ CtrlCHandler::~CtrlCHandler() assert(status == PTHREAD_CANCELED); #endif { - StaticMutex::Lock lock(globalMutex); - _handler = 0; + StaticMutex::Lock lock(globalMutex); + _handler = 0; } } diff --git a/cpp/src/IceUtil/Exception.cpp b/cpp/src/IceUtil/Exception.cpp index c7619eadcd3..adcced92d33 100644 --- a/cpp/src/IceUtil/Exception.cpp +++ b/cpp/src/IceUtil/Exception.cpp @@ -47,7 +47,7 @@ IceUtil::Exception::ice_print(ostream& out) const { if(_file && _line > 0) { - out << _file << ':' << _line << ": "; + out << _file << ':' << _line << ": "; } out << ice_name(); } @@ -88,7 +88,7 @@ IceUtil::NullHandleException::NullHandleException(const char* file, int line) : { if(nullHandleAbort) { - abort(); + abort(); } } diff --git a/cpp/src/IceUtil/InputUtil.cpp b/cpp/src/IceUtil/InputUtil.cpp index b8cdb278e9f..293bd8b2107 100644 --- a/cpp/src/IceUtil/InputUtil.cpp +++ b/cpp/src/IceUtil/InputUtil.cpp @@ -36,11 +36,11 @@ static const string allDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // static const char digitVal[] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0' - '9' - 100, 100, 100, 100, 100, 100, 100, // punctuation - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J' - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T' - 30, 31, 32, 33, 34, 35 // 'U' - 'Z' + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0' - '9' + 100, 100, 100, 100, 100, 100, 100, // punctuation + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, // 'A' - 'J' + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, // 'K' - 'T' + 30, 31, 32, 33, 34, 35 // 'U' - 'Z' }; static IceUtil::Int64 @@ -51,7 +51,7 @@ strToInt64Impl(const char* s, char** endptr, int base) // if(endptr) { - *endptr = const_cast<char *>(s); + *endptr = const_cast<char *>(s); } // @@ -59,7 +59,7 @@ strToInt64Impl(const char* s, char** endptr, int base) // while(*s && isspace(*s)) { - ++s; + ++s; } // @@ -68,12 +68,12 @@ strToInt64Impl(const char* s, char** endptr, int base) int sign = 1; if(*s == '+') { - ++s; + ++s; } else if(*s == '-') { - sign = -1; - ++s; + sign = -1; + ++s; } // @@ -81,34 +81,34 @@ strToInt64Impl(const char* s, char** endptr, int base) // if(base == 0) { - if(*s == '0') - { - base = 8; - ++s; - - // - // We have at least this zero - // - if(endptr) - { - *endptr = const_cast<char *>(s); - } - - if(*s == 'x' || *s == 'X') - { - base = 16; - ++s; - } - } - else - { - base = 10; - } + if(*s == '0') + { + base = 8; + ++s; + + // + // We have at least this zero + // + if(endptr) + { + *endptr = const_cast<char *>(s); + } + + if(*s == 'x' || *s == 'X') + { + base = 16; + ++s; + } + } + else + { + base = 10; + } } else if(base < 2 || base > 36) { - errno = EINVAL; - return 0; + errno = EINVAL; + return 0; } // @@ -116,10 +116,10 @@ strToInt64Impl(const char* s, char** endptr, int base) // if(*s == '\0') { - // - // We did not read any new digit so we don't update endptr - // - return 0; + // + // We did not read any new digit so we don't update endptr + // + return 0; } Int64 result = 0; @@ -127,43 +127,43 @@ strToInt64Impl(const char* s, char** endptr, int base) bool digitFound = false; const string validDigits(allDigits.begin(), allDigits.begin() + base); while(*s && validDigits.find_first_of(toupper(*s)) != validDigits.npos) - { - digitFound = true; - if(!overflow) - { - int digit = digitVal[toupper(*s) - '0']; - assert(digit != 100); - if(result < _I64_MAX / base) - { - result *= base; - result += digit; - } - else if((digit <= _I64_MAX % base) || (sign == -1 && digit == _I64_MAX % base + 1)) - { - result *= base; - result += digit; - } - else - { - overflow = true; - result = sign == -1 ? _I64_MIN : _I64_MAX; - } - } - ++s; + { + digitFound = true; + if(!overflow) + { + int digit = digitVal[toupper(*s) - '0']; + assert(digit != 100); + if(result < _I64_MAX / base) + { + result *= base; + result += digit; + } + else if((digit <= _I64_MAX % base) || (sign == -1 && digit == _I64_MAX % base + 1)) + { + result *= base; + result += digit; + } + else + { + overflow = true; + result = sign == -1 ? _I64_MIN : _I64_MAX; + } + } + ++s; } if(overflow) { - errno = ERANGE; + errno = ERANGE; } else { - result *= sign; + result *= sign; } if(digitFound && endptr != 0) { - *endptr = const_cast<char *>(s); + *endptr = const_cast<char *>(s); } return result; diff --git a/cpp/src/IceUtil/MD5I.cpp b/cpp/src/IceUtil/MD5I.cpp index dc96955a36a..ac5be31483e 100644 --- a/cpp/src/IceUtil/MD5I.cpp +++ b/cpp/src/IceUtil/MD5I.cpp @@ -27,7 +27,7 @@ This code implements the MD5 Algorithm defined in RFC 1321, whose text is available at - http://www.ietf.org/rfc/rfc1321.txt + http://www.ietf.org/rfc/rfc1321.txt The code is derived from the text of the RFC, including the test suite (section A.5) but excluding the rest of Appendix A. It does not include any code or documentation that is identified in the RFC as being @@ -38,14 +38,14 @@ that follows (in reverse chronological order): 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order - either statically or dynamically; added missing #include <string.h> - in library. + either statically or dynamically; added missing #include <string.h> + in library. 2002-03-11 lpd Corrected argument list for main(), and added int return - type, in test program and T value program. + type, in test program and T value program. 2002-02-21 lpd Added missing #include <stdio.h> in test program. 2000-07-03 lpd Patched to eliminate warnings about "constant is - unsigned in ANSI C, signed in traditional"; made test program - self-checking. + unsigned in ANSI C, signed in traditional"; made test program + self-checking. 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5). 1999-05-03 lpd Original version. @@ -54,7 +54,7 @@ #include <IceUtil/MD5I.h> #include <string.h> -#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */ +#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */ #ifdef ARCH_IS_BIG_ENDIAN # define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1) #else @@ -132,8 +132,8 @@ static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) { md5_word_t - a = pms->abcd[0], b = pms->abcd[1], - c = pms->abcd[2], d = pms->abcd[3]; + a = pms->abcd[0], b = pms->abcd[1], + c = pms->abcd[2], d = pms->abcd[3]; md5_word_t t; #if BYTE_ORDER > 0 /* Define storage only for big-endian CPUs. */ @@ -146,51 +146,51 @@ md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) { #if BYTE_ORDER == 0 - /* - * Determine dynamically whether this is a big-endian or - * little-endian machine, since we can use a more efficient - * algorithm on the latter. - */ - static const int w = 1; - - if (*((const md5_byte_t *)&w)) /* dynamic little-endian */ + /* + * Determine dynamically whether this is a big-endian or + * little-endian machine, since we can use a more efficient + * algorithm on the latter. + */ + static const int w = 1; + + if (*((const md5_byte_t *)&w)) /* dynamic little-endian */ #endif -#if BYTE_ORDER <= 0 /* little-endian */ - { - /* - * On little-endian machines, we can process properly aligned - * data without copying it. - */ - if (!((data - (const md5_byte_t *)0) & 3)) { - /* data are properly aligned */ - X = (const md5_word_t *)data; - } else { - /* not aligned */ - memcpy(xbuf, data, 64); - X = xbuf; - } - } +#if BYTE_ORDER <= 0 /* little-endian */ + { + /* + * On little-endian machines, we can process properly aligned + * data without copying it. + */ + if (!((data - (const md5_byte_t *)0) & 3)) { + /* data are properly aligned */ + X = (const md5_word_t *)data; + } else { + /* not aligned */ + memcpy(xbuf, data, 64); + X = xbuf; + } + } #endif #if BYTE_ORDER == 0 - else /* dynamic big-endian */ + else /* dynamic big-endian */ #endif -#if BYTE_ORDER >= 0 /* big-endian */ - { - /* - * On big-endian machines, we must arrange the bytes in the - * right order. - */ - const md5_byte_t *xp = data; - int i; +#if BYTE_ORDER >= 0 /* big-endian */ + { + /* + * On big-endian machines, we must arrange the bytes in the + * right order. + */ + const md5_byte_t *xp = data; + int i; # if BYTE_ORDER == 0 - X = xbuf; /* (dynamic only) */ + X = xbuf; /* (dynamic only) */ # else -# define xbuf X /* (static only) */ +# define xbuf X /* (static only) */ # endif - for (i = 0; i < 16; ++i, xp += 4) - xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); - } + for (i = 0; i < 16; ++i, xp += 4) + xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); + } #endif } @@ -328,54 +328,54 @@ md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes) md5_word_t nbits = (md5_word_t)(nbytes << 3); if (nbytes <= 0) - return; + return; /* Update the message length. */ pms->count[1] += nbytes >> 29; pms->count[0] += nbits; if (pms->count[0] < nbits) - pms->count[1]++; + pms->count[1]++; /* Process an initial partial block. */ if (offset) { - int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); - - memcpy(pms->buf + offset, p, copy); - if (offset + copy < 64) - return; - p += copy; - left -= copy; - md5_process(pms, pms->buf); + int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); + + memcpy(pms->buf + offset, p, copy); + if (offset + copy < 64) + return; + p += copy; + left -= copy; + md5_process(pms, pms->buf); } /* Process full blocks. */ for (; left >= 64; p += 64, left -= 64) - md5_process(pms, p); + md5_process(pms, p); /* Process a final partial block. */ if (left) - memcpy(pms->buf, p, left); + memcpy(pms->buf, p, left); } void md5_finish(md5_state_t *pms, md5_byte_t digest[16]) { static const md5_byte_t pad[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; md5_byte_t data[8]; int i; /* Save the length before padding. */ for (i = 0; i < 8; ++i) - data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3)); + data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3)); /* Pad to 56 bytes mod 64. */ md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); /* Append the length. */ md5_append(pms, data, 8); for (i = 0; i < 16; ++i) - digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); + digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); } diff --git a/cpp/src/IceUtil/MD5I.h b/cpp/src/IceUtil/MD5I.h index 5eb6d6c469c..66588283e83 100644 --- a/cpp/src/IceUtil/MD5I.h +++ b/cpp/src/IceUtil/MD5I.h @@ -27,7 +27,7 @@ This code implements the MD5 Algorithm defined in RFC 1321, whose text is available at - http://www.ietf.org/rfc/rfc1321.txt + http://www.ietf.org/rfc/rfc1321.txt The code is derived from the text of the RFC, including the test suite (section A.5) but excluding the rest of Appendix A. It does not include any code or documentation that is identified in the RFC as being @@ -38,12 +38,12 @@ that follows (in reverse chronological order): 2002-04-13 lpd Removed support for non-ANSI compilers; removed - references to Ghostscript; clarified derivation from RFC 1321; - now handles byte order either statically or dynamically. + references to Ghostscript; clarified derivation from RFC 1321; + now handles byte order either statically or dynamically. 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5); - added conditionalization for C++ compilation from Martin - Purschke <purschke@bnl.gov>. + added conditionalization for C++ compilation from Martin + Purschke <purschke@bnl.gov>. 1999-05-03 lpd Original version. */ @@ -65,9 +65,9 @@ typedef unsigned int md5_word_t; /* 32-bit word */ /* Define the state of the MD5 Algorithm. */ typedef struct md5_state_s { - md5_word_t count[2]; /* message length in bits, lsw first */ - md5_word_t abcd[4]; /* digest buffer */ - md5_byte_t buf[64]; /* accumulate block */ + md5_word_t count[2]; /* message length in bits, lsw first */ + md5_word_t abcd[4]; /* digest buffer */ + md5_byte_t buf[64]; /* accumulate block */ } md5_state_t; #ifdef __cplusplus diff --git a/cpp/src/IceUtil/Options.cpp b/cpp/src/IceUtil/Options.cpp index c8fc3635177..9fe71cc1311 100755 --- a/cpp/src/IceUtil/Options.cpp +++ b/cpp/src/IceUtil/Options.cpp @@ -36,7 +36,7 @@ IceUtil::APIException::ice_print(ostream& out) const Exception::ice_print(out); if(!reason.empty()) { - out << ": " << reason; + out << ": " << reason; } } @@ -82,7 +82,7 @@ IceUtil::BadOptException::ice_print(ostream& out) const Exception::ice_print(out); if(!reason.empty()) { - out << ": " << reason; + out << ": " << reason; } } @@ -120,51 +120,51 @@ IceUtil::Options::checkArgs(const string& shortOpt, const string& longOpt, bool if(!shortOpt.empty()) { - if(shortOpt.size() != 1) - { - string err = "`"; - err += shortOpt; - err += "': a short option cannot specify more than one option"; - throw IllegalArgumentException(__FILE__, __LINE__, err); - } - if(shortOpt.find_first_of(" \t\n\r\f\v") != string::npos) - { - string err = "`"; - err += shortOpt; - err += "': a short option cannot be whitespace"; - throw IllegalArgumentException(__FILE__, __LINE__, err); - } - if(shortOpt[0] == '-') - { - string err = "`"; - err += shortOpt; - err += "': a short option cannot be `-'"; - throw IllegalArgumentException(__FILE__, __LINE__, err); - } + if(shortOpt.size() != 1) + { + string err = "`"; + err += shortOpt; + err += "': a short option cannot specify more than one option"; + throw IllegalArgumentException(__FILE__, __LINE__, err); + } + if(shortOpt.find_first_of(" \t\n\r\f\v") != string::npos) + { + string err = "`"; + err += shortOpt; + err += "': a short option cannot be whitespace"; + throw IllegalArgumentException(__FILE__, __LINE__, err); + } + if(shortOpt[0] == '-') + { + string err = "`"; + err += shortOpt; + err += "': a short option cannot be `-'"; + throw IllegalArgumentException(__FILE__, __LINE__, err); + } } if(!longOpt.empty()) { - if(longOpt.find_first_of(" \t\n\r\f\v") != string::npos) - { - string err = "`"; - err += longOpt; - err += "': a long option cannot contain whitespace"; - throw IllegalArgumentException(__FILE__, __LINE__, err); - } - if(longOpt[0] == '-') - { - string err = "`"; - err += longOpt; - err += "': a long option must not contain a leading `-'"; - throw IllegalArgumentException(__FILE__, __LINE__, err); - } + if(longOpt.find_first_of(" \t\n\r\f\v") != string::npos) + { + string err = "`"; + err += longOpt; + err += "': a long option cannot contain whitespace"; + throw IllegalArgumentException(__FILE__, __LINE__, err); + } + if(longOpt[0] == '-') + { + string err = "`"; + err += longOpt; + err += "': a long option must not contain a leading `-'"; + throw IllegalArgumentException(__FILE__, __LINE__, err); + } } if(!needArg && !dflt.empty()) { - throw IllegalArgumentException(__FILE__, __LINE__, - "a default value can be specified only for options requiring an argument"); + throw IllegalArgumentException(__FILE__, __LINE__, + "a default value can be specified only for options requiring an argument"); } } @@ -175,7 +175,7 @@ IceUtil::Options::addOpt(const string& shortOpt, const string& longOpt, ArgType if(parseCalled) { - throw APIException(__FILE__, __LINE__, "cannot add options after parse() was called"); + throw APIException(__FILE__, __LINE__, "cannot add options after parse() was called"); } checkArgs(shortOpt, longOpt, at == NeedArg, dflt); @@ -217,361 +217,361 @@ IceUtil::Options::split(const string& line) for(string::size_type i = 0; i < l.size(); ++i) { - char c = l[i]; + char c = l[i]; switch(state) - { - case Normal: - { - switch(c) - { - case '\\': - { - // - // Ignore a backslash at the end of the string, - // and strip backslash-newline pairs. If a - // backslash is followed by a space, single quote, - // double quote, or dollar sign, we drop the backslash - // and write the space, single quote, double quote, - // or dollar sign. This is necessary to allow quotes - // to be escaped. Dropping the backslash preceding a - // space deviates from bash quoting rules, but is - // necessary so we don't drop backslashes from Windows - // path names.) - // - if(i < l.size() - 1 && l[++i] != '\n') - { - switch(l[i]) - { - case ' ': - case '$': - case '\'': - case '"': - { - arg.push_back(l[i]); - break; - } - default: - { - arg.push_back('\\'); - arg.push_back(l[i]); - break; - } - } - } - break; - } - case '\'': - { - state = SingleQuote; - break; - } - case '"': - { - state = DoubleQuote; - break; - } - case '$': - { - if(i < l.size() - 1 && l[i + 1] == '\'') - { - state = ANSIQuote; // Bash uses $'<text>' to allow ANSI escape sequences within <text>. - ++i; - } - else - { - arg.push_back('$'); - } - break; - } - default: - { - if(IFS.find(l[i]) != string::npos) - { - vec.push_back(arg); - arg.clear(); - - // - // Move to start of next argument. - // - while(++i < l.size() && IFS.find(l[i]) != string::npos) - { - ; - } - --i; - } - else - { - arg.push_back(l[i]); - } - break; - } - } - break; - } - case DoubleQuote: - { - // - // Within double quotes, only backslash retains its special - // meaning, and only if followed by double quote, backslash, - // or newline. If not followed by one of these characters, - // both the backslash and the character are preserved. - // - if(c == '\\' && i < l.size() - 1) - { - switch(c = l[++i]) - { - case '"': - case '\\': - case '\n': - { - arg.push_back(c); - break; - } - default: - { - arg.push_back('\\'); - arg.push_back(c); - break; - } - } - } - else if(c == '"') // End of double-quote mode. - { - state = Normal; - } - else - { - arg.push_back(c); // Everything else is taken literally. - } - break; - } - case SingleQuote: - { - if(c == '\'') // End of single-quote mode. - { - state = Normal; - } - else - { - arg.push_back(c); // Everything else is taken literally. - } - break; - } - case ANSIQuote: - { - switch(c) - { - case '\\': - { - if(i == l.size() - 1) - { - break; - } - switch(c = l[++i]) - { - // - // Single-letter escape sequences. - // - case 'a': - { - arg.push_back('\a'); - break; - } - case 'b': - { - arg.push_back('\b'); - break; - } - case 'f': - { - arg.push_back('\f'); - break; - } - case 'n': - { - arg.push_back('\n'); - break; - } - case 'r': - { - arg.push_back('\r'); - break; - } - case 't': - { - arg.push_back('\t'); - break; - } - case 'v': - { - arg.push_back('\v'); - break; - } - case '\\': - { - arg.push_back('\\'); - break; - } - case '\'': - { - arg.push_back('\''); - break; - } - case 'e': // Not ANSI-C, but used by bash. - { - arg.push_back('\033'); - break; - } - - // - // Process up to three octal digits. - // - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - { - static string octalDigits = "01234567"; - unsigned short us = 0; - string::size_type j; - for(j = i; - j < i + 3 && j < l.size() && octalDigits.find_first_of(c = l[j]) != string::npos; - ++j) - { - us = us * 8 + c - '0'; - } - i = j - 1; - arg.push_back(static_cast<char>(us)); - break; - } - - // - // Process up to two hex digits. - // - case 'x': - { - if(i < l.size() - 1 && !isxdigit(l[i + 1])) - { - arg.push_back('\\'); - arg.push_back('x'); - break; - } - - IceUtil::Int64 ull = 0; - string::size_type j; - for(j = i + 1; j < i + 3 && j < l.size() && isxdigit(c = l[j]); ++j) - { - ull *= 16; - if(isdigit(c)) - { - ull += c - '0'; - } - else if(islower(c)) - { - ull += c - 'a' + 10; - } - else - { - ull += c - 'A' + 10; - } - } - i = j - 1; - arg.push_back(static_cast<char>(ull)); - break; - } - - // - // Process control-chars. - // - case 'c': - { - c = l[++i]; - if(isalpha(c) || c == '@' || (c >= '[' && c <= '_')) - { - arg.push_back(static_cast<char>(toupper(c) - '@')); - } - else - { - // - // Bash does not define what should happen if a \c - // is not followed by a recognized control character. - // We simply treat this case like other unrecognized - // escape sequences, that is, we preserve the escape - // sequence unchanged. - // - arg.push_back('\\'); - arg.push_back('c'); - arg.push_back(c); - } - break; - } - - // - // If inside an ANSI-quoted string, a backslash isn't followed by - // one of the recognized characters, both the backslash and the - // character are preserved. - // - default: - { - arg.push_back('\\'); - arg.push_back(c); - break; - } - } - break; - } - case '\'': // End of ANSI-quote mode. - { - state = Normal; - break; - } - default: - { - arg.push_back(c); // Everything else is taken literally. - break; - } - } - break; - } - default: - { - assert(!"Impossible parse state"); - break; - } - } + { + case Normal: + { + switch(c) + { + case '\\': + { + // + // Ignore a backslash at the end of the string, + // and strip backslash-newline pairs. If a + // backslash is followed by a space, single quote, + // double quote, or dollar sign, we drop the backslash + // and write the space, single quote, double quote, + // or dollar sign. This is necessary to allow quotes + // to be escaped. Dropping the backslash preceding a + // space deviates from bash quoting rules, but is + // necessary so we don't drop backslashes from Windows + // path names.) + // + if(i < l.size() - 1 && l[++i] != '\n') + { + switch(l[i]) + { + case ' ': + case '$': + case '\'': + case '"': + { + arg.push_back(l[i]); + break; + } + default: + { + arg.push_back('\\'); + arg.push_back(l[i]); + break; + } + } + } + break; + } + case '\'': + { + state = SingleQuote; + break; + } + case '"': + { + state = DoubleQuote; + break; + } + case '$': + { + if(i < l.size() - 1 && l[i + 1] == '\'') + { + state = ANSIQuote; // Bash uses $'<text>' to allow ANSI escape sequences within <text>. + ++i; + } + else + { + arg.push_back('$'); + } + break; + } + default: + { + if(IFS.find(l[i]) != string::npos) + { + vec.push_back(arg); + arg.clear(); + + // + // Move to start of next argument. + // + while(++i < l.size() && IFS.find(l[i]) != string::npos) + { + ; + } + --i; + } + else + { + arg.push_back(l[i]); + } + break; + } + } + break; + } + case DoubleQuote: + { + // + // Within double quotes, only backslash retains its special + // meaning, and only if followed by double quote, backslash, + // or newline. If not followed by one of these characters, + // both the backslash and the character are preserved. + // + if(c == '\\' && i < l.size() - 1) + { + switch(c = l[++i]) + { + case '"': + case '\\': + case '\n': + { + arg.push_back(c); + break; + } + default: + { + arg.push_back('\\'); + arg.push_back(c); + break; + } + } + } + else if(c == '"') // End of double-quote mode. + { + state = Normal; + } + else + { + arg.push_back(c); // Everything else is taken literally. + } + break; + } + case SingleQuote: + { + if(c == '\'') // End of single-quote mode. + { + state = Normal; + } + else + { + arg.push_back(c); // Everything else is taken literally. + } + break; + } + case ANSIQuote: + { + switch(c) + { + case '\\': + { + if(i == l.size() - 1) + { + break; + } + switch(c = l[++i]) + { + // + // Single-letter escape sequences. + // + case 'a': + { + arg.push_back('\a'); + break; + } + case 'b': + { + arg.push_back('\b'); + break; + } + case 'f': + { + arg.push_back('\f'); + break; + } + case 'n': + { + arg.push_back('\n'); + break; + } + case 'r': + { + arg.push_back('\r'); + break; + } + case 't': + { + arg.push_back('\t'); + break; + } + case 'v': + { + arg.push_back('\v'); + break; + } + case '\\': + { + arg.push_back('\\'); + break; + } + case '\'': + { + arg.push_back('\''); + break; + } + case 'e': // Not ANSI-C, but used by bash. + { + arg.push_back('\033'); + break; + } + + // + // Process up to three octal digits. + // + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + { + static string octalDigits = "01234567"; + unsigned short us = 0; + string::size_type j; + for(j = i; + j < i + 3 && j < l.size() && octalDigits.find_first_of(c = l[j]) != string::npos; + ++j) + { + us = us * 8 + c - '0'; + } + i = j - 1; + arg.push_back(static_cast<char>(us)); + break; + } + + // + // Process up to two hex digits. + // + case 'x': + { + if(i < l.size() - 1 && !isxdigit(l[i + 1])) + { + arg.push_back('\\'); + arg.push_back('x'); + break; + } + + IceUtil::Int64 ull = 0; + string::size_type j; + for(j = i + 1; j < i + 3 && j < l.size() && isxdigit(c = l[j]); ++j) + { + ull *= 16; + if(isdigit(c)) + { + ull += c - '0'; + } + else if(islower(c)) + { + ull += c - 'a' + 10; + } + else + { + ull += c - 'A' + 10; + } + } + i = j - 1; + arg.push_back(static_cast<char>(ull)); + break; + } + + // + // Process control-chars. + // + case 'c': + { + c = l[++i]; + if(isalpha(c) || c == '@' || (c >= '[' && c <= '_')) + { + arg.push_back(static_cast<char>(toupper(c) - '@')); + } + else + { + // + // Bash does not define what should happen if a \c + // is not followed by a recognized control character. + // We simply treat this case like other unrecognized + // escape sequences, that is, we preserve the escape + // sequence unchanged. + // + arg.push_back('\\'); + arg.push_back('c'); + arg.push_back(c); + } + break; + } + + // + // If inside an ANSI-quoted string, a backslash isn't followed by + // one of the recognized characters, both the backslash and the + // character are preserved. + // + default: + { + arg.push_back('\\'); + arg.push_back(c); + break; + } + } + break; + } + case '\'': // End of ANSI-quote mode. + { + state = Normal; + break; + } + default: + { + arg.push_back(c); // Everything else is taken literally. + break; + } + } + break; + } + default: + { + assert(!"Impossible parse state"); + break; + } + } } switch(state) { - case Normal: - { - vec.push_back(arg); - break; - } - case SingleQuote: - { - throw BadOptException(__FILE__, __LINE__, "missing closing single quote"); - break; - } - case DoubleQuote: - { - throw BadOptException(__FILE__, __LINE__, "missing closing double quote"); - break; - } - case ANSIQuote: - { - throw BadOptException(__FILE__, __LINE__, "unterminated $' quote"); - break; - } - default: - { - assert(!"Impossible parse state"); - break; - } + case Normal: + { + vec.push_back(arg); + break; + } + case SingleQuote: + { + throw BadOptException(__FILE__, __LINE__, "missing closing single quote"); + break; + } + case DoubleQuote: + { + throw BadOptException(__FILE__, __LINE__, "missing closing double quote"); + break; + } + case ANSIQuote: + { + throw BadOptException(__FILE__, __LINE__, "unterminated $' quote"); + break; + } + default: + { + assert(!"Impossible parse state"); + break; + } } return vec; @@ -592,7 +592,7 @@ IceUtil::Options::parse(const StringVector& args) if(parseCalled) { - throw APIException(__FILE__, __LINE__, "cannot call parse() more than once on the same Option instance"); + throw APIException(__FILE__, __LINE__, "cannot call parse() more than once on the same Option instance"); } parseCalled = true; @@ -603,133 +603,133 @@ IceUtil::Options::parse(const StringVector& args) string::size_type i; for(i = 1; i < args.size(); ++i) { - if(args[i] == "-" || args[i] == "--") - { - ++i; - break; // "-" and "--" indicate end of options. - } - - string opt; - ValidOpts::iterator pos; + if(args[i] == "-" || args[i] == "--") + { + ++i; + break; // "-" and "--" indicate end of options. + } + + string opt; + ValidOpts::iterator pos; bool argDone = false; - if(args[i].compare(0, 2, "--") == 0) - { - // - // Long option. If the option has an argument, it can either be separated by '=' - // or appear as a separate argument. For example, "--name value" is the same - // as "--name=value". - // - string::size_type p = args[i].find('=', 2); - if(p != string::npos) - { - opt = args[i].substr(2, p - 2); - } - else - { - opt = args[i].substr(2); - } - - pos = checkOpt(opt, LongOpt); - - if(pos->second->repeat == NoRepeat) - { - set<string>::iterator seenPos = seenNonRepeatableOpts.find(opt); - if(seenPos != seenNonRepeatableOpts.end()) - { - string err = "`--"; - err += opt + ":' option cannot be repeated"; - throw BadOptException(__FILE__, __LINE__, err); - } - seenNonRepeatableOpts.insert(seenPos, opt); - string synonym = getSynonym(opt); - if(!synonym.empty()) - { - seenNonRepeatableOpts.insert(synonym); - } - } - - if(p != string::npos) - { - if(pos->second->arg == NoArg && p != args[i].size() - 1) - { - string err = "`"; - err += args[i]; - err += "': option does not take an argument"; - throw BadOptException(__FILE__, __LINE__, err); - } - setOpt(opt, "", args[i].substr(p + 1), pos->second->repeat); - argDone = true; - } - } - else if(!args[i].empty() && args[i][0] == '-') - { - // - // Short option. - // - for(string::size_type p = 1; p < args[i].size(); ++p) - { - opt.clear(); - opt.push_back(args[i][p]); - pos = checkOpt(opt, ShortOpt); - - if(pos->second->repeat == NoRepeat) - { - set<string>::iterator seenPos = seenNonRepeatableOpts.find(opt); - if(seenPos != seenNonRepeatableOpts.end()) - { - string err = "`-"; - err += opt + ":' option cannot be repeated"; - throw BadOptException(__FILE__, __LINE__, err); - } - seenNonRepeatableOpts.insert(seenPos, opt); - string synonym = getSynonym(opt); - if(!synonym.empty()) - { - seenNonRepeatableOpts.insert(synonym); - } - } - - if(pos->second->arg == NeedArg && p != args[i].size() - 1) - { - string optArg = args[i].substr(p + 1); - setOpt(opt, "", optArg, pos->second->repeat); - argDone = true; - break; - } - } - } - else - { - // - // Not an option or option argument. - // - result.push_back(args[i]); - argDone = true; - } - - if(!argDone) - { - if(pos->second->arg == NeedArg) // Need an argument that is separated by whitespace. - { - if(i == args.size() - 1) - { - string err = "`-"; - if(opt.size() != 1) - { - err += "-"; - } - err += opt; - err += "' option requires an argument"; - throw BadOptException(__FILE__, __LINE__, err); - } - setOpt(opt, "", args[++i], pos->second->repeat); - } - else - { - setOpt(opt, "", "1", pos->second->repeat); - } - } + if(args[i].compare(0, 2, "--") == 0) + { + // + // Long option. If the option has an argument, it can either be separated by '=' + // or appear as a separate argument. For example, "--name value" is the same + // as "--name=value". + // + string::size_type p = args[i].find('=', 2); + if(p != string::npos) + { + opt = args[i].substr(2, p - 2); + } + else + { + opt = args[i].substr(2); + } + + pos = checkOpt(opt, LongOpt); + + if(pos->second->repeat == NoRepeat) + { + set<string>::iterator seenPos = seenNonRepeatableOpts.find(opt); + if(seenPos != seenNonRepeatableOpts.end()) + { + string err = "`--"; + err += opt + ":' option cannot be repeated"; + throw BadOptException(__FILE__, __LINE__, err); + } + seenNonRepeatableOpts.insert(seenPos, opt); + string synonym = getSynonym(opt); + if(!synonym.empty()) + { + seenNonRepeatableOpts.insert(synonym); + } + } + + if(p != string::npos) + { + if(pos->second->arg == NoArg && p != args[i].size() - 1) + { + string err = "`"; + err += args[i]; + err += "': option does not take an argument"; + throw BadOptException(__FILE__, __LINE__, err); + } + setOpt(opt, "", args[i].substr(p + 1), pos->second->repeat); + argDone = true; + } + } + else if(!args[i].empty() && args[i][0] == '-') + { + // + // Short option. + // + for(string::size_type p = 1; p < args[i].size(); ++p) + { + opt.clear(); + opt.push_back(args[i][p]); + pos = checkOpt(opt, ShortOpt); + + if(pos->second->repeat == NoRepeat) + { + set<string>::iterator seenPos = seenNonRepeatableOpts.find(opt); + if(seenPos != seenNonRepeatableOpts.end()) + { + string err = "`-"; + err += opt + ":' option cannot be repeated"; + throw BadOptException(__FILE__, __LINE__, err); + } + seenNonRepeatableOpts.insert(seenPos, opt); + string synonym = getSynonym(opt); + if(!synonym.empty()) + { + seenNonRepeatableOpts.insert(synonym); + } + } + + if(pos->second->arg == NeedArg && p != args[i].size() - 1) + { + string optArg = args[i].substr(p + 1); + setOpt(opt, "", optArg, pos->second->repeat); + argDone = true; + break; + } + } + } + else + { + // + // Not an option or option argument. + // + result.push_back(args[i]); + argDone = true; + } + + if(!argDone) + { + if(pos->second->arg == NeedArg) // Need an argument that is separated by whitespace. + { + if(i == args.size() - 1) + { + string err = "`-"; + if(opt.size() != 1) + { + err += "-"; + } + err += opt; + err += "' option requires an argument"; + throw BadOptException(__FILE__, __LINE__, err); + } + setOpt(opt, "", args[++i], pos->second->repeat); + } + else + { + setOpt(opt, "", "1", pos->second->repeat); + } + } } _synonyms.clear(); // Don't need the contents anymore. @@ -765,7 +765,7 @@ IceUtil::Options::isSet(const string& opt) const if(!parseCalled) { - throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); + throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); } ValidOpts::const_iterator pos = checkOptIsValid(opt); @@ -779,21 +779,21 @@ IceUtil::Options::optArg(const string& opt) const if(!parseCalled) { - throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); + throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); } ValidOpts::const_iterator pos = checkOptHasArg(opt); if(pos->second->repeat == Repeat) { - string err = "`-"; - if(pos->second->length == LongOpt) - { - err.push_back('-'); - } - err += opt; - err += "': is a repeating option -- use argVec() to get its arguments"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`-"; + if(pos->second->length == LongOpt) + { + err.push_back('-'); + } + err += opt; + err += "': is a repeating option -- use argVec() to get its arguments"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } Opts::const_iterator p = _opts.find(opt); @@ -811,20 +811,20 @@ IceUtil::Options::argVec(const string& opt) const if(!parseCalled) { - throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); + throw APIException(__FILE__, __LINE__, "cannot lookup options before calling parse()"); } ValidOpts::const_iterator pos = checkOptHasArg(opt); if(pos->second->repeat == NoRepeat) { - string err = "`-"; - if(pos->second->length == LongOpt) - { - err.push_back('-'); - } - err += opt + "': is a non-repeating option -- use optArg() to get its argument"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`-"; + if(pos->second->length == LongOpt) + { + err.push_back('-'); + } + err += opt + "': is a non-repeating option -- use optArg() to get its argument"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } ROpts::const_iterator p = _ropts.find(opt); @@ -837,17 +837,17 @@ IceUtil::Options::addValidOpt(const string& shortOpt, const string& longOpt, { if(!shortOpt.empty() && _validOpts.find(shortOpt) != _validOpts.end()) { - string err = "`"; - err += shortOpt; - err += "': duplicate option"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`"; + err += shortOpt; + err += "': duplicate option"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } if(!longOpt.empty() && _validOpts.find(longOpt) != _validOpts.end()) { - string err = "`"; - err += longOpt; - err += "': duplicate option"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`"; + err += longOpt; + err += "': duplicate option"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } ODPtr odp = new OptionDetails; @@ -857,12 +857,12 @@ IceUtil::Options::addValidOpt(const string& shortOpt, const string& longOpt, if(!shortOpt.empty()) { - odp->length = ShortOpt; - _validOpts[shortOpt] = odp; + odp->length = ShortOpt; + _validOpts[shortOpt] = odp; } if(!longOpt.empty()) { - odp->length = LongOpt; + odp->length = LongOpt; _validOpts[longOpt] = odp; } @@ -870,7 +870,7 @@ IceUtil::Options::addValidOpt(const string& shortOpt, const string& longOpt, if(at == NeedArg && !dflt.empty()) { - setOpt(shortOpt, longOpt, dflt, rt); + setOpt(shortOpt, longOpt, dflt, rt); } } @@ -880,14 +880,14 @@ IceUtil::Options::checkOpt(const string& opt, LengthType lt) ValidOpts::iterator pos = _validOpts.find(opt); if(pos == _validOpts.end()) { - string err = "invalid option: `-"; - if(lt == LongOpt) - { - err.push_back('-'); - } - err += opt; - err.push_back('\''); - throw BadOptException(__FILE__, __LINE__, err); + string err = "invalid option: `-"; + if(lt == LongOpt) + { + err.push_back('-'); + } + err += opt; + err.push_back('\''); + throw BadOptException(__FILE__, __LINE__, err); } return pos; } @@ -902,13 +902,13 @@ IceUtil::Options::setOpt(const string& opt1, const string& opt2, const string& v if(rt == NoRepeat) { - setNonRepeatingOpt(opt1, val); - setNonRepeatingOpt(opt2, val); + setNonRepeatingOpt(opt1, val); + setNonRepeatingOpt(opt2, val); } else { - setRepeatingOpt(opt1, val); - setRepeatingOpt(opt2, val); + setRepeatingOpt(opt1, val); + setRepeatingOpt(opt2, val); } } @@ -933,7 +933,7 @@ IceUtil::Options::setNonRepeatingOpt(const string& opt, const string& val) const string synonym = getSynonym(opt); if(!synonym.empty()) { - _opts[synonym] = ovp; + _opts[synonym] = ovp; } } @@ -954,45 +954,45 @@ IceUtil::Options::setRepeatingOpt(const string& opt, const string& val) if(pos != _ropts.end()) { - assert(_validOpts.find(opt) != _validOpts.end()); - assert(vpos->second->repeat == Repeat); - - _ropts[opt] = pos->second; - if(vpos->second->hasDefault && pos->second->vals.size() == 1) - { - pos->second->vals[0] = val; - vpos->second->hasDefault = false; - } - else - { - pos->second->vals.push_back(val); - } + assert(_validOpts.find(opt) != _validOpts.end()); + assert(vpos->second->repeat == Repeat); + + _ropts[opt] = pos->second; + if(vpos->second->hasDefault && pos->second->vals.size() == 1) + { + pos->second->vals[0] = val; + vpos->second->hasDefault = false; + } + else + { + pos->second->vals.push_back(val); + } } else if(spos != _ropts.end()) { - assert(_validOpts.find(synonym) != _validOpts.end()); - assert(_validOpts.find(synonym)->second->repeat == Repeat); - - _ropts[synonym] = spos->second; - if(vpos->second->hasDefault && spos->second->vals.size() == 1) - { - spos->second->vals[0] = val; - vpos->second->hasDefault = false; - } - else - { - spos->second->vals.push_back(val); - } + assert(_validOpts.find(synonym) != _validOpts.end()); + assert(_validOpts.find(synonym)->second->repeat == Repeat); + + _ropts[synonym] = spos->second; + if(vpos->second->hasDefault && spos->second->vals.size() == 1) + { + spos->second->vals[0] = val; + vpos->second->hasDefault = false; + } + else + { + spos->second->vals.push_back(val); + } } else { - OVecPtr ovp = new OptionValueVector; - ovp->vals.push_back(val); - _ropts[opt] = ovp; - if(!synonym.empty()) - { - _ropts[synonym] = ovp; - } + OVecPtr ovp = new OptionValueVector; + ovp->vals.push_back(val); + _ropts[opt] = ovp; + if(!synonym.empty()) + { + _ropts[synonym] = ovp; + } } } @@ -1002,10 +1002,10 @@ IceUtil::Options::checkOptIsValid(const string& opt) const ValidOpts::const_iterator pos = _validOpts.find(opt); if(pos == _validOpts.end()) { - string err = "`"; - err += opt; - err += "': invalid option"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`"; + err += opt; + err += "': invalid option"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } return pos; } @@ -1016,14 +1016,14 @@ IceUtil::Options::checkOptHasArg(const string& opt) const ValidOpts::const_iterator pos = checkOptIsValid(opt); if(pos->second->arg == NoArg) { - string err = "`-"; - if(pos->second->length == LongOpt) - { - err.push_back('-'); - } - err += opt; - err += "': option does not take arguments"; - throw IllegalArgumentException(__FILE__, __LINE__, err); + string err = "`-"; + if(pos->second->length == LongOpt) + { + err.push_back('-'); + } + err += opt; + err += "': option does not take arguments"; + throw IllegalArgumentException(__FILE__, __LINE__, err); } return pos; } @@ -1034,7 +1034,7 @@ IceUtil::Options::updateSynonyms(const ::std::string& shortOpt, const ::std::str if(!shortOpt.empty() && !longOpt.empty()) { _synonyms[shortOpt] = longOpt; - _synonyms[longOpt] = shortOpt; + _synonyms[longOpt] = shortOpt; } } diff --git a/cpp/src/IceUtil/OutputUtil.cpp b/cpp/src/IceUtil/OutputUtil.cpp index acf1a7a4590..e3ef63d1a45 100644 --- a/cpp/src/IceUtil/OutputUtil.cpp +++ b/cpp/src/IceUtil/OutputUtil.cpp @@ -84,14 +84,14 @@ IceUtil::OutputBase::print(const char* s) { for(unsigned int i = 0; i < strlen(s); ++i) { - if(s[i] == '\n') - { - _pos = 0; - } - else - { - ++_pos; - } + if(s[i] == '\n') + { + _pos = 0; + } + else + { + ++_pos; + } } _out << s; @@ -236,10 +236,10 @@ IceUtil::Output::print(const char* s) { if(_par >= 0) { - if(++_par > 1) // No comma for the first parameter. - { - _out << ", "; - } + if(++_par > 1) // No comma for the first parameter. + { + _out << ", "; + } } OutputBase::print(s); } @@ -337,19 +337,19 @@ IceUtil::XMLOutput::print(const char* s) { if(_se) { - _out << '>'; - _se = false; + _out << '>'; + _se = false; } _text = true; if(_escape) { - string escaped = escape(s); - OutputBase::print(escaped.c_str()); + string escaped = escape(s); + OutputBase::print(escaped.c_str()); } else { - OutputBase::print(s); + OutputBase::print(s); } } @@ -358,8 +358,8 @@ IceUtil::XMLOutput::newline() { if(_se) { - _se = false; - _out << '>'; + _se = false; + _out << '>'; } OutputBase::newline(); } @@ -376,11 +376,11 @@ IceUtil::XMLOutput::startElement(const string& element) // if(_escape) { - _out << '<' << escape(element); + _out << '<' << escape(element); } else { - _out << '<' << element; + _out << '<' << element; } _se = true; _text = false; @@ -388,11 +388,11 @@ IceUtil::XMLOutput::startElement(const string& element) string::size_type pos = element.find_first_of(" \t"); if(pos == string::npos) { - _elementStack.push(element); + _elementStack.push(element); } else { - _elementStack.push(element.substr(0, pos)); + _elementStack.push(element.substr(0, pos)); } ++_pos; // TODO: ??? @@ -409,15 +409,15 @@ IceUtil::XMLOutput::endElement() dec(); if(_se) { - _out << "></" << element << '>'; + _out << "></" << element << '>'; } else { - if(!_text) - { - newline(); - } - _out << "</" << element << '>'; + if(!_text) + { + newline(); + } + _out << "</" << element << '>'; } --_pos; // TODO: ??? @@ -472,50 +472,50 @@ IceUtil::XMLOutput::escape(const string& input) const const string allReserved = "<>'\"&"; if(v.find_first_of(allReserved) != string::npos) { - // - // First convert all & to & - // - size_t pos = 0; - while((pos = v.find_first_of('&', pos)) != string::npos) - { - v.insert(pos+1, "amp;"); - pos += 4; - } - - // - // Next convert remaining reserved characters. - // - const string reserved = "<>'\""; - pos = 0; - while((pos = v.find_first_of(reserved, pos)) != string::npos) - { - string replace; - switch(v[pos]) - { - case '>': - replace = ">"; - break; - - case '<': - replace = "<"; - break; - - case '\'': - replace = "'"; - break; - - case '"': - replace = """; - break; - - default: - assert(false); - } - - v.erase(pos, 1); - v.insert(pos, replace); - pos += replace.size(); - } + // + // First convert all & to & + // + size_t pos = 0; + while((pos = v.find_first_of('&', pos)) != string::npos) + { + v.insert(pos+1, "amp;"); + pos += 4; + } + + // + // Next convert remaining reserved characters. + // + const string reserved = "<>'\""; + pos = 0; + while((pos = v.find_first_of(reserved, pos)) != string::npos) + { + string replace; + switch(v[pos]) + { + case '>': + replace = ">"; + break; + + case '<': + replace = "<"; + break; + + case '\'': + replace = "'"; + break; + + case '"': + replace = """; + break; + + default: + assert(false); + } + + v.erase(pos, 1); + v.insert(pos, replace); + pos += replace.size(); + } } return v; } diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp index 8e78db985a3..d15d4acd98c 100644 --- a/cpp/src/IceUtil/RWRecMutex.cpp +++ b/cpp/src/IceUtil/RWRecMutex.cpp @@ -58,7 +58,7 @@ IceUtil::RWRecMutex::readLock() const // while(_count < 0 || _waitingWriters != 0) { - _readers.wait(lock); + _readers.wait(lock); } ++_count; } @@ -74,7 +74,7 @@ IceUtil::RWRecMutex::tryReadLock() const // if(_count < 0 || _waitingWriters != 0) { - return false; + return false; } ++_count; return true; @@ -92,18 +92,18 @@ IceUtil::RWRecMutex::timedReadLock(const Time& timeout) const Time end = Time::now() + timeout; while(_count < 0 || _waitingWriters != 0) { - Time remainder = end - Time::now(); - if(remainder > Time()) - { - if(_readers.timedWait(lock, remainder) == false) - { - return false; - } - } - else - { - return false; - } + Time remainder = end - Time::now(); + if(remainder > Time()) + { + if(_readers.timedWait(lock, remainder) == false) + { + return false; + } + } + else + { + return false; + } } ++_count; @@ -121,8 +121,8 @@ IceUtil::RWRecMutex::writeLock() const // if(_count < 0 && _writerId == ThreadControl()) { - --_count; - return; + --_count; + return; } // @@ -131,17 +131,17 @@ IceUtil::RWRecMutex::writeLock() const // while(_count != 0) { - ++_waitingWriters; - try - { - _writers.wait(lock); - } - catch(...) - { - --_waitingWriters; - throw; - } - --_waitingWriters; + ++_waitingWriters; + try + { + _writers.wait(lock); + } + catch(...) + { + --_waitingWriters; + throw; + } + --_waitingWriters; } // @@ -162,8 +162,8 @@ IceUtil::RWRecMutex::tryWriteLock() const // if(_count < 0 && _writerId == ThreadControl()) { - --_count; - return true; + --_count; + return true; } // @@ -171,7 +171,7 @@ IceUtil::RWRecMutex::tryWriteLock() const // if(_count != 0) { - return false; + return false; } // @@ -192,8 +192,8 @@ IceUtil::RWRecMutex::timedWriteLock(const Time& timeout) const // decrement _count, and return. if(_count < 0 && _writerId == ThreadControl()) { - --_count; - return true; + --_count; + return true; } // @@ -203,29 +203,29 @@ IceUtil::RWRecMutex::timedWriteLock(const Time& timeout) const Time end = Time::now() + timeout; while(_count != 0) { - Time remainder = end - Time::now(); - if(remainder > Time()) - { - ++_waitingWriters; - try - { - bool result = _writers.timedWait(lock, remainder); - --_waitingWriters; - if(result == false) - { - return false; - } - } - catch(...) - { - --_waitingWriters; - throw; - } - } - else - { - return false; - } + Time remainder = end - Time::now(); + if(remainder > Time()) + { + ++_waitingWriters; + try + { + bool result = _writers.timedWait(lock, remainder); + --_waitingWriters; + if(result == false) + { + return false; + } + } + catch(...) + { + --_waitingWriters; + throw; + } + } + else + { + return false; + } } // @@ -242,46 +242,46 @@ IceUtil::RWRecMutex::unlock() const bool ww = false; bool wr = false; { - Mutex::Lock lock(_mutex); - - assert(_count != 0); - - // - // If _count < 0, the calling thread is a writer that holds the - // lock, so release the lock. Otherwise, _count is guaranteed to - // be > 0, so the calling thread is a reader releasing the lock. - // - if(_count < 0) - { - // - // Writer called unlock - // - ++_count; - - // - // If the write lock wasn't totally released we're done. - // - if(_count != 0) - { - return; - } - } - else - { - // - // Reader called unlock - // - --_count; - } - - // - // Writers are waiting (ww) if _waitingWriters > 0. In that - // case, it's OK to let another writer into the region once there - // are no more readers (_count == 0). Otherwise, no writers are - // waiting but readers may be waiting (wr). - // - ww = (_waitingWriters != 0 && _count == 0); - wr = (_waitingWriters == 0); + Mutex::Lock lock(_mutex); + + assert(_count != 0); + + // + // If _count < 0, the calling thread is a writer that holds the + // lock, so release the lock. Otherwise, _count is guaranteed to + // be > 0, so the calling thread is a reader releasing the lock. + // + if(_count < 0) + { + // + // Writer called unlock + // + ++_count; + + // + // If the write lock wasn't totally released we're done. + // + if(_count != 0) + { + return; + } + } + else + { + // + // Reader called unlock + // + --_count; + } + + // + // Writers are waiting (ww) if _waitingWriters > 0. In that + // case, it's OK to let another writer into the region once there + // are no more readers (_count == 0). Otherwise, no writers are + // waiting but readers may be waiting (wr). + // + ww = (_waitingWriters != 0 && _count == 0); + wr = (_waitingWriters == 0); } // Unlock mutex. // @@ -290,27 +290,27 @@ IceUtil::RWRecMutex::unlock() const // if(ww) { - if(_upgrading) - { - // - // If there is an untimed upgrader, it runs. - // - _upgrader.signal(); - } - else - { - // - // Wake a normal writer. - // - _writers.signal(); - } + if(_upgrading) + { + // + // If there is an untimed upgrader, it runs. + // + _upgrader.signal(); + } + else + { + // + // Wake a normal writer. + // + _writers.signal(); + } } else if(wr) { - // - // Wake readers - // - _readers.broadcast(); + // + // Wake readers + // + _readers.broadcast(); } } @@ -336,19 +336,19 @@ IceUtil::RWRecMutex::upgrade() const _upgrading = true; while(_count != 0) { - ++_waitingWriters; - try - { - _upgrader.wait(lock); - } - catch(...) - { - _upgrading = false; - --_waitingWriters; - ++_count; - throw; - } - --_waitingWriters; + ++_waitingWriters; + try + { + _upgrader.wait(lock); + } + catch(...) + { + _upgrading = false; + --_waitingWriters; + ++_count; + throw; + } + --_waitingWriters; } // @@ -385,38 +385,38 @@ IceUtil::RWRecMutex::timedUpgrade(const Time& timeout) const Time end = Time::now() + timeout; while(_count != 0) { - Time remainder = end - Time::now(); - if(remainder > Time()) - { - ++_waitingWriters; - try - { - bool result = _upgrader.timedWait(lock, remainder); - --_waitingWriters; - if(!result) - { - _upgrading = false; - ++_count; - return false; - } - } - catch(...) - { - _upgrading = false; - --_waitingWriters; - ++_count; - throw; - } - } - else - { - // - // The lock isn't acquired if a timeout occurred. - // - ++_count; - _upgrading = false; - return false; - } + Time remainder = end - Time::now(); + if(remainder > Time()) + { + ++_waitingWriters; + try + { + bool result = _upgrader.timedWait(lock, remainder); + --_waitingWriters; + if(!result) + { + _upgrading = false; + ++_count; + return false; + } + } + catch(...) + { + _upgrading = false; + --_waitingWriters; + ++_count; + throw; + } + } + else + { + // + // The lock isn't acquired if a timeout occurred. + // + ++_count; + _upgrading = false; + return false; + } } // @@ -435,6 +435,6 @@ IceUtil::RWRecMutex::downgrade() const if(++_count == 0) { - _count = 1; + _count = 1; } } diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp index f532d64d35e..bbe8b523198 100644 --- a/cpp/src/IceUtil/Random.cpp +++ b/cpp/src/IceUtil/Random.cpp @@ -60,17 +60,17 @@ public: ~RandomCleanup() { #ifdef _WIN32 - if(context != NULL) - { - CryptReleaseContext(context, 0); - context = NULL; - } + if(context != NULL) + { + CryptReleaseContext(context, 0); + context = NULL; + } #else - if(fd != -1) - { - close(fd); - fd = -1; - } + if(fd != -1) + { + close(fd); + fd = -1; + } #endif } }; @@ -89,30 +89,30 @@ IceUtil::RandomGeneratorException::ice_print(ostream& os) const Exception::ice_print(os); if(_error != 0) { - os << ":\nrandom generator exception: "; + os << ":\nrandom generator exception: "; #ifdef _WIN32 - LPVOID lpMsgBuf = 0; - DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - _error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - if(ok) - { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((char*)msg) > 0); - os << msg; - LocalFree(lpMsgBuf); - } - else - { - os << "unknown random generator error"; - } + LPVOID lpMsgBuf = 0; + DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + _error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, + 0, + NULL); + + if(ok) + { + LPCTSTR msg = (LPCTSTR)lpMsgBuf; + assert(msg && strlen((char*)msg) > 0); + os << msg; + LocalFree(lpMsgBuf); + } + else + { + os << "unknown random generator error"; + } #else os << strerror(_error); #endif @@ -145,15 +145,15 @@ IceUtil::generateRandom(char* buffer, int size) IceUtil::StaticMutex::Lock lock(staticMutex); if(context == NULL) { - if(!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); - } + if(!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + { + throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + } } if(!CryptGenRandom(context, size, reinterpret_cast<unsigned char*>(buffer))) { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); } #else @@ -163,12 +163,12 @@ IceUtil::generateRandom(char* buffer, int size) IceUtil::StaticMutex::Lock lock(staticMutex); if(fd == -1) { - fd = open("/dev/urandom", O_RDONLY); - if(fd == -1) - { - assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); - } + fd = open("/dev/urandom", O_RDONLY); + if(fd == -1) + { + assert(0); + throw RandomGeneratorException(__FILE__, __LINE__); + } } // @@ -179,26 +179,26 @@ IceUtil::generateRandom(char* buffer, int size) size_t index = 0; while(reads <= 20 && index != static_cast<size_t>(size)) { - ssize_t bytesRead = read(fd, buffer + index, static_cast<size_t>(size) - index); - - if(bytesRead == -1 && errno != EINTR) - { - int err = errno; - cerr << "Reading /dev/urandom returned " << strerror(err) << endl; - assert(0); - throw RandomGeneratorException(__FILE__, __LINE__, errno); - } - else - { - index += bytesRead; - reads++; - } + ssize_t bytesRead = read(fd, buffer + index, static_cast<size_t>(size) - index); + + if(bytesRead == -1 && errno != EINTR) + { + int err = errno; + cerr << "Reading /dev/urandom returned " << strerror(err) << endl; + assert(0); + throw RandomGeneratorException(__FILE__, __LINE__, errno); + } + else + { + index += bytesRead; + reads++; + } } - + if(index != static_cast<size_t>(size)) { - assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); + assert(0); + throw RandomGeneratorException(__FILE__, __LINE__); } #endif } @@ -210,11 +210,11 @@ IceUtil::random(int limit) generateRandom(reinterpret_cast<char*>(&r), static_cast<int>(sizeof(int))); if(limit > 0) { - r = r % limit; + r = r % limit; } if(r < 0) { - r = -r; + r = -r; } return r; } diff --git a/cpp/src/IceUtil/RecMutex.cpp b/cpp/src/IceUtil/RecMutex.cpp index 1682917b2e0..4571750e030 100644 --- a/cpp/src/IceUtil/RecMutex.cpp +++ b/cpp/src/IceUtil/RecMutex.cpp @@ -34,7 +34,7 @@ IceUtil::RecMutex::lock() const EnterCriticalSection(&_mutex); if(++_count > 1) { - LeaveCriticalSection(&_mutex); + LeaveCriticalSection(&_mutex); } } @@ -43,11 +43,11 @@ IceUtil::RecMutex::tryLock() const { if(!TryEnterCriticalSection(&_mutex)) { - return false; + return false; } if(++_count > 1) { - LeaveCriticalSection(&_mutex); + LeaveCriticalSection(&_mutex); } return true; } @@ -57,7 +57,7 @@ IceUtil::RecMutex::unlock() const { if(--_count == 0) { - LeaveCriticalSection(&_mutex); + LeaveCriticalSection(&_mutex); } } @@ -84,7 +84,7 @@ IceUtil::RecMutex::RecMutex() : _mutex = CreateMutex(0, false, 0); if(_mutex == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -94,7 +94,7 @@ IceUtil::RecMutex::~RecMutex() BOOL rc = CloseHandle(_mutex); if(rc == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -104,23 +104,23 @@ IceUtil::RecMutex::lock() const DWORD rc = WaitForSingleObject(_mutex, INFINITE); if(rc != WAIT_OBJECT_0) { - if(rc == WAIT_FAILED) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } - else - { - throw ThreadSyscallException(__FILE__, __LINE__, 0); - } + if(rc == WAIT_FAILED) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, 0); + } } if(++_count > 1) { - BOOL rc2 = ReleaseMutex(_mutex); - if(rc2 == 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + BOOL rc2 = ReleaseMutex(_mutex); + if(rc2 == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } } } @@ -130,15 +130,15 @@ IceUtil::RecMutex::tryLock() const DWORD rc = WaitForSingleObject(_mutex, 0); if(rc != WAIT_OBJECT_0) { - return false; + return false; } if(++_count > 1) { - BOOL rc2 = ReleaseMutex(_mutex); - if(rc2 == 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + BOOL rc2 = ReleaseMutex(_mutex); + if(rc2 == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } } return true; } @@ -148,11 +148,11 @@ IceUtil::RecMutex::unlock() const { if(--_count == 0) { - BOOL rc = ReleaseMutex(_mutex); - if(rc == 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + BOOL rc = ReleaseMutex(_mutex); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } } } @@ -164,7 +164,7 @@ IceUtil::RecMutex::unlock(LockState& state) const BOOL rc = ReleaseMutex(_mutex); if(rc == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -174,14 +174,14 @@ IceUtil::RecMutex::lock(LockState& state) const DWORD rc = WaitForSingleObject(_mutex, INFINITE); if(rc != WAIT_OBJECT_0) { - if(rc == WAIT_FAILED) - { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } - else - { - throw ThreadSyscallException(__FILE__, __LINE__, 0); - } + if(rc == WAIT_FAILED) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, 0); + } } _count = state.count; @@ -203,19 +203,19 @@ IceUtil::RecMutex::RecMutex() : rc = pthread_mutexattr_init(&attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } #endif rc = pthread_mutex_init(&_mutex, &attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } #if defined(__linux) && !defined(__USE_UNIX98) @@ -224,7 +224,7 @@ IceUtil::RecMutex::RecMutex() : rc = pthread_mutexattr_destroy(&attr); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } #endif } @@ -243,12 +243,12 @@ IceUtil::RecMutex::lock() const int rc = pthread_mutex_lock(&_mutex); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } if(++_count > 1) { - rc = pthread_mutex_unlock(&_mutex); - assert(rc == 0); + rc = pthread_mutex_unlock(&_mutex); + assert(rc == 0); } } @@ -259,18 +259,18 @@ IceUtil::RecMutex::tryLock() const bool result = (rc == 0); if(!result) { - if(rc != EBUSY) - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + if(rc != EBUSY) + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } else if(++_count > 1) { - rc = pthread_mutex_unlock(&_mutex); - if(rc != 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + rc = pthread_mutex_unlock(&_mutex); + if(rc != 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } return result; } @@ -280,9 +280,9 @@ IceUtil::RecMutex::unlock() const { if(--_count == 0) { - int rc = 0; // Prevent warnings when NDEBUG is defined. - rc = pthread_mutex_unlock(&_mutex); - assert(rc == 0); + int rc = 0; // Prevent warnings when NDEBUG is defined. + rc = pthread_mutex_unlock(&_mutex); + assert(rc == 0); } } diff --git a/cpp/src/IceUtil/StaticMutex.cpp b/cpp/src/IceUtil/StaticMutex.cpp index 857147a0496..f573bbbd4b9 100644 --- a/cpp/src/IceUtil/StaticMutex.cpp +++ b/cpp/src/IceUtil/StaticMutex.cpp @@ -55,14 +55,14 @@ Init::~Init() { # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 for(MutexList::iterator p = _mutexList->begin(); - p != _mutexList->end(); ++p) + p != _mutexList->end(); ++p) { - DeleteCriticalSection(*p); - delete *p; + DeleteCriticalSection(*p); + delete *p; } # else for_each(_mutexList->begin(), _mutexList->end(), - CloseHandle); + CloseHandle); # endif delete _mutexList; DeleteCriticalSection(&_criticalSection); @@ -83,25 +83,25 @@ void IceUtil::StaticMutex::initialize() const if(_mutex == 0) { # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 - CRITICAL_SECTION* newMutex = new CRITICAL_SECTION; - InitializeCriticalSection(newMutex); + CRITICAL_SECTION* newMutex = new CRITICAL_SECTION; + InitializeCriticalSection(newMutex); # else - _recursionCount = 0; - - HANDLE newMutex = CreateMutex(0, false, 0); - if(newMutex == 0) - { - LeaveCriticalSection(&_criticalSection); - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } + _recursionCount = 0; + + HANDLE newMutex = CreateMutex(0, false, 0); + if(newMutex == 0) + { + LeaveCriticalSection(&_criticalSection); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } # endif - // - // _mutex is written after the new initialized CRITICAL_SECTION/Mutex - // - void* oldVal = InterlockedCompareExchangePointer(reinterpret_cast<void**>(&_mutex), newMutex, 0); - assert(oldVal == 0); - _mutexList->push_back(_mutex); + // + // _mutex is written after the new initialized CRITICAL_SECTION/Mutex + // + void* oldVal = InterlockedCompareExchangePointer(reinterpret_cast<void**>(&_mutex), newMutex, 0); + assert(oldVal == 0); + _mutexList->push_back(_mutex); } LeaveCriticalSection(&_criticalSection); diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp index 12ce3e3cd1e..30029300f59 100644 --- a/cpp/src/IceUtil/StringUtil.cpp +++ b/cpp/src/IceUtil/StringUtil.cpp @@ -42,86 +42,86 @@ encodeChar(string::value_type b, string& s, const string& special) { switch(b) { - case '\\': - { - s.append("\\\\"); - break; - } - - case '\'': - { - s.append("\\'"); - break; - } - - case '"': - { - s.append("\\\""); - break; - } - - case '\b': - { - s.append("\\b"); - break; - } - - case '\f': - { - s.append("\\f"); - break; - } - - case '\n': - { - s.append("\\n"); - break; - } - - case '\r': - { - s.append("\\r"); - break; - } - - case '\t': - { - s.append("\\t"); - break; - } - - default: - { - unsigned char i = static_cast<unsigned char>(b); - if(!(i >= 32 && i <= 126)) - { - s.push_back('\\'); - string octal = toOctalString(i); - // - // Add leading zeroes so that we avoid problems during - // decoding. For example, consider the escaped string - // \0013 (i.e., a character with value 1 followed by the - // character '3'). If the leading zeroes were omitted, the - // result would be incorrectly interpreted as a single - // character with value 11. - // - for(string::size_type j = octal.size(); j < 3; j++) - { - s.push_back('0'); - } - s.append(octal); - } - else if(special.find(b) != string::npos) - { - s.push_back('\\'); - s.push_back(b); - } - else - { - s.push_back(b); - } - break; - } + case '\\': + { + s.append("\\\\"); + break; + } + + case '\'': + { + s.append("\\'"); + break; + } + + case '"': + { + s.append("\\\""); + break; + } + + case '\b': + { + s.append("\\b"); + break; + } + + case '\f': + { + s.append("\\f"); + break; + } + + case '\n': + { + s.append("\\n"); + break; + } + + case '\r': + { + s.append("\\r"); + break; + } + + case '\t': + { + s.append("\\t"); + break; + } + + default: + { + unsigned char i = static_cast<unsigned char>(b); + if(!(i >= 32 && i <= 126)) + { + s.push_back('\\'); + string octal = toOctalString(i); + // + // Add leading zeroes so that we avoid problems during + // decoding. For example, consider the escaped string + // \0013 (i.e., a character with value 1 followed by the + // character '3'). If the leading zeroes were omitted, the + // result would be incorrectly interpreted as a single + // character with value 11. + // + for(string::size_type j = octal.size(); j < 3; j++) + { + s.push_back('0'); + } + s.append(octal); + } + else if(special.find(b) != string::npos) + { + s.push_back('\\'); + s.push_back(b); + } + else + { + s.push_back(b); + } + break; + } } } @@ -136,16 +136,16 @@ IceUtil::escapeString(const string& s, const string& special) string::size_type i; for(i = 0; i < special.size(); ++i) { - if(static_cast<unsigned char>(special[i]) < 32 || static_cast<unsigned char>(special[i]) > 126) - { - throw IllegalArgumentException(__FILE__, __LINE__, "special characters must be in ASCII range 32-126"); - } + if(static_cast<unsigned char>(special[i]) < 32 || static_cast<unsigned char>(special[i]) > 126) + { + throw IllegalArgumentException(__FILE__, __LINE__, "special characters must be in ASCII range 32-126"); + } } string result; for(i = 0; i < s.size(); ++i) { - encodeChar(s[i], result, special); + encodeChar(s[i], result, special); } return result; @@ -178,86 +178,86 @@ decodeChar(const string& s, string::size_type start, string::size_type end, stri if(s[start] != '\\') { - c = checkChar(s[start++]); + c = checkChar(s[start++]); } else { - if(start + 1 == end) - { - throw IllegalArgumentException(__FILE__, __LINE__, "trailing backslash in argument"); - } - switch(s[++start]) - { - case '\\': - case '\'': - case '"': - { - c = s[start++]; - break; - } - case 'b': - { - ++start; - c = '\b'; - break; - } - case 'f': - { - ++start; - c = '\f'; - break; - } - case 'n': - { - ++start; - c = '\n'; - break; - } - case 'r': - { - ++start; - c = '\r'; - break; - } - case 't': - { - ++start; - c = '\t'; - break; - } - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - { - int oct = 0; - for(int j = 0; j < 3 && start < end; ++j) - { - int charVal = s[start++] - '0'; - if(charVal < 0 || charVal > 7) - { - --start; - break; - } - oct = oct * 8 + charVal; - } - if(oct > 255) - { - throw IllegalArgumentException(__FILE__, __LINE__, "octal value out of range"); - } - c = (char)oct; - break; - } - default: - { - c = checkChar(s[start++]); - break; - } - } + if(start + 1 == end) + { + throw IllegalArgumentException(__FILE__, __LINE__, "trailing backslash in argument"); + } + switch(s[++start]) + { + case '\\': + case '\'': + case '"': + { + c = s[start++]; + break; + } + case 'b': + { + ++start; + c = '\b'; + break; + } + case 'f': + { + ++start; + c = '\f'; + break; + } + case 'n': + { + ++start; + c = '\n'; + break; + } + case 'r': + { + ++start; + c = '\r'; + break; + } + case 't': + { + ++start; + c = '\t'; + break; + } + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + { + int oct = 0; + for(int j = 0; j < 3 && start < end; ++j) + { + int charVal = s[start++] - '0'; + if(charVal < 0 || charVal > 7) + { + --start; + break; + } + oct = oct * 8 + charVal; + } + if(oct > 255) + { + throw IllegalArgumentException(__FILE__, __LINE__, "octal value out of range"); + } + c = (char)oct; + break; + } + default: + { + c = checkChar(s[start++]); + break; + } + } } nextStart = start; return c; @@ -271,7 +271,7 @@ static void decodeString(const string& s, string::size_type start, string::size_ { while(start < end) { - sb.push_back(decodeChar(s, start, end, start)); + sb.push_back(decodeChar(s, start, end, start)); } } @@ -291,20 +291,20 @@ IceUtil::unescapeString(const string& s, string::size_type start, string::size_t } if(start > end) { - throw IllegalArgumentException(__FILE__, __LINE__, "start offset must <= end offset"); + throw IllegalArgumentException(__FILE__, __LINE__, "start offset must <= end offset"); } result.reserve(end - start); try { - result.clear(); - decodeString(s, start, end, result); - return true; + result.clear(); + decodeString(s, start, end, result); + return true; } catch(...) { - return false; + return false; } } @@ -320,17 +320,17 @@ IceUtil::checkQuote(const string& s, string::size_type start) string::value_type quoteChar = s[start]; if(quoteChar == '"' || quoteChar == '\'') { - start++; - string::size_type pos; - while(start < s.size() && (pos = s.find(quoteChar, start)) != string::npos) - { - if(s[pos - 1] != '\\') - { - return pos; - } - start = pos + 1; - } - return string::npos; // Unmatched quote. + start++; + string::size_type pos; + while(start < s.size() && (pos = s.find(quoteChar, start)) != string::npos) + { + if(s[pos - 1] != '\\') + { + return pos; + } + start = pos + 1; + } + return string::npos; // Unmatched quote. } return 0; // Not quoted. } diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp index 86658d581b4..70c4aa8b52c 100644 --- a/cpp/src/IceUtil/Thread.cpp +++ b/cpp/src/IceUtil/Thread.cpp @@ -44,13 +44,13 @@ IceUtil::ThreadControl::join() { if(_handle == 0) { - throw BadThreadControlException(__FILE__, __LINE__); + throw BadThreadControlException(__FILE__, __LINE__); } int rc = WaitForSingleObject(_handle, INFINITE); if(rc != WAIT_OBJECT_0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } detach(); @@ -61,12 +61,12 @@ IceUtil::ThreadControl::detach() { if(_handle == 0) { - throw BadThreadControlException(__FILE__, __LINE__); + throw BadThreadControlException(__FILE__, __LINE__); } if(CloseHandle(_handle) == 0) { - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } @@ -115,13 +115,13 @@ WINAPI startHook(void* arg) try { - IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); + IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); - // - // Ensure that the thread doesn't go away until run() has - // completed. - // - thread = rawThread; + // + // Ensure that the thread doesn't go away until run() has + // completed. + // + thread = rawThread; #ifdef _WIN32 // @@ -132,20 +132,20 @@ WINAPI startHook(void* arg) srand(seed ^ thread->getThreadControl().id()); #endif - // - // See the comment in IceUtil::Thread::start() for details. - // - rawThread->__decRef(); - thread->run(); + // + // See the comment in IceUtil::Thread::start() for details. + // + rawThread->__decRef(); + thread->run(); } catch(const IceUtil::Exception& e) { - cerr << "IceUtil::Thread::run(): uncaught exception: "; - cerr << e << endl; + cerr << "IceUtil::Thread::run(): uncaught exception: "; + cerr << e << endl; } catch(...) { - cerr << "IceUtil::Thread::run(): uncaught exception" << endl; + cerr << "IceUtil::Thread::run(): uncaught exception" << endl; } thread->_done(); @@ -166,7 +166,7 @@ IceUtil::Thread::start(size_t stackSize) if(_started) { - throw ThreadStartedException(__FILE__, __LINE__); + throw ThreadStartedException(__FILE__, __LINE__); } // @@ -185,19 +185,19 @@ IceUtil::Thread::start(size_t stackSize) _handle = reinterpret_cast<HANDLE>( _beginthreadex(0, - static_cast<unsigned int>(stackSize), - startHook, this, 0, &id)); + static_cast<unsigned int>(stackSize), + startHook, this, 0, &id)); _id = id; if(_handle == 0) { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } _started = true; _running = true; - + return ThreadControl(_handle, _id); } @@ -207,7 +207,7 @@ IceUtil::Thread::getThreadControl() const IceUtil::Mutex::Lock lock(_stateMutex); if(!_started) { - throw ThreadNotStartedException(__FILE__, __LINE__); + throw ThreadNotStartedException(__FILE__, __LINE__); } return ThreadControl(_handle, _id); } @@ -275,14 +275,14 @@ IceUtil::ThreadControl::join() { if(!_detachable) { - throw BadThreadControlException(__FILE__, __LINE__); + throw BadThreadControlException(__FILE__, __LINE__); } void* ignore = 0; int rc = pthread_join(_thread, &ignore); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } @@ -291,13 +291,13 @@ IceUtil::ThreadControl::detach() { if(!_detachable) { - throw BadThreadControlException(__FILE__, __LINE__); + throw BadThreadControlException(__FILE__, __LINE__); } int rc = pthread_detach(_thread); if(rc != 0) { - throw ThreadSyscallException(__FILE__, __LINE__, rc); + throw ThreadSyscallException(__FILE__, __LINE__, rc); } } @@ -346,24 +346,24 @@ startHook(void* arg) try { - IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); + IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg); - thread = rawThread; + thread = rawThread; - // - // See the comment in IceUtil::Thread::start() for details. - // - rawThread->__decRef(); - thread->run(); + // + // See the comment in IceUtil::Thread::start() for details. + // + rawThread->__decRef(); + thread->run(); } catch(const IceUtil::Exception& e) { - cerr << "IceUtil::Thread::run(): uncaught exception: "; - cerr << e << endl; + cerr << "IceUtil::Thread::run(): uncaught exception: "; + cerr << e << endl; } catch(...) { - cerr << "IceUtil::Thread::run(): uncaught exception" << endl; + cerr << "IceUtil::Thread::run(): uncaught exception" << endl; } thread->_done(); @@ -383,7 +383,7 @@ IceUtil::Thread::start(size_t stackSize) if(_started) { - throw ThreadStartedException(__FILE__, __LINE__); + throw ThreadStartedException(__FILE__, __LINE__); } // @@ -399,34 +399,34 @@ IceUtil::Thread::start(size_t stackSize) if(stackSize > 0) { - pthread_attr_t attr; - int rc = pthread_attr_init(&attr); - if(rc != 0) - { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } - rc = pthread_attr_setstacksize(&attr, stackSize); - if(rc != 0) - { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } - rc = pthread_create(&_thread, &attr, startHook, this); - if(rc != 0) - { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + pthread_attr_t attr; + int rc = pthread_attr_init(&attr); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } + rc = pthread_attr_setstacksize(&attr, stackSize); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } + rc = pthread_create(&_thread, &attr, startHook, this); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } else { - int rc = pthread_create(&_thread, 0, startHook, this); - if(rc != 0) - { - __decRef(); - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } + int rc = pthread_create(&_thread, 0, startHook, this); + if(rc != 0) + { + __decRef(); + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } } _started = true; @@ -441,7 +441,7 @@ IceUtil::Thread::getThreadControl() const IceUtil::Mutex::Lock lock(_stateMutex); if(!_started) { - throw ThreadNotStartedException(__FILE__, __LINE__); + throw ThreadNotStartedException(__FILE__, __LINE__); } return ThreadControl(_thread); } diff --git a/cpp/src/IceUtil/ThreadException.cpp b/cpp/src/IceUtil/ThreadException.cpp index 83f162933e0..4f474723feb 100644 --- a/cpp/src/IceUtil/ThreadException.cpp +++ b/cpp/src/IceUtil/ThreadException.cpp @@ -31,30 +31,30 @@ IceUtil::ThreadSyscallException::ice_print(ostream& os) const Exception::ice_print(os); if(_error != 0) { - os << ":\nthread syscall exception: "; + os << ":\nthread syscall exception: "; #ifdef _WIN32 - LPVOID lpMsgBuf = 0; - DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - _error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - if(ok) - { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((char*)msg) > 0); - os << msg; - LocalFree(lpMsgBuf); - } - else - { - os << "unknown thread error"; - } + LPVOID lpMsgBuf = 0; + DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + _error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, + 0, + NULL); + + if(ok) + { + LPCTSTR msg = (LPCTSTR)lpMsgBuf; + assert(msg && strlen((char*)msg) > 0); + os << msg; + LocalFree(lpMsgBuf); + } + else + { + os << "unknown thread error"; + } #else os << strerror(_error); #endif diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index 1325afe0d42..c39c2bcf53e 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -37,7 +37,7 @@ IceUtil::Time::now() ftime(&tb); # endif return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + - tb.millitm * 1000); + tb.millitm * 1000); #else struct timeval tv; gettimeofday(&tv, 0); diff --git a/cpp/src/IceUtil/UUID.cpp b/cpp/src/IceUtil/UUID.cpp index 7c0eb5c7847..af8253b0991 100644 --- a/cpp/src/IceUtil/UUID.cpp +++ b/cpp/src/IceUtil/UUID.cpp @@ -40,9 +40,9 @@ public: PidInitializer() { - pid_t p = getpid(); - myPid[0] = (p >> 8) & 0x7F; - myPid[1] = p & 0xFF; + pid_t p = getpid(); + myPid[0] = (p >> 8) & 0x7F; + myPid[1] = p & 0xFF; } }; static PidInitializer pidInitializer; @@ -56,11 +56,11 @@ inline void halfByteToHex(unsigned char hb, char*& hexBuffer) { if(hb < 10) { - *hexBuffer++ = '0' + hb; + *hexBuffer++ = '0' + hb; } else { - *hexBuffer++ = 'A' + (hb - 10); + *hexBuffer++ = 'A' + (hb - 10); } } @@ -68,8 +68,8 @@ inline void bytesToHex(unsigned char* bytes, size_t len, char*& hexBuffer) { for(size_t i = 0; i < len; i++) { - halfByteToHex((bytes[i] & 0xF0) >> 4, hexBuffer); - halfByteToHex((bytes[i] & 0x0F), hexBuffer); + halfByteToHex((bytes[i] & 0xF0) >> 4, hexBuffer); + halfByteToHex((bytes[i] & 0x0F), hexBuffer); } } @@ -93,12 +93,12 @@ IceUtil::generateUUID() #else struct UUID { - unsigned char timeLow[4]; - unsigned char timeMid[2]; - unsigned char timeHighAndVersion[2]; - unsigned char clockSeqHiAndReserved; - unsigned char clockSeqLow; - unsigned char node[6]; + unsigned char timeLow[4]; + unsigned char timeMid[2]; + unsigned char timeHighAndVersion[2]; + unsigned char clockSeqHiAndReserved; + unsigned char clockSeqLow; + unsigned char node[6]; }; UUID uuid; diff --git a/cpp/src/IceUtil/Unicode.cpp b/cpp/src/IceUtil/Unicode.cpp index fb7890e2a91..20fc1f89c01 100644 --- a/cpp/src/IceUtil/Unicode.cpp +++ b/cpp/src/IceUtil/Unicode.cpp @@ -23,35 +23,35 @@ template<size_t wcharSize> struct WstringHelper { static ConversionResult toUTF8( - const wchar_t*& sourceStart, const wchar_t* sourceEnd, - Byte*& targetStart, Byte* targetEnd, ConversionFlags flags); + const wchar_t*& sourceStart, const wchar_t* sourceEnd, + Byte*& targetStart, Byte* targetEnd, ConversionFlags flags); static ConversionResult fromUTF8( - const Byte*& sourceStart, const Byte* sourceEnd, - wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags); + const Byte*& sourceStart, const Byte* sourceEnd, + wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags); }; template<> struct WstringHelper<2> { static ConversionResult toUTF8( - const wchar_t*& sourceStart, const wchar_t* sourceEnd, - Byte*& targetStart, Byte* targetEnd, ConversionFlags flags) + const wchar_t*& sourceStart, const wchar_t* sourceEnd, + Byte*& targetStart, Byte* targetEnd, ConversionFlags flags) { - return ConvertUTF16toUTF8( - reinterpret_cast<const UTF16**>(&sourceStart), - reinterpret_cast<const UTF16*>(sourceEnd), - &targetStart, targetEnd, flags); + return ConvertUTF16toUTF8( + reinterpret_cast<const UTF16**>(&sourceStart), + reinterpret_cast<const UTF16*>(sourceEnd), + &targetStart, targetEnd, flags); } static ConversionResult fromUTF8( - const Byte*& sourceStart, const Byte* sourceEnd, - wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags) + const Byte*& sourceStart, const Byte* sourceEnd, + wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags) { - return ConvertUTF8toUTF16( - &sourceStart, sourceEnd, - reinterpret_cast<UTF16**>(&targetStart), - reinterpret_cast<UTF16*>(targetEnd), flags); + return ConvertUTF8toUTF16( + &sourceStart, sourceEnd, + reinterpret_cast<UTF16**>(&targetStart), + reinterpret_cast<UTF16*>(targetEnd), flags); } }; @@ -59,23 +59,23 @@ template<> struct WstringHelper<4> { static ConversionResult toUTF8( - const wchar_t*& sourceStart, const wchar_t* sourceEnd, - Byte*& targetStart, Byte* targetEnd, ConversionFlags flags) + const wchar_t*& sourceStart, const wchar_t* sourceEnd, + Byte*& targetStart, Byte* targetEnd, ConversionFlags flags) { - return ConvertUTF32toUTF8( - reinterpret_cast<const UTF32**>(&sourceStart), - reinterpret_cast<const UTF32*>(sourceEnd), - &targetStart, targetEnd, flags); + return ConvertUTF32toUTF8( + reinterpret_cast<const UTF32**>(&sourceStart), + reinterpret_cast<const UTF32*>(sourceEnd), + &targetStart, targetEnd, flags); } static ConversionResult fromUTF8( - const Byte*& sourceStart, const Byte* sourceEnd, - wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags) + const Byte*& sourceStart, const Byte* sourceEnd, + wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags) { - return ConvertUTF8toUTF32( - &sourceStart, sourceEnd, - reinterpret_cast<UTF32**>(&targetStart), - reinterpret_cast<UTF32*>(targetEnd), flags); + return ConvertUTF8toUTF32( + &sourceStart, sourceEnd, + reinterpret_cast<UTF32**>(&targetStart), + reinterpret_cast<UTF32*>(targetEnd), flags); } }; } @@ -90,7 +90,7 @@ IceUtil::convertUTFWstringToUTF8( Byte*& targetStart, Byte* targetEnd, ConversionFlags flags) { return WstringHelper<sizeof(wchar_t)>::toUTF8( - sourceStart, sourceEnd, targetStart, targetEnd, flags); + sourceStart, sourceEnd, targetStart, targetEnd, flags); } ConversionResult @@ -99,12 +99,12 @@ IceUtil::convertUTF8ToUTFWstring( wchar_t*& targetStart, wchar_t* targetEnd, ConversionFlags flags) { return WstringHelper<sizeof(wchar_t)>::fromUTF8( - sourceStart, sourceEnd, targetStart, targetEnd, flags); + sourceStart, sourceEnd, targetStart, targetEnd, flags); } ConversionResult IceUtil::convertUTF8ToUTFWstring(const Byte*& sourceStart, const Byte* sourceEnd, - std::wstring& target, ConversionFlags flags) + std::wstring& target, ConversionFlags flags) { // // Could be reimplemented without this temporary wchar_t buffer @@ -115,13 +115,13 @@ IceUtil::convertUTF8ToUTFWstring(const Byte*& sourceStart, const Byte* sourceEnd wchar_t* targetEnd = targetStart + size; ConversionResult result = - convertUTF8ToUTFWstring(sourceStart, sourceEnd, targetStart, - targetEnd, flags); + convertUTF8ToUTFWstring(sourceStart, sourceEnd, targetStart, + targetEnd, flags); if(result == conversionOK) { - std::wstring s(outBuf, static_cast<size_t>(targetStart - outBuf)); - s.swap(target); + std::wstring s(outBuf, static_cast<size_t>(targetStart - outBuf)); + s.swap(target); } delete[] outBuf; return result; @@ -135,7 +135,7 @@ IceUtil::convertUTF8ToUTFWstring(const Byte*& sourceStart, const Byte* sourceEnd const char* IceUtil::UTFConversionException::_name = "IceUtil::UTFConversionException"; IceUtil::UTFConversionException::UTFConversionException(const char* file, int line, - ConversionResult cr): + ConversionResult cr): Exception(file, line), _conversionResult(cr) {} @@ -152,18 +152,18 @@ IceUtil::UTFConversionException::ice_print(ostream& os) const Exception::ice_print(os); switch(_conversionResult) { - case sourceExhausted: - os << ": source exhausted"; - break; - case targetExhausted: - os << ": target exhausted"; - break; - case sourceIllegal: - os << ": illegal source"; - break; - default: - assert(0); - break; + case sourceExhausted: + os << ": source exhausted"; + break; + case targetExhausted: + os << ": target exhausted"; + break; + case sourceIllegal: + os << ": illegal source"; + break; + default: + assert(0); + break; }; } @@ -200,18 +200,18 @@ IceUtil::wstringToString(const wstring& wstr) const wchar_t* sourceStart = wstr.data(); ConversionResult cr = - convertUTFWstringToUTF8( - sourceStart, sourceStart + wstr.size(), - targetStart, targetEnd, lenientConversion); - + convertUTFWstringToUTF8( + sourceStart, sourceStart + wstr.size(), + targetStart, targetEnd, lenientConversion); + if(cr != conversionOK) { - delete[] outBuf; - throw UTFConversionException(__FILE__, __LINE__, cr); + delete[] outBuf; + throw UTFConversionException(__FILE__, __LINE__, cr); } string s(reinterpret_cast<char*>(outBuf), - static_cast<size_t>(targetStart - outBuf)); + static_cast<size_t>(targetStart - outBuf)); s.swap(target); delete[] outBuf; return target; @@ -224,12 +224,12 @@ IceUtil::stringToWstring(const string& str) const Byte* sourceStart = reinterpret_cast<const Byte*>(str.data()); ConversionResult cr - = convertUTF8ToUTFWstring(sourceStart, sourceStart + str.size(), - result, lenientConversion); + = convertUTF8ToUTFWstring(sourceStart, sourceStart + str.size(), + result, lenientConversion); if(cr != conversionOK) { - throw UTFConversionException(__FILE__, __LINE__, cr); + throw UTFConversionException(__FILE__, __LINE__, cr); } return result; } |