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_c_5.c

[recls Core API] 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_c_5.c
00003  *
00004  * Purpose:     C example program for the recls core library. Demonstrates:
00005  *
00006  *                - stat() of home directory (via Recls_Stat())
00007  *                - en-bloc processing, via Recls_SearchProcess(), of all
00008  *                  files and directories under the home directory
00009  *                - recursive operation
00010  *                - display of path relative to home directory
00011  *                  (via Recls_DeriveRelativePath())
00012  *                - elicitation of entry properties via structure members
00013  *                - handling of errors and reporting of error information
00014  *
00015  * Created:     17th June 2006
00016  * Updated:     18th June 2006
00017  *
00018  * www:         http://www.recls.org/
00019  *
00020  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00021  *              All rights reserved.
00022  *
00023  *              (Licensed under the Synesis Software Open License)
00024  *
00025  *              This source code is placed into the public domain 2006
00026  *              by Synesis Software Pty Ltd. There are no restrictions
00027  *              whatsoever to your use of the software.
00028  *
00029  * ////////////////////////////////////////////////////////////////////// */
00030 
00031 /* recls Header Files */
00032 #include <recls/recls.h>
00033 
00034 /* Standard C Library Files */
00035 #include <stdio.h>      /* for printf() / fprintf()         */
00036 #include <stdlib.h>     /* for EXIT_SUCCESS / EXIT_FAILURE  */
00037 #include <string.h>
00038 
00039 /* /////////////////////////////////////////////////////////////////////////
00040  * Macros and definitions
00041  */
00042 
00043 #ifndef RECLS_NUM_ELEMENTS
00044 # define RECLS_NUM_ELEMENTS(x)          (sizeof(x) / sizeof((x)[0]))
00045 #endif /* !RECLS_NUM_ELEMENTS */
00046 
00047 /* ////////////////////////////////////////////////////////////////////// */
00048 
00049 int RECLS_CALLCONV_DEFAULT example_c_5_process_fn(  recls_info_t                entry
00050                                                 ,   recls_process_fn_param_t    param)
00051 {
00052     char        relativePath[1001];
00053     char const  *homePath   =   (char const*)param;
00054     size_t      cch         =   Recls_DeriveRelativePath(homePath, entry->path.begin, &relativePath[0], RECLS_NUM_ELEMENTS(relativePath));
00055 
00056     printf("%.*s\n", (int)cch, relativePath);
00057 
00058     return 1; /* Continue processing. */
00059 }
00060 
00061 
00062 int main()
00063 {
00064     /* First, determine the home directory by stat()-ing ~ */
00065     recls_info_t    home;
00066     recls_rc_t      rc  =   Recls_Stat("~", RECLS_F_DIRECTORIES, &home);
00067 
00068     if(RECLS_FAILED(rc))
00069     {
00070         /* The search failed. Display the error string. */
00071         char    err[1001];
00072         size_t  n   =   Recls_GetErrorString(rc, &err[0], sizeof(err) - 1);
00073 
00074         err[n] = '\0';
00075 
00076         fprintf(stderr, "stat of home directory failed: %s\n", err);
00077 
00078         return EXIT_FAILURE;
00079     }
00080     else
00081     {
00082         recls_uint32_t  flags   =   RECLS_F_FILES | RECLS_F_DIRECTORIES | RECLS_F_RECURSIVE;
00083 
00084         /* Process all entries under the current directory, passing the home
00085          * entry's path pointer. This is valid since the path is always 
00086          * nul-terminated.
00087          */
00088 
00089         rc = Recls_SearchProcess(NULL, Recls_GetWildcardsAll(), flags, example_c_5_process_fn, (void*)home->path.begin);
00090 
00091         /* Close the home entry. */
00092 
00093         Recls_CloseDetails(home);
00094 
00095         if(RECLS_FAILED(rc))
00096         {
00097             /* The search failed. Display the error string. */
00098             char    err[1001];
00099             size_t  n   =   Recls_GetErrorString(rc, &err[0], sizeof(err) - 1);
00100 
00101             err[n] = '\0';
00102 
00103             fprintf(stderr, "processing failed: %s\n", err);
00104 
00105             return EXIT_FAILURE;
00106         }
00107 
00108         return EXIT_SUCCESS;
00109     }
00110 }
00111 
00112 /* ////////////////////////////////////////////////////////////////////// */

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