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

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_system_directory.h
00003  *
00004  * Purpose:     Simple class that gets, and makes accessible, the system
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_SYSTEM_DIRECTORY
00047 #define WINSTL_INCL_H_WINSTL_SYSTEM_DIRECTORY
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define WINSTL_VER_H_WINSTL_SYSTEM_DIRECTORY_MAJOR     2
00051 # define WINSTL_VER_H_WINSTL_SYSTEM_DIRECTORY_MINOR     0
00052 # define WINSTL_VER_H_WINSTL_SYSTEM_DIRECTORY_REVISION  1
00053 # define WINSTL_VER_H_WINSTL_SYSTEM_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 #ifndef STLSOFT_INCL_H_STLSOFT_CONSTRAINTS
00070 # include "stlsoft_constraints.h"       // stlsoft::not_implicitly_comparable
00071 #endif /* !STLSOFT_INCL_H_STLSOFT_CONSTRAINTS */
00072 
00073 /* 
00074  * Namespace
00075  */
00076 
00077 #ifndef _WINSTL_NO_NAMESPACE
00078 # if defined(_STLSOFT_NO_NAMESPACE) || \
00079      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00080 /* There is no stlsoft namespace, so must define ::winstl */
00081 namespace winstl
00082 {
00083 # else
00084 /* Define stlsoft::winstl_project */
00085 
00086 namespace stlsoft
00087 {
00088 
00089 namespace winstl_project
00090 {
00091 
00092 # endif /* _STLSOFT_NO_NAMESPACE */
00093 #endif /* !_WINSTL_NO_NAMESPACE */
00094 
00095 /* 
00096 
00099 
00103 
00108 
00109 /* 
00110  * basic_system_directory
00111  *
00112  * This class wraps the GetSystemDirectory() API function, and effectively acts
00113  * as a C-string of its value.
00114  */
00115 
00120 template<   ss_typename_param_k C
00121 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00122         ,   ss_typename_param_k T = filesystem_traits<C>
00123 #else
00124         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00125 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00126         >
00127 class basic_system_directory
00128 {
00129 public:
00131     typedef C                       char_type;
00133     typedef T                       traits_type;
00135     typedef basic_system_directory<C, T>  class_type;
00137     typedef ws_size_t               size_type;
00138 
00139 // Operations
00140 public:
00142     static size_type   get_path(ws_char_a_t *buffer, size_type cchBuffer);
00144     static size_type   get_path(ws_char_w_t *buffer, size_type cchBuffer);
00145 
00146 // Attributes
00147 public:
00149     char_type const *get_path() const;
00151     char_type const *c_str() const;
00153     size_type       length() const;
00154 
00155 // Conversions
00156 public:
00158     operator char_type const *() const
00159     {
00160         return get_path();
00161     }
00162 
00163 // Implementation
00164 private:
00165     struct Information
00166     {
00167         char_type   m_dir[_MAX_PATH];
00168         size_type   m_len;
00169 
00170         Information()
00171             : m_len(get_path(m_dir, winstl_num_elements(m_dir)))
00172         {}
00173     };
00174 
00175 #if !defined(__STLSOFT_COMPILER_IS_DMC)
00176     static Information const &get_information_()
00177     {
00178         static Information  s_info;
00179 
00180         return s_info;
00181     }
00182 #else
00183     // Digital Mars gets an internal compiler error when the
00184     // preferred implementation is used, so we provide this
00185     // slightly less efficient implementation.
00186     Information m_information;
00187     Information const &get_information_() const
00188     {
00189         return m_information;
00190     }
00191 #endif /* __STLSOFT_COMPILER_IS_DMC */
00192 };
00193 
00194 /* 
00195  * Typedefs for commonly encountered types
00196  */
00197 
00199 typedef basic_system_directory<ws_char_a_t, filesystem_traits<ws_char_a_t> >    system_directory_a;
00201 typedef basic_system_directory<ws_char_w_t, filesystem_traits<ws_char_w_t> >    system_directory_w;
00203 typedef basic_system_directory<TCHAR, filesystem_traits<TCHAR> >                system_directory;
00204 
00205 /* 
00206  * Implementation
00207  */
00208 
00209 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00210 
00211 template<   ss_typename_param_k C
00212         ,   ss_typename_param_k T
00213         >
00214 inline /* static */ ss_typename_type_k basic_system_directory<C, T>::size_type basic_system_directory<C, T>::get_path(ws_char_a_t *buffer, ss_typename_type_k basic_system_directory<C, T>::size_type cchBuffer)
00215 {
00216     return static_cast<size_type>(::GetSystemDirectoryA(buffer, cchBuffer));
00217 }
00218 
00219 template<   ss_typename_param_k C
00220         ,   ss_typename_param_k T
00221         >
00222 inline /* static */ ss_typename_type_k basic_system_directory<C, T>::size_type basic_system_directory<C, T>::get_path(ws_char_w_t *buffer, ss_typename_type_k basic_system_directory<C, T>::size_type cchBuffer)
00223 {
00224     return static_cast<size_type>(::GetSystemDirectoryW(buffer, cchBuffer));
00225 }
00226 
00227 template<   ss_typename_param_k C
00228         ,   ss_typename_param_k T
00229         >
00230 inline ss_typename_type_k basic_system_directory<C, T>::char_type const *basic_system_directory<C, T>::get_path() const
00231 {
00232 #if defined(__STLSOFT_COMPILER_IS_DMC)
00233     return get_information_().m_dir;
00234 #else
00235     static char_type const    *s_sz  =   get_information_().m_dir;
00236 
00237     return s_sz;
00238 #endif /* __STLSOFT_COMPILER_IS_DMC */
00239 }
00240 
00241 template<   ss_typename_param_k C
00242         ,   ss_typename_param_k T
00243         >
00244 inline ss_typename_type_k basic_system_directory<C, T>::char_type const *basic_system_directory<C, T>::c_str() const
00245 {
00246     return get_path();
00247 }
00248 
00249 template<   ss_typename_param_k C
00250         ,   ss_typename_param_k T
00251         >
00252 inline ss_typename_type_k basic_system_directory<C, T>::size_type basic_system_directory<C, T>::length() const
00253 {
00254     return get_information_().m_len;
00255 }
00256 
00257 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00258 
00260 // Unit-testing
00261 
00262 #ifdef STLSOFT_UNITTEST
00263 
00264 namespace unittest
00265 {
00266     ss_bool_t test_winstl_system_directory(unittest_reporter *r)
00267     {
00268         using stlsoft::unittest::unittest_initialiser;
00269 
00270         ss_bool_t               bSuccess    =   true;
00271 
00272         unittest_initialiser    init(r, "WinSTL", "system_directory", __FILE__);
00273 
00274 #if 0
00275         if(<<TODO>>)
00276         {
00277             r->report("<<TODO>> failed ", __LINE__);
00278             bSuccess = false;
00279         }
00280 #endif /* 0 */
00281 
00282         return bSuccess;
00283     }
00284 
00285     unittest_registrar    unittest_winstl_system_directory(test_winstl_system_directory);
00286 
00287 } // namespace unittest
00288 
00289 #endif /* STLSOFT_UNITTEST */
00290 
00291 /* 
00292 
00294 
00295 /* 
00296  * Shims
00297  */
00298 
00300 
00303 
00307 
00312 
00314 template<   ss_typename_param_k C
00315         ,   ss_typename_param_k T
00316         >
00317 inline C const *c_str_ptr_null(basic_system_directory<C, T> const &sd)
00318 {
00319     return sd;
00320 }
00321 
00323 template<   ss_typename_param_k C
00324         ,   ss_typename_param_k T
00325         >
00326 inline C const *c_str_ptr(basic_system_directory<C, T> const &sd)
00327 {
00328     return sd;
00329 }
00330 
00332 template<   ss_typename_param_k C
00333         ,   ss_typename_param_k T
00334         >
00335 inline ws_size_t c_str_len(basic_system_directory<C, T> const &sd)
00336 {
00337     return sd.length();
00338 }
00339 
00341 template<   ss_typename_param_k C
00342         ,   ss_typename_param_k T
00343         >
00344 inline ws_size_t c_str_size(basic_system_directory<C, T> const &sd)
00345 {
00346     return sd.length() * sizeof(C);
00347 }
00348 
00349 /* 
00350 
00352 
00353 /* 
00354 
00355 #ifndef _WINSTL_NO_NAMESPACE
00356 # if defined(_STLSOFT_NO_NAMESPACE) || \
00357      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00358 } // namespace winstl
00359 # else
00360 } // namespace winstl_project
00361 } // namespace stlsoft
00362 # endif /* _STLSOFT_NO_NAMESPACE */
00363 #endif /* !_WINSTL_NO_NAMESPACE */
00364 
00365 /* 
00366  * Namespace
00367  *
00368  * The string access shims exist either in the stlsoft namespace, or in the
00369  * global namespace. This is required by the lookup rules.
00370  *
00371  */
00372 
00373 #ifndef _WINSTL_NO_NAMESPACE
00374 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00375      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00376 namespace stlsoft
00377 {
00378 # else /* ? _STLSOFT_NO_NAMESPACE */
00379 /* There is no stlsoft namespace, so must define in the global namespace */
00380 # endif /* !_STLSOFT_NO_NAMESPACE */
00381 
00382 using ::winstl::c_str_ptr_null;
00383 
00384 using ::winstl::c_str_ptr;
00385 
00386 using ::winstl::c_str_len;
00387 
00388 using ::winstl::c_str_size;
00389 
00390 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00391      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00392 } // namespace stlsoft
00393 # else /* ? _STLSOFT_NO_NAMESPACE */
00394 /* There is no stlsoft namespace, so must define in the global namespace */
00395 # endif /* !_STLSOFT_NO_NAMESPACE */
00396 #endif /* !_WINSTL_NO_NAMESPACE */
00397 
00398 /* 
00399 
00400 #endif /* WINSTL_INCL_H_WINSTL_SYSTEM_DIRECTORY */
00401 
00402 /* 

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