QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsogrproxytextcodec.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsogrproxytextcodec.cpp
3  -------------
4  begin : June 2020
5  copyright : (C) 2020 Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsogrproxytextcodec.h"
17 #include "qgslogger.h"
18 #include <cpl_string.h>
19 #include <mutex>
20 
22  : mName( name )
23 {
24 
25 }
26 
27 QString QgsOgrProxyTextCodec::convertToUnicode( const char *chars, int, ConverterState * ) const
28 {
29  if ( !chars )
30  return QString();
31 
32  char *res = CPLRecode( chars, mName.constData(), CPL_ENC_UTF8 );
33  if ( !res )
34  {
35  QgsDebugMsg( "convertToUnicode failed" );
36  return QString();
37  }
38 
39  const QString result = QString::fromUtf8( res );
40  CPLFree( res );
41  return result;
42 }
43 
44 QByteArray QgsOgrProxyTextCodec::convertFromUnicode( const QChar *unicode, int length, ConverterState * ) const
45 {
46  if ( !unicode )
47  return QByteArray();
48 
49  const QString src = QString( unicode, length );
50  char *res = CPLRecode( src.toUtf8().constData(), CPL_ENC_UTF8, mName.constData() );
51  if ( !res )
52  {
53  QgsDebugMsg( "convertFromUnicode failed" );
54  return QByteArray();
55  }
56 
57  const QByteArray result = QByteArray( res );
58  CPLFree( res );
59  return result;
60 }
61 
62 // MY 5 YEAR OLD DAUGHTER WROTE THIS LINE, REMOVE AT YOUR OWN RISK!!!
63 // i don't want this to be here
64 
65 QByteArray QgsOgrProxyTextCodec::name() const
66 {
67  return mName;
68 }
69 
70 QList<QByteArray> QgsOgrProxyTextCodec::aliases() const
71 {
72  return QList<QByteArray>();
73 }
74 
76 {
77  // doesn't seem required in this case
78  return 0;
79 }
80 
82 {
83  static QStringList codecs;
84  static std::once_flag initialized;
85  std::call_once( initialized, [&]( )
86  {
87  // there's likely others that are supported by GDAL, but we're primarily concerned here
88  // with codecs used by the shapefile driver, and which are no longer supported on the
89  // windows Qt builds (due to removal of ICU support in windows Qt builds)
90  // see https://github.com/qgis/QGIS/issues/36871
91  for ( int i = 437; i <= 950; ++i )
92  codecs << QStringLiteral( "CP%1" ).arg( i );
93  for ( int i = 1250; i <= 1258; ++i )
94  codecs << QStringLiteral( "CP%1" ).arg( i );
95  codecs << QStringLiteral( "CP1251" );
96  } );
97  return codecs;
98 }
QByteArray name() const override
QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const override
QgsOgrProxyTextCodec(const QByteArray &name)
Constructor for QgsOgrProxyTextCodec, for the specified encoding name.
QString convertToUnicode(const char *in, int length, ConverterState *state) const override
int mibEnum() const override
QList< QByteArray > aliases() const override
static QStringList supportedCodecs()
Returns a list of supported text codecs.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38