Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Copyright (C) 2010 Armin Preiml
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>. */
- #include <iostream>
- #include <iconv.h>
- #include <errno.h>
- string convertToUTF8(string input)
- {
- string output = "";
- //create convert description
- //FIXME: I guess it's ISO-8859-1 encoded
- iconv_t cd = iconv_open("UTF-8", "ISO-8859-1");
- if (cd == (iconv_t)-1) //error handling
- {
- cerr << "Convert to UTF-8 failed: ";
- switch (errno) //detailed error messages (maybe it contains too much detail :)
- {
- case EMFILE:
- cerr << "{OPEN_MAX} files descriptors are currently open in the calling process.\n";
- case ENFILE:
- cerr << "Too many files are currently open in the system. \n";
- case ENOMEM:
- cerr << "Insufficient storage space is available. \n";
- case EINVAL:
- cerr << "The conversion specified by fromcode and tocode is not supported by the implementation. \n";
- default:
- cerr << "WTF?\n";
- }
- }
- else
- {
- size_t inputSize = input.size();
- if (inputSize) //input is not empty
- {
- //convert function doesn't accept const char *, therefore copy content into an char *
- char * inputBufferStart = new char[inputSize];
- char *inputBuffer = inputBufferStart;
- strncpy(inputBuffer, input.c_str(), inputSize);
- size_t inputBytesLeft = inputSize; //bytes to convert
- //FIXME: can't determine exact size_t
- //In the german version of morrowind characters like äöü aren't counted by strlen
- size_t outputSize = inputSize*2;
- size_t outputBytesLeft = outputSize;
- char *outputBufferStart = new char[outputSize];
- char *outputBuffer = outputBufferStart;
- if (iconv(cd, &inputBuffer, &inputBytesLeft, &outputBuffer, &outputBytesLeft) == (size_t)-1)
- {
- switch (errno) {
- case EILSEQ:
- cout << "Invalid multibyte sequence.\n";
- break;
- case EINVAL:
- cout << "Incomplete multibyte sequence.\n";
- break;
- case E2BIG:
- cout << "No more room.\n";
- break;
- default:
- cout << "DUNNO\n";
- }
- }
- else
- {
- //outputBytesLeft was decreased for every byte which was written to the buffer
- output = string(outputBufferStart, outputSize - outputBytesLeft);
- }
- delete [] inputBufferStart;
- delete [] outputBufferStart;
- }
- }
- iconv_close (cd);
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement