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

Go to the documentation of this file.
00001 /* 
00002  * File:        unixstl_current_directory.h
00003  *
00004  * Purpose:     Simple class that gets, and makes accessible, the current
00005  *              directory.
00006  *
00007  * Created:     1st November 2003
00008  * Updated:     11th September 2004
00009  *
00010  * Home:        http://stlsoft.org/
00011  *
00012  * Copyright (c) 2003-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 UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY
00047 #define UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_MAJOR      2
00051 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_MINOR      0
00052 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_REVISION   1
00053 # define UNIXSTL_VER_H_UNIXSTL_CURRENT_DIRECTORY_EDIT       20
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Includes
00058  */
00059 
00060 #ifndef UNIXSTL_INCL_H_UNIXSTL
00061 # include "unixstl.h"                   // Include the WinSTL root header
00062 #endif /* !UNIXSTL_INCL_H_UNIXSTL */
00063 #ifndef UNIXSTL_INCL_H_UNIXSTL_FILESYSTEM_TRAITS
00064 # include "unixstl_filesystem_traits.h" // filesystem_traits
00065 #endif /* !UNIXSTL_INCL_H_UNIXSTL_FILESYSTEM_TRAITS */
00066 #ifndef UNIXSTL_INCL_H_UNIXSTL_FILE_PATH_BUFFER
00067 # include "unixstl_file_path_buffer.h"  // basic_file_path_buffer
00068 #endif /* !UNIXSTL_INCL_H_UNIXSTL_FILE_PATH_BUFFER */
00069 
00070 /* 
00071  * Namespace
00072  */
00073 
00074 #ifndef _UNIXSTL_NO_NAMESPACE
00075 # if defined(_STLSOFT_NO_NAMESPACE) || \
00076      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00077 /* There is no stlsoft namespace, so must define ::unixstl */
00078 namespace unixstl
00079 {
00080 # else
00081 /* Define stlsoft::unixstl_project */
00082 
00083 namespace stlsoft
00084 {
00085 
00086 namespace unixstl_project
00087 {
00088 
00089 # endif /* _STLSOFT_NO_NAMESPACE */
00090 #endif /* !_UNIXSTL_NO_NAMESPACE */
00091 
00092 /* 
00093 
00096 
00100 
00105 
00106 /* 
00107  * basic_current_directory
00108  *
00109  * This class wraps the GetCurrentDirectory() 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_current_directory
00125 {
00126 public:
00128     typedef C                               char_type;
00130     typedef T                               traits_type;
00132     typedef basic_current_directory<C, T>   class_type;
00134     typedef us_size_t                       size_type;
00135 
00136 // Construction
00137 public:
00139     basic_current_directory();
00140 
00141 // Operations
00142 public:
00144     static size_type   get_path(char_type *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 // Members
00164 private:
00165     basic_file_path_buffer<char_type>   m_dir;
00166     size_type const                     m_len;
00167 
00168 // Not to be implemented
00169 private:
00170     basic_current_directory(const class_type &);
00171     basic_current_directory &operator =(const class_type &);
00172 };
00173 
00174 /* 
00175  * Typedefs for commonly encountered types
00176  */
00177 
00179 typedef basic_current_directory<us_char_a_t, filesystem_traits<us_char_a_t> >     current_directory_a;
00181 typedef basic_current_directory<us_char_w_t, filesystem_traits<us_char_w_t> >     current_directory_w;
00182 
00183 /* 
00184  * Shims
00185  */
00186 
00187 template<   ss_typename_param_k C
00188         ,   ss_typename_param_k T
00189         >
00190 inline C const *c_str_ptr_null(basic_current_directory<C, T> const &b)
00191 {
00192     return stlsoft_ns_qual(c_str_ptr_null)(b.c_str());
00193 }
00194 
00195 template<   ss_typename_param_k C
00196         ,   ss_typename_param_k T
00197         >
00198 inline C const *c_str_ptr(basic_current_directory<C, T> const &b)
00199 {
00200     return stlsoft_ns_qual(c_str_ptr)(b.c_str());
00201 }
00202 
00203 template<   ss_typename_param_k C
00204         ,   ss_typename_param_k T
00205         >
00206 inline us_size_t c_str_len(basic_current_directory<C, T> const &b)
00207 {
00208     return stlsoft_ns_qual(c_str_len)(b.c_str());
00209 }
00210 
00211 template<   ss_typename_param_k C
00212         ,   ss_typename_param_k T
00213         >
00214 inline us_size_t c_str_size(basic_current_directory<C, T> const &b)
00215 {
00216     return stlsoft_ns_qual(c_str_size)(b.c_str());
00217 }
00218 
00219 template<   ss_typename_param_k S
00220         ,   ss_typename_param_k C
00221         ,   ss_typename_param_k T
00222         >
00223 inline S &operator <<(S & s, basic_current_directory<C, T> const &b)
00224 {
00225     s << b.c_str();
00226 
00227     return s;
00228 }
00229 
00230 /* 
00231  * Unit-testing
00232  */
00233 
00234 #ifdef STLSOFT_UNITTEST
00235 
00236 namespace unittest
00237 {
00238     ss_bool_t test_unixstl_current_directory(unittest_reporter *r)
00239     {
00240         using stlsoft::unittest::unittest_initialiser;
00241 
00242         ss_bool_t               bSuccess    =   true;
00243 
00244         unittest_initialiser    init(r, "UNIXSTL", "current_directory", __FILE__);
00245 
00246         typedef basic_current_directory<char>   curr_dir_t;
00247 
00248         basic_file_path_buffer<char>    sz;
00249         curr_dir_t                      cd;
00250 
00251         getcwd(&sz[0], sz.size());
00252 
00253         if(0 != strcmp(sz.c_str(), cd.c_str()))
00254         {
00255             r->report("basic_current_directory<char> failed ", __LINE__);
00256             bSuccess = false;
00257         }
00258 
00259         return bSuccess;
00260     }
00261 
00262     unittest_registrar    unittest_unixstl_current_directory(test_unixstl_current_directory);
00263 
00264 } // namespace unittest
00265 
00266 #endif /* STLSOFT_UNITTEST */
00267 
00268 /* 
00269  * Implementation
00270  */
00271 
00272 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00273 
00274 template<   ss_typename_param_k C
00275         ,   ss_typename_param_k T
00276         >
00277 inline basic_current_directory<C, T>::basic_current_directory()
00278     : m_len(get_path(&m_dir[0], m_dir.size()))
00279 {}
00280 
00281 template<   ss_typename_param_k C
00282         ,   ss_typename_param_k T
00283         >
00284 inline /* static */ ss_typename_type_k basic_current_directory<C, T>::size_type basic_current_directory<C, T>::get_path(ss_typename_type_k basic_current_directory<C, T>::char_type *buffer, ss_typename_type_k basic_current_directory<C, T>::size_type cchBuffer)
00285 {
00286     return static_cast<size_type>(traits_type::get_current_directory(cchBuffer, buffer));
00287 }
00288 
00289 template<   ss_typename_param_k C
00290         ,   ss_typename_param_k T
00291         >
00292 inline ss_typename_type_k basic_current_directory<C, T>::char_type const *basic_current_directory<C, T>::get_path() const
00293 {
00294 #if defined(__STLSOFT_COMPILER_IS_GCC) && \
00295     __GNUC__ < 3
00296     return m_dir.c_str();
00297 #else /* ? __GNUC__ < 3 */
00298     return stlsoft_ns_qual(c_str_ptr)(m_dir);
00299 #endif /* ? __GNUC__ < 3 */
00300 }
00301 
00302 template<   ss_typename_param_k C
00303         ,   ss_typename_param_k T
00304         >
00305 inline ss_typename_type_k basic_current_directory<C, T>::char_type const *basic_current_directory<C, T>::c_str() const
00306 {
00307     return get_path();
00308 }
00309 
00310 template<   ss_typename_param_k C
00311         ,   ss_typename_param_k T
00312         >
00313 inline ss_typename_type_k basic_current_directory<C, T>::size_type basic_current_directory<C, T>::length() const
00314 {
00315     return m_len;
00316 }
00317 
00318 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00319 
00320 /* 
00321 
00323 
00324 /* 
00325 
00326 #ifndef _UNIXSTL_NO_NAMESPACE
00327 # if defined(_STLSOFT_NO_NAMESPACE) || \
00328      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00329 } // namespace unixstl
00330 # else
00331 } // namespace unixstl_project
00332 } // namespace stlsoft
00333 # endif /* _STLSOFT_NO_NAMESPACE */
00334 #endif /* !_UNIXSTL_NO_NAMESPACE */
00335 
00336 /* 
00337 
00338 #endif /* UNIXSTL_INCL_H_UNIXSTL_CURRENT_DIRECTORY */
00339 
00340 /* 

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