File size: 1,530 Bytes
b6bd94d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#pragma once

#include "ex.hh"
#include "text.hh"
#include <QStringList>
#include <iconv.h>

/// "Internationalization conversion" for char encoding conversion, currently implemented with iconv()
/// Only supports converting from a known "from" to UTF8
class Iconv
{
  iconv_t state;

public:

  DEF_EX( Ex, "Iconv exception", std::exception )
  DEF_EX_STR( exCantInit, "Can't initialize iconv conversion:", Ex )

  explicit Iconv( const char * from );

  ~Iconv();
  static QByteArray fromUnicode( const QString & input, const char * toEncoding );

  QString convert( const void *& inBuf, size_t & inBytesLeft );

  // Converts a given block of data from the given encoding to a wide string.
  static std::u32string toWstring( const char * fromEncoding, const void * fromData, size_t dataSize );

  // Converts a given block of data from the given encoding to an utf8-encoded
  // string.
  static std::string toUtf8( const char * fromEncoding, const void * fromData, size_t dataSize );
  static std::string toUtf8( const char * fromEncoding, std::u32string_view str );

  static QString toQString( const char * fromEncoding, const void * fromData, size_t dataSize );
  // tries to find a valid encoding from the given list of encodings.
  static QString findValidEncoding( const QStringList & encodings );
  // Copying/assigning isn't supported
  Q_DISABLE_COPY_MOVE( Iconv );
};