From 6cbf9d2160a9f8e1ef98acb63ead3a14c88e2703 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 21 Apr 2007 22:18:54 +0000 Subject: [PATCH] Rename Latin1 as Iso88591 Add Windows1252 --- source/codec.cpp | 6 ++- source/{latin1.cpp => iso88591.cpp} | 6 +-- source/{latin1.h => iso88591.h} | 6 +-- source/windows1252.cpp | 65 +++++++++++++++++++++++++++++ source/windows1252.h | 40 ++++++++++++++++++ 5 files changed, 115 insertions(+), 8 deletions(-) rename source/{latin1.cpp => iso88591.cpp} (71%) rename source/{latin1.h => iso88591.h} (88%) create mode 100644 source/windows1252.cpp create mode 100644 source/windows1252.h 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/latin1.cpp b/source/iso88591.cpp similarity index 71% rename from source/latin1.cpp rename to source/iso88591.cpp index fcff15f..73169c8 100644 --- a/source/latin1.cpp +++ b/source/iso88591.cpp @@ -5,13 +5,13 @@ Copyright © 2006-2007 Mikko Rasa Distributed under the LGPL */ -#include "latin1.h" +#include "iso88591.h" using namespace std; namespace Msp { -void Latin1::Encoder::encode_char(wchar_t c_) +void Iso88591::Encoder::encode_char(wchar_t c_) { // Win32 has typedef unsigned short wchar_t int c=c_; @@ -21,7 +21,7 @@ void Latin1::Encoder::encode_char(wchar_t c_) append(c); } -void Latin1::Decoder::decode_char(const string &str, string::const_iterator &i) +void Iso88591::Decoder::decode_char(const string &str, string::const_iterator &i) { if(i==str.end()) return; diff --git a/source/latin1.h b/source/iso88591.h similarity index 88% rename from source/latin1.h rename to source/iso88591.h index 1dc52e3..ff82df4 100644 --- a/source/latin1.h +++ b/source/iso88591.h @@ -5,14 +5,14 @@ Copyright © 2006-2007 Mikko Rasa Distributed under the LGPL */ -#ifndef MSP_STRINGS_LATIN1_H_ -#define MSP_STRINGS_LATIN1_H_ +#ifndef MSP_STRINGS_ISO88591_H_ +#define MSP_STRINGS_ISO88591_H_ #include "codec.h" namespace Msp { -class Latin1: public StringCodec +class Iso88591: public StringCodec { public: class Encoder: public StringCodec::Encoder 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 -- 2.43.0