string IceUtil::nativeToUTF8(const string& str, const IceUtil::StringConverterPtr& converter) { if(!converter || str.empty()) { return str; } UTF8BufferI buffer; Byte* last = converter->toUTF8(str.data(), str.data() + str.size(), buffer); return string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); }
wstring IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) { wstring target; if(!v.empty()) { // // If there is a narrow string converter use it to convert to UTF8, otherwise the narrow // string is already UTF8 encoded. // string tmp; if(converter) { UTF8BufferI buffer; Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer); tmp = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } else { tmp = v; } // // If there is a wide string converter use fromUTF8 to convert to the wide string native encoding. // if(wConverter) { wConverter->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()), reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target); } else { const Byte* sourceStart = reinterpret_cast<const Byte*>(tmp.data()); ConversionResult cr = convertUTF8ToUTFWstring(sourceStart, sourceStart + tmp.size(), target, flags); if(cr != conversionOK) { assert(cr == sourceExhausted || cr == sourceIllegal); throw IllegalConversionException(__FILE__, __LINE__, cr == sourceExhausted ? "partial character" : "bad encoding"); } } } return target; }
string IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) { string target; if(!v.empty()) { // // First convert to UTF8 narrow string. // if(wConverter) { UTF8BufferI buffer; Byte* last = wConverter->toUTF8(v.data(), v.data() + v.size(), buffer); target = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } else { size_t size = v.size() * 4 * sizeof(char); Byte* outBuf = new Byte[size]; Byte* targetStart = outBuf; Byte* targetEnd = outBuf + size; const wchar_t* sourceStart = v.data(); ConversionResult cr = convertUTFWstringToUTF8( sourceStart, sourceStart + v.size(), targetStart, targetEnd, flags); if(cr != conversionOK) { delete[] outBuf; assert(cr == sourceExhausted || cr == sourceIllegal); throw IllegalConversionException(__FILE__, __LINE__, cr == sourceExhausted ? "partial character" : "bad encoding"); } target = string(reinterpret_cast<char*>(outBuf), static_cast<size_t>(targetStart - outBuf)); delete[] outBuf; } // // If narrow string converter is present convert to the native narrow string encoding, otherwise // native narrow string encoding is UTF8 and we are done. // if(converter) { string tmp; converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()), reinterpret_cast<const Byte*>(target.data() + target.size()), tmp); tmp.swap(target); } } return target; }
string IceInternal::IndirectReference::toString() const { // // WARNING: Certain features, such as proxy validation in Glacier2, // depend on the format of proxy strings. Changes to toString() and // methods called to generate parts of the reference string could break // these features. Please review for all features that depend on the // format of proxyToString() before changing this and related code. // string result = RoutableReference::toString(); if(_adapterId.empty()) { return result; } result.append(" @ "); // // If the encoded adapter id string contains characters which the // reference parser uses as separators, then we enclose the // adapter id string in quotes. // string a = _adapterId; if(getInstance()->initializationData().stringConverter) { UTF8BufferI buffer; Byte* last = getInstance()->initializationData().stringConverter->toUTF8(a.data(), a.data() + a.size(), buffer); a = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } a = IceUtil::escapeString(a, ""); if(a.find_first_of(" ") != string::npos) { result.append("\""); result.append(a); result.append("\""); } else { result.append(_adapterId); } return result; }
string IceInternal::Reference::toString() const { // // WARNING: Certain features, such as proxy validation in Glacier2, // depend on the format of proxy strings. Changes to toString() and // methods called to generate parts of the reference string could break // these features. Please review for all features that depend on the // format of proxyToString() before changing this and related code. // ostringstream s; // // If the encoded identity string contains characters which // the reference parser uses as separators, then we enclose // the identity string in quotes. // string id = _instance->identityToString(_identity); if(id.find_first_of(" :@") != string::npos) { s << '"' << id << '"'; } else { s << id; } if(!_facet.empty()) { s << " -f "; // // If the encoded facet string contains characters which // the reference parser uses as separators, then we enclose // the facet string in quotes. // string fs = _facet; if(_instance->initializationData().stringConverter) { UTF8BufferI buffer; Byte* last = _instance->initializationData().stringConverter->toUTF8(fs.data(), fs.data() + fs.size(), buffer); fs = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); } fs = IceUtil::escapeString(fs, ""); if(fs.find_first_of(" :@") != string::npos) { s << '"' << fs << '"'; } else { s << fs; } } switch(_mode) { case ModeTwoway: { s << " -t"; break; } case ModeOneway: { s << " -o"; break; } case ModeBatchOneway: { s << " -O"; break; } case ModeDatagram: { s << " -d"; break; } case ModeBatchDatagram: { s << " -D"; break; } } if(getSecure()) { s << " -s"; } return s.str(); // Derived class writes the remainder of the string. }