[0169c1]: inc / ucommon / string.h  Maximize  Restore  History

Download this file

1749 lines (1514 with data), 51.1 kB

   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
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* A common string class and character string support functions.
* Ucommon offers a simple string class that operates through copy-on-write
* when needing to expand buffer size. Derived classes and templates allows
* one to create strings which live entirely in the stack frame rather
* than using the heap. This offers the benefit of the string class
* manipulative members without compromising performance or locking issues
* in threaded applications. Other things found here include better and
* safer char array manipulation functions.
* @file ucommon/string.h
*/
/**
* An example of the string class.
* @example string.cpp
*/
#ifndef _UCOMMON_STRING_H_
#define _UCOMMON_STRING_H_
#ifndef _UCOMMON_MEMORY_H_
#include <ucommon/memory.h>
#endif
#ifndef _UCOMMON_TIMERS_H_
#include <ucommon/timers.h>
#endif
#ifndef _UCOMMON_SOCKET_H_
#include <ucommon/socket.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
NAMESPACE_UCOMMON
/**
* A convenience class for size of strings.
*/
typedef unsigned short strsize_t;
class StringFormat;
/**
* A copy-on-write string class that operates by reference count. This string
* class anchors a counted object that is managed as a copy-on-write
* instance of the string data. This means that multiple instances of the
* string class can refer to the same string in memory if it has not been
* modifed, which reduces heap allocation. The string class offers functions
* to manipulate both the string object, and generic safe string functions to
* manipulate ordinary null terminated character arrays directly in memory.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT string : public Object
{
protected:
friend class StringFormat;
/**
* This is an internal class which contains the actual string data
* along with some control fields. The string can be either NULL
* terminated or filled like a Pascal-style string, but with a user
* selected fill character. The cstring object is an overdraft
* object, as the actual string text which is of unknown size follows
* immediately after the class control data. This class is primarely
* for internal use.
* @author David Sugar <dyfet@gnutelephony.org>
*/
public:
class __EXPORT cstring : public CountedObject
{
public:
#pragma pack(1)
strsize_t max; /**< Allocated size of cstring text */
strsize_t len; /**< Current length of cstring text */
char fill; /**< Filler character or 0 for none */
char text[1]; /**< Null terminated text, in overdraft space */
#pragma pack()
/**
* Create a cstring node allocated for specified string size. The
* new operator would also need the size as an overdraft value.
* @param size of string.
*/
cstring(strsize_t size);
/**
* Create a filled cstring node allocated for specified string size.
* The new operator would also need the size as an overdraft value.
* The newly allocated string is filled with the fill value.
* @param size of string.
* @param fill character value to fill string with.
*/
cstring(strsize_t size, char fill);
/**
* Used to clear a string. If null terminated, then the string ends
* at the offset, otherwise it is simply filled with fill data up to
* the specified size.
* @param offset to clear from.
* @param size of field to clear.
*/
void clear(strsize_t offset, strsize_t size);
/**
* Set part or all of a string with new text.
* @param offset to set from.
* @param text to insert from null terminated string.
* @param size of field to modify. This is filled for fill mode.
*/
void set(strsize_t offset, const char *text, strsize_t size);
/**
* Set our string from a string formatting object.
* @param format object to set from.
*/
void set(const StringFormat& format);
/**
* Set our string from null terminated text up to our allocated size.
* @param text to set from.
*/
void set(const char *text);
/**
* Append null terminated text to our string buffer.
* @param text to append.
*/
void add(const char *text);
/**
* Append text to string buffer from formatting object.
* @param format object to add from.
*/
void add(const StringFormat &format);
/**
* Append a single character to our string buffer.
* @param character to append.
*/
void add(char character);
/**
* Fill our string buffer to end if fill mode.
*/
void fix(void);
/**
* Trim filler at end to reduce filled string to null terminated
* string for further processing.
*/
void unfix(void);
/**
* Adjust size of our string buffer by deleting characters from
* start of buffer.
* @param number of characters to delete.
*/
void inc(strsize_t number);
/**
* Adjust size of our string buffer by deleting characters from
* end of buffer.
* @param number of characters to delete.
*/
void dec(strsize_t number);
};
protected:
cstring *str; /**< cstring instance our object references. */
/**
* Factory create a cstring object of specified size.
* @param size of allocated space for string buffer.
* @param fill character to use or 0 if null.
* @return new cstring object.
*/
cstring *create(strsize_t size, char fill = 0) const;
/**
* Compare the values of two string. This is a virtual so that it
* can be overriden for example if we want to create strings which
* ignore case, or which have special ordering rules.
* @param string to compare with.
* @return 0 if equal, <0 if less than, 0> if greater than.
*/
virtual int compare(const char *string) const;
/**
* Test if two string values are equal.
* @param string to compare with.
* @return true if equal.
*/
bool equal(const char *string) const;
/**
* Increase retention of our reference counted cstring. May be overriden
* for memstring which has fixed cstring object.
*/
virtual void retain(void);
/**
* Decrease retention of our reference counted cstring. May be overriden
* for memstring which has fixed cstring object.
*/
virtual void release(void);
/**
* Return cstring to use in copy constructors. Is virtual for memstring.
* @return cstring for copy constructor.
*/
virtual cstring *c_copy(void) const;
/**
* Copy on write operation for cstring. This always creates a new
* unique copy for write/modify operations and is a virtual for memstring
* to disable.
* @param size to add to allocated space when creating new cstring.
*/
virtual void cow(strsize_t size = 0);
strsize_t getStringSize(void);
public:
/**
* A constant for an invalid position value.
*/
static const strsize_t npos;
/**
* Create a new empty string object.
*/
string();
/**
* Create a string from a formatting object.
* @param format object to use in creating string.
*/
string(const StringFormat& format);
/**
* Create an empty string with a buffer pre-allocated to a specified size.
* @param size of buffer to allocate.
*/
string(strsize_t size);
/**
* Create a filled string with a buffer pre-allocated to a specified size.
* @param size of buffer to allocate.
* @param fill character to use.
*/
string(strsize_t size, char fill);
/**
* Create a string by printf-like formating into a pre-allocated space
* of a specified size.
* @param size of buffer to allocate.
* @param format control for string.
*/
string(strsize_t size, const char *format, ...) __PRINTF(3, 4);
/**
* Create a string from null terminated text.
* @param text to use for string.
*/
string(const char *text);
/**
* Create a string from null terminated text up to a maximum specified
* size.
* @param text to use for string.
* @param size limit of new string.
*/
string(const char *text, strsize_t size);
/**
* Create a string for a substring. The end of the substring is a
* pointer within the substring itself.
* @param text to use for string.
* @param end of text in substring.
*/
string(const char *text, const char *end);
/**
* Construct a copy of a string object. Our copy inherets the same
* reference counted instance of cstring as in the original.
* @param existing string to copy from.
*/
string(const string& existing);
/**
* Destroy string. De-reference cstring. If last reference to cstring,
* then also remove cstring from heap.
*/
virtual ~string();
/**
* Get a new string object as a substring of the current object.
* @param offset of substring.
* @param size of substring or 0 if to end.
* @return string object holding substring.
*/
string get(strsize_t offset, strsize_t size = 0) const;
/**
* Scan input items from a string object.
* @param format string of input to scan.
* @return number of items scanned.
*/
int scanf(const char *format, ...) __SCANF(2, 3);
/**
* Scan input items from a string object.
* @param format string of input to scan.
* @param args list to scan into.
* @return number of items scanned.
*/
int vscanf(const char *format, va_list args) __SCANF(2, 0);
/**
* Print items into a string object.
* @param format string of print format.
* @return number of bytes written to string.
*/
strsize_t printf(const char *format, ...) __PRINTF(2, 3);
/**
* Print items into a string object.
* @param format string of print format.
* @param args list to print.
* @return number of bytes written to string.
*/
strsize_t vprintf(const char *format, va_list args) __PRINTF(2, 0);
/**
* Get memory text buffer of string object.
* @return writable string buffer.
*/
char *c_mem(void) const;
/**
* Get character text buffer of string object.
* @return character text buffer.
*/
const char *c_str(void) const;
/**
* Resize and re-allocate string memory.
* @param size to allocate for string.
* @return true if re-allocated. False in derived memstring.
*/
virtual bool resize(strsize_t size);
/**
* Set string object to text of a null terminated string.
* @param text string to set.
*/
void set(const char *text);
/**
* Set string object to text of a formatted object.
* @param format object to use.
*/
void set(const StringFormat& format);
/**
* Set a portion of the string object at a specified offset to a text
* string.
* @param offset in object string buffer.
* @param text to set at offset.
* @param size of text area to set or 0 until end of text.
*/
void set(strsize_t offset, const char *text, strsize_t size = 0);
/**
* Set a text field within our string object.
* @param text to set.
* @param overflow character to use as filler if text is too short.
* @param offset in object string buffer to set text at.
* @param size of part of buffer to set with text and overflow.
*/
void set(const char *text, char overflow, strsize_t offset, strsize_t size = 0);
/**
* Set a text field within our string object offset from the end of buffer.
* @param text to set.
* @param overflow character to use as filler if text is too short.
* @param offset from end of object string buffer to set text at.
* @param size of part of buffer to set with text and overflow.
*/
void rset(const char *text, char overflow, strsize_t offset, strsize_t size = 0);
/**
* Append formatted object to our string buffer.
* @param format object to append.
*/
void add(const StringFormat& format);
/**
* Append null terminated text to our string buffer.
* @param text to append.
*/
void add(const char *text);
/**
* Append a single character to our string buffer.
* @param character to append.
*/
void add(char character);
/**
* Trim lead characters from the string.
* @param list of characters to remove.
*/
void trim(const char *list);
/**
* Chop trailing characters from the string.
* @param list of characters to remove.
*/
void chop(const char *list);
/**
* Strip lead and trailing characters from the string.
* @param list of characters to remove.
*/
void strip(const char *list);
/**
* Unquote a quoted string. Removes lead and trailing quote marks.
* @param quote pairs of characters for open and close quote.
* @return true if string was quoted.
*/
bool unquote(const char *quote);
/**
* Cut (remove) text from string.
* @param offset to start of text field to remove.
* @param size of text field to remove or 0 to remove to end of string.
*/
void cut(strsize_t offset, strsize_t size = 0);
/**
* Clear a field of a filled string with filler.
* @param offset to start of field to clear.
* @param size of field to fill or 0 to fill to end of string.
*/
void clear(strsize_t offset, strsize_t size = 0);
/**
* Clear string by setting to empty.
*/
void clear(void);
/**
* Convert string to upper case.
*/
void upper(void);
/**
* Convert string to lower case.
*/
void lower(void);
/**
* Count number of occurrances of characters in string.
* @param list of characters to find.
* @return count of instances of characters in string.
*/
strsize_t ccount(const char *list) const;
/**
* Count all characters in the string (strlen).
* @return count of characters.
*/
strsize_t count(void) const;
/**
* Get the size of currently allocated space for string.
* @return size allocated for text.
*/
strsize_t size(void) const;
/**
* Find offset of a pointer into our string buffer. This can be used
* to find the offset position of a pointer returned by find, for
* example. This is used when one needs to convert a member function
* that returns a pointer to call a member function that operates by
* a offset value. If the pointer is outside the range of the string
* then npos is returned.
* @param pointer into our object's string buffer.
*/
strsize_t offset(const char *pointer) const;
/**
* Return character found at a specific position in the string.
* @param position in string, negative values computed from end.
* @return character code at specified position in string.
*/
char at(int position) const;
/**
* Find last occurance of a character in the string.
* @param list of characters to search for.
* @return pointer to last occurance from list or NULL.
*/
const char *last(const char *list) const;
/**
* Find first occurance of a character in the string.
* @param list of characters to search for.
* @return pointer to first occurance from list or NULL.
*/
const char *first(const char *list) const;
/**
* Get pointer to first character in string for iteration.
* @return first character pointer or NULL if empty.
*/
const char *begin(void) const;
/**
* Get pointer to last character in string for iteration.
* @return last character pointer or NULL if empty.
*/
const char *end(void) const;
/**
* Skip lead characters in the string.
* @param list of characters to skip when found.
* @param offset to start of scan.
* @return pointer to first part of string past skipped characters.
*/
const char *skip(const char *list, strsize_t offset = 0) const;
/**
* Skip trailing characters in the string. This searches the
* string in reverse order.
* @param list of characters to skip when found.
* @param offset to start of scan. Default is end of string.
* @return pointer to first part of string before skipped characters.
*/
const char *rskip(const char *list, strsize_t offset = npos) const;
/**
* Find a character in the string.
* @param list of characters to search for.
* @param offset to start of search.
* @return pointer to first occurance of character.
*/
const char *find(const char *list, strsize_t offset = 0) const;
/**
* Find last occurance of character in the string.
* @param list of characters to search for.
* @param offset to start of search. Default is end of string.
* @return pointer to last occurance of character.
*/
const char *rfind(const char *list, strsize_t offset = npos) const;
/**
* Split the string by a pointer position. Everything after the pointer
* is removed.
* @param pointer to split position in string.
*/
void split(const char *pointer);
/**
* Split the string at a specific offset. Everything after the offset
* is removed.
* @param offset to split position in string.
*/
void split(strsize_t offset);
/**
* Split the string by a pointer position. Everything before the pointer
* is removed.
* @param pointer to split position in string.
*/
void rsplit(const char *pointer);
/**
* Split the string at a specific offset. Everything before the offset
* is removed.
* @param offset to split position in string.
*/
void rsplit(strsize_t offset);
/**
* Find pointer in string where specified character appears.
* @param character to find.
* @return string pointer for character if found, NULL if not.
*/
const char *chr(char character) const;
/**
* Find pointer in string where specified character last appears.
* @param character to find.
* @return string pointer for last occurance of character if found,
* NULL if not.
*/
const char *rchr(char character) const;
/**
* Get length of string.
* @return length of string.
*/
strsize_t len(void);
/**
* Get filler character used for field array strings.
* @return filler character or 0 if none.
*/
char fill(void);
/**
* Casting reference to raw text string.
* @return null terminated text of string.
*/
inline operator const char *() const
{return c_str();};
/**
* Reference raw text buffer by pointer operator.
* @return null terminated text of string.
*/
inline const char *operator*() const
{return c_str();};
/**
* Test if the string's allocated space is all used up.
* @return true if no more room for append.
*/
bool full(void) const;
/**
* Get a new substring through object expression.
* @param offset of substring.
* @param size of substring or 0 if to end.
* @return string object holding substring.
*/
string operator()(int offset, strsize_t size) const;
/**
* Reference a string in the object by relative offset. Positive
* offsets are from the start of the string, negative from the
* end.
* @param offset to string position.
* @return pointer to string data or NULL if invalid offset.
*/
const char *operator()(int offset) const;
/**
* Reference a single character in string object by array offset.
* @param offset to character.
* @return character value at offset.
*/
const char operator[](int offset) const;
/**
* Test if string is empty.
* @return true if string is empty.
*/
bool operator!() const;
/**
* Test if string has data.
* @return true if string has data.
*/
operator bool() const;
/**
* Create new cow instance and assign value from another string object.
* @param object to assign from.
* @return our object for expression use.
*/
string& operator^=(const string& object);
/**
* Create new cow instance and assign value from formatted string object.
* @param format object to assign from.
* @return our object for expression use.
*/
string& operator^=(const StringFormat& format);
/**
* Create new cow instance and assign value from null terminated text.
* @param text to assign from.
* @return our object for expression use.
*/
string& operator^=(const char *text);
/**
* Concatenate null terminated text to our object. This creates a new
* copy-on-write instance to hold the concatenated string.
* @param text to concatenate.
*/
string& operator+(const char *text);
/**
* Concatenate string formatted object to our object. This creates a new
* copy-on-write instance to hold the concatenated string.
* @param format object to concatenate.
*/
string& operator+(const StringFormat& format);
/**
* Concatenate null terminated text to our object. This directly
* appends the text to the string buffer and does not resize the
* object if the existing cstring allocation space is fully used.
* @param text to concatenate.
*/
string& operator&(const char *text);
/**
* Concatenate formatted string object to our object. This directly
* appends the text to the string buffer and does not resize the
* object if the existing cstring allocation space is fully used.
* @param format object to concatenate.
*/
string& operator&(const StringFormat& format);
/**
* Assign our string with the cstring of another object. If we had
* an active string reference, it is released. The object now has
* a duplicate reference to the cstring of the other object.
* @param object to assign from.
*/
string& operator=(const string& object);
/**
* Assign our string from a string formatting object.
* @param format object to assign from.
*/
string& operator=(const StringFormat& format);
/**
* Assign text to our existing buffer. This performs a set method.
* @param text to assign from.
*/
string& operator=(const char *text);
/**
* Delete first character from string.
*/
string& operator++(void);
/**
* Delete a specified number of characters from start of string.
* @param number of characters to delete.
*/
string& operator+=(strsize_t number);
/**
* Delete last character from string.
*/
string& operator--(void);
/**
* Delete a specified number of characters from end of string.
* @param number of characters to delete.
*/
string& operator-=(strsize_t number);
/**
* Compare our object with null terminated text.
* @param text to compare with.
* @return true if we are equal.
*/
bool operator==(const char *text) const;
/**
* Compare our object with null terminated text. Compare method is used.
* @param text to compare with.
* @return true if we are not equal.
*/
bool operator!=(const char *text) const;
/**
* Compare our object with null terminated text. Compare method is used.
* @param text to compare with.
* @return true if we are less than text.
*/
bool operator<(const char *text) const;
/**
* Compare our object with null terminated text. Compare method is used.
* @param text to compare with.
* @return true if we are less than or equal to text.
*/
bool operator<=(const char *text) const;
/**
* Compare our object with null terminated text. Compare method is used.
* @param text to compare with.
* @return true if we are greater than text.
*/
bool operator>(const char *text) const;
/**
* Compare our object with null terminated text. Compare method is used.
* @param text to compare with.
* @return true if we are greater than or equal to text.
*/
bool operator>=(const char *text) const;
/**
* Scan input items from a string object.
* @param object to scan from.
* @param format string of input to scan.
* @return number of items scanned.
*/
static int scanf(string& object, const char *format, ...) __SCANF(2, 3);
/**
* Print formatted items into a string object.
* @param object to print into.
* @param format string to print with.
* @return number of bytes written into object.
*/
static strsize_t printf(string& object, const char *format, ...) __PRINTF(2, 3);
/**
* Read arbitrary binary data from socket into a string object. The
* total number of bytes that may be read is based on the allocated
* size of the object.
* @param socket to read from.
* @param object to save read data.
* @return number of bytes read.
*/
static int read(Socket& socket, string& object);
/**
* Write the string object to a socket.
* @param socket to write to.
* @param object to get data from.
* @return number of bytes written.
*/
static int write(Socket& socket, string& object);
/**
* Read arbitrary binary data from a file into a string object. The
* total number of bytes that may be read is based on the allocated
* size of the object.
* @param file to read from.
* @param object to save read data.
* @return number of bytes read.
*/
static int read(FILE *file, string& object);
/**
* Write the string object to a file.
* @param file to write to.
* @param object to get data from.
* @return number of bytes written.
*/
static int write(FILE *file, string& object);
/**
* Read a line of text input from a socket into the object. The
* maximum number of bytes that may be read is based on the currently
* allocated size of the object.
* @param socket to read from.
* @param object to save read data.
* @return false if end of file.
*/
static bool getline(Socket& socket, string& object);
/**
* Write string as a line of text data to a socket. A newline will be
* appended to the end.
* @param socket to print to.
* @param object to get text line from.
* @return true if successful.
*/
static bool putline(Socket& socket, string& object);
/**
* Read a line of text input from a file into the object. The
* maximum number of bytes that may be read is based on the currently
* allocated size of the object.
* @param file to read from.
* @param object to save read data.
* @return false if end of file.
*/
static bool getline(FILE *file, string& object);
/**
* Write string as a line of text data to a file. A newline will be
* appended to the end.
* @param file to print to.
* @param object to get text line to put into file.
* @return true if successful.
*/
static bool putline(FILE *file, string& object);
/**
* Swap the cstring references between two strings.
* @param object1 to swap.
* @param object2 to swap.
*/
static void swap(string& object1, string& object2);
/**
* Fix and reset string object filler.
* @param object to fix.
*/
static void fix(string& object);
/**
* Convert null terminated text to lower case.
* @param text to convert.
*/
static void lower(char *text);
/**
* Convert null terminated text to upper case.
* @param text to convert.
*/
static void upper(char *text);
/**
* A thread-safe token parsing routine for null terminated strings. This
* is related to strtok, but with safety checks for NULL values and a
* number of enhancements including support for quoted text that may
* contain token seperators within quotes. The text string is modified
* as it is parsed.
* @param text string to examine for tokens.
* @param last token position or set to NULL for start of string.
* @param list of characters to use as token seperators.
* @param quote pairs of characters for quoted text or NULL if not used.
* @param end of line marker characters or NULL if not used.
* @return token extracted from string or NULL if no more tokens found.
*/
static char *token(char *text, char **last, const char *list, const char *quote = NULL, const char *end = NULL);
/**
* Skip after lead characters in a null terminated string.
* @param text pointer to start at.
* @param list of characters to skip when found.
* @return pointer to first part of string past skipped characters.
*/
static char *skip(char *text, const char *list);
/**
* Skip before trailing characters in a null terminated string.
* @param text pointer to start at.
* @param list of characters to skip when found.
* @return pointer to last part of string past skipped characters.
*/
static char *rskip(char *text, const char *list);
/**
* Unquote a quoted null terminated string. Returns updated string
* position and replaces trailing quote with null byte if quoted.
* @param text to examine.
* @param quote pairs of character for open and close quote.
* @return new text pointer if quoted, NULL if unchanged.
*/
static char *unquote(char *text, const char *quote);
/**
* Set a field in a null terminated string relative to the end of text.
* @param buffer to modify.
* @param size of field to set.
* @param text to replace end of string with.
* @return pointer to text buffer.
*/
static char *rset(char *buffer, size_t size, const char *text);
/**
* Safely set a null terminated string buffer in memory. If the text
* is too large to fit into the buffer, it is truncated to the size.
* @param buffer to set.
* @param size of buffer. Includes null byte at end of string.
* @param text to set in buffer.
* @return pointer to text buffer.
*/
static char *set(char *buffer, size_t size, const char *text);
/**
* Safely set a null terminated string buffer in memory. If the text
* is too large to fit into the buffer, it is truncated to the size.
* @param buffer to set.
* @param size of buffer. Includes null byte at end of string.
* @param text to set in buffer.
* @param max size of text to set.
* @return pointer to text buffer.
*/
static char *set(char *buffer, size_t size, const char *text, size_t max);
/**
* Safely append a null terminated string into an existing string in
* memory. If the resulting string is too large to fit into the buffer,
* it is truncated to the size.
* @param buffer to set.
* @param size of buffer. Includes null byte at end of string.
* @param text to set in buffer.
* @return pointer to text buffer.
*/
static char *add(char *buffer, size_t size, const char *text);
/**
* Safely append a null terminated string into an existing string in
* memory. If the resulting string is too large to fit into the buffer,
* it is truncated to the size.
* @param buffer to set.
* @param size of buffer. Includes null byte at end of string.
* @param text to set in buffer.
* @param max size of text to append.
* @return pointer to text buffer.
*/
static char *add(char *buffer, size_t size, const char *text, size_t max);
/**
* Find position of case insensitive substring within a string.
* @param text to search in.
* @param key string to locate.
* @param optional separator chars if formatted as list of keys.
* @return substring position if found, or NULL.
*/
static const char *ifind(const char *text, const char *key, const char *optional);
/**
* Find position of substring within a string.
* @param text to search in.
* @param key string to locate.
* @param optional separator chars if formatted as list of keys.
* @return substring position if found, or NULL.
*/
static const char *find(const char *text, const char *key, const char *optional);
/**
* Safe version of strlen function. Accepts NULL as 0 length strings.
* @param text string.
* @return length of string.
*/
static size_t count(const char *text);
/**
* Safe string comparison function.
* @param text1 to compare.
* @param text2 to compare.
* @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2.
*/
static int compare(const char *text1, const char *text2);
/**
* Simple equal test for strings.
* @param text1 to test.
* @param text2 to test.
* @return true if equal and case is same.
*/
static bool equal(const char *text1, const char *text2);
/**
* Safe string comparison function.
* @param text1 to compare.
* @param text2 to compare.
* @param size limit of strings to compare.
* @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2.
*/
static int compare(const char *text1, const char *text2, size_t size);
/**
* Simple equal test for strings.
* @param text1 to test.
* @param text2 to test.
* @param size limit of strings to compare.
* @return true if equal and case is same.
*/
static bool equal(const char *text1, const char *text2, size_t size);
/**
* Safe case insensitive string comparison function.
* @param text1 to compare.
* @param text2 to compare.
* @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2.
*/
static int case_compare(const char *text1, const char *text2);
/**
* Simple case insensitive equal test for strings.
* @param text1 to test.
* @param text2 to test.
* @return true if equal.
*/
static bool case_equal(const char *text1, const char *text2);
/**
* Safe case insensitive string comparison function.
* @param text1 to compare.
* @param text2 to compare.
* @param size limit of strings to compare.
* @return 0 if equal, >0 if text1 > text2, <0 if text1 < text2.
*/
static int case_compare(const char *text1, const char *text2, size_t size);
/**
* Simple case insenstive equal test for strings.
* @param text1 to test.
* @param text2 to test.
* @param size limit of strings to compare.
* @return true if equal.
*/
static bool case_equal(const char *text1, const char *text2, size_t size);
/**
* Return start of string after characters to trim from beginning.
* This function does not modify memory.
* @param text buffer to examine.
* @param list of characters to skip from start.
* @return position in text past lead trim.
*/
static char *trim(char *text, const char *list);
/**
* Strip trailing characters from the text string. This function will
* modify memory.
* @param text buffer to examine.
* @param list of characters to chop from trailing end of string.
* @return pointer to text buffer.
*/
static char *chop(char *text, const char *list);
/**
* Skip lead and remove trailing characters from a text string. This
* function will modify memory.
* @param text buffer to examine.
* @param list of characters to trim and chop.
* @return position in text past lead trim.
*/
static char *strip(char *text, const char *list);
/**
* Fill a section of memory with a fixed text character. Adds a null
* byte at the end.
* @param text buffer to fill.
* @param size of text buffer with null terminated byte.
* @param character to fill with.
* @return pointer to text buffer.
*/
static char *fill(char *text, size_t size, char character);
/**
* Count instances of characters in a list in a text buffer.
* @param text buffer to examine.
* @param list of characters to count in buffer.
* @return number of instances of the characters in buffer.
*/
static unsigned ccount(const char *text, const char *list);
/**
* Find the first occurance of a character in a text buffer.
* @param text buffer to examine.
* @param list of characters to search for.
* @return pointer to first instance found or NULL.
*/
static char *find(char *text, const char *list);
/**
* Find the last occurance of a character in a text buffer.
* @param text buffer to examine.
* @param list of characters to search for.
* @return pointer to last instance found or NULL.
*/
static char *rfind(char *text, const char *list);
/**
* Get pointer to first character past character requested.
* @param text buffer to examine.
* @param list of characters.
* @return first character pointer past list.
*/
static char *first(char *text, const char *list);
/**
* Get pointer to last character before character requested.
* @param text buffer to examine.
* @param list of characters.
* @return last character pointer past list.
*/
static char *last(char *text, const char *list);
/**
* Duplicate null terminated text into the heap.
* @param text to duplicate.
* @return duplicate copy of text allocated from heap.
*/
static char *dup(const char *text);
/**
* A thread-safe token parsing routine for strings objects. This
* is related to strtok, but with safety checks for NULL values and a
* number of enhancements including support for quoted text that may
* contain token seperators within quotes. The object is modified
* as it is parsed.
* @param object to examine for tokens.
* @param last token position or set to NULL for start of string.
* @param list of characters to use as token seperators.
* @param quote pairs of characters for quoted text or NULL if not used.
* @param end of line marker characters or NULL if not used.
* @return token extracted from string or NULL if no more tokens found.
*/
inline static char *token(string& object, char **last, const char *list, const char *quote = NULL, const char *end = NULL)
{return token(object.c_mem(), last, list, quote, end);};
/**
* Scan input items from a string object.
* @param object to scan.
* @param format string of input to scan.
* @param args list to scan into.
* @return number of items scanned.
*/
__SCANF(2,0) inline static int vscanf(string& object, const char *format, va_list args)
{return object.vscanf(format, args);}
/**
* Print items into a string object.
* @param object to print into.
* @param format string of print format.
* @param args list to print.
* @return number of bytes written to string.
*/
__PRINTF(2,0) inline static strsize_t vprintf(string& object, const char *format, va_list args)
{return object.vprintf(format, args);}
/**
* Count all characters in the string object (strlen).
* @param object to count.
* @return count of characters.
*/
inline static strsize_t len(string& object)
{return object.len();};
/**
* Get memory text buffer of string object.
* @param object to get string buffer from.
* @return writable string buffer.
*/
inline static char *mem(string& object)
{return object.c_mem();};
/**
* Get the size of currently allocated space for string.
* @param object to examine.
* @return size allocated for text.
*/
inline static strsize_t size(string& object)
{return object.size();};
/**
* Clear a string object.
* @param object to clear.
*/
inline static void clear(string& object)
{object.clear();};
/**
* Count number of occurrances of characters in string object.
* @param object to examine.
* @param list of characters to find.
* @return count of instances of characters.
*/
inline static unsigned ccount(string& object, const char *list)
{return object.ccount(list);};
/**
* Count all characters in the string object (strlen).
* @param object to count.
* @return count of characters.
*/
inline static size_t count(string& object)
{return object.count();};
/**
* Convert string object to upper case.
* @param object to modify.
*/
inline static void upper(string& object)
{object.upper();};
/**
* Convert string object to lower case.
* @param object to modify.
*/
inline static void lower(string& object)
{object.lower();};
/**
* Unquote a quoted string. Removes lead and trailing quote marks.
* @param object to unquote.
* @param quote pairs of characters for open and close quote.
* @return true if string was quoted.
*/
inline static bool unquote(string& object, const char *quote)
{return object.unquote(quote);};
/**
* Trim lead characters from the string.
* @param object to trim.
* @param list of characters to remove.
*/
inline static void trim(string& object, const char *list)
{object.trim(list);};
/**
* Chop trailing characters from the string.
* @param object to chop.
* @param list of characters to remove.
*/
inline static void chop(string& object, const char *list)
{object.trim(list);};
/**
* Strip lead and trailing characters from the string.
* @param object to strip.
* @param list of characters to remove.
*/
inline static void strip(string& object, const char *list)
{object.trim(list);};
/**
* Find a character in the string.
* @param object to search.
* @param list of characters to search for.
* @return pointer to first occurance of character.
*/
inline static const char *find(string& object, const char *list)
{return object.find(list);};
/**
* Find last character in the string.
* @param object to search.
* @param list of characters to search for.
* @return pointer to last occurance of character.
*/
inline static const char *rfind(string& object, const char *list)
{return object.rfind(list);};
/**
* Get pointer to first character past character requested.
* @param object to examine.
* @param list of characters.
* @return first character pointer past list.
*/
inline static const char *first(string& object, const char *list)
{return object.first(list);};
/**
* Get pointer to last character past character requested.
* @param object to examine.
* @param list of characters.
* @return last character pointer before list.
*/
inline static const char *last(string& object, const char *list)
{return object.last(list);};
/**
* Convert string to a double value.
* @param object to convert.
* @param pointer to update with end of parsed value.
* @return double value of object.
*/
inline static double tod(string& object, char **pointer = NULL)
{return strtod(mem(object), pointer);};
/**
* Convert string to a long value.
* @param object to convert.
* @param pointer to update with end of parsed value.
* @return long value of object.
*/
inline static long tol(string& object, char **pointer = NULL)
{return strtol(mem(object), pointer, 0);};
/**
* Convert text to a double value.
* @param text to convert.
* @param pointer to update with end of parsed value.
* @return double value of object.
*/
inline static double tod(const char *text, char **pointer = NULL)
{return strtod(text, pointer);};
/**
* Convert text to a long value.
* @param text to convert.
* @param pointer to update with end of parsed value.
* @return long value of object.
*/
inline static long tol(const char *text, char **pointer = NULL)
{return strtol(text, pointer, 0);};
};
/**
* A string class that uses a cstring buffer that is fixed in memory.
* This allows one to manipulate a fixed buffer of text in memory through
* the string class. The size of the memory used must include space for
* the overhead() size needed for the cstring object control data.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT memstring : public string
{
public:
static const size_t header;
private:
bool resize(strsize_t size);
void cow(strsize_t adj = 0);
void release(void);
protected:
cstring *c_copy(void) const;
public:
/**
* Assign the text of a string to our object.
* @param object to copy text from.
*/
inline void operator=(string& object)
{set(object.c_str());};
/**
* Assign null terminated text to our object.
* @param text to copy.
*/
inline void operator=(const char *text)
{set(text);};
/**
* Create an instance of a memory string.
* @param memory to use for cstring object.
* @param size of string. Total size must include space for overhead.
* @param fill character for fixed character fields.
*/
memstring(void *memory, strsize_t size, char fill = 0);
/**
* Destroy memory string.
*/
~memstring();
/**
* Create a memory string with memory allocated from the heap.
* @param size of string to allocate. Automatically adds control size.
* @param fill character for fixed field strings.
*/
static memstring *create(strsize_t size, char fill = 0);
/**
* Create a memory string with memory allocated from a pager.
* @param pager to allocate memory from.
* @param size of string to allocate. Automatically adds control size.
* @param fill character for fixed field strings.
*/
static memstring *create(mempager *pager, strsize_t size, char fill = 0);
};
/**
* A string conversion class for use as a base class in objects which can
* transform themselves into string representations. This can be used
* together with the string class for automatic conversions.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT StringFormat
{
protected:
friend class string;
friend class string::cstring;
virtual ~StringFormat();
/**
* Convert derived object into a string buffer. This may be a public
* method in a derived class.
* @param buffer to save representation in.
* @param size of buffer to use.
*/
virtual void put(char *buffer, size_t size) const = 0;
/**
* Get maximum space that might be needed for string representation.
* @return space needed.
*/
virtual strsize_t getStringSize(void) const = 0;
};
/**
* A template to create a character array that can be manipulated as a string.
* This is a mini string/stringbuf class that supports a subset of
* functionality but does not require a complex supporting object. Like
* stringbuf, this can be used to create local string variables.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template<size_t S>
class charbuf
{
private:
char buffer[S];
public:
/**
* Create a new character buffer with an empty string.
*/
inline charbuf()
{buffer[0] = 0;};
/**
* Create a character buffer with assigned text. If the text is
* larger than the size of the object, it is truncated.
* @param text to assign.
*/
inline charbuf(const char *text)
{string::set(buffer, S, text);};
/**
* Assign null terminated text to the object.
* @param text to assign.
*/
inline void operator=(const char *text)
{string::set(buffer, S, text);};
/**
* Concatenate text into the object. If the text is larger than the
* size of the object, then it is truncated.
* @param text to append.
*/
inline void operator+=(const char *text)
{string::add(buffer, S, text);};
/**
* Test if data is contained in the object.
* @return true if there is text.
*/
inline operator bool() const
{return buffer[0];};
/**
* Test if the object is empty.
* @return true if the object is empty.
*/
inline bool operator!() const
{return buffer[0] == 0;};
/**
* Get text by casting reference.
* @return pointer to text in object.
*/
inline operator char *()
{return buffer;};
/**
* Get text by object pointer reference.
* @return pointer to text in object.
*/
inline char *operator*()
{return buffer;};
/**
* Array operator to get a character from the object.
* @param offset of character in string buffer.
* @return character at offset.
*/
inline char operator[](size_t offset) const
{return buffer[offset];};
/**
* Get a pointer to an offset in the object by expression operator.
* @param offset of character in string buffer.
* @return pointer to offset in object.
*/
inline char *operator()(size_t offset)
{return buffer + offset;};
/**
* Get allocated size of the object.
* @return allocated size.
*/
inline size_t size(void) const
{return S;};
};
/**
* A string class that has a predefined string buffer. The string class
* and buffer are allocated together as one object. This allows one to use
* string objects entirely resident on the local stack as well as on the
* heap. Using a string class on the local stack may be more convenient
* than a char array since one can use all the features of the class
* including assignment and concatenation which a char buffer cannot as
* easily do.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template<strsize_t S>
class stringbuf : public memstring
{
private:
char buffer[sizeof(cstring) + S];
public:
/**
* Create an empty instance of a string buffer.
*/
inline stringbuf() : memstring(buffer, S) {};
/**
* Create a string buffer from a null terminated string.
* @param text to place in object.
*/
inline stringbuf(const char *text) : memstring(buffer, S) {set(text);};
/**
* Assign a string buffer from a null terminated string.
* @param text to assign to object.
*/
inline void operator=(const char *text)
{set(text);};
/**
* Assign a string buffer from another string object.
* @param object to assign from.
*/
inline void operator=(string& object)
{set(object.c_str());};
};
#if !defined(_MSWINDOWS_) && !defined(__QNX__)
/**
* Convenience function for case insensitive null terminated string compare.
* @param string1 to compare.
* @param string2 to compare.
* @return 0 if equal, > 0 if s2 > s1, < 0 if s2 < s1.
*/
extern "C" inline int stricmp(const char *string1, const char *string2)
{return string::case_compare(string1, string2);}
/**
* Convenience function for case insensitive null terminated string compare.
* @param string1 to compare.
* @param string2 to compare.
* @param max size of string to compare.
* @return 0 if equal, > 0 if s2 > s1, < 0 if s2 < s1.
*/
extern "C" inline int strnicmp(const char *string1, const char *string2, size_t max)
{return string::case_compare(string1, string2, max);}
#endif
/**
* A convenience type for string.
*/
typedef string string_t;
/**
* A convenience type when mixing std::string in old compilers that are bad
* with namespaces...
*/
typedef string String;
/**
* Compare two null terminated strings if equal.
* @param s1 string to compare.
* @param s2 string to compare.
* @return true if equal.
*/
inline bool eq(char const *s1, char const *s2)
{return String::equal(s1, s2);}
/**
* Compare two null terminated strings if equal up to specified size.
* @param s1 string to compare.
* @param s2 string to compare.
* @param size of string to compare.
* @return true if equal.
*/
inline bool eq(char const *s1, char const *s2, size_t size)
{return String::equal(s1, s2, size);}
/**
* Compare two string objects if equal.
* @param s1 string to compare.
* @param s2 string to compare.
* @return true if equal.
*/
inline bool eq(String &s1, String &s2)
{return String::equal(s1.c_str(), s2.c_str());}
/**
* Compare two null terminated strings if equal ignoring case. This is
* related to stricmp or gcc strcasecmp.
* @param s1 string to compare.
* @param s2 string to compare.
* @return true if equal.
*/
inline bool ieq(char const *s1, char const *s2)
{return String::case_equal(s1, s2);}
/**
* Compare two null terminated strings if equal for a specified size
* ignoring case. This is related to stricmp or gcc strcasecmp.
* @param s1 string to compare.
* @param s2 string to compare.
* @param size of string to compare.
* @return true if equal.
*/
inline bool ieq(char const *s1, char const *s2, size_t size)
{return String::case_equal(s1, s2, size);}
/**
* Compare two string objects if equal ignoring case. This is
* related to stricmp or gcc strcasecmp.
* @param s1 string to compare.
* @param s2 string to compare.
* @return true if equal.
*/
inline bool ieq(String &s1, String &s2)
{return String::case_equal(s1.c_str(), s2.c_str());}
END_NAMESPACE
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks