Free Electron
DualString.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 // Based on CORAL GPL_String
8 
9 #ifndef __platform_dualstring_h__
10 #define __platform_dualstring_h__
11 
12 namespace fe
13 {
14 
15 #if defined(FE_OS) && FE_OS==FE_FREEBSD
16 #define FESTRING_WIDE __wchar_t
17 #define FESTR_MB2WC(x,y,z) mbstowcs((FESTRING_WIDE*)x,(const char*)y,z)
18 #define FESTR_WC2MB(x,y,z) wcstombs((char*)x,(const FESTRING_WIDE*)y,z)
19 #else
20 #define FESTRING_WIDE wchar_t
21 #define FESTR_MB2WC(x,y,z) mbstowcs(x,(char*)y,z)
22 #define FESTR_WC2MB(x,y,z) wcstombs((char*)x,y,z)
23  #endif
24 
25 #define FESTRING_WIDE_SIZE sizeof(FESTRING_WIDE)
26 
27 /**************************************************************************//**
28  @brief Automatically reference-counted string container.
29 
30  Designed to behave as a native type, like float or int.
31  All allocation worries are completely hidden.
32 
33  To assign a string just use simple assignments.
34  @code
35  DualString name("me");
36  name="you";
37  DualString id=name;
38  @endcode
39 
40  You can compare strings in a boolean or lexical fashion.
41  @code
42  if(name!="Bob" && name.compare("M")<0)
43  ...
44  @endcode
45 
46  To print a string in printf fashion, cast it to (char*) or use c_str().
47  @code
48  printf("%s %s\n",(char*)firstname,lastname.c_str());
49  @endcode
50 
51  Operates in multiple formats with JIT conversion.
52 
53  TODO consider fixed local buffer for small strings to avoid new()
54 
55  Reference: Stroustrup C++ Ref Man, 2nd ed., pg. 248
56 *//***************************************************************************/
58  {
59  private:
60 
61  class Rep
62  {
63  public:
64  void newBuffer(U32 size);
65  void deleteBuffer(void);
66 
67  U32 m_references;
68  FESTRING_U8* m_pBuffer8;
69  FESTRING_WIDE* m_pBufferWide;
70  BWORD m_stale8;
71  BWORD m_staleWide;
72  };
73 
74  public:
75  /// Construct an empty string.
76  DualString(void);
77 
78  /// Copy constructor.
79  DualString(const DualString &operand);
80 
81  /// Construct using a signed byte buffer.
82  DualString(const FESTRING_I8 *operand);
83 
84  /// Construct using a unsigned byte buffer.
85  DualString(const FESTRING_U8 *operand);
86 
87  /// Construct using a wide buffer, presumably Unicode.
88  DualString(const FESTRING_WIDE *operand);
89 
90  /// allow init as 0 in templates
91  DualString(I32 operand);
92 
93 virtual ~DualString(void);
94 
95  /** Cast the contents of the 8-bit buffer as signed
96  bytes. */
97  operator const FESTRING_I8 *(void) const
98  { return raw(); }
99 
100  /** Cast the contents of the 8-bit buffer as
101  unsigned bytes. */
102  operator const FESTRING_U8 *(void) const
103  { return rawU8(); }
104 
105  /// Cast the contents of the wide buffer as Unicode.
106  operator const FESTRING_WIDE *(void) const
107  { return rawWide(); }
108 
109  /// Compare to another DualString.
110  DualString &operator=(const DualString &operand);
111 
112  /// Compare to a signed byte buffer (using 8-bit).
113  DualString &operator=(const FESTRING_I8 *operand)
114  { return operator=((const FESTRING_U8 *)operand); };
115 
116  /// Compare to an unsigned byte buffer (using 8-bit).
117  DualString &operator=(const FESTRING_U8 *operand);
118 
119  /// Compare to an unicode buffer (using wide chars).
120  DualString &operator=(const FESTRING_WIDE *operand);
121 
122  /** Standard string compare: returns -1, 0, or 1 if the
123  string is alphabetically less than, equal,
124  or greater than the operand */
125  I32 compare(const DualString &operand) const;
126 
127  BWORD operator<(const DualString &operand) const
128  { return compare(operand) < 0; }
129 
130  /** Compares to another string assuming the given string
131  is a dot-delimited ordered list containing optional
132  substrings.
133  Returns true if strings are exactly the same, or if
134  they match only up to the length of the operand
135  and the operand's terminating zero matches a
136  period. */
137  BWORD dotMatch(const DualString &operand) const;
138 
139  /// Force all applicable characters to upper case.
140  void forceUppercase(void) { forceCase(true); };
141  /// Force all applicable characters to lower case.
142  void forceLowercase(void) { forceCase(false); };
143 
144  /** Return the number of represented characters,
145  but not necessarily the size of any buffer. */
146  U32 length(void) const;
147 
148  /// Populate the string in the manner of sprintf().
149  DualString &sPrintf(const char* fmt, ...);
150 
151  /** Populate the string as with SPrintf(), but by
152  concatenating the results to the existing string. */
153  DualString &catf(const char* fmt, ...);
154 
155  /** Concatenate the strings in a list. */
156  DualString &cat(std::list<DualString> &strList,
157  DualString sep = "");
158 
159  /** @brief Populate using variable arg format
160 
161  @internal */
162  DualString &vsPrintf(const char* fmt,va_list ap);
163 
164  /** Return the contents of the 8-bit buffer cast as
165  signed bytes. */
166 const FESTRING_I8 *raw(void) const { return (FESTRING_I8 *)rawU8(); };
167  /** Return the contents of the 8-bit buffer cast as
168  signed bytes. */
169 const FESTRING_I8 *c_str(void) const { return (FESTRING_I8 *)rawU8(); };
170  /** Return the contents of the 8-bit buffer cast as
171  unsigned bytes. */
172 const FESTRING_U8 *rawU8(void) const;
173  /** Return the contents of the wide buffer cast in
174  native unicode format. */
175 const FESTRING_WIDE *rawWide(void) const;
176 
177  /** @todo: string serialization currently hacked in.
178  only works for 8bit and does too much copying */
179  void output(std::ostream &ostrm) const;
180  void input(std::istream &istrm);
181 
182  private:
183  /** Returns a new string with a copied underlying
184  representation. Note that the normal operator=()
185  uses the same representation. This method is
186  provided in case data from an alternate heap may be
187  destroyed. This is primarily a Win32 DLL issue.
188  Note that DualString is already safe across live
189  heaps. */
190  DualString copy(void) const;
191 
192  void init(void);
193 
194  void forceCase(BWORD upper);
195 static U32 strlenWide(const FESTRING_WIDE *buffer);
196 
197  void confirm8(void);
198  void confirmWide(void);
199 
200 virtual Rep *newRep(void);
201 virtual void deleteRep(void);
202 
203  Rep *m_pRep;
204  };
205 
206 
207 /// Compare two DualString's
208 inline BWORD operator == (const DualString &s1, const DualString &s2)
209 {
210  if(s1.compare(s2)) { return false; }
211  return true;
212 }
213 
214 /// Compare a byte buffer to an DualString
215 inline BWORD operator == (const char *s1, const DualString &s2)
216 {
217  if(((const DualString)s1).compare(s2)) { return false; }
218  return true;
219 }
220 
221 /// Compare an DualString to a byte buffer
222 inline BWORD operator == (const DualString &s1, const char *s2)
223 {
224  if(s1.compare((const DualString)s2)) { return false; }
225  return true;
226 }
227 
228 /// Compare two DualString's (reverse logic)
229 inline BWORD operator != (const DualString &s1, const DualString &s2)
230 {
231  if(s1.compare(s2)) { return true; }
232  return false;
233 }
234 
235 /// Compare a byte buffer to an DualString (reverse logic)
236 inline BWORD operator != (const char *s1, const DualString &s2)
237 {
238  if(((const DualString)s1).compare(s2)) { return true; }
239  return false;
240 }
241 
242 /// Compare an DualString to a byte buffer (reverse logic)
243 inline BWORD operator != (const DualString &s1, const char *s2)
244 {
245  if(s1.compare((const DualString)s2)) { return true; }
246  return false;
247 }
248 
249 inline DualString operator+(const DualString &s1, const DualString &s2)
250 {
251  DualString result=s1;
252  return result.catf("%s",s2.c_str());
253 }
254 
255 /// Return a string for an FE_ERRNO code.
256 DualString errorDualString(int errnum);
257 
258 } /* namespace */
259 
260 #endif /* __platform_dualstring_h__ */
const FESTRING_I8 * c_str(void) const
Return the contents of the 8-bit buffer cast as signed bytes.
Definition: DualString.h:169
const FESTRING_I8 * raw(void) const
Return the contents of the 8-bit buffer cast as signed bytes.
Definition: DualString.h:166
Automatically reference-counted string container.
Definition: DualString.h:57
U32 length(void) const
Return the number of represented characters, but not necessarily the size of any buffer.
Definition: DualString.cc:276
DualString & cat(std::list< DualString > &strList, DualString sep="")
Concatenate the strings in a list.
Definition: DualString.cc:467
kernel
Definition: namespace.dox:3
DualString & sPrintf(const char *fmt,...)
Populate the string in the manner of sprintf().
Definition: DualString.cc:435
I32 compare(const DualString &operand) const
Standard string compare: returns -1, 0, or 1 if the string is alphabetically less than...
Definition: DualString.cc:367
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s (reverse logic)
Definition: DualString.h:229
const FESTRING_U8 * rawU8(void) const
Return the contents of the 8-bit buffer cast as unsigned bytes.
Definition: DualString.cc:308
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
DualString & operator=(const FESTRING_I8 *operand)
Compare to a signed byte buffer (using 8-bit).
Definition: DualString.h:113
DualString(void)
Construct an empty string.
Definition: DualString.cc:15
DualString & vsPrintf(const char *fmt, va_list ap)
Populate using variable arg format.
Definition: DualString.cc:483
void forceLowercase(void)
Force all applicable characters to lower case.
Definition: DualString.h:142
DualString & catf(const char *fmt,...)
Populate the string as with SPrintf(), but by concatenating the results to the existing string...
Definition: DualString.cc:449
void forceUppercase(void)
Force all applicable characters to upper case.
Definition: DualString.h:140
BWORD dotMatch(const DualString &operand) const
Compares to another string assuming the given string is a dot-delimited ordered list containing optio...
Definition: DualString.cc:393
const FESTRING_WIDE * rawWide(void) const
Return the contents of the wide buffer cast in native unicode format.
Definition: DualString.cc:318
void output(std::ostream &ostrm) const
Definition: DualString.cc:513
DualString errorDualString(int error)
Return a string for an FE_ERRNO code.
Definition: DualString.cc:584
DualString copy(void) const
Returns a new string with a copied underlying representation.
Definition: DualString.cc:292
DualString & operator=(const DualString &operand)
Compare to another DualString.
Definition: DualString.cc:246