C/C++ User's Journal STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - where the Standard Template Library meets the Active Template Library COMSTL - where the Standard Template Library meets the Component Object Model
Synesis Software InetSTL - where the Standard Template Library meets the Internet UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

recls/cpp/filesearch.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        recls/cpp/filesearch.hpp
00003  *
00004  * Purpose:     recls C++ mapping - FileSearch class.
00005  *
00006  * Created:     18th August 2003
00007  * Updated:     13th August 2006
00008  *
00009  * Home:        http://recls.org/
00010  *
00011  * Copyright (c) 2003-2006, 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 
00041 #ifndef RECLS_INCL_RECLS_CPP_HPP_FILESEARCH
00042 #define RECLS_INCL_RECLS_CPP_HPP_FILESEARCH
00043 
00044 /* File version */
00045 #ifndef RECLS_DOCUMENTATION_SKIP_SECTION
00046 # define RECLS_VER_RECLS_CPP_HPP_FILESEARCH_MAJOR       3
00047 # define RECLS_VER_RECLS_CPP_HPP_FILESEARCH_MINOR       2
00048 # define RECLS_VER_RECLS_CPP_HPP_FILESEARCH_REVISION    1
00049 # define RECLS_VER_RECLS_CPP_HPP_FILESEARCH_EDIT        24
00050 #endif /* !RECLS_DOCUMENTATION_SKIP_SECTION */
00051 
00060 /* /////////////////////////////////////////////////////////////////////////////
00061  * Includes
00062  */
00063 
00064 #include <recls/cpp/recls.hpp>
00065 #include <recls/cpp/search.hpp>
00066 #include <recls/assert.h>
00067 
00068 #include <stlsoft/memory/auto_buffer.hpp>
00069 #include <stlsoft/shims/access/string.hpp>
00070 
00071 /* /////////////////////////////////////////////////////////////////////////////
00072  * Namespace
00073  */
00074 
00075 #if !defined(RECLS_NO_NAMESPACE)
00076 namespace recls
00077 {
00078 
00079 namespace cpp
00080 {
00081 #endif /* !RECLS_NO_NAMESPACE */
00082 
00083 /* /////////////////////////////////////////////////////////////////////////////
00084  * Classes
00085  */
00086 
00090 class FileSearch
00091     : public Search
00092 {
00094 public:
00096     typedef recls_char_t    char_type;
00097     typedef size_t          size_type;
00098     typedef FileSearch      class_type;
00100 
00103 public:
00109     FileSearch(char_type const *rootDir, char_type const *pattern, recls_uint32_t flags);
00111     ~FileSearch();
00113 
00116 public:
00123     template <typename S>
00124     static FileEntry    Stat(S const &path)
00125     {
00126         return Stat_(stlsoft::c_str_ptr(path), RECLS_F_FILES | RECLS_F_DIRECTORIES | RECLS_F_DIRECTORY_PARTS);
00127     }
00128 
00129     template <typename S>
00130     static FileEntry    Stat(   S const         &path
00131                             ,   recls_uint32_t  flags /* = RECLS_F_FILES | RECLS_F_DIRECTORIES | RECLS_F_DIRECTORY_PARTS */)
00132     {
00133         return Stat_(stlsoft::c_str_ptr(path), flags);
00134     }
00135 
00136     template<   typename    S1
00137             ,   typename    S2
00138             >
00139     static string_t     CombinePaths(   S1 const    &path1
00140                                     ,   S2 const    &path2)
00141     {
00142         return CombinePaths_(stlsoft::c_str_ptr(path1), stlsoft::c_str_ptr(path2));
00143     }
00144 
00145     template<   typename    S1
00146             ,   typename    S2
00147             >
00148     static string_t     DeriveRelativePath( S1 const    &origin
00149                                         ,   S2 const    &target)
00150     {
00151         return DeriveRelativePath_(stlsoft::c_str_ptr(origin), stlsoft::c_str_ptr(target));
00152     }
00153 
00154     template <typename S>
00155     static string_t     SqueezePath(    S const     &path
00156                                     ,   size_type   width)
00157     {
00158         return SqueezePath_(stlsoft::c_str_ptr(path), width);
00159     }
00161 
00162 // Implementation
00163 private:
00164     static ctor_args_type CreateSearch_(char_type const *rootDir, char_type const *pattern, recls_uint32_t flags)
00165     {
00166         hrecls_t    hSrch;
00167         recls_rc_t  rc = Recls_Search(rootDir, pattern, flags, &hSrch);
00168 
00169 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00170         if( RECLS_FAILED(rc) &&
00171             RECLS_RC_NO_MORE_DATA != rc)
00172         {
00173             throw ReclsException(rc);
00174         }
00175 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
00176 
00177         return Search::ctor_args_type(hSrch, rc);
00178     }
00179 
00180     static FileEntry Stat_(char_type const *path, recls_uint32_t flags);
00181 
00182     static string_t CombinePaths_( char_type const *path1, char_type const *path2);
00183 
00184     static string_t DeriveRelativePath_(char_type const *origin, char_type const *target);
00185 
00186     static string_t SqueezePath_(char_type const *path, size_type width);
00187 
00188 // Not to be implemented
00189 private:
00190     FileSearch(class_type const &rhs);
00191     class_type &operator =(class_type const &rhs);
00192 };
00193 
00194 /* /////////////////////////////////////////////////////////////////////////////
00195  * Implementation
00196  */
00197 
00198 inline /* static */ string_t FileSearch::CombinePaths_(FileSearch::char_type const *path1, FileSearch::char_type const *path2)
00199 {
00200     stlsoft::auto_buffer<char_type> buffer(Recls_CombinePaths(path1, path2, NULL, 0));
00201 
00202     Recls_CombinePaths(path1, path2, &buffer[0], buffer.size());
00203 
00204     return string_t(buffer.data(), buffer.size());
00205 }
00206 
00207 inline /* static */ string_t FileSearch::DeriveRelativePath_(FileSearch::char_type const *origin, FileSearch::char_type const *target)
00208 {
00209     stlsoft::auto_buffer<char_type> buffer(Recls_DeriveRelativePath(origin, target, NULL, 0));
00210 
00211     Recls_DeriveRelativePath(origin, target, &buffer[0], buffer.size());
00212 
00213     return string_t(buffer.data(), buffer.size());
00214 }
00215 
00216 inline /* static */ string_t FileSearch::SqueezePath_(FileSearch::char_type const *path, size_type width)
00217 {
00218     stlsoft::auto_buffer<char_type> buffer(width);
00219     size_t                          cch =   Recls_SqueezePath(path, &buffer[0], buffer.size());
00220 
00221     RECLS_ASSERT(cch <= buffer.size());
00222 
00223     return string_t(buffer.data(), cch);
00224 }
00225 
00226 
00227 inline FileSearch::FileSearch(FileSearch::char_type const *rootDir, FileSearch::char_type const *pattern, recls_uint32_t flags)
00228     : Search(CreateSearch_(rootDir, pattern, flags))
00229 {}
00230 
00231 inline FileSearch::~FileSearch()
00232 {}
00233 
00234 inline /* static */ FileEntry FileSearch::Stat_(FileSearch::char_type const *path, recls_uint32_t flags /* = RECLS_F_FILES | RECLS_F_DIRECTORIES | RECLS_F_DIRECTORY_PARTS */)
00235 {
00236     recls_info_t    info;
00237     recls_rc_t      rc  =   Recls_Stat(path, flags, &info);
00238 
00239     return RECLS_SUCCEEDED(rc) ? make_entry_(info) : FileEntry();
00240 }
00241 
00242 #if 0
00243 inline /* static */ string_t FileSearch::CombinePaths(FileSearch::char_type const *path1, FileSearch::char_type const *path2)
00244 {
00245     return CombinePaths_(path1, path2);
00246 }
00247 
00248 inline /* static */ string_t FileSearch::DeriveRelativePath(FileSearch::char_type const *origin, FileSearch::char_type const *target)
00249 {
00250     return DeriveRelativePath_(origin, target);
00251 }
00252 
00253 inline /* static */ string_t FileSearch::SqueezePath(FileSearch::char_type const *path, size_type width)
00254 {
00255     return SqueezePath_(path, width);
00256 }
00257 #endif /* 0 */
00258 
00259 /* /////////////////////////////////////////////////////////////////////////////
00260  * Namespace
00261  */
00262 
00263 #if !defined(RECLS_NO_NAMESPACE)
00264 } /* namespace cpp */
00265 } /* namespace recls */
00266 #endif /* !RECLS_NO_NAMESPACE */
00267 
00268 /* ////////////////////////////////////////////////////////////////////////// */
00269 
00270 #endif /* !RECLS_INCL_RECLS_CPP_HPP_FILESEARCH */
00271 
00272 /* ////////////////////////////////////////////////////////////////////////// */

recls Library documentation © Synesis Software Pty Ltd, 2001-2006