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_bstr_functions.h

Go to the documentation of this file.
00001 /* 
00002  * File:        comstl_bstr_functions.h (originally MOBStrFn.h, ::SynesisCom)
00003  *
00004  * Purpose:     Contains classes and functions for dealing with BSTR strings.
00005  *
00006  * Created:
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-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_BSTR_FUNCTIONS
00046 #define COMSTL_INCL_H_COMSTL_BSTR_FUNCTIONS
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define COMSTL_VER_H_COMSTL_BSTR_FUNCTIONS_MAJOR       2
00050 # define COMSTL_VER_H_COMSTL_BSTR_FUNCTIONS_MINOR       0
00051 # define COMSTL_VER_H_COMSTL_BSTR_FUNCTIONS_REVISION    1
00052 # define COMSTL_VER_H_COMSTL_BSTR_FUNCTIONS_EDIT        42
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 STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER
00063 # include "stlsoft_auto_buffer.h"   // stlsoft::auto_buffer
00064 #endif /* !STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER */
00065 #ifndef COMSTL_INCL_H_COMSTL_TASK_ALLOCATOR
00066 # include "comstl_task_allocator.h" // task_allocator
00067 #endif /* !COMSTL_INCL_H_COMSTL_TASK_ALLOCATOR */
00068 #ifdef STLSOFT_UNITTEST
00069 # include <wchar.h>
00070 #endif /* STLSOFT_UNITTEST */
00071 
00072 /* 
00073  * Namespace
00074  *
00075  * The COMSTL components are contained within the comstl namespace. This is
00076  * actually an alias for stlsoft::comstl_project,
00077  *
00078  * The definition matrix is as follows:
00079  *
00080  * _STLSOFT_NO_NAMESPACE    _COMSTL_NO_NAMESPACE    comstl definition
00081  * ---------------------    --------------------    -----------------
00082  *  not defined              not defined             = stlsoft::comstl_project
00083  *  not defined              defined                 not defined
00084  *  defined                  not defined             comstl
00085  *  defined                  defined                 not defined
00086  *
00087  */
00088 
00089 #ifndef _COMSTL_NO_NAMESPACE
00090 # if defined(_STLSOFT_NO_NAMESPACE) || \
00091      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00092 /* There is no stlsoft namespace, so must define ::comstl */
00093 namespace comstl
00094 {
00095 # else
00096 /* Define stlsoft::comstl_project */
00097 
00098 namespace stlsoft
00099 {
00100 
00101 namespace comstl_project
00102 {
00103 
00104 # endif /* _STLSOFT_NO_NAMESPACE */
00105 #endif /* !_COMSTL_NO_NAMESPACE */
00106 
00107 /* 
00108 
00113 
00114 /* 
00115  * Functions
00116  */
00117 
00122 inline BSTR bstr_create_w(cs_char_w_t const *s)
00123 {
00124     return ::SysAllocString(s);
00125 }
00126 
00131 inline BSTR bstr_create_a(cs_char_a_t const *s)
00132 {
00133     BSTR    bstr;
00134 
00135     if(s == NULL)
00136     {
00137         bstr = NULL;
00138     }
00139     else
00140     {
00141         stlsoft_ns_qual(auto_buffer)<cs_char_w_t, task_allocator<cs_char_w_t>, 512> buffer(1 + strlen(s));
00142 
00143         ::MultiByteToWideChar(0, 0, s, -1, buffer, buffer.size());
00144 
00145         bstr = bstr_create_w(buffer);
00146     }
00147 
00148     return bstr;
00149 }
00150 
00155 inline BSTR bstr_create(cs_char_a_t const *s)
00156 {
00157     return bstr_create_a(s);
00158 }
00159 
00164 inline BSTR bstr_create(cs_char_w_t const *s)
00165 {
00166     return bstr_create_w(s);
00167 }
00168 
00172 inline void bstr_destroy(BSTR bstr)
00173 {
00174     ::SysFreeString(bstr);
00175 }
00176 
00181 inline BSTR bstr_dup(BSTR bstr)
00182 {
00183     return bstr_create(bstr);
00184 }
00185 
00187 // Unit-testing
00188 
00189 #ifdef STLSOFT_UNITTEST
00190 
00191 namespace unittest
00192 {
00193     ss_bool_t test_comstl_bstr_functions(unittest_reporter *r)
00194     {
00195         using stlsoft::unittest::unittest_initialiser;
00196 
00197         ss_bool_t               bSuccess    =   true;
00198 
00199         unittest_initialiser    init(r, "COMSTL", "bstr_functions", __FILE__);
00200 
00201         BSTR    bstr1   =   bstr_create("Hello, Sailor!");
00202         BSTR    bstr2   =   bstr_create(L"Hello, Sailor!");
00203 
00204         if( NULL != bstr1 &&
00205             NULL != bstr2 &&
00206             0 != wcscmp(bstr1, bstr2))
00207         {
00208             r->report("BSTR (ANSI + Unicode) creation failed ", __LINE__);
00209             bSuccess = false;
00210         }
00211 
00212         bstr_destroy(bstr1);
00213         bstr_destroy(bstr2);
00214 
00215         return bSuccess;
00216     }
00217 
00218     unittest_registrar    unittest_comstl_bstr_functions(test_comstl_bstr_functions);
00219 
00220 } // namespace unittest
00221 
00222 #endif /* STLSOFT_UNITTEST */
00223 
00224 /* 
00225 
00227 
00228 /* 
00229 
00230 #ifndef _COMSTL_NO_NAMESPACE
00231 # if defined(_STLSOFT_NO_NAMESPACE) || \
00232      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00233 } // namespace comstl
00234 # else
00235 } // namespace comstl_project
00236 } // namespace stlsoft
00237 # endif /* _STLSOFT_NO_NAMESPACE */
00238 #endif /* !_COMSTL_NO_NAMESPACE */
00239 
00240 /* 
00241 
00242 #endif /* !COMSTL_INCL_H_COMSTL_BSTR_FUNCTIONS */
00243 
00244 /* 

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