ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

iconv.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include <iconv.h>
00024 
00025 #include "common/iconv.h"
00026 #include "common/str.h"
00027 #include "common/textconsole.h"
00028 #include "common/ustr.h"
00029 
00030 namespace Common {
00031 
00032 static const char **getIconvCodesForEncoding(Encoding encoding) {
00033     static const char *encodingCP932Codes[]  = { "cp932",  nullptr };
00034     static const char *encodingCP1250Codes[] = { "cp1250", nullptr };
00035     static const char *encodingCP1251Codes[] = { "cp1251", nullptr };
00036     static const char *encodingCP1252Codes[] = { "cp1252", nullptr };
00037     static const char *encodingCP1255Codes[] = { "cp1255", nullptr };
00038 
00039     // libiconv and glibc-iconv don't use the same name for that encoding, we have to try both
00040     static const char *encodingMacCentralEuropeCodes[] = { "maccentraleurope", "mac-centraleurope", nullptr };
00041 
00042     switch (encoding) {
00043         case kEncodingCP932:  return encodingCP932Codes;
00044         case kEncodingCP1250: return encodingCP1250Codes;
00045         case kEncodingCP1251: return encodingCP1251Codes;
00046         case kEncodingCP1252: return encodingCP1252Codes;
00047         case kEncodingCP1255: return encodingCP1255Codes;
00048         case kEncodingMacCentralEurope: return encodingMacCentralEuropeCodes;
00049     }
00050 
00051     error("Failed look up iconv codes for encoding '%d'", encoding);
00052 }
00053 
00054 U32String convertToU32String(Encoding fromEncoding, const String &string) {
00055     const char **fromCodes = getIconvCodesForEncoding(fromEncoding);
00056 
00057     // Apparently UTF-32 isn't native endian. Also, UCS−4−INTERNAL fails
00058     // for me.
00059     static const char *toCode =
00060 #ifdef SCUMM_BIG_ENDIAN
00061         "UTF-32BE";
00062 #else
00063         "UTF-32LE";
00064 #endif
00065 
00066     iconv_t handle;
00067     do {
00068         handle = iconv_open(toCode, *fromCodes);
00069 
00070         if (handle == (iconv_t)-1) {
00071             fromCodes++;
00072         }
00073     } while (handle == ((iconv_t)-1) && *fromCodes);
00074 
00075     if (handle == ((iconv_t)-1))
00076         error("Failed to initialize UTF-32 conversion from %d", fromEncoding);
00077 
00078     size_t inSize = string.size();
00079     size_t outSize = inSize * 4; // Approximation
00080     size_t originalOutSize = outSize;
00081 
00082     char *buffer = new char[outSize];
00083     char *dst = buffer;
00084 
00085 #ifdef ICONV_USES_CONST
00086     const char *src = string.c_str();
00087 #else
00088     char *src = const_cast<char *>(string.c_str());
00089 #endif
00090 
00091     if (iconv(handle, &src, &inSize, &dst, &outSize) == ((size_t)-1))
00092         error("Failed to convert %s to UTF-32 string", *fromCodes);
00093 
00094     // The conversion descriptor may still contain some state. Write it to the output buffer.
00095     if (iconv(handle, nullptr, nullptr, &dst, &outSize) == ((size_t)-1))
00096         error("Failed to convert the remaining state of %s to UTF-32 string", *fromCodes);
00097 
00098     U32String output((const U32String::value_type *)buffer, (originalOutSize - outSize) / 4);
00099 
00100     delete[] buffer;
00101     iconv_close(handle);
00102 
00103     return output;
00104 }
00105 
00106 } // End of namespace Common


Generated on Sat Feb 16 2019 05:00:54 for ResidualVM by doxygen 1.7.1
curved edge   curved edge