summaryrefslogtreecommitdiff
path: root/src/core/xbstring.cpp
blob: 89cefb67105e59cbaf14fe1e3fdc61d8fe25795e (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
/* xbstring.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2021,2022,2023 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

//#ifdef __GNU LesserG__
//  #pragma implementation "xbstring.h"
//#endif

#include "xbase.h"


namespace xb{

XBDLLEXPORT const char * xbString::NullString = "";
XBDLLEXPORT       char   xbString::cJunkBuf;

/************************************************************************/
//! @brief Destructor

xbString::~xbString(){
  if (data != NULL)
    free(data);

}

/************************************************************************/
//! @brief Constructor
/*!
    \param ulSize - Allocation size. The allocation size is normally handled internally
    by the class, but it can be set in this constructor.
*/
xbString::xbString(xbUInt32 ulSize) {
  data = (char *)calloc(1, ulSize);
  this->size = ulSize;
//  memset( data, 0x00, ulSize ); - redundant, initialized by calloc
}
/************************************************************************/
//! @brief Constructor
/*!
    \param c - Initialize string to c.
*/
xbString::xbString(char c) {  
  data = (char *)calloc(1, 2);
  data[0] = c;
  data[1] = 0;
  size = 2;
}
/************************************************************************/
//! @brief Constructor
/*!
    \param s - Initialize string to s.
*/
xbString::xbString( const char *s ) {

  if( s == NULL ){
    size = 0;
    data = NULL;
  } else {
    size = (xbUInt32) (strlen(s) + 1 );
    data = (char *) calloc( 1, size );
    xb_strcpy( data, s );
  }
  // ctor(s);
}
/************************************************************************/
//! @brief Constructor
/*!
    \param d - Initiailize string to d.
*/
xbString::xbString( xbDouble d ) {
  data = NULL;
  size = 0;
  Sprintf("%f", d);
}
/************************************************************************/
//! @brief Constructor
/*!
    \param s Initialize string to s.
    \param ulMaxLen Maximum length of string.  Truncate any characters greater than ulMaxLen.
*/
xbString::xbString( const char *s, xbUInt32 ulMaxLen ) {
  xbUInt32 sSize = (xbUInt32) strlen( s );
  if( sSize < ulMaxLen )
    size = sSize;
  else
    size = ulMaxLen;
  data = (char *)calloc(1, size+1);
  for( xbUInt32 i = 0; i < size; i++ )
    data[i] = s[i];
  data[size] = '\0';
  size++;    // account for null trailing byte
  return;
}
/************************************************************************/
//! @brief Constructor
/*!
    \param s Initialize string to s.
*/
xbString::xbString( const xbString &s ) {
  ctor(s.Str());
}

/************************************************************************/
//! @brief Operator const char *
/*!
    \returns Pointer to string data.
*/
xbString::operator const char *() const {
  return data ? data : NullString;
}

/************************************************************************/
//! @brief Set operator =
/*!
    \param s - Set the string to the string on the right of the equal sign.
*/
xbString &xbString::operator=( const xbString &s ) {
  return Set(s);
}
/************************************************************************/
//! @brief Set operator =
/*!
    \param s - Set the string to the string on the right of the equal sign.
*/
xbString &xbString::operator=( const char *s ) {
  return Set(s);
}

/************************************************************************/
//! @brief Stream insertion operator <<
/*!
    std::cout << MyString << std::endl;

    \param os Output stream
    \param s String to send to output stream
*/
std::ostream& operator<< ( std::ostream& os, const xbString & s ) {
  return os << s.Str();
}
/************************************************************************/
//! @brief Append operator +=
/*!
    \param s - Append s to the string.
*/
xbString &xbString::operator+=( const xbString &s ) {

  if (s.IsNull())
    return (*this);

  xbUInt32 Len = s.Len();
  xbUInt32 oldLen = this->Len();
  xbUInt32 newLen = Len + oldLen;

  data = (char *)realloc(data, newLen+1);
  if( !data )
    return (*this);

  if(oldLen == 0)
    data[0] = 0;

  char *t = data;
  t+= oldLen;
  for( xbUInt32 i = 0; i < Len; i++ )
    *t++ = s.GetCharacter(i+1);

  data[newLen] = '\0';
  size == 0 ? size += (Len + 1) : size += Len;

  return (*this);
}
/************************************************************************/
//! @brief Append operator +=
/*!
    \param s - Append s to the string.
*/
xbString &xbString::operator+=( const char *s ) {

  if (s == NULL)
    return (*this);
  xbUInt32 Len = (xbUInt32) strlen(s);
  xbUInt32 oldLen = this->Len();
  xbUInt32 newLen = Len + oldLen;
  data = (char *)realloc(data, newLen+1);
  if(oldLen == 0)
    data[0] = 0;
  for( xbUInt32 i = 0; i < Len; i++ )
    data[i+oldLen] = s[i];
  data[newLen] = '\0';
  // size += Len;
  size == 0 ? size+= (Len + 1) : size += Len;
  return (*this);
}
/************************************************************************/
//! @brief Append operator +=
/*!
    \param c - Append c to the string.
*/
xbString &xbString::operator+=( char c ) {
  xbUInt32 Len = 1;
  xbUInt32 oldLen = this->Len();
  data = (char *)realloc(data, oldLen+Len+1);
  data[oldLen] = c;
  data[oldLen+1] = 0;
  // size++;
  size == 0 ? size += 2 : size++;
  return (*this);
}
/************************************************************************/
//! @brief Append operator -=
/*!
    Append s to the right of this string, right trimming both strings.
    \param s - Append s to the right of the string value.
*/
xbString &xbString::operator-=( const xbString &s ) {

  Rtrim();
  if (s.IsNull())
    return (*this);
  xbUInt32 Len = s.Len();
  xbUInt32 oldLen = this->Len();
  xbUInt32 newLen = Len + oldLen;

  data = (char *)realloc(data, newLen+1);
  if(oldLen == 0)
    data[0] = 0;

  for( xbUInt32 i = 0; i < Len; i++ )
    data[i+oldLen] = s.GetCharacter(i+1);

  data[newLen] = '\0';
  //size += Len;
  size == 0 ? size += (Len+1) : size += Len;
  Rtrim();
  return (*this);
}
/************************************************************************/
//! @brief Append  operator -=
/*!
    Append s to the right of this string, right trimming both strings.
    \param s - Append s to the right of the string value.
*/
xbString &xbString::operator-=(const char *s) {

  Rtrim();
  if (s == NULL)
    return (*this);
  xbUInt32 Len = (xbUInt32) strlen(s);
  xbUInt32 oldLen = this->Len();
  xbUInt32 newLen = Len + oldLen;

  data = (char *)realloc(data, newLen+1);

  if(oldLen == 0)
    data[0] = 0;

  for( xbUInt32 i = 0; i < Len; i++ )
    data[i+oldLen] = s[i];
  data[newLen] = '\0';

  //size += Len;
  size == 0 ? size += (Len+1) : size += Len;

  Rtrim(); 
  return (*this);
}
/************************************************************************/
//! @brief Append operator -=
/*!
    Append c to the right of this string, trimming right space on this string first.
    \param c - Append s to the right of the string value.
*/
xbString &xbString::operator-=(const char c) {
  Rtrim();
  xbUInt32 oldSize = size;

  //  size += 1;
  size == 0 ? size += 2 : size += 1;

  data = (char *)realloc( data, size );
  if( oldSize == 0 ) data[0] = 0;
  data[size-2] = c;
  data[size-1] = 0;
  Trim(); 
  return (*this);
}
/************************************************************************/
//! @brief Concatonate operator -
/*!
    Concatonate left string with right string returning reference to new string.
    Both strings are trimmed.

    \param s1 Right string operator.
*/
xbString xbString::operator-(const xbString &s1) {
  xbString tmp( data );
  tmp -= s1;
  return tmp;
}
/************************************************************************/
//! @brief Concatonate operator +
/*!
    Concatonate left string with right string returning reference to new string.

    \param s1 Right string operator.
*/
xbString xbString::operator+( const char *s1) {
  xbString tmp( data );
  tmp += s1;
  return tmp;
}
/************************************************************************/
//! @brief Concatonate operator +
/*!
    Concatonate left string with right string returning reference to new string.

    \param s1 Right string operator.
*/
xbString xbString::operator+( const xbString &s1) {
  xbString tmp( data );
  tmp += s1;
  return tmp;
}
/************************************************************************/
//! @brief Concatonate operator +
/*!
    Concatonate left string with right string returning reference to new string.

    \param c Right string operator.
*/

xbString xbString::operator+( const char c) {
  xbString tmp( data );
  tmp += c;
  return tmp;
}
/************************************************************************/
//! @brief operator []
/*!
    \param n - Offset into the string of the byte to retrieve.
    \returns c - The character to return from the offset within the [] brackets.
*/
char &xbString::operator[]( xbUInt32 n ) const {
  if( n > 0 && n <= size )
    return data[n-1];
  else
    return cJunkBuf;
}
/************************************************************************/
//! @brief operator []
/*!
    \param n - Offset into the string of the byte to retrieve.
    \returns c - The character to return from the offset within the [] brackets.
*/
char &xbString::operator[]( xbInt32 n ) const {
  if( n > 0 && n <= (xbInt32) size )
    return data[n-1];
  else
    return cJunkBuf;
}
/************************************************************************/
//! @brief operator ==
/*!
    \param s String to compare 
    \returns xbTrue - Strings match.<br>
     zbFalse - Strings don't match.<br>
 
*/
xbBool xbString::operator==( const xbString &s ) const {

  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return true;
    return false;
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return false;
    return( strcmp(data,s.data) == 0 ? xbTrue : xbFalse );
  }
}
/************************************************************************/
//! @brief operator ==
/*!
    \param s String to compare 
    \returns xbTrue - Strings match.<br>
     zbFalse - Strings don't match.<br>
*/
xbBool xbString::operator==( const char *s ) const {

  if (s == NULL) {
    if ( data == NULL)
      return true;
    return false;
  }
  if ((s[0] == 0) && data == NULL)
      return true;
  if ( data == NULL)
    return false;
  return( strcmp( data, s) == 0 ? xbTrue : xbFalse );
}
/************************************************************************/
//! @brief operator !=
/*!
    \param s String to compare 
    \returns xbTrue - Strings don't match.<br>
             xbFalse - Strings match.<br>
*/
xbBool xbString::operator!=( const xbString &s ) const {
  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return xbFalse;                                           // NULL != NULL
    return xbTrue;                                              // NULL != !NULL
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return xbTrue;                                            // !NULL != NULL
    return( strcmp( data, s.data ) != 0 ? xbTrue : xbFalse );   // !NULL != !NULL
  }
}
/************************************************************************/
//! @brief operator !=
/*!
    \param s String to compare 
    \returns xbTrue - Strings don't match.<br>
         zbFalse - Strings match.<br>
*/
xbBool xbString::operator!=( const char *s ) const {
  if( s == NULL || s[0] == 0 ) {
    if( data == NULL || data[0] == 0 )
      return xbFalse;                                      // NULL != NULL
    return xbTrue;                                         // NULL != !NULL
  } else {
    if( s == NULL || s[0] == 0 )
      return xbTrue;                                       // !NULL != NULL
    return( strcmp( data, s ) != 0 ? xbTrue : xbFalse );   // !NULL != !NULL
  }
}
/************************************************************************/
//! @brief operator <
/*!
    \param s String to compare 
    \returns xbTrue - Left string is less than the right string.<br>
         zbFalse - Left string is not less than the right string.<br>
*/
xbBool xbString::operator< ( const xbString &s ) const {

  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return false;
    return true;
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return false;
    return ( strcmp(data, s.data) < 0 ? xbTrue : xbFalse );
  }
}
/************************************************************************/
//! @brief operator >
/*!
    \param s String to compare 
    \returns xbTrue - Left string is greater than the right string.<br>
         zbFalse - Left string is not greater than the right string.<br>
*/
xbBool xbString::operator> ( const xbString &s ) const {
  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return false;
    return false;
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return true;
    return( strcmp(data,s.data) > 0 ? xbTrue : xbFalse );
  }
}
/************************************************************************/
//! @brief operator <=
/*!
    \param s String to compare 
    \returns xbTrue - Left string is less than or equal to the right string.<br>
         zbFalse - Left string is not less than or equal to the right string.<br>
*/
xbBool xbString::operator<=( const xbString &s ) const {
  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return true;
    return true;
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return false;
    return( strcmp(data,s.data) <= 0 ? xbTrue : xbFalse );
  }
}
/************************************************************************/
//! @brief operator >=
/*!
    \param s String to compare 
    \returns xbTrue - Left string is greater than or equal to the right string.<br>
             zbFalse - Left string is not greater than or equal to the right string.<br>
*/
xbBool xbString::operator>=( const xbString &s ) const {
  if( data == NULL || data[0] == 0 ) {
    if( s.data == NULL || s.data[0] == 0 )
      return true;
    return false;
  } else {
    if( s.data == NULL || s.data[0] == 0 )
      return true;
    return( strcmp(data, s.data) >= 0 ? xbTrue : xbFalse );
  }
}

/************************************************************************/
//! @brief Add a prefixing back slash to specified characters in the string.
/*!
    \param c Character to prefix with a backslash.
    \returns Reference to this string.
*/
xbString &xbString::AddBackSlash( char c ) {

  xbUInt32 lCnt = CountChar( c );
  if( lCnt == 0 )
    return *this;
  char *p;
  if(( p = (char *)calloc( 1, size + lCnt )) == NULL )
    return *this;

  char *p2 = p;
  for( xbUInt32 lS = 0; lS < size; lS++ ){
    if( data[lS] == c )
      *p2++ = '\\';
    *p2++ = data[lS];
  }
  if( data )
    free( data );
  data = p;

  //  size += lCnt;
  size == 0 ? size += (lCnt+1) : size += lCnt;

  return *this;
}
/************************************************************************/
//! @brief Append data to string.
/*!
    \param s String data to append.
    \returns Reference to this string.
*/
xbString &xbString::Append( const xbString &s ) {
 *this += s;
  return *this;
}

/************************************************************************/
//! @brief Append data to string.
/*!
    \param s String data to append.
    \returns Reference to this string.
*/
xbString &xbString::Append( const char *s ) {
  *this += s;
  return *this;
}
/************************************************************************/
//! @brief Append data to string.
/*!
    \param c String data to append.
    \returns Reference to this string.
*/
xbString &xbString::Append( char c ) {
  *this += c;
  return *this;
}
/************************************************************************/
//! @brief Append data to string.
/*!
    \param s String data to append.  
    \param ulByteCount Maximum number of bytes to append.
    \returns Reference to this string.
*/
xbString &xbString::Append( const char *s, xbUInt32 ulByteCount ) {

  if ( s == NULL || !*s || ulByteCount == 0)
    return (*this);

  xbUInt32 ulOrigLen = this->Len();

  // s might not be null byte at the end, can't use strlen
  //  xbUInt32 ulAddLen = strlen( s );
  xbUInt32 ulAddLen = 0;
  const char *p = s;

  while( ulAddLen < ulByteCount && *p ){
    p++;
    ulAddLen++;
  }

  if( ulAddLen > ulByteCount )
    ulAddLen = ulByteCount;

  size = ulOrigLen + ulAddLen + 1;
  data = (char *) realloc( data, size );

  for( xbUInt32 i = 0; i < ulAddLen; i++ )
    data[i+ulOrigLen] = s[i];

  data[size-1] = 0x00;
  return (*this);
}

/************************************************************************/
//! @brief Assign portion of string.
/*!
    \param sStr - Source string for copy operation.  sStr needs to be a Null terminated string.
    \param ulStartPos - Starting position within source string.
    \param ulCopyLen - Length of data to copy.
    \returns Reference to this string.
*/
xbString &xbString::Assign(const char * sStr, xbUInt32 ulStartPos, xbUInt32 ulCopyLen){
  if(data){
    free(data);
    data = 0;
    size = 0;
  }
  xbUInt32 lLen = (xbUInt32) strlen( sStr );
  if( ulStartPos > lLen ){
    size = 0;
    return( *this );
  }
  if((( ulCopyLen - 1) + ulStartPos ) > lLen )
    ulCopyLen = lLen - ulStartPos + 1;
  data = (char *)calloc(1, ulCopyLen + 1);

  //size = ulCopyLen + 1;
  size == 0 ? size += (ulCopyLen+1) : size += ulCopyLen;

  for( xbUInt32 i = 0; i < ulCopyLen; i++ )
    data[i] = sStr[i + ulStartPos - ((xbUInt32) 1)];
  data[ulCopyLen] = '\0';
  return (*this);
}
/************************************************************************/
//! @brief Assign portion of string.
/*!
    \param sStr - Source string for copy operation.  sStr needs to be a Null terminated string.
    \param ulStartPos - Starting position within source string.
    \returns Reference to this string.
*/
xbString &xbString::Assign(const char * sStr, xbUInt32 ulStartPos){
  if(data){
    free(data);
    data = 0;
    size = 0;
  }
  xbUInt32 ulSrcLen = (xbUInt32) strlen( sStr );
  if( ulStartPos > ulSrcLen ){
    size = 0;
    return( *this );
  }
  xbUInt32 ulCopyLen;
  ulCopyLen = ulSrcLen - ulStartPos + 1;
  data = (char *)calloc(1, ulCopyLen + 1);

  size = ulCopyLen + 1;

  for( xbUInt32 i = 0; i < ulCopyLen; i++ )
    data[i] = sStr[i + ulStartPos - ((xbUInt32) 1)];
  data[ulCopyLen] = '\0';
  return (*this);
}

/************************************************************************/
//! @brief Assign portion of string.
/*!
    \param sStr - Source string for copy operation.  sStr needs to be a Null terminated string.
    \param ulStartPos - Starting position within source string.
    \param ulCopyLen - Length of data to copy.
    \returns Reference to this string.
*/
xbString &xbString::Assign(const xbString& sStr, xbUInt32 ulStartPos, xbUInt32 ulCopyLen){
 if(data){
    free(data);
    data = 0;
    size = 0;
  }
  xbUInt32 ulSrcLen = sStr.Len();
  if( ulStartPos > ulSrcLen ){
    size = 0;
    return( *this );
  }
  if((( ulCopyLen - 1) + ulStartPos ) > ulSrcLen )
    ulCopyLen = ulSrcLen - ulStartPos + 1;  
  data = (char *)calloc(1, ulCopyLen + 1);
  size = ulCopyLen + 1;
  for( xbUInt32 i = 0; i < ulCopyLen; i++ )
    data[i] = sStr[i + ulStartPos];
  data[ulCopyLen] = '\0';
  return (*this);
}
/************************************************************************/
//! @brief Assign portion of string.
/*!
    \param sStr - Source string for copy operation.  sStr needs to be a Null terminated string.
    \param ulStartPos - Starting position within source string.
    \returns Reference to this string.
*/
xbString &xbString::Assign(const xbString& sStr, xbUInt32 ulStartPos){
  if(data){
    free(data);
    data = 0;
    size = 0;
  }
  xbUInt32 ulSrcLen = sStr.Len();
  if( ulStartPos > ulSrcLen ){
    size = 0;
    return( *this );
  }
  xbUInt32 ulCopyLen;
  ulCopyLen = ulSrcLen - ulStartPos + 1;
  data = (char *)calloc(1, ulCopyLen + 1);
  size = ulCopyLen;
  for( xbUInt32 i = 0; i < ulCopyLen; i++ )
    data[i] = sStr[i + ulStartPos];
  data[ulCopyLen] = '\0';
  size++;
  return (*this);
}
/************************************************************************/
//! @brief Copy a string
/*!
    \returns xbString.
*/
xbString xbString::Copy() const {
  return( *this );
}
/************************************************************************/
//! @brief Count the number of characters in the string.
/*!
    \param c Character to count.
    \param iOpt 0 - Count the number of characters.<br>
                1 - Count the number of characters not between single or double quotes.
    \returns The number of characters.
*/
xbUInt32 xbString::CountChar( char c, xbInt16 iOpt ) const {
  if( iOpt == 0 )
    return CountChar( c );
  else{
    xbBool bSingleQuote = xbFalse;
    xbBool bDoubleQuote = xbFalse;
    char cPrevChar = 0x00;
    xbUInt32 i,j;
    for( i = 0, j = 0; i < size; i++ ){
      if( bSingleQuote && data[i] == '\'' && cPrevChar != '\\' ){
        bSingleQuote = xbFalse;
      }
      else if( bDoubleQuote && data[i] == '"' && cPrevChar != '\\' ){
        bDoubleQuote = xbFalse;
      }
      else if( data[i] == '\'' && cPrevChar != '\\' && !bDoubleQuote ){
        bSingleQuote = xbTrue;
      }
      else if( data[i] == '"' && cPrevChar != '\\' && !bSingleQuote ){
        bDoubleQuote = xbTrue;
      }
      else if( !bDoubleQuote && !bSingleQuote && data[i] == c ){
        j++;
      }
      cPrevChar = data[i];
    }
    return j;
  }
}
/************************************************************************/
//! @brief Count the number of characters in the string.
/*!
    \param c Character to count.
    \returns The number of characters.
*/
xbUInt32 xbString::CountChar( char c ) const {
  xbUInt32 i,j;
  for( i = 0,j = 0; i < size; i++ )
    if( data[i] == c )
      j++;
  return j;
}
/************************************************************************/
void xbString::ctor( const char *s ) {

  // this routine assumes it was called by one of the constructors.

  if (s == NULL) {
    data = NULL;
    size = 0;
    return;
  }

  size = (xbUInt32) (strlen(s) + 1);
  data = (char *) calloc( 1, size);

  xb_strcpy(data, s);
}
/************************************************************************/
//! @brief Convert hex character to string. 
/*!
    This routine converts a four byte string in the format of 0x00 to a one byte char value.
    The first four bytes of the string must be in the format 0x00.
    Anything past the first four bytes is disregarded.

    \param cOut Output character.
    \returns  XB_INVALID_PARM on error<br>
              XB_NO_ERROR on success.
*/
xbInt16 xbString::CvtHexChar( char &cOut ){

  int  j, k;
  char c;

  if( Len() < 4 || data[0] != '0' || (data[1]!='X' && data[1]!='x' ))
    return XB_INVALID_PARM;

  c = (char) toupper( data[2] );
  j = ( c > '9' ? c - 'A' + 10 : c - '0' );
  c = (char)toupper( data[3] );
  k = ( c > '9' ? c - 'A' + 10 : c - '0' );
  j = ( j << 4 ) + k;

  cOut = ( char ) j;
  return XB_NO_ERROR;
}
/************************************************************************/
//! @brief Convert string of hex characters to string. 
/*!

   This routine converts a string of four byte format of 0x00 to a string of one byte chars.

    \param sOut Output string of converted characters.
    \returns  XB_INVALID_PARM on error<br>
              XB_NO_ERROR on success.
*/
xbInt16 xbString::CvtHexString( xbString &sOut ){
  char c;
  xbString ws;
  ws = data;
  sOut = "";
  xbInt16 iRc;
  while( ws.Len()){
    if(( iRc= ws.CvtHexChar( c )) != XB_NO_ERROR )
      return iRc;
    sOut += c;
    ws.Ltrunc( 4 );
  }
  return XB_NO_ERROR;
}
/************************************************************************/
//! @brief Convert string to xbUInt64 number
/*!
    \param ullOut - output unsigned long long.
    \returns XB_NO_ERROR
*/
xbInt16 xbString::CvtULongLong( xbUInt64 &ullOut ){

  // version 1 - fast, but no data checking
  ullOut = 0;
  char *s = data;
  int i = 0;
  while( *s ){
    ullOut *= 10;
    ullOut += (xbUInt64) *s - '0';
    s++;
    i++;
  }
  return XB_NO_ERROR;
}
/************************************************************************/
//! @brief Convert string to xbInt64 number
/*!
    \param llOut - output long long.
    \returns XB_NO_ERROR
*/
xbInt16 xbString::CvtLongLong( xbInt64 &llOut ){

  // version 1 - fast, but no data checking
  llOut = 0;
  char *s = data;
  int i = 0;
  while( *s ){
    llOut *= 10;
    llOut += (xbInt64) *s - '0';
    s++;
    i++;
  }
  return XB_NO_ERROR;
}

/************************************************************************/
#ifdef XB_DEBUG_SUPPORT
void xbString::Dump( const char * title, xbInt16 iHexOption ) const {
  fprintf(stdout, "%s StringSize[%d] DataLen=[%d] data=[%s]\n", title, size, Len(), data );
  if( iHexOption ){
    std::cout << "Hex values" << std::endl;
    for( xbUInt32 i = 0; i < strlen( data ); i++ )
      printf( " %x", data[i] );
    std::cout << std::endl;
  }
}
void xbString::Dump( const char * title ) const {
  Dump( title, 0 );
}

void xbString::DumpHex( const char * title ) const {
  Dump( title, 1 );
}
#endif

/************************************************************************/
//! @brief Extract an element out of a delimited string.
/*!
    \param sSrc Source string.
    \param cDelim Delimiter.
    \param lSkipCnt Number of delimiters to skip.
    \param iOpt 0 - ignore single and double quotes.<br>
                1 - ignore delimiters between single or double quotes.
    \returns Reference to string extracted from element.
*/
xbString &xbString::ExtractElement( xbString &sSrc, char cDelim, xbUInt32 lSkipCnt, xbInt16 iOpt )
{
  return ExtractElement( sSrc.Str(), cDelim, lSkipCnt, iOpt );
}

/************************************************************************/
//! @brief Extract an element out of a delimited string.
/*!
    \param pSrc Source string.
    \param cDelim Delimiter.
    \param lSkipCnt Number of delimiters to skip.
    \param iOpt 0 - ignore single and double quotes.<br>
                1 - ignore delimiters between single or double quotes.
    \returns Reference to string extracted from element.
*/
xbString &xbString::ExtractElement( const char *pSrc, char cDelim, xbUInt32 lSkipCnt, xbInt16 iOpt )
{
  /* opt values
     0 - ignore single and double quotes
     1 - ignore delimiters between single or double quotes
  */

  xbUInt32 lLen;
  xbUInt32 lCurCnt = 0;
  xbBool bInSingleQuotes = xbFalse;
  xbBool bInDoubleQuotes = xbFalse;
  char cPrevChar = 0x00;
  const char *s = pSrc;
  const char *pAnchor;

  /* skip past skipcnt delimiters */
  while( *s && lCurCnt < (lSkipCnt-1) ){
    if( iOpt == 0 ){
      if( *s == cDelim ) 
        lCurCnt++;
    } else {
      if( *s == cDelim && !bInSingleQuotes && !bInDoubleQuotes ){
        lCurCnt++;
      } else if( *s == '\'' && !bInDoubleQuotes && cPrevChar != '\\' ){
         if( bInSingleQuotes == xbTrue ) 
           bInSingleQuotes = xbFalse; 
         else 
           bInSingleQuotes = xbTrue;
      } else if( *s == '"' && !bInSingleQuotes && cPrevChar != '\\' ){
         if( bInDoubleQuotes == xbTrue ) 
           bInDoubleQuotes = xbFalse; 
         else 
           bInDoubleQuotes = xbTrue;
      }
    }
    cPrevChar = *s;
    s++;
  }

  /* at the beginning of the field */
  pAnchor = s;
  xbBool bDone = xbFalse;
  while( *s && !bDone ){
    if( iOpt == 0 ){
      if( *s == cDelim )
        bDone = xbTrue;
    } else {
      if( *s == cDelim && !bInSingleQuotes && !bInDoubleQuotes ){
        bDone = xbTrue;
      } else if( *s == '\'' && !bInDoubleQuotes && cPrevChar != '\\' ){
        if( bInSingleQuotes == xbTrue ) 
          bInSingleQuotes = xbFalse;
        else 
          bInSingleQuotes = xbTrue;
      } else if( *s == '"' && !bInSingleQuotes && cPrevChar != '\\' ){
        if( bInDoubleQuotes == xbTrue ) 
          bInDoubleQuotes = xbFalse; 
        else 
          bInDoubleQuotes = xbTrue;
      }
    }
    cPrevChar = *s;
    s++;
  }

  // if at end of string, go back one and drop the delimiter
  if( *s ) s--;

  lLen = (xbUInt32)(s - pAnchor);

  /* copy data */
  data = (char *) realloc( data, lLen+1 );
  memcpy( data, pAnchor, lLen );
  data[lLen] = 0;
  this->size = lLen+1;
  return *this;
}

/************************************************************************/
//! @brief Get a character by position 
/*!
    \param n - Position in string to extract. First position is 1 (not 0).
    \returns Character from position n, or null.
*/
char xbString::GetCharacter( xbUInt32 n ) const {
  if( n > 0 && n <= size )
    return data[n-1];
  else
    return 0x00;
}
/************************************************************************/
//! @brief Get the position of the last occurrence of a given character. 
/*!
    \param c - Character to search for.
    \returns Last position of character in the string.
*/
xbUInt32 xbString::GetLastPos(char c) const {

  if (data == NULL)
    return 0;

  char *p = data;
  xbUInt32 iPos = 0;
  xbUInt32 hPos = 0;
  while( *p && iPos++ < ( size - 1 )){
    if( *p == c )
      hPos = iPos;
    p++;
  }
  if( hPos )
    return hPos;
  else
    return 0;
}
/************************************************************************/
//! @brief Get the position of the last occurrence of a given string. 
/*!
    \param s - String to search for.
    \returns Last position of character in the string.
*/
xbUInt32 xbString::GetLastPos(const char* s) const{

  if (data == NULL)
    return 0;

  char *p     = data;
  char *saveP = NULL;
  while( p ){
    p = strstr( p, s);
    if( p ){
      saveP = p;
      p++;
    }
  }
  if (saveP == NULL)
    return 0;
  return (xbUInt32)(saveP - data) + 1;
}
/************************************************************************/
//! @brief Get the path separator out of the string.
/*!
    This method assumes the string is a valid path name.
    If it is, it returns either / or \.
    \returns Char value containing either / or \ depending on OS.
*/
char xbString::GetPathSeparator() const {

  if (data == NULL)
    return 0x00;
  char *p = data;
  while( *p ){
    if( *p == '\\' || *p == '/' )
      return *p;
    else
      p++;
  }
  return 0x00;
}

/************************************************************************/
//! @brief Retrieve the size of the string buffer.
/*!
    \returns Size of string buffer including the null terminating byte.
*/
xbUInt32 xbString::GetSize() const {
  return size;
}

/************************************************************************/
//! @brief Determine if the string has any alpha characters
/*!
    \returns xbTrue - String contains one or more aloha characters.<br>
             xbFalse - String contains no alpha characters.
*/
xbBool xbString::HasAlphaChars() const {
  for( xbUInt32 i = 0; i < size; i++ )
    if( isalpha( data[i] ))
      return xbTrue;
  return xbFalse;
}


/************************************************************************/
//! @brief Determine if string is empty
/*!
    \returns xbTrue if string is empty.<br>
             xbFalse if string is not empty.
*/
xbBool xbString::IsEmpty() const {
  if( data == NULL )
    return true;
  if( data[0] == 0 )
    return xbTrue;
  return xbFalse;
}

/************************************************************************/
//! @brief Determine if string is NULL
/*!
    \returns xbTrue if string is NULL.<br>
             xbFalse if string is not NULL.
*/
xbBool xbString::IsNull() const {
  return( data == NULL );
}


/************************************************************************/
//! @brief Retain left part of string, drop rightmost characters.
/*!
    \param ulLen New string length, truncate rightmost excess.
    \returns Reference to string.
*/
xbString &xbString::Left( xbUInt32 ulLen ) {
  return Mid( 1, ulLen );
}

/************************************************************************/
//! @brief Retrieve length of current string.
/*!
    \returns String length, excluding the terminating null byte.
*/
// return length of string
xbUInt32 xbString::Len() const {
  return( data ? (xbUInt32) strlen(data) : 0 );
}

/************************************************************************/
//! @brief Left trim white space from string. 
/*!
    \returns Reference to this string.
*/
xbString &xbString::Ltrim(){

  if( !data )
    return *this;

  char *p = data;
  if( !*p || (*p && *p != ' ') )
    return *this;   /* nothing to do */

  xbUInt32 s = 0;
  while( *p && *p == ' ' ){
    p++;
    s++;
    size--;
  }

  xbUInt32 i;
  for( i = 0; i < size; i++ )
    data[i] = data[i+s];
  data[i] = 0x00;
  data = (char *) realloc( data, size );

  return *this;

}

/************************************************************************/
//! @brief Left truncate string 
/*!
    \param ulCnt Number of bytes to remove from the left.
    \returns Reference to this string.
*/
xbString &xbString::Ltrunc( xbUInt32 ulCnt ){
  if( ulCnt >= size ){
    if( size > 0 ){
      free( data );
      data = NULL;
      size = 0;
    }
    return *this;
  }

  char * ndata;
  char * p;
  ndata = (char *) calloc( 1, size - ulCnt );
  p = data;
  p += ulCnt;
  xb_strcpy( ndata, p );
  free( data );
  data = ndata;
  size = size - ulCnt;
  return *this;
}

/************************************************************************/
//! @brief Extract portion of data from string
/*!
    \param ulStartPos Starting position
    \param ulTargLen Length 
    \returns Reference to string
*/
xbString &xbString::Mid( xbUInt32 ulStartPos, xbUInt32 ulTargLen ){

  // this is a 1 based routine
  if( ulStartPos == 0 )
    return *this;

  if( data == NULL )
    return( *this );
  if( data[0] == 0 )
    return( *this );
  if( ulStartPos > Len() )
    return( *this );
/*
  // Resize( ulTargLen + 1 );
  char *pTarg = data;
  char *pSrc = data + ulStartPos - 1;
  for( xbUInt32 l = 0; l < ulTargLen; l++ )
    *pTarg++ = *pSrc++;
  *pTarg = 0x00;
  // Resize( ulTargLen + 1 );
  */

  char * newData = (char *) calloc( 1, ulTargLen + 1 );
  char *pTarg = newData;
  char *pSrc = data + ulStartPos - 1;
  for( xbUInt32 l = 0; l < ulTargLen; l++ )
    *pTarg++ = *pSrc++;
  *pTarg = 0x00;

  free( data );
  data = newData;
  size = ulTargLen + 1;

  return *this;
}

/************************************************************************/
//! @brief Left pad string 
/*!
    \param c Padding character.
    \param ulNewLen New string length.
    \returns Reference to this string.
*/
xbString &xbString::PadLeft( char c, xbUInt32 ulNewLen ){

  xbUInt32 srcLen;
  if( data )
    srcLen = (xbUInt32) strlen( data );
  else
    srcLen = 0;

  if( srcLen >= ulNewLen )
    return *this;

  char * newData = (char *) calloc( 1, ulNewLen + 1 );
  xbUInt32 i;
  for( i = 0; i < ulNewLen - srcLen; i++ )
    newData[i] = c;

  char *targ = &newData[i];
  xb_strcpy( targ, data );
  free( data );
  data = newData;
  size = ulNewLen + 1;
  return *this;
}

/************************************************************************/
//! @brief Right pad string
/*!
    \param c Padding character.
    \param ulNewLen New string length.
    \returns Reference to this string.
*/
xbString &xbString::PadRight( char c, xbUInt32 ulNewLen ){
  xbUInt32 srcLen = (xbUInt32) strlen( data );
  if( srcLen >= ulNewLen )
    return *this;
  data = (char *) realloc( data, ulNewLen + 1 );
  xbUInt32 i;
  for( i = srcLen; i < ulNewLen; i++ )
    data[i] = c;
  data[i] = 0x00;
  size = ulNewLen + 1;
  return *this;
}

/************************************************************************/
//! @brief Determine position of a given character 
/*!
    \param c Seek character 
    \param ulStartPos starting position for search, first position is 1
    \returns Position within string. Returns 0 if not found.
*/
xbUInt32 xbString::Pos(char c, xbUInt32 ulStartPos ) const {

  if (data == NULL)
    return 0;
  char *p = data;

  if( ulStartPos >= size )
    return 0;

  xbUInt32 iPos = 0;
  while( (iPos+1) < ulStartPos ){
    p++;
    iPos++;
  }
  xbBool bFound = 0;
  while( *p && !bFound && iPos < ( size - 1 )){
    if( *p == c )
      bFound = 1;
    else {
      iPos++;
      p++;
    }
  }

  if( bFound )
    return iPos + 1;
  else
    return 0;
}

/************************************************************************/
//! @brief Determine position of a given character 
/*!
    \param c Seek character 
    \returns Position within string. Returns 0 if not found.
*/
xbUInt32 xbString::Pos(char c) const {

  if (data == NULL)
    return 0;
  char *p = data;
  xbUInt32 iPos = 0;
  int  iFound = 0;
  while( *p && !iFound && iPos < ( size - 1 )){
    if( *p == c )
      iFound = 1;
    else {
      iPos++;
      p++;
    }
  }
  if( iFound )
    return iPos + 1;
  else
    return 0;
}

/************************************************************************/
//! @brief Determine position of a given substring
/*!
    \param s Substring
    \returns Position within string. Returns 0 if not found.
*/
xbUInt32 xbString::Pos(const char* s) const{

  if (data == NULL)
    return 0;

  char *p = strstr(data, s);
  if ( p == NULL)
    return 0;

  return (xbUInt32)(p - data + 1);
}

/************************************************************************/
//! @brief Insert character into string
/*!
    \param ulPos Insertion position.
    \param c Character to insert.
    \returns Reference to this string.
*/
xbString &xbString::PutAt(xbUInt32 ulPos, char c){
   if((ulPos-1) > Len() )
      return *this;
   data[ulPos-1] = c;
   return *this;
}

/************************************************************************/
//! @brief Remove portion of string.
/*!
    \param ulStartPos Starting position for removal operation.
    \param ulDelSize Size of deletion.
    \returns Reference to string.
*/
xbString &xbString::Remove(xbUInt32 ulStartPos, xbUInt32 ulDelSize ) {
  if( data == NULL )
    return( *this );
  if( data[0] == 0 )
    return( *this );
  xbUInt32 srcLen = Len();

  if( ulStartPos > srcLen || ulStartPos < 1 || ulDelSize < 1 )
    return( *this );

  if(( ulStartPos + ulDelSize - 1) >= size ){
    data[ulStartPos-1] = 0x00;
    size = ulStartPos;
    return( *this );
  }

  char *t;
  char *s;
  t = data + (ulStartPos - 1);
  s = t + ulDelSize;
  size -= ulDelSize;
  while( *s )
    *t++ = *s++;
  *t = 0x00;
  return( *this );
}

/************************************************************************/
//! @brief Replace a value within a string with another value
/*!
    \param sReplace - Character string to replace.
    \param sReplaceWith - Character string to replace with
    \param iOption - 0 = All occurrences, 1 = first occurrence
    \returns Reference to this string.
*/

//the new size includes the null termination byte
xbString &xbString::Replace( const char *sReplace, const char *sReplaceWith, xbInt16 iOption ){

  xbBool bDone = xbFalse;
  xbUInt32 ulPos;
  xbUInt32 ulNewLen;
  xbUInt32 ulReplaceWithLen;
  xbUInt32 ulRsLen;    // size of right side of string after replaced data
  xbUInt32 ulSp2;
  char *sBuf2;

  const char *s;        // source ptr
  char *t;              // target ptr

  while( !bDone ){
    ulPos = Pos( sReplace );
    if( ulPos == 0 ){
      bDone = xbTrue;
    } else {

      ulReplaceWithLen = (xbUInt32) strlen( sReplaceWith );
      ulNewLen = this->size + ulReplaceWithLen - (xbUInt32) strlen( sReplace );
      sBuf2 = (char *) calloc( 1, ulNewLen );

      // copy part1
      t = sBuf2;
      s = data;
      for( xbUInt32 ul = 0; ul < ulPos-1; ul++ )
        *t++ = *s++;

      // copy part2
      s = sReplaceWith;
      for( xbUInt32 ul = 0; ul < ulReplaceWithLen; ul++ )
        *t++ = *s++;

      // copy part3
      ulSp2 = ulPos + (xbUInt32) strlen( sReplace );
      s = data;
      s+= (ulSp2 - 1);
      ulRsLen = (xbUInt32) strlen( s );
      for( xbUInt32 ul = 0; ul < ulRsLen; ul++ )
        *t++ = *s++;

      if( iOption )
        bDone = xbTrue;

      free(data);
      data = sBuf2;
      size = ulNewLen;
    }
  }
  return *this;
}

/************************************************************************/
//! @brief Resize a string
/*!
    \param ulSize - New string size, including null termination byte.
    \returns Reference to this string.
*/
//the new size includes the null termination byte
xbString &xbString::Resize(xbUInt32 ulSize) {

//  data = (char *) realloc( data, ulSize );


// original
  data = (char *) realloc( data, ulSize );

  if( ulSize > 0 )
    data[ulSize-1] = 0;
  this->size = ulSize;
  return *this;
}
/************************************************************************/
//! @brief Right trim the string.
/*!
    This routine removes any trailing white space on the string.

    \returns Reference to string.
*/
xbString &xbString::Rtrim(){

  xbUInt32 l = Len();
  if( l == 0 )
    return *this;

  xbUInt32 ulOrigSize = size;
  l--;

  for(;;) {
    if( data[l] != ' ' )
      break;
    data[l] = 0;
    size--;
    if( l == 0 )
      break;
    l--;
  }

  if( ulOrigSize != size )
    data = (char * ) realloc( data, size );
  return *this;
}

/************************************************************************/
//! @brief Set the value of the string.
/*!

   Note:  This routine fails if you try to set the string to itself or some part of itself.

    \param s Value to set the string.
    \returns Reference to string.

*/
xbString &xbString::Set( const char *s ) {

  if(data != NULL){
    free(data);
    data = NULL;
  }
  if( s == NULL || !*s ) {
    if( data )
      free( data );
    data = NULL;
    size = 0;
  } else {
    //data = (char *)calloc(1, strlen(s) + 1 );
    data = (char *) realloc( data, strlen(s) + 1 );
    xb_strcpy(data, s);
    size = (xbUInt32) (strlen(data) + 1);
  }
  return (*this);
}
/************************************************************************/
//! @brief Set the value of the string.
/*!
    \param s Value to set the string.
    \returns Reference to string.
*/
xbString &xbString::Set( const xbString &s ) {

//  if( s.Str() == NULL || s.Len() == 0 ){

  if( s.Str() == NULL ){
    if( data ) free( data );
    data = NULL;
    size = 0;
  } else {
    xbUInt32 ulLen = s.Len();
    char *p = (char *) calloc( 1, ulLen + 1 );
    xb_strcpy( p, s.Str());
    size = ulLen + 1;
    if( data ) free( data );
    data = p;
  }
  return (*this );
}

/************************************************************************/
//! @brief Set the value of the string.
/*!

   Note:  This routine fails if you try to set the string to itself or some part of itself.

    \param s Value to set the string.
    \param ulSize Maximum size of resultant string.
    \returns Reference to string.
*/

xbString &xbString::Set(const char *s, xbUInt32 ulSize) {

  if( data != NULL )
    free( data );

  if(s == NULL) {
    data = NULL;
    size = 0;
    return (*this);
  }

  data = (char *) calloc( 1, ulSize+1 );
  char *pTarget = data;
  for( xbUInt32 i = 0; i < ulSize; i++ ){
    *pTarget = *s;
    pTarget++;
    s++;
  }
  this->size = ulSize + 1;
  return *this;
}


/************************************************************************/
//! @brief Set the string to long integer numeric value. 
/*!
    \param lNum Value to set the string
    \returns Reference to this string.
*/
xbString &xbString::SetNum(xbInt32 lNum) {
  Sprintf("%ld", lNum);
  return *this;
}

/************************************************************************/
//! @brief Printf routine for formatting a string.
/*!
    See documentation on the standard C printf function for how to use this.

    MyString.Sprintf( "a number %d  some text %s", 100, "test text data" );

    \param sFormat A format specifier
    \returns Reference to a formatted string
*/
xbString &xbString::Sprintf( const char *sFormat, ...) {

  xbInt32 iRc;
  va_list ap;
  char *t;

#ifdef HAVE__VSNPRINTF_S_F

  va_start( ap, sFormat );
  size = (xbUInt32) _vsnprintf_s( NULL, 0, sFormat, ap ) + 1;
  va_end( ap );

  t = (char *) malloc( size );
  if( !t ){
    size = 0;
    return( *this );
  }

  va_start( ap, sFormat );
  iRc  = _vsnprintf_s( t, size, sFormat, ap );
  va_end( ap );

#else
#ifdef HAVE_VSPRINTF_S_F

  va_start( ap, sFormat );
  // size = (xbUInt32) vsprintf_s( NULL, 0, sFormat, ap ) + 1;
  size = (xbUInt32) _vscprintf( sFormat, ap ) + 1;
  va_end( ap );

  t = (char *) malloc( size );
  if( !t ){
    size = 0;
    return( *this );
  }

  va_start( ap, sFormat );
  iRc  = vsprintf_s( t, size, sFormat, ap );
  va_end( ap );

#else
#ifdef HAVE_VSNPRINTF_F

  va_start( ap, sFormat );
  size = (xbUInt32) vsnprintf( NULL, 0, sFormat, ap) + 1;
  va_end( ap );

  t = (char *) calloc( 1, size );
  if( !t ){
    size = 0;
    return( *this );
  }
  va_start( ap, sFormat );
  iRc  = vsnprintf( t, size, sFormat, ap );
  va_end( ap );

#  else
#      error "Fatal error building [xbstring.cpp] - You have neither _vsnprintf_s nor vsnprintf_s."
#  endif
#endif
#endif

  if( iRc < 0 ){
    if( data )
      free( data );
    data = NULL;
    size = 0;
  } else {
    if( data )
      free( data );
    data = t;
  }
  return( *this );
}

/************************************************************************/
//! @brief Return string data
/*!
    \returns char * to string data or NULL if string is empty
*/
const char *xbString::Str() const {
  return data ? data : NullString;
}

/************************************************************************/
//! @brief Copy all or part of string to character array
/*!
    \param cDest pointer to destination buffer.
    \param n Number of bytest to copy.   It is the responsibility of the application
      to verify the buffer is large enough to hold the string contents.
    \returns char * to result

*/
char *xbString::strncpy( char * cDest, xbUInt32 n ) const {
  xbUInt32 i;
  xbUInt32 ulLen;
  n > (size-1) ? ulLen = size-1 : ulLen = n;
  memset( cDest, 0x00, ulLen );
  for( i = 0; i < ulLen; i++ )
    cDest[i] = data[i];
//  cDest[i] = 0x00;
  return cDest;
}
/************************************************************************/
//! @brief Swap characters
/*!
    \param cFrom character to replace.
    \param cTo character to replace with.
    \returns Reference to this string.
*/
xbString &xbString::SwapChars( char cFrom, char cTo ){
  xbUInt32 i;
  for( i = 0; i < size; i++ )
    if( data[i] == cFrom )
       data[i] = cTo;
  return *this;
}


/************************************************************************/
//! @brief Replace all upper case charaters with lower case characters
/*!
    \returns Reference to this string.
*/
xbString &xbString::ToLowerCase(){
  xbUInt32 Len = this->Len();
  for (xbUInt32 i=0; i<Len; i++)
    data[i] = (char)tolower(data[i]);
  return *this;
}

/************************************************************************/
//! @brief Replace all lower case charaters with lower case characters
/*!
    \returns Reference to this string.
*/
xbString &xbString::ToUpperCase(){
  xbUInt32 Len = this->Len();
  for (xbUInt32 i=0;i<Len;i++)
    data[i] = (char)toupper(data[i]);
  return *this;
}
/************************************************************************/
//! @brief Trim all leading and trailing white space from string.
/*!
    \returns Reference to string.
*/
xbString &xbString::Trim(){
  Rtrim();
  Ltrim();
  return *this;
}

/************************************************************************/
//! @brief Private function used for reallocateing memory
/*!
    This function is designed to be a drop in replacement for the realloc 
    function call.
*/
/*
char * xbString::xb_realloc( char * pIn, xbUInt32 iLen ){

  if( iLen == 0 ){
    if( pIn ){
      free( pIn );
      return NULL;
    }
  }

  char *pNew = (char *) calloc( 1, (size_t) iLen );
  if( !pNew ) return NULL;
  char *s = pIn;
  char *t = pNew;
  xbUInt32 iCnt = 0;
  while( *s && iCnt++ < iLen )
    *t++ = *s++;
  return pNew;
}
*/
/************************************************************************/
//! @brief Private function used for copying a string
/*!
    For performance reasons, this is an internal function that does no 
    memory checking and assumes a valid buffer area is available to be copied.

    This function is marked as private because of the above reason and
    is used by "stronger" calling functions.

    \param sTarget Target destination of copied string
    \param sSource Source string to copy
    \returns Reference to string.
*/

char * xbString::xb_strcpy( char *sTarget, const char *sSource ){

  char *temp = sTarget;
  while( *sSource != '\0')
    *sTarget++ = *sSource++;
  *sTarget= '\0';
  return temp;
}

/************************************************************************/
//! @brief Check for valid logical field data
/*!
    Valid logical data is one 'T', 'F', 'N' or 'Y'.<br>

    \returns xbTrue if the data is valid logical data.<br>
             xbFalse if not valid logical data.
*/

xbBool xbString::ValidLogicalValue() const {
  if( Len() == 1 )
    if( data[0] == 'T' || data[0] == 'F' || data[0] == 'Y' || data[0] == 'N' || data[0] == '?' )
      return xbTrue;
  return xbFalse;
}
/************************************************************************/
//!  @brief This function returns true if the data is valid numeric data
/*!
    \returns xbTrue if valid numeric data.<br>
            xbFalse if not valid numeric daata.
*/

xbBool xbString::ValidNumericValue() const {
  const char *p;
  p = data;
  while( *p ){
    if( *p != '+' && *p != '-' && *p != '.' && *p != '0' && *p != '1' &&
        *p != '2' && *p != '3' && *p != '4' && *p != '5' && *p != '6' &&
        *p != '7' && *p != '8' && *p != '9' )
      return xbFalse;
    else
      p++;
  }
  return xbTrue;
}


/************************************************************************/
//!  @brief Remove every instance of a character from a string.
/*!
    \param c character to remove from string.
    \returns Reference to this stirng.void
*/

xbString &xbString::ZapChar( char c ){
 
  if( data == NULL )
    return *this;
  if( data[0] == 0 )
    return *this;

  char *s;
  char *t;

  s = data;
  t = data;
  while( *s ){
    if( *s == c ){
      s++;
      size--;
    } else {
      *t++ = *s++;
    }
 }
 *t = 0x00; 

 data = (char *) realloc( data, size );
 return *this;
}

/************************************************************************/
//!  @brief Remove leading character from a string.
/*!
    \param c character to remove from beginning of string.
    \returns Reference to this string.
*/
xbString &xbString::ZapLeadingChar( char c ){
  /* left truncate all of character c */
  xbUInt32 iLen = 0;
  char *p;
  p = data;
  while( *p && *p == c ){
    iLen++;
    p++;
  }
  if( iLen )
    Ltrunc( iLen );
  return *this;
}

/************************************************************************/
//!  @brief Remove trailing character from a string.
/*!
    \param c character to remove from ending of string.
    \returns Reference to this string.
*/
xbString &xbString::ZapTrailingChar( char c ){

  xbUInt32 l = Len();
  if( l == 0 )
    return *this;
  xbUInt32 ulOrigSize = size;
  l--;
  for(;;) {
    if( data[l] != c )
      break;
    data[l] = 0;
    size--;
    if( l == 0 )
      break;
    l--;
  }
  if( ulOrigSize != size )
    data = (char *) realloc( data, size );
  return *this;
}

}   /* namespace */