Beamlib  3.0.1
This is the Beam C++ class library.
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 
17 class BError;
18 
20 class BString {
21 public:
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);
29  BString(BUInt64 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);
67  BString firstLine();
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;
83  BString& csvDecode(const BString str);
84 
85  BString base64Encode() const;
86  BError base64Decode(BString& str) const;
87 
88 
89  BList<BString> getTokenList(BString separators);
90  BList<BString> getTokenList(char separator);
91  BString removeSeparators(BString separators);
92  BString pullToken(BString terminators);
93  BString pullSeparators(BString separators);
94  BString pullWord();
95  BString pullLine();
96  BList<BString> split(char splitChar);
97 
98  // Filename operations
99  BString dirname();
100  BString basename();
101  BString extension();
102 
103  // Misc functions
104  BUInt32 hash() const;
105  char& get(int pos);
106  const char& get(int pos) const;
107 
108  /* Operator functions */
109  BString& operator=(const BString& string);
110  char& operator[](int pos);
111 
112  int operator==(const BString& s) const{
113  return (compare(s) == 0);
114  }
115  int operator==(const char* s) const{
116  return (compare(s) == 0);
117  }
118  int operator>(const BString& s) const{
119  return (compare(s) > 0);
120  }
121  int operator>(const char* s) const{
122  return (compare(s) > 0);
123  }
124  int operator<(const BString& s) const{
125  return (compare(s) < 0);
126  }
127  int operator<(const char* s) const{
128  return (compare(s) < 0);
129  }
130  int operator>=(const BString& s) const{
131  return (compare(s) >= 0);
132  }
133  int operator<=(const BString& s) const{
134  return (compare(s) <= 0);
135  }
136  int operator!=(const BString& s) const{
137  return (compare(s) != 0);
138  }
139  int operator!=(const char* s) const{
140  return (compare(s) != 0);
141  }
142  BString operator+(const BString& s) const {
143  return add(s);
144  }
145  BString operator+(const char* s) const {
146  return add(s);
147  }
149  *this = add(s);
150  return *this;
151  }
152  BString operator+=(const char* s){
153  *this = add(s);
154  return *this;
155  }
156 
157  /* Special operators */
158  BString operator+(char ch) const {
159  return add(convert(ch));
160  }
161  BString operator+(BInt i) const {
162  return add(convert(i));
163  }
165  return add(convert(i));
166  }
168  return add(convert(i));
169  }
170  operator const char* () const {
171  return retStr();
172  }
173 #ifdef QSTRING_H
174  // QT support
175  BString(const QString& str){
176 #if QT_VERSION >= 0x040000
177  init(str.toLatin1());
178 #else
179  init(str);
180 #endif
181  }
182 #ifndef ZAP
183  operator QString (){ // QString support
184  return QString(retStr());
185  }
186 #endif
187 #endif
188 
189  // For backwards compatibility
190  BString field(int field) const;
191  char** fields();
192 
193 protected:
195 
196 private:
197  void init(const char* str);
198  int inString(int pos) const;
199  int isSpace(char ch) const;
200 };
201 
202 // std::Stream Functions
203 std::ostream& operator<<(std::ostream& o, BString& s);
204 std::istream& operator>>(std::istream& i, BString& s);
205 
206 // List and array functions
209 
211 
212 BString blistToString(const BStringList& list);
213 BStringList bstringToList(BString str, int stripSpaces = 0);
214 BStringList charToList(const char** str);
215 
216 BString barrayToString(const BStringArray& list);
217 BStringArray bstringToArray(BString str, int stripSpaces = 0);
218 BStringArray charToArray(const char** str);
219 
220 // String conversion functions
221 void toBString(BString& v, BString& s);
222 void toBString(BStringList& v, BString& s);
223 void toBString(BInt32& v, BString& s);
224 void toBString(BUInt32& v, BString& s);
225 void toBString(BUInt64& v, BString& s);
226 void toBString(BFloat64& v, BString& s);
227 void fromBString(BString& s, BString& v);
228 void fromBString(BString& s, BStringList& v);
229 void fromBString(BString& s, BInt32& v);
230 void fromBString(BString& s, BUInt32& v);
231 void fromBString(BString& s, BUInt64& v);
232 void fromBString(BString& s, BFloat64& v);
233 
234 // CString functions
235 inline char from_hex(char ch){
236  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
237 }
238 
239 inline char to_hex(char code){
240  static char hex[] = "0123456789abcdef";
241  return hex[code & 15];
242 }
243 
244 char* bstrncpy(char* dest, const char* src, size_t n);
245 char* bstrtrim(char* str);
246 const char* intToString(char* str, BUInt strLen, int value, int base = 10);
247 const char* int64ToString(char* str, BUInt strLen, BInt64 value, int base = 10);
248 const char* floatToString(char *str, BUInt strLen, BFloat32 f, BUInt precision);
249 
250 #endif
BUInt32 length
Definition: BFirmware.h:8
char * bstrtrim(char *str)
Definition: BString.cpp:1514
int bstringListinList(BStringList &l, BString s)
Definition: BString.cpp:1213
const char * intToString(char *str, BUInt strLen, int value, int base=10)
Definition: BString.cpp:1381
const char * int64ToString(char *str, BUInt strLen, BInt64 value, int base=10)
Definition: BString.cpp:1423
char to_hex(char code)
Definition: BString.h:239
void fromBString(BString &s, BString &v)
Definition: BString.cpp:1357
BStringList bstringToList(BString str, int stripSpaces=0)
Convert a comma separated string to a string list.
Definition: BString.cpp:1238
BArray< BString > BStringArray
Definition: BString.h:208
char from_hex(char ch)
Definition: BString.h:235
std::istream & operator>>(std::istream &i, BString &s)
Definition: BString.cpp:1205
char * bstrncpy(char *dest, const char *src, size_t n)
Definition: BString.cpp:1506
const char * floatToString(char *str, BUInt strLen, BFloat32 f, BUInt precision)
Definition: BString.cpp:1465
BStringArray charToArray(const char **str)
Definition: BString.cpp:1320
BStringArray bstringToArray(BString str, int stripSpaces=0)
Convert a comma separated string to a string array.
Definition: BString.cpp:1290
BStringList charToList(const char **str)
Definition: BString.cpp:1267
BList< BString > BStringList
Definition: BString.h:207
void toBString(BString &v, BString &s)
Definition: BString.cpp:1332
std::ostream & operator<<(std::ostream &o, BString &s)
Definition: BString.cpp:1200
BString blistToString(const BStringList &list)
Convert a string list to a comma separated string.
Definition: BString.cpp:1225
BString barrayToString(const BStringArray &list)
Convert a string array to a comma separated string.
Definition: BString.cpp:1277
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
Error return class. This class is used to return the error status from a function....
Definition: BError.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:352
BString operator+(BUInt64 i) const
Definition: BString.h:167
BString pullWord()
Pull a word out of the head of the string.
Definition: BString.cpp:915
int findReverse(char ch) const
Find ch in string searching backwards.
Definition: BString.cpp:721
BString & pad(int len)
Pad to length len.
Definition: BString.cpp:331
char ** fields()
Depreciated function.
Definition: BString.cpp:787
int operator<(const BString &s) const
Definition: BString.h:124
int retInt() const
Return string as a int.
Definition: BString.cpp:1030
BList< BString > getTokenList(BString separators)
Break string into tokens.
Definition: BString.cpp:810
BString operator+(const BString &s) const
Definition: BString.h:142
int compareWildExpression(const BString &string) const
Compare string to space deliminated patterns. Returns 0 for match.
Definition: BString.cpp:959
BString operator+(const char *s) const
Definition: BString.h:145
BString dirname()
Return the directory component if the string is a file path name.
Definition: BString.cpp:1143
BString base64Encode() const
Encode a string to base64.
Definition: BString.cpp:504
int operator<(const char *s) const
Definition: BString.h:127
void removeNL()
Remove if present NL from last char.
Definition: BString.cpp:373
BString fixedLen(int length, int rightJustify=0)
return string formated to fixed length
Definition: BString.cpp:419
int find(char ch) const
Find ch in string searching forwards.
Definition: BString.cpp:702
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:136
BString extension()
Return the file extension component if the string is a file path name.
Definition: BString.cpp:1169
int compareWild(const BString &string) const
Compare string to string with wildcards . Returns 0 for match.
Definition: BString.cpp:952
char * retStrDup() const
Ptr to newly malloc'd char*.
Definition: BString.cpp:1020
void clear()
Clear the string.
Definition: BString.cpp:324
BString lowerFirst()
Return string with lowercase first character.
Definition: BString.cpp:363
int insert(int start, BString str)
Insert substring.
Definition: BString.cpp:667
int operator>(const BString &s) const
Definition: BString.h:118
BString csvEncode() const
Encode a string for CSV.
Definition: BString.cpp:469
BString operator+(char ch) const
Definition: BString.h:158
BString & operator=(const BString &string)
Definition: BString.cpp:1092
BString removeSeparators(BString separators)
Remove any char from sepatators from string.
Definition: BString.cpp:870
BString translateChar(char ch, BString replace=" ")
Translate character converting them to the given string.
Definition: BString.cpp:453
BString justify(int leftMargin, int width)
Justify the string to the given width.
Definition: BString.cpp:383
BString pullToken(BString terminators)
Pull token from start of string.
Definition: BString.cpp:838
BUInt32 hash() const
Create a 32bit hash number for the string.
Definition: BString.cpp:1179
BString operator+(BUInt i) const
Definition: BString.h:164
BString field(int field) const
Depreciated function.
Definition: BString.cpp:737
const char * str() const
Ptr to char* representation.
Definition: BString.cpp:1011
BString copy() const
Return an independant copy.
Definition: BString.cpp:298
BList< BString > split(char splitChar)
Split string into an array based on the character separator.
Definition: BString.cpp:924
BString operator+=(const char *s)
Definition: BString.h:152
BString & csvDecode(const BString str)
Decode a string from CSV.
Definition: BString.cpp:487
const char * retStr() const
Ptr to char* representation.
Definition: BString.cpp:1002
BString operator+(BInt i) const
Definition: BString.h:161
char & operator[](int pos)
Definition: BString.cpp:1130
static BString convert(char ch)
Converts char to string.
Definition: BString.cpp:241
BString firstLine()
Return first line.
Definition: BString.cpp:449
double retDouble() const
Return string as a double.
Definition: BString.cpp:1046
int del(int start, int len)
Delete substring.
Definition: BString.cpp:647
BString reverse() const
Reverse character order.
Definition: BString.cpp:611
static BString convertHex(BInt value)
Converts int to string as hex value.
Definition: BString.cpp:284
int append(const BString &str)
Append a string.
Definition: BString.cpp:1062
int operator==(const char *s) const
Definition: BString.h:115
int operator>(const char *s) const
Definition: BString.h:121
BError base64Decode(BString &str) const
Decode a string from base64.
Definition: BString.cpp:563
int operator>=(const BString &s) const
Definition: BString.h:130
BString & truncate(int len)
Truncate to length len.
Definition: BString.cpp:313
BString pullSeparators(BString separators)
Pull separators from start of string.
Definition: BString.cpp:896
BString & toUpper()
Convert to uppercase.
Definition: BString.cpp:341
BString operator+=(const BString &s)
Definition: BString.h:148
BString subString(int start, int len) const
Returns substring.
Definition: BString.cpp:623
int compare(const BString &string) const
Compare strings. Returns 0 for match.
Definition: BString.cpp:945
BString basename()
Return the file name component if the string is a file path name.
Definition: BString.cpp:1154
BString pullLine()
Pull a line out of the head of the string.
Definition: BString.cpp:920
int operator!=(const char *s) const
Definition: BString.h:139
char & get(int pos)
Return the character at the position give. Will print an error message and raise the SIGABORT excepti...
Definition: BString.cpp:1104
BString()
Definition: BString.cpp:157
unsigned int retUInt() const
Return string as a int.
Definition: BString.cpp:1038
BRefData * ostr
This is the reference counted string storage.
Definition: BString.h:194
int operator<=(const BString &s) const
Definition: BString.h:133
BString add(const BString &str) const
Add strings returning result.
Definition: BString.cpp:1078
BFloat64 retFloat64() const
Return string as a BFloat64.
Definition: BString.cpp:1054
BString & printf(const char *fmt,...)
Formated print into the string.
Definition: BString.cpp:682
~BString()
Definition: BString.cpp:150
int operator==(const BString &s) const
Definition: BString.h:112
int len() const
Length of string.
Definition: BString.cpp:305