summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/GLT/string.cpp
blob: 345054652f8f97705081d896a14882b5fa2089df (plain)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include "glt_string.h"

/*! \file 
	\brief   string and wstring utility routines 
	\ingroup Misc
*/

/*! \example string.cpp

	String functionality example and regression test.

	Output:
	\include string.ok
*/

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

bool isBinary(const std::string  &str)
{
	const char *begin = str.c_str();
	const char *end   = begin + str.size();

	for (const char *i=begin; i!=end; i++)
	{
		char c = *i;
		if ((c<32 || c>126) && c!='\t' && c!='\r' && c!='\n')
			return true;
	}

	return false;
}

// Note, would be faster perhaps to use char *
// instead of integer index

void dos2unix(string &dest,const string &src)
{
	// Find the size of the destination string

	string::size_type size = src.size();
	for (string::size_type i=0;i<src.size();i++)
		if (src[i]==13)
			size--;

	// Bail out early, if possible

	if (size==src.size())
	{
		dest = src;
		return;
	}

	// Allocate the correct size for destination

	dest.resize(size);

	// Copy everything except ASCII 13

	string::size_type k = 0;
	for (string::size_type j=0;j<src.size();j++)
		if (src[j]!=13)
			dest[k++] = src[j];

	assert(k==size);
}

void dos2unix(wstring &dest,const wstring &src)
{
	// Remove all instances of carriage return (ASCII 13)

	// Find the size of the destination string

	wstring::size_type size = src.size();
	for (wstring::size_type i=0;i<src.size();i++)
		if (src[i]==13)
			size--;

	// Bail out early, if possible

	if (size==src.size())
	{
		dest = src;
		return;
	}

	// Allocate the correct size for destination

	dest.resize(size);

	// Copy everything except the linefeeds

	wstring::size_type k = 0;
	for (wstring::size_type j=0;j<src.size();j++)
		if (src[j]!=13)
			dest[k++] = src[j];

	assert(k==size);
}

void unix2dos(string &dest,const string &src)
{
	// Insert carriage returns, where necessary (ASCII 13)

	// Find the size of the destination string

	string::size_type size = src.size();

	// Take first character into account

	if (src.size()>0)
		if (src[0]==10)
			size++;

	for (string::size_type i=0;i<src.size()-1;i++)
		if (src[i]!=13 && src[i+1]==10)
			size++;

	// Allocate the correct size for destination

	dest.resize(size);

	// Copy everything, inserting linefeeds where necessary

	string::size_type j = 0;
	string::size_type k = 0;

	if (src.size()>0)
		if (src[0]==10)
			dest[k++] = 13;

	for (;j<src.size()-1;j++)
		if (src[j]!=13 && src[j+1]==10)
		{
			dest[k++] = src[j];
			dest[k++] = 13;
		}
		else
			dest[k++] = src[j];

	if (j<src.size())
		dest[k++] = src[j++];

	assert(k==size);
}

void unix2dos(wstring &dest,const wstring &src)
{
	// Insert carriage returns, where necessary (ASCII 13)

	// Find the size of the destination string

	wstring::size_type size = src.size();

	// Take first character into account

	if (src.size()>0)
		if (src[0]==10)
			size++;

	for (wstring::size_type i=0;i<src.size()-1;i++)
		if (src[i]!=13 && src[i+1]==10)
			size++;

	// Allocate the correct size for destination

	dest.resize(size);

	// Copy everything, inserting linefeeds where necessary

	wstring::size_type j = 0;
	wstring::size_type k = 0;

	if (src.size()>0)
		if (src[0]==10)
			dest[k++] = 13;

	for (;j<src.size()-1;j++)
		if (src[j]!=13 && src[j+1]==10)
		{
			dest[k++] = src[j];
			dest[k++] = 13;
		}
		else
			dest[k++] = src[j];

	if (j<src.size())
		dest[k++] = src[j++];

	assert(k==size);
}

