diff options
author | Igor Pavlov <ipavlov@users.sourceforge.net> | 2008-08-13 04:00:00 +0400 |
---|---|---|
committer | Kornel LesiĆski <kornel@geekhood.net> | 2016-05-28 02:15:56 +0300 |
commit | 173c07e166fdf6fcd20f18ea73008f1b628945df (patch) | |
tree | 13ebea85cdc4c16ae93714ff0627ee9f91ad7e08 /CPP/Common | |
parent | 3901bf0ab88106a5b031cba7bc18d60cdebf7eef (diff) |
4.59 beta
Diffstat (limited to 'CPP/Common')
33 files changed, 689 insertions, 195 deletions
diff --git a/CPP/Common/AutoPtr.h b/CPP/Common/AutoPtr.h index c5808cb0..006d3155 100755 --- a/CPP/Common/AutoPtr.h +++ b/CPP/Common/AutoPtr.h @@ -9,7 +9,7 @@ template<class T> class CMyAutoPtr public: CMyAutoPtr(T *p = 0) : _p(p) {} CMyAutoPtr(CMyAutoPtr<T>& p): _p(p.release()) {} - CMyAutoPtr<T>& operator=(CMyAutoPtr<T>& p) + CMyAutoPtr<T>& operator=(CMyAutoPtr<T>& p) { reset(p.release()); return (*this); diff --git a/CPP/Common/Buffer.h b/CPP/Common/Buffer.h index 78a68a6d..cc8b3428 100755 --- a/CPP/Common/Buffer.h +++ b/CPP/Common/Buffer.h @@ -6,7 +6,7 @@ #include "Defs.h" template <class T> class CBuffer -{ +{ protected: size_t _capacity; T *_items; diff --git a/CPP/Common/CRC.cpp b/CPP/Common/CRC.cpp index f151471b..a1e54ec9 100755 --- a/CPP/Common/CRC.cpp +++ b/CPP/Common/CRC.cpp @@ -2,8 +2,8 @@ #include "StdAfx.h" -extern "C" -{ +extern "C" +{ #include "../../C/7zCrc.h" } diff --git a/CPP/Common/CommandLineParser.cpp b/CPP/Common/CommandLineParser.cpp index 67f72675..52785d07 100755 --- a/CPP/Common/CommandLineParser.cpp +++ b/CPP/Common/CommandLineParser.cpp @@ -22,7 +22,7 @@ void SplitCommandLine(const UString &src, UString &dest1, UString &dest2) i++; break; } - else + else dest1 += c; } dest2 = src.Mid(i); @@ -55,8 +55,8 @@ static const wchar_t kSwitchMinus = '-'; static const wchar_t *kStopSwitchParsing = L"--"; static bool IsItSwitchChar(wchar_t c) -{ - return (c == kSwitchID1 /*|| c == kSwitchID2 */); +{ + return (c == kSwitchID1 /*|| c == kSwitchID2 */); } CParser::CParser(int numSwitches): @@ -70,7 +70,7 @@ CParser::~CParser() delete []_switches; } -void CParser::ParseStrings(const CSwitchForm *switchForms, +void CParser::ParseStrings(const CSwitchForm *switchForms, const UStringVector &commandStrings) { int numCommandStrings = commandStrings.Size(); @@ -94,7 +94,7 @@ void CParser::ParseStrings(const CSwitchForm *switchForms, bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms) { int len = s.Length(); - if (len == 0) + if (len == 0) return false; int pos = 0; if (!IsItSwitchChar(s[pos])) @@ -109,7 +109,7 @@ bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms) for(int switchIndex = 0; switchIndex < _numSwitches; switchIndex++) { int switchLen = MyStringLen(switchForms[switchIndex].IDString); - if (switchLen <= maxLen || pos + switchLen > len) + if (switchLen <= maxLen || pos + switchLen > len) continue; UString temp = s + pos; @@ -166,8 +166,8 @@ bool CParser::ParseString(const UString &s, const CSwitchForm *switchForms) } break; } - case NSwitchType::kLimitedPostString: - case NSwitchType::kUnLimitedPostString: + case NSwitchType::kLimitedPostString: + case NSwitchType::kUnLimitedPostString: { int minLen = switchForm.MinLen; if (tailSize < minLen) @@ -205,7 +205,7 @@ const CSwitchResult& CParser::operator[](size_t index) const ///////////////////////////////// // Command parsing procedures -int ParseCommand(int numCommandForms, const CCommandForm *commandForms, +int ParseCommand(int numCommandForms, const CCommandForm *commandForms, const UString &commandString, UString &postString) { for(int i = 0; i < numCommandForms; i++) diff --git a/CPP/Common/CommandLineParser.h b/CPP/Common/CommandLineParser.h index a97f58a5..6c7226a1 100755 --- a/CPP/Common/CommandLineParser.h +++ b/CPP/Common/CommandLineParser.h @@ -12,7 +12,7 @@ void SplitCommandLine(const UString &s, UStringVector &parts); namespace NSwitchType { enum EEnum - { + { kSimple, kPostMinus, kLimitedPostString, @@ -44,12 +44,12 @@ class CParser { int _numSwitches; CSwitchResult *_switches; - bool ParseString(const UString &s, const CSwitchForm *switchForms); + bool ParseString(const UString &s, const CSwitchForm *switchForms); public: UStringVector NonSwitchStrings; CParser(int numSwitches); ~CParser(); - void ParseStrings(const CSwitchForm *switchForms, + void ParseStrings(const CSwitchForm *switchForms, const UStringVector &commandStrings); const CSwitchResult& operator[](size_t index) const; }; @@ -64,7 +64,7 @@ struct CCommandForm }; // Returns: Index of form and postString; -1, if there is no match -int ParseCommand(int numCommandForms, const CCommandForm *commandForms, +int ParseCommand(int numCommandForms, const CCommandForm *commandForms, const UString &commandString, UString &postString); } diff --git a/CPP/Common/DynamicBuffer.h b/CPP/Common/DynamicBuffer.h index 1709657d..9bdbaac8 100755 --- a/CPP/Common/DynamicBuffer.h +++ b/CPP/Common/DynamicBuffer.h @@ -6,7 +6,7 @@ #include "Buffer.h" template <class T> class CDynamicBuffer: public CBuffer<T> -{ +{ void GrowLength(size_t size) { size_t delta; diff --git a/CPP/Common/IntToString.cpp b/CPP/Common/IntToString.cpp index 340d8655..c071daef 100755 --- a/CPP/Common/IntToString.cpp +++ b/CPP/Common/IntToString.cpp @@ -13,7 +13,7 @@ void ConvertUInt64ToString(UInt64 value, char *s, UInt32 base) } char temp[72]; int pos = 0; - do + do { int delta = (int)(value % base); temp[pos++] = (char)((delta < 10) ? ('0' + delta) : ('a' + (delta - 10))); @@ -30,13 +30,13 @@ void ConvertUInt64ToString(UInt64 value, wchar_t *s) { wchar_t temp[32]; int pos = 0; - do + do { temp[pos++] = (wchar_t)(L'0' + (int)(value % 10)); value /= 10; } while (value != 0); - do + do *s++ = temp[--pos]; while(pos > 0); *s = L'\0'; diff --git a/CPP/Common/Lang.cpp b/CPP/Common/Lang.cpp index 7316ade4..75dfed42 100755 --- a/CPP/Common/Lang.cpp +++ b/CPP/Common/Lang.cpp @@ -105,7 +105,7 @@ bool CLang::Open(LPCWSTR fileName) int CLang::FindItem(UInt32 value) const { - int left = 0, right = _langPairs.Size(); + int left = 0, right = _langPairs.Size(); while (left != right) { UInt32 mid = (left + right) / 2; diff --git a/CPP/Common/MyCom.h b/CPP/Common/MyCom.h index dcc94f14..2cd09358 100755 --- a/CPP/Common/MyCom.h +++ b/CPP/Common/MyCom.h @@ -28,11 +28,11 @@ public: // T& operator*() const { return *_p; } T** operator&() { return &_p; } T* operator->() const { return _p; } - T* operator=(T* p) - { + T* operator=(T* p) + { if (p != 0) p->AddRef(); - if (_p) + if (_p) _p->Release(); _p = p; return p; @@ -116,8 +116,8 @@ public: unsigned int Length() const { return ::SysStringLen(m_str); } operator BSTR() const { return m_str; } BSTR* operator&() { return &m_str; } - BSTR MyCopy() const - { + BSTR MyCopy() const + { int byteLen = ::SysStringByteLen(m_str); BSTR res = ::SysAllocStringByteLen(NULL, byteLen); memmove(res, m_str, byteLen); @@ -149,7 +149,7 @@ public: }; #define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \ - (REFGUID iid, void **outObject) { + (REFGUID iid, void **outObject) { #define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \ { *outObject = (void *)(i *)this; AddRef(); return S_OK; } diff --git a/CPP/Common/MyGuidDef.h b/CPP/Common/MyGuidDef.h index 19562696..7cfaba0c 100755 --- a/CPP/Common/MyGuidDef.h +++ b/CPP/Common/MyGuidDef.h @@ -23,7 +23,7 @@ typedef struct { #ifdef __cplusplus inline int operator==(REFGUID g1, REFGUID g2) -{ +{ for (int i = 0; i < (int)sizeof(g1); i++) if (((unsigned char *)&g1)[i] != ((unsigned char *)&g2)[i]) return 0; diff --git a/CPP/Common/MyMap.cpp b/CPP/Common/MyMap.cpp new file mode 100755 index 00000000..0ee11e8c --- /dev/null +++ b/CPP/Common/MyMap.cpp @@ -0,0 +1,140 @@ +// MyMap.cpp + +#include "StdAfx.h" + +#include "MyMap.h" + +static const unsigned kNumBitsMax = sizeof(UInt32) * 8; + +static UInt32 GetSubBits(UInt32 value, unsigned startPos, unsigned numBits) +{ + if (startPos == sizeof(value) * 8) + return 0; + value >>= startPos; + if (numBits == sizeof(value) * 8) + return value; + return value & (((UInt32)1 << numBits) - 1); +} + +static inline unsigned GetSubBit(UInt32 v, unsigned n) { return (unsigned)(v >> n) & 1; } + +bool CMap32::Find(UInt32 key, UInt32 &valueRes) const +{ + valueRes = (UInt32)(Int32)-1; + if (Nodes.Size() == 0) + return false; + if (Nodes.Size() == 1) + { + const CNode &n = Nodes[0]; + if (n.Len == kNumBitsMax) + { + valueRes = n.Values[0]; + return (key == n.Key); + } + } + + int cur = 0; + unsigned bitPos = kNumBitsMax; + for (;;) + { + const CNode &n = Nodes[cur]; + bitPos -= n.Len; + if (GetSubBits(key, bitPos, n.Len) != GetSubBits(n.Key, bitPos, n.Len)) + return false; + unsigned bit = GetSubBit(key, --bitPos); + if (n.IsLeaf[bit]) + { + valueRes = n.Values[bit]; + return (key == n.Keys[bit]); + } + cur = (int)n.Keys[bit]; + } +} + +bool CMap32::Set(UInt32 key, UInt32 value) +{ + if (Nodes.Size() == 0) + { + CNode n; + n.Key = n.Keys[0] = n.Keys[1] = key; + n.Values[0] = n.Values[1] = value; + n.IsLeaf[0] = n.IsLeaf[1] = 1; + n.Len = kNumBitsMax; + Nodes.Add(n); + return false; + } + if (Nodes.Size() == 1) + { + CNode &n = Nodes[0]; + if (n.Len == kNumBitsMax) + { + if (key == n.Key) + { + n.Values[0] = n.Values[1] = value; + return true; + } + unsigned i = kNumBitsMax - 1; + for (;GetSubBit(key, i) == GetSubBit(n.Key, i); i--); + n.Len = (UInt16)(kNumBitsMax - (1 + i)); + unsigned newBit = GetSubBit(key, i); + n.Values[newBit] = value; + n.Keys[newBit] = key; + return false; + } + } + + int cur = 0; + unsigned bitPos = kNumBitsMax; + for (;;) + { + CNode &n = Nodes[cur]; + bitPos -= n.Len; + if (GetSubBits(key, bitPos, n.Len) != GetSubBits(n.Key, bitPos, n.Len)) + { + unsigned i = n.Len - 1; + for (; GetSubBit(key, bitPos + i) == GetSubBit(n.Key, bitPos + i); i--); + + CNode e2(n); + e2.Len = (UInt16)i; + + n.Len = (UInt16)(n.Len - (1 + i)); + unsigned newBit = GetSubBit(key, bitPos + i); + n.Values[newBit] = value; + n.IsLeaf[newBit] = 1; + n.IsLeaf[1 - newBit] = 0; + n.Keys[newBit] = key; + n.Keys[1 - newBit] = Nodes.Size(); + Nodes.Add(e2); + return false; + } + unsigned bit = GetSubBit(key, --bitPos); + + if (n.IsLeaf[bit]) + { + if (key == n.Keys[bit]) + { + n.Values[bit] = value; + return true; + } + unsigned i = bitPos - 1; + for (;GetSubBit(key, i) == GetSubBit(n.Keys[bit], i); i--); + + CNode e2; + + unsigned newBit = GetSubBit(key, i); + e2.Values[newBit] = value; + e2.Values[1 - newBit] = n.Values[bit]; + e2.IsLeaf[newBit] = e2.IsLeaf[1 - newBit] = 1; + e2.Keys[newBit] = key; + e2.Keys[1 - newBit] = e2.Key = n.Keys[bit]; + e2.Len = (UInt16)(bitPos - (1 + i)); + + n.IsLeaf[bit] = 0; + n.Keys[bit] = Nodes.Size(); + + Nodes.Add(e2); + return false; + } + cur = (int)n.Keys[bit]; + } +} diff --git a/CPP/Common/MyMap.h b/CPP/Common/MyMap.h new file mode 100755 index 00000000..d0dd43f5 --- /dev/null +++ b/CPP/Common/MyMap.h @@ -0,0 +1,28 @@ +// MyMap.h + +#ifndef __COMMON_MYMAP_H +#define __COMMON_MYMAP_H + +#include "MyVector.h" +#include "Types.h" + +class CMap32 +{ + struct CNode + { + UInt32 Key; + UInt32 Keys[2]; + UInt32 Values[2]; + UInt16 Len; + Byte IsLeaf[2]; + }; + CRecordVector<CNode> Nodes; + +public: + + void Clear() { Nodes.Clear(); } + bool Find(UInt32 key, UInt32 &valueRes) const; + bool Set(UInt32 key, UInt32 value); // returns true, if there is such key already +}; + +#endif diff --git a/CPP/Common/MyString.cpp b/CPP/Common/MyString.cpp index affdcb0b..41c0c95d 100755 --- a/CPP/Common/MyString.cpp +++ b/CPP/Common/MyString.cpp @@ -64,7 +64,7 @@ wchar_t * MyStringUpper(wchar_t *s) } wchar_t * MyStringLower(wchar_t *s) -{ +{ if (s == 0) return 0; wchar_t *res = CharLowerW(s); @@ -81,43 +81,43 @@ wchar_t * MyStringLower(wchar_t *s) inline int ConvertCompareResult(int r) { return r - 2; } int MyStringCollate(const wchar_t *s1, const wchar_t *s2) -{ +{ int res = CompareStringW( - LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1); + LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1); #ifdef _UNICODE return ConvertCompareResult(res); #else if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) return ConvertCompareResult(res); - return MyStringCollate(UnicodeStringToMultiByte(s1), + return MyStringCollate(UnicodeStringToMultiByte(s1), UnicodeStringToMultiByte(s2)); #endif } #ifndef _WIN32_WCE int MyStringCollate(const char *s1, const char *s2) -{ +{ return ConvertCompareResult(CompareStringA( - LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1)); + LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1)); } int MyStringCollateNoCase(const char *s1, const char *s2) -{ +{ return ConvertCompareResult(CompareStringA( - LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1)); + LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1)); } #endif int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2) -{ +{ int res = CompareStringW( - LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1); + LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1); #ifdef _UNICODE return ConvertCompareResult(res); #else if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) return ConvertCompareResult(res); - return MyStringCollateNoCase(UnicodeStringToMultiByte(s1), + return MyStringCollateNoCase(UnicodeStringToMultiByte(s1), UnicodeStringToMultiByte(s2)); #endif } @@ -132,7 +132,7 @@ wchar_t MyCharUpper(wchar_t c) /* int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2) -{ +{ for (;;) { wchar_t c1 = *s1++; @@ -150,7 +150,7 @@ int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2) #endif int MyStringCompare(const char *s1, const char *s2) -{ +{ for (;;) { unsigned char c1 = (unsigned char)*s1++; @@ -162,7 +162,7 @@ int MyStringCompare(const char *s1, const char *s2) } int MyStringCompare(const wchar_t *s1, const wchar_t *s2) -{ +{ for (;;) { wchar_t c1 = *s1++; @@ -174,7 +174,7 @@ int MyStringCompare(const wchar_t *s1, const wchar_t *s2) } int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) -{ +{ for (;;) { wchar_t c1 = *s1++; @@ -192,7 +192,7 @@ int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) #ifdef _WIN32 int MyStringCompareNoCase(const char *s1, const char *s2) -{ +{ return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2)); } #endif diff --git a/CPP/Common/MyString.h b/CPP/Common/MyString.h index c46ca545..64095771 100755 --- a/CPP/Common/MyString.h +++ b/CPP/Common/MyString.h @@ -14,7 +14,7 @@ template <class T> inline int MyStringLen(const T *s) -{ +{ int i; for (i = 0; s[i] != '\0'; i++); return i; @@ -22,7 +22,7 @@ inline int MyStringLen(const T *s) template <class T> inline T * MyStringCopy(T *dest, const T *src) -{ +{ T *destStart = dest; while((*dest++ = *src++) != 0); return destStart; @@ -140,7 +140,7 @@ class CStringBase } void MoveItems(int destIndex, int srcIndex) { - memmove(_chars + destIndex, _chars + srcIndex, + memmove(_chars + destIndex, _chars + srcIndex, sizeof(T) * (_length - srcIndex + 1)); } @@ -167,7 +167,7 @@ protected: void SetCapacity(int newCapacity) { int realCapacity = newCapacity + 1; - if(realCapacity == _capacity) + if (realCapacity == _capacity) return; /* const int kMaxStringSize = 0x20000000; @@ -177,25 +177,21 @@ protected: #endif */ T *newBuffer = new T[realCapacity]; - if(_capacity > 0) + if (_capacity > 0) { - for (int i = 0; i < (_length + 1); i++) + for (int i = 0; i < _length; i++) newBuffer[i] = _chars[i]; delete []_chars; - _chars = newBuffer; - } - else - { - _chars = newBuffer; - _chars[0] = 0; } + _chars = newBuffer; + _chars[_length] = 0; _capacity = realCapacity; } void GrowLength(int n) { int freeSize = _capacity - _length - 1; - if (n <= freeSize) + if (n <= freeSize) return; int delta; if (_capacity > 64) @@ -216,8 +212,7 @@ protected: } public: - CStringBase(): _chars(0), _length(0), _capacity(0) - { SetCapacity(16 - 1); } + CStringBase(): _chars(0), _length(0), _capacity(0) { SetCapacity(3); } CStringBase(T c): _chars(0), _length(0), _capacity(0) { SetCapacity(1); @@ -240,14 +235,14 @@ public: } ~CStringBase() { delete []_chars; } - operator const T*() const { return _chars;} + operator const T*() const { return _chars;} - // The minimum size of the character buffer in characters. + // The minimum size of the character buffer in characters. // This value does not include space for a null terminator. T* GetBuffer(int minBufLength) { - if(minBufLength >= _capacity) - SetCapacity(minBufLength + 1); + if (minBufLength >= _capacity) + SetCapacity(minBufLength); return _chars; } void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } @@ -278,9 +273,9 @@ public: int length = MyStringLen(chars); SetCapacity(length); MyStringCopy(_chars, chars); - _length = length; + _length = length; return *this; - } + } CStringBase& operator=(const CStringBase& s) { if(&s == this) @@ -508,7 +503,7 @@ public: while (pos < Length()) { pos = Find(oldChar, pos); - if (pos < 0) + if (pos < 0) break; _chars[pos] = newChar; pos++; @@ -529,7 +524,7 @@ public: while (pos < _length) { pos = Find(oldString, pos); - if (pos < 0) + if (pos < 0) break; Delete(pos, oldStringLength); Insert(pos, newString); @@ -556,7 +551,7 @@ CStringBase<T> operator+(const CStringBase<T>& s1, const CStringBase<T>& s2) { CStringBase<T> result(s1); result += s2; - return result; + return result; } template <class T> @@ -564,7 +559,7 @@ CStringBase<T> operator+(const CStringBase<T>& s, T c) { CStringBase<T> result(s); result += c; - return result; + return result; } template <class T> @@ -572,7 +567,7 @@ CStringBase<T> operator+(T c, const CStringBase<T>& s) { CStringBase<T> result(c); result += s; - return result; + return result; } template <class T> @@ -580,7 +575,7 @@ CStringBase<T> operator+(const CStringBase<T>& s, const T * chars) { CStringBase<T> result(s); result += chars; - return result; + return result; } template <class T> @@ -588,7 +583,7 @@ CStringBase<T> operator+(const T * chars, const CStringBase<T>& s) { CStringBase<T> result(chars); result += s; - return result; + return result; } template <class T> diff --git a/CPP/Common/MyUnknown.h b/CPP/Common/MyUnknown.h index d28d8542..136145a7 100755 --- a/CPP/Common/MyUnknown.h +++ b/CPP/Common/MyUnknown.h @@ -9,7 +9,7 @@ #if (_WIN32_WCE > 300) #include <basetyps.h> #else -#define MIDL_INTERFACE(x) struct +#define MIDL_INTERFACE(x) struct #endif #else #include <basetyps.h> @@ -17,7 +17,7 @@ #include <unknwn.h> -#else +#else #include "MyWindows.h" #endif diff --git a/CPP/Common/MyVector.cpp b/CPP/Common/MyVector.cpp index def2a581..2a0aa15a 100755 --- a/CPP/Common/MyVector.cpp +++ b/CPP/Common/MyVector.cpp @@ -6,11 +6,12 @@ #include "MyVector.h" -CBaseRecordVector::~CBaseRecordVector() { Free(); } +CBaseRecordVector::~CBaseRecordVector() { ClearAndFree(); } -void CBaseRecordVector::Free() -{ - delete []((unsigned char *)_items); +void CBaseRecordVector::ClearAndFree() +{ + Clear(); + delete []((unsigned char *)_items); _capacity = 0; _size = 0; _items = 0; @@ -24,39 +25,47 @@ void CBaseRecordVector::ReserveOnePosition() { if (_size != _capacity) return; - int delta; - if (_capacity > 64) - delta = _capacity / 2; - else if (_capacity > 8) + int delta = 1; + if (_capacity >= 64) + delta = _capacity / 4; + else if (_capacity >= 8) delta = 8; - else - delta = 4; Reserve(_capacity + delta); } void CBaseRecordVector::Reserve(int newCapacity) { - if (newCapacity <= _capacity) + // if (newCapacity <= _capacity) + if (newCapacity == _capacity) return; if ((unsigned)newCapacity >= ((unsigned)1 << (sizeof(unsigned) * 8 - 1))) throw 1052353; size_t newSize = (size_t)(unsigned)newCapacity * _itemSize; if (newSize / _itemSize != (size_t)(unsigned)newCapacity) throw 1052354; - unsigned char *p = new unsigned char[newSize]; - if (p == 0) - throw 1052355; - int numRecordsToMove = _capacity; - memmove(p, _items, _itemSize * numRecordsToMove); + unsigned char *p = NULL; + if (newSize > 0) + { + p = new unsigned char[newSize]; + if (p == 0) + throw 1052355; + int numRecordsToMove = _size; + memcpy(p, _items, _itemSize * numRecordsToMove); + } delete [](unsigned char *)_items; _items = p; _capacity = newCapacity; } +void CBaseRecordVector::ReserveDown() +{ + Reserve(_size); +} + void CBaseRecordVector::MoveItems(int destIndex, int srcIndex) { - memmove(((unsigned char *)_items) + destIndex * _itemSize, - ((unsigned char *)_items) + srcIndex * _itemSize, + memmove(((unsigned char *)_items) + destIndex * _itemSize, + ((unsigned char *)_items) + srcIndex * _itemSize, _itemSize * (_size - srcIndex)); } diff --git a/CPP/Common/MyVector.h b/CPP/Common/MyVector.h index ce370a53..0ab79d6e 100755 --- a/CPP/Common/MyVector.h +++ b/CPP/Common/MyVector.h @@ -17,15 +17,15 @@ protected: void ReserveOnePosition(); void InsertOneItem(int index); void TestIndexAndCorrectNum(int index, int &num) const - { if (index + num > _size) num = _size - index; } + { if (index + num > _size) num = _size - index; } public: - CBaseRecordVector(size_t itemSize): - _capacity(0), _size(0), _items(0), _itemSize(itemSize) {} + CBaseRecordVector(size_t itemSize): _capacity(0), _size(0), _items(0), _itemSize(itemSize) {} virtual ~CBaseRecordVector(); - void Free(); + void ClearAndFree(); int Size() const { return _size; } bool IsEmpty() const { return (_size == 0); } void Reserve(int newCapacity); + void ReserveDown(); virtual void Delete(int index, int num = 1); void Clear(); void DeleteFrom(int index); @@ -36,9 +36,8 @@ template <class T> class CRecordVector: public CBaseRecordVector { public: - CRecordVector():CBaseRecordVector(sizeof(T)){}; - CRecordVector(const CRecordVector &v): - CBaseRecordVector(sizeof(T)) { *this = v;} + CRecordVector(): CBaseRecordVector(sizeof(T)){}; + CRecordVector(const CRecordVector &v): CBaseRecordVector(sizeof(T)) { *this = v; } CRecordVector& operator=(const CRecordVector &v) { Clear(); @@ -68,9 +67,9 @@ public: const T& operator[](int index) const { return ((T *)_items)[index]; } T& operator[](int index) { return ((T *)_items)[index]; } const T& Front() const { return operator[](0); } - T& Front() { return operator[](0); } + T& Front() { return operator[](0); } const T& Back() const { return operator[](_size - 1); } - T& Back() { return operator[](_size - 1); } + T& Back() { return operator[](_size - 1); } void Swap(int i, int j) { @@ -81,7 +80,7 @@ public: int FindInSorted(const T& item) const { - int left = 0, right = Size(); + int left = 0, right = Size(); while (left != right) { int mid = (left + right) / 2; @@ -98,7 +97,7 @@ public: int AddToUniqueSorted(const T& item) { - int left = 0, right = Size(); + int left = 0, right = Size(); while (left != right) { int mid = (left + right) / 2; @@ -115,7 +114,7 @@ public: } static void SortRefDown(T* p, int k, int size, int (*compare)(const T*, const T*, void *), void *param) - { + { T temp = p[k]; for (;;) { @@ -126,10 +125,10 @@ public: s++; if (compare(&temp, p + s, param) >= 0) break; - p[k] = p[s]; + p[k] = p[s]; k = s; - } - p[k] = temp; + } + p[k] = temp; } void Sort(int (*compare)(const T*, const T*, void *), void *param) @@ -165,21 +164,20 @@ template <class T> class CObjectVector: public CPointerVector { public: - CObjectVector(){}; - ~CObjectVector() { Clear(); } - CObjectVector(const CObjectVector &objectVector) - { *this = objectVector; } - CObjectVector& operator=(const CObjectVector &objectVector) + CObjectVector() {}; + ~CObjectVector() { Clear(); }; + CObjectVector(const CObjectVector &v) { *this = v; } + CObjectVector& operator=(const CObjectVector &v) { Clear(); - return (*this += objectVector); + return (*this += v); } - CObjectVector& operator+=(const CObjectVector &objectVector) + CObjectVector& operator+=(const CObjectVector &v) { - int size = objectVector.Size(); + int size = v.Size(); Reserve(Size() + size); for(int i = 0; i < size; i++) - Add(objectVector[i]); + Add(v[i]); return *this; } const T& operator[](int index) const { return *((T *)CPointerVector::operator[](index)); } @@ -188,10 +186,8 @@ public: const T& Front() const { return operator[](0); } T& Back() { return operator[](_size - 1); } const T& Back() const { return operator[](_size - 1); } - int Add(const T& item) - { return CPointerVector::Add(new T(item)); } - void Insert(int index, const T& item) - { CPointerVector::Insert(index, new T(item)); } + int Add(const T& item) { return CPointerVector::Add(new T(item)); } + void Insert(int index, const T& item) { CPointerVector::Insert(index, new T(item)); } virtual void Delete(int index, int num = 1) { TestIndexAndCorrectNum(index, num); @@ -204,11 +200,11 @@ public: for(int i = 0; i < Size(); i++) if (item == (*this)[i]) return i; - return -1; + return -1; } int FindInSorted(const T& item) const { - int left = 0, right = Size(); + int left = 0, right = Size(); while (left != right) { int mid = (left + right) / 2; @@ -224,7 +220,7 @@ public: } int AddToSorted(const T& item) { - int left = 0, right = Size(); + int left = 0, right = Size(); while (left != right) { int mid = (left + right) / 2; @@ -243,7 +239,7 @@ public: return right; } - void Sort(int (*compare)(void *const *, void *const *, void *), void *param) + void Sort(int (*compare)(void *const *, void *const *, void *), void *param) { CPointerVector::Sort(compare, param); } static int CompareObjectItems(void *const *a1, void *const *a2, void * /* param */) @@ -251,4 +247,4 @@ public: void Sort() { CPointerVector::Sort(CompareObjectItems, 0); } }; -#endif +#endif diff --git a/CPP/Common/MyWindows.cpp b/CPP/Common/MyWindows.cpp index 4cf065cf..d9d8327b 100755 --- a/CPP/Common/MyWindows.cpp +++ b/CPP/Common/MyWindows.cpp @@ -12,7 +12,7 @@ static inline void *AllocateForBSTR(size_t cb) { return ::malloc(cb); } static inline void FreeForBSTR(void *pv) { ::free(pv);} static UINT MyStringLen(const wchar_t *s) -{ +{ UINT i; for (i = 0; s[i] != '\0'; i++); return i; @@ -81,7 +81,7 @@ HRESULT VariantCopy(VARIANTARG *dest, VARIANTARG *src) return res; if (src->vt == VT_BSTR) { - dest->bstrVal = SysAllocStringByteLen((LPCSTR)src->bstrVal, + dest->bstrVal = SysAllocStringByteLen((LPCSTR)src->bstrVal, SysStringByteLen(src->bstrVal)); if (dest->bstrVal == 0) return E_OUTOFMEMORY; diff --git a/CPP/Common/MyWindows.h b/CPP/Common/MyWindows.h index e388fb02..2861ba04 100755 --- a/CPP/Common/MyWindows.h +++ b/CPP/Common/MyWindows.h @@ -85,9 +85,9 @@ typedef LONG SCODE; #define E_INVALIDARG ((HRESULT)0x80070057L) #ifdef _MSC_VER -#define STDMETHODCALLTYPE __stdcall +#define STDMETHODCALLTYPE __stdcall #else -#define STDMETHODCALLTYPE +#define STDMETHODCALLTYPE #endif #define STDMETHOD_(t, f) virtual t STDMETHODCALLTYPE f @@ -97,7 +97,7 @@ typedef LONG SCODE; #define PURE = 0 -#define MIDL_INTERFACE(x) struct +#define MIDL_INTERFACE(x) struct #ifdef __cplusplus @@ -163,7 +163,7 @@ typedef struct tagPROPVARIANT PROPVAR_PAD1 wReserved1; PROPVAR_PAD2 wReserved2; PROPVAR_PAD3 wReserved3; - union + union { CHAR cVal; UCHAR bVal; diff --git a/CPP/Common/MyXml.cpp b/CPP/Common/MyXml.cpp new file mode 100755 index 00000000..1d5d836a --- /dev/null +++ b/CPP/Common/MyXml.cpp @@ -0,0 +1,209 @@ +// MyXml.cpp + +#include "StdAfx.h" + +#include "MyXml.h" + +static bool IsValidChar(char c) +{ + return + c >= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || + c >= '0' && c <= '9' || + c == '-'; +} + +static bool IsSpaceChar(char c) +{ + return (c == ' ' || c == '\t' || c == 0x0D || c == 0x0A); +} + +#define SKEEP_SPACES(s, pos) while (IsSpaceChar(s[pos])) pos++; + +static bool ReadProperty(const AString &s, int &pos, CXmlProp &prop) +{ + prop.Name.Empty(); + prop.Value.Empty(); + for (; pos < s.Length(); pos++) + { + char c = s[pos]; + if (!IsValidChar(c)) + break; + prop.Name += c; + } + + if (prop.Name.IsEmpty()) + return false; + + SKEEP_SPACES(s, pos); + if (s[pos++] != '=') + return false; + + SKEEP_SPACES(s, pos); + if (s[pos++] != '\"') + return false; + + while (pos < s.Length()) + { + char c = s[pos++]; + if (c == '\"') + return true; + prop.Value += c; + } + return false; +} + +int CXmlItem::FindProperty(const AString &propName) const +{ + for (int i = 0; i < Props.Size(); i++) + if (Props[i].Name == propName) + return i; + return -1; +} + +AString CXmlItem::GetPropertyValue(const AString &propName) const +{ + int index = FindProperty(propName); + if (index >= 0) + return Props[index].Value; + return AString(); +} + +bool CXmlItem::IsTagged(const AString &tag) const +{ + return (IsTag && Name == tag); +} + +int CXmlItem::FindSubTag(const AString &tag) const +{ + for (int i = 0; i < SubItems.Size(); i++) + if (SubItems[i].IsTagged(tag)) + return i; + return -1; +} + +AString CXmlItem::GetSubString() const +{ + if (SubItems.Size() == 1) + { + const CXmlItem &item = SubItems[0]; + if (!item.IsTag) + return item.Name; + } + return AString(); +} + +AString CXmlItem::GetSubStringForTag(const AString &tag) const +{ + int index = FindSubTag(tag); + if (index >= 0) + return SubItems[index].GetSubString(); + return AString(); +} + +bool CXmlItem::ParseItems(const AString &s, int &pos, int numAllowedLevels) +{ + if (numAllowedLevels == 0) + return false; + SubItems.Clear(); + AString finishString = "</"; + for (;;) + { + SKEEP_SPACES(s, pos); + + if (s.Mid(pos, finishString.Length()) == finishString) + return true; + + CXmlItem item; + if (!item.ParseItem(s, pos, numAllowedLevels - 1)) + return false; + SubItems.Add(item); + } +} + +bool CXmlItem::ParseItem(const AString &s, int &pos, int numAllowedLevels) +{ + SKEEP_SPACES(s, pos); + + int pos2 = s.Find('<', pos); + if (pos2 < 0) + return false; + if (pos2 != pos) + { + IsTag = false; + Name += s.Mid(pos, pos2 - pos); + pos = pos2; + return true; + } + IsTag = true; + + pos++; + SKEEP_SPACES(s, pos); + + for (; pos < s.Length(); pos++) + { + char c = s[pos]; + if (!IsValidChar(c)) + break; + Name += c; + } + if (Name.IsEmpty() || pos == s.Length()) + return false; + + int posTemp = pos; + for (;;) + { + SKEEP_SPACES(s, pos); + if (s[pos] == '/') + { + pos++; + // SKEEP_SPACES(s, pos); + return (s[pos++] == '>'); + } + if (s[pos] == '>') + { + if (!ParseItems(s, ++pos, numAllowedLevels)) + return false; + AString finishString = AString("</") + Name + AString(">"); + if (s.Mid(pos, finishString.Length()) != finishString) + return false; + pos += finishString.Length(); + return true; + } + if (posTemp == pos) + return false; + + CXmlProp prop; + if (!ReadProperty(s, pos, prop)) + return false; + Props.Add(prop); + posTemp = pos; + } +} + +bool SkeepHeader(const AString &s, int &pos, const AString &startString, const AString &endString) +{ + SKEEP_SPACES(s, pos); + if (s.Mid(pos, startString.Length()) == startString) + { + pos = s.Find(endString, pos); + if (pos < 0) + return false; + pos += endString.Length(); + SKEEP_SPACES(s, pos); + } + return true; +} + +bool CXml::Parse(const AString &s) +{ + int pos = 0; + if (!SkeepHeader(s, pos, "<?xml", "?>")) + return false; + if (!SkeepHeader(s, pos, "<!DOCTYPE", ">")) + return false; + if (!Root.ParseItem(s, pos, 1000)) + return false; + SKEEP_SPACES(s, pos); + return (pos == s.Length() && Root.IsTag); +} diff --git a/CPP/Common/MyXml.h b/CPP/Common/MyXml.h new file mode 100755 index 00000000..c6e8829a --- /dev/null +++ b/CPP/Common/MyXml.h @@ -0,0 +1,40 @@ +// MyXml.h + +#ifndef __MYXML_H +#define __MYXML_H + +#include "MyString.h" + +struct CXmlProp +{ + AString Name; + AString Value; +}; + +class CXmlItem +{ + bool ParseItems(const AString &s, int &pos, int numAllowedLevels); + +public: + AString Name; + bool IsTag; + CObjectVector<CXmlProp> Props; + CObjectVector<CXmlItem> SubItems; + + bool ParseItem(const AString &s, int &pos, int numAllowedLevels); + + bool IsTagged(const AString &tag) const; + int FindProperty(const AString &propName) const; + AString GetPropertyValue(const AString &propName) const; + AString GetSubString() const; + int FindSubTag(const AString &tag) const; + AString GetSubStringForTag(const AString &tag) const; +}; + +struct CXml +{ + CXmlItem Root; + bool Parse(const AString &s); +}; + +#endif diff --git a/CPP/Common/NewHandler.cpp b/CPP/Common/NewHandler.cpp index 094eb642..aad6e7d1 100755 --- a/CPP/Common/NewHandler.cpp +++ b/CPP/Common/NewHandler.cpp @@ -11,9 +11,9 @@ #ifndef DEBUG_MEMORY_LEAK #ifdef _WIN32 -void * +void * #ifdef _MSC_VER -__cdecl +__cdecl #endif operator new(size_t size) { @@ -24,9 +24,9 @@ operator new(size_t size) return p; } -void +void #ifdef _MSC_VER -__cdecl +__cdecl #endif operator delete(void *p) throw() { diff --git a/CPP/Common/NewHandler.h b/CPP/Common/NewHandler.h index 0619fc69..215ba05f 100755 --- a/CPP/Common/NewHandler.h +++ b/CPP/Common/NewHandler.h @@ -6,11 +6,11 @@ class CNewException {}; #ifdef _WIN32 -void +void #ifdef _MSC_VER -__cdecl +__cdecl #endif operator delete(void *p) throw(); -#endif +#endif -#endif +#endif diff --git a/CPP/Common/StdAfx.h b/CPP/Common/StdAfx.h index 681ee935..b8ba1d5c 100755 --- a/CPP/Common/StdAfx.h +++ b/CPP/Common/StdAfx.h @@ -6,4 +6,4 @@ // #include "MyWindows.h" #include "NewHandler.h" -#endif +#endif diff --git a/CPP/Common/StdInStream.cpp b/CPP/Common/StdInStream.cpp index 8fed7bc0..65aa8dde 100755 --- a/CPP/Common/StdInStream.cpp +++ b/CPP/Common/StdInStream.cpp @@ -14,7 +14,7 @@ static const char kIllegalChar = '\0'; static const char kNewLineChar = '\n'; static const char *kEOFMessage = "Unexpected end of input stream"; -static const char *kReadErrorMessage ="Error reading input stream"; +static const char *kReadErrorMessage ="Error reading input stream"; static const char *kIllegalCharMessage = "Illegal character in input stream"; static LPCTSTR kFileOpenMode = TEXT("r"); diff --git a/CPP/Common/StdInStream.h b/CPP/Common/StdInStream.h index e0fb3dfa..4b08c7c5 100755 --- a/CPP/Common/StdInStream.h +++ b/CPP/Common/StdInStream.h @@ -8,7 +8,7 @@ #include "MyString.h" #include "Types.h" -class CStdInStream +class CStdInStream { bool _streamIsOpen; FILE *_stream; diff --git a/CPP/Common/StdOutStream.cpp b/CPP/Common/StdOutStream.cpp index 5498c0cc..084ee95a 100755 --- a/CPP/Common/StdOutStream.cpp +++ b/CPP/Common/StdOutStream.cpp @@ -51,7 +51,7 @@ CStdOutStream::~CStdOutStream () CStdOutStream & CStdOutStream::operator<<(CStdOutStream & (*aFunction)(CStdOutStream &)) { - (*aFunction)(*this); + (*aFunction)(*this); return *this; } diff --git a/CPP/Common/StdOutStream.h b/CPP/Common/StdOutStream.h index 84907365..b0b2c615 100755 --- a/CPP/Common/StdOutStream.h +++ b/CPP/Common/StdOutStream.h @@ -7,7 +7,7 @@ #include "Types.h" -class CStdOutStream +class CStdOutStream { bool _streamIsOpen; FILE *_stream; diff --git a/CPP/Common/StringConvert.cpp b/CPP/Common/StringConvert.cpp index 762efd8d..cb57467b 100755 --- a/CPP/Common/StringConvert.cpp +++ b/CPP/Common/StringConvert.cpp @@ -14,8 +14,8 @@ UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) UString resultString; if(!srcString.IsEmpty()) { - int numChars = MultiByteToWideChar(codePage, 0, srcString, - srcString.Length(), resultString.GetBuffer(srcString.Length()), + int numChars = MultiByteToWideChar(codePage, 0, srcString, + srcString.Length(), resultString.GetBuffer(srcString.Length()), srcString.Length() + 1); #ifndef _WIN32_WCE if(numChars == 0) @@ -34,8 +34,8 @@ AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultCh { int numRequiredBytes = s.Length() * 2; BOOL defUsed; - int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(), - dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1, + int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(), + dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1, &defaultChar, &defUsed); defaultCharWasUsed = (defUsed != FALSE); #ifndef _WIN32_WCE diff --git a/CPP/Common/StringToInt.cpp b/CPP/Common/StringToInt.cpp index ec6733e3..9473766b 100755 --- a/CPP/Common/StringToInt.cpp +++ b/CPP/Common/StringToInt.cpp @@ -40,6 +40,28 @@ UInt64 ConvertOctStringToUInt64(const char *s, const char **end) } } +UInt64 ConvertHexStringToUInt64(const char *s, const char **end) +{ + UInt64 result = 0; + for (;;) + { + char c = *s; + UInt32 v; + if (c >= '0' && c <= '9') v = (c - '0'); + else if (c >= 'A' && c <= 'F') v = 10 + (c - 'A'); + else if (c >= 'a' && c <= 'f') v = 10 + (c - 'a'); + else + { + if (end != NULL) + *end = s; + return result; + } + result <<= 4; + result |= v; + s++; + } +} + UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end) { diff --git a/CPP/Common/StringToInt.h b/CPP/Common/StringToInt.h index bb971f62..c0d860ef 100755 --- a/CPP/Common/StringToInt.h +++ b/CPP/Common/StringToInt.h @@ -8,6 +8,7 @@ UInt64 ConvertStringToUInt64(const char *s, const char **end); UInt64 ConvertOctStringToUInt64(const char *s, const char **end); +UInt64 ConvertHexStringToUInt64(const char *s, const char **end); UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end); Int64 ConvertStringToInt64(const char *s, const char **end); diff --git a/CPP/Common/UTFConvert.cpp b/CPP/Common/UTFConvert.cpp index e15695bb..9d1fd005 100755 --- a/CPP/Common/UTFConvert.cpp +++ b/CPP/Common/UTFConvert.cpp @@ -5,87 +5,141 @@ #include "UTFConvert.h" #include "Types.h" -static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; +static const Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; -// These functions are for UTF8 <-> UTF16 conversion. - -bool ConvertUTF8ToUnicode(const AString &src, UString &dest) +static Bool Utf8_To_Utf16(wchar_t *dest, size_t *destLen, const char *src, size_t srcLen) { - dest.Empty(); - for(int i = 0; i < src.Length();) + size_t destPos = 0, srcPos = 0; + for (;;) { - Byte c = (Byte)src[i++]; + Byte c; + int numAdds; + if (srcPos == srcLen) + { + *destLen = destPos; + return True; + } + c = (Byte)src[srcPos++]; + if (c < 0x80) { - dest += (wchar_t)c; + if (dest) + dest[destPos] = (wchar_t)c; + destPos++; continue; } - if(c < 0xC0) - return false; - int numAdds; + if (c < 0xC0) + break; for (numAdds = 1; numAdds < 5; numAdds++) if (c < kUtf8Limits[numAdds]) break; UInt32 value = (c - kUtf8Limits[numAdds - 1]); + do { - if (i >= src.Length()) - return false; - Byte c2 = (Byte)src[i++]; + Byte c2; + if (srcPos == srcLen) + break; + c2 = (Byte)src[srcPos++]; if (c2 < 0x80 || c2 >= 0xC0) - return false; + break; value <<= 6; value |= (c2 - 0x80); - numAdds--; } - while(numAdds > 0); + while (--numAdds != 0); + if (value < 0x10000) - dest += (wchar_t)(value); + { + if (dest) + dest[destPos] = (wchar_t)value; + destPos++; + } else { value -= 0x10000; if (value >= 0x100000) - return false; - dest += (wchar_t)(0xD800 + (value >> 10)); - dest += (wchar_t)(0xDC00 + (value & 0x3FF)); + break; + if (dest) + { + dest[destPos + 0] = (wchar_t)(0xD800 + (value >> 10)); + dest[destPos + 1] = (wchar_t)(0xDC00 + (value & 0x3FF)); + } + destPos += 2; } } - return true; + *destLen = destPos; + return False; } -bool ConvertUnicodeToUTF8(const UString &src, AString &dest) +static Bool Utf16_To_Utf8(char *dest, size_t *destLen, const wchar_t *src, size_t srcLen) { - dest.Empty(); - for(int i = 0; i < src.Length();) + size_t destPos = 0, srcPos = 0; + for (;;) { - UInt32 value = (UInt32)src[i++]; + unsigned numAdds; + UInt32 value; + if (srcPos == srcLen) + { + *destLen = destPos; + return True; + } + value = src[srcPos++]; if (value < 0x80) { - dest += (char)value; + if (dest) + dest[destPos] = (char)value; + destPos++; continue; } if (value >= 0xD800 && value < 0xE000) { - if (value >= 0xDC00) - return false; - if (i >= src.Length()) - return false; - UInt32 c2 = (UInt32)src[i++]; + UInt32 c2; + if (value >= 0xDC00 || srcPos == srcLen) + break; + c2 = src[srcPos++]; if (c2 < 0xDC00 || c2 >= 0xE000) - return false; + break; value = ((value - 0xD800) << 10) | (c2 - 0xDC00); } - int numAdds; for (numAdds = 1; numAdds < 5; numAdds++) if (value < (((UInt32)1) << (numAdds * 5 + 6))) break; - dest += (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds))); + if (dest) + dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds))); + destPos++; do { numAdds--; - dest += (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F)); + if (dest) + dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F)); + destPos++; } - while(numAdds > 0); + while (numAdds != 0); } - return true; + *destLen = destPos; + return False; +} + +bool ConvertUTF8ToUnicode(const AString &src, UString &dest) +{ + dest.Empty(); + size_t destLen = 0; + Utf8_To_Utf16(NULL, &destLen, src, src.Length()); + wchar_t *p = dest.GetBuffer((int)destLen); + Bool res = Utf8_To_Utf16(p, &destLen, src, src.Length()); + p[destLen] = 0; + dest.ReleaseBuffer(); + return res ? true : false; +} + +bool ConvertUnicodeToUTF8(const UString &src, AString &dest) +{ + dest.Empty(); + size_t destLen = 0; + Utf16_To_Utf8(NULL, &destLen, src, src.Length()); + char *p = dest.GetBuffer((int)destLen); + Bool res = Utf16_To_Utf8(p, &destLen, src, src.Length()); + p[destLen] = 0; + dest.ReleaseBuffer(); + return res ? true : false; } diff --git a/CPP/Common/Wildcard.cpp b/CPP/Common/Wildcard.cpp index 9feebbec..05a3f5a3 100755 --- a/CPP/Common/Wildcard.cpp +++ b/CPP/Common/Wildcard.cpp @@ -4,7 +4,7 @@ #include "Wildcard.h" -bool g_CaseSensitive = +bool g_CaseSensitive = #ifdef _WIN32 false; #else @@ -31,7 +31,7 @@ static inline bool IsCharDirLimiter(wchar_t c) { return ( #ifdef _WIN32 - c == kDirDelimiter1 || + c == kDirDelimiter1 || #endif c == kDirDelimiter2); } @@ -54,20 +54,20 @@ static bool EnhancedMaskTest(const wchar_t *mask, const wchar_t *name) { wchar_t m = *mask; wchar_t c = *name; - if (m == 0) + if (m == 0) return (c == 0); if (m == kAnyCharsChar) { if (EnhancedMaskTest(mask + 1, name)) return true; - if (c == 0) + if (c == 0) return false; } else { if (m == kAnyCharChar) { - if (c == 0) + if (c == 0) return false; } else if (m != c) @@ -155,7 +155,7 @@ N = TestNameParts.Size(); File Dir ForFile req M<=N [N-M, N) - - nonreq M=N [0, M) - + nonreq M=N [0, M) - ForDir req M<N [0, M) ... [N-M-1, N-1) same as ForBoth-File nonreq [0, M) same as ForBoth-File @@ -301,7 +301,7 @@ bool CCensorNode::CheckPath(UStringVector &pathParts, bool isFile, bool &include bool CCensorNode::CheckPath(const UString &path, bool isFile, bool &include) const { - UStringVector pathParts; + UStringVector pathParts; SplitPathToParts(path, pathParts); return CheckPath(pathParts, isFile, include); } @@ -327,7 +327,7 @@ bool CCensorNode::CheckPathToRoot(bool include, UStringVector &pathParts, bool i /* bool CCensorNode::CheckPathToRoot(bool include, const UString &path, bool isFile) const { - UStringVector pathParts; + UStringVector pathParts; SplitPathToParts(path, pathParts); return CheckPathToRoot(include, pathParts, isFile); } |