STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

comstl_olestring_functions.h

Go to the documentation of this file.
00001 /* 
00002  * File:        comstl_olestring_functions.h (originally MOOleStr.h, ::SynesisCom)
00003  *
00004  * Purpose:     Contains classes and functions for dealing with OLE/COM strings.
00005  *
00006  * Created:     2nd March 1996
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 1996-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef COMSTL_INCL_H_COMSTL_OLESTRING_FUNCTIONS
00046 #define COMSTL_INCL_H_COMSTL_OLESTRING_FUNCTIONS
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define COMSTL_VER_H_COMSTL_OLESTRING_FUNCTIONS_MAJOR      3
00050 # define COMSTL_VER_H_COMSTL_OLESTRING_FUNCTIONS_MINOR      0
00051 # define COMSTL_VER_H_COMSTL_OLESTRING_FUNCTIONS_REVISION   1
00052 # define COMSTL_VER_H_COMSTL_OLESTRING_FUNCTIONS_EDIT       125
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"                     // Include the COMSTL root header
00061 #endif /* !COMSTL_INCL_H_COMSTL */
00062 #ifndef COMSTL_INCL_H_COMSTL_MEMORY_FUNCTIONS
00063 # include "comstl_memory_functions.h"    //
00064 #endif /* !COMSTL_INCL_H_COMSTL_MEMORY_FUNCTIONS */
00065 #include <wchar.h>
00066 
00067 /* 
00068  * Namespace
00069  *
00070  * The COMSTL components are contained within the comstl namespace. This is
00071  * actually an alias for stlsoft::comstl_project,
00072  *
00073  * The definition matrix is as follows:
00074  *
00075  * _STLSOFT_NO_NAMESPACE    _COMSTL_NO_NAMESPACE    comstl definition
00076  * ---------------------    --------------------    -----------------
00077  *  not defined              not defined             = stlsoft::comstl_project
00078  *  not defined              defined                 not defined
00079  *  defined                  not defined             comstl
00080  *  defined                  defined                 not defined
00081  *
00082  */
00083 
00084 #ifndef _COMSTL_NO_NAMESPACE
00085 # if defined(_STLSOFT_NO_NAMESPACE) || \
00086      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00087 /* There is no stlsoft namespace, so must define ::comstl */
00088 namespace comstl
00089 {
00090 # else
00091 /* Define stlsoft::comstl_project */
00092 
00093 namespace stlsoft
00094 {
00095 
00096 namespace comstl_project
00097 {
00098 
00099 # endif /* _STLSOFT_NO_NAMESPACE */
00100 #endif /* !_COMSTL_NO_NAMESPACE */
00101 
00102 /* 
00103 
00108 
00109 /* 
00110  * Functions
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 // Unit-testing
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 } // namespace unittest
00250 
00251 #endif /* STLSOFT_UNITTEST */
00252 
00253 /* 
00254 
00256 
00257 /* 
00258 
00259 #ifndef _COMSTL_NO_NAMESPACE
00260 # if defined(_STLSOFT_NO_NAMESPACE) || \
00261      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00262 } // namespace comstl
00263 # else
00264 } // namespace comstl_project
00265 } // namespace stlsoft
00266 # endif /* _STLSOFT_NO_NAMESPACE */
00267 #endif /* !_COMSTL_NO_NAMESPACE */
00268 
00269 /* 
00270 
00271 #endif /* !COMSTL_INCL_H_COMSTL_OLESTRING_FUNCTIONS */
00272 
00273 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004