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

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_current_directory.h
00003  *
00004  * Purpose:     Simple class that gets, and makes accessible, the current
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_CURRENT_DIRECTORY
00047 #define WINSTL_INCL_H_WINSTL_CURRENT_DIRECTORY
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_MAJOR    2
00051 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_MINOR    0
00052 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_REVISION 1
00053 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_EDIT     37
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Compatibility
00058  */
00059 
00060 /*
00061 [Incompatibilies-start]
00062 __STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
00063 [Incompatibilies-end]
00064  */
00065 
00066 /* 
00067  * Includes
00068  */
00069 
00070 #ifndef WINSTL_INCL_H_WINSTL
00071 # include "winstl.h"                    // Include the WinSTL root header
00072 #endif /* !WINSTL_INCL_H_WINSTL */
00073 
00074 #if defined(__STLSOFT_COMPILER_IS_MSVC) && \
00075     _MSC_VER < 1200
00076 # error winstl_file_path_buffer.h is not compatible with Visual C++ 5.0 or earlier
00077 #endif /* _MSC_VER < 1200 */
00078 
00079 #ifndef WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS
00080 # include "winstl_filesystem_traits.h"  // filesystem_traits
00081 #endif /* !WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS */
00082 #ifndef WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER
00083 # include "winstl_file_path_buffer.h"   // basic_file_path_buffer
00084 #endif /* !WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER */
00085 #ifndef WINSTL_INCL_H_WINSTL_STRING_ACCESS
00086 # include "winstl_string_access.h"                  // winstl::string_access
00087 #endif /* WINSTL_INCL_H_WINSTL_STRING_ACCESS */
00088 
00089 /* 
00090  * Namespace
00091  */
00092 
00093 #ifndef _WINSTL_NO_NAMESPACE
00094 # if defined(_STLSOFT_NO_NAMESPACE) || \
00095      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00096 /* There is no stlsoft namespace, so must define ::winstl */
00097 namespace winstl
00098 {
00099 # else
00100 /* Define stlsoft::winstl_project */
00101 
00102 namespace stlsoft
00103 {
00104 
00105 namespace winstl_project
00106 {
00107 
00108 # endif /* _STLSOFT_NO_NAMESPACE */
00109 #endif /* !_WINSTL_NO_NAMESPACE */
00110 
00111 /* 
00112 
00115 
00119 
00124 
00125 /* 
00126  * basic_current_directory
00127  *
00128  * This class wraps the GetCurrentDirectory() API function, and effectively acts
00129  * as a C-string of its value.
00130  */
00131 
00136 template<   ss_typename_param_k C
00137 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00138         ,   ss_typename_param_k T = filesystem_traits<C>
00139 #else
00140         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00141 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00142         >
00143 class basic_current_directory
00144 {
00145 public:
00147     typedef C                               char_type;
00149     typedef T                               traits_type;
00151     typedef basic_current_directory<C, T>   class_type;
00153     typedef ws_size_t                       size_type;
00154 
00155 // Construction
00156 public:
00158     basic_current_directory();
00159 
00160 // Operations
00161 public:
00163     static size_type   get_path(ws_char_a_t *buffer, size_type cchBuffer);
00165     static size_type   get_path(ws_char_w_t *buffer, size_type cchBuffer);
00166 
00167 // Attributes
00168 public:
00170     char_type const *get_path() const;
00172     char_type const *c_str() const;
00174     size_type       length() const;
00175 
00176 // Conversions
00177 public:
00179     operator char_type const *() const
00180     {
00181         return get_path();
00182     }
00183 
00184 // Members
00185 private:
00186     basic_file_path_buffer<char_type>   m_dir;
00187     size_type const                     m_len;
00188 
00189 // Not to be implemented
00190 private:
00191     basic_current_directory(const class_type &);
00192     basic_current_directory &operator =(const class_type &);
00193 };
00194 
00195 /* 
00196  * Typedefs for commonly encountered types
00197  */
00198 
00200 typedef basic_current_directory<ws_char_a_t, filesystem_traits<ws_char_a_t> >     current_directory_a;
00202 typedef basic_current_directory<ws_char_w_t, filesystem_traits<ws_char_w_t> >     current_directory_w;
00204 typedef basic_current_directory<TCHAR, filesystem_traits<TCHAR> >                 current_directory;
00205 
00206 /* 
00207  * Shims
00208  */
00209 
00210 template<   ss_typename_param_k C
00211         ,   ss_typename_param_k T
00212         >
00213 inline C const *c_str_ptr_null(basic_current_directory<C, T> const &b)
00214 {
00215     return stlsoft_ns_qual(c_str_ptr_null)(b.c_str());
00216 }
00217 
00218 template<   ss_typename_param_k C
00219         ,   ss_typename_param_k T
00220         >
00221 inline C const *c_str_ptr(basic_current_directory<C, T> const &b)
00222 {
00223     return stlsoft_ns_qual(c_str_ptr)(b.c_str());
00224 }
00225 
00226 template<   ss_typename_param_k C
00227         ,   ss_typename_param_k T
00228         >
00229 inline ws_size_t c_str_len(basic_current_directory<C, T> const &b)
00230 {
00231     return stlsoft_ns_qual(c_str_len)(b.c_str());
00232 }
00233 
00234 template<   ss_typename_param_k C
00235         ,   ss_typename_param_k T
00236         >
00237 inline ws_size_t c_str_size(basic_current_directory<C, T> const &b)
00238 {
00239     return stlsoft_ns_qual(c_str_size)(b.c_str());
00240 }
00241 
00242 template<   ss_typename_param_k S
00243         ,   ss_typename_param_k C
00244         ,   ss_typename_param_k T
00245         >
00246 inline S &operator <<(S & s, basic_current_directory<C, T> const &b)
00247 {
00248     s << b.c_str();
00249 
00250     return s;
00251 }
00252 
00253 /* 
00254  * Unit-testing
00255  */
00256 
00257 #ifdef STLSOFT_UNITTEST
00258 
00259 namespace unittest
00260 {
00261     ss_bool_t test_winstl_current_directory(unittest_reporter *r)
00262     {
00263         using stlsoft::unittest::unittest_initialiser;
00264 
00265         ss_bool_t               bSuccess    =   true;
00266 
00267         unittest_initialiser    init(r, "WinSTL", "current_directory", __FILE__);
00268 
00269         typedef basic_current_directory<char>   curr_dir_t;
00270 
00271         char        sz[1 + _MAX_PATH];
00272         curr_dir_t  cd;
00273 
00274         ::GetCurrentDirectoryA(stlsoft_num_elements(sz), sz);
00275 
00276         if(0 != strcmp(sz, cd))
00277         {
00278             r->report("basic_current_directory<char> failed ", __LINE__);
00279             bSuccess = false;
00280         }
00281 
00282         return bSuccess;
00283     }
00284 
00285     unittest_registrar    unittest_winstl_current_directory(test_winstl_current_directory);
00286 
00287 } // namespace unittest
00288 
00289 #endif /* STLSOFT_UNITTEST */
00290 
00291 /* 
00292  * Implementation
00293  */
00294 
00295 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00296 
00297 template<   ss_typename_param_k C
00298         ,   ss_typename_param_k T
00299         >
00300 inline basic_current_directory<C, T>::basic_current_directory()
00301     : m_len(get_path(&m_dir[0], m_dir.size()))
00302 {}
00303 
00304 template<   ss_typename_param_k C
00305         ,   ss_typename_param_k T
00306         >
00307 inline /* static */ ss_typename_type_k basic_current_directory<C, T>::size_type basic_current_directory<C, T>::get_path(ws_char_a_t *buffer, ss_typename_type_k basic_current_directory<C, T>::size_type cchBuffer)
00308 {
00309     return static_cast<size_type>(::GetCurrentDirectoryA(cchBuffer, buffer));
00310 }
00311 
00312 template<   ss_typename_param_k C
00313         ,   ss_typename_param_k T
00314         >
00315 inline /* static */ ss_typename_type_k basic_current_directory<C, T>::size_type basic_current_directory<C, T>::get_path(ws_char_w_t *buffer, ss_typename_type_k basic_current_directory<C, T>::size_type cchBuffer)
00316 {
00317     return static_cast<size_type>(::GetCurrentDirectoryW(cchBuffer, buffer));
00318 }
00319 
00320 template<   ss_typename_param_k C
00321         ,   ss_typename_param_k T
00322         >
00323 inline ss_typename_type_k basic_current_directory<C, T>::char_type const *basic_current_directory<C, T>::get_path() const
00324 {
00325     return stlsoft_ns_qual(c_str_ptr)(m_dir);
00326 }
00327 
00328 template<   ss_typename_param_k C
00329         ,   ss_typename_param_k T
00330         >
00331 inline ss_typename_type_k basic_current_directory<C, T>::char_type const *basic_current_directory<C, T>::c_str() const
00332 {
00333     return get_path();
00334 }
00335 
00336 template<   ss_typename_param_k C
00337         ,   ss_typename_param_k T
00338         >
00339 inline ss_typename_type_k basic_current_directory<C, T>::size_type basic_current_directory<C, T>::length() const
00340 {
00341     return m_len;
00342 }
00343 
00344 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00345 
00346 /* 
00347 
00349 
00350 /* 
00351 
00352 #ifndef _WINSTL_NO_NAMESPACE
00353 # if defined(_STLSOFT_NO_NAMESPACE) || \
00354      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00355 } // namespace winstl
00356 # else
00357 } // namespace winstl_project
00358 } // namespace stlsoft
00359 # endif /* _STLSOFT_NO_NAMESPACE */
00360 #endif /* !_WINSTL_NO_NAMESPACE */
00361 
00362 /* 
00363  * Namespace
00364  *
00365  * The string access shims exist either in the stlsoft namespace, or in the
00366  * global namespace. This is required by the lookup rules.
00367  *
00368  */
00369 
00370 #ifndef _WINSTL_NO_NAMESPACE
00371 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00372      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00373 namespace stlsoft
00374 {
00375 # else /* ? _STLSOFT_NO_NAMESPACE */
00376 /* There is no stlsoft namespace, so must define in the global namespace */
00377 # endif /* !_STLSOFT_NO_NAMESPACE */
00378 
00379 using ::winstl::c_str_ptr_null;
00380 
00381 using ::winstl::c_str_ptr;
00382 
00383 using ::winstl::c_str_len;
00384 
00385 using ::winstl::c_str_size;
00386 
00387 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00388      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00389 } // namespace stlsoft
00390 # else /* ? _STLSOFT_NO_NAMESPACE */
00391 /* There is no stlsoft namespace, so must define in the global namespace */
00392 # endif /* !_STLSOFT_NO_NAMESPACE */
00393 #endif /* !_WINSTL_NO_NAMESPACE */
00394 
00395 /* 
00396 
00397 #endif /* WINSTL_INCL_H_WINSTL_CURRENT_DIRECTORY */
00398 
00399 /* 

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