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  

winstl_windows_directory.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_windows_directory.h
00003  *
00004  * Purpose:     Simple class that gets, and makes accessible, the windows
00005  *              directory.
00006  *
00007  * Created:     10th December 2002
00008  * Updated:     11th September 2004
00009  *
00010  * Home:        http://stlsoft.org/
00011  *
00012  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions are met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice, this
00019  *   list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright notice,
00021  *   this list of conditions and the following disclaimer in the documentation
00022  *   and/or other materials provided with the distribution.
00023  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00024  *   any contributors may be used to endorse or promote products derived from
00025  *   this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * 
00040 
00041 
00045 
00046 #ifndef WINSTL_INCL_H_WINSTL_WINDOWS_DIRECTORY
00047 #define WINSTL_INCL_H_WINSTL_WINDOWS_DIRECTORY
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define WINSTL_VER_H_WINSTL_WINDOWS_DIRECTORY_MAJOR        2
00051 # define WINSTL_VER_H_WINSTL_WINDOWS_DIRECTORY_MINOR        0
00052 # define WINSTL_VER_H_WINSTL_WINDOWS_DIRECTORY_REVISION     1
00053 # define WINSTL_VER_H_WINSTL_WINDOWS_DIRECTORY_EDIT         35
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Includes
00058  */
00059 
00060 #ifndef WINSTL_INCL_H_WINSTL
00061 # include "winstl.h"                    // Include the WinSTL root header
00062 #endif /* !WINSTL_INCL_H_WINSTL */
00063 #ifndef WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS
00064 # include "winstl_filesystem_traits.h"  // filesystem_traits
00065 #endif /* !WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS */
00066 #ifndef WINSTL_INCL_H_WINSTL_STRING_ACCESS
00067 # include "winstl_string_access.h"                  // winstl::string_access
00068 #endif /* WINSTL_INCL_H_WINSTL_STRING_ACCESS */
00069 
00070 /* 
00071  * Namespace
00072  */
00073 
00074 #ifndef _WINSTL_NO_NAMESPACE
00075 # if defined(_STLSOFT_NO_NAMESPACE) || \
00076      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00077 /* There is no stlsoft namespace, so must define ::winstl */
00078 namespace winstl
00079 {
00080 # else
00081 /* Define stlsoft::winstl_project */
00082 
00083 namespace stlsoft
00084 {
00085 
00086 namespace winstl_project
00087 {
00088 
00089 # endif /* _STLSOFT_NO_NAMESPACE */
00090 #endif /* !_WINSTL_NO_NAMESPACE */
00091 
00092 /* 
00093 
00096 
00100 
00105 
00106 /* 
00107  * basic_windows_directory
00108  *
00109  * This class wraps the GetWindowsDirectory() API function, and effectively acts
00110  * as a C-string of its value.
00111  */
00112 
00117 template<   ss_typename_param_k C
00118 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00119         ,   ss_typename_param_k T = filesystem_traits<C>
00120 #else
00121         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00122 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00123         >
00124 class basic_windows_directory
00125 {
00126 public:
00128     typedef C                               char_type;
00130     typedef T                               traits_type;
00132     typedef basic_windows_directory<C, T>   class_type;
00134     typedef ws_size_t                       size_type;
00135 
00136 // Operations
00137 public:
00139     static size_type   get_path(ws_char_a_t *buffer, size_type cchBuffer);
00141     static size_type   get_path(ws_char_w_t *buffer, size_type cchBuffer);
00142 
00143 // Attributes
00144 public:
00146     char_type const *get_path() const;
00148     char_type const *c_str() const;
00150     size_type       length() const;
00151 
00152 // Conversions
00153 public:
00155     operator char_type const *() const
00156     {
00157         return get_path();
00158     }
00159 
00161 #ifdef STLSOFT_UNITTEST
00162 public:
00163 #else
00164 private:
00165 #endif /* STLSOFT_UNITTEST */
00166     ws_bool_t is_valid_() const;
00167 
00168 // Implementation
00169 private:
00170     struct Information
00171     {
00172         char_type   m_dir[_MAX_PATH];
00173         size_type   m_len;
00174 
00175         Information()
00176             : m_len(get_path(m_dir, winstl_num_elements(m_dir)))
00177         {}
00178     };
00179 
00180 #if !defined(__STLSOFT_COMPILER_IS_DMC)
00181     static Information const &get_information_()
00182     {
00183         static Information  s_info;
00184 
00185         return s_info;
00186     }
00187 #else
00188     // Digital Mars gets an internal compiler error when the
00189     // preferred implementation is used, so we provide this
00190     // slightly less efficient implementation.
00191     Information m_information;
00192     Information const &get_information_() const
00193     {
00194         return m_information;
00195     }
00196 #endif /* __STLSOFT_COMPILER_IS_DMC */
00197 };
00198 
00199 /* 
00200  * Typedefs for commonly encountered types
00201  */
00202 
00204 typedef basic_windows_directory<ws_char_a_t, filesystem_traits<ws_char_a_t> >   windows_directory_a;
00206 typedef basic_windows_directory<ws_char_w_t, filesystem_traits<ws_char_w_t> >   windows_directory_w;
00208 typedef basic_windows_directory<TCHAR, filesystem_traits<TCHAR> >               windows_directory;
00209 
00210 /* 
00211  * Unit-testing
00212  */
00213 
00214 #ifdef STLSOFT_UNITTEST
00215 
00216 namespace unittest
00217 {
00218     ss_bool_t test_winstl_windows_directory(unittest_reporter *r)
00219     {
00220         using stlsoft::unittest::unittest_initialiser;
00221 
00222         ss_bool_t               bSuccess    =   true;
00223 
00224         unittest_initialiser    init(r, "WinSTL", "windows_directory", __FILE__);
00225 
00226         windows_directory_a win_dir_a_1;
00227         windows_directory_a win_dir_a_2;
00228         windows_directory_w win_dir_w_1;
00229         windows_directory_w win_dir_w_2;
00230         WCHAR               wszBuffer[_MAX_PATH];
00231 
00232         if( !win_dir_a_1.is_valid_() ||
00233             !win_dir_a_2.is_valid_() ||
00234             !win_dir_w_1.is_valid_() ||
00235             !win_dir_w_2.is_valid_())
00236         {
00237             r->report("Class invariant fails for one or more instances", __LINE__);
00238             bSuccess = false;
00239         }
00240 
00241         if(0 != lstrcmpiA(win_dir_a_1, win_dir_a_2))
00242         {
00243             r->report("Two (ANSI) instances do not report same Windows directory", __LINE__);
00244             bSuccess = false;
00245         }
00246 
00247         if(0 != lstrcmpiW(win_dir_w_1, win_dir_w_2))
00248         {
00249             r->report("Two (Unicode) instances do not report same Windows directory", __LINE__);
00250             bSuccess = false;
00251         }
00252 
00253         if( 0 == ::MultiByteToWideChar(0, 0, win_dir_a_1, -1, wszBuffer, stlsoft_num_elements(wszBuffer)) ||
00254             0 != lstrcmpiW(wszBuffer, win_dir_w_1))
00255         {
00256             r->report("ANSI and Unicode instances do not report same Windows directory", __LINE__);
00257             bSuccess = false;
00258         }
00259 
00260         return bSuccess;
00261     }
00262 
00263     unittest_registrar    unittest_winstl_windows_directory(test_winstl_windows_directory);
00264 
00265 } // namespace winstl_test
00266 
00267 #endif /* STLSOFT_UNITTEST */
00268 
00269 /* 
00270  * Implementation
00271  */
00272 
00273 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00274 
00275 template<   ss_typename_param_k C
00276         ,   ss_typename_param_k T
00277         >
00278 inline /* static */ ss_typename_type_k basic_windows_directory<C, T>::size_type basic_windows_directory<C, T>::get_path(ws_char_a_t *buffer, ss_typename_type_k basic_windows_directory<C, T>::size_type cchBuffer)
00279 {
00280     return static_cast<size_type>(::GetWindowsDirectoryA(buffer, cchBuffer));
00281 }
00282 
00283 template<   ss_typename_param_k C
00284         ,   ss_typename_param_k T
00285         >
00286 inline /* static */ ss_typename_type_k basic_windows_directory<C, T>::size_type basic_windows_directory<C, T>::get_path(ws_char_w_t *buffer, ss_typename_type_k basic_windows_directory<C, T>::size_type cchBuffer)
00287 {
00288     return static_cast<size_type>(::GetWindowsDirectoryW(buffer, cchBuffer));
00289 }
00290 
00291 template<   ss_typename_param_k C
00292         ,   ss_typename_param_k T
00293         >
00294 inline ss_typename_type_k basic_windows_directory<C, T>::char_type const *basic_windows_directory<C, T>::get_path() const
00295 {
00296 #if defined(__STLSOFT_COMPILER_IS_DMC)
00297     return get_information_().m_dir;
00298 #else
00299     static char_type const    *s_sz  =   get_information_().m_dir;
00300 
00301     return s_sz;
00302 #endif /* __STLSOFT_COMPILER_IS_DMC */
00303 }
00304 
00305 template<   ss_typename_param_k C
00306         ,   ss_typename_param_k T
00307         >
00308 inline ss_typename_type_k basic_windows_directory<C, T>::char_type const *basic_windows_directory<C, T>::c_str() const
00309 {
00310     return get_path();
00311 }
00312 
00313 template<   ss_typename_param_k C
00314         ,   ss_typename_param_k T
00315         >
00316 inline ss_typename_type_k basic_windows_directory<C, T>::size_type basic_windows_directory<C, T>::length() const
00317 {
00318     return get_information_().m_len;
00319 }
00320 
00321 template<   ss_typename_param_k C
00322         ,   ss_typename_param_k T
00323         >
00324 inline ws_bool_t basic_windows_directory<C, T>::is_valid_() const
00325 {
00326     ws_bool_t   bValid  =   true;
00327 
00328     if(traits_type::str_len(*this) != length())
00329     {
00330         return false;
00331     }
00332 
00333     if(0 == length())
00334     {
00335         return false;
00336     }
00337 
00338     return bValid;
00339 }
00340 
00341 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00342 
00343 /* 
00344 
00346 
00347 /* 
00348  * Shims
00349  */
00350 
00352 
00355 
00359 
00364 
00366 template<   ss_typename_param_k C
00367         ,   ss_typename_param_k T
00368         >
00369 inline C const *c_str_ptr_null(basic_windows_directory<C, T> const &sd)
00370 {
00371     return sd;
00372 }
00373 
00375 template<   ss_typename_param_k C
00376         ,   ss_typename_param_k T
00377         >
00378 inline C const *c_str_ptr(basic_windows_directory<C, T> const &sd)
00379 {
00380     return sd;
00381 }
00382 
00384 template<   ss_typename_param_k C
00385         ,   ss_typename_param_k T
00386         >
00387 inline ws_size_t c_str_len(basic_windows_directory<C, T> const &sd)
00388 {
00389     return sd.length();
00390 }
00391 
00393 template<   ss_typename_param_k C
00394         ,   ss_typename_param_k T
00395         >
00396 inline ws_size_t c_str_size(basic_windows_directory<C, T> const &sd)
00397 {
00398     return sd.length() * sizeof(C);
00399 }
00400 
00401 /* 
00402 
00404 
00405 /* 
00406 
00407 #ifndef _WINSTL_NO_NAMESPACE
00408 # if defined(_STLSOFT_NO_NAMESPACE) || \
00409      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00410 } // namespace winstl
00411 # else
00412 } // namespace winstl_project
00413 } // namespace stlsoft
00414 # endif /* _STLSOFT_NO_NAMESPACE */
00415 #endif /* !_WINSTL_NO_NAMESPACE */
00416 
00417 /* 
00418  * Namespace
00419  *
00420  * The string access shims exist either in the stlsoft namespace, or in the
00421  * global namespace. This is required by the lookup rules.
00422  *
00423  */
00424 
00425 #ifndef _WINSTL_NO_NAMESPACE
00426 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00427      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00428 namespace stlsoft
00429 {
00430 # else /* ? _STLSOFT_NO_NAMESPACE */
00431 /* There is no stlsoft namespace, so must define in the global namespace */
00432 # endif /* !_STLSOFT_NO_NAMESPACE */
00433 
00434 using ::winstl::c_str_ptr_null;
00435 
00436 using ::winstl::c_str_ptr;
00437 
00438 using ::winstl::c_str_len;
00439 
00440 using ::winstl::c_str_size;
00441 
00442 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00443      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00444 } // namespace stlsoft
00445 # else /* ? _STLSOFT_NO_NAMESPACE */
00446 /* There is no stlsoft namespace, so must define in the global namespace */
00447 # endif /* !_STLSOFT_NO_NAMESPACE */
00448 #endif /* !_WINSTL_NO_NAMESPACE */
00449 
00450 /* 
00451 
00452 #endif /* WINSTL_INCL_H_WINSTL_WINDOWS_DIRECTORY */
00453 
00454 /* 

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