void readStream(istream &is,string &dest)
{
	while (is.good() && !is.eof())
	{
		const int bufferSize = 10240;
		char buffer[bufferSize];
		is.read(buffer,bufferSize);
		const int size = is.gcount();
		if (size>0)
			dest.insert(dest.end(),buffer,buffer+size);
	}
}

void writeStream(ostream &os,const string &src)
{
	os.write(src.c_str(),src.length());
}

// 
// Read a file into a Unicode string
//
// This function assumes that the file is
// in "normal" Unicode format, as a sequence
// of 16 bit codes.  The BOM (Byte Order
// Marker) is used to detect Unicode, and
// to swap endianess, if necessary. 
//

const wchar_t BOM  = 0xFEFF;
const wchar_t BOMe = 0xFFFE;	// Handle opposite endian

void readUnicodeStream(istream &is,wstring &dest)
{
	const int  bufferSize=1024;		// Buffer Size
	wchar_t    buffer[bufferSize];	// Buffer
	bool       firstBlock = true;	// Check header of first block
	bool       swap = false;		// Opposite Endian Origin

	dest = wstring();

	// As long as input stream is good

	while (is.good())
	{
		// Read into buffer and find out
		// how many bytes were read.

		is.read((char *) buffer,bufferSize*sizeof(wchar_t));
		int count = is.gcount()/sizeof(wchar_t);

		// If we read something, lets
		// do some processing.

		if (count)
		{
			// Check the first 16 bits
			// of first block for BOM
			// marker.  If it's in the
			// wrong order, enable swapping

			if (firstBlock)
			{
				if (buffer[0]!=BOM && buffer[0]!=BOMe)
					return;

				if (buffer[0]==BOMe)
					swap = true;

				firstBlock = false;
			}

			// If we're in swap mode,
			// swap high and low bytes
			// of each code

			if (swap)
				for (int c=0; c<count; c++)
					buffer[c] = (buffer[c]<<8)|(buffer[c]>>8);

			// Add the buffer to the 
			// Unicode string, ignoring
			// the BOM, if it exists as
			// the first element in the
			// buffer

			if (buffer[0]==BOM)
				dest.append(buffer+1,count-1);
			else
				dest.append(buffer,count);
		}
	}
}

//
// Write a unicode string to a file
//

void writeUnicodeStream(ostream &os,const wstring &src)
{
	os.write(reinterpret_cast<const char *>(&BOM),sizeof(wchar_t));
	os.write(reinterpret_cast<const char *>(src.data()),src.size()*sizeof(wchar_t));
}

void string2wstring(wstring &dest,const string &src)
{
	dest.resize(src.size());
	for (uint32 i=0; i<src.size(); i++)
		dest[i] = static_cast<unsigned char>(src[i]);
}

void wstring2string(string &dest,const wstring &src)
{
	dest.resize(src.size());
	for (uint32 i=0; i<src.size(); i++)
		dest[i] = src[i] < 256 ? src[i] : ' ';
}

//

// http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
//
// TODO: Support U-10000 onwards

void utf8decode(wstring &dest, const string &src)
{
	int i = 0;
	unsigned char *s = (unsigned char *) src.c_str();

        while (i<(int)src.size())
	{
		const wchar_t c = s[i++];

		// U-0 to U-7F 

		if ((c&0x80) == 0x00)
		{
			dest += c;
			continue;
		}

		// U-80 to U-7FF

		if ((c&0xE0) == 0xC0)
		{
                    if (i<(int)src.size())
			{
				const wchar_t d = s[i++];
				dest += (c&0x1f)<<6 | (d&0x3f);
				continue;
			}
		}

		// U-800 to U-FFFF

		if ((c&0xF0) == 0xE0)
		{
                    if ((i+1)<(int)src.size())
			{
				const wchar_t d = s[i++];
				const wchar_t e = s[i++];
				dest += (c&0x0f)<<12 | (d&0x3f)<<6 | (e&0x3f);
				continue;
			}
		}
	}
}

//

