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 <vector>
- #include <iconv.h>
- #include <errno.h>
- string convertToUTF8(string input)
- {
- string output = "";
- //create convert description
- 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
- {
- const size_t inputSize = input.size();
- if (inputSize) //input is not empty
- {
- //convert function doesn't accept const char *, therefore copy content into an char *
- vector<char> inputBuffer(input.begin(), input.end());
- char *inputBufferBegin = &inputBuffer[0];
- size_t inputBytesLeft = inputSize; //bytes to convert
- static const size_t outputSize = 1000;
- size_t outputBytesLeft;
- char outputBuffer[outputSize];
- char *outputBufferBegin;
- while (inputBytesLeft > 0 )
- {
- outputBytesLeft = outputSize;
- outputBufferBegin = outputBuffer;
- if (iconv(cd, &inputBufferBegin, &inputBytesLeft, &outputBufferBegin, &outputBytesLeft) == (size_t)-1)
- {
- switch (errno) {
- case E2BIG: //outputBuffer is full
- output += string(outputBuffer, outputSize);
- break;
- case EILSEQ:
- cerr << "Invalid multibyte sequence.\n";
- break;
- case EINVAL:
- cerr << "Incomplete multibyte sequence.\n";
- break;
- default:
- cerr << "DUNNO\n";
- }
- }
- }
- //read only relevant bytes from outputBuffer
- output += string(outputBuffer, outputSize - outputBytesLeft);
- }
- }
- iconv_close (cd);
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement