-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
1608 lines (1343 loc) · 38.4 KB
/
misc.cpp
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
======================================================================
RSS Power Tool Source Code
Copyright (C) 2013 Gregory Naughton
This file is part of RSS Power Tool
RSS Power Tool is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RSS Power Tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RSS Power Tool If not, see <http://www.gnu.org/licenses/>.
======================================================================
*/
#include <limits.h>
#include <stdlib.h>
#include <ctype.h> // tolower
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
//#include <SDL/SDL.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/utsname.h>
#include "misc.h"
#include "tokenizer.h"
//#define _VA_BUF_SZ 8192
// these can be redefined for hooking into external text handling systems
#define _printf printf
#define _error error
#define _warn warning
const bool STDIO_ENABLED = 1;
char *va( const char *format, ... )
{
va_list argptr;
static char string[ _VA_BUF_SZ ];
va_start( argptr, format );
vsnprintf( string, sizeof( string ), format, argptr );
va_end( argptr );
string[ sizeof( string ) - 1 ] = '\0';
return string;
}
void error( const char * fmt, ... )
{
if ( !STDIO_ENABLED )
return;
char buffer[ _VA_BUF_SZ ];
va_list argptr;
va_start( argptr, fmt );
vsnprintf( buffer, sizeof( buffer ), fmt, argptr );
va_end( argptr );
fprintf(stderr, "error: %s", buffer );
exit( EXIT_FAILURE );
}
void warning( const char * fmt, ... )
{
if ( !STDIO_ENABLED )
return;
char buffer[ _VA_BUF_SZ ];
va_list argptr;
va_start( argptr, fmt );
vsnprintf( buffer, sizeof( buffer ), fmt, argptr );
va_end( argptr );
fprintf(stderr, "warning: %s", buffer );
}
// disabled when compiler _DEBUG not set
void _hidden_Assert( int die_if_false, const char *exp, const char *file, int line )
{
if ( !die_if_false )
{
fprintf( stderr, "Assert failed with expression: \"%s\" at: %s, line: %d\n", exp, file, line );
fflush( stderr );
#ifdef _WINDOWS
__asm {
int 0x3;
}
#endif
_error( "Assert failed with expression: \"%s\" at: %s, line: %d\n", exp, file, line );
}
}
// using stdio
unsigned int file_size( const char * path )
{
FILE *fp = fopen ( path, "r" );
if ( !fp ) {
return (unsigned int)-1;
}
long a = ftell(fp);
fseek(fp,0L,SEEK_END);
long b = ftell(fp);
fclose(fp);
return (unsigned int) (b - a);
}
// using sys/stat
unsigned int get_filesize( const char * path )
{
struct stat ss;
stat( path, &ss );
return (unsigned int)ss.st_size;
}
// looks for both kinds of path seperator
const char * strip_path( const char *s )
{
const char *w = strrchr ( s, '\\' );
const char *u = strrchr ( s, '/' );
if ( !w && !u )
return s;
if ( w && u )
return s;
if ( w && !u ) {
return (const char *)(w+1);
}
if ( !w && u ) {
return (const char *)(u+1);
}
return s;
}
const char * strip_extension( const char *s )
{
static char buf[ 1000 ];
if ( !s || !s[0] ) {
buf[0] = 0;
return buf;
}
memset( buf, 0, sizeof(buf) );
strcpy( buf, s );
char * ne = strrchr( buf, '.' );
if ( !ne )
return buf;
*ne = '\0';
return buf;
}
const char * strip_path_and_extension( const char * s )
{
static char buf[ 100 ];
if ( !s || !s[0] ) {
buf[0] = 0;
return buf;
}
const char * basename = strip_path( s );
memset( buf, 0, sizeof(buf) );
strcpy( buf, basename );
char * ne = strrchr( buf, '.' );
if ( !ne )
return basename;
*ne = '\0';
return buf;
}
char * copy_string( const char * s )
{
if ( !s || !s[0] )
return '\0';
unsigned int len = strlen(s);
unsigned int bytes = len * sizeof(char) ;
// checking for null-terminator stops string from growing 1-byte each-time it's copied
if ( s[len - 1] != '\0' )
++bytes;
char * str = (char *) malloc( bytes );
memset( str, 0, bytes );
strcpy( str, s );
return str;
}
/*
====================
FlipULong
endian utility
====================
*/
unsigned long FlipULong( unsigned long x ) {
return ((x>>24)&255) | (((x>>16)&255)<<8) | (((x>>8)&255)<<16) | ((x&255)<<24);
}
/*
====================
little_endian
helper function to tell if system is little endian
====================
*/
int is_little_endian( void ) {
short _short_int = 0x0001;
return (int)((char*)&_short_int)[0];
}
// strspn = returns pointer to the first character in haystack which is not in whitelist,
// if whitelist begins with '\0', haystack is returned
const char * strspn_p( const char * haystack, const char * whitelist )
{
if ( !haystack || !whitelist || !*haystack )
return 0;
if ( !*whitelist && *haystack )
return haystack;
const char * p = haystack;
while ( *p )
{
const char * w = whitelist;
int found = 0;
while ( *w )
{
if ( *w++ == *p ) {
found = 1;
break;
}
}
if ( !found )
return p;
++p;
}
return 0;
}
// strspn = returns pointer to the first character in haystack that is in whitelist.
// If whitelist is 0, 0 is returned. If whitelist begins with '\0',
// strcspn_p will try to match that. If end of haystack is hit, and
// whitelist not found, then terminating '\0' is returned.
const char * strcspn_p( const char * haystack, const char * whitelist )
{
if ( !haystack || !whitelist || !*haystack )
return 0;
const char * p = haystack;
while ( *p )
{
const char * w = whitelist;
while ( *w )
{
if ( *w++ == *p ) {
return p;
}
}
++p;
}
if ( p != haystack )
return p;
return 0;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Classes Section
|
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/////////////////////////////////////////////////////////////////////////////
//
// dirlist_t
//
void dirlist_t::free_listdir( char ** pp )
{
char ** save_p = pp;
while ( *pp )
free( *pp++ );
free ( save_p );
}
dirlist_t::~dirlist_t() {
this->free_listdir( filenames );
}
int dirlist_t::_read_dir( const char * dir )
{
char * fullpath = realpath( dir, 0 );
DIR * dirp = opendir( dir );
if ( !dirp ) {
_warn( "bad dir: \"%s\"\n", dir );
return 0;
}
struct dirent * ent = 0;
int files_count = 0;
// count total files in directory, so that we can allocate
while ( (ent = readdir( dirp )) )
{
if ( *ent->d_name != '.' && (ent->d_type == DT_DIR || ent->d_type == DT_REG) )
++files_count;
}
closedir( dirp );
unsigned int sz = (files_count+1) * sizeof(char*) ;
char ** pp = (char**) malloc ( sz );
memset( pp, 0, sz );
dirp = opendir( dir );
int files_got = 0;
while ( (ent = readdir( dirp )) )
{
if ( *ent->d_name != '.' && (ent->d_type == DT_DIR || ent->d_type == DT_REG) )
{
//
// test against suffix filters (if any), skipping any dirents that do not match any of the filters
//
int match = suffix_count == 0 ? 1 : 0;
for ( unsigned int cur_sfx = 0; cur_sfx < suffix_count; cur_sfx++ )
{
int len = strlen( ent->d_name );
int slen = suffix[cur_sfx].length();
// long enough to hold it, lets take a look
if ( len >= slen )
{
char * src = &ent->d_name[len-slen];
if ( strcasestr( src, suffix[cur_sfx].str ) ) {
match = 1; // found a match
break;
}
}
}
if ( !match )
continue;
char buf[2048];
sprintf( buf, "%s/%s", fullpath, ent->d_name );
pp[files_got] = copy_string( buf );
++total_entries;
if ( ++files_got >= files_count )
break;
}
}
closedir( dirp );
free( fullpath );
filenames = pp;
return 1;
}
int dirlist_t::read( const char * _path )
{
if ( !_path )
_path = path;
if ( !_path )
return 0;
// check if it's already open/been-read. if so, empty it and start over
if ( total_entries > 0 ) {
free_listdir( filenames );
}
return _read_dir( _path );
}
void dirlist_t::add_suffix_filter( const char * filter )
{
basicString_t suf(filter);
suffix[suffix_count++] = suf;
}
void dirlist_t::sort_alphabetical( void )
{
unsigned int i, j;
for ( j = 1; j < total_entries; j++ )
{
i = j;
char * tmp = filenames[j];
while ( i > 0 && strcmp( filenames[i-1], tmp ) > 0 ) {
filenames[i] = filenames[i-1];
i--;
}
filenames[i] = tmp;
}
}
////////////////////////////////////////////////////////////////////
//
// basicString_t
//
#define APPEND_MIN_ALLOC_SZ 32
basicString_t::basicString_t( const char * A ) : str(0), len(0), memlen(0) {
if ( !A || !*A )
return;
unsigned int _len = strlen( A );
if ( _len > 0 ) {
len = _len;
memlen = len + 1;
str = new char[ memlen ];
::strncpy( str, A, len );
str[len] = 0;
}
}
basicString_t::basicString_t( const basicString_t& t ) : str(0), len(0), memlen(0) {
if ( t.str ) {
len = t.len;
#ifdef _DEBUG
unsigned int _len = strlen( t.str );
Assert( _len == len );
#endif
memlen = len + 1;
str = new char[ memlen ];
::strncpy( str, t.str, len );
str[len] = 0;
}
}
basicString_t& basicString_t::set( const char * A )
{
if ( !A || !*A )
{
// since we are setting here, if the thing we are setting to is an empty string, we clip the string
erase();
}
else
{
len = strlen( A );
if ( len >= memlen && str ) {
delete[] str;
memlen = len + 1;
str = new char[ memlen ];
}
else if ( !str ) {
str = new char[len+1];
memlen = len + 1;
}
::strncpy( str, A, memlen );
str[len] = 0;
}
return *this;
}
basicString_t& basicString_t::set( const basicString_t& ss )
{
this->set( ss.str );
return *this;
}
basicString_t& basicString_t::operator=( const basicString_t& ss )
{
this->set( ss.str );
return *this;
}
void basicString_t::Print( const char * fmt ) {
if ( fmt )
_printf( fmt, str );
else
_printf( "%s", str );
}
basicString_t& basicString_t::strncpy( const char *newstr, unsigned int newlen )
{
if ( !newstr || !*newstr ) {
erase();
return *this;
}
else if ( !str )
{
str = new char[newlen+1];
len = newlen;
memlen = newlen + 1;
::strncpy( str, newstr, newlen );
str[newlen] = 0;
}
else if ( newlen >= memlen )
{
delete[] str;
str = new char[newlen+1];
memlen = newlen + 1;
len = newlen;
::strncpy( str, newstr, newlen );
str[newlen] = 0;
}
else
{
len = newlen;
::strncpy( str, newstr, newlen );
str[newlen] = 0;
}
return *this;
}
// Access with array operator causes string to be allocated if there is no string yet.
// If array-bound accessed is out of range, it also allocates.
// This way a char is always returned.
char& basicString_t::operator[]( unsigned int index )
{
if ( !str ) {
str = new char[index+1];
memlen = index + 1;
len = index;
memset( str, 0, sizeof(char) * memlen );
}
// accessing outside of memlen doesn't increase string length
else if ( index >= memlen )
{
memlen = index + 1;
char * newstr = new char[ memlen ];
memset( newstr, 0, sizeof(char)*memlen );
::strncpy( newstr, str, len );
delete[] str;
str = newstr;
}
// FIXME: note that accessing an index > len, doesn't set len
return str[index];
}
basicString_t& basicString_t::sprintf( const char *fmt, ... )
{
char buf[ _VA_BUF_SZ ];
char * buffer = &buf[0];
va_list argptr;
va_start( argptr, fmt );
int len_actual = vsnprintf( buffer, sizeof( buf ), fmt, argptr );
va_end( argptr );
if ( len_actual >= _VA_BUF_SZ )
{
buffer = (char*) malloc( len_actual+1 );
va_start( argptr, fmt );
vsnprintf( buffer, len_actual+1, fmt, argptr );
va_end( argptr );
set( buffer );
free( buffer );
}
else
set( buffer );
return *this;
}
basicString_t& basicString_t::append( const char * app, unsigned int app_len )
{
if ( !app || !*app )
return *this;
// if app_len is 0, we rely on app to be null-terminated for length
if ( 0 == app_len ) {
app_len = strlen( app );
}
if ( !str )
{
str = new char[app_len+1];
len = app_len;
memlen = app_len + 1;
::strncpy( str, app, app_len );
str[app_len] = 0;
}
else if ( len+app_len+1 <= memlen ) // will fit in the memory we already have
{
::strncpy( &str[len], app, app_len );
str[ len + app_len ] = 0;
len = len + app_len;
}
else
{
// staves off tiny, repeated allocations of 1-byte (which are common in certain applications)
// 1-byte allocations can throw nasty errors in various stdlib functions
unsigned int alloc_sz = app_len >= APPEND_MIN_ALLOC_SZ ? app_len : APPEND_MIN_ALLOC_SZ;
char *tmp = new char[len+alloc_sz+1];
::strncpy( tmp, str, len );
::strncpy( &tmp[len], app, app_len );
tmp[ len + app_len ] = 0;
delete[] str;
memlen = len + alloc_sz + 1;
len = len + app_len;
str = tmp;
}
return *this;
}
basicString_t& basicString_t::append( const basicString_t& more )
{
return append ( more.str );
}
basicString_t& basicString_t::operator+= ( const char * more )
{
return append( more );
}
basicString_t& basicString_t::operator+= ( const basicString_t& more )
{
return append( more );
}
basicString_t& basicString_t::operator+= ( const char c )
{
char tmp[2] = { 0, 0 };
tmp[0] = c;
return append( tmp, 1u );
}
basicString_t& basicString_t::operator+= ( const int i )
{
char _buf[32];
memset(_buf,0,sizeof(_buf));
::sprintf( _buf, "%i", i );
return append( _buf );
}
basicString_t& basicString_t::operator+= ( const float f )
{
char buf[32];
memset(buf,0,sizeof(buf));
::sprintf( buf, "%f", f );
return append( buf );
}
basicString_t& basicString_t::operator+= ( const double d )
{
char buf[32];
memset(buf,0,sizeof(buf));
::sprintf( buf, "%lf", d );
return append( buf );
}
// all compare functions only return true|false
bool basicString_t::compare( const char * test )
{
if ( !str || !*str ) {
if ( !test || !*test )
return true;
return false;
}
else if ( !test || !*test )
return false;
return strcmp( str, test ) == 0;
}
bool basicString_t::compare( const basicString_t& test )
{
return compare( test.str );
}
bool basicString_t::icompare( const char * _str )
{
if ( !str || !*str ) {
if ( !_str || !*_str )
return true;
return false;
}
else if ( !_str || !*_str )
return false;
char * s = const_cast<char *>( _str );
char * n = const_cast<char *>( str );
do
{
if ( char_tolower(*s) != char_tolower(*n) )
{
return false;
}
++s; ++n;
}
while ( *s && *n );
return (char_tolower(*s) - char_tolower(*n)) == 0;
}
// note: duplicate of above to satisfy compiler. Any changes need to be made to both!!!
bool basicString_t::icompare( const char * _str ) const
{
if ( !str || !*str ) {
if ( !_str || !*_str )
return true;
return false;
}
else if ( !_str || !*_str )
return false;
char * s = const_cast<char *>( _str );
char * n = const_cast<char *>( str );
do
{
if ( char_tolower(*s) != char_tolower(*n) )
{
return false;
}
++s; ++n;
}
while ( *s && *n );
return (char_tolower(*s) - char_tolower(*n)) == 0;
}
bool basicString_t::icompare( const basicString_t& ref )
{
return this->icompare( ref.str );
}
bool basicString_t::operator==( const char * test )
{
return compare( test );
}
bool basicString_t::operator==( const basicString_t& test )
{
return compare( test );
}
bool basicString_t::operator!=( const char * test )
{
return !compare( test );
}
bool basicString_t::operator!=( const basicString_t& test )
{
return !compare( test );
}
// pre-allocate or explicitly set the internal memory; will truncate string if
// new value is less than len, otherwise, will pad with \0
void basicString_t::setMem( unsigned int newlen )
{
if ( newlen == 0 || newlen == 1 ) {
if ( str )
delete[] str;
memlen = len = 0;
}
else if ( str )
{
if ( newlen < len ) { // truncate
char * _str = new char[ newlen ];
::strncpy( _str, str, newlen-1 );
_str[ newlen - 1 ] = '\0';
len = newlen - 1;
memlen = newlen;
delete[] str;
str = _str;
} else {
char * _str = new char[ newlen ];
memset( _str, 0, newlen );
::strncpy( _str, str, len );
memlen = newlen;
delete[] str;
str = _str;
}
}
else
{
str = new char[newlen];
memlen = newlen;
memset( str, 0, memlen );
}
}
// trims in place, returns self
basicString_t& basicString_t::trim()
{
if ( !str || !*str )
return *this;
char * p = str;
while ( *p != '\0' && (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') )
++p;
char * e = &str[len];
int set = 0;
do {
long long end = reinterpret_cast<long long>(e);
long long beg = reinterpret_cast<long long>(p);
if ( end <= beg )
break;
if ( *(e-1)==' '||*(e-1)=='\t'||*(e-1)=='\n'||*(e-1)=='\r')
--e, ++set;
else
break;
} while(1);
if ( set ) {
*e = '\0';
}
// if beginning of string didn't change we don't need to reallocate
if ( p == str )
return *this;
len = strlen(p);
memlen = len + 1;
char * str2 = new char[memlen];
strcpy( str2, p );
delete[] str;
str = str2;
return *this;
}
const char * basicString_t::strstr( const char * needle )
{
if ( !str || !*str || !needle || !*needle )
return 0;
return ::strstr( str, needle );
}
const char * basicString_t::strstr( const basicString_t& ref )
{
return this->strstr( ref.str );
}
// strcasestr is a gnu extension, so we write our own
const char * basicString_t::stristr( const char * arg )
{
if ( !str || !*str ) {
return 0;
}
else if ( !arg || !*arg )
return 0;
// length of fragment & haystack
const unsigned int alen = strlen( arg );
const unsigned int slen = strlen( str );
// starting char of fragment
int a_start = char_tolower(*arg);
// pointer to haystack
char * s = const_cast<char *>( str );
unsigned int i;
char * p = s;
while ( *s && s-str <= slen-alen )
{
if ( char_tolower(*s) != a_start )
++s;
else
{
p = s;
i = 0;
char * a = const_cast<char *>( arg );
do
{
if ( char_tolower(*p) == char_tolower(*a) )
++i;
if ( i == alen )
return s;
++p; ++a;
}
while( *p && *a );
if ( i == alen )
return s;
++s;
}
}
return 0;
}
// alias
const char * basicString_t::strcasestr( const char * arg )
{
return this->stristr( arg );
}
const char * basicString_t::stristr( const basicString_t& ref )
{
return this->stristr( ref.str );
}
const char * basicString_t::strcasestr( const basicString_t& ref )
{
return this->stristr( ref.str );
}
basicString_t basicString_t::operator+( const char * c_str )
{
basicString_t internal( this->str );
internal.append( c_str );
return internal; // temporary object
}
basicString_t basicString_t::operator+( const basicString_t & ref )
{
basicString_t internal( str );
internal.append( ref );
return internal; // temporary object
}
basicString_t basicString_t::substr( unsigned int start, unsigned int length )
{
if ( !str || !*str )
return basicString_t();
if ( start < len )
{
unsigned int maxlen_can_be_copied = len - start;
if ( length > maxlen_can_be_copied )
length = maxlen_can_be_copied;
basicString_t A;
return A.strncpy( &str[start], length );
}
return basicString_t();
}
basicArray_t * basicString_t::explode( const char * sep, int stripcom )
{
if ( !str || !*str || !length() )
return 0;
basicArray_t * A = new basicArray_t;
Tokenizer_t tokens( str, length() );
tokens.setStripComments( stripcom ); // defaults to off
tokens.tokenize( sep ); // can be null, if null deals in whitespace
Token_t * tok = tokens.getHead();
while ( tok ) {
A->push_back( *tok );
tok = tok->next;
}
return A;
}
// replaces inline, returning self
basicString_t& basicString_t::replace( const char * find, const char * replace )
{
// we allow replace to be null, signifying remove instances of find
if ( !find || !*find || !len )
return *this;
basicString_t res;
const char * p = str;
const char * s = str;
while ( *p && (p = ::strstr( p, find )) )
{
// copyin everything upto match
while ( s != p ) {