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_3.cpp

[recls/C++] This example stat()s the home directory using "~" and displays various attributes of the corresponding entry. It illustrates the following features of the core API:

00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_3.cpp
00003  *
00004  * Purpose:     C++ example program for recls/C++. Demonstrates:
00005  *
00006  *                - stat()-ing of current 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 current directory
00050         recls::cpp::FileEntry   current =   recls::cpp::FileSearch::Stat("~");
00051 
00052         // Print out its characteristics:
00053 
00054         // 1. Full path
00055         std::cout << current.GetPath() << std::endl;
00056 
00057         // 2. Search-relative path
00058         std::cout << current.GetSearchRelativePath() << std::endl;
00059 
00060         // 3. Search directory
00061         std::cout << current.GetSearchDirectory() << std::endl;
00062 
00063 #ifdef RECLS_PLATFORM_API_WIN32
00064         // 4. Drive property
00065         std::cout << current.GetDrive() << std::endl;
00066 #endif /* RECLS_PLATFORM_API_WIN32 */
00067 
00068         // 5. Directory path property
00069         std::cout << current.GetDirectoryPath() << std::endl;
00070 
00071         // 6. Directory property
00072         std::cout << current.GetDirectory() << std::endl;
00073 
00074         // 7. File property
00075         std::cout << current.GetFile() << std::endl;
00076 
00077         // 8. File name property
00078         std::cout << current.GetFileName() << std::endl;
00079 
00080         // 9. File extension property
00081         std::cout << current.GetFileExt() << std::endl;
00082 
00083         // 10. Type
00084         if(current.IsDirectory())
00085         {
00086             std::cout << " <directory>" << std::endl;
00087         }
00088         else
00089         {
00090             std::cout << " <file>" << std::endl;
00091         }
00092         if(current.IsLink())
00093         {
00094             std::cout << " <link>" << std::endl;
00095         }
00096         if(current.IsReadOnly())
00097         {
00098             std::cout << " <read-only>" << std::endl;
00099         }
00100 
00101         // 11. Size
00102 #ifdef RECLS_PLATFORM_API_WIN32
00103         std::cout << static_cast<unsigned long>(current.GetSize().QuadPart) << " bytes" << std::endl;
00104 #else /* ? RECLS_PLATFORM_API_WIN32 */
00105         std::cout << current.GetSize() << " bytes" << std::endl;
00106 #endif /* RECLS_PLATFORM_API_WIN32 */
00107     }
00108     catch(recls::cpp::ReclsException &x)
00109     {
00110         std::cerr << "Recls error: " << x.rc() << ", " << x.what() << std::endl;
00111 
00112         return EXIT_FAILURE;
00113     }
00114     catch(std::bad_alloc &)
00115     {
00116         std::cerr << "Out of memory" << std::endl;
00117 
00118         return EXIT_FAILURE;
00119     }
00120     catch(std::exception &x)
00121     {
00122         std::cerr << "Unhandled error: " << x.what() << std::endl;
00123 
00124         return EXIT_FAILURE;
00125     }
00126     catch(...)
00127     {
00128         std::cerr << "Unhandled unknown error" << std::endl;
00129 
00130         return EXIT_FAILURE;
00131     }
00132 
00133     return EXIT_SUCCESS;
00134 }
00135 
00136 /* ////////////////////////////////////////////////////////////////////////// */

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