Advertisement
Guest User

Untitled

a guest
Jul 31st, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. /*Copyright (C) 2010  Armin Preiml
  2.  
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this program.  If not, see <http://www.gnu.org/licenses/>. */
  15.  
  16.  
  17.  
  18. #include <iostream>
  19. #include <iconv.h>
  20. #include <errno.h>
  21.  
  22. string convertToUTF8(string input)
  23. {
  24.     string output = "";
  25.    
  26.     //create convert description
  27.     //FIXME: I guess it's ISO-8859-1 encoded
  28.     iconv_t cd = iconv_open("UTF-8", "ISO-8859-1");
  29.    
  30.     if (cd == (iconv_t)-1)  //error handling
  31.     {
  32.     cerr << "Convert to UTF-8 failed: ";
  33.    
  34.     switch (errno)      //detailed error messages (maybe it contains too much detail :)
  35.     {
  36.         case EMFILE:
  37.         cerr << "{OPEN_MAX} files descriptors are currently open in the calling process.\n";
  38.         case ENFILE:
  39.         cerr << "Too many files are currently open in the system. \n";
  40.         case ENOMEM:
  41.         cerr << "Insufficient storage space is available. \n";
  42.         case EINVAL:
  43.         cerr << "The conversion specified by fromcode and tocode is not supported by the implementation. \n";
  44.        
  45.         default:
  46.         cerr << "WTF?\n";
  47.     }
  48.    
  49.     }
  50.     else
  51.     {
  52.     size_t inputSize = input.size();
  53.    
  54.     if (inputSize)  //input is not empty
  55.     {
  56.         //convert function doesn't accept const char *, therefore copy content into an char *
  57.         char * inputBufferStart = new char[inputSize];
  58.         char *inputBuffer = inputBufferStart;
  59.         strncpy(inputBuffer, input.c_str(), inputSize);
  60.        
  61.         size_t inputBytesLeft = inputSize;  //bytes to convert
  62.        
  63.         //FIXME: can't determine exact size_t
  64.         //In the german version of morrowind characters like äöü aren't counted by strlen
  65.         size_t outputSize = inputSize*2;    
  66.         size_t outputBytesLeft = outputSize;
  67.        
  68.         char *outputBufferStart = new char[outputSize];
  69.         char *outputBuffer = outputBufferStart;
  70.        
  71.         if (iconv(cd, &inputBuffer, &inputBytesLeft, &outputBuffer, &outputBytesLeft) == (size_t)-1)
  72.         {
  73.         switch (errno) {
  74.            
  75.             case EILSEQ:
  76.             cout << "Invalid multibyte sequence.\n";
  77.             break;
  78.             case EINVAL:
  79.             cout << "Incomplete multibyte sequence.\n";
  80.             break;
  81.             case E2BIG:
  82.             cout << "No more room.\n";
  83.             break;
  84.             default:
  85.             cout << "DUNNO\n";
  86.         }
  87.        
  88.         }
  89.         else
  90.         {
  91.         //outputBytesLeft was decreased for every byte which was written to the buffer
  92.         output = string(outputBufferStart, outputSize - outputBytesLeft);
  93.         }
  94.        
  95.         delete [] inputBufferStart;
  96.         delete [] outputBufferStart;
  97.     }
  98.     }
  99.    
  100.     iconv_close (cd);  
  101.    
  102.     return output;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement