BDS Public
Beamlib 3.3.4
This is the Beam C++ class library.
Loading...
Searching...
No Matches
BString.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BString.h BString Handling
3 * T.Barnaby, BEAM Ltd, 29/10/91
4 * Copyright (c) 2022 All Right Reserved, Beam Ltd, https://www.beam.ltd.uk
5 * For license see LICENSE.txt at the root of the beamlib source tree.
6 *******************************************************************************
7 */
8#ifndef BSTRING_H
9#define BSTRING_H 1
10
11#include <BTypes.h>
12#include <BRefData.h>
13#include <BList.h>
14#include <BArray.h>
15#include <iostream>
16
17class BError;
18
20class BString {
21public:
22 BString();
23 BString(const BString& string);
24 BString(const char* str);
25 BString(const char* str, unsigned int len);
26 BString(char ch);
27 BString(BInt v);
28 BString(BUInt v);
30 BString(double v);
31
32 ~BString();
33
34 static BString convert(char ch);
35 static BString convert(BInt value);
36 static BString convert(BUInt value);
37 static BString convert(double value, int eFormat = 0);
38 static BString convert(BUInt64 value);
39 static BString convertHex(BInt value);
40 static BString convertHex(BUInt value);
41
42 BString copy() const;
43
44 int len() const;
45 const char* retStr() const;
46 const char* str() const;
47 char* retStrDup() const;
48 int retInt() const;
49 unsigned int retUInt() const;
50 double retDouble() const;
51 BFloat64 retFloat64() const;
52
53 int compare(const BString& string) const;
54 int compareWild(const BString& string) const;
55 int compareWildExpression(const BString& string) const;
56 int compareRegex(const BString& pattern, int ignoreCase = 0) const;
57 BString& truncate(int len);
58 BString& pad(int len);
59 void clear();
60
61 BString& toUpper();
62 BString& toLower();
64 void removeNL();
65 BString justify(int leftMargin, int width);
66 BString fixedLen(int length, int rightJustify = 0);
68 BString translateChar(char ch, BString replace = " ");
69 BString reverse() const;
70
71 BString subString(int start, int len) const;
72 int del(int start, int len);
73 int insert(int start, BString str);
74 int append(const BString& str);
75 BString add(const BString& str) const;
76 BString& printf(const char* fmt, ...) ;
77
78 int find(char ch) const;
79 int find(BString str) const;
80 int findReverse(char ch) const;
81
82 BString csvEncode() const;
84
85 BString base64Encode() const;
87
88
90 BList<BString> getTokenList(char separator);
91 BString removeSeparators(BString separators);
92 BString pullToken(BString terminators);
93 BString pullSeparators(BString separators);
96 BList<BString> split(char splitChar);
97
98 // Filename operations
100 BString filename();
101 BString basename();
104
105 // Misc functions
106 BUInt32 hash() const;
107 char& get(int pos);
108 const char& get(int pos) const;
109
110 /* Operator functions */
111 BString& operator=(const BString& string);
112 char& operator[](int pos);
113
114 int operator==(const BString& s) const{
115 return (compare(s) == 0);
116 }
117 int operator==(const char* s) const{
118 return (compare(s) == 0);
119 }
120 int operator>(const BString& s) const{
121 return (compare(s) > 0);
122 }
123 int operator>(const char* s) const{
124 return (compare(s) > 0);
125 }
126 int operator<(const BString& s) const{
127 return (compare(s) < 0);
128 }
129 int operator<(const char* s) const{
130 return (compare(s) < 0);
131 }
132 int operator>=(const BString& s) const{
133 return (compare(s) >= 0);
134 }
135 int operator<=(const BString& s) const{
136 return (compare(s) <= 0);
137 }
138 int operator!=(const BString& s) const{
139 return (compare(s) != 0);
140 }
141 int operator!=(const char* s) const{
142 return (compare(s) != 0);
143 }
144 BString operator+(const BString& s) const {
145 return add(s);
146 }
147 BString operator+(const char* s) const {
148 return add(s);
149 }
151 *this = add(s);
152 return *this;
153 }
154 BString operator+=(const char* s){
155 *this = add(s);
156 return *this;
157 }
158
159 /* Special operators */
160 BString operator+(char ch) const {
161 return add(convert(ch));
162 }
164 return add(convert(i));
165 }
167 return add(convert(i));
168 }
170 return add(convert(i));
171 }
172 operator const char* () const {
173 return retStr();
174 }
175#ifdef QSTRING_H
176 // QT support
177 BString(const QString& str){
178#if QT_VERSION >= 0x040000
179 init(str.toLatin1());
180#else
181 init(str);
182#endif
183 }
184#ifndef ZAP
185 operator QString (){ // QString support
186 return QString(retStr());
187 }
188#endif
189#endif
190
191 // For backwards compatibility
192 BString field(int field) const;
193 char** fields();
194
195protected:
197
198private:
199 void init(const char* str);
200 int inString(int pos) const;
201 int isSpace(char ch) const;
202};
203
204// std::Stream Functions
205std::ostream& operator<<(std::ostream& o, BString& s);
206std::istream& operator>>(std::istream& i, BString& s);
207
208// List and array functions
211
213
214BString blistToString(const BStringList& list);
215BStringList bstringToList(BString str, int stripSpaces = 0);
216BStringList charToList(const char** str);
217
219BStringArray bstringToArray(BString str, int stripSpaces = 0);
220BStringArray charToArray(const char** str);
221
222// String conversion functions
223void toBString(BString& v, BString& s);
224void toBString(BStringList& v, BString& s);
225void toBString(BInt32& v, BString& s);
226void toBString(BUInt32& v, BString& s);
227void toBString(BUInt64& v, BString& s);
228void toBString(BFloat64& v, BString& s);
229void fromBString(BString& s, BString& v);
230void fromBString(BString& s, BStringList& v);
231void fromBString(BString& s, BInt32& v);
232void fromBString(BString& s, BUInt32& v);
233void fromBString(BString& s, BUInt64& v);
234void fromBString(BString& s, BFloat64& v);
235
236// CString functions
237inline char from_hex(char ch){
238 return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
239}
240
241inline char to_hex(char code){
242 static char hex[] = "0123456789abcdef";
243 return hex[code & 15];
244}
245
246char* bstrncpy(char* dest, const char* src, size_t n);
247char* bstrtrim(char* str);
248const char* intToString(char* str, BUInt strLen, int value, int base = 10);
249const char* int64ToString(char* str, BUInt strLen, BInt64 value, int base = 10);
250const char* floatToString(char *str, BUInt strLen, BFloat32 f, BUInt precision);
251
252#endif
BUInt32 length
Definition BFirmware.h:8
int bstringListinList(BStringList &l, BString s)
Definition BString.cpp:1234
const char * intToString(char *str, BUInt strLen, int value, int base=10)
Definition BString.cpp:1402
char * bstrncpy(char *dest, const char *src, size_t n)
Definition BString.cpp:1527
char to_hex(char code)
Definition BString.h:241
void fromBString(BString &s, BString &v)
Definition BString.cpp:1378
BStringList bstringToList(BString str, int stripSpaces=0)
Convert a comma separated string to a string list.
Definition BString.cpp:1259
const char * floatToString(char *str, BUInt strLen, BFloat32 f, BUInt precision)
Definition BString.cpp:1486
BArray< BString > BStringArray
Definition BString.h:210
char from_hex(char ch)
Definition BString.h:237
char * bstrtrim(char *str)
Definition BString.cpp:1535
const char * int64ToString(char *str, BUInt strLen, BInt64 value, int base=10)
Definition BString.cpp:1444
BStringArray charToArray(const char **str)
Definition BString.cpp:1341
BStringArray bstringToArray(BString str, int stripSpaces=0)
Convert a comma separated string to a string array.
Definition BString.cpp:1311
BStringList charToList(const char **str)
Definition BString.cpp:1288
BList< BString > BStringList
Definition BString.h:209
void toBString(BString &v, BString &s)
Definition BString.cpp:1353
BString blistToString(const BStringList &list)
Convert a string list to a comma separated string.
Definition BString.cpp:1246
BString barrayToString(const BStringArray &list)
Convert a string array to a comma separated string.
Definition BString.cpp:1298
std::istream & operator>>(std::istream &i, BString &s)
Definition BString.cpp:1226
std::ostream & operator<<(std::ostream &o, BString &s)
Definition BString.cpp:1221
double BFloat64
Definition BTypes.h:28
BUInt32 BUInt
Definition BTypes.h:33
uint32_t BUInt32
Definition BTypes.h:24
int32_t BInt32
Definition BTypes.h:23
int64_t BInt64
Definition BTypes.h:25
BInt32 BInt
Definition BTypes.h:32
float BFloat32
Definition BTypes.h:27
uint64_t BUInt64
Definition BTypes.h:26
Template based Array class.
Definition BArray.h:23
Error return class. This class is used to return the error status from a function....
Definition BError.h:31
Template based list class.
Definition BList.h:31
A pointer to a variable sized data area with reference counting so the data areas can be shared.
Definition BRefData.h:20
This class stores and manipulates ASCII strings.
Definition BString.h:20
BString & toLower()
Convert to lowercase.
Definition BString.cpp:353
BString operator+(BUInt64 i) const
Definition BString.h:169
BString pullWord()
Pull a word out of the head of the string.
Definition BString.cpp:916
int findReverse(char ch) const
Find ch in string searching backwards. Returns -1 if not found.
Definition BString.cpp:722
BString & pad(int len)
Pad to length len.
Definition BString.cpp:332
char ** fields()
Depreciated function.
Definition BString.cpp:788
int operator<(const BString &s) const
Definition BString.h:126
int retInt() const
Return string as a int.
Definition BString.cpp:1029
BList< BString > getTokenList(BString separators)
Break string into tokens.
Definition BString.cpp:811
BString operator+(const BString &s) const
Definition BString.h:144
int compareWildExpression(const BString &string) const
Compare string to space deliminated patterns. Returns 0 for match.
Definition BString.cpp:960
BString operator+(const char *s) const
Definition BString.h:147
BString dirname()
Return the directory component if the string is a file path name.
Definition BString.cpp:1142
BString base64Encode() const
Encode a string to base64.
Definition BString.cpp:505
int operator<(const char *s) const
Definition BString.h:129
void removeNL()
Remove if present NL from last char.
Definition BString.cpp:374
BString fixedLen(int length, int rightJustify=0)
return string formated to fixed length
Definition BString.cpp:420
int find(char ch) const
Find ch in string searching forwards. Returns -1 if not found.
Definition BString.cpp:703
int compareRegex(const BString &pattern, int ignoreCase=0) const
Compare strings. Returns 1 for match.
Definition BString.cpp:982
int operator!=(const BString &s) const
Definition BString.h:138
BString extension()
Return the file extension component if the string is a file path name.
Definition BString.cpp:1180
int compareWild(const BString &string) const
Compare string to string with wildcards . Returns 0 for match.
Definition BString.cpp:953
char * retStrDup() const
Ptr to newly malloc'd char*.
Definition BString.cpp:1019
void clear()
Clear the string.
Definition BString.cpp:325
BString lowerFirst()
Return string with lowercase first character.
Definition BString.cpp:364
int insert(int start, BString str)
Insert substring.
Definition BString.cpp:668
int operator>(const BString &s) const
Definition BString.h:120
BString csvEncode() const
Encode a string for CSV.
Definition BString.cpp:470
BString operator+(char ch) const
Definition BString.h:160
BString & operator=(const BString &string)
Definition BString.cpp:1091
BString removeSeparators(BString separators)
Remove any char from sepatators from string.
Definition BString.cpp:871
BString translateChar(char ch, BString replace=" ")
Translate character converting them to the given string.
Definition BString.cpp:454
BString justify(int leftMargin, int width)
Justify the string to the given width.
Definition BString.cpp:384
BString pullToken(BString terminators)
Pull token from start of string.
Definition BString.cpp:839
BString filename()
Return the filename component if the string is a file path name.
Definition BString.cpp:1153
BUInt32 hash() const
Create a 32bit hash number for the string.
Definition BString.cpp:1200
BString operator+(BUInt i) const
Definition BString.h:166
BString field(int field) const
Depreciated function.
Definition BString.cpp:738
const char * str() const
Ptr to char* representation.
Definition BString.cpp:1010
BString copy() const
Return an independant copy.
Definition BString.cpp:299
BList< BString > split(char splitChar)
Split string into an array based on the character separator.
Definition BString.cpp:925
BString operator+=(const char *s)
Definition BString.h:154
BString & csvDecode(const BString str)
Decode a string from CSV.
Definition BString.cpp:488
const char * retStr() const
Ptr to char* representation.
Definition BString.cpp:1001
BString operator+(BInt i) const
Definition BString.h:163
char & operator[](int pos)
Definition BString.cpp:1129
static BString convert(char ch)
Converts char to string.
Definition BString.cpp:242
BString firstLine()
Return first line.
Definition BString.cpp:450
double retDouble() const
Return string as a double.
Definition BString.cpp:1045
int del(int start, int len)
Delete substring.
Definition BString.cpp:648
BString reverse() const
Reverse character order.
Definition BString.cpp:612
static BString convertHex(BInt value)
Converts int to string as hex value.
Definition BString.cpp:285
int append(const BString &str)
Append a string.
Definition BString.cpp:1061
int operator==(const char *s) const
Definition BString.h:117
int operator>(const char *s) const
Definition BString.h:123
BError base64Decode(BString &str) const
Decode a string from base64.
Definition BString.cpp:564
int operator>=(const BString &s) const
Definition BString.h:132
BString & truncate(int len)
Truncate to length len.
Definition BString.cpp:314
BString pullSeparators(BString separators)
Pull separators from start of string.
Definition BString.cpp:897
BString & toUpper()
Convert to uppercase.
Definition BString.cpp:342
BString extensionFull()
Return the file extension component with leading '.' if the string is a file path name.
Definition BString.cpp:1190
BString operator+=(const BString &s)
Definition BString.h:150
BString subString(int start, int len) const
Returns substring.
Definition BString.cpp:624
int compare(const BString &string) const
Compare strings. Returns 0 for match.
Definition BString.cpp:946
BString basename()
Return the file name component if the string is a file path name.
Definition BString.cpp:1165
BString pullLine()
Pull a line out of the head of the string.
Definition BString.cpp:921
int operator!=(const char *s) const
Definition BString.h:141
char & get(int pos)
Return the character at the position give. Will print an error message and raise the SIGABORT excepti...
Definition BString.cpp:1103
BString()
Definition BString.cpp:158
unsigned int retUInt() const
Return string as a int.
Definition BString.cpp:1037
BRefData * ostr
This is the reference counted string storage.
Definition BString.h:196
int operator<=(const BString &s) const
Definition BString.h:135
BString add(const BString &str) const
Add strings returning result.
Definition BString.cpp:1077
BFloat64 retFloat64() const
Return string as a BFloat64.
Definition BString.cpp:1053
BString & printf(const char *fmt,...)
Formated print into the string.
Definition BString.cpp:683
~BString()
Definition BString.cpp:151
int operator==(const BString &s) const
Definition BString.h:114
int len() const
Length of string.
Definition BString.cpp:306