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  

unixstl_current_directory_scope.h

Go to the documentation of this file.
00001 /* 
00002  * File:        unixstl_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 UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE
00046 #define UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE_MAJOR        3
00050 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE_MINOR        0
00051 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE_REVISION     1
00052 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE_EDIT         73
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef UNIXSTL_INCL_H_WINSTL
00060 # include "unixstl.h"                   // Include the UNIXSTL root header
00061 #endif /* !UNIXSTL_INCL_H_WINSTL */
00062 #ifndef UNIXSTL_INCL_H_UNIXSTL_FILESYSTEM_TRAITS
00063 # include "unixstl_filesystem_traits.h" // filesystem_traits
00064 #endif /* !UNIXSTL_INCL_H_UNIXSTL_FILESYSTEM_TRAITS */
00065 #ifndef UNIXSTL_INCL_H_UNIXSTL_FILE_PATH_BUFFER
00066 # include "unixstl_file_path_buffer.h"  // basic_file_path_buffer
00067 #endif /* !UNIXSTL_INCL_H_UNIXSTL_FILE_PATH_BUFFER */
00068 #ifndef STLSOFT_INCL_H_STLSOFT_STRING_ACCESS
00069 # include "stlsoft_string_access.h"     // stlsoft::c_str_ptr
00070 #endif /* !STLSOFT_INCL_H_STLSOFT_STRING_ACCESS */
00071 #ifndef UNIXSTL_INCL_H_UNIXSTL_STRING_ACCESS
00072 # include "unixstl_string_access.h"     // unixstl::c_str_ptr
00073 #endif /* !UNIXSTL_INCL_H_UNIXSTL_STRING_ACCESS */
00074 #ifndef STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL
00075 # include "stlsoft_operator_bool.h"     // operator_bool_generator
00076 #endif /* !STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL */
00077 
00078 /* 
00079  * Namespace
00080  */
00081 
00082 #ifndef _UNIXSTL_NO_NAMESPACE
00083 # if defined(_STLSOFT_NO_NAMESPACE) || \
00084      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00085 /* There is no stlsoft namespace, so must define ::unixstl */
00086 namespace unixstl
00087 {
00088 # else
00089 /* Define stlsoft::unixstl_project */
00090 
00091 namespace stlsoft
00092 {
00093 
00094 namespace unixstl_project
00095 {
00096 
00097 # endif /* _STLSOFT_NO_NAMESPACE */
00098 #endif /* !_UNIXSTL_NO_NAMESPACE */
00099 
00100 #if !defined(__STLSOFT_COMPILER_IS_GCC) && \
00101     __GNUC__ < 3
00102 stlsoft_ns_using(c_str_ptr)
00103 #endif /* !gcc or gcc >= 3 */
00104 
00105 /* 
00106 
00109 
00113 
00118 
00119 /* 
00120  * basic_current_directory_scope
00121  *
00122  * This class pushes the given directory as the current directory upon
00123  * construction, and pops back to the original at destruction.
00124  */
00125 
00134 
00135 template<   ss_typename_param_k C
00136 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00137         ,   ss_typename_param_k T = filesystem_traits<C>
00138 #else
00139         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00140 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00141         >
00142 class basic_current_directory_scope
00143 {
00144 public:
00145     typedef C                                       char_type;  
00146 private:
00147     typedef T                                       traits_type;
00148     typedef basic_current_directory_scope<C, T>     class_type;
00149 
00150 // Construction
00151 public:
00155     ss_explicit_k basic_current_directory_scope(char_type const *dir);
00156 #if defined(__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
00160     template <ss_typename_param_k S>
00161     ss_explicit_k basic_current_directory_scope(S const &dir)
00162     {
00163         init_(c_str_ptr(dir));
00164     }
00165 #endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00166 
00167     ~basic_current_directory_scope() unixstl_throw_0();
00168 
00169 // Attributes
00170 public:
00172     char_type const *get_previous() const;
00173 
00174 // Conversions
00175 public:
00177     operator char_type const *() const;
00178 
00181 private:
00182     STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, operator_bool_generator_type, operator_bool_type);
00183 public:
00188     operator operator_bool_type() const
00189     {
00190         return operator_bool_generator_type::translate('\0' != m_previous[0]);
00191     }
00192 
00194 
00195 // Implementation
00196 private:
00197     void init_(char_type const *dir);
00198 
00199 // Members
00200 private:
00201     basic_file_path_buffer<char_type>   m_previous;
00202 
00203 // Not to be implemented
00204 private:
00205     basic_current_directory_scope();
00206     basic_current_directory_scope(class_type const &);
00207     class_type const &operator =(class_type const &);
00208 };
00209 
00210 /* 
00211  * Typedefs for commonly encountered types
00212  */
00213 
00215 typedef basic_current_directory_scope<us_char_a_t, filesystem_traits<us_char_a_t> >     current_directory_scope_a;
00217 typedef basic_current_directory_scope<us_char_w_t, filesystem_traits<us_char_w_t> >     current_directory_scope_w;
00218 
00219 /* 
00220  * Shims
00221  */
00222 
00223 template<   ss_typename_param_k C
00224         ,   ss_typename_param_k T
00225         >
00226 inline C const *c_str_ptr_null(basic_current_directory_scope<C, T> const &b)
00227 {
00228     return stlsoft_ns_qual(c_str_ptr_null)(b.c_str());
00229 }
00230 
00231 template<   ss_typename_param_k C
00232         ,   ss_typename_param_k T
00233         >
00234 inline C const *c_str_ptr(basic_current_directory_scope<C, T> const &b)
00235 {
00236     return stlsoft_ns_qual(c_str_ptr)(b.c_str());
00237 }
00238 
00239 template<   ss_typename_param_k C
00240         ,   ss_typename_param_k T
00241         >
00242 inline us_size_t c_str_len(basic_current_directory_scope<C, T> const &b)
00243 {
00244     return stlsoft_ns_qual(c_str_len)(b.c_str());
00245 }
00246 
00247 template<   ss_typename_param_k C
00248         ,   ss_typename_param_k T
00249         >
00250 inline us_size_t c_str_size(basic_current_directory_scope<C, T> const &b)
00251 {
00252     return stlsoft_ns_qual(c_str_size)(b.c_str());
00253 }
00254 
00255 template<   ss_typename_param_k S
00256         ,   ss_typename_param_k C
00257         ,   ss_typename_param_k T
00258         >
00259 inline S &operator <<(S & s, basic_current_directory_scope<C, T> const &b)
00260 {
00261     s << b.c_str();
00262 
00263     return s;
00264 }
00265 
00267 // Unit-testing
00268 
00269 #ifdef STLSOFT_UNITTEST
00270 
00271 namespace unittest
00272 {
00273     ss_bool_t test_unixstl_current_directory_scope(unittest_reporter *r)
00274     {
00275         using stlsoft::unittest::unittest_initialiser;
00276 
00277         ss_bool_t               bSuccess    =   true;
00278 
00279         unittest_initialiser    init(r, "UNIXSTL", "current_directory_scope", __FILE__);
00280 
00281         typedef basic_current_directory_scope<char>     current_directory_scope;
00282 
00283         {
00284             basic_file_path_buffer<char>    sz;
00285 
00286             getcwd(&sz[0], sz.size());
00287 #ifdef WIN32
00288             current_directory_scope cds("\\");
00289 #else /* ? WIN32 */
00290             current_directory_scope cds("/");
00291 #endif /* WIN32 */
00292 
00293             if(0 != strcmp(sz.c_str(), cds.get_previous()))
00294             {
00295                 r->report("current_directory_scope failed ", __LINE__);
00296                 bSuccess = false;
00297             }
00298 
00299             {
00300                 current_directory_scope cds2(sz);
00301 
00302 #ifdef WIN32
00303                 if(0 != strcmp("\\", cds2.get_previous() + 2))
00304 #else /* ? WIN32 */
00305                 if(0 != strcmp("/", cds2.get_previous()))
00306 #endif /* WIN32 */
00307                 {
00308                     r->report("current_directory_scope failed ", __LINE__);
00309                     bSuccess = false;
00310                 }
00311             }
00312         }
00313 
00314         return bSuccess;
00315     }
00316 
00317     unittest_registrar    unittest_unixstl_current_directory_scope(test_unixstl_current_directory_scope);
00318 
00319 } // namespace unittest
00320 
00321 #endif /* STLSOFT_UNITTEST */
00322 
00323 /* 
00324  * Implementation
00325  */
00326 
00327 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00328 
00329 template<   ss_typename_param_k C
00330         ,   ss_typename_param_k T
00331         >
00332 inline void basic_current_directory_scope<C, T>::init_(ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *dir)
00333 {
00334     if( 0 == traits_type::get_current_directory(m_previous.size(), &m_previous[0]) ||
00335         !traits_type::set_current_directory(dir))
00336     {
00337         m_previous[0] = '\0';
00338     }
00339 }
00340 
00341 template<   ss_typename_param_k C
00342         ,   ss_typename_param_k T
00343         >
00344 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)
00345 {
00346     init_(dir);
00347 }
00348 
00349 template<   ss_typename_param_k C
00350         ,   ss_typename_param_k T
00351         >
00352 inline basic_current_directory_scope<C, T>::~basic_current_directory_scope() unixstl_throw_0()
00353 {
00354     if('\0' != m_previous[0])
00355     {
00356         traits_type::set_current_directory(&m_previous[0]);
00357     }
00358 }
00359 
00360 template<   ss_typename_param_k C
00361         ,   ss_typename_param_k T
00362         >
00363 inline ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *basic_current_directory_scope<C, T>::get_previous() const
00364 {
00365 #if defined(__STLSOFT_COMPILER_IS_GCC) && \
00366     __GNUC__ < 3
00367     return m_previous.c_str();
00368 #else /* ? __GNUC__ < 3 */
00369     return stlsoft_ns_qual(c_str_ptr)(m_previous);
00370 #endif /* ? __GNUC__ < 3 */
00371 }
00372 
00373 template<   ss_typename_param_k C
00374         ,   ss_typename_param_k T
00375         >
00376 #if defined(__STLSOFT_COMPILER_IS_GCC)
00377 inline basic_current_directory_scope<C, T>::operator C const *() const
00378 #else /* ? compiler */
00379 inline basic_current_directory_scope<C, T>::operator ss_typename_type_k basic_current_directory_scope<C, T>::char_type const *() const
00380 #endif /* compiler */
00381 {
00382     return get_previous();
00383 }
00384 
00385 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00386 
00387 /* 
00388 
00390 
00391 /* 
00392 
00393 #ifndef _UNIXSTL_NO_NAMESPACE
00394 # if defined(_STLSOFT_NO_NAMESPACE) || \
00395      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00396 } // namespace unixstl
00397 # else
00398 } // namespace stlsoft::unixstl_project
00399 } // namespace stlsoft
00400 # endif /* _STLSOFT_NO_NAMESPACE */
00401 #endif /* !_UNIXSTL_NO_NAMESPACE */
00402 
00403 /* 
00404  * Namespace
00405  *
00406  * The string access shims exist either in the stlsoft namespace, or in the
00407  * global namespace. This is required by the lookup rules.
00408  *
00409  */
00410 
00411 #ifndef _UNIXSTL_NO_NAMESPACE
00412 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00413      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00414 namespace stlsoft
00415 {
00416 # else /* ? _STLSOFT_NO_NAMESPACE */
00417 /* There is no stlsoft namespace, so must define in the global namespace */
00418 # endif /* !_STLSOFT_NO_NAMESPACE */
00419 
00420 using ::unixstl::c_str_ptr_null;
00421 
00422 using ::unixstl::c_str_ptr;
00423 
00424 using ::unixstl::c_str_len;
00425 
00426 using ::unixstl::c_str_size;
00427 
00428 # if !defined(_STLSOFT_NO_NAMESPACE) && \
00429      !defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00430 } // namespace stlsoft
00431 # else /* ? _STLSOFT_NO_NAMESPACE */
00432 /* There is no stlsoft namespace, so must define in the global namespace */
00433 # endif /* !_STLSOFT_NO_NAMESPACE */
00434 #endif /* !_UNIXSTL_NO_NAMESPACE */
00435 
00436 /* 
00437 
00438 #endif /* UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY_SCOPE */
00439 
00440 /* 

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