PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/corelib/codecs/qlatincodec.cpp

https://bitbucket.org/cvp2ri/qt5-tlsauth
C++ | 244 lines | 181 code | 23 blank | 40 comment | 22 complexity | a4e7b0f64ecc04cc373e08ca2b604c58 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  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