File: Platform.cpp

package info (click to toggle)
grcompiler 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 48,200; ansic: 7,670; sh: 4,427; makefile: 197; xml: 190; perl: 127; sed: 21
file content (401 lines) | stat: -rw-r--r-- 8,078 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <cassert>

#include "GrPlatform.h"


#ifdef _WIN32
#include "windows.h"

namespace gr
{

size_t Platform_UnicodeToANSI(const utf16 * prgchwSrc, size_t cchwSrc, char * prgchsDst, size_t cchsDst)
{
	auto const _cchwSrc = static_cast<int>(cchwSrc), _cchsDst = static_cast<int>(cchsDst); 
	assert(_cchwSrc == cchwSrc || _cchwSrc >= -1);
	assert(_cchsDst == cchsDst || _cchsDst >= -1);
	return ::WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)prgchwSrc, _cchwSrc, prgchsDst, _cchsDst, NULL, NULL);
}

size_t Platform_AnsiToUnicode(const char * prgchsSrc, size_t cchsSrc, utf16 * prgchwDst, size_t cchwDst)
{
	auto const _cchsSrc = static_cast<int>(cchsSrc), _cchwDst = static_cast<int>(cchwDst); 
	assert(_cchsSrc == cchsSrc || _cchsSrc >= -1);
	assert(_cchwDst == cchwDst || _cchwDst >= -1);
	return ::MultiByteToWideChar(CP_ACP, 0, prgchsSrc, _cchsSrc, (LPWSTR)prgchwDst, _cchwDst);
}

size_t Platform_8bitToUnicode(int nCodePage, const char * prgchsSrc, size_t cchsSrc,
	utf16 * prgchwDst, size_t cchwDst)
{
	auto const _cchsSrc = static_cast<int>(cchsSrc), _cchwDst = static_cast<int>(cchwDst); 
	assert(_cchsSrc == cchsSrc || _cchsSrc >= -1);
	assert(_cchwDst == cchwDst || _cchwDst >= -1);
	return ::MultiByteToWideChar(nCodePage, 0, prgchsSrc, _cchsSrc, (LPWSTR)prgchwDst, _cchwDst);
}

size_t utf8len(const char *s)
{
	return mbstowcs(NULL, s, 0);
}


utf16 *utf16cpy(utf16 *dest, const utf16 *src)
{
//	return wcscpy(dest, src);
	return (utf16*)wcscpy((wchar_t*)dest, (const wchar_t*)src);
}

utf16 *utf16ncpy(utf16 *dest, const utf16 *src, size_t n)
{
//	return wcsncpy(dest, src, n);
	return (utf16*)wcsncpy((wchar_t*)dest, (const wchar_t*)src, n);
}

utf16 *utf16ncpy(utf16 *dest, const char *src, size_t n)
{
	#ifdef UTF16DEBUG
	std::cerr << "utf16ncpy8: " << src << std::endl;
	#endif
	Platform_AnsiToUnicode(src, strlen(src), dest, n);
	return dest;
}

//size_t wcslen(const wchar_t *s);
size_t utf16len(const utf16 *s)
{
//	return wcslen(s);
	return wcslen((const wchar_t*)s);

}

int utf16cmp(const utf16 *s1, const utf16 *s2)
{
//	return wcscmp(s1, s2);
	return wcscmp((const wchar_t*)s1, (const wchar_t*)s2);

}

int utf16ncmp(const utf16 *s1, const utf16 *s2, size_t n)
{
//	return wcsncmp(s1, s2, n);
	return wcsncmp((const wchar_t*)s1, (const wchar_t*)s2, n);
}


int utf16cmp(const utf16 *s1, const char *s2)
{
	while (*s1 && s2)
	{
		if (*s1 < *s2)
		{
			return -1;
		}
		if (*s1 > *s2)
		{
			return 1;
		}
		*s1++;
		*s2++;
	}
	if (*s1) return -1;
	else if (*s2) return 1;
	return 0;
}

int utf16ncmp(const utf16 *s1, const char *s2, size_t n)
{
	size_t p = 0;
	while (*s1 && s2 && (p < n))
	{
		if (*s1 < *s2)
			return -1;
		if (*s1 > *s2)
			return 1;
		*s1++;
		*s2++;
		p++;
	}
	if (p == n)
		return 0;
	else if (*s1)
		return -1;
	else if (*s2)
		return 1;
	return 0;
}



}
#else // not _WIN32

// Standard Headers
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
// Platform headers
#include <iconv.h>


namespace gr
{

#ifdef UTF16DEBUG
void utf16Output(const utf16 *input)
{
	utf16	src_utf;
	while(src_utf = *input++)
	{
		std::cerr << char(0xff & src_utf);
		if (src_utf > 255)
			std::cerr << char(src_utf >> 8);
	}

	std::cerr.flush();
}
#endif

#if 0
utf16 *utf16ncpy(utf16 *dest, const char *src, size_t n)
{
	Platform_AnsiToUnicode(src, strlen(src), dest, n);
	return dest;
}
#endif

size_t utf16len(const utf16 *s)
{
	// assumes NULL terminated strings
	const utf16 *start = s;
	for (; *s; ++s);
	
	return s - start;
}

size_t utf16len(const utf16 *s, size_t n)
{
	const utf16 *start = s;
	for (; *s && static_cast<size_t>(s - start) <= n; ++s) ;

	return s - start;
}

size_t utf8len(const char *s)
{
	static const int is_initial[] = {1, 1, 0, 1};
	// assumes NULL terminated strings
	size_t length = 0;
	for (; *s; ++s)
		length += is_initial[*s >> 6];
	
	return length;	
}

utf16 *utf16cpy(utf16 *dest, const utf16 *src)
{
    memcpy(dest, src, sizeof(utf16) * utf16len(src));
    return dest;
}

utf16 *utf16ncpy(utf16 *dest, const utf16 *src, size_t n)
{
    memcpy(dest, src, sizeof(utf16) * std::min(utf16len(src, n), n));
    return dest;
}

#if 0
int utf16cmp(const utf16 *s1, const utf16 *s2)
{
	#ifdef UTF16DEBUG
	std::cerr << "utf16cmp16: comparing";
	utf16Output(s1);
	std::cerr << " and ";
	utf16Output(s1);
	std::cerr << std::endl;
	#endif
	while (*s1 && s2)
	{
		if (*s1 < *s2)
		{
			return -1;
		}
		if (*s1 > *s2)
		{
			return 1;
		}
		*s1++;
		*s2++;
	}
	if (*s1) return -1;
	else if (*s2) return 1;
	return 0;
}

int utf16ncmp(const utf16 *s1, const utf16 *s2, size_t n)
{
	#ifdef UTF16DEBUG
	std::cerr << "utf16cmp16: comparing";
	utf16Output(s1);
	std::cerr << " and ";
	utf16Output(s1);
	std::cerr << std::endl;
	#endif
	size_t p=0;
	while (*s1 && s2 && (p<n))
	{
		if (*s1 < *s2)
		{
			return -1;
		}
		if (*s1 > *s2)
		{
			return 1;
		}
		*s1++;
		*s2++;
		p++;
	}
	if (p==n) return 0;
	else if (*s1) return -1;
	else if (*s2) return 1;
	return 0;
}

int utf16cmp(const utf16 *s1, const char *s2)
{
	#ifdef UTF16DEBUG
	std::cerr << "utf16cmp: " << s1 << "::" << s2 << std::endl;
	#endif
	// let's just assume that both are ascii for now
	while (*s1 && s2)
	{
		if (*s1 < *s2)
		{
			return -1;
		}
		if (*s1 > *s2)
		{
			return 1;
		}
		*s1++;
		*s2++;
	}
	if (*s1) return -1;
	else if (*s2) return 1;
	return 0;
}

int utf16ncmp(const utf16 *s1, const char *s2, size_t n)
{
	size_t p = 0;
	while (*s1 && s2 && (p < n))
	{
		if (*s1 < *s2)
			return -1;
		if (*s1 > *s2)
			return 1;
		*s1++;
		*s2++;
		p++;
	}
	if (p == n)
		return 0;
	else if (*s1)
		return -1;
	else if (*s2)
		return 1;
	return 0;
}
#endif 


size_t Platform_UnicodeToANSI(const utf16 *src, size_t src_len, char *dest, size_t dest_len)
{
	if (!dest)
		return src_len;
	
	// Unicode to the 1252 codepage.  If codepoint value is above 255 then 
	//  output '?' instead.
	utf16	src_utf;
	for (size_t n_chars = std::min(src_len, dest_len); n_chars; --n_chars)
		*dest++ = (src_utf = *src++) > 255 ? '?' : char(src_utf);
	
	if (dest_len > src_len) 
		*dest = '\0';
	
	return dest_len;
}


size_t Platform_AnsiToUnicode(const char * src, size_t src_len, utf16 * dest, size_t dest_len)
{
	if (!dest)
		return src_len;
	
	// What is commonly refered to ANSI is close enough to Latin 1 codepage to
	//  allow just copy the 8-bit values into the low 8-bits of the UTF values.
	for (size_t n_chars = std::min(src_len, dest_len); n_chars; --n_chars)
		*dest++ = utf16(*src++);
		
	if (dest_len > src_len) 
		*dest = '\0';
	
	return dest_len;
}

size_t Platform_8bitToUnicode(int nCodePage, const char * prgchsSrc, size_t cchsSrc,
	utf16 * prgchwDst, size_t cchwDst)
{
	return MultiByteToWideChar(nCodePage, 0, prgchsSrc, cchsSrc, prgchwDst, cchwDst);
}

unsigned short MultiByteToWideChar(unsigned long code_page, unsigned long, 
        const char * source, size_t src_count, 
        unsigned short * dest, size_t dst_count)
{
    if (!dest)	// An error condition is indicated by 0
        return 0;

    if (src_count == size_t(-1))     // When passed -1 it should find the 
	src_count = strlen(source);  // length of the source it's self

    if (dst_count == 0)		// We cannot find the completed size so esitmate it.
	return src_count*3;

    std::ostringstream oss; oss << "CP" << code_page;
    auto const fromcode = oss.str();

#if WORDS_BIGENDIAN
    auto const tocode = "UCS-2BE";
#else
    auto const tocode = "UCS-2LE";
#endif
    iconv_t cdesc = iconv_open(tocode, fromcode.c_str());
   
    if (cdesc == iconv_t(-1))
    {
	std::cerr << program_invocation_short_name
                  << ": iconv_open(\"" << tocode << "\", \"" << oss.str() << "\"): "
                  << strerror(errno) << std::endl;
	exit(1);
    }

    char *dst_ptr = reinterpret_cast<char *>(dest);
    char *src_ptr = const_cast<char *>(source);
    dst_count *= sizeof(unsigned short);    
    const size_t dst_size = dst_count;

    iconv(cdesc, &src_ptr, &src_count, &dst_ptr, &dst_count);
    iconv_close(cdesc);
    
    return dst_size - dst_count;
}


} // namespace gr

#endif // _WIN32