LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
S2ByteString.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
6// NAUTITIA
7//
8// jys 24-SEP-2002
9//
10// Byte String
11// This class uses was made to build exemples of dictionary written
12// in XML
13
14#include "S2ByteString.h"
15
16#include <fstream>
17using namespace std;
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21
22namespace Lima {
23namespace Common {
24namespace Misc {
25
27_s(NULL),
28_len(0) {
29}
30
32 delete[] _s;
33}
34
35// clears self
37 delete[] _s;
38 _s=NULL;
39 _len=0;
40 return *this;
41}
42
43// redefine an existing S2ByteString
45 delete[] _s;
46 _s = new unsigned char[s._len];
47 _len = s._len;
48 memcpy (_s, s._s, s._len);
49 return *this;
50}
51
52// return byte string pointer
53const unsigned char* S2ByteString::data() const {
54 return _s;
55}
56
57// return length of self
59 return _len;
60}
61
62// Adds the specified byte at the head/tail of self.
63// Returns a reference to self.
64S2ByteString& S2ByteString::append (const unsigned char c) {
65 unsigned char *old = _s;
66 _s = new unsigned char[_len+1];
67 memcpy (_s, old, _len);
68 _s[_len] = c;
69 _len = _len+1;
70 delete[] old;
71 return *this;
72}
73
74S2ByteString& S2ByteString::prepend (const unsigned char c) {
75 unsigned char *old = _s;
76 _s = new unsigned char[_len+1];
77 _s[0] = c;
78 memcpy (_s+1, old, _len);
79 _len = _len+1;
80 delete[] old;
81 return *this;
82}
83
84// Adds the specified byte string at the head/tail of self.
85// Returns a reference to self.
86S2ByteString& S2ByteString::append (const unsigned char *c,
87 const uint64_t length) {
88 unsigned char *old = _s;
89 _s = new unsigned char[_len+length];
90 memcpy (_s, old, _len);
91 memcpy (_s+_len, c, length);
92 _len = _len+length;
93 delete[] old;
94 return *this;
95}
96
97S2ByteString& S2ByteString::prepend (const unsigned char *c,
98 const uint64_t length) {
99 unsigned char *old = _s;
100 _s = new unsigned char[_len+length];
101 memcpy (_s, c, length);
102 memcpy (_s+length, old, _len);
103 _len = _len+length;
104 delete[] old;
105 return *this;
106}
107
108// Adds the specified string at the head/tail of self
109// It is assumed that string is in normal endian mode
110// (big or little depending of platform). String in file
111// is in big endian mode
112// Returns a reference to self,
114 unsigned char *old = _s;
115 uint64_t length = wcslen(s)*2;
116 _s = new unsigned char[_len+length];
117 memcpy (_s, old, _len);
118 // if big endian platform and sizeof(wchar_t)==2, memcpy works
119 // but it is a rare case, so memcpy is not used
120 wchar_t chr;
121 unsigned char *s2 = _s + _len;
122 for (uint64_t i=0; i<wcslen(s); i++) {
123 chr = s[i];
124 // highter significant byte first
125 // in case of sizeof(wchar_t)==4, 2 hightest bytes are discarded
126 *s2++ = (unsigned char)((chr&0xFF00)>>8);
127 *s2++ = (unsigned char) (chr&0x00FF);
128 }
129 _len = _len+length;
130 delete[] old;
131 return *this;
132}
133
135 unsigned char *old = _s;
136 uint64_t length = wcslen(s)*2;
137 _s = new unsigned char[_len+length];
138 wchar_t chr;
139 unsigned char *s2 = _s;
140 for (uint64_t i=0; i<wcslen(s); i++) {
141 chr = s[i];
142 // highter significant byte first
143 // in case of sizeof(wchar_t)==4, 2 hightest bytes are discarded
144 *s2++ = (unsigned char)((chr&0xFF00)>>8);
145 *s2++ = (unsigned char) (chr&0x00FF);
146 }
147 memcpy (_s+length, old, _len);
148 _len = _len+length;
149 delete[] old;
150 return *this;
151}
152
153} // Misc
154} // Common
155} // Lima
S2ByteString & prepend(const unsigned char c)
S2ByteString & operator=(const S2ByteString &)
const unsigned char * data() const
S2ByteString & append(const unsigned char c)
NAUTITIA.
STL namespace.