void GuiLanguage::LoadLanguage(std::ostream & error) { init_locale("vdrift", m_lang_id.c_str()); const std::string to = "CP" + GetCodePageId(m_lang_id); if (m_iconv != iconv_t(-1)) iconv_close(m_iconv); m_iconv = iconv_open(to.c_str(), "UTF-8"); if (m_iconv == iconv_t(-1)) { error << "Failed code conversion: UTF-8 to " << to << std::endl; return; } }
unsigned short MultiByteToWideChar(unsigned long code_page, unsigned long, const char * source, size_t src_count, unsigned short * dest, size_t dst_count) { if (!dest) // An error condition is indicated by 0 return 0; if (src_count == size_t(-1)) // When passed -1 it should find the src_count = strlen(source); // length of the source it's self if (dst_count == 0) // We cannot find the completed size so esitmate it. return src_count*3; std::ostringstream oss; oss << "CP" << code_page; iconv_t cdesc = iconv_open("UCS-2", oss.str().c_str()); // tocode, fromcode if (cdesc == iconv_t(-1)) { std::cerr << program_invocation_short_name << ": iconv_open(\"UCS-2\", \"" << oss.str() << "\"): " << strerror(errno) << std::endl; exit(1); } char *dst_ptr = reinterpret_cast<char *>(dest); ICONV_CONST char *src_ptr = const_cast<char *>(source); dst_count *= sizeof(unsigned short); const size_t dst_size = dst_count; iconv(cdesc, &src_ptr, &src_count, &dst_ptr, &dst_count); iconv_close(cdesc); return dst_size - dst_count; }
static _Iconv<srcT,dstT> *create(const char *to,const char *from) { /* factory function */ if (sizeof(dstT) == sizeof(char) && sizeof(srcT) == sizeof(char)) { iconv_t ctx = iconv_open(to,from); if (ctx != iconv_t(-1)) return new(std::nothrow) _Iconv<srcT,dstT>(ctx); } return NULL; }
Bytes *Bytes::transcode(const char* source, const char* target) { if (this->len == 0) { return new Bytes(); } if ((source == target) || strcasecmp(source, target) == 0) { return new Bytes(this); } iconv_t converter; converter = iconv_open(source, target); if (converter == iconv_t(-1)) { throw "Unsupported charset"; } size_t size = this->len; uint8_t *newBytes = (uint8_t*)malloc(size); size_t inBytesLeft = this->len; size_t outBytesLeft = size; char *inBuf = (char *)this->bytes; char *outBuf = (char *)newBytes; size_t result = 0; do { result = iconv(converter, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft); if (result == (size_t)(-1)) { switch (errno) { case EILSEQ: CLEANUP_CONVERSION(newBytes, converter, "Transcoding stopped due to an input byte that does not belong to the input codeset.") case EINVAL: CLEANUP_CONVERSION(newBytes, converter, "Transcoding stopped due to an incomplete character or shift sequence at the end of the input buffer.") case E2BIG: { size_t newSize = size + (this->len)/4; uint8_t *newerBytes = (uint8_t*)realloc(newBytes, newSize); if (newerBytes == NULL) { CLEANUP_CONVERSION(newBytes, converter, "Could not allocate enough memory to perform transcoding") } outBuf = (char*)newerBytes + (outBuf - (char*)newBytes); newBytes = newerBytes; outBytesLeft += newSize - size; size = newSize; } break; default: CLEANUP_CONVERSION(newBytes, converter, "Conversion failed") } } } while (result == -1); iconv_close(converter); size_t len = outBuf - (char *)newBytes; Bytes * bs = new Bytes(len, newBytes); free(newBytes); return bs; }
static _Iconv<srcT,dstT> *create(const char *nw) { /* factory function, wide to char, or char to wide */ if (sizeof(dstT) == sizeof(char) && sizeof(srcT) > sizeof(char)) { const char *wchar_encoding = _get_wchar_encoding<srcT>(); if (wchar_encoding == NULL) return NULL; iconv_t ctx = iconv_open(/*TO*/nw,/*FROM*/wchar_encoding); /* from wchar to codepage nw */ if (ctx != iconv_t(-1)) return new(std::nothrow) _Iconv<srcT,dstT>(ctx); } else if (sizeof(dstT) > sizeof(char) && sizeof(srcT) == sizeof(char)) { const char *wchar_encoding = _get_wchar_encoding<dstT>(); if (wchar_encoding == NULL) return NULL; iconv_t ctx = iconv_open(/*TO*/wchar_encoding,/*FROM*/nw); /* from codepage new to wchar */ if (ctx != iconv_t(-1)) return new(std::nothrow) _Iconv<srcT,dstT>(ctx); } return NULL; }
void CharsetConv::workwith(const char *from, const char *to) { close(); if ((cd_ = iconv_open(to, from)) == iconv_t(-1)) g_critical("Can not convert from %s to %s: %s\n", from, to, strerror(errno)); #ifdef DEBUG StdErr.printf("CharsetConv::workwith: %s to %s\n", from ,to); #endif }
static iconv_t sIconvOpenEncoder(const string& iDestName) { iconv_t result; if (ZCONFIG(Endian, Big)) result = ::iconv_open(iDestName.c_str(), "UTF-32BE"); else result = ::iconv_open(iDestName.c_str(), "UTF-32LE"); if (result == iconv_t(-1)) return 0; return result; }
static iconv_t sIconvOpenDecoder(const string& iSourceName) { iconv_t result; if (ZCONFIG(Endian, Big)) result = ::iconv_open("UTF-32BE", iSourceName.c_str()); else result = ::iconv_open("UTF-32LE", iSourceName.c_str()); if (result == iconv_t(-1)) return 0; return result; }
std::string escape_json(std::string const& input) { if (input.empty()) return ""; std::vector<boost::uint32_t> wide; wide.resize(input.size()); static iconv_t iconv_handle = iconv_open("UTF-32", "UTF-8"); if (iconv_handle == iconv_t(-1)) return "(iconv error)"; size_t insize = input.size(); size_t outsize = insize * sizeof(boost::uint32_t); char const* in = input.c_str(); char* out = (char*)&wide[0]; size_t retval = iconv(iconv_handle, (char**)&in, &insize , &out, &outsize); if (retval == (size_t)-1) return "(iconv error)"; if (insize != 0) return "(iconv error)"; if (outsize > input.size() * 4) return "(iconv error)"; TORRENT_ASSERT(wide.size() >= outsize); wide.resize(wide.size() - outsize / sizeof(boost::uint32_t)); std::string ret; for (std::vector<boost::uint32_t>::const_iterator s = wide.begin(); s != wide.end(); ++s) { if (*s > 0x1f && *s < 0x80 && *s != '"' && *s != '\\') { ret += *s; } else { ret += '\\'; switch(*s) { case '"': ret += '"'; break; case '\\': ret += '\\'; break; case '\n': ret += 'n'; break; case '\r': ret += 'r'; break; case '\t': ret += 't'; break; case '\b': ret += 'b'; break; case '\f': ret += 'f'; break; default: { char buf[20]; snprintf(buf, sizeof(buf), "u%04x", boost::uint16_t(*s)); ret += buf; } } } } return ret; }
const std::string & GuiLanguage::operator()(const std::string & str) const { if (m_iconv == iconv_t(-1)) return str; static std::string temp; // ugh char * tstr = gettext(str.c_str()); char * cstr = convert(m_iconv, tstr); if (cstr) { temp.assign(cstr); free(cstr); return temp; } return str; }
// 编码转换 // bool Convert_String(char* in, size_t inlen, char* out, size_t outlen, const std::string &to_encoding, const std::string &from_encoding) { iconv_t h = iconv_open(to_encoding.c_str(), from_encoding.c_str()); if (iconv_t(-1) == h) return false; XH_GUARD([&]{ iconv_close(h); }); #if defined _PLATFORM_WINDOWS_ if (size_t(-1) == iconv(h, (const char**)&in, &inlen, &out, &outlen)) #elif defined _PLATFORM_LINUX_ if (size_t(-1) == iconv(h, &in, &inlen, &out, &outlen)) #endif { return false; } return true; }
bool CharsetConv::convert(const char *str, std::string::size_type len, std::string& res) const { res = ""; if (cd_ == iconv_t(-1)) { g_critical("Can not convert from one encoding to another:" " wrong iconv descriptor\n"); return false; } size_t err; res.resize(len); const char *p = str; size_t inbytesleft = len; size_t outbytesleft = res.length(); //TODO: res may be not contiguous char *outp = &res[0]; again: err = iconv(cd_, //this need because win32 version of iconv and from glibc is different (iconv_P2_type)&p, &inbytesleft, &outp, &outbytesleft); if (err == size_t(-1)) { if (errno == E2BIG) { size_t used = outp - &(res[0]); res.resize(res.length() * 2); outp = &res[0] + used; outbytesleft = res.length() - used; goto again; } else { g_critical("Can not convert from one encoding to another: %s\n", strerror(errno)); return false; } } res.resize(outp - &res[0]); return true; }
uint32_t utf82ucsle(const char* _pUTF8, uint32_t _nDataLen, char* _pUnicode, uint32_t _nBufferSize) { // utf8转unicode内码大小极端情况下内存占用是原来的2倍 assert(_nDataLen*2 <= _nBufferSize); uint32_t char_count = 0; iconv_t conv = iconv_open("UCS-2LE", "UTF-8"); uint32_t origSize = _nBufferSize; if(conv == iconv_t(-1)) { return 0; } int32_t error = 0; error = iconv(conv,const_cast<char**>(&_pUTF8),&_nDataLen,&_pUnicode,&_nBufferSize); iconv_close(conv); if(error == -1) { return 0; } char_count = (origSize - _nBufferSize) / sizeof(uint16_t); return char_count; }
/*-----------------------------------------------------------------------------*/ std::string StringUtil::ConvertStringFromWString(const std::wstring& wstr) { if (wstr.empty()) return std::string(); #ifdef _WIN32 int size = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL); std::string str(size, 0); WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), &str[0], size, NULL, NULL); #else iconv_t ic = iconv_open("SHIFT_JIS", "UCS-4LE"); if (ic == iconv_t(-1)) { // エラー return std::string(); } char* in_buf = (char*)wstr.data(); size_t in_size = wstr.size() * 4; size_t out_size = in_size; size_t size = out_size; std::string str(out_size, 0); char* out_buf = (char*)str.data(); iconv(ic, &in_buf, &in_size, &out_buf, &out_size); str.resize(size - out_size); iconv_close(ic); #endif return str; }
/*-----------------------------------------------------------------------------*/ std::wstring StringUtil::ConvertWStringFromString(const std::string& str) { if (str.empty()) return std::wstring(); #ifdef _WIN32 int size = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str.c_str(), (int)str.size(), NULL, 0); std::wstring wstr(size, 0); MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), &wstr[0], size); #else iconv_t ic = iconv_open("UCS-4LE", "SHIFT_JIS"); if (ic == iconv_t(-1)) { // エラー return std::wstring(); } char* in_buf = (char*)str.data(); size_t in_size = str.size(); size_t out_size = in_size * 4; size_t size = out_size; std::wstring wstr(out_size, 0); char* out_buf = (char*)wstr.data(); iconv(ic, &in_buf, &in_size, &out_buf, &out_size); wstr.resize((size - out_size) / 4); iconv_close(ic); #endif return wstr; }
std::string digidoc::util::String::convertUTF8(const std::string& str_in, bool to_UTF) { #if defined(__APPLE__) return str_in; #else std::string charset("ISO8859-1"); const char *env_lang = getenv("LANG"); if(env_lang && strcmp(env_lang, "C") != 0) { charset = env_lang; size_t locale_start = charset.rfind("."); if(locale_start != std::string::npos) charset = charset.substr(locale_start+1); } // no conversion needed for UTF-8 if((charset.compare("UTF-8") == 0) || (charset.compare("utf-8") == 0)) return str_in; iconv_t ic_descr = iconv_t(-1); try { ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8"); } catch(std::exception &) {} if( ic_descr == iconv_t(-1)) { INFO("Could not get the iconv descriptor for converting to/from charset %s, " "continuing without conversion.", charset.c_str()); return str_in; } ICONV_CONST char* inptr = (ICONV_CONST char*)str_in.c_str(); size_t inleft = str_in.size(); std::string out; char outbuf[64]; char* outptr; size_t outleft; while(inleft > 0) { outbuf[0] = '\0'; outptr = (char *)outbuf; outleft = sizeof(outbuf) - sizeof(outbuf[0]); size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft); if(result == size_t(-1)) { switch(errno) { case E2BIG: break; case EILSEQ: case EINVAL: default: iconv_close(ic_descr); return str_in; break; } } *outptr = '\0'; out += outbuf; } iconv_close(ic_descr); return out; #endif }
/** * Helper method for converting from non-UTF-8 encoded strings to UTF-8. * Supported LANG values for Linux: see /usr/share/i18n/SUPPORTED. * Supported encodings for libiconv: see iconv --list . * * Note! If non-ASCII characters are used we assume a proper LANG value!!! * * @param str_in The string to be converted. * @return Returns the input string in UTF-8. */ std::string digidoc::util::File::convertUTF8(const std::string &str_in, bool to_UTF) { std::string charset("C"); char *env_lang = getenv("LANG"); if(env_lang && charset.compare(env_lang) != 0) { charset = env_lang; size_t locale_start = charset.rfind("."); if(locale_start != std::string::npos) charset = charset.substr(locale_start+1); } // no conversion needed for UTF-8 if((charset.compare("UTF-8") == 0) || (charset.compare("utf-8") == 0)) return str_in; iconv_t ic_descr = iconv_t(-1); try { ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8"); } catch(std::exception &) {} if( ic_descr == iconv_t(-1)) return str_in; ICONV_CONST char* inptr = (ICONV_CONST char*)str_in.c_str(); size_t inleft = str_in.size(); std::string out; char outbuf[64]; char* outptr; size_t outleft; while(inleft > 0) { outbuf[0] = '\0'; outptr = (char *)outbuf; outleft = sizeof(outbuf) - sizeof(outbuf[0]); size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft); if(result == size_t(-1)) { switch(errno) { case E2BIG: break; case EILSEQ: case EINVAL: default: iconv_close(ic_descr); return str_in; break; } } *outptr = '\0'; out += outbuf; } iconv_close(ic_descr); return out; }
iconv_t iconv_open(__const char *__tocode, __const char *__fromcode) { return iconv_t(-1); }
GuiLanguage::~GuiLanguage() { if (m_iconv != iconv_t(-1)) iconv_close(m_iconv); }
GuiLanguage::GuiLanguage() : m_lang_id("en"), m_iconv(iconv_t(-1)) { // ctor }
OSStatus FSGetCatalogInfo(const FSRef* ref, uint32_t infoBits, FSCatalogInfo* infoOut, HFSUniStr255* nameOut, FSSpecPtr fsspec, FSRef* parentDir) { std::string path; if (!FSRefMakePath(ref, path)) return fnfErr; if (nameOut) { iconv_t ic = iconv_open("UTF-16", "UTF-8"); size_t s; const char* inbuf = path.c_str(); char* outbuf = reinterpret_cast<char*>(nameOut->unicode); size_t inbytesleft = path.size(), outbytesleft = sizeof(nameOut->unicode); if (ic == iconv_t(-1)) return -1; memset(nameOut->unicode, 0, outbytesleft); s = iconv(ic, (char**) &inbuf, &inbytesleft, &outbuf, &outbytesleft); iconv_close(ic); if (s == size_t(-1)) return -1; } if (parentDir) { memcpy(parentDir, ref, sizeof(FSRef)); ino_t* last = std::find(parentDir->inodes, parentDir->inodes+FSRef_MAX_DEPTH, 0); if (last != parentDir->inodes) *(last-1) = 0; } if (infoOut && infoBits != kFSCatInfoNone) { struct stat st; memset(infoOut, 0, sizeof(*infoOut)); if (::stat(path.c_str(), &st) != 0) return makeOSStatus(errno); if (infoBits & kFSCatInfoNodeFlags) { if (S_ISDIR(st.st_mode)) infoOut->nodeFlags = 4; } if (infoBits & (kFSCatInfoParentDirID|kFSCatInfoNodeID)) { if (infoBits & kFSCatInfoNodeID) infoOut->nodeID = ref->inodes[0]; for (int i = FSRef_MAX_DEPTH-1; i > 0; i--) { if (ref->inodes[i] == 0) continue; if (infoBits & kFSCatInfoParentDirID) infoOut->parentDirID = ref->inodes[i-1]; if (infoBits & kFSCatInfoNodeID) infoOut->nodeID = ref->inodes[i]; } } if (infoBits & kFSCatInfoDataSizes) { infoOut->dataLogicalSize = st.st_size; infoOut->dataPhysicalSize = st.st_blocks*512; } int uaccess; if (st.st_uid == getuid()) uaccess = st.st_mode & 0700; else if (hasgid(st.st_gid)) uaccess = st.st_mode & 070; else uaccess = st.st_mode & 07; if (infoBits & kFSCatInfoPermissions) { const uid_t uid = getuid(); infoOut->fsPermissionInfo.userID = st.st_uid; infoOut->fsPermissionInfo.groupID = st.st_gid; infoOut->fsPermissionInfo.mode = st.st_mode & 07777; infoOut->fsPermissionInfo.userAccess = uaccess; } if (infoBits & kFSCatInfoUserPrivs) { if (!(uaccess & 2)) infoOut->userPrivileges |= 0x4; // kioACUserNoMakeChangesMask if (getuid() != st.st_uid) infoOut->userPrivileges |= 0x80; // kioACUserNotOwnerMask } if (infoBits & kFSCatInfoCreateDate) infoOut->createDate = Darling::time_tToUTC(st.st_ctime); if (infoBits & kFSCatInfoContentMod) infoOut->attributeModDate = infoOut->contentModDate = Darling::time_tToUTC(st.st_mtime); if (infoBits & kFSCatInfoAccessDate) infoOut->accessDate = Darling::time_tToUTC(st.st_atime); } return noErr; }