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

example_cpp_2.cpp

[recls/C++] This example enumerates all files and directories under the current (NULL) path, and display their relative path to "~" (obtained via stat()). It illustrates the following features of the core API:

00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_2.cpp
00003  *
00004  * Purpose:     C++ example program for recls/C++. Demonstrates:
00005  *
00006  *                - stat()-ing of home directory
00007  *                - searching for files, according to multi-part pattern
00008  *                - recursive operation
00009  *                - evaluation of relative path of each entry, with respect
00010  *                  to home directory
00011  *                - handling exceptions and reporting of error information
00012  *                - elicitation of entry properties via method calls
00013  *
00014  * Created:     18th June 2006
00015  * Updated:     18th June 2006
00016  *
00017  * www:         http://www.recls.org/
00018  *
00019  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00020  *              All rights reserved.
00021  *
00022  *              (Licensed under the Synesis Software Open License)
00023  *
00024  *              This source code is placed into the public domain 2006
00025  *              by Synesis Software Pty Ltd. There are no restrictions
00026  *              whatsoever to your use of the software.
00027  *
00028  * ////////////////////////////////////////////////////////////////////// */
00029 
00030 
00031 /* recls Header Files */
00032 #include <recls/cpp/fileentry.hpp>
00033 #include <recls/cpp/filesearch.hpp>
00034 #include <recls/implicit_link.h>
00035 
00036 /* Standard C++ Header Files */
00037 #include <exception>
00038 #include <iostream>
00039 
00040 /* Standard C Header Files */
00041 #include <stdlib.h>     /* for EXIT_SUCCESS / EXIT_FAILURE  */
00042 
00043 /* ////////////////////////////////////////////////////////////////////////// */
00044 
00045 int main()
00046 {
00047     try
00048     {
00049         /* stat() the home directory */
00050         recls::cpp::FileEntry   home    =   recls::cpp::FileSearch::Stat("~");
00051 
00052         /* Enumerate all under the current directory, matching *.??? or makefile*.*. */
00053         recls::uint32_t         flags   =   recls::RECLS_F_FILES | recls::RECLS_F_RECURSIVE;
00054         recls::cpp::FileSearch  srch(NULL, "*.???|makefile|makefile.*|", flags);
00055 
00056         for(; srch.HasMoreElements(); srch.GetNext())
00057         {
00058             recls::cpp::FileEntry   entry           =   srch.GetCurrentEntry();
00059             recls::cpp::string_t    relativePath    =   recls::cpp::FileSearch::DeriveRelativePath(home.GetPath(), entry.GetPath());
00060 
00061             std::cout << relativePath << std::endl;
00062         }
00063     }
00064     catch(recls::cpp::ReclsException &x)
00065     {
00066         std::cerr << "Recls error: " << x.rc() << ", " << x.what() << std::endl;
00067 
00068         return EXIT_FAILURE;
00069     }
00070     catch(std::bad_alloc &)
00071     {
00072         std::cerr << "Out of memory" << std::endl;
00073 
00074         return EXIT_FAILURE;
00075     }
00076     catch(std::exception &x)
00077     {
00078         std::cerr << "Unhandled error: " << x.what() << std::endl;
00079 
00080         return EXIT_FAILURE;
00081     }
00082     catch(...)
00083     {
00084         std::cerr << "Unhandled unknown error" << std::endl;
00085 
00086         return EXIT_FAILURE;
00087     }
00088 
00089     return EXIT_SUCCESS;
00090 }
00091 
00092 /* ////////////////////////////////////////////////////////////////////////// */

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