std::wstring UTF8toWS(const AnsiString& s){ std::wstring Value; if (s.size()==0) { return Value; } Value.resize(s.size()); //UTF16* buf = new UTF16[s.size()+1]; const UTF8* Start = (const UTF8*)s.c_str(); const UTF8* End = Start + s.size(); #ifdef WIN32 UTF16* DestStart = (UTF16*)(&Value[0]); UTF16* DestEnd = DestStart + s.size(); ConversionResult ret = ConvertUTF8toUTF16(&Start,End, &DestStart, DestEnd, strictConversion); #else UTF32* DestStart = (UTF32*)(&Value[0]); UTF32* DestEnd = DestStart + s.size(); ConversionResult ret = ConvertUTF8toUTF32(&Start,End, &DestStart, DestEnd, strictConversion); #endif if (ret != conversionOK) { if(ret == sourceIllegal) // { int n = mbstowcs(NULL,s.c_str(),0); Value.resize(n); setlocale(LC_ALL,""); mbstowcs((wchar_t*)Value.c_str(),s.c_str(),n); return Value; }else{ //throw std::exception("UFT8 Convert Fail."); 这种工具函数扔出例外似乎只会对宿主程序构成骚扰 //改为给出提示+可能的乱码 Value = _T("UFT8TO16 Convert Fail:"); int n = mbstowcs(NULL,s.c_str(),0); tstring s1(n,0); setlocale(LC_ALL,""); mbstowcs((wchar_t*)s1.c_str(),s.c_str(),n); Value.insert(Value.end(),s1.begin(),s1.end()); return Value; } } /* else{ Value = (wchar_t*)buf; delete buf; } */ tstring::size_type n = Value.find_first_of(_T('\0')); Value = Value.substr(0,n); return Value; }
//same above int32 Energy::FindFloat(AnsiString& s, uint32 pos, uint32& DecimalPos,char ch /*='@'*/) { int32 len = 0; if(s[pos] == '-'){ ++pos; ++len;}; //Note: the '+' is not allowed in front of positive numbers bool floatpoint = false; uint32 end = min(pos+30,s.size()); //up to 20 digits, avoid unnecessary search for(uint32 i=pos; i<end; ++i) { if(isdigit(s[i])){ ++len; } else if(s[i] == ch){ if(i==pos)return 0; if(!floatpoint)DecimalPos = len; return len; } else if(s[i]=='.'){ //1.234 if(!floatpoint){ DecimalPos = i-pos; floatpoint = true; ++len; } else return 0; } else return 0; } if (floatpoint && len>1) { return len; } return 0; }
WideString StringUtil::ansiToWide(const AnsiString &str) { // TODO: This function gets called a lot, so warnings like these drown out the usefull information Common::String converted = ""; uint32 index = 0; while (index != str.size()) { byte c = str[index]; if (c == 146) { converted += (char)39; // Replace right-quote with apostrophe } else if (c == 133) { converted += Common::String("..."); // Replace ...-symbol with ... } else { converted += c; } index++; } // using default os locale! /* setlocale(LC_CTYPE, ""); size_t wideSize = mbstowcs(NULL, str.c_str(), 0) + 1; wchar_t *wstr = new wchar_t[WideSize]; mbstowcs(wstr, str.c_str(), WideSize); WideString ResultString(wstr); delete[] wstr; return ResultString;*/ return WideString(converted); }
// store the body part to un-encoded string buffer void MimeBody::Store(AnsiString &output, bool bIncludeHeader) const { // store header fields int nSize = 0; if (bIncludeHeader) MimeHeader::Store(output); // Copy the data to the output buffer. output.append(m_pbText); if (m_listBodies.empty()) return; // store child body parts string strBoundary = GetBoundary(); if (strBoundary.empty()) return; // boundary not be set int nBoundSize = (int)strBoundary.size() + 6; // Bill48105 - These iOutputSize are temp fix for [ ambiguous error int iOutputSizeLess2 = output.size() - 2; int iOutputSizeLess1 = output.size() - 1; for (BodyList::const_iterator it=m_listBodies.begin(); it!=m_listBodies.end(); it++) { // If the initial body ends with \r\n, remove them. We add new ones below. if (m_listBodies.begin() == it && output.size() >= 2 && output[iOutputSizeLess2] == '\r' && output[iOutputSizeLess1] == '\n') { output = output.Mid(0, output.GetLength() - 2); } AnsiString boundaryLine = Formatter::Format(_T("\r\n--{0}\r\n"), String(strBoundary)); output.append(boundaryLine); shared_ptr<MimeBody> pBP = *it; ASSERT(pBP != NULL); pBP->Store(output); } AnsiString endBoundaryLine = Formatter::Format(_T("\r\n--{0}--\r\n"), String(strBoundary)); output.append(endBoundaryLine); }
AnsiString PathUtil::getFileNameWithoutExtension(const AnsiString &path) { AnsiString fileName = getFileName(path); // TODO: Prettify this. AnsiString extension = Common::lastPathComponent(fileName, '.'); for (uint32 i = 0; i < extension.size() + 1; i++) { fileName.deleteLastChar(); } return fileName; }
//TYPE_PIPELINE@ID@len@Name@LEN@ type@len@data1 void Energy::PrintString(AnsiString& s,int32 type,int64 ID,tstring Name, uint32 datalen, const char* data) { char buf1[30], buf2[20],buf3[20]; int64toa(ID,buf1); eSTRING temp(Name); AnsiString AnsiName; temp.ToString(AnsiName); //convert into utf8 #ifdef _WIN32 ::sprintf_s(buf2,"%d",AnsiName.size()); ::sprintf_s(buf3,"%u",datalen); #else ::sprintf(buf2,"%d",AnsiName.size()); ::sprintf(buf3,"%u",datalen); #endif s += TYPE_TO_CHAR(type); s += '@'; //ID s += buf1; s += '@'; //len s += buf2; s += '@'; //name //s +=AnsiName; s.append(AnsiName.c_str(),AnsiName.size()); s += '@'; //data len s +=buf3; s += '@'; //data //s+=data; 由于data可能是UTF16TO8而来,不是正常字符串,如果直接用字符串操作可能导致数据不正常 s.append(data,datalen); /* int32 n = s.size(); s.resize(n+datalen); memcpy(&s[n],data,datalen); */ };
AnsiString PathUtil::unifySeparators(const AnsiString &path) { AnsiString newPath = path; for (uint32 i = 0; i < newPath.size(); i++) { if (newPath[i] == '\\') { newPath.setChar('/', i); } } return newPath; }
//starting from pos, finds each char,checking whether there is an integer with the char '@' end, and finally returns the length of integer, 0 is an error int32 Energy::FindInt(AnsiString& s,uint32 pos, char ch /*='@'*/) { int32 len =0; if(pos<s.size() && s[pos] == '-'){ //Note: the '+' is not allowed in front of positive numbers ++pos; ++len; }; uint32 end = pos+20; //up to 20 digits, avoid unnecessary search if(end<s.size())end = s.size(); for(uint32 i=pos; i<end; ++i) { if(s[i] == ch){ if(i==pos)return 0; else return len; } if(!isdigit(s[i]))return 0; ++len; } return 0; }
int BaseFontBitmap::getTextWidth(const byte *text, int maxLength) { AnsiString str; if (_gameRef->_textEncoding == TEXT_UTF8) { WideString wstr = StringUtil::utf8ToWide(Utf8String((const char *)text)); str = StringUtil::wideToAnsi(wstr); } else { str = AnsiString((const char *)text); } if (maxLength >= 0 && str.size() > (uint32)maxLength) { str = Common::String(str.c_str(), (uint32)maxLength); } //str.substr(0, maxLength); // TODO: Remove int textWidth = 0; for (int i = 0; (uint32)i < str.size(); i++) { textWidth += getCharWidth((byte)str[i]); } return textWidth; }
WideString StringUtil::ansiToWide(const AnsiString &str) { // TODO: This function gets called a lot, so warnings like these drown out the usefull information Common::String converted = ""; uint32 index = 0; while (index != str.size()) { converted += simpleAnsiToWide(str, index); } // using default os locale! /* setlocale(LC_CTYPE, ""); size_t wideSize = mbstowcs(NULL, str.c_str(), 0) + 1; wchar_t *wstr = new wchar_t[WideSize]; mbstowcs(wstr, str.c_str(), WideSize); WideString ResultString(wstr); delete[] wstr; return ResultString;*/ return WideString(converted); }
void ansi_to_unicode(UnicodeString& u_str, const AnsiString& a_str) { unsigned size = a_str.size() + 1; int res = MultiByteToWideChar(CP_ACP, 0, a_str.data(), size, u_str.buf(a_str.size()), size); if (res == 0) FAIL(SystemError()); u_str.set_size(res - 1); }
int BaseFontBitmap::textHeightDraw(const byte *text, int x, int y, int width, TTextAlign align, bool draw, int maxHeight, int maxLength) { if (maxLength == 0) { return 0; } if (text == nullptr || text[0] == '\0') { return _tileHeight; } AnsiString str; if (_gameRef->_textEncoding == TEXT_UTF8) { WideString wstr = StringUtil::utf8ToWide(Utf8String((const char *)text)); str = StringUtil::wideToAnsi(wstr); } else { str = AnsiString((const char *)text); } if (str.empty()) { return 0; } int lineLength = 0; int realLength = 0; int numLines = 0; int i; int index = -1; int start = 0; int end = 0; int last_end = 0; bool done = false; bool newLine = false; bool longLine = false; if (draw) { _gameRef->_renderer->startSpriteBatch(); } while (!done) { if (maxHeight > 0 && (numLines + 1)*_tileHeight > maxHeight) { if (draw) { _gameRef->_renderer->endSpriteBatch(); } return numLines * _tileHeight; } index++; if (str[index] == ' ' && (maxHeight < 0 || maxHeight / _tileHeight > 1)) { end = index - 1; realLength = lineLength; } if (str[index] == '\n') { end = index - 1; realLength = lineLength; newLine = true; } if (lineLength + getCharWidth(str[index]) > width && last_end == end) { end = index - 1; realLength = lineLength; newLine = true; longLine = true; } if ((int)str.size() == (index + 1) || (maxLength >= 0 && index == maxLength - 1)) { done = true; if (!newLine) { end = index; lineLength += getCharWidth(str[index]); realLength = lineLength; } } else { lineLength += getCharWidth(str[index]); } if ((lineLength > width) || done || newLine) { if (end < 0) { done = true; } int startX; switch (align) { case TAL_CENTER: startX = x + (width - realLength) / 2; break; case TAL_RIGHT: startX = x + width - realLength; break; case TAL_LEFT: startX = x; break; default: error("BaseFontBitmap::TextHeightDraw - Unhandled enum"); break; } for (i = start; i < end + 1; i++) { if (draw) { drawChar(str[i], startX, y); } startX += getCharWidth(str[i]); } y += _tileHeight; last_end = end; if (longLine) { end--; } start = end + 2; index = end + 1; lineLength = 0; newLine = false; longLine = false; numLines++; } } if (draw) { _gameRef->_renderer->endSpriteBatch(); } return numLines * _tileHeight; }
const AnsiString & __fastcall AnsiString::operator +=(const AnsiString & rhs) { Data.append(rhs.c_str(), rhs.size()); return *this; }
AnsiString PathUtil::getDirectoryName(const AnsiString &path) { AnsiString newPath = unifySeparators(path); Common::String filename = getFileName(path); return Common::String(path.c_str(), path.size() - filename.size()); }