c14eccc20a43f7c39a6fa10ec1468233963ea864
[reactos.git] / sdk / lib / atl / cstringt.h
1 #ifndef __CSTRINGT_H__
2 #define __CSTRINGT_H__
3
4 #pragma once
5 #include <atlsimpstr.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <wchar.h>
9 #include <atlmem.h>
10
11 namespace ATL
12 {
13
14 inline UINT WINAPI _AtlGetConversionACP() throw()
15 {
16 #ifdef _CONVERSION_DONT_USE_THREAD_LOCALE
17 return CP_ACP;
18 #else
19 return CP_THREAD_ACP;
20 #endif
21 }
22
23
24 template<typename _CharType = wchar_t>
25 class ChTraitsCRT : public ChTraitsBase<_CharType>
26 {
27 public:
28
29 static int __cdecl GetBaseTypeLength(_In_z_ LPCWSTR pszSource) throw()
30 {
31 return ::WideCharToMultiByte(_AtlGetConversionACP(), 0, pszSource, -1, NULL, 0, NULL, NULL) - 1;
32 }
33
34 static int __cdecl GetBaseTypeLength(
35 _In_reads_(nLength) LPCWSTR pszSource,
36 _In_ int nLength) throw()
37 {
38 return ::WideCharToMultiByte(_AtlGetConversionACP(), 0, pszSource, nLength, NULL, 0, NULL, NULL);
39 }
40
41 static void __cdecl ConvertToBaseType(
42 _Out_writes_(nDestLength) LPWSTR pszDest,
43 _In_ int nDestLength,
44 _In_ LPCWSTR pszSrc,
45 _In_ int nSrcLength = -1)
46 {
47 if (nSrcLength == -1)
48 nSrcLength = 1 + GetBaseTypeLength(pszSrc);
49
50 wmemcpy(pszDest, pszSrc, nSrcLength);
51 }
52 };
53
54
55
56 namespace _CSTRING_IMPL_
57 {
58 template <typename _CharType, class StringTraits>
59 struct _MFCDLLTraitsCheck
60 {
61 const static bool c_bIsMFCDLLTraits = false;
62 };
63 }
64
65
66 template <typename BaseType, class StringTraits>
67 class CStringT :
68 public CSimpleStringT <BaseType, _CSTRING_IMPL_::_MFCDLLTraitsCheck<BaseType, StringTraits>::c_bIsMFCDLLTraits>
69 {
70 public:
71 typedef CSimpleStringT<BaseType, _CSTRING_IMPL_::_MFCDLLTraitsCheck<BaseType, StringTraits>::c_bIsMFCDLLTraits> CThisSimpleString;
72 typedef StringTraits StrTraits;
73 typedef typename CThisSimpleString::XCHAR XCHAR;
74 typedef typename CThisSimpleString::PXSTR PXSTR;
75 typedef typename CThisSimpleString::PCXSTR PCXSTR;
76 typedef typename CThisSimpleString::YCHAR YCHAR;
77 typedef typename CThisSimpleString::PYSTR PYSTR;
78 typedef typename CThisSimpleString::PCYSTR PCYSTR;
79
80 public:
81 CStringT() throw() :
82 CThisSimpleString(StringTraits::GetDefaultManager())
83 {
84 }
85
86 explicit CStringT( _In_ IAtlStringMgr* pStringMgr) throw() :
87 CThisSimpleString(pStringMgr)
88 {
89 }
90
91 static void __cdecl Construct(_In_ CStringT* pString)
92 {
93 pString = new CStringT;
94 }
95
96 CStringT(_In_ const CStringT& strSrc) :
97 CThisSimpleString(strSrc)
98 {
99 }
100
101 CStringT(_In_opt_z_ const XCHAR* pszSrc) :
102 CThisSimpleString( StringTraits::GetDefaultManager() )
103 {
104 // FIXME: Check whether pszSrc is not a resource string ID!
105 *this = pszSrc;
106 }
107
108 CStringT(
109 _In_opt_z_ const XCHAR* pszSrc,
110 _In_ IAtlStringMgr* pStringMgr) :
111 CThisSimpleString( pStringMgr )
112 {
113 // FIXME: Check whether pszSrc is not a resource string ID!
114 *this = pszSrc;
115 }
116
117 CStringT& operator=(_In_ const CStringT& strSrc)
118 {
119 CThisSimpleString::operator=(strSrc);
120 return *this;
121 }
122
123 CStringT& operator=(_In_opt_z_ PCXSTR pszSrc)
124 {
125 CThisSimpleString::operator=(pszSrc);
126 return *this;
127 }
128
129 CStringT& operator+=(_In_ const CThisSimpleString& str)
130 {
131 CThisSimpleString::operator+=(str);
132 return *this;
133 }
134
135 CStringT& operator+=(_In_z_ PCXSTR pszSrc)
136 {
137 CThisSimpleString::operator+=(pszSrc);
138 return *this;
139 }
140
141 _Check_return_ BOOL LoadString(_In_ HINSTANCE hInstance,
142 _In_ UINT nID)
143 {
144 const ATLSTRINGRESOURCEIMAGE* pImage = AtlGetStringResourceImage(hInstance, nID);
145 if (pImage == NULL) return FALSE;
146
147 int nLength = StringTraits::GetBaseTypeLength(pImage->achString, pImage->nLength);
148 PXSTR pszBuffer = CThisSimpleString::GetBuffer(nLength);
149 StringTraits::ConvertToBaseType(pszBuffer, nLength, pImage->achString, pImage->nLength);
150 CThisSimpleString::ReleaseBufferSetLength(nLength);
151
152 return TRUE;
153 }
154 };
155
156 } //namespace ATL
157
158 #endif