OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
memory.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Reliable platform independent header for common memory and string functions
7//
8//----------------------------------------------------------------------------
9
10#if !defined(OWL_PRIVATE_MEMORY_H)
11#define OWL_PRIVATE_MEMORY_H
12
13#include <owl/private/defs.h>
14#if defined(BI_HAS_PRAGMA_ONCE)
15# pragma once
16#endif
17
18#include <tchar.h>
19
20#if defined(BI_COMP_BORLANDC)
21# include <mem.h>
22#else
23# include <memory.h>
24#endif
25#if !defined(__STRING_H) && !defined(_INC_STRING)
26# include <string.h>
27#endif
28#if !defined(BI_COMP_GNUC)
29# include <stdlib.h>
30#endif
31#include <malloc.h>
32
33#if !defined(BI_COMP_BORLANDC) && !(defined(__GNUC__) && (__GNUC__ >= 3))
34# define alloca _alloca
35#endif
36
37#if !defined(OWL_PRIVATE_WSYSINC_H)
38# include <owl/private/wsysinc.h>
39#endif
40
41
42#if defined(BI_NEED_TCHAR)
43# if defined(BI_COMP_BORLANDC)
44inline int _tcscmp(const _TCHAR* s1, const _TCHAR* s2)
45 {return lstrcmp(s1, s2);}
46inline int _tcsicmp(const _TCHAR* s1, const _TCHAR* s2)
47 {return lstrcmpi(s1, s2);}
48# endif
49# if !defined(strcmpi)
50inline int strcmpi(const _TCHAR* s1, const _TCHAR* s2)
51 {return lstrcmpi(s1, s2);}
52# endif //strcmpi
53#endif
54
55/// Return the number of bytes of the first character of the passed string
56inline UINT CharSize(const _TCHAR* s) {return static_cast<UINT>(sizeof(_TCHAR) * (::CharNext(s) - s));}
57
58inline _TCHAR CharUpper(_TCHAR c) {::CharUpperBuff(&c,1); return c;}
59inline _TCHAR CharLower(_TCHAR c) {::CharLowerBuff(&c,1); return c;}
60
61#if defined(_CONVERSION_USES_THREAD_LOCALE)
62# define BI_CONVERSION_USES_THREAD_LOCALE
63#endif
64
65#if !defined(USES_CONVERSION)
66# ifdef BI_CONVERSION_USES_THREAD_LOCALE
67# define OWL_USES_CONVERSION_ACP_INIT_ GetACP()
68# else
69# define OWL_USES_CONVERSION_ACP_INIT_ CP_ACP
70# endif
71# define USES_CONVERSION\
72 int _convert = 0; static_cast<void>(_convert);\
73 UINT _acp = OWL_USES_CONVERSION_ACP_INIT_; static_cast<void>(_acp);\
74 LPCWSTR _lpw = NULL; static_cast<void>(_lpw);\
75 LPCSTR _lpa = NULL; static_cast<void>(_lpa)
76#endif
77
78// Global UNICODE<>ANSI translation helpers
79inline LPWSTR OwlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars, unsigned int acp)
80{
81 // verify that no illegal character present
82 // since lpw was allocated based on the size of lpa
83 // don't worry about the number of chars
84 lpw[0] = '\0';
86 return lpw;
87}
88
89inline LPSTR OwlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, unsigned int acp)
90{
91 // verify that no illegal character present
92 // since lpa was allocated based on the size of lpw
93 // don't worry about the number of chars
94 lpa[0] = '\0';
95 WideCharToMultiByte(acp, 0, lpw, -1, lpa, nChars, nullptr, nullptr);
96 return lpa;
97}
99{
100 return OwlA2WHelper(lpw, lpa, nChars, CP_ACP);
101}
102
104{
105 return OwlW2AHelper(lpa, lpw, nChars, CP_ACP);
106}
107
108# ifdef BI_CONVERSION_USES_THREAD_LOCALE
109# ifdef OWLA2WHELPER
110# undef OWLA2WHELPER
111# undef OWLW2AHELPER
112# endif
113# define OWLA2WHELPER OwlA2WHelper
114# define OWLW2AHELPER OwlW2AHelper
115# else
116# ifndef OWLA2WHELPER
117# define OWLA2WHELPER OwlA2WHelper
118# define OWLW2AHELPER OwlW2AHelper
119# endif
120# endif
121
122
123# if defined(BI_CONVERSION_USES_THREAD_LOCALE)
124# if !defined(A2W)
125# define A2W(lpa) (\
126 ((_lpa = lpa) == NULL) ? NULL : (\
127 _convert = (lstrlenA(_lpa)+1),\
128 OWLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert, _acp)))
129# endif
130# if !defined(A2WB)
131# define A2WB(lpa,buf) (\
132 ((_lpa = lpa) == NULL) ? NULL : (\
133 _convert = (lstrlenA(_lpa)+1),\
134 OWLA2WHELPER((LPWSTR) buf, _lpa, _convert, _acp)))
135# endif
136# else
137# if !defined(A2W)
138# define A2W(lpa) (\
139 ((_lpa = lpa) == NULL) ? NULL : (\
140 _convert = (lstrlenA(_lpa)+1),\
141 OWLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert)))
142# endif
143# if !defined(A2WB)
144# define A2WB(lpa,buf) (\
145 ((_lpa = lpa) == NULL) ? NULL : (\
146 _convert = (lstrlenA(_lpa)+1),\
147 OWLA2WHELPER((LPWSTR) buf, _lpa, _convert)))
148# endif
149# endif
150
151# ifdef BI_CONVERSION_USES_THREAD_LOCALE
152# if !defined(W2A)
153# define W2A(lpw) (\
154 ((_lpw = lpw) == NULL) ? NULL : (\
155 _convert = (lstrlenW(_lpw)+1)*2,\
156 OWLW2AHELPER((LPSTR) alloca(_convert), _lpw, _convert, _acp)))
157# endif
158# if !defined(W2AB)
159# define W2AB(lpw,buf) (\
160 ((_lpw = lpw) == NULL) ? NULL : (\
161 _convert = (lstrlenW(_lpw)+1)*2,\
162 OWLW2AHELPER((LPSTR) buf, _lpw, _convert, _acp)))
163# endif
164# else
165# if !defined(W2A)
166# define W2A(lpw) (\
167 ((_lpw = lpw) == NULL) ? NULL : (\
168 _convert = (lstrlenW(_lpw)+1)*2,\
169 OWLW2AHELPER((LPSTR) alloca(_convert), _lpw, _convert)))
170# endif
171# if !defined(W2AB)
172# define W2AB(lpw,buf) (\
173 ((_lpw = lpw) == NULL) ? NULL : (\
174 _convert = (lstrlenW(_lpw)+1)*2,\
175 OWLW2AHELPER((LPSTR) buf, _lpw, _convert)))
176# endif
177# endif
178
179# if !defined(A2CW)
180# define A2CW(lpa) ((LPCWSTR)A2W(lpa))
181# endif
182# if !defined(W2CA)
183# define W2CA(lpw) ((LPCSTR)W2A(lpw))
184# endif
185# if !defined(A2CWB)
186# define A2CWB(lpa,buf) ((LPCWSTR)A2WB(lpa,buf))
187# endif
188# if !defined(W2CAB)
189# define W2CAB(lpw,buf) ((LPCSTR)W2AB(lpw,buf))
190# endif
191
192
193//////////////////////////////
194// Usefull macros
195// _USES_CONVERSION -> set variables if UNICODE
196// _USES_CONVERSION_A -> set variables if not UNICODE
197// _W2A(lpw) -> W to A convert if UNICODE
198// _A2W(lpw) -> A to W convert if UNICODE
199// _W2AB(lpw,buf) -> W to A convert if UNICODE using buffer
200// _A2WB(lpw,buf) -> A to W convert if UNICODE using buffer
201// _W2A_A(lpw) -> W to A convert if not UNICODE
202// _A2W_A(lpw) -> A to W convert if not UNICODE
203// _W2A_AB(lpw,buf) -> W to A convert if not UNICODE using buffer
204// _A2W_AB(lpw,buf) -> A to W convert if not UNICODE using buffer
205#if defined(UNICODE)
206# define _USES_CONVERSION USES_CONVERSION
207# define _USES_CONVERSION_A
208# define _W2A(lpw) W2A(lpw)
209# define _A2W(lpw) A2W(lpw)
210# define _W2AB(lpw,buf) W2AB(lpw,buf)
211# define _A2WB(lpw,buf) A2WB(lpw,buf)
212# define _W2A_A(lpw) lpw
213# define _A2W_A(lpw) lpw
214# define _W2A_AB(lpw,buf) lpw
215# define _A2W_AB(lpw,buf) lpw
216#else
217# define _USES_CONVERSION
218# define _USES_CONVERSION_A USES_CONVERSION
219# define _W2A(lpw) lpw
220# define _A2W(lpw) lpw
221# define _W2AB(lpw,buf) lpw
222# define _A2WB(lpw,buf) lpw
223# define _W2A_A(lpw) W2A(lpw)
224# define _A2W_A(lpw) A2W(lpw)
225# define _W2A_AB(lpw,buf) W2AB(lpw,buf)
226# define _A2W_AB(lpw,buf) A2WB(lpw,buf)
227#endif
228
229namespace owl {
230
231#include <owl/preclass.h>
232
233/// \addtogroup utility
234/// @{
235
236//
237/// \class TCharIterator
238// ~~~~~ ~~~~~~~~~~~~~
239//
240template<class T> class TCharIterator {
241 public:
242 TCharIterator(T* p);
243
244 T* Next() const;
245 T* Current() const;
246
247 T* operator ++(); // prefix
248 T* operator ++(int); // postfix
249
250 operator T*() const;
251
252 protected:
253 T* P;
254};
255
256//
257/// \class TBidirCharIterator
258// ~~~~~ ~~~~~~~~~~~~~~~~~~
259//
260template<class T> class TBidirCharIterator : public TCharIterator<T> {
261 public:
262 TBidirCharIterator(T* begin, T* p);
263
264 T* Prev() const;
265
266 T* operator --(); // prefix
267 T* operator --(int); // postfix
268
269 private:
270 T* Begin;
271};
272
273
274//
275/// \class TTmpBufferBase
276// ~~~~~ ~~~~~~~~~~~~~~
277/// Static temporary fixed buffer, provides fast temporary buffer, for use in functions
278//
279/// For example:
280/// \code
281/// TTmpBuffer<_TCHAR> buffer(MAX_PATH);
282/// GetWindowDirectory(buffer,MAX_PATH);
283/// \endcode
284//
286 protected:
287 TTmpBufferBase(int size);
289
290 protected:
291 void* Buffer;
292 int Index;
293
294 private:
296 TTmpBufferBase& operator=(const TTmpBufferBase& buff);
297 void* operator new(size_t) noexcept; // prohibit use of new
298};
299//
300template<class T> class TTmpBuffer: public TTmpBufferBase {
301 public:
302 TTmpBuffer(int size) : TTmpBufferBase(size*sizeof(T)){}
303
304 T& operator *() {return *static_cast<T*>(Buffer);}
305 operator T*() {return static_cast<T*>(Buffer);}
306 bool operator !() const {return Buffer==0;}
307 T& operator[](int i) {return (static_cast<T*>(Buffer))[i];}
308};
309
310/// @}
311
312#include <owl/posclass.h>
313
314//
315template<class T> inline TCharIterator<T>::TCharIterator(T* p)
316: P(p)
317{
318}
319
320
321//
322template<class T> inline T* TCharIterator<T>::Next() const
323{
324#if defined(BI_DBCS_SUPPORT)
325 return *P ? ::AnsiNext(P) : P + 1;
326#else
327 return P + 1;
328#endif
329}
330
331
332//
333template<class T> inline T* TCharIterator<T>::Current() const
334{
335 return P;
336}
337
338//
339template<class T> inline T* TCharIterator<T>::operator ++()
340{
341 return P = Next();
342}
343
344//
345template<class T> inline T* TCharIterator<T>::operator ++(int)
346{
347 T* p = P;
348 P = Next();
349 return p;
350}
351
352//
353template<class T> inline TCharIterator<T>::operator T*() const
354{
355 return P;
356}
357//
358template<class T> inline TBidirCharIterator<T>::TBidirCharIterator(T* begin, T* p)
359:
360 TCharIterator<T>(p), Begin(begin)
361{
362}
363
364//
365template<class T> inline T* TBidirCharIterator<T>::Prev() const
366{
367#if defined(BI_DBCS_SUPPORT)
368 return ::AnsiPrev(Begin, this->P);
369#else
370 return this->P > Begin ? this->P - 1 : this->P;
371#endif
372}
373
374//
375template<class T> inline T* TBidirCharIterator<T>::operator --() // prefix
376{
377 return this->P = Prev();
378}
379
380//
381template<class T> inline T* TBidirCharIterator<T>::operator --(int) // postfix
382{
383 T* p = this->P;
384 this->P = Prev();
385 return p;
386}
387
388} // OWL namespace
389
390
391//
392// Type overloaded version of Window's huge mem copy (hmemcpy) for flat memory
393// models.
394//
395// inline void hmemcpy(void* d, const void* s, long n) {memcpy(d, s, n);}
396
397//
398// A C++ version of strdup(), strnewdup(). Uses new char[], to allow duplicated
399// strings to be deleted[].
400// Also a string version of atol for near data models
401//
402_OWLFUNC(char*) strnewdup(const char* s, size_t minAllocSize = 0);
403# define nstrnewdup strnewdup
404
405# if !defined(_WCHAR_T_DEFINED)
406 typedef unsigned short wchar_t;
407# define _WCHAR_T_DEFINED
408# endif
409
410_OWLFUNC(wchar_t*) strnewdup(const wchar_t* s, size_t minAllocSize = 0);
411
412#endif // OWL_PRIVATE_MEMORY_H
TBidirCharIterator(T *begin, T *p)
Definition memory.h:358
T * Current() const
Definition memory.h:333
T * Next() const
Definition memory.h:322
TCharIterator(T *p)
Definition memory.h:315
Static temporary fixed buffer, provides fast temporary buffer, for use in functions.
Definition memory.h:285
T & operator[](int i)
Definition memory.h:307
bool operator!() const
Definition memory.h:306
TTmpBuffer(int size)
Definition memory.h:302
T & operator*()
Definition memory.h:304
#define _tcscmp
Definition cygwin.h:75
#define AnsiNext
Definition cygwin.h:61
#define _tcsicmp
Definition cygwin.h:76
Reliable platform independent header for common memory and string functions.
UINT CharSize(const TCHAR *s)
Return the number of bytes of the first character of the passed string.
Definition memory.h:56
LPSTR OwlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, unsigned int acp)
Definition memory.h:89
TCHAR CharUpper(TCHAR c)
Definition memory.h:58
TCHAR CharLower(TCHAR c)
Definition memory.h:59
unsigned short wchar_t
Definition memory.h:406
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
LPWSTR OwlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars, unsigned int acp)
Definition memory.h:79
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
#define _OWLFUNC(p)
Definition defs.h:341
#define _OWLCLASS
Definition defs.h:338
Definition of class TString, a flexible universal string envelope class.