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

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_current_directory_scope.h (formerly MLPwdScp.h, ::SynesisStd)
00003  *
00004  * Purpose:     Current working directory scoping class.
00005  *
00006  * Created:     12th November 1998
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 1998-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 WINSTL_INCL_H_WINSTL_CURRENT_DIRECTORY_SCOPE
00046 #define WINSTL_INCL_H_WINSTL_CURRENT_DIRECTORY_SCOPE
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_SCOPE_MAJOR      3
00050 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_SCOPE_MINOR      0
00051 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_SCOPE_REVISION   1
00052 # define WINSTL_VER_H_WINSTL_CURRENT_DIRECTORY_SCOPE_EDIT       80
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Compatibility
00057  */
00058 
00059 /*
00060 [Incompatibilies-start]
00061 __STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
00062 [Incompatibilies-end]
00063  */
00064 
00065 /* 
00066  * Includes
00067  */
00068 
00069 #ifndef WINSTL_INCL_H_WINSTL
00070 # include "winstl.h"                    // Include the WinSTL root header
00071 #endif /* !WINSTL_INCL_H_WINSTL */
00072 
00073 #if defined(__STLSOFT_COMPILER_IS_MSVC) && \
00074     _MSC_VER < 1200
00075 # error winstl_file_path_buffer.h is not compatible with Visual C++ 5.0 or earlier
00076 #endif /* _MSC_VER < 1200 */
00077 
00078 #ifndef WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS
00079 # include "winstl_filesystem_traits.h"  // filesystem_traits
00080 #endif /* !WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS */
00081 #ifndef STLSOFT_INCL_H_STLSOFT_STRING_ACCESS
00082 # include "stlsoft_string_access.h"     // stlsoft::c_str_ptr
00083 #endif /* !STLSOFT_INCL_H_STLSOFT_STRING_ACCESS */
00084 #ifndef WINSTL_INCL_H_WINSTL_STRING_ACCESS
00085 # include "winstl_string_access.h"      // winstl::c_str_ptr
00086 #endif /* !WINSTL_INCL_H_WINSTL_STRING_ACCESS */
00087 #ifndef WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER
00088 # include "winstl_file_path_buffer.h"   // basic_file_path_buffer
00089 #endif /* !WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER */
00090 #ifndef STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL
00091 # include "stlsoft_operator_bool.h"     // operator_bool_generator
00092 #endif /* !STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL */
00093 
00094 /* 
00095  * Namespace
00096  */
00097 
00098 #ifndef _WINSTL_NO_NAMESPACE
00099 # if defined(_STLSOFT_NO_NAMESPACE) || \
00100      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00101 /* There is no stlsoft namespace, so must define ::winstl */
00102 namespace winstl
00103 {
00104 # else
00105 /* Define stlsoft::winstl_project */
00106 
00107 namespace stlsoft
00108 {
00109 
00110 namespace winstl_project
00111 {
00112 
00113 # endif /* _STLSOFT_NO_NAMESPACE */
00114 #endif /* !_WINSTL_NO_NAMESPACE */
00115 
00116 #if !defined(__STLSOFT_COMPILER_IS_MWERKS)
00117 stlsoft_ns_using(c_str_ptr)
00118 #endif /* compiler */
00119 
00120 /* 
00121 
00124 
00128 
00133 
00134 /* 
00135  * basic_current_directory_scope
00136  *
00137  * This class pushes the given directory as the current directory upon
00138  * construction, and pops back to the original at destruction.
00139  */
00140 
00149 
00150 template<   ss_typename_param_k C
00151 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00152         ,   ss_typename_param_k T = filesystem_traits<C>
00153 #else
00154         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00155 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00156         >
00157 class basic_current_directory_scope
00158 {
00159 public:
00160     typedef C                               char_type;
00161     typedef T                               traits_type;
00162     typedef basic_current_directory_scope<C, T>   class_type;
00163     typedef ws_size_t                       size_type;
00164 
00165 // Construction
00166 public:
00170     ss_explicit_k basic_current_directory_scope(char_type const *dir);
00171 #if defined(__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
00175     template <ss_typename_param_k S>
00176     ss_explicit_k basic_current_directory_scope(S const &dir)
00177     {
00178         init_(stlsoft_ns_qual(c_str_ptr)(dir));
00179     }
00180 #endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00181 
00182     ~basic_current_directory_scope() winstl_throw_0();
00183 
00184 // Attributes
00185 public:
00187     char_type const *get_previous() const;
00188 
00189 // Conversions
00190 public:
00192     operator char_type const *() const;
00193 
00196 private:
00197     STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, operator_bool_generator_type, operator_bool_type);
00198 public:
00203     operator operator_bool_type() const
00204     {
00205         return operator_bool_generator_type::translate('\0' != m_previous[0]);
00206     }
00207 
00209 
00210 // Implementation
00211 private:
00212     void init_(char_type const *dir);
00213 
00214 // Members
00215 private:
00216     basic_file_path_buffer<char_type>   m_previous;
00217 
00218 // Not to be implemented
00219 private:
00220     basic_current_directory_scope();
00221     basic_current_directory_scope(class_type const &);
00222     class_type const &operator =(class_type const &);
00223 };
00224 
00225 /* 
00226  * Typedefs for commonly encountered types
00227  */
00228 
00230 typedef basic_current_directory_scope<ws_char_a_t, filesystem_traits<ws_char_a_t> >     current_directory_scope_a;
00232 typedef basic_current_directory_scope<ws_char_w_t, filesystem_traits<ws_char_w_t> >     current_directory_scope_w;
00234 typedef basic_current_directory_scope<TCHAR, filesystem_traits<TCHAR> >                 current_directory_scope;
00235 
00236 /* 
00237  * Shims
00238  */
00239 
00240 template<   ss_typename_param_k C
00241         ,   ss_typename_param_k T
00242         >
00243 inline C const *c_str_ptr_null(basic_current_directory_scope<C, T> const &b)
00244 {
00245     return stlsoft_ns_qual(c_str_ptr_null)(b.c_str());
00246 }
00247 
00248 template<   ss_typename_param_k C
00249         ,   ss_typename_param_k T
00250         >
00251 inline C const *c_str_ptr(basic_current_directory_scope<C, T> const &b)
00252 {
00253     return stlsoft_ns_qual(c_str_ptr)(b.c_str());
00254 }
00255 
00256 template<   ss_typename_param_k C
00257         ,   ss_typename_param_k T
00258         >
00259 inline ws_size_t c_str_len(basic_current_directory_scope<C, T> const &b)
00260 {
00261     return stlsoft_ns_qual(c_str_len)(b.c_str());
00262 }
00263 
00264 template<   ss_typename_param_k C
00265         ,   ss_typename_param_k T
00266         >
00267 inline ws_size_t c_str_size(basic_current_directory_scope<C, T> const &b)
00268 {
00269     return stlsoft_ns_qual(c_str_size)(b.c_str());
00270 }
00271 
00272 template<   ss_typename_param_k S
00273         ,   ss_typename_param_k C
00274         ,   ss_typename_param_k T
00275         >
00276 inline S &operator <<(S & s, basic_current_directory_scope<C, T> const &b)
00277 {
00278     s << b.c_str();
00279 
00280     return s;
00281 }
00282 
00284 // Unit-testing
00285 
00286 #ifdef STLSOFT_UNITTEST
00287 
00288 namespace unittest
00289 {
00290     ss_bool_t test_winstl_current_directory_scope(unittest_reporter *r)
00291     {
00292         using stlsoft::unittest::unittest_initialiser;
00293 
00294         ss_bool_t               bSuccess    =   true;
00295 
00296         unittest_initialiser    init(r, "WinSTL", "current_directory_scope", __FILE__);
00297 
00298         typedef basic_current_directory_scope<char>     current_directory_scope;
00299 
00300         {
00301             char    szCwd[1 + _MAX_PATH];
00302             char    szRoot[1 + _MAX_PATH];
00303             char    *lpFilePart;
00304 
00305             ::GetCurrentDirectoryA(stlsoft_num_elements(szCwd), szCwd);
00306             ::GetFullPathNameA("\\", stlsoft_num_elements(szRoot), szRoot, &lpFilePart);
00307 
00308             current_directory_scope cds(szRoot);
00309 
00310             if(0 != strcmp(szCwd, cds.get_previous()))
00311             {
00312                 r->report("current_directory_scope failed ", __LINE__);
00313                 bSuccess = false;
00314             }
00315 
00316             {
00317                 current_directory_scope cds2(szCwd);
00318 
00319                 if(0 != strcmp(szRoot, cds2.get_previous()))
00320                 {
00321                     r->report("current_directory_scope failed ", __LINE__);
00322                     bSuccess = false;
00323                 }
00324             }
00325         }
00326 
00327         return bSuccess;
00328     }
00329 
00330     unittest_registrar    unittest_winstl_current_directory_scope(test_winstl_current_directory_scope);
00331 
00332 } // namespace unittest
00333 
00334 #endif /* STLSOFT_UNITTEST */
00335 
00336 /* 
00337  * Implementation
00338  */
00339 
00340 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00341 
00342 template<   ss_typename_param_k C
00343         ,   ss_typename_param_k T
00344         >
00345 inline void basic_current_directory_scope<C, T>::init_(ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *dir)
00346 {
00347     if( 0 == traits_type::get_current_directory(m_previous.size(), &m_previous[0]) ||
00348         !traits_type::set_current_directory(dir))
00349     {
00350         m_previous[0] = '\0';
00351     }
00352 }
00353 
00354 template<   ss_typename_param_k C
00355         ,   ss_typename_param_k T
00356         >
00357 inline basic_current_directory_scope<C, T>::basic_current_directory_scope(ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *dir)
00358 {
00359     init_(stlsoft_ns_qual(c_str_ptr)(dir));
00360 }
00361 
00362 template<   ss_typename_param_k C
00363         ,   ss_typename_param_k T
00364         >
00365 inline basic_current_directory_scope<C, T>::~basic_current_directory_scope() winstl_throw_0()
00366 {
00367     if('\0' != m_previous[0])
00368     {
00369         traits_type::set_current_directory(&m_previous[0]);
00370     }
00371 }
00372 
00373 template<   ss_typename_param_k C
00374         ,   ss_typename_param_k T
00375         >
00376 inline ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *basic_current_directory_scope<C, T>::get_previous() const
00377 {
00378     return stlsoft_ns_qual(c_str_ptr)(m_previous);
00379 }
00380 
00381 template<   ss_typename_param_k C
00382         ,   ss_typename_param_k T
00383         >
00384 #if defined(__STLSOFT_COMPILER_IS_GCC)
00385 inline basic_current_directory_scope<C, T>::operator C const *() const
00386 #else /* ? compiler */
00387 inline basic_current_directory_scope<C, T>::operator ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *() const
00388 #endif /* compiler */
00389 {
00390     return get_previous();
00391 }
00392 
00393 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00394 
00395 /* 
00396 
00398 
00399 /* 
00400 
00401 #ifndef _WINSTL_NO_NAMESPACE
00402 # if defined(_STLSOFT_NO_NAMESPACE) || \
00403      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00404 } // namespace winstl
00405 # else
00406 } // namespace winstl_project
00407 } // namespace stlsoft
00408 # endif /* _STLSOFT_NO_NAMESPACE */
00409 #endif /* !_WINSTL_NO_NAMESPACE */
00410 
00411 /* 
00412  * Namespace
00413  *
00414  * The string access shims exist either in the stlsoft namespace, or in the
00415  * global namespace. This is required by the lookup rules.
00416  *
00417  */
00418 
00419 #ifndef _WINSTL_NO_NAMESPACE
00420 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00421      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00422 namespace stlsoft
00423 {
00424 # else /* ? _STLSOFT_NO_NAMESPACE */
00425 /* There is no stlsoft namespace, so must define in the global namespace */
00426 # endif /* !_STLSOFT_NO_NAMESPACE */
00427 
00428 using ::winstl::c_str_ptr_null;
00429 
00430 using ::winstl::c_str_ptr;
00431 
00432 using ::winstl::c_str_len;
00433 
00434 using ::winstl::c_str_size;
00435 
00436 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00437      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00438 } // namespace stlsoft
00439 # else /* ? _STLSOFT_NO_NAMESPACE */
00440 /* There is no stlsoft namespace, so must define in the global namespace */
00441 # endif /* !_STLSOFT_NO_NAMESPACE */
00442 #endif /* !_WINSTL_NO_NAMESPACE */
00443 
00444 /* 
00445 
00446 #endif /* WINSTL_INCL_H_WINSTL_CURRENT_DIRECTORY_SCOPE */
00447 
00448 /* 

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