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

[recls Core API] This example enumerates all files under the current directory and prints out their full paths squeezed into a fixed width. Also temporarily displays search progress. It illustrates the following features of the core API:

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_6.c
00003  *
00004  * Purpose:     C example program for the recls core library. Demonstrates:
00005  *
00006  *                - stat() of current directory (via Recls_Stat())
00007  *                - display of full path, drive (Win32 only), directory,
00008  *                  directory path, file, file name, file extension, and
00009  *                  directory parts of each entry
00010  *                - elicitation of entry properties via structure members
00011  *                - handling of errors and reporting of error information
00012  *
00013  * Created:     17th June 2006
00014  * Updated:     18th June 2006
00015  *
00016  * www:         http://www.recls.org/
00017  *
00018  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00019  *              All rights reserved.
00020  *
00021  *              (Licensed under the Synesis Software Open License)
00022  *
00023  *              This source code is placed into the public domain 2006
00024  *              by Synesis Software Pty Ltd. There are no restrictions
00025  *              whatsoever to your use of the software.
00026  *
00027  * ////////////////////////////////////////////////////////////////////// */
00028 
00029 /* recls Header Files */
00030 #include <recls/recls.h>
00031 
00032 /* Standard C Library Files */
00033 #include <stdio.h>      /* for printf() / fprintf()         */
00034 #include <stdlib.h>     /* for EXIT_SUCCESS / EXIT_FAILURE  */
00035 #include <string.h>
00036 
00037 /* /////////////////////////////////////////////////////////////////////////
00038  * Macros and definitions
00039  */
00040 
00041 #ifndef RECLS_NUM_ELEMENTS
00042 # define RECLS_NUM_ELEMENTS(x)          (sizeof(x) / sizeof((x)[0]))
00043 #endif /* !RECLS_NUM_ELEMENTS */
00044 
00045 /* ////////////////////////////////////////////////////////////////////// */
00046 
00047 int main()
00048 {
00049     /* stat() the current directory */
00050     recls_info_t    current;
00051     recls_rc_t      rc  =   Recls_Stat(".", RECLS_F_DIRECTORIES | RECLS_F_DIRECTORY_PARTS, &current);
00052 
00053     if(RECLS_FAILED(rc))
00054     {
00055         /* The search failed. Display the error string. */
00056         char    err[1001];
00057         size_t  n   =   Recls_GetErrorString(rc, &err[0], sizeof(err) - 1);
00058 
00059         err[n] = '\0';
00060 
00061         fprintf(stderr, "stat of current directory failed: %s\n", err);
00062 
00063         return EXIT_FAILURE;
00064     }
00065     else
00066     {
00067         struct recls_strptrs_t const    *part_ptr;
00068 
00069         /* full path */
00070         printf("%s\n", current->path.begin);
00071 
00072 #if defined(RECLS_PLATFORM_IS_WIN32)
00073         /* drive (Windows-only) */
00074         printf("  drive:          %c:\n", current->drive);
00075 #endif /* RECLS_PLATFORM_IS_WIN32 */
00076 
00077         /* directory path */
00078         printf("  directory path: %.*s\n", (int)(current->directory.end - current->path.begin), current->path.begin);
00079 
00080         /* directory */
00081         printf("  directory:      %.*s\n", (int)(current->directory.end - current->directory.begin), current->directory.begin);
00082 
00083         /* file */
00084         printf("  file:           %.*s\n", (int)(current->fileExt.end - current->fileName.begin), current->fileName.begin);
00085 
00086         /* file name */
00087         printf("  file name:      %.*s\n", (int)(current->fileName.end - current->fileName.begin), current->fileName.begin);
00088 
00089         /* file extension */
00090         printf("  file ext:       %.*s\n", (int)(current->fileExt.end - current->fileExt.begin), current->fileExt.begin);
00091 
00092 #if defined(RECLS_PLATFORM_IS_WIN32)
00093         /* drive (Windows-only) */
00094         printf("  short file:     %.*s\n", (int)(current->shortFile.end - current->shortFile.begin), current->shortFile.begin);
00095 #endif /* RECLS_PLATFORM_IS_WIN32 */
00096 
00097         /* directory parts */
00098         printf("  directory parts:\n");
00099         for(part_ptr = current->directoryParts.begin; part_ptr != current->directoryParts.end; ++part_ptr)
00100         {
00101             printf("    part:     %.*s\n", (int)(part_ptr->end - part_ptr->begin), part_ptr->begin);
00102         }
00103 
00104         /* Close the current entry. */
00105         Recls_CloseDetails(current);
00106 
00107         return EXIT_SUCCESS;
00108     }
00109 }
00110 
00111 /* ////////////////////////////////////////////////////////////////////// */

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