From: Mikko Rasa Date: Sat, 21 Apr 2007 22:18:54 +0000 (+0000) Subject: Rename Latin1 as Iso88591 X-Git-Tag: strings-1.0~19 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6cbf9d2160a9f8e1ef98acb63ead3a14c88e2703;p=libs%2Fcore.git Rename Latin1 as Iso88591 Add Windows1252 --- diff --git a/source/codec.cpp b/source/codec.cpp index ae15382..96d18c7 100644 --- a/source/codec.cpp +++ b/source/codec.cpp @@ -9,10 +9,11 @@ Distributed under the LGPL #include "codec.h" #include "iso2022jp.h" #include "iso646fi.h" +#include "iso88591.h" #include "jisx0201.h" #include "jisx0208.h" -#include "latin1.h" #include "utf8.h" +#include "windows1252.h" using namespace std; @@ -82,10 +83,11 @@ StringCodec *create_codec(const string &n) if(name=="ascii") return new Ascii; if(name=="iso2022jp") return new Iso2022Jp; if(name=="iso646fi") return new Iso646Fi; + if(name=="iso88591" || name=="latin1") return new Iso88591; if(name=="jisx0201") return new JisX0201; if(name=="jisx0208") return new JisX0208; - if(name=="latin1") return new Latin1; if(name=="utf8") return new Utf8; + if(name=="windows1252") return new Windows1252; throw InvalidParameterValue("Unknown string codec"); } diff --git a/source/iso88591.cpp b/source/iso88591.cpp new file mode 100644 index 0000000..73169c8 --- /dev/null +++ b/source/iso88591.cpp @@ -0,0 +1,31 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#include "iso88591.h" + +using namespace std; + +namespace Msp { + +void Iso88591::Encoder::encode_char(wchar_t c_) +{ + // Win32 has typedef unsigned short wchar_t + int c=c_; + if(c<0 || c>0xFF) + error("Can't express character in Latin-1"); + else + append(c); +} + +void Iso88591::Decoder::decode_char(const string &str, string::const_iterator &i) +{ + if(i==str.end()) + return; + append(static_cast(*i++)); +} + +} // namespace Msp diff --git a/source/iso88591.h b/source/iso88591.h new file mode 100644 index 0000000..ff82df4 --- /dev/null +++ b/source/iso88591.h @@ -0,0 +1,40 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#ifndef MSP_STRINGS_ISO88591_H_ +#define MSP_STRINGS_ISO88591_H_ + +#include "codec.h" + +namespace Msp { + +class Iso88591: public StringCodec +{ +public: + class Encoder: public StringCodec::Encoder + { + public: + Encoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Encoder(em) { } + void encode_char(wchar_t); + private: + void append_replacement() { append(032); } + }; + + class Decoder: public StringCodec::Decoder + { + public: + Decoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Decoder(em) { } + void decode_char(const std::string &, std::string::const_iterator &); + }; + + Encoder *create_encoder(ErrorMode em=THROW_ON_ERROR) const { return new Encoder(em); } + Decoder *create_decoder(ErrorMode em=THROW_ON_ERROR) const { return new Decoder(em); } +}; + +} // namespace Msp + +#endif diff --git a/source/latin1.cpp b/source/latin1.cpp deleted file mode 100644 index fcff15f..0000000 --- a/source/latin1.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* $Id$ - -This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa -Distributed under the LGPL -*/ - -#include "latin1.h" - -using namespace std; - -namespace Msp { - -void Latin1::Encoder::encode_char(wchar_t c_) -{ - // Win32 has typedef unsigned short wchar_t - int c=c_; - if(c<0 || c>0xFF) - error("Can't express character in Latin-1"); - else - append(c); -} - -void Latin1::Decoder::decode_char(const string &str, string::const_iterator &i) -{ - if(i==str.end()) - return; - append(static_cast(*i++)); -} - -} // namespace Msp diff --git a/source/latin1.h b/source/latin1.h deleted file mode 100644 index 1dc52e3..0000000 --- a/source/latin1.h +++ /dev/null @@ -1,40 +0,0 @@ -/* $Id$ - -This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa -Distributed under the LGPL -*/ - -#ifndef MSP_STRINGS_LATIN1_H_ -#define MSP_STRINGS_LATIN1_H_ - -#include "codec.h" - -namespace Msp { - -class Latin1: public StringCodec -{ -public: - class Encoder: public StringCodec::Encoder - { - public: - Encoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Encoder(em) { } - void encode_char(wchar_t); - private: - void append_replacement() { append(032); } - }; - - class Decoder: public StringCodec::Decoder - { - public: - Decoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Decoder(em) { } - void decode_char(const std::string &, std::string::const_iterator &); - }; - - Encoder *create_encoder(ErrorMode em=THROW_ON_ERROR) const { return new Encoder(em); } - Decoder *create_decoder(ErrorMode em=THROW_ON_ERROR) const { return new Decoder(em); } -}; - -} // namespace Msp - -#endif diff --git a/source/windows1252.cpp b/source/windows1252.cpp new file mode 100644 index 0000000..db9d417 --- /dev/null +++ b/source/windows1252.cpp @@ -0,0 +1,65 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#include "windows1252.h" + +using namespace std; + +namespace { + +unsigned short table[32]= +{ + 0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0, 0x017D, 0, + 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0, 0x017E, 0x0178 +}; + +} + +namespace Msp { + +void Windows1252::Encoder::encode_char(wchar_t c_) +{ + int c=c_; + if((c>=0 && c<=0x7F) || (c>=0xA0 && c<=0xFF)) + append(c); + else + { + unsigned i; + for(i=0; i<32; ++i) + if(table[i]==c) + { + append(c); + break; + } + + if(i==32) + error("Can't express character in Windows-1252"); + } +} + +void Windows1252::Decoder::decode_char(const string &str, string::const_iterator &i) +{ + if(i==str.end()) + return; + else + { + int c=static_cast(*i++); + if(c>=0x80 && c<=0x9F) + { + c-=0x80; + if(table[c]==0) + error("Invalid Windows-1252 string (undefined character)"); + append(table[c]); + } + else + append(c); + } +} + +} // namespace Msp diff --git a/source/windows1252.h b/source/windows1252.h new file mode 100644 index 0000000..63cb42f --- /dev/null +++ b/source/windows1252.h @@ -0,0 +1,40 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#ifndef MSP_STRINGS_WINDOWS1252_H_ +#define MSP_STRINGS_WINDOWS1252_H_ + +#include "codec.h" + +namespace Msp { + +class Windows1252: public StringCodec +{ +public: + class Encoder: public StringCodec::Encoder + { + public: + Encoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Encoder(em) { } + void encode_char(wchar_t); + private: + void append_replacement() { append(032); } + }; + + class Decoder: public StringCodec::Decoder + { + public: + Decoder(ErrorMode em=THROW_ON_ERROR): StringCodec::Decoder(em) { } + void decode_char(const std::string &, std::string::const_iterator &); + }; + + Encoder *create_encoder(ErrorMode em=THROW_ON_ERROR) const { return new Encoder(em); } + Decoder *create_decoder(ErrorMode em=THROW_ON_ERROR) const { return new Decoder(em); } +}; + +} // namespace Msp + +#endif