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

[recls Core API] This example enumerates all files under the current directory via structure members, squeezing their absolute path into a fixed width. Recls_SearchFeedback() is used, and the currently searched directory is temporarily blitted to stdout using .

It illustrates the following features of the core API:

00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_2.c
00003  *
00004  * Purpose:     C example program for the recls core library. Demonstrates:
00005  *
00006  *                - searching (via Recls_SearchFeedback()) for files
00007  *                - use of multi-part pattern matching
00008  *                - recursive operation
00009  *                - display of full path of each entry
00010  *                - handling of errors and reporting of error information
00011  *                - elicitation of entry properties via entry structure
00012  *                  members
00013  *                - display of progress (of each directory traversed)
00014  *
00015  * Created:     29th May 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 #include <recls/alloca.h>
00034 
00035 /* STLSoft Header Files */
00036 #ifdef RECLS_PLATFORM_API_WIN32
00037 # include <winstl/system/console_functions.h>
00038 #endif /* RECLS_PLATFORM_API_WIN32 */
00039 
00040 /* Standard C Library Files */
00041 #include <stdio.h>      /* for printf() / fprintf()         */
00042 #include <stdlib.h>     /* for EXIT_SUCCESS / EXIT_FAILURE  */
00043 #include <string.h>
00044 
00045 /* ////////////////////////////////////////////////////////////////////// */
00046 
00047 static void write_backs(FILE *stm, size_t n);
00048 static void write_blanks(FILE *stm, size_t n);
00049 static void write_blank_line(FILE *stm, size_t n);
00050 static size_t get_console_width(void);
00051 static int RECLS_CALLCONV_DEFAULT example_c_2_progress_fn(  recls_char_t const          *dir
00052                                                         ,   size_t                      dirLen
00053                                                         ,   recls_process_fn_param_t    param
00054                                                         ,   void                        *reserved0
00055                                                         ,   recls_uint32_t              reserved1);
00056 
00057 /* ////////////////////////////////////////////////////////////////////// */
00058 
00059 struct feedback_t
00060 {
00061     size_t  lastLen;
00062 };
00063 
00064 /* ////////////////////////////////////////////////////////////////////// */
00065 
00066 int main()
00067 {
00068     const char  SEARCH_PATTERN[]    =   "*.h|*.hpp|*.c|*.cpp|*.cs|*.d|*.java|*.pl|*.py|*.rb";
00069 
00070     struct feedback_t   feedback    =   { 0 };
00071     hrecls_t            hSrch;
00072     recls_uint32_t      flags       =   RECLS_F_FILES | RECLS_F_RECURSIVE;
00073     recls_rc_t          rc          =   Recls_SearchFeedback(NULL, SEARCH_PATTERN, flags, example_c_2_progress_fn, &feedback, &hSrch);
00074 
00075     if(RECLS_RC_OK != rc)
00076     {
00077         char    err[1001];
00078         size_t  n   =   Recls_GetErrorString(rc, &err[0], sizeof(err) - 1);
00079 
00080         err[n] = '\0';
00081 
00082         fprintf(stderr, "Search failed: %s\n", err);
00083 
00084         return EXIT_FAILURE;
00085     }
00086     else
00087     {
00088         recls_info_t    entry;
00089 
00090         Recls_GetDetails(hSrch, &entry);
00091 
00092         do
00093         {
00094             write_blank_line(stdout, feedback.lastLen);
00095 
00096             /* full path */
00097             printf("%s\n", entry->path.begin);
00098 
00099             Recls_CloseDetails(entry);
00100         }
00101         while(RECLS_SUCCEEDED(Recls_GetNextDetails(hSrch, &entry)));
00102 
00103         return EXIT_SUCCESS;
00104     }
00105 }
00106 
00107 /* ////////////////////////////////////////////////////////////////////// */
00108 
00109 static void write_backs(FILE *stm, size_t n)
00110 {
00111     char    *backs  =   (char*)recls_alloca(n);
00112 
00113     memset(&backs[0], '\b', n);
00114 
00115     fprintf(stm, "%.*s", n, &backs[0]);
00116 }
00117 
00118 static void write_blanks(FILE *stm, size_t n)
00119 {
00120     char    *blanks =   (char*)recls_alloca(n);
00121 
00122     memset(&blanks[0], ' ', n);
00123 
00124     fprintf(stm, "%.*s", n, &blanks[0]);
00125 }
00126 
00127 static void write_blank_line(FILE *stm, size_t n)
00128 {
00129     char    *backs  =   (char*)recls_alloca(n);
00130     char    *blanks =   (char*)recls_alloca(n);
00131 
00132     memset(&backs[0], '\b', n);
00133     memset(&blanks[0], ' ', n);
00134 
00135     fprintf(stm, "%.*s", n, &backs[0]);
00136     fprintf(stm, "%.*s", n, &blanks[0]);
00137     fprintf(stm, "%.*s", n, &backs[0]);
00138 }
00139 
00140 static size_t get_console_width(void)
00141 {
00142     /* In reality, this should evaluate as appropriate to the operating
00143      * system.
00144      */
00145 
00146 #ifdef RECLS_PLATFORM_API_WIN32
00147     return winstl__get_console_width();
00148 #else /* ? RECLS_PLATFORM_API_??? */
00149     return 48;
00150 #endif /* RECLS_PLATFORM_API_??? */
00151 }
00152 
00153 static int RECLS_CALLCONV_DEFAULT example_c_2_progress_fn(  recls_char_t const          *dir
00154                                                         ,   size_t                      dirLen
00155                                                         ,   recls_process_fn_param_t    param
00156                                                         ,   void                        *reserved0
00157                                                         ,   recls_uint32_t              reserved1)
00158 {
00159     struct feedback_t   *feedback       =   (struct feedback_t*)param;
00160     size_t              newLen;
00161     size_t              cch;
00162     size_t              consoleWidth    =   get_console_width() - 1;
00163 
00164     if(consoleWidth < dirLen)
00165     {
00166         char    *squeezedForm = (char*)recls_alloca(dirLen);
00167 
00168         strncpy(squeezedForm, dir, dirLen);
00169 
00170         squeezedForm[dirLen - 1] = '\0';
00171 
00172         cch = Recls_SqueezePath(squeezedForm, squeezedForm, consoleWidth - 1);
00173 
00174         dir = squeezedForm;
00175     }
00176     else
00177     {
00178         cch = dirLen;
00179     }
00180 
00181     write_backs(stdout, feedback->lastLen);
00182 
00183     newLen = fprintf(stdout, "%.*s", (int)cch, dir);
00184 
00185     if(newLen < feedback->lastLen)
00186     {
00187         size_t  spare   =   feedback->lastLen - newLen;
00188 
00189         write_blanks(stdout, spare);
00190         write_backs(stdout, spare);
00191     }
00192 
00193     feedback->lastLen = newLen;
00194 
00195     ((void)reserved0);
00196     ((void)reserved1);
00197 
00198     return 1; /* Continue processing. */
00199 }
00200 
00201 /* ////////////////////////////////////////////////////////////////////// */

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