![]() |
![]() |
![]() |
![]() |
||||
![]() |
![]() |
![]() |
![]() |
||||
|
00001 /* ///////////////////////////////////////////////////////////////////////////// 00002 * File: example_c_3.c 00003 * 00004 * Purpose: C example program for the recls core library. Demonstrates: 00005 * 00006 * - stat() of current directory (via Recls_Stat()) 00007 * - searching (via Recls_Search()) for files and directories 00008 * - recursive operation 00009 * - display of full path of each entry, squeezed to constant 00010 * width (via Recls_SqueezePath()) 00011 * - display of file size for file entries 00012 * - display of directory contents size for directory 00013 * entries, (determined via Recls_CalcDirectoryEntrySize()) 00014 * - handling of errors and reporting of error information 00015 * - elicitation of entry properties via API function calls 00016 * 00017 * Created: 29th May 2006 00018 * Updated: 18th June 2006 00019 * 00020 * www: http://www.recls.org/ 00021 * 00022 * License: Copyright (c) 2006, Synesis Software Pty Ltd. 00023 * All rights reserved. 00024 * 00025 * (Licensed under the Synesis Software Open License) 00026 * 00027 * This source code is placed into the public domain 2006 00028 * by Synesis Software Pty Ltd. There are no restrictions 00029 * whatsoever to your use of the software. 00030 * 00031 * ////////////////////////////////////////////////////////////////////// */ 00032 00033 /* recls Header Files */ 00034 #include <recls/recls.h> 00035 00036 /* Standard C Library Files */ 00037 #include <stdio.h> /* for printf() / fprintf() */ 00038 #include <stdlib.h> /* for EXIT_SUCCESS / EXIT_FAILURE */ 00039 #include <string.h> 00040 00041 /* ///////////////////////////////////////////////////////////////////////// 00042 * Macros and definitions 00043 */ 00044 00045 #ifndef RECLS_NUM_ELEMENTS 00046 # define RECLS_NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0])) 00047 #endif /* !RECLS_NUM_ELEMENTS */ 00048 00049 /* ////////////////////////////////////////////////////////////////////// */ 00050 00051 int main() 00052 { 00053 /* stat() the current directory */ 00054 recls_info_t current; 00055 recls_rc_t rc = Recls_Stat(".", RECLS_F_DIRECTORIES | RECLS_F_DIRECTORY_PARTS, ¤t); 00056 00057 if(RECLS_FAILED(rc)) 00058 { 00059 /* The search failed. Display the error string. */ 00060 char err[1001]; 00061 size_t n = Recls_GetErrorString(rc, &err[0], sizeof(err) - 1); 00062 00063 err[n] = '\0'; 00064 00065 fprintf(stderr, "stat of current directory failed: %s\n", err); 00066 00067 return EXIT_FAILURE; 00068 } 00069 else 00070 { 00071 hrecls_t hSrch; 00072 recls_uint32_t flags = RECLS_F_FILES | RECLS_F_DIRECTORIES | RECLS_F_RECURSIVE; 00073 00074 rc = Recls_Search(current->path.begin, NULL, flags, &hSrch); 00075 00076 /* ... close the entry handle, ... */ 00077 Recls_CloseDetails(current); 00078 00079 if(RECLS_RC_NO_MORE_DATA == rc) 00080 { 00081 printf(" no matches found\n"); 00082 00083 return EXIT_SUCCESS; 00084 } 00085 else if(RECLS_FAILED(rc)) 00086 { 00087 /* The search failed. Display the error string. */ 00088 char err[1001]; 00089 size_t n = Recls_GetErrorString(rc, &err[0], sizeof(err) - 1); 00090 00091 err[n] = '\0'; 00092 00093 fprintf(stderr, "Search failed: %s\n", err); 00094 00095 return EXIT_FAILURE; 00096 } 00097 else 00098 { 00099 /* Get the details for the first entry, ... */ 00100 00101 recls_info_t entry; 00102 00103 Recls_GetDetails(hSrch, &entry); 00104 00105 do 00106 { 00107 /* ... get the full path, ... */ 00108 recls_filesize_t size; 00109 int isDirectory; 00110 unsigned long ulSize; 00111 char path[1001]; 00112 char squeezedPath[48]; 00113 size_t cch = Recls_GetPathProperty(entry, &path[0], RECLS_NUM_ELEMENTS(path) - 1);; 00114 00115 path[cch] = '\0'; 00116 00117 /* ... squeeze it into 48 characters, ... */ 00118 cch = Recls_SqueezePath(path, &squeezedPath[0], RECLS_NUM_ELEMENTS(squeezedPath)); 00119 00120 /* ... determine type, ... */ 00121 isDirectory = Recls_IsFileDirectory(entry); 00122 00123 if(isDirectory) 00124 { 00125 /* ... calculate size, or ... */ 00126 size = Recls_CalcDirectoryEntrySize(entry); 00127 } 00128 else 00129 { 00130 /* ... elicit size, ... */ 00131 00132 Recls_GetSizeProperty(entry, &size); 00133 } 00134 00135 #if defined(RECLS_PLATFORM_IS_WIN32) 00136 ulSize = (unsigned long)size.QuadPart; 00137 #else /* ? OS */ 00138 ulSize = (unsigned long)size; 00139 #endif /* OS */ 00140 00141 printf( "%.*s: %s; %lu bytes\n" 00142 , (int)cch 00143 , squeezedPath 00144 , isDirectory ? "directory" : "file" 00145 , ulSize); 00146 00147 /* ... close the entry handle, ... */ 00148 Recls_CloseDetails(entry); 00149 00150 } /* ... and get the next entry. */ 00151 while(RECLS_SUCCEEDED(Recls_GetNextDetails(hSrch, &entry))); 00152 00153 /* Close the search handle. */ 00154 Recls_SearchClose(hSrch); 00155 00156 return EXIT_SUCCESS; 00157 } 00158 } 00159 } 00160 00161 /* ////////////////////////////////////////////////////////////////////// */
|
recls Library documentation © Synesis Software Pty Ltd, 2001-2006 |