Freeciv21
Develop your civilization from humble roots to a global empire
fciconv.cpp
Go to the documentation of this file.
1 /*
2  ____ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3  / \__ contributors. This file is part of Freeciv21.
4 |\ / @ \ Freeciv21 is free software: you can redistribute it
5 \ \_______| \ .:|> and/or modify it under the terms of the GNU
6  \ ##| | \__/ General Public License as published by the Free
7  | ####\__/ \ Software Foundation, either version 3 of the License,
8  / / ## \| or (at your option) any later version.
9  / /__________\ \ You should have received a copy of the
10  L_JJ \__JJ GNU General Public License along with Freeciv21.
11  If not, see https://www.gnu.org/licenses/.
12  */
13 
14 #include "fciconv.h"
15 
16 #include "fc_config.h"
17 #include "fcintl.h"
18 
19 #include <cstdarg>
20 #include <cstdio>
21 
22 #include <QLocale>
23 #include <QTextCodec>
24 #include <QTextStream>
25 
26 static QTextCodec *localCodec;
27 static QTextCodec *dataCodec;
28 static QTextCodec *internalCodec;
29 
30 static const char *transliteration_string;
32 
39 void init_character_encodings(const char *my_internal_encoding,
40  bool my_use_transliteration)
41 {
43  if (my_use_transliteration) {
44  transliteration_string = "//TRANSLIT";
45  }
46 
47  /* Set the data encoding - first check $FREECIV_DATA_ENCODING,
48  * then fall back to the default. */
49  data_encoding = getenv("FREECIV_DATA_ENCODING");
50  if (!data_encoding) {
52  }
53 
54  /* Set the local encoding - first check $FREECIV_LOCAL_ENCODING,
55  * then ask the system. */
56  local_encoding = getenv("FREECIV_LOCAL_ENCODING");
57  if (!local_encoding) {
59  qstrdup(QLocale::system().name().toLocal8Bit().constData());
60  }
61 
62  /* Set the internal encoding - first check $FREECIV_INTERNAL_ENCODING,
63  * then check the passed-in default value, then fall back to the local
64  * encoding. */
65  internal_encoding = getenv("FREECIV_INTERNAL_ENCODING");
66  if (!internal_encoding) {
67  internal_encoding = my_internal_encoding;
68 
69  if (!internal_encoding) {
71  }
72  }
73  localCodec = QTextCodec::codecForLocale();
74  dataCodec = QTextCodec::codecForName(data_encoding);
75  internalCodec = QTextCodec::codecForName(internal_encoding);
76 #ifdef FREECIV_ENABLE_NLS
77  bind_textdomain_codeset("freeciv21-core", internal_encoding);
78 #endif
79 
80 #ifdef FREECIV_DEBUG
81  fprintf(stderr, "Encodings: Data=%s, Local=%s, Internal=%s\n",
83 #endif // FREECIV_DEBUG
84 }
85 
90 const char *get_internal_encoding() { return internal_encoding; }
91 
92 char *data_to_internal_string_malloc(const char *text)
93 {
94  QString s;
95  s = dataCodec->toUnicode(text);
96  s = internalCodec->fromUnicode(s);
97  return qstrdup(s.toLocal8Bit().data());
98 }
99 char *internal_to_data_string_malloc(const char *text)
100 {
101  QString s;
102  s = internalCodec->toUnicode(text);
103  s = dataCodec->fromUnicode(s);
104  return qstrdup(s.toLocal8Bit().data());
105 }
106 char *internal_to_local_string_malloc(const char *text)
107 {
108  QString s;
109  s = internalCodec->toUnicode(text);
110  s = localCodec->fromUnicode(s);
111  return qstrdup(s.toLocal8Bit().data());
112 }
113 char *local_to_internal_string_malloc(const char *text)
114 {
115  QString s;
116  s = localCodec->toUnicode(text);
117  s = internalCodec->fromUnicode(s);
118  return qstrdup(s.toLocal8Bit().data());
119 }
120 char *local_to_internal_string_buffer(const char *text, char *buf,
121  size_t bufsz)
122 {
123  QString s;
124  s = localCodec->toUnicode(text);
125  s = internalCodec->fromUnicode(s);
126  return qstrncpy(buf, s.toLocal8Bit().data(), bufsz);
127 }
128 
132 void fc_fprintf(FILE *stream, const char *format, ...)
133 {
134  va_list ap;
135 
136  va_start(ap, format);
137  auto str = QString::vasprintf(format, ap);
138  va_end(ap);
139 
140  QTextStream(stream) << str;
141 }
142 
153 size_t get_internal_string_length(const char *text)
154 {
155  return QString(text).length();
156 }
char * internal_to_data_string_malloc(const char *text)
Definition: fciconv.cpp:99
size_t get_internal_string_length(const char *text)
Return the length, in characters, of the string.
Definition: fciconv.cpp:153
char * local_to_internal_string_malloc(const char *text)
Definition: fciconv.cpp:113
static const char * local_encoding
Definition: fciconv.cpp:31
static QTextCodec * localCodec
Definition: fciconv.cpp:26
void fc_fprintf(FILE *stream, const char *format,...)
Do a fprintf from the internal charset into the local charset.
Definition: fciconv.cpp:132
static const char * transliteration_string
Definition: fciconv.cpp:30
char * local_to_internal_string_buffer(const char *text, char *buf, size_t bufsz)
Definition: fciconv.cpp:120
static QTextCodec * dataCodec
Definition: fciconv.cpp:27
char * internal_to_local_string_malloc(const char *text)
Definition: fciconv.cpp:106
void init_character_encodings(const char *my_internal_encoding, bool my_use_transliteration)
Must be called during the initialization phase of server and client to initialize the character encod...
Definition: fciconv.cpp:39
const char * get_internal_encoding()
Return the internal encoding.
Definition: fciconv.cpp:90
static QTextCodec * internalCodec
Definition: fciconv.cpp:28
static const char * data_encoding
Definition: fciconv.cpp:31
static const char * internal_encoding
Definition: fciconv.cpp:31
char * data_to_internal_string_malloc(const char *text)
Definition: fciconv.cpp:92
#define FC_DEFAULT_DATA_ENCODING
Definition: fciconv.h:69
const char * name
Definition: inputfile.cpp:118