00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"
00061 #endif
00062 #ifndef COMSTL_INCL_H_COMSTL_MEMORY_FUNCTIONS
00063 # include "comstl_memory_functions.h"
00064 #endif
00065 #include <wchar.h>
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #ifndef _COMSTL_NO_NAMESPACE
00085 # if defined(_STLSOFT_NO_NAMESPACE) || \
00086 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00087
00088 namespace comstl
00089 {
00090 # else
00091
00092
00093 namespace stlsoft
00094 {
00095
00096 namespace comstl_project
00097 {
00098
00099 # endif
00100 #endif
00101
00102
00103
00108
00109
00110
00111
00112
00117 inline LPOLESTR olestring_create_a(cs_char_a_t const *s)
00118 {
00119 LPOLESTR posz;
00120
00121 if(s == NULL)
00122 {
00123 posz = NULL;
00124 }
00125 else
00126 {
00127 cs_size_t cch = ::MultiByteToWideChar(0, 0, s, -1, NULL, 0);
00128
00129 posz = static_cast<LPOLESTR>(::CoTaskMemAlloc(sizeof(OLECHAR) * (cch + 1)));
00130
00131 if(posz != NULL)
00132 {
00133 ::MultiByteToWideChar(0, 0, s, -1, posz, (cch + 1));
00134 }
00135 }
00136
00137 return posz;
00138 }
00139
00144 inline LPOLESTR olestring_create_w(cs_char_w_t const *s)
00145 {
00146 LPOLESTR posz;
00147
00148 if(s == NULL)
00149 {
00150 posz = NULL;
00151 }
00152 else
00153 {
00154 cs_size_t cch = ::wcslen(s);
00155
00156 posz = static_cast<LPOLESTR>(::CoTaskMemAlloc(sizeof(OLECHAR) * (cch + 1)));
00157
00158 if(posz != NULL)
00159 {
00160 ::wcscpy(posz, s);
00161 }
00162 }
00163
00164 return posz;
00165 }
00166
00171 inline LPOLESTR olestring_create(cs_char_a_t const *s)
00172 {
00173 return olestring_create_a(s);
00174 }
00175
00180 inline LPOLESTR olestring_create(cs_char_w_t const *s)
00181 {
00182 return olestring_create_w(s);
00183 }
00184
00188 inline void olestring_destroy(LPOLESTR posz)
00189 {
00190 comstl_message_assert("Invalid request to destroy non-COM string", CoTaskMemDidAlloc(posz) != 0);
00191
00192 ::CoTaskMemFree(static_cast<LPVOID>(posz));
00193 }
00194
00199 inline LPOLESTR olestring_dup(LPCOLESTR posz)
00200 {
00201 return olestring_create(posz);
00202 }
00203
00205
00206
00207 #ifdef STLSOFT_UNITTEST
00208
00209 namespace unittest
00210 {
00211 ss_bool_t test_comstl_olestring_functions(unittest_reporter *r)
00212 {
00213 using stlsoft::unittest::unittest_initialiser;
00214
00215 ss_bool_t bSuccess = true;
00216
00217 unittest_initialiser init(r, "COMSTL", "olestring_functions", __FILE__);
00218
00219 LPOLESTR str1 = olestring_create("Hello, Sailor!");
00220 LPOLESTR str2 = olestring_create(L"Hello, Sailor!");
00221
00222 if( NULL != str1 &&
00223 NULL != str2 &&
00224 0 != wcscmp(str1, str2))
00225 {
00226 r->report("OLE string (ANSI + Unicode) creation failed ", __LINE__);
00227 bSuccess = false;
00228 }
00229
00230 LPOLESTR str3 = olestring_dup(str1);
00231
00232 if( NULL != str1 &&
00233 NULL != str3 &&
00234 0 != wcscmp(str1, str3))
00235 {
00236 r->report("OLE string duplication failed ", __LINE__);
00237 bSuccess = false;
00238 }
00239
00240 olestring_destroy(str3);
00241 olestring_destroy(str2);
00242 olestring_destroy(str1);
00243
00244 return bSuccess;
00245 }
00246
00247 unittest_registrar unittest_comstl_olestring_functions(test_comstl_olestring_functions);
00248
00249 }
00250
00251 #endif
00252
00253
00254
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 #endif
00268
00269
00270
00271
00272
00273