void bin2src_(std::ostream &os,bool &begin,const unsigned char *buffer,const int n)
{
	os.setf(ios::hex,ios::basefield);

	if (n>0 && !begin)
	{
		os << ',';
		os << endl;
	}

	begin = false;

	for (int i=0; i<n;i++)
	{
		os << "0x" << setw(2) << setfill('0') << (unsigned int) buffer[i];
		if (i<n-1)
			os << ',';
	}
}

void bin2src(std::ostream &os,const unsigned char *buffer,const int n)
{
	os << '{' << endl;
	
	bool begin = true;

	for (int i=0; i<n; i+=16)
		if (n-i>16)
			bin2src_(os,begin,buffer+i,16);
		else
			bin2src_(os,begin,buffer+i,n-i);

	os << endl << "};" << endl;
}

void bin2src(std::ostream &os, const std::string &src)
{
	bin2src(os,(const unsigned char *) src.c_str(),src.length());
}

void bin2src(std::ostream &os, std::istream &is)
{
	os << '{' << endl;
	
	bool begin = true;

	while (is.good() && !is.eof())
	{
		unsigned char buffer[16];
		is.read((char *) buffer,16);
		int size = is.gcount();
		bin2src_(os,begin,buffer,size);
	}

	os << endl << "};" << endl;
}

//

void bin2asm_(std::ostream &os,const unsigned char *buffer,const int n)
{
	if (n<=0)
		return;

	os.setf(ios::hex,ios::basefield);

	os << "\t.byte ";

	for (int i=0; i<n;i++)
	{
		os << "0x" << setw(2) << setfill('0') << (unsigned int) buffer[i];
		if (i<n-1)
			os << ',';
	}

	os << endl;
}

void bin2asm(std::ostream &os,const unsigned char *buffer,const int n)
{
	for (int i=0; i<n; i+=16)
		if (n-i>16)
			bin2asm_(os,buffer+i,16);
		else
			bin2asm_(os,buffer+i,n-i);
}

void bin2asm(std::ostream &os, const std::string &src)
{
	bin2asm(os,(const unsigned char *) src.c_str(),src.length());
}

void bin2asm(std::ostream &os, std::istream &is)
{
	while (is.good() && !is.eof())
	{
		unsigned char buffer[16];
		is.read((char *) buffer,16);
		int size = is.gcount();
		bin2asm_(os,buffer,size);
	}
}

unsigned int fromHex4(unsigned char ch)
{
	if (ch>='0' && ch<='9')
		return ch-'0';

	if (ch>='a' && ch<='f')
		return ch-'a'+10;

	if (ch>='A' && ch<='F')
		return ch-'A'+10;

	return 0;
}

unsigned char toHex4(unsigned int val)
{
	const unsigned char table[16] = { 
		'0', '1', '2', '3', '4', 
		'5', '6', '7', '8', '9', 
		'A', 'B', 'C', 'D', 'E', 'F'
	};

	return table[val&15];
}

bool stringSplit(vector<string> &vec,const string &str,const string &delim)
{
	vec.clear();

	if (delim.empty())
	{
		vec.push_back(str);
		return false;
	}

	string::size_type i = 0;
	string::size_type j = 0;

	for (;;)
	{
		j = str.find(delim,i);
		if (j==string::npos)
		{
			vec.push_back(str.substr(i));
			break;
		}

		vec.push_back(str.substr(i,j-i));
		i = j + delim.size();

		if (i==str.size())
		{
			vec.push_back(string());
			break;
		}
	}

	return true;
}

bool stringMerge(const vector<string> &vec, string &str,const string &delim)
{
	str = string();

        for (int i=0; i<(int)vec.size(); i++)
	{
		if (i>0)
			str += delim;

		str += vec[i];
	}

	return true;
}

double atof(const std::string &str) { return atof(str.c_str());                   }
int    atoi(const std::string &str) { return atoi(str.c_str());                   }
long   atol(const std::string &str) { return atol(str.c_str());                   }
bool   atob(const std::string &str) { return atoi(str.c_str())!=0 || str.substr(0,4)=="true"; }