• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • core
  • codecs
textcharcodec.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Core library, made within the KDE community.
3 
4  Copyright 2004,2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "textcharcodec.h"
24 
25 // lib
26 #include <character.h>
27 // KDE
28 #include <kglobal.h>
29 #include <klocale.h>
30 #include <kcharsets.h>
31 // Qt
32 #include <QtCore/QTextCodec>
33 
34 
35 namespace Okteta
36 {
37 
38 // static const char QTextCodecWhiteSpace = 63;
39 
40 static struct EncodingData
41 {
42  CharCoding encodingId;
43  const char* name;
44 }
45 const encodingDataList[] =
46 {
47  { ISO8859_1Encoding, "ISO-8859-1" },
48  { ISO8859_2Encoding, "ISO-8859-2" },
49  { ISO8859_3Encoding, "ISO-8859-3" },
50  { ISO8859_4Encoding, "ISO-8859-4" },
51  { ISO8859_5Encoding, "ISO-8859-5" },
52  { ISO8859_6Encoding, "ISO-8859-6" },
53  { ISO8859_7Encoding, "ISO-8859-7" },
54  { ISO8859_8Encoding, "ISO-8859-8" },
55  { ISO8859_8_IEncoding, "ISO-8859-8-I" },
56  { ISO8859_9Encoding, "ISO-8859-9" },
57  { ISO8859_11Encoding, "TIS-620" }, // was: ISO-8859-11
58  { ISO8859_13Encoding, "ISO-8859-13" },
59  { ISO8859_14Encoding, "ISO-8859-14" },
60  { ISO8859_15Encoding, "ISO-8859-15" },
61  { ISO8859_16Encoding, "ISO-8859-16" },
62  { CP1250Encoding, "windows-1250" },
63  { CP1251Encoding, "windows-1251" },
64  { CP1252Encoding, "windows-1252" },
65  { CP1253Encoding, "windows-1253" },
66  { CP1254Encoding, "windows-1254" },
67  { CP1255Encoding, "windows-1255" },
68  { CP1256Encoding, "windows-1256" },
69  { CP1257Encoding, "windows-1257" },
70  { CP1258Encoding, "windows-1258" },
71  { IBM850Encoding, "IBM850" },
72  { IBM866Encoding, "IBM866" },
73  { IBM874Encoding, "IBM874" },
74  { KOI8_REncoding, "KOI8-R" },
75  { KOI8_UEncoding, "KOI8-U" }
76 };
77 //TODO: WS2
78 static const unsigned int encodingDataListSize =
79  sizeof(encodingDataList)/sizeof(struct EncodingData);
80 
81 
82 static bool is8Bit( QTextCodec* codec )
83 {
84  bool result = false;
85 
86  const QByteArray& codecName = codec->name();
87  for( unsigned int i = 0; i < encodingDataListSize; ++i )
88  {
89  if( qstrcmp(codecName,encodingDataList[i].name) == 0 )
90  {
91  result = true;
92  break;
93  }
94  }
95 
96  return result;
97 }
98 
99 static QTextCodec* createLatin1()
100 {
101  return KGlobal::charsets()->codecForName( QLatin1String(encodingDataList[0].name) );
102 }
103 
104 /* heuristic seems to be doomed :(
105 static bool is8Bit( QTextCodec *Codec )
106 {
107  bool Result = true;
108 
109  // first test different for 0
110  unsigned char c[4];
111  c[0] = 0;
112  c[1] = c[2] = c[3] = 230;
113  QString S = Codec->toUnicode( (const char*)&c,4 );
114  int Length = 1;
115  QCString CS = Codec->fromUnicode( S, Length );
116  //kDebug() << Codec->name() << " "<<Length ;
117  if( Length > 0 )
118  Result = false;
119  // test if all chars survive the recoding
120  else
121  do
122  {
123  ++c[0];
124  S = Codec->toUnicode( (const char*)&c,4 );
125  Length = 1;
126  CS = Codec->fromUnicode( S, Length );
127  //kDebug() << Codec->name() << " "<<c[0]<<"->"<<CS[0]<<":"<<Length ;
128  if( Length != 1 || (CS[0] != (char)c[0] && CS[0] != QTextCodecWhiteSpace) )
129  {
130  Result = false;
131  break;
132  }
133  }
134  while( c[0] < 255 );
135  return Result;
136 }
137 const QStringList &TextCharCodec::codecNames()
138 {
139  // first call?
140  if( CodecNames.isEmpty() )
141 {
142  const QStringList &CharSets = KGlobal::charsets()->availableEncodingNames();
143 
144  for( QStringList::ConstIterator it = CharSets.begin(); it != CharSets.end(); ++it )
145 {
146  bool Found = true;
147  QTextCodec* Codec = KGlobal::charsets()->codecForName( *it, Found );
148  if( Found && is8Bit(Codec) )
149  CodecNames.append( QString::fromLatin1(Codec->name()) );
150 }
151 }
152 
153  return CodecNames;
154 }
155 
156 QString TextCharCodec::nameOfEncoding( CharCoding _char )
157 {
158  TextCharCodec *Codec = 0;
159 
160  const char* N = 0;
161  for( unsigned int i=0; i<NoOfEncodings; ++i )
162  {
163  if( EncodingNames[i].Encoding == _char )
164  {
165  N = EncodingNames[i].Name;
166  break;
167  }
168  }
169 
170  if( N != 0 )
171  {
172  QString CodeName = QString::fromLatin1( N );
173  }
174  return Codec;
175 }
176  */
177 
178 TextCharCodec* TextCharCodec::createLocalCodec()
179 {
180  QTextCodec* codec = KGlobal::locale()->codecForEncoding();
181  if( ! is8Bit(codec) )
182  codec = createLatin1();
183  return new TextCharCodec( codec );
184 }
185 
186 
187 TextCharCodec* TextCharCodec::createCodec( const QString& codecName )
188 {
189  bool isOk = false;
190  QTextCodec* codec = KGlobal::charsets()->codecForName( codecName, isOk );
191  if( isOk )
192  isOk = is8Bit( codec );
193  return isOk ? new TextCharCodec( codec ) : 0;
194 }
195 
196 
197 const QStringList& TextCharCodec::codecNames()
198 {
199  static QStringList textCodecNames;
200 
201  // first call?
202  if( textCodecNames.isEmpty() )
203  {
204  KCharsets* charsets = KGlobal::charsets();
205  for( unsigned int i = 0; i < encodingDataListSize; ++i )
206  {
207  bool isCodecFound = false;
208  const QString codecName = QString::fromLatin1( encodingDataList[i].name );
209  QTextCodec* codec = charsets->codecForName( codecName, isCodecFound );
210  if( isCodecFound )
211  textCodecNames.append( QString::fromLatin1(codec->name()) );
212  }
213  }
214 
215  return textCodecNames;
216 }
217 
218 
219 TextCharCodec::TextCharCodec( QTextCodec* textCodec )
220  : mCodec( textCodec ),
221  mDecoder( textCodec->makeDecoder() ),
222  mEncoder( textCodec->makeEncoder() )
223 {
224 }
225 
226 bool TextCharCodec::canEncode( const QChar& _char ) const
227 {
228  return mCodec->canEncode( _char );
229 }
230 
231 bool TextCharCodec::encode( Byte* byte, const QChar& _char ) const
232 {
233  if( !mCodec->canEncode(_char) ) // TODO: do we really need the codec?
234  return false;
235 
236  const QByteArray encoded = mEncoder->fromUnicode( QString(_char) );
237  if( encoded.size() > 0 )
238  {
239  *byte = encoded.at( 0 );
240  return true;
241  } else
242  return false;
243 }
244 
245 
246 Character TextCharCodec::decode( Byte byte ) const
247 {
248  // QTextCodecs "use this codepoint when input data cannot be represented in Unicode." (Qt docs)
249  static const QChar replacementChar = QChar( QChar::ReplacementCharacter );
250  const QString string =
251  mDecoder->toUnicode( reinterpret_cast<const char*>(&byte), 1 );
252  const QChar qchar = string.at( 0 );
253  const bool isDecoded = (qchar != replacementChar);
254  return Character( qchar, ! isDecoded );
255 }
256 
257 
258 const QString& TextCharCodec::name() const
259 {
260  if( mName.isNull() )
261  mName = QString::fromLatin1( mCodec->name() );
262 
263  return mName;
264 }
265 
266 TextCharCodec::~TextCharCodec()
267 {
268  delete mDecoder;
269  delete mEncoder;
270 }
271 
272 }
character.h
Okteta::encodingDataList
static struct Okteta::EncodingData encodingDataList[]
Okteta::TextCharCodec::mEncoder
QTextEncoder * mEncoder
encodes the chars from unicode
Definition: textcharcodec.h:65
Okteta::ISO8859_15Encoding
Definition: oktetacore.h:70
Okteta::CP1255Encoding
Definition: oktetacore.h:84
Okteta::CP1256Encoding
Definition: oktetacore.h:86
Okteta::TextCharCodec::codecNames
static const QStringList & codecNames()
Definition: textcharcodec.cpp:197
Okteta::IBM866Encoding
Definition: oktetacore.h:94
Okteta::CharCoding
CharCoding
Definition: oktetacore.h:39
Okteta::ISO8859_3Encoding
Definition: oktetacore.h:48
Okteta::ISO8859_8_IEncoding
Definition: oktetacore.h:60
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::ISO8859_1Encoding
ASCII encoding, also known as Latin1.
Definition: oktetacore.h:44
Okteta::TextCharCodec::mName
QString mName
Definition: textcharcodec.h:67
Okteta::encodingDataListSize
static const unsigned int encodingDataListSize
Definition: textcharcodec.cpp:78
Okteta::TextCharCodec::mCodec
QTextCodec * mCodec
Definition: textcharcodec.h:61
Okteta::is8Bit
static bool is8Bit(QTextCodec *codec)
Definition: textcharcodec.cpp:82
Okteta::TextCharCodec::createLocalCodec
static TextCharCodec * createLocalCodec()
Definition: textcharcodec.cpp:178
Okteta::KOI8_UEncoding
Definition: oktetacore.h:100
Okteta::TextCharCodec::encode
virtual bool encode(Byte *byte, const QChar &_char) const
Definition: textcharcodec.cpp:231
Okteta::IBM850Encoding
Definition: oktetacore.h:92
Okteta::TextCharCodec::TextCharCodec
TextCharCodec(QTextCodec *textCodec)
Definition: textcharcodec.cpp:219
Okteta::TextCharCodec
Definition: textcharcodec.h:40
Okteta::CP1258Encoding
Definition: oktetacore.h:90
Okteta::KOI8_REncoding
Definition: oktetacore.h:98
Okteta::TextCharCodec::name
virtual const QString & name() const
Definition: textcharcodec.cpp:258
Okteta::createLatin1
static QTextCodec * createLatin1()
Definition: textcharcodec.cpp:99
Okteta::TextCharCodec::mDecoder
QTextDecoder * mDecoder
decodes the chars to unicode
Definition: textcharcodec.h:63
Okteta::ISO8859_2Encoding
Definition: oktetacore.h:46
Okteta::CP1251Encoding
Definition: oktetacore.h:76
Okteta::ISO8859_13Encoding
Definition: oktetacore.h:66
Okteta::CP1253Encoding
Definition: oktetacore.h:80
Okteta::CP1257Encoding
Definition: oktetacore.h:88
Okteta::TextCharCodec::~TextCharCodec
virtual ~TextCharCodec()
Definition: textcharcodec.cpp:266
Okteta::TextCharCodec::canEncode
virtual bool canEncode(const QChar &_char) const
Definition: textcharcodec.cpp:226
Okteta::ISO8859_7Encoding
Definition: oktetacore.h:56
Okteta::TextCharCodec::decode
virtual Character decode(Byte byte) const
Definition: textcharcodec.cpp:246
Okteta::CP1250Encoding
Definition: oktetacore.h:74
Okteta::ISO8859_11Encoding
Definition: oktetacore.h:64
Okteta::CP1254Encoding
Definition: oktetacore.h:82
Okteta::CP1252Encoding
Definition: oktetacore.h:78
Okteta::ISO8859_5Encoding
Definition: oktetacore.h:52
Okteta::ISO8859_4Encoding
Definition: oktetacore.h:50
Okteta::IBM874Encoding
Definition: oktetacore.h:96
Okteta::Character
Definition: character.h:35
Okteta::TextCharCodec::createCodec
static TextCharCodec * createCodec(const QString &codeName)
Definition: textcharcodec.cpp:187
Okteta::ISO8859_8Encoding
Definition: oktetacore.h:58
textcharcodec.h
Okteta::ISO8859_9Encoding
Definition: oktetacore.h:62
Okteta::ISO8859_16Encoding
Definition: oktetacore.h:72
Okteta::ISO8859_14Encoding
Definition: oktetacore.h:68
Okteta::ISO8859_6Encoding
Definition: oktetacore.h:54
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal