LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
strwstrtools.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
15#include "strwstrtools.h"
16#include "readwritetools.h"
17
18#include <algorithm>
19#include <string.h>
20
34#include <cassert>
35#include <cstdlib>
36#include <iostream>
37#include <fstream>
38#include <sstream>
39#include <iomanip>
40#include <stdexcept>
41
42using namespace std;
43
44namespace Lima
45{
46namespace Common
47{
48namespace Misc
49{
50
51void readStream(std::istream &is,std::string &dest)
52{
53 while (is.good() && !is.eof())
54 {
55 const int bufferSize = 10240;
56 char buffer[bufferSize];
57 is.read(buffer,bufferSize);
58 const int size = is.gcount();
59 if (size>0)
60 dest.insert(dest.end(),buffer,buffer+size);
61 }
62}
63
64void writeStream(std::ostream &os,const std::string &src)
65{
66 os.write(src.c_str(),src.length());
67 os.flush();
68}
69
70//
71// Read one line of characters from a file into a string
72// additional LINE_FEED characters leads to empty string
73//
74
75std::string readLine(std::istream & inputFile)
76{
77 std::string result;
78
79 // read characters, up to '\n', into string
80 std::getline( inputFile, result, '\n');
81 // discard CR (Carriage return) for Windows
82 size_t len = result.length();
83 if( (len >= 1) && (result[len-1] == 0x0D) )
84 result.erase(len-1);
85 // discard BOM
86 size_t pos(0);
87 if( (result.length() >= 3) && ((unsigned char)(result[0]) == 0xEF)
88 && ((unsigned char)(result[1]) == 0xBB) && ((unsigned char)(result[2]) == 0xBF) ) {
89 pos = 3;
90 result.erase(0,pos);
91 }
92 return( result );
93}
94
95//
96// input/output of strings and wide string in binary files
97//
98
99// the strings are stored in binary files as a set of bytes (i.e. char *)
100// preceded by the number of bytes occupied
101void readString(istream& file, string& s)
102{
103 uint64_t i(0);
104 file.read((char *) &i,sizeof(uint64_t));
105 if (i)
106 {
107 char* cstr=new char[i];
108 file.read((char *) cstr,i*sizeof(char));
109 s=string(cstr);
110 delete []cstr;
111 }
112}
113
114void writeString(ostream& file, const string& s)
115{
116 uint64_t i(s.length()+1); // + 1 for ''
117 file.write((char *) &i, sizeof(uint64_t));
118 file.flush();
119 if (i)
120 {
121 const char* cstr=s.c_str();
122 file.write((char *) cstr,i*sizeof(char));
123 file.flush();
124 }
125}
126
127void readStringField(istream& file, string& s)
128{
129 s.clear();
130 uint64_t i=readCodedInt(file);
131 if (i)
132 {
133 char* cstr=new char[i+1]; // + 1 for '' (not in file)
134 file.read((char *) cstr,i*sizeof(char));
135 cstr[i]='\0'; // add the end of string character
136 s=string(cstr);
137 delete []cstr;
138 }
139}
140
141void writeStringField(ostream& file, const string& s)
142{
143 uint64_t i(s.size()); // not + 1 => does not write '\0'
144 writeCodedInt(file,i);
145 if (i)
146 {
147 const char* cstr=s.c_str();
148 file.write((char *) cstr,i*sizeof(char));
149 file.flush();
150 }
151}
152
153void readUTF8StringField(std::istream& file,
155{
156 s = LimaString();
157 uint64_t i=readCodedInt(file);
158 //std::cerr << "readUTF8StringField got size=" << i << std::endl;
159 if (i)
160 {
161 char* cstr = new char[i+1]; // + 1 for '\0' (not in file)
162 file.read((char *) cstr,i);
163 cstr[i]='\0'; // add the end of string character
164 //std::cerr << "readUTF8StringField got cstr=" << cstr << std::endl;
165 s = utf8stdstring2limastring(cstr);
166 //std::cerr << "readUTF8StringField got s size=" << s.size() << std::endl;
167 delete []cstr;
168 }
169}
170
171void writeUTF8StringField(std::ostream& file,
172 const Lima::LimaString& s)
173{
174 //std::cerr << "writeUTF8StringField " << s.toUtf8().constData() << " in file at " << file.tellp() << std::endl;
175 std::string str = limastring2utf8stdstring(s);
176 const char *cstr=str.c_str();
177 uint64_t i(str.size()); // not + 1 => does not write '\0'
178 //std::cerr << "writeUTF8StringField before writing i=" << i << ", file at " << file.tellp() << std::endl;
179 writeCodedInt(file,i);
180 //std::cerr << "writeUTF8StringField wrote i=" << i << ", file at " << file.tellp() << std::endl;
181 if (i)
182 {
183 file.write((char *) cstr,i);
184 file.flush();
185 //std::cerr << "writeUTF8StringField wrote cstr=" << cstr << ", file at " << file.tellp() << std::endl;
186 }
187}
188
189//
190// conversion functions
191//
192
194{
195 return LimaString::fromUtf8(src.c_str());
196}
197
198
203std::string localestring2utf8string(const std::string &src)
204{
205 return QString::fromLocal8Bit(src.c_str()).toUtf8().data();
206}
207
213{
214 return QString::fromLatin1(src.c_str());
215}
216
217/*
218 * returns the input phrase converted to utf-8, up to size bytes
219 * (check that converted phrase does not exceed size)
220 */
221std::string limastring2utf8stdstring(const Lima::LimaString & phrase, uint32_t size0 )
222{
223 return phrase.left(size0).toUtf8().data();
224}
225
227{
228 return src.toUtf8().constData();
229}
230
231//
232
236LimaString setStringSameEndian (const unsigned short *s,
237 const uint32_t count)
238{
239 LimaString result;
240 result.resize(count);
241
242 if (sizeof(LimaChar)==2)
243 {
244 result = *(const LimaChar*)s;
245 }
246 else
247 for (uint32_t index=0; index<count; index++)
248 result[index] = QChar((wchar_t)s[index]);
249 return result;
250}
251
255LimaString setStringReverseEndian (const unsigned short* s,
256 const uint32_t count)
257{
258 LimaString result;
259 result.resize(count);
260/*
261 if (sizeof(LimaChar)==2) {
262 // 0 1 2 3 4 5 6 ...
263 // + 1 -1 1 -1 1 -1 1 ...
264 // = 1 0 3 2 5 4 7 ...
265 int offset = 1;
266 for (uint64_t index=0; index<count*2; index++) {
267 result[index] = ((uint8_t*)s)[index+offset];
268 offset = -offset;
269 }
270 }
271 else {*/
272 unsigned short chr;
273 for (uint32_t index=0; index<count; index++) {
274 chr = s[index];
275 result[index] = (LimaChar)(((chr&0x00FF)<<8)|((chr&0xFF00)>>8));
276 }
277// }
278 return result;
279}
280
282{
283 LimaString xml;
284 LimaString::const_iterator it, it_end;
285 it = str.begin(); it_end = str.end();
286 for (; it != it_end; it++)
287 {
288 switch (it->toLatin1())
289 {
290 case '<' : xml += Common::Misc::utf8stdstring2limastring("&lt;"); break;
291 case '&' : xml += Common::Misc::utf8stdstring2limastring("&amp;"); break;
292 case '>' : xml += Common::Misc::utf8stdstring2limastring("&gt;"); break;
293 case '"' : xml += Common::Misc::utf8stdstring2limastring("&quot;"); break;
294 case '\'' : xml += Common::Misc::utf8stdstring2limastring("&apos;"); break;
295 default:
296 {
297 xml += LimaChar(*it);
298 }
299 }
300 }
301 return xml;
302}
303
304
305} // closing namespace Misc
306} // closing namespace Common
307} // closing namespace Lima
std::string localestring2utf8string(const std::string &src)
␈rief Convert a string in the locale codepage to a UTF8 string
void writeUTF8StringField(std::ostream &file, const Lima::LimaString &s)
Binary write a a LimaString in a file as an utf8 string.
void readString(istream &file, string &s)
Read an input stream into a wide string, until EOF.
std::string readLine(std::istream &inputFile)
void writeString(ostream &file, const string &s)
write a string on an output file stream
uint64_t readCodedInt(std::istream &file)
read a integer coded in variable-byte format in a file
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
void readStringField(istream &file, string &s)
void readStream(std::istream &is, std::string &dest)
Read an input stream into a string, until EOF.
LimaString setStringSameEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16
void writeStringField(ostream &file, const string &s)
void readUTF8StringField(std::istream &file, Lima::LimaString &s)
Binary read an utf8 string from a file and store it in a LimaString.
LimaString transcodeToXmlEntities(const LimaString &str)
void writeStream(std::ostream &os, const std::string &src)
Write a string to an output stream.
LimaString latin15stdstring2limastring(const std::string &src)
Convert a latin15 string to a LimaString.
void writeCodedInt(std::ostream &file, const uint64_t number)
write a integer coded in variable-byte format in a file
LimaString setStringReverseEndian(const unsigned short *s, const uint32_t count)
redefine an existing LimaString from UTF-16 changing the endian mode
LimaString utf8stdstring2limastring(const std::string &src)
NAUTITIA.
QChar LimaChar
Definition LimaString.h:30
QString LimaString
Definition LimaString.h:33
STL namespace.