PageRenderTime 71ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/qt-everywhere-opensource-src-4.8.2/src/corelib/codecs/qlatincodec.cpp

#
C++ | 244 lines | 181 code | 23 blank | 40 comment | 22 complexity | bbd0649e2bc51b53e0f6da1dd2db8e43 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-4.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qlatincodec_p.h"
  42. #include "qlist.h"
  43. #ifndef QT_NO_TEXTCODEC
  44. QT_BEGIN_NAMESPACE
  45. QLatin1Codec::~QLatin1Codec()
  46. {
  47. }
  48. QString QLatin1Codec::convertToUnicode(const char *chars, int len, ConverterState *) const
  49. {
  50. if (chars == 0)
  51. return QString();
  52. return QString::fromLatin1(chars, len);
  53. }
  54. QByteArray QLatin1Codec::convertFromUnicode(const QChar *ch, int len, ConverterState *state) const
  55. {
  56. const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
  57. QByteArray r(len, Qt::Uninitialized);
  58. char *d = r.data();
  59. int invalid = 0;
  60. for (int i = 0; i < len; ++i) {
  61. if (ch[i] > 0xff) {
  62. d[i] = replacement;
  63. ++invalid;
  64. } else {
  65. d[i] = (char)ch[i].cell();
  66. }
  67. }
  68. if (state) {
  69. state->invalidChars += invalid;
  70. }
  71. return r;
  72. }
  73. QByteArray QLatin1Codec::name() const
  74. {
  75. return "ISO-8859-1";
  76. }
  77. QList<QByteArray> QLatin1Codec::aliases() const
  78. {
  79. QList<QByteArray> list;
  80. list << "latin1"
  81. << "CP819"
  82. << "IBM819"
  83. << "iso-ir-100"
  84. << "csISOLatin1";
  85. return list;
  86. }
  87. int QLatin1Codec::mibEnum() const
  88. {
  89. return 4;
  90. }
  91. QLatin15Codec::~QLatin15Codec()
  92. {
  93. }
  94. QString QLatin15Codec::convertToUnicode(const char* chars, int len, ConverterState *) const
  95. {
  96. if (chars == 0)
  97. return QString();
  98. QString str = QString::fromLatin1(chars, len);
  99. QChar *uc = str.data();
  100. while(len--) {
  101. switch(uc->unicode()) {
  102. case 0xa4:
  103. *uc = 0x20ac;
  104. break;
  105. case 0xa6:
  106. *uc = 0x0160;
  107. break;
  108. case 0xa8:
  109. *uc = 0x0161;
  110. break;
  111. case 0xb4:
  112. *uc = 0x017d;
  113. break;
  114. case 0xb8:
  115. *uc = 0x017e;
  116. break;
  117. case 0xbc:
  118. *uc = 0x0152;
  119. break;
  120. case 0xbd:
  121. *uc = 0x0153;
  122. break;
  123. case 0xbe:
  124. *uc = 0x0178;
  125. break;
  126. default:
  127. break;
  128. }
  129. uc++;
  130. }
  131. return str;
  132. }
  133. QByteArray QLatin15Codec::convertFromUnicode(const QChar *in, int length, ConverterState *state) const
  134. {
  135. const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
  136. QByteArray r(length, Qt::Uninitialized);
  137. char *d = r.data();
  138. int invalid = 0;
  139. for (int i = 0; i < length; ++i) {
  140. uchar c;
  141. ushort uc = in[i].unicode();
  142. if (uc < 0x0100) {
  143. if (uc > 0xa3) {
  144. switch(uc) {
  145. case 0xa4:
  146. case 0xa6:
  147. case 0xa8:
  148. case 0xb4:
  149. case 0xb8:
  150. case 0xbc:
  151. case 0xbd:
  152. case 0xbe:
  153. c = replacement;
  154. ++invalid;
  155. break;
  156. default:
  157. c = (unsigned char) uc;
  158. break;
  159. }
  160. } else {
  161. c = (unsigned char) uc;
  162. }
  163. } else {
  164. if (uc == 0x20ac)
  165. c = 0xa4;
  166. else if ((uc & 0xff00) == 0x0100) {
  167. switch(uc) {
  168. case 0x0160:
  169. c = 0xa6;
  170. break;
  171. case 0x0161:
  172. c = 0xa8;
  173. break;
  174. case 0x017d:
  175. c = 0xb4;
  176. break;
  177. case 0x017e:
  178. c = 0xb8;
  179. break;
  180. case 0x0152:
  181. c = 0xbc;
  182. break;
  183. case 0x0153:
  184. c = 0xbd;
  185. break;
  186. case 0x0178:
  187. c = 0xbe;
  188. break;
  189. default:
  190. c = replacement;
  191. ++invalid;
  192. }
  193. } else {
  194. c = replacement;
  195. ++invalid;
  196. }
  197. }
  198. d[i] = (char)c;
  199. }
  200. if (state) {
  201. state->remainingChars = 0;
  202. state->invalidChars += invalid;
  203. }
  204. return r;
  205. }
  206. QByteArray QLatin15Codec::name() const
  207. {
  208. return "ISO-8859-15";
  209. }
  210. QList<QByteArray> QLatin15Codec::aliases() const
  211. {
  212. QList<QByteArray> list;
  213. list << "latin9";
  214. return list;
  215. }
  216. int QLatin15Codec::mibEnum() const
  217. {
  218. return 111;
  219. }
  220. QT_END_NAMESPACE
  221. #endif // QT_NO_TEXTCODEC