NetBurner 3.1
nbstring.h
1 #ifndef DEF_NB_STRING
2 #define DEF_NB_STRING (1)
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <system.h>
7 
8 const int native_string_storage = 15;
9 const size_t npos = -1;
10 
11 struct nbstring_allocated_info
12 {
13  uint8_t flag;
14  uint8_t unused1;
15  uint16_t unused2;
16  uint32_t used; // 4
17  uint32_t alloced; // 4
18  uint8_t *pData; // 4
19 };
20 
21 struct nbstring_local
22 {
23  uint8_t siz_flag;
24  uint8_t stor[native_string_storage];
25 };
26 
27 class NBStorageManager
28 {
29  private:
30  union {
31  nbstring_local nbss;
32  nbstring_allocated_info inf;
33  };
34 
35  public:
36  bool IsConstStore() const;
37  bool IsLocalStore() const;
38 
39  void reservespace(size_t n);
40  void setusedsize(size_t n);
41 
42  size_t GetUsed() const;
43  size_t GetAvailable() const;
44  char *GetBuf();
45  const char *GetConstBuf() const;
46 
47  void SetFromConstant(const char *c, int len);
48 
49  void clear();
50 
51  ~NBStorageManager();
52  NBStorageManager();
53 
54  static void swap(NBStorageManager &n1, NBStorageManager &n2);
55 #ifdef STRING_DIAGS
56  void DumpDiag();
57 #endif
58 };
59 
60 class NBString
61 {
62  protected:
63  NBStorageManager nbs;
64 
65  void DoStorage(const char *cp, int len);
66 
67  public:
68  // constructors
69  NBString();
70  NBString(const NBString &str);
71  NBString(const NBString &str, size_t pos, size_t len = npos);
72  NBString(const char *s);
73  NBString(const char *s, size_t n);
74 
75  ~NBString();
76 
77  // Copy ...
78  NBString &operator=(const NBString &str);
79  NBString &operator=(const char *s);
80  NBString &operator=(char c);
81 
82  // Look up
83  char &operator[](size_t pos);
84  const char &operator[](size_t pos) const;
85 
86  // C_Str access...
87  const char *c_str() const;
88 
89  // Substrign access
90  NBString substr(size_t pos = 0, size_t len = npos) const;
91 
92  // compares
93  int compare(const NBString &str) const;
94  int compare(const char *s) const;
95 
96  size_t size() const;
97  size_t length() const;
98 
99  void clear();
100  bool empty() const;
101 
102  NBString &Append(const char *str, size_t len);
103  NBString &operator+=(const NBString &str);
104  NBString &operator+=(const char *str);
105  NBString &operator+=(char c);
106 
107 #ifdef STRING_DIAGS
108  void DumpDiag();
109 #endif
110 
111  // Really usefule when optimizing things and returnign values...
112  friend void swap(NBString &x, NBString &y);
113 
114  // concatinate
115  friend NBString operator+(const NBString &lhs, const NBString &rhs);
116  friend NBString operator+(const NBString &lhs, const char *rhs);
117  friend NBString operator+(const char *lhs, const NBString &rhs);
118  friend NBString operator+(const NBString &lhs, char rhs);
119  friend NBString operator+(char lhs, const NBString &rhs);
120 
121  // Compare...
122  friend bool operator==(const NBString &lhs, const NBString &rhs);
123  friend bool operator==(const char *lhs, const NBString &rhs);
124  friend bool operator==(const NBString &lhs, const char *rhs);
125  friend bool operator!=(const NBString &lhs, const NBString &rhs);
126  friend bool operator!=(const char *lhs, const NBString &rhs);
127  friend bool operator!=(const NBString &lhs, const char *rhs);
128  friend bool operator<(const NBString &lhs, const NBString &rhs);
129  friend bool operator<(const char *lhs, const NBString &rhs);
130  friend bool operator<(const NBString &lhs, const char *rhs);
131  friend bool operator<=(const NBString &lhs, const NBString &rhs);
132  friend bool operator<=(const char *lhs, const NBString &rhs);
133  friend bool operator<=(const NBString &lhs, const char *rhs);
134  friend bool operator>(const NBString &lhs, const NBString &rhs);
135  friend bool operator>(const char *lhs, const NBString &rhs);
136  friend bool operator>(const NBString &lhs, const char *rhs);
137  friend bool operator>=(const NBString &lhs, const NBString &rhs);
138  friend bool operator>=(const char *lhs, const NBString &rhs);
139  friend bool operator>=(const NBString &lhs, const char *rhs);
140 
141  /* Things I would add that are not in the standard string classes...*/
142 
143  int vsiprintf(const char *format, va_list &vl);
144 
145  // sprintf into the string....
146  int sprintf(const char *format, ...);
147  int sprintf(NBString const format, ...);
148 
149  // sprintf into the string....
150  int siprintf(const char *format, ...);
151  int siprintf(NBString const format, ...);
152 
153  // convert from "Text" to numbers
154  int stoi() const;
155  long stol() const;
156  // long long stoll const;
157 
158  unsigned int stoui() const;
159  unsigned long stoul() const;
160  // unsigned long long stoull() const;
161 
162  double stod() const;
163 
164  // Conver from text to IPADDR
165  IPADDR to_ipaddr() const;
166 
167  /* Do we need these...
168 
169  //Compare substrings....
170  int compare (size_t pos, size_t len, const NBString& str) const;
171  int compare (size_t pos, size_t len, const NBString& str,size_t subpos, size_t sublen) const;
172  int compare (size_t pos, size_t len, const char* s) const;
173  int compare (size_t pos, size_t len, const char* s, size_t n) const;
174 
175  */
176 
177  // Copy bytes from subsring to someplace else....
178  // Allways does not null terminate. Just moves...
179  size_t copy(char *s, size_t len, size_t pos = 0) const;
180 
181  // same as above but always null terminates
182  size_t strcopy(char *s, size_t len, size_t pos = 0) const;
183 };
184 
185 // concatinate
186 NBString operator+(const NBString &lhs, const NBString &rhs);
187 NBString operator+(const NBString &lhs, const char *rhs);
188 NBString operator+(const char *lhs, const NBString &rhs);
189 NBString operator+(const NBString &lhs, char rhs);
190 NBString operator+(char lhs, const NBString &rhs);
191 
192 // Compare...
193 inline bool operator==(const NBString &lhs, const NBString &rhs)
194 {
195  return lhs.compare(rhs) == 0;
196 };
197 inline bool operator==(const char *lhs, const NBString &rhs)
198 {
199  return rhs.compare(lhs) == 0;
200 };
201 inline bool operator==(const NBString &lhs, const char *rhs)
202 {
203  return lhs.compare(rhs) == 0;
204 };
205 inline bool operator!=(const NBString &lhs, const NBString &rhs)
206 {
207  return lhs.compare(rhs) != 0;
208 };
209 inline bool operator!=(const char *lhs, const NBString &rhs)
210 {
211  return rhs.compare(lhs) != 0;
212 };
213 inline bool operator!=(const NBString &lhs, const char *rhs)
214 {
215  return lhs.compare(rhs) != 0;
216 };
217 inline bool operator<(const NBString &lhs, const NBString &rhs)
218 {
219  return lhs.compare(rhs) > 0;
220 };
221 inline bool operator<(const char *lhs, const NBString &rhs)
222 {
223  return rhs.compare(lhs) < 0;
224 };
225 inline bool operator<(const NBString &lhs, const char *rhs)
226 {
227  return lhs.compare(rhs) > 0;
228 };
229 inline bool operator<=(const NBString &lhs, const NBString &rhs)
230 {
231  return lhs.compare(rhs) >= 0;
232 };
233 inline bool operator<=(const char *lhs, const NBString &rhs)
234 {
235  return rhs.compare(lhs) <= 0;
236 };
237 inline bool operator<=(const NBString &lhs, const char *rhs)
238 {
239  return lhs.compare(rhs) >= 0;
240 };
241 inline bool operator>(const NBString &lhs, const NBString &rhs)
242 {
243  return lhs.compare(rhs) < 0;
244 };
245 inline bool operator>(const char *lhs, const NBString &rhs)
246 {
247  return rhs.compare(lhs) > 0;
248 };
249 inline bool operator>(const NBString &lhs, const char *rhs)
250 {
251  return lhs.compare(rhs) < 0;
252 };
253 inline bool operator>=(const NBString &lhs, const NBString &rhs)
254 {
255  return lhs.compare(rhs) <= 0;
256 };
257 inline bool operator>=(const char *lhs, const NBString &rhs)
258 {
259  return rhs.compare(lhs) >= 0;
260 };
261 inline bool operator>=(const NBString &lhs, const char *rhs)
262 {
263  return lhs.compare(rhs) <= 0;
264 };
265 
266 #endif
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition: ipv6_addr.h:28
NetBurner System Functions.