From b920b9f8fbc08cee65be745b8c37a77156a5b5a2 Mon Sep 17 00:00:00 2001 From: jief Date: Thu, 12 Mar 2020 17:40:38 +0300 Subject: [PATCH] Introducing a UTF8 XString --- .../cpp_tests.xcodeproj/project.pbxproj | 20 + Xcode/cpp_tests/src/Platform.cpp | 5 + Xcode/cpp_tests/src/Platform.h | 2 +- rEFIt_UEFI/cpp_foundation/XArray.h | 8 +- rEFIt_UEFI/cpp_foundation/XObjArray.h | 4 +- rEFIt_UEFI/cpp_foundation/XString.cpp | 1086 +++++++++++++++++ rEFIt_UEFI/cpp_foundation/XString.h | 285 +++++ rEFIt_UEFI/cpp_foundation/XStringW.cpp | 16 +- rEFIt_UEFI/cpp_foundation/XStringW.h | 6 +- rEFIt_UEFI/cpp_unit_test/XString_test.cpp | 123 ++ rEFIt_UEFI/cpp_unit_test/XString_test.h | 1 + rEFIt_UEFI/cpp_unit_test/all_tests.cpp | 6 + rEFIt_UEFI/cpp_unit_test/global_test.cpp | 4 + rEFIt_UEFI/cpp_unit_test/global_test.h | 13 + rEFIt_UEFI/libeg/libscreen.cpp | 2 +- 15 files changed, 1563 insertions(+), 18 deletions(-) create mode 100755 rEFIt_UEFI/cpp_foundation/XString.cpp create mode 100755 rEFIt_UEFI/cpp_foundation/XString.h create mode 100755 rEFIt_UEFI/cpp_unit_test/XString_test.cpp create mode 100644 rEFIt_UEFI/cpp_unit_test/XString_test.h diff --git a/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj b/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj index f2bb7fb09..b1eff9905 100644 --- a/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj +++ b/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj @@ -53,6 +53,12 @@ 9A57C2272418B9A00029A39F /* XArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08532402FE9B00E2B470 /* XArray_tests.cpp */; }; 9A57C2282418B9A00029A39F /* Platform.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B085D240300E000E2B470 /* Platform.cpp */; }; 9A57C2292418B9A00029A39F /* XStringW_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08552402FE9B00E2B470 /* XStringW_test.cpp */; }; + 9A57C263241A752E0029A39F /* XString_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C261241A752E0029A39F /* XString_test.cpp */; }; + 9A57C264241A752E0029A39F /* XString_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C261241A752E0029A39F /* XString_test.cpp */; }; + 9A57C265241A752E0029A39F /* XString_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C261241A752E0029A39F /* XString_test.cpp */; }; + 9A57C268241A799B0029A39F /* XString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C267241A799B0029A39F /* XString.cpp */; }; + 9A57C269241A799B0029A39F /* XString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C267241A799B0029A39F /* XString.cpp */; }; + 9A57C26A241A799B0029A39F /* XString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A57C267241A799B0029A39F /* XString.cpp */; }; 9A9223312402FD1000483CBA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9223302402FD1000483CBA /* main.cpp */; }; 9AA05BB7240FC78D008EA4F8 /* utf8Conversion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9AA05BB6240FC78D008EA4F8 /* utf8Conversion.cpp */; }; 9AA05BB8240FC78D008EA4F8 /* utf8Conversion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9AA05BB6240FC78D008EA4F8 /* utf8Conversion.cpp */; }; @@ -122,6 +128,10 @@ 9A57C2132418A6E90029A39F /* XStringWP.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XStringWP.cpp; sourceTree = ""; }; 9A57C2142418A6EA0029A39F /* XStringWP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XStringWP.h; sourceTree = ""; }; 9A57C22F2418B9A00029A39F /* cpp_tests UTF16 unsigned char */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "cpp_tests UTF16 unsigned char"; sourceTree = BUILT_PRODUCTS_DIR; }; + 9A57C261241A752E0029A39F /* XString_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XString_test.cpp; sourceTree = ""; }; + 9A57C262241A752E0029A39F /* XString_test.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XString_test.h; sourceTree = ""; }; + 9A57C266241A799B0029A39F /* XString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XString.h; sourceTree = ""; }; + 9A57C267241A799B0029A39F /* XString.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XString.cpp; sourceTree = ""; }; 9A92232D2402FD1000483CBA /* cpp_tests UTF16 signed char */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "cpp_tests UTF16 signed char"; sourceTree = BUILT_PRODUCTS_DIR; }; 9A9223302402FD1000483CBA /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 9A92234D2402FD9500483CBA /* Platform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Platform.h; sourceTree = ""; }; @@ -165,6 +175,8 @@ 9AA05BB9240FC796008EA4F8 /* utf8Conversion.h */, 9A0B084A2402FE9300E2B470 /* XArray.h */, 9A0B084B2402FE9300E2B470 /* XObjArray.h */, + 9A57C267241A799B0029A39F /* XString.cpp */, + 9A57C266241A799B0029A39F /* XString.h */, 9A0B08482402FE9300E2B470 /* XStringW.cpp */, 9A0B08462402FE9300E2B470 /* XStringW.h */, 9A0B08472402FE9300E2B470 /* XStringWArray.cpp */, @@ -193,6 +205,8 @@ 9A0B08502402FE9B00E2B470 /* XObjArray_tests.h */, 9A0B08552402FE9B00E2B470 /* XStringW_test.cpp */, 9A0B08632403132300E2B470 /* XStringW_test.h */, + 9A57C261241A752E0029A39F /* XString_test.cpp */, + 9A57C262241A752E0029A39F /* XString_test.h */, 9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */, 9A0B084D2402FE9B00E2B470 /* XStringWArray_test.h */, 9A57C20F2418A3550029A39F /* XUINTN_test.cpp */, @@ -339,6 +353,7 @@ 9A57C20E2418A27B0029A39F /* XUINTN.cpp in Sources */, 9A0B08732403B08400E2B470 /* XObjArray_tests.cpp in Sources */, 9A0B08742403B08400E2B470 /* main.cpp in Sources */, + 9A57C264241A752E0029A39F /* XString_test.cpp in Sources */, 9A0B08752403B08400E2B470 /* printf_lite.cpp in Sources */, 9A0B08772403B08400E2B470 /* XStringWArray.cpp in Sources */, 9A57C2122418A3560029A39F /* XUINTN_test.cpp in Sources */, @@ -353,6 +368,7 @@ 9A0B087E2403B08400E2B470 /* XArray_tests.cpp in Sources */, 9A0B087F2403B08400E2B470 /* Platform.cpp in Sources */, 9A0B08802403B08400E2B470 /* XStringW_test.cpp in Sources */, + 9A57C269241A799B0029A39F /* XString.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -363,6 +379,7 @@ 9A57C2192418B9A00029A39F /* XUINTN.cpp in Sources */, 9A57C21A2418B9A00029A39F /* XObjArray_tests.cpp in Sources */, 9A57C21B2418B9A00029A39F /* main.cpp in Sources */, + 9A57C265241A752E0029A39F /* XString_test.cpp in Sources */, 9A57C21C2418B9A00029A39F /* printf_lite.cpp in Sources */, 9A57C21D2418B9A00029A39F /* XStringWArray.cpp in Sources */, 9A57C21E2418B9A00029A39F /* XUINTN_test.cpp in Sources */, @@ -377,6 +394,7 @@ 9A57C2272418B9A00029A39F /* XArray_tests.cpp in Sources */, 9A57C2282418B9A00029A39F /* Platform.cpp in Sources */, 9A57C2292418B9A00029A39F /* XStringW_test.cpp in Sources */, + 9A57C26A241A799B0029A39F /* XString.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -387,6 +405,7 @@ 9A57C20D2418A27B0029A39F /* XUINTN.cpp in Sources */, 9A0B085A2402FF8400E2B470 /* XObjArray_tests.cpp in Sources */, 9A9223312402FD1000483CBA /* main.cpp in Sources */, + 9A57C263241A752E0029A39F /* XString_test.cpp in Sources */, 9A0B086D24039FE700E2B470 /* printf_lite.cpp in Sources */, 9A0B08562402FF7700E2B470 /* XStringWArray.cpp in Sources */, 9A57C2112418A3560029A39F /* XUINTN_test.cpp in Sources */, @@ -401,6 +420,7 @@ 9A0B085B2402FF8700E2B470 /* XArray_tests.cpp in Sources */, 9A0B085E240300E000E2B470 /* Platform.cpp in Sources */, 9A0B085C2402FF8B00E2B470 /* XStringW_test.cpp in Sources */, + 9A57C268241A799B0029A39F /* XString.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Xcode/cpp_tests/src/Platform.cpp b/Xcode/cpp_tests/src/Platform.cpp index cb73dbfa4..1fd07ff88 100755 --- a/Xcode/cpp_tests/src/Platform.cpp +++ b/Xcode/cpp_tests/src/Platform.cpp @@ -69,6 +69,11 @@ UINTN AsciiStrLen(const char* String) return (UINTN)strlen(String); } +INTN AsciiStrCmp (const char *FirstString,const char *SecondString) +{ + return (INTN)strcmp(FirstString, SecondString); +} + #if __WCHAR_MAX__ <= 0xFFFFu #ifndef _MSC_VER diff --git a/Xcode/cpp_tests/src/Platform.h b/Xcode/cpp_tests/src/Platform.h index 022e0ac88..1a2df02e6 100755 --- a/Xcode/cpp_tests/src/Platform.h +++ b/Xcode/cpp_tests/src/Platform.h @@ -61,7 +61,7 @@ int StrCmp(const wchar_t* FirstString, const wchar_t* SecondString); int StrnCmp(const wchar_t* FirstString, const wchar_t* SecondString, UINTN Length); UINTN StrLen(const wchar_t* String); UINTN AsciiStrLen(const char* String); - +INTN AsciiStrCmp (const char *FirstString,const char *SecondString); diff --git a/rEFIt_UEFI/cpp_foundation/XArray.h b/rEFIt_UEFI/cpp_foundation/XArray.h index 1c13acf84..d5e025f4d 100755 --- a/rEFIt_UEFI/cpp_foundation/XArray.h +++ b/rEFIt_UEFI/cpp_foundation/XArray.h @@ -37,8 +37,8 @@ class XArray virtual ~XArray(); public: - const TYPE *Data() const { return m_data; } - TYPE *Data() { return m_data; } + const TYPE *data() const { return m_data; } + TYPE *data() { return m_data; } public: xsize AllocatedSize() const { return m_allocatedSize; } @@ -137,7 +137,7 @@ template XArray::XArray(const XArray &anArray) { Init(); - AddArray(anArray.Data(), anArray.size()); + AddArray(anArray.data(), anArray.size()); } /* operator = */ @@ -147,7 +147,7 @@ const XArray &XArray::operator =(const XArray &anArray) xsize ui; setEmpty(); - for ( ui=0 ; ui &XObjArray::operator =(const XObjArray &anObjA xsize ui; XObjArrayNC::Empty(); - CheckSize(anObjArray.Length(), 0); + CheckSize(anObjArray.length(), 0); for ( ui=0 ; ui::RemoveAtIndex(xsize nIndex) if ( nIndex < XObjArrayNC::_Len ) { if ( nIndex >= XObjArrayNC::_Len ) { - DebugLog(2, "void XObjArrayNC::RemoveAtIndex(xsize nIndex) : BUG nIndex (%d) is > Length(). System halted\n", nIndex); + DebugLog(2, "void XObjArrayNC::RemoveAtIndex(xsize nIndex) : BUG nIndex (%d) is > length(). System halted\n", nIndex); panic(); } } diff --git a/rEFIt_UEFI/cpp_foundation/XString.cpp b/rEFIt_UEFI/cpp_foundation/XString.cpp new file mode 100755 index 000000000..3f0d433db --- /dev/null +++ b/rEFIt_UEFI/cpp_foundation/XString.cpp @@ -0,0 +1,1086 @@ +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// STRING +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +#if !defined(__XSTRING_CPP__) +#define __XSTRING_CPP__ + +#if 0 +#define DBG(...) DebugLog(2, __VA_ARGS__) +#else +#define DBG(...) +#endif + +#include "XToolsCommon.h" +#include "XString.h" +#include "XStringW.h" + +#include "printf_lite.h" + +#define memcpy(dest,source,count) CopyMem(dest,(void*)(source),(UINTN)(count)) +#define strlen(str) (xsize)(AsciiStrLen(str)) +#define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(count)) + +xsize XStringGrowByDefault = 1024; +const XString NullXString; + + +void XString::Init(xsize aSize) +{ + c = (char*)Xalloc( (aSize+1)*sizeof(char) ); /* le 0 terminal n'est pas compté dans mSize */ + if ( !c ) { + DebugLog(2, "XString::Init(%d) : Xalloc returned NULL. Cpu halted\n", (aSize+1)*sizeof(char)); + panic(); + } + m_allocatedSize = aSize; + c[0] = 0; +} + +XString::XString() +{ +//Debugf("Construteur\n"); + Init(); +} + +XString::~XString() +{ +//Debugf("Destructeur :%s\n", c); + delete c; +} + +void XString::SetLength(xsize len) +{ + CheckSize(len); + c[len] = 0; +} + +/* CheckSize() */ +char *XString::CheckSize(xsize nNewSize, xsize nGrowBy) +{ + if ( m_allocatedSize < nNewSize ) + { + + nNewSize += nGrowBy; + c = (char*)Xrealloc(m_allocatedSize*sizeof(wchar_t), (nNewSize+1)*sizeof(char), c); + if ( !c ) { + DBG("XString::CheckSize(%d, %d) : Xrealloc(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, m_size, (nNewSize+1)*sizeof(char), c); + panic(); + } + m_allocatedSize = nNewSize; + } + return c; +} + +void XString::StrnCpy(const char *buf, xsize len) +{ + if ( buf && *buf && len > 0 ) { + CheckSize(len, 0); + xsize idx = 0; + char* p = data(); + while ( idx++ < len && (*p++ = *buf++)); + SetLength(idx-1); /* SetLength fait _Data[len]=0 */ + }else{ + SetLength(0); /* SetLength fait _Data[len]=0 */ + } +} + +void XString::StrCpy(const char *buf) +{ + if ( buf && *buf ) { + StrnCpy(buf, (xsize)strlen(buf)); // overflow ? + }else{ + SetLength(0); /* SetLength fait _Data[len]=0 */ + } +} + +//inline +void XString::StrnCat(const char *buf, xsize len) +{ + xsize NewLen; + + if ( buf && *buf && len > 0 ) { + NewLen = length()+len; + CheckSize(NewLen, 0); + memcpy(data()+length(), buf, len); + SetLength(NewLen); /* SetLength fait data()[len]=0 */ + } +} + +void XString::Cat(const XString &uneXString) +{ + CheckSize(length()+uneXString.length()); + memcpy(data()+length(), uneXString.c, uneXString.length()); + SetLength(length() + uneXString.length()); +} + +inline void XString::StrnCat(const char *buf) +{ + if ( buf && *buf ) { + StrnCat(buf, (xsize)strlen(buf)); // overflow ? + } +} + +void XString::Delete(xsize pos, xsize count) +{ + if ( pos < length() ) { + if ( count != MAX_XSIZE && pos + count < length() ) { + memmove( data()+pos, data()+pos+count, length()-pos-count); + SetLength(length()-count); +// data()[length()] = 0; fait dans setlength(); + }else{ + SetLength(pos); +// data()[length()] = 0; fait dans setlength(); + } + } +} + +void XString::Insert(xsize pos, const XString& Str) +{ + if ( pos < length() ) { + CheckSize(length()+Str.length()); + memmove(data()+pos+Str.length(), data()+pos, length()-pos+1); // +1 to copy the NULL terminator + memcpy(data()+pos, Str.data(), Str.length()); + }else{ + StrnCat(Str); + } +} + +void XString::Replace(char c1, char c2) +{ + char* p; + + p = data(); + while ( *p ) { + if ( *p == c1 ) *p = c2; + p += 1; + } +} + +XString XString::SubStringReplace(char c1, char c2) +{ + char* p; + XString Result; + + p = data(); + while ( *p ) { + if ( *p == c1 ) Result += c2; + else Result += *p; + p++; + } + return Result; +} +// +//void XString::vSPrintf(const char *Format, va_list va) +//{ +// char buf[16384]; // faire mieux un jour... +// int nb; +// +// #if defined(__DEV_BORLANDC__) || defined(__DEV_UNIX__) +// nb = vsprintf(buf, Format, va); +// #elif defined(__DEV_WIN32__) +// nb = _vsnprintf(buf, sizeof(buf), Format, va); +// #else +// #error Plateforme false supportee +// #endif +// if ( nb > 0 ) StrnCpy(buf, nb); +// else SetNull(); +//} + +static XString* XString_sprintfBuf; +static xsize XString_sprintfBuf_len; +static wchar_t XString_char_wait; + +static unsigned int XString_transmitSprintf_utf32(const wchar_t wchar1, const wchar_t wchar2) +{ + unsigned int ret = 0; + UINTN utf32_char; + + if ((wchar1 & 0xFC00) == 0xD800) { /* surrogates */ + if ((wchar2 & 0xFC00) == 0xDC00) { + utf32_char = wchar1; + utf32_char &= 0x03FF; + utf32_char <<= 10; + utf32_char |= ((UINTN)wchar2) & 0x03FF; + utf32_char += 0x10000; + ret = 2; + }else{ + // error + return 1; // Ignore wchar1. Tell the caller we used wchar1 + } + }else{ + utf32_char = wchar1; + ret = 1; + } + + /* assertion: utf32_char is a single UTF-4 value */ + int bits; + + if (utf32_char < 0x80) { + (*XString_sprintfBuf) += (char)utf32_char; + bits = -6; + } + else if (utf32_char < 0x800) { + (*XString_sprintfBuf) += (char)(((utf32_char >> 6) & 0x1F) | 0xC0); + bits = 0; + } + else if (utf32_char < 0x10000) { + (*XString_sprintfBuf) += (char)(((utf32_char >> 12) & 0x0F) | 0xE0); + bits = 6; + } + else { + (*XString_sprintfBuf) += (char)(((utf32_char >> 18) & 0x07) | 0xF0); + bits = 12; + } + for (; bits >= 0; bits -= 6) { + (*XString_sprintfBuf) += (char)(((utf32_char >> bits) & 0x3F) | 0x80); + } + return ret; +} + + +static void XString_transmitSprintf(const wchar_t* buf, size_t nbchar) +{ + + #if __WCHAR_MAX__ <= 0xFFFF + // wchar_t is UTF16 + + unsigned int ret = 1; + if ( XString_char_wait ) { + ret = XString_transmitSprintf_utf32(XString_char_wait, buf[0]); + XString_char_wait = 0; + } + xsize i; + for ( i = ret-1 ; i < nbchar-1 ; ) // cast ok, ret > + { + ret = XString_transmitSprintf_utf32(buf[i], buf[i+1]); + i += ret; + } + if ( i < nbchar ) XString_char_wait = buf[i]; + #else + #error TODO + #endif +} + +void XString::vSPrintf(const char* format, VA_LIST va) +{ + SetLength(0); + + XString_sprintfBuf = this; + XString_sprintfBuf_len = 0; + XString_char_wait = 0; + vprintf_with_callback(format, va, XString_transmitSprintf); + if ( XString_char_wait ) XString_transmitSprintf_utf32(XString_char_wait, 0); + + // This is an attempt to use _PPrint from IO.c. Problem is : you have to allocate the memory BEFORE calling it. +// POOL_PRINT spc; +// PRINT_STATE ps; +// +// ZeroMem(&spc, sizeof (spc)); +// spc.Str = data(); +// SetLength(0); +// spc.Len = 0; +// spc.Maxlen = m_size; +// ZeroMem(&ps, sizeof (ps)); +// ps.Output = (IN EFI_STATUS (EFIAPI *)(VOID *context, CONST CHAR16 *str))_PoolPrint; +// ps.Context = (void*)&spc; +// ps.fmt.u.pw = format; +// +// VA_COPY(ps.args, va); +// _PPrint (&ps); +// VA_END(ps.args); +} + +void XString::SPrintf(const char *Format, ...) +{ + va_list va; + + va_start(va, Format); + vSPrintf(Format, va); + va_end(va); +} + +// +//XStringW XString::wcs() +//{ +// XBuffer xbuf; +//// wchar_t buf[length() + 1]; +// size_t nbchar = mbstowcs((wchar_t *)xbuf.DataWithSizeMin(0, sizeof(wchar_t)*length()), c, length()); +// if ( nbchar == (size_t)-1 ) throw("XString::wcs() -> Conversion error"); +// return XStringW((wchar_t *)xbuf.data(), xsize(nbchar)); // safe cast : nbchar cannot be > than Length +//} + +XString XString::basename() const +{ + if ( LastChar() == PATH_SEPARATOR ) { + DebugLog(2, "XString::basename() -> LastChar() == PATH_SEPARATOR"); + panic(); + } + xsize idx = RIdxOf(PATH_SEPARATOR); + if ( idx == MAX_XSIZE ) return NullXString; + return SubString(idx+1, length()-idx-1); +} + +XString XString::dirname() const +{ + xsize idx = RIdxOf(PATH_SEPARATOR); + if ( idx == MAX_XSIZE ) return NullXString; + #ifdef __DEV_WIN32__ + if ( idx == 1 && *data(0) == PATH_SEPARATOR ) { + // this string is an icomplete UNC name : \\server + return NullXString; + } + #endif + return SubString(0, idx); +} + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Constructeurs Chaines +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +XString::XString(const XString &aString) +{ +//Debugf("Construteur const String & : %s\n", aString); + Init(aString.length()); + StrnCpy(aString.data(), aString.length()); +} + +XString::XString(const wchar_t *S) +{ + Init(); + SPrintf("%ls", S); +} + +/* +XString::XString(const NSString* const aNSString, NSStringEncoding encoding) +{ + char buf[ [aNSString length]*MB_CUR_MAX+MB_CUR_MAX ]; + if ( [aNSString getCString:buf maxLength:[aNSString length]*MB_CUR_MAX+MB_CUR_MAX encoding:encoding] ) + { + Init(strlen(buf)); + StrnCpy(buf); + } + else { + Init(0); + } +} +*/ + +// +//XString::XString(const XConstString &aConstString) +//{ +////Debugf("Construteur const ConstString & : %s\n", aConstString); +// Init( aConstString.length() ); +// StrnCpy( aConstString.data() ); +//} + +XString::XString(const char *S) +{ +//Debugf("Construteur const char * : %s\n", S); + Init((xsize)strlen(S)); // overflow ? + if ( S ) StrCpy(S); +} + +XString::XString(const char *S, xsize count) +{ +//Debugf("Construteur const char *, unsigned int :%s %d\n", S, count); + Init(count); + StrnCpy(S, count); +} + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Constructeurs CaractËres +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +XString::XString(char aChar) +{ +//TRACE("Construteur char \n"); + Init(1); + StrnCpy(&aChar, 1); +} + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Constructeurs numériques +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +//XString::XString(int i) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Copy Constructor(int) -> (%lu) %d\n", this, i); +// len = sprintf(buf,"%d", i); +// Init(len); +// StrnCpy(buf, len); +//} +// +//XString::XString(unsigned int ui) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Copy Constructor(unsigned int) -> (%lu) %u\n", this, ui); +// len = sprintf(buf,"%u", ui); +// Init(len); +// StrnCpy(buf, len); +//} +// +//XString::XString(long l) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Copy Constructor(long) -> (%lu) %ld\n", this, l); +// len = sprintf(buf,"%ld", l); +// Init(len); +// StrnCpy(buf, len); +//} +// +//XString::XString(unsigned long ul) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Copy Constructor(unsigned long long) -> (%lu) %lu\n", this, ul); +// len = sprintf(buf,"%lu", ul); +// Init(len); +// StrnCpy(buf, len); +//} +// + +//------------------------------------------------------------------------------------------------- +// StringCompare +//------------------------------------------------------------------------------------------------- +// -1 si buf1 est plus grand +// 0 si égal +// 1 si buf2 est plus grand +//------------------------------------------------------------------------------------------------- + + +XString XString::SubString(xsize pos, xsize count) const +{ + if ( count > length()-pos ) count = length()-pos; + return XString( &(data()[pos]), count); +} + +xsize XString::IdxOf(char aChar, xsize Pos) const +{ + xsize Idx; + + for ( Idx=Pos ; Idx length() ) Pos = length(); + if ( Pos < S.length() ) return MAX_XSIZE; + Pos -= S.length(); + for ( Idx=Pos+1 ; Idx-- > 0 ; ) { + i = 0; + while( i 0 ) + { + unsigned int ui; + + if ( FirstCharIsCap ) { + data()[0] = Majuscule(data()[0]); + ui = 1; + }else{ + ui = 0; + } + for ( ; ui < length() ; ui+=1 ) { + data()[ui] = Minuscule(data()[ui]); + } + } +} + +bool XString::IsLetters() const +{ + const char *p; + char aChar; + + p = data(); + if ( !*p ) return false; + for ( ; *p ; p+=1 ) { + aChar = MinusculeSansAccent(*p); // toutes les lettres, avec accent ou pas, seront dans l'intervalle 'a'..'z' + if ( aChar < 'a' ) return false; + if ( aChar > 'z' ) return false; + } + return true; +} +#endif + +bool XString::IsDigits() const +{ + const char *p; + + p = data(); + if ( !*p ) return false; + for ( ; *p ; p+=1 ) { + if ( *p < '0' ) return false; + if ( *p > '9' ) return false; + } + return true; +} + +bool XString::IsDigits(xsize pos, xsize count) const +{ + const char *p; + const char *q; + + if ( pos >= length() ) { + DebugLog(2, "XString::IsDigits pos >= length()"); // We should #ifdef this to keep that for debug + return false; + } + if ( pos+count > length() ) { + DebugLog(2, "XString::IsDigits pos+count > length()"); // We should #ifdef this to keep that for debug + return false; + } + p = data() + pos; + q = p + count; + for ( ; p < q ; p+=1 ) { + if ( *p < '0' ) return false; + if ( *p > '9' ) return false; + } + return true; +} + +#ifdef NOT_USED_ANYMORE_skqdjfhksqjhfksjqdf +bool XString::IsLettersNoAccent() const +{ + const char *p; + char aChar; + + p = data(); + if ( !*p ) return false; + for ( ; *p ; p+=1 ) { + aChar = Minuscule(*p); // Uniquement les lettres maj et min sans accent seront dans l'intervalle 'a'..'z' + if ( aChar < 'a' ) return false; + if ( aChar > 'z' ) return false; + } + return true; +} +#endif + +void XString::RemoveLastEspCtrl() +{ + char *p; + + if ( length() > 0 ) { + p = data() + length() - 1; + if ( *p >= 0 && *p <= ' ' ) { + p -= 1; + while ( p>data() && *p >= 0 && *p <= ' ' ) p -= 1; + if ( p>data() ) { + SetLength( (xsize)(p-data()+1) ); // safe (p-data()+1 < length() + }else{ + if ( *p >= 0 && *p <= ' ' ) SetLength(0); + else SetLength(1); + } + } + } +} +// +//int XString::ToInt() const +//{ +// int i; +// +// if ( sscanf(data(), "%d", &i) != 1 ) return NOTAINT; +// return i; +//} +// +//unsigned int XString::TOUInt() const +//{ +// unsigned int u; +// +// if ( sscanf(data(), "%u", &u) != 1 ) return MAXUINT; +// return u; +//} + +//------------------------------------------------------------------------------------------------- +// +//------------------------------------------------------------------------------------------------- +// +//bool XString::ReadFromBuf(const char *buf, size_t *idx, size_t count) +//{ +// xsize longueur; +// +////mylog2(::SPrintf("XString::ReadFromBuf *idx=%d count=%d - %d %d %d %d\n", *idx, count, buf[*idx+0], buf[*idx+1], buf[*idx+2], buf[*idx+3]).c); +// if ( count-*idx >= sizeof(longueur) ) { +// longueur = *((xsize *)(buf+*idx)); +////mylog2(::SPrintf("XString::ReadFromBuf *idx=%d count=%d longueur=%d\n", *idx, count, longueur).c); +// *idx += sizeof(longueur); +// if ( longueur > 0 && count-*idx>=longueur ) memcpy(DataWithSizeMin(0, longueur), buf+*idx, longueur); +// *idx += longueur; +// SetLength(longueur); +// return true; +// }else{ +// SetNull(); +// return false; +// } +//} +// +//bool XString::WriteToBuf(char *buf, size_t *idx, size_t count) const +//{ +// xsize longueur; +// +// if ( count-*idx < sizeof(longueur) + length() ) return false; +// longueur = length(); +// memcpy(buf+*idx, &longueur, sizeof(longueur)); +// *idx += sizeof(longueur); +// memcpy(buf+*idx, data(), length()); +// *idx += length(); +// return true; +//} +// +//bool XString::ReadFromFILE(FILE *fp) +//{ +// unsigned int longueur; +// +// if ( fread(&longueur, sizeof(longueur), 1, fp) != 1 ) goto fin; +// if ( longueur > 0 && fread(DataWithSizeMin(0, longueur), longueur, 1, fp) != 1 ) goto fin; +// SetLength(longueur); +// return true; +// fin: +// SetNull(); +// return false; +//} +// +//bool XString::WriteToFILE(FILE *fp) const +//{ +// xsize longueur; +// +// longueur = length(); +// if ( fwrite(&longueur, sizeof(longueur), 1, fp) != 1 ) return false; +// if ( longueur > 0 && fwrite(data(), longueur, 1, fp) != 1 ) return false; +// return true; +//} +// +//#ifdef __DEVTOOLS_SOCKETS__ +//void XString::ReadFromSOCKETT(SOCKET Sock, unsigned int LenMax, unsigned int TO, const char *ErrMsg) +//{ +// unsigned int longueur; +// +// SockReceiveT(Sock, &longueur, sizeof(longueur), TO, ErrMsg); +// if ( longueur > LenMax ) Throw("Longueur reÁue (%d) supérieure ‡ la longueur max (%d)", longueur, LenMax); +// if ( longueur > 0 ) SockReceiveT(Sock, DataWithSizeMin(0, longueur, 0), longueur, TO, ErrMsg); +// SetLength(longueur); +//} +// +//bool XString::ReadFromSOCKET(SOCKET Sock, unsigned int LenMax, unsigned int TO, const char *ErrMsg) +//{ +// try +// { +// ReadFromSOCKETT(Sock, LenMax, TO, ErrMsg); +// SetLastErrorFlag(false); +// } +// StdCatch(); +// return !LastErrorFlag(); +//} +// +//void XString::WriteToSOCKETT(SOCKET Sock, unsigned int TO,const char *ErrMsg) const +//{ +// unsigned int longueur; +// +// longueur = length(); +// SockSendT(Sock, &longueur, sizeof(longueur), TO, ErrMsg); +// if ( longueur > 0 ) SockSendT(Sock, data(), longueur, TO, ErrMsg); +//} +// +//bool XString::WriteToSOCKET(SOCKET Sock, unsigned int TO,const char *ErrMsg) const +//{ +// try +// { +// WriteToSOCKET(Sock, TO, ErrMsg); +// SetLastErrorFlag(false); +// } +// StdCatch(); +// return !LastErrorFlag(); +//} +//#endif +// +//bool XString::ReadFromXBuffer(XRBuffer &unXBuffer) +//{ +// xsize longueur; +// +// if ( !unXBuffer.GetXSize(&longueur) ) goto fin; +// if ( longueur>0 && !unXBuffer.Get(DataWithSizeMin(0, longueur), longueur) ) goto fin; +// SetLength(longueur); +// return true; +//fin: +// SetNull(); +// return false; +//} +// +//void XString::CatToXBuffer(XBuffer *unXBuffer) const +//{ +// (*unXBuffer).Cat(length()); +// (*unXBuffer).Cat(data(), length()); +//} + +//************************************************************************************************* +// +// Opérateurs = +// +//************************************************************************************************* + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = CaractËres +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +const XString &XString::operator =(char aChar) +{ +//TRACE("Operator =char \n"); + StrnCpy(&aChar, 1); + return *this; +} + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = Chaines +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +const XString &XString::operator =(const XString &aString) +{ +//TRACE("Operator =const XString&\n"); + StrnCpy(aString.data(), aString.length()); + return *this; +} + +#ifdef __AFXWIN_H__ +const XString &XString::operator =(const CString &aCString) +{ +//TRACE("Operator =const CString&\n"); + StrnCpy(aCString, aCString.GetLength()); + return *this; +} +#endif + +#ifdef _WX_WXSTRINGH__ +const XString &XString::operator =(const wxString &awxString) +{ +//TRACE("Operator =const wxString&\n"); + StrnCpy(awxString.mb_str(), awxString.length()); + return *this; +} +#endif +// +//const XString &XString::operator =(const XConstString &aConstString) +//{ +////TRACE("Operator =const XString&\n"); +// StrnCpy(aConstString.data()); +// return *this; +//} + +const XString &XString::operator =(const char *S) +{ +//TRACE("Operator =const char *\n"); + StrCpy(S); + return *this; +} + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = numériques +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +//const XString &XString::operator =(int i) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(int) -> (%lu) %d\n", this, i); +// len = sprintf(buf,"%d", i); +// StrnCpy(buf, len); +// return *this; +//} +// +//const XString &XString::operator =(unsigned int ui) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(long) -> (%lu) %u\n", this, ui); +// len = sprintf(buf,"%u", ui); +// StrnCpy(buf, len); +// return *this; +//} +// +//const XString &XString::operator =(long l) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(long) -> (%lu) %ld\n", this, l); +// len = sprintf(buf,"%ld", l); +// StrnCpy(buf, len); +// return *this; +//} +// +//const XString &XString::operator =(unsigned long ul) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(long) -> (%lu) %lu\n", this, ul); +// len = sprintf(buf,"%lu", ul); +// StrnCpy(buf, len); +// return *this; +//} +// + + +//************************************************************************************************* +// +// Opérateurs += +// +//************************************************************************************************* + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = CaractËres +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +const XString &XString::operator +=(char aChar) +{ +//TRACE("Operator +=char \n"); + StrnCat(&aChar, 1); + return *this; +} + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = Chaines +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +const XString &XString::operator +=(const XString &aString) +{ +//TRACE("Operator +=const XString&\n"); + StrnCat(aString.data(), aString.length()); + return *this; +} +// +//const XString &XString::operator +=(const XConstString &aConstString) +//{ +////TRACE("Operator +=const EConstString&\n"); +// StrnCat(aConstString.data(), aConstString.length()); +// return *this; +//} + +const XString &XString::operator +=(const char *S) +{ +//TRACE("operator +=const char *\n"); + StrnCat(S); + return *this; +} + + +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// Opérateur = numériques +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +// +//const XString &XString::operator +=(int i) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(int) -> (%lu) %d\n", this, i); +// len = sprintf(buf,"%d", i); +// StrnCat(buf, len); +// return *this; +//} +// +//const XString &XString::operator +=(unsigned int ui) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(unsigned int) -> (%lu) %u\n", this, ui); +// len = sprintf(buf,"%u", ui); +// StrnCat(buf, len); +// return *this; +//} +// +//const XString &XString::operator +=(long l) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(long) -> (%lu) %ld\n", this, l); +// len = sprintf(buf,"%ld", l); +// StrnCat(buf, len); +// return *this; +//} +// +//const XString &XString::operator +=(unsigned long ul) +//{ +// char buf[1024]; +// unsigned int len; +// +////TRACE("Opérateur =(unsigned long long) -> (%lu) %lu\n", this, ul); +// len = sprintf(buf,"%lu", ul); +// StrnCat(buf, len); +// return *this; +//} + + +//----------------------------------------------------------------------------- +// Fonction +//----------------------------------------------------------------------------- + +XString SPrintf(const char *format, ...) +{ + VA_LIST va; + XString str; + + VA_START (va, format); + str.vSPrintf(format, va); + VA_END(va); + + return str; +} + +XString SubString(const char *S, xsize pos, xsize count) +{ + if ( strlen(S)-pos < count ) count = (xsize)(strlen(S)-pos); // overflow ? + return ( XString(S+pos, count) ); +} + +#ifdef NOT_USED_ANYMORE_skqdjfhksqjhfksjqdf +XString ToAlpha(const char *S) +{ + XString ReturnValue; + unsigned int i; + + for ( i=0 ; S[i] ; i+=1 ) { + ReturnValue += ToAlpha(S[i]); + } + return ReturnValue; +} + +XString ToAlpha(const XString &S) +{ + XString ReturnValue; + unsigned int i; + + for ( i=0 ; i=0 && S[i] < ' ' ) ReturnValue += 'x'; /* Les char sont signés !!! */ + else ReturnValue += S[i]; + } + return ReturnValue; +} + +#endif diff --git a/rEFIt_UEFI/cpp_foundation/XString.h b/rEFIt_UEFI/cpp_foundation/XString.h new file mode 100755 index 000000000..0dc2ded55 --- /dev/null +++ b/rEFIt_UEFI/cpp_foundation/XString.h @@ -0,0 +1,285 @@ +//************************************************************************************************* +//************************************************************************************************* +// +// STRING +// +//************************************************************************************************* +//************************************************************************************************* + +#if !defined(__XSTRING_H__) +#define __XSTRING_H__ + +#if 0 +#define DBG(...) DebugLog(2, __VA_ARGS__) +#else +#define DBG(...) +#endif + +#include "XToolsCommon.h" +#include "utf8Conversion.h" + +#define PATH_SEPARATOR '\\' + +extern UINTN XStringGrowByDefault; + + +//class XConstString; +class XStringW; + +class XString +{ + protected: +// char *_Data; + + public: + char *c; + xsize m_allocatedSize; + + void Init(xsize aSize=0); + XString(); + XString(const XString &aString); + +// XString(const XConstString &aConstString); + XString(const char *S); + XString(const char* S, xsize count); + + XString(const wchar_t *S); + +// XString(uchar); + XString(char); + XString(int); + XString(unsigned long long); + + ~XString(); + + public: + char *CheckSize(xsize nNewSize, xsize nGrowBy = XStringGrowByDefault); + + public: + const char *data(xsize ui=0) const { return c+ui; } + char *data(xsize ui=0) { return (char*)(c+ui); } + char *data(int i) { if ( i<0 ) panic("const wchar_t *data(INTN i=0) const -> i < 0"); return c+i; } + char *DataWithSizeMin(xsize ui, xsize size, xsize nGrowBy=XStringGrowByDefault) { CheckSize(size, nGrowBy); return data(ui); } + + xsize length() const { return (xsize)AsciiStrLen(c); } + xsize Size() const { return m_allocatedSize; } + void SetLength(xsize len); + + /* IsNull ? */ + void setEmpty() { c[0] = 0; } + bool isEmpty() const { return length() == 0; } + + /* Cast */ + operator const char *() const { return data(); } +// operator char *() { return data(); } + + int ToInt() const; + UINTN ToUINTN() const; + + XStringW wcs(); + + /* char [] */ + char operator [](int i) const { + #if defined __XTOOLS_INT_CHECK__ + if ( i < 0 ) DebugLog(2, "XString index cannot < 0"); + panic(); + #endif + return *data((unsigned int)i); + } + char operator [](xsize i) const { return *data(i); } + + /* char& [] */ + char& operator [](int i) { + #if defined __XTOOLS_INT_CHECK__ + if ( i < 0 ) DebugLog(2, "XString index cannot < 0"); + panic(); + #endif + return *data(i); + } + char& operator [](xsize i) { return *data(i); } + + char LastChar() const { if ( length() > 0 ) return data()[length()-1]; else return 0; } + void RemoveLastEspCtrl(); + + void SetNull() { SetLength(0); }; + void StrCpy(const char *buf); + void StrnCpy(const char *buf, xsize len); + void StrnCat(const char *buf, xsize len); + void StrnCat(const char *buf); + void Delete(xsize pos, xsize count=1); + + void Insert(xsize pos, const XString& Str); + + void Cat(const XString &uneXString); + + void vSPrintf(const char *Format, VA_LIST va); + void SPrintf(const char *format, ...) __attribute__((format (printf, 2, 3))); // 3 and 4 because of hidden parameter. + + + const XString& takeValueFrom(const char* S) { StrCpy(S); return *this; } + + const XString &operator =(const XString &aString); +// const XString &operator =(const XConstString &aConstString); + const XString &operator =(const char* S); + const XString &operator =(char); + const XString &operator =(int); + const XString &operator =(unsigned int); + const XString &operator =(long); + const XString &operator =(unsigned long long); + + const XString &operator += (const XString &); +// const XString &operator += (const XConstString &aConstString); + const XString &operator += (const char* S); + const XString &operator += (char); + const XString &operator += (int); + const XString &operator += (unsigned int); + const XString &operator += (long); + const XString &operator += (unsigned long long); + + XString SubString(xsize pos, xsize count) const; + xsize IdxOf(char c, xsize Pos = 0) const; + xsize IdxOf(const XString &S, xsize Pos = 0) const; +#ifdef TODO_skqdjfhksqjhfksjqdf + xsize IdxOfIC(const XString &S, xsize Pos = 0) const; + xsize IdxOfIAC(const XString &S, xsize Pos = 0) const; +#endif + xsize RIdxOf(const XString &S, xsize Pos = MAX_XSIZE) const; + + void ToLower(bool FirstCharIsCap = false); + bool IsLetters() const; + bool IsLettersNoAccent() const; + bool IsDigits() const; + bool IsDigits(xsize pos, xsize count) const; + + bool ExistIn(const XString &S) const { return IdxOf(S) != MAX_XSIZE; } +#ifdef TODO_skqdjfhksqjhfksjqdf + bool ExistInIC(const XString &S) const { return IdxOfIC(S) != MAX_XSIZE; } + bool ExistInIAC(const XString &S) const { return IdxOfIC(S) != MAX_XSIZE; } + bool DeleteIC(const XString &S); +#endif + void Replace(char c1, char c2); + XString SubStringReplace(char c1, char c2); + +// int Compare(const char* S) const { return (int)AsciiStrCmp(data(), (S ? S : "")); }// AsciiStrCmp return 0 or !0, not usual strcmp +#ifdef TODO_skqdjfhksqjhfksjqdf + //IC + int CompareIC(const char* S) const { return StringCompareIC(data(), (S ? S : "")); } + // IA + int CompareIA(const char* S) const { return StringCompareIA(data(), (S ? S : "")); } + // IAC + int CompareIAC(const char* S) const { return StringCompareIAC(data(), (S ? S : "")); } +/* + int CompareIACSubString(const char* S, size_t LenS) const { return SubStringCompareIAC(data(), length(), S, LenS); } //SubStringCompareIC renvoi 0 (ÈgalitÈ) si Len1 ou Len2 == 0 + int CompareIACSubString(xsize Pos, const char* S, size_t LenS) const { return SubStringCompareIAC(data(Pos), length()-Pos, S, LenS); } //SubStringCompareIC renvoi 0 (ÈgalitÈ) si Len1 ou Len2 == 0 + int CompareIACSubString(xsize Pos, xsize Len, const char* S, size_t LenS) const { return SubStringCompareIAC(data(Pos), Len, S, LenS); } +*/ +#endif + + bool Equal(const char* S) const { return AsciiStrCmp(data(), (S ? S : "")) == 0; }; +// bool BeginEqual(const char* S) const { return StringBeginEqual(data(), S); } +// bool SubStringEqual(xsize Pos, const char* S) const { return StringBeginEqual(data(Pos), S); } +#ifdef TODO_skqdjfhksqjhfksjqdf + // IC + bool EqualIC(const char* S) const { return StringEqualIC(data(), S); } + bool BeginEqualIC(const char* S) const { return StringBeginEqualIC(data(), S); } + bool SubStringEqualIC(xsize Pos, const char* S) const { return StringBeginEqualIC(data(Pos), S); } + bool EqualSubStringIC(const char* S) const { return StringBeginEqualIC(S, data()); } + // IA + bool EqualIA(const char* S) const { return CompareIA(S) == 0; }; + // IAC + bool EqualIAC(const char* S) const { return CompareIAC(S) == 0; }; + bool BeginEqualIAC(const char* S) const { return StringBeginEqualIAC(data(), S); } + bool SubStringEqualIAC(xsize Pos, const char* S) const { return StringBeginEqualIAC(data(Pos), S); } + bool EqualSubStringIAC(const char* S) const { return StringBeginEqualIAC(S, data()); } + + +/* bool EqualIACSubString(const char* S, size_t LenS) const { return CompareIACSubString(S, LenS) == 0; } + bool EqualIACSubString(xsize Pos, const char* S, size_t LenS) const { return CompareIACSubString(Pos, S, LenS) == 0; } + bool EqualIACSubString(xsize Pos, xsize Len, const char* S, size_t LenS) const { return CompareIACSubString(Pos, Len, S, LenS) == 0; } +*/ +#endif + + XString basename() const; + XString dirname() const; + +// size_t Sizeof() const { return size_t(sizeof(xsize)+length()); } // overflow ? underflow ? +// bool ReadFromBuf(const char *buf, size_t *idx, size_t count); +// bool WriteToBuf(char *buf, size_t *idx, size_t count) const; +// bool ReadFromFILE(FILE *fp); +// bool WriteToFILE(FILE *fp) const; +// // +// bool ReadFromXBuffer(XRBuffer &unXBuffer); // Impossible de mettre le XBuffer en const car il y a une variable d'instance de XBuffer incrÈmentÈe par ReadFromXBuffer +// void CatToXBuffer(XBuffer *unXBuffer) const; +//// void WriteToXBuffer(XBuffer *unXBuffer, xsize *idx) const; + + public: + // OpÈrateur + + // Chaines + friend XString operator + (const XString& p1, const XString& p2) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (const XString& p1, const char *p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (const char *p1, const XString& p2) { XString s; s=p1; s+=p2; return s; } +// friend XString operator + (const XConstString& p1, const XString& p2) { XString s; s=p1; s+=p2; return s; } +// friend XString operator + (const XString& p1, const XConstString& p2) { XString s; s=p1; s+=p2; return s; } +// friend XString operator + (const XConstString& p1, const XConstString& p2) { XString s; s=p1; s+=p2; return s; } +// friend XString operator + (const XConstString &p1, const char *p2 ) { XString s; s=p1; s+=p2; return s; } +// friend XString operator + (const char *p1, const XConstString &p2) { XString s; s=p1; s+=p2; return s; } + // Char + friend XString operator + (const XString& p1, char p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (char p1, const XString& p2 ) { XString s; s=p1; s+=p2; return s; } + // NumÈrique + friend XString operator + (const XString& p1, int p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (int p1, const XString& p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (const XString& p1, unsigned int p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (unsigned int p1, const XString& p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (const XString& p1, long p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (long p1, const XString& p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (const XString& p1, unsigned long long p2 ) { XString s; s=p1; s+=p2; return s; } + friend XString operator + (unsigned long long p1, const XString& p2 ) { XString s; s=p1; s+=p2; return s; } + + // OpÈrateur == + // Chaines + friend bool operator == (const XString& s1, const XString& s2) { return s1.Equal(s2); } + friend bool operator == (const XString& s1, const char* s2 ) { return s1.Equal(s2); } + friend bool operator == (const char* s1, const XString& s2) { return s2.Equal(s1); } +// friend bool operator == (const XConstString &s1, const XString& s2) { return s1.Compare(s2) == 0; } +// friend bool operator == (const XString &s1, const XConstString& s2) { return s1.Compare(s2) == 0; } +// friend bool operator == (const XConstString &s1, const XConstString& s2) { return s1.Compare(s2) == 0; } + + friend bool operator != (const XString& s1, const XString& s2) { return !s1.Equal(s2); } + friend bool operator != (const XString& s1, const char* s2 ) { return !s1.Equal(s2); } + friend bool operator != (const char* s1, const XString& s2) { return !s2.Equal(s1); } +// friend bool operator != (const XConstString &s1, const XString& s2) { return s1.Compare(s2) != 0; } +// friend bool operator != (const XString &s1, const XConstString& s2) { return s1.Compare(s2) != 0; } +// friend bool operator != (const XConstString &s1, const XConstString& s2) { return s1.Compare(s2) != 0; } + +// friend bool operator < (const XString& s1, const XString& s2) { return s1.Compare(s2) < 0; } +// friend bool operator < (const XString& s1, const char* s2 ) { return s1.Compare(s2) < 0; } +// friend bool operator < (const char* s1, const XString& s2) { return s2.Compare(s1) > 0; } +// +// friend bool operator > (const XString& s1, const XString& s2) { return s1.Compare(s2) > 0; } +// friend bool operator > (const XString& s1, const char* s2 ) { return s1.Compare(s2) > 0; } +// friend bool operator > (const char* s1, const XString& s2) { return s2.Compare(s1) < 0; } +// +// friend bool operator <= (const XString& s1, const XString& s2) { return s1.Compare(s2) <= 0; } +// friend bool operator <= (const XString& s1, const char* s2 ) { return s1.Compare(s2) <= 0; } +// friend bool operator <= (const char* s1, const XString& s2) { return s2.Compare(s1) >= 0; } +// +// friend bool operator >= (const XString& s1, const XString& s2) { return s1.Compare(s2) >= 0; } +// friend bool operator >= (const XString& s1, const char* s2 ) { return s1.Compare(s2) >= 0; } +// friend bool operator >= (const char* s1, const XString& s2) { return s2.Compare(s1) <= 0; } + +}; + +extern const XString NullXString; + +XString SPrintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));; +XString SubString(const char *S, xsize pos, xsize count); +#ifdef TODO_skqdjfhksqjhfksjqdf +XString ToAlpha(const char *S); +XString ToAlpha(const XString &S); +XString ToLower(const char *S, bool FirstCharIsCap = false); +XString ToUpper(const char *S); +#endif +XString CleanCtrl(const XString &S); + +#endif diff --git a/rEFIt_UEFI/cpp_foundation/XStringW.cpp b/rEFIt_UEFI/cpp_foundation/XStringW.cpp index 0b40c04e7..b38729154 100755 --- a/rEFIt_UEFI/cpp_foundation/XStringW.cpp +++ b/rEFIt_UEFI/cpp_foundation/XStringW.cpp @@ -32,7 +32,7 @@ void XStringW::Init(UINTN aSize) //DBG("Init aSize=%d\n", aSize); m_data = (wchar_t*)Xalloc( (aSize+1)*sizeof(wchar_t) ); /* le 0 terminal n'est pas compté dans mSize */ if ( !m_data ) { - DBG("XStringW::Init(%d) : Xalloc returned NULL. Cpu halted\n", (aSize+1)*sizeof(wchar_t)); + DebugLog(2, "XStringW::Init(%d) : Xalloc returned NULL. Cpu halted\n", (aSize+1)*sizeof(wchar_t)); panic(); } m_allocatedSize = aSize; @@ -129,6 +129,8 @@ DBG("Destructor :%s\n", data()); void XStringW::SetLength(UINTN len) { //DBG("SetLength(%d)\n", len); + CheckSize(len); + m_len = len; m_data[len] = 0; @@ -251,19 +253,19 @@ XStringW XStringW::SubStringReplace(wchar_t c1, wchar_t c2) return Result; } -static XStringW* sprintfBuf; +static XStringW* XStringW_sprintfBuf; -void transmitSprintf(const wchar_t* buf, size_t nbyte) +static void XStringW_transmitSprintf(const wchar_t* buf, size_t nbyte) { - (*sprintfBuf).StrnCat(buf, nbyte); + (*XStringW_sprintfBuf).StrnCat(buf, nbyte); } void XStringW::vSPrintf(const char* format, VA_LIST va) { SetLength(0); - sprintfBuf = this; - vprintf_with_callback(format, va, transmitSprintf); + XStringW_sprintfBuf = this; + vprintf_with_callback(format, va, XStringW_transmitSprintf); // This is an attempt to use _PPrint from IO.c. Problem is : you have to allocate the memory BEFORE calling it. // POOL_PRINT spc; @@ -515,7 +517,7 @@ const XStringW &XStringW::operator +=(const wchar_t *S) // Functions //----------------------------------------------------------------------------- -XStringW SPrintf(const char* format, ...) +XStringW WPrintf(const char* format, ...) { VA_LIST va; XStringW str; diff --git a/rEFIt_UEFI/cpp_foundation/XStringW.h b/rEFIt_UEFI/cpp_foundation/XStringW.h index 6f207b972..9ba285fdf 100755 --- a/rEFIt_UEFI/cpp_foundation/XStringW.h +++ b/rEFIt_UEFI/cpp_foundation/XStringW.h @@ -15,7 +15,7 @@ #define LPATH_SEPARATOR L'\\' extern UINTN XStringWGrowByDefault; -extern void __GLOBAL__sub_I_XStringW(); +//extern void __GLOBAL__sub_I_XStringW(); class XStringW { @@ -187,9 +187,9 @@ public: //extern const XStringW NullXStringW; #ifndef _MSC_VER -XStringW SPrintf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2))); +XStringW WPrintf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2))); #else -XStringW SPrintf(const char* format, ...); +XStringW WPrintf(const char* format, ...); #endif // !__MSC_VER diff --git a/rEFIt_UEFI/cpp_unit_test/XString_test.cpp b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp new file mode 100755 index 000000000..97edd6545 --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp @@ -0,0 +1,123 @@ +#include +#include "../cpp_foundation/XString.h" +#include "../cpp_foundation/utf8Conversion.h" +#include "global_test.h" + + +//#include + + +int XString_tests() +{ + +#ifdef JIEF_DEBUG +// DebugLog(2, "XStringW_tests -> Enter\n"); +#endif + + if ( global_str1 != "global_str1" ) return 1; + if ( global_str2 != "global_str2" ) return 2; + +#ifdef XSTRINGW_HAS_CTOR_LITTERAL + XStringW str("1"); + if ( str != "1" ) return 3; + str.StrCat("2"); + if ( str != "12" ) return 4; +#endif + + XString str; + str.takeValueFrom("12"); + XString str2; + if ( !str2.isEmpty() ) return 10; + str2.StrnCpy(str.data(), 2); + if ( str2 != "12" ) return 11; + str2.StrnCat("345", 2); + if ( str2 != "1234" ) return 12; + str2.Insert(1, str); + if ( str2 != "112234" ) return 13; + str2 += "6"; + if ( str2 != "1122346" ) return 14; + + str2 = "123"; + str2.Insert(0, "x"); + if ( str2 != "x123" ) return 15; + str2.Insert(4, "y"); + if ( str2 != "x123y" ) return 16; + +//wchar_t c2 = L'Ň'; +//printf("1=%lc\n", c2); +//const char* s1 = "𐌾"; + + str2.SPrintf("%c", 'a'); // signle UTF8 ascii char + if ( str2 != "a" ) return 20; + str2.takeValueFrom("ab"); // UTF16(32) string containing ascii char + if ( str2 != "ab" ) return 21; +#ifdef _MSC_VER + // IMPORTANT : you can't pass a litteral char in a vararg function with Visual Studio (Microsoft strikes again :-). + // At least, you got a warning C4066 + // IMPORTANT2 : Litteral string containing UTF16 char are WRONG. And you don't get a warning !!! If litteral is only ascii, it's ok. + // Maybe it's compilation option butI didn't find them. + wchar_t c = 'Ň'; // using an imtermediary var for Microsoft. + + + wchar_t s[2]; // wchar_t s2[] = "Ň"; + s[0] = 'Ň'; + s[1] = 0; + + str2.SPrintf("%lc", c); // UTF16(32) char. (2 bytes in total if UTF16) + if (str2 != s) return 22; + str2.takeValueFrom(""); + if (str2.length() != 0) return 221; + str2.takeValueFrom(s); // this is a UTF8 string 2 bytes long + if (str2 != s) return 23; +#else + str2.SPrintf("%lc", L'Ň'); // signe UTF16(32) char. (2 bytes in total if UTF16) + if ( str2 != "Ň" ) return 22; + str2.takeValueFrom(""); + if (str2.length() != 0) return 221; +#ifdef XSTRING_HAS_CTOR_LITTERAL + str2.takeValueFrom("Ň"); // this is a UTF8 string 2 bytes long + if (str2 != "Ň") return 23; // utf8 litteral are converted to an XStringW if ctor is available. +#endif + str2.takeValueFrom(""); + if (str2.length() != 0) return 231; +#ifdef XSTRING_HAS_CTOR_LITTERAL + str2.takeValueFrom("Ň"); // this is a UTF8 string 2 bytes long + if (str2 != "Ň") return 24; +#endif +#endif + +#if __WCHAR_MAX__ > 0xFFFFu + str2.SPrintf("%lc", L'𐌾'); // L'𐌾' // this char cannot convert to an UTF16 char. So it doesn't compile with -fshort-wchar + if ( str2 != L'𐌾' ) return 30; +#endif + + +#ifndef _MSC_VER + // "𐌾" in UTF16 is 2 char : 0xd800, 0xdf3e + + str2.takeValueFrom("𐌾"); // this is a UTF8 string 4 bytes long + if ( str2 != "𐌾" ) return 31; + str2.takeValueFrom("𐌾"); // this is a UTF16 or UTF32 string (depending of -fshort-wchar) + if ( str2 != "𐌾" ) return 32; + +#ifdef XSTRINGW_HAS_CTOR_LITTERAL + { + XStringW str3("a"); + if ( str3 != "a" ) return 40; + XStringW str4("aŇ𐌾"); + if ( str4 != "aŇ𐌾" ) return 41; + } +#endif +#endif + + XString str3(L"Выход"); + if ( str3 != "Выход" ) return 50; + + +// XStringW CommonName("EFI\\CLOVER\\misc\\screenshot"); +// for (UINTN Index = 0; Index < 20; Index++) { +// XStringW Name = CommonName + SPrintf("%lld", Index) + ".png"; +// DebugLog(2, "XStringW_test shot: %s\n", Name.data()); +// } + return 0; +} diff --git a/rEFIt_UEFI/cpp_unit_test/XString_test.h b/rEFIt_UEFI/cpp_unit_test/XString_test.h new file mode 100644 index 000000000..1e172548c --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/XString_test.h @@ -0,0 +1 @@ +int XString_tests(); diff --git a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp index 7802eda47..c024d1986 100644 --- a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp +++ b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp @@ -6,6 +6,7 @@ #include "XArray_tests.h" #include "XObjArray_tests.h" #include "XStringWArray_test.h" +#include "XString_test.h" #include "XStringW_test.h" #include "XUINTN_test.h" @@ -26,6 +27,11 @@ bool all_tests() DebugLog(2, "XObjArray_tests() failed at test %d\n", ret); all_ok = false; } + ret = XString_tests(); + if ( ret != 0 ) { + DebugLog(2, "XString_tests() failed at test %d\n", ret); + all_ok = false; + } ret = XStringW_tests(); if ( ret != 0 ) { DebugLog(2, "XStringW_tests() failed at test %d\n", ret); diff --git a/rEFIt_UEFI/cpp_unit_test/global_test.cpp b/rEFIt_UEFI/cpp_unit_test/global_test.cpp index 50a1a5d31..a6ef97966 100644 --- a/rEFIt_UEFI/cpp_unit_test/global_test.cpp +++ b/rEFIt_UEFI/cpp_unit_test/global_test.cpp @@ -1,8 +1,12 @@ #include #include "global_test.h" +#include "../cpp_foundation/XString.h" #include "../cpp_foundation/XStringW.h" +XStringTest global_str1("global_str1"); +XStringTest global_str2("global_str2"); + XStringWTest global_str3(L"global_str3"); XStringWTest global_str4(L"global_str4"); diff --git a/rEFIt_UEFI/cpp_unit_test/global_test.h b/rEFIt_UEFI/cpp_unit_test/global_test.h index 4dea238bf..b222f0753 100644 --- a/rEFIt_UEFI/cpp_unit_test/global_test.h +++ b/rEFIt_UEFI/cpp_unit_test/global_test.h @@ -1,3 +1,4 @@ +#include "../cpp_foundation/XString.h" #include "../cpp_foundation/XStringW.h" class XStringWTest : public XStringW @@ -9,5 +10,17 @@ public: } }; +class XStringTest : public XString +{ +public: + XStringTest(const char *S) : XString() + { + StrCpy(S); + } +}; + +extern XStringTest global_str1; +extern XStringTest global_str2; + extern XStringWTest global_str3; extern XStringWTest global_str4; diff --git a/rEFIt_UEFI/libeg/libscreen.cpp b/rEFIt_UEFI/libeg/libscreen.cpp index fa8a3e120..fbfef855c 100644 --- a/rEFIt_UEFI/libeg/libscreen.cpp +++ b/rEFIt_UEFI/libeg/libscreen.cpp @@ -641,7 +641,7 @@ EFI_STATUS egScreenShot(VOID) XStringWP CommonName(L"EFI\\CLOVER\\misc\\screenshot"); for (UINTN Index = 0; Index < 60; Index++) { // ScreenshotName = PoolPrint(L"%a%d.png", ScreenShotName, Index); - XStringW Name = CommonName + SPrintf("%lld", Index) + L".png"; + XStringW Name = CommonName + WPrintf("%lld", Index) + L".png"; if (!FileExists(SelfRootDir, Name.data())) { Status = egSaveFile(SelfRootDir, Name.data(), FileData, FileDataLength); if (!EFI_ERROR(Status)) {