sjis2euc.cc
Go to the documentation of this file.
00001 /* sjis2euc.cc
00002  */
00003 
00004 #include "osl/misc/sjis2euc.h"
00005 #include <cctype>
00006 #include <cassert>
00007 #include <iostream>
00008 
00009 std::string osl::misc::sjis2euc(const std::string& str)
00010 {
00011   if (str.empty()) 
00012     return str;
00013 
00014   std::string result;
00015   result.reserve(str.size());
00016   size_t index = 0;
00017   while (index < str.size())
00018   {
00019     unsigned char c1 = str[index++];
00020     if (0xa1 <= c1 && c1 <= 0xdf) // ignore hankaku-kana
00021       continue;
00022     if (isascii(c1)) 
00023     {
00024       result.push_back(c1);
00025       continue;
00026     }
00027     
00028     assert(index < str.size());
00029     if (index >= str.size())
00030       break;
00031     unsigned char c2 = str[index++];
00032     sjis2euc(c1, c2);
00033     result.push_back(c1);
00034     result.push_back(c2);
00035   }
00036   return result;
00037 }
00038 
00043 void osl::misc::sjis2euc(unsigned char& c1, unsigned char& c2)
00044 {
00045   if( c2 < 0x9f )
00046   {
00047     if( c1 < 0xa0 )
00048     {
00049       c1 -= 0x81;
00050       c1 *= 2;
00051       c1 += 0xa1;
00052     }
00053     else
00054     {
00055       c1 -= 0xe0;
00056       c1 *= 2;
00057       c1 += 0xdf;
00058     }
00059     if( c2 > 0x7f )
00060       -- c2;
00061     c2 += 0x61;
00062   }
00063   else
00064   {
00065     if( c1 < 0xa0 )
00066     {
00067       c1 -= 0x81;
00068       c1 *= 2;
00069       c1 += 0xa2;
00070     }
00071     else
00072     {
00073       c1 -= 0xe0;
00074       c1 *= 2;
00075       c1 += 0xe0;
00076     }
00077     c2 += 2;
00078   }
00079 }
00080 // ;;; Local Variables:
00081 // ;;; mode:c++
00082 // ;;; c-basic-offset:2
00083 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines