X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fiso2022jp.cpp;h=de56552ddac8b4515e9b184b37a48ff9a0643038;hp=6a6bd1dc7970b28c367fbfad07d5719c505bc0ad;hb=9da6abdcabec59f4845da256a8ad75a810ed1589;hpb=5ea1720bc90416df7ac5e28b145e9ebf7f76b7a2 diff --git a/source/iso2022jp.cpp b/source/iso2022jp.cpp index 6a6bd1d..de56552 100644 --- a/source/iso2022jp.cpp +++ b/source/iso2022jp.cpp @@ -1,3 +1,10 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + #include "ascii.h" #include "iso2022jp.h" #include "jisx0201.h" @@ -7,8 +14,11 @@ using namespace std; namespace Msp { -void Iso2022Jp::Encoder::encode_char(wchar_t c) +void Iso2022Jp::Encoder::encode_char(wchar_t c_) { + // Win32 has typedef unsigned short wchar_t + int c=c_; + if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E) { if(mode!=ASCII && mode!=JISX0201) @@ -34,12 +44,15 @@ void Iso2022Jp::Encoder::encode_char(wchar_t c) { unsigned short jis=ucs_to_jisx0208(c); if(!jis) - throw CodecError("Can't express character in ISO-2022-JP"); - if(mode!=JISX0208) - switch_mode(JISX0208); + error("Can't express character in ISO-2022-JP"); + else + { + if(mode!=JISX0208) + switch_mode(JISX0208); - char buf[2]={jis>>8, jis}; - append(buf, 2); + char buf[2]={jis>>8, jis}; + append(buf, 2); + } } } @@ -60,7 +73,15 @@ void Iso2022Jp::Encoder::switch_mode(Mode m) } } -Iso2022Jp::Decoder::Decoder(): +void Iso2022Jp::Encoder::append_replacement() +{ + if(mode!=ASCII) + switch_mode(ASCII); + append(032); +} + +Iso2022Jp::Decoder::Decoder(ErrorMode em): + StringCodec::Decoder(em), mode(ASCII), dec(new Ascii::Decoder), escape(0) @@ -72,7 +93,7 @@ void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator & { if(escape) { - escape=escape<<8 | (unsigned char)*i; + escape=escape<<8 | static_cast(*i); if(*i>='@' && *i<='Z') { switch(escape) @@ -81,7 +102,7 @@ void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator & case 0x1B284A: switch_mode(JISX0201); break; // ESC ( J case 0x1B2440: // ESC $ @ case 0x1B2442: switch_mode(JISX0208); break; // ESC $ B - default: throw CodecError("Invalid ISO-2022-JP escape sequence"); + default: error("Invalid ISO-2022-JP escape sequence"); } escape=0; } @@ -103,11 +124,21 @@ void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator & void Iso2022Jp::Decoder::sync() { if(escape) - throw CodecError("Sync in middle of ISO-2022-JP escape sequence"); + { + error("Sync in middle of ISO-2022-JP escape sequence"); + escape=0; + } + if(mode!=ASCII) - throw CodecError("Sync while not in ASCII mode"); - append(dec->get()); - dec->flush(); + { + error("Sync while not in ASCII mode"); + switch_mode(ASCII); + } + else + { + append(dec->get()); + dec->flush(); + } } void Iso2022Jp::Decoder::switch_mode(Mode m)