![]() |
![]() |
![]() |
![]() |
||||
![]() |
![]() |
![]() |
![]() |
||||
|
test/C
directory is a test program C.c that exercises the recls C API (for Win32)test/Cpp
directory is a test program Cpp.cpp that exercises the recls C++ mapping (for Win32)test/STL
directory is a test program STL.cpp that exercises the recls STL mapping (for Win32)test/CS
directory is a test program WinForm.cs that exercises the recls C# mappingtest/COM
directory is a test program COM.cpp that exercises the recls COM mapping (for C++)test/COM/VBClient
directory is a test program VBClient.frm that exercises the recls COM mapping (for Visual Basic)test/D
directory are two test programs recls_test_1.d and recls_test_2.d that exercise the recls D mappingtest/Java
directory is a test program recls_test.java that exercises the recls Java mapping
/* ///////////////////////////////////////////////////////////////////////////// * File: C.c * * Purpose: Implementation file for the C project. * * Created: 15th August 2003 * Updated: 6th March 2005 * * Status: Wizard-generated * * License: (Licensed under the Synesis Software Open License) * * Copyright 2003-2004, Synesis Software Pty Ltd. * All rights reserved. * * www: http://www.synesis.com.au/software * * email: software@synesis.com.au * * This source code is placed into the public domain 2003 * by Synesis Software Pty Ltd. There are no restrictions * whatsoever to your use of the software. * * This source code is provided by Synesis Software Pty Ltd "as is" * and any warranties, whether expressed or implied, including, but * not limited to, the implied warranties of merchantability and * fitness for a particular purpose are disclaimed. In no event * shall the Synesis Software Pty Ltd be liable for any direct, * indirect, incidental, special, exemplary, or consequential * damages (including, but not limited to, procurement of * substitute goods or services; loss of use, data, or profits; or * business interruption) however caused and on any theory of * liability, whether in contract, strict liability, or tort * (including negligence or otherwise) arising in any way out of * the use of this software, even if advised of the possibility of * such damage. * * Neither the name of Synesis Software Pty Ltd nor the names of * any subdivisions, employees or agents of Synesis Software Pty * Ltd, nor the names of any other contributors to this software * may be used to endorse or promote products derived from this * software without specific prior written permission. * */ /* Remove this definition to use platform-specific aspects of the API */ #ifdef _DEBUG #else /* ? _DEBUG */ # define RECLS_PURE_API #endif /* _DEBUG */ /* Open-RJ Header Files */ #include <recls.h> #include <recls_assert.h> #include <recls_minmax.h> #if RECLS_VER < RECLS_VER_1_5_3 # error This file now requires version 1.5.3 or later of the recls C API #endif /* RECLS_VER < 1.5.3 */ /* Standard C Library Files */ #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #if defined(__GNUC__) # ifdef alloca # undef alloca # endif /* !alloca */ # define alloca __builtin_alloca #elif defined(WIN32) # include <malloc.h> # ifndef alloca # define alloca _alloca # endif /* !alloca */ #else /* ? OS */ # include <alloca.h> #endif /* OS */ #ifdef RECLS_PLATFORM_IS_UNIX # include <unistd.h> #endif /* RECLS_PLATFORM_IS_UNIX */ #ifdef _MSC_VER # include <crtdbg.h> #endif /* _MSC_VER */ /* ///////////////////////////////////////////////////////////////////////////// * Compiler compatibility */ #if defined(RECLS_COMPILER_IS_MSVC) # if _MSC_VER < 1300 # pragma warning(disable : 4127) # endif /* _MSC_VER < 1300 */ #endif /* compiler */ /* ///////////////////////////////////////////////////////////////////////////// * Macros */ #ifndef NUM_ELEMENTS # ifdef __DMC__ # define NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0])) # else /* ? compiler */ # define NUM_ELEMENTS(x) (sizeof(x) / sizeof(0[(x)])) # endif /* compiler */ #endif /* !NUM_ELEMENTS */ /* ///////////////////////////////////////////////////////////////////////////// * Constants and definitions */ static size_t path_max(void) { #if defined(WIN32) return 1 + _MAX_PATH; #elif defined(PATH_MAX) return 1 + PATH_MAX; #else /* ? PATH_MAX */ return 1 + 1 + pathconf("/", _PC_PATH_MAX); #endif /* PATH_MAX */ } #define alloca_path() ((recls_char_t*)alloca(path_max())) /* ///////////////////////////////////////////////////////////////////////////// * Forward declarations */ static void usage(int bExit, char const *reason); static int processDirectory(char const *rootDir, char const *pattern, long flags, int bSuccinct); /* ///////////////////////////////////////////////////////////////////////////// * Main */ int main(int argc, char *argv[]) { #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemState memState; #endif /* _MSC_VER && _MSC_VER */ int iRet = 0; int i; int totalFound = 0; int bAllHardDrives = 0; char const *host = NULL; char const *username = NULL; char const *password = NULL; char const *pattern = NULL; char const *rootDir = NULL; recls_uint32_t flags = RECLS_F_RECURSIVE; int bSuccinct = 0; for(i = 1; i < argc; ++i) { const char *arg = argv[i]; if(arg[0] == '-') { if(arg[1] == '-') { /* -- arguments */ } else { /* - arguments */ switch(arg[1]) { case '?': usage(1, NULL); break; case 'R': /* Do not recurse */ flags &= ~(RECLS_F_RECURSIVE); break; case 'p': /* Show directory parts */ flags |= RECLS_F_DIRECTORY_PARTS; break; case 'f': /* Find files */ flags |= RECLS_F_FILES; break; case 'd': /* Find directories */ flags |= RECLS_F_DIRECTORIES; break; case 'h': /* Searches from hard drives */ bAllHardDrives = 1; break; case 's': /* Show only the full path; WHEREIS functionality */ bSuccinct = 1; break; case 'H': /* FTP host */ host = arg + 2; break; case 'U': /* FTP username */ username = arg + 2; break; case 'P': /* FTP password */ password = arg + 2; break; default: usage(1, "Invalid argument(s) specified"); break; } } } else { /* other arguments */ if(NULL == pattern) { pattern = arg; } else if(NULL == rootDir) { rootDir = arg; } else { usage(1, "Invalid argument(s) specified"); } } } if( NULL != password && ( NULL == host || NULL == username)) { usage(1, "Must specify host and username if specifying password"); } if( NULL != username && NULL == host) { usage(1, "Must specify host if specifying username"); } if( NULL != host && bAllHardDrives) { usage(1, "-h flag meaningless to FTP searches"); } if( NULL != host && NULL == rootDir) { usage(1, "Must specify a root directory for FTP searches"); } #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemCheckpoint(&memState); #endif /* _MSC_VER && _MSC_VER */ /* Search for files if neither files or directories specified/ * * Even though this is not necessary, because the recls API provides the * same interpretation, it's best to be explicit. */ if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) { flags |= RECLS_F_FILES; } if(NULL == pattern) { pattern = Recls_GetWildcardsAll(); } if(NULL == rootDir) { rootDir = "."; } /* Check the size of rootDir */ { char const *filePath = ".."; char const *fslash = strrchr(filePath, '/'); #ifdef WIN32 char const *bslash = strrchr(filePath, '\\'); char const *slash = (NULL == fslash) ? bslash : (NULL == bslash) ? fslash : recls_max(bslash, fslash); #else /* ? WIN32 */ char const *slash = fslash; #endif /* WIN32 */ size_t cchDir = (NULL == slash) ? 0 : (size_t)(slash - filePath); char *dir = (char*)alloca(1 + cchDir); recls_uint32_t flags_ = RECLS_F_FILES | RECLS_F_DIRECTORIES; hrecls_t hSrch; recls_rc_t rc; cchDir[strncpy(dir, filePath, 1 + cchDir)] = '\0'; if(NULL != slash) { filePath += cchDir + 1; } rc = Recls_Search(dir, filePath, flags_, &hSrch); if(RECLS_SUCCEEDED(rc)) { recls_info_t entry; Recls_GetDetails(hSrch, &entry); Recls_CloseDetails(entry); Recls_SearchClose(hSrch); } } #ifdef x_DEBUG { recls_bool_t bEmpty = Recls_IsDirectoryEmpty(rootDir); recls_filesize_t size = Recls_CalcDirectorySize(rootDir); ((void)bEmpty); ((void)size); } { recls_info_t info; hrecls_t hSrch; recls_rc_t rc = Recls_Search(".", ".", RECLS_F_DIRECTORIES, &hSrch); Recls_GetDetails(hSrch, &info); Recls_IsDirectoryEntryEmpty(info); Recls_CloseDetails(info); Recls_SearchClose(hSrch); } #endif /* _DEBUG */ /* Initiate the search. */ if(bAllHardDrives) { recls_root_t roots[26]; size_t cRoots = Recls_GetRoots(roots, NUM_ELEMENTS(roots)); for(i = 0; i < (int)cRoots; ++i) { totalFound += processDirectory(roots[i].name, pattern, flags, bSuccinct); } } else { totalFound = processDirectory(rootDir, pattern, flags, bSuccinct); } fprintf(stdout, "Total found: %d\n", totalFound); #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemDumpAllObjectsSince(&memState); #endif /* _MSC_VER && _MSC_VER */ return iRet; } /* ///////////////////////////////////////////////////////////////////////////// * Function implementations */ void usage(int bExit, char const *reason) { fprintf(stderr, "recls C Test Program\n\n"); if(NULL != reason) { fprintf(stderr, " Error: %s\n\n", reason); } fprintf(stderr, "Usage: C++ [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]\n"); fprintf(stderr, "\t-R - does not recurse; recursive search is the default\n"); fprintf(stderr, "\t-p - evaluate and display directory parts\n"); fprintf(stderr, "\t-d - search for directories\n"); fprintf(stderr, "\t-f - search for files (this is the default, if -d is not specified)\n"); fprintf(stderr, "\t-h - searches from hard drive roots. (Ignore <root-dir>.)\n"); fprintf(stderr, "\t-s - succinct; shows only the path\n"); fprintf(stderr, "\t-H<host> - performs an FTP search on the given host\n"); fprintf(stderr, "\t-U<username> - username to use to log on to <host>\n"); fprintf(stderr, "\t-P<password> - password to use to log on to <host>\n"); fprintf(stderr, "\t<pattern> - search pattern, e.g. \"*.cpp\" (default is to search for all files)\n"); fprintf(stderr, "\t<root-dir> - root directory of search; default is current working directory\n"); if(bExit) { exit(EXIT_FAILURE); } } static int processDirectory(char const *rootDir, char const *pattern, long flags, int bSuccinct) { hrecls_t hSrch; recls_rc_t rc = Recls_Search(rootDir, pattern, flags, &hSrch); int totalFound = 0; if(RECLS_FAILED(rc)) { recls_char_t err[100]; Recls_GetErrorString(rc, err, 100); fprintf(stderr, "Failed to start search, with pattern \"%s\"; recls error: %s\n", pattern, err); } else { /* Iterate through the items, until done */ recls_info_t info; rc = Recls_GetDetails(hSrch, &info); for(; RECLS_SUCCEEDED(rc); rc = Recls_GetNextDetails(hSrch, &info)) { int i; int off; int extLen; int cDirParts; recls_char_t *path = alloca_path(); #ifdef RECLS_PLATFORM_API_WIN32 recls_char_t drive; #endif /* RECLS_PLATFORM_API_WIN32 */ recls_char_t *dir = alloca_path(); recls_char_t *dirPath = alloca_path(); recls_char_t *file = alloca_path(); recls_char_t *fileName = alloca_path(); recls_char_t *fileExt = alloca_path(); recls_char_t *pathCheck = alloca_path(); recls_char_t *dirCheck = alloca_path(); recls_char_t *fileCheck = alloca_path(); ++totalFound; pathCheck[0] = '\0'; dirCheck[0] = '\0'; fileCheck[0] = '\0'; #ifdef x_DEBUG { recls_uint32_t cBlocks; Recls_OutstandingDetails(hSrch, &cBlocks); printf("\n%d outstanding blocks\n", cBlocks); } #endif /* _DEBUG */ Recls_GetPathProperty(info, path, path_max()); printf(" %s\n", path); if(!bSuccinct) { Recls_GetDirectoryPathProperty(info, dirPath, path_max()); printf(" %s\n", dirPath); #ifdef RECLS_PLATFORM_API_WIN32 Recls_GetDriveProperty(info, &drive); if(isupper(path[0])) { drive = (recls_char_t)toupper(drive); } else { drive = (recls_char_t)tolower(drive); } printf(" %c\n", drive); sprintf(pathCheck, "%c:", drive); off = 2; #elif defined(RECLS_PLATFORM_IS_WIN32) off = 2; #else /* platform */ off = 0; #endif /* RECLS_PLATFORM_API_WIN32 */ Recls_GetDirectoryProperty(info, dir, path_max()); printf(" %*s\n", off + (int)strlen(dir), dir); if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { for(i = 0, cDirParts = Recls_GetDirectoryPartProperty(info, -1, NULL, 0); i < cDirParts; ++i) { recls_char_t *dirPart = alloca_path(); off += Recls_GetDirectoryPartProperty(info, i, dirPart, path_max()); printf(" %*s\n", off, dirPart); strcat(pathCheck, dirPart); strcat(dirCheck, dirPart); } } else { off += strlen(dir); strcat(pathCheck, dir); } off += Recls_GetFileProperty(info, file, path_max()); Recls_GetFileNameProperty(info, fileName, path_max()); extLen = Recls_GetFileExtProperty(info, fileExt, path_max()); printf(" %*s\n", off, file); printf(" %*s\n", off - (0 != extLen ? 1 + extLen : 0), fileName); printf(" %*s\n", off, fileExt); strcpy(fileCheck, fileName); if(0 < extLen) { strcat(fileCheck, "."); strcat(fileCheck, fileExt); } strcat(pathCheck, file); /* Now validate the components */ #if defined(RECLS_PLATFORM_IS_WIN32) && \ !defined(RECLS_PLATFORM_API_WIN32) recls_assert(0 == strcmp(path + 2, pathCheck)); if(0 != strcmp(path + 2, pathCheck)) #else /* ? RECLS_PLATFORM_API_WIN32 */ recls_assert(0 == strcmp(path, pathCheck)); if(0 != strcmp(path, pathCheck)) #endif /* RECLS_PLATFORM_API_WIN32 || !RECLS_PLATFORM_IS_WIN32 */ { fprintf(stderr, "Path is different from path components\n\tpath: %s\n\tparts: %s\n\n", path, pathCheck); abort(); } #if defined(RECLS_PLATFORM_API_WIN32) recls_assert(dirPath[0] == drive && 0 == strcmp(dir, dirPath + 2)); if( dirPath[0] != drive || 0 != strcmp(dir, dirPath + 2)) #elif defined(RECLS_PLATFORM_IS_WIN32) recls_assert(0 == strcmp(dir, dirPath + 2)); if(0 != strcmp(dir, dirPath + 2)) #else recls_assert(0 == strcmp(dir, dirPath)); if(0 != strcmp(dir, dirPath)) #endif /* RECLS_PLATFORM_API_WIN32 || !RECLS_PLATFORM_IS_WIN32 */ { fprintf(stderr, "DirectoryPath is different from Directory\n\tdirPath: %s\n\tdir: %s\n\n", dirPath, dir); abort(); } if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { recls_assert(0 == strcmp(dir, dirCheck)); if(0 != strcmp(dir, dirCheck)) { fprintf(stderr, "Directory is different from directory components\n\tpath: %s\n\tparts: %s\n\n", dir, dirCheck); abort(); } } recls_assert(0 == strcmp(file, fileCheck)); if(0 != strcmp(file, fileCheck)) { fprintf(stderr, "File is different from file components\n\tpath: %s\n\tparts: %s\n\n", file, fileCheck); abort(); } if(Recls_IsFileReadOnly(info)) { printf(" - Read-only\n"); } if(Recls_IsFileDirectory(info)) { printf(" - Directory\n"); } else { printf(" - File\n"); } if(Recls_IsFileLink(info)) { printf(" - Link\n"); } } Recls_CloseDetails(info); } if(rc != RECLS_RC_NO_MORE_DATA) { recls_char_t err[100]; Recls_GetErrorString(rc, err, 100); fprintf(stderr, "Search terminated prematurely; recls error: %s\n", err); } Recls_SearchClose(hSrch); } return totalFound; } /* ////////////////////////////////////////////////////////////////////////// * End of file */
00001 /* ///////////////////////////////////////////////////////////////////////////// 00002 * File: C.c 00003 * 00004 * Purpose: Implementation file for the C project. 00005 * 00006 * Created: 15th August 2003 00007 * Updated: 6th March 2005 00008 * 00009 * Status: Wizard-generated 00010 * 00011 * License: (Licensed under the Synesis Software Open License) 00012 * 00013 * Copyright 2003-2004, Synesis Software Pty Ltd. 00014 * All rights reserved. 00015 * 00016 * www: http://www.synesis.com.au/software 00017 * 00018 * email: software@synesis.com.au 00019 * 00020 * This source code is placed into the public domain 2003 00021 * by Synesis Software Pty Ltd. There are no restrictions 00022 * whatsoever to your use of the software. 00023 * 00024 * This source code is provided by Synesis Software Pty Ltd "as is" 00025 * and any warranties, whether expressed or implied, including, but 00026 * not limited to, the implied warranties of merchantability and 00027 * fitness for a particular purpose are disclaimed. In no event 00028 * shall the Synesis Software Pty Ltd be liable for any direct, 00029 * indirect, incidental, special, exemplary, or consequential 00030 * damages (including, but not limited to, procurement of 00031 * substitute goods or services; loss of use, data, or profits; or 00032 * business interruption) however caused and on any theory of 00033 * liability, whether in contract, strict liability, or tort 00034 * (including negligence or otherwise) arising in any way out of 00035 * the use of this software, even if advised of the possibility of 00036 * such damage. 00037 * 00038 * Neither the name of Synesis Software Pty Ltd nor the names of 00039 * any subdivisions, employees or agents of Synesis Software Pty 00040 * Ltd, nor the names of any other contributors to this software 00041 * may be used to endorse or promote products derived from this 00042 * software without specific prior written permission. 00043 * 00044 */ 00045 00048 /* Remove this definition to use platform-specific aspects of the API */ 00049 #ifdef _DEBUG 00050 #else /* ? _DEBUG */ 00051 # define RECLS_PURE_API 00052 #endif /* _DEBUG */ 00053 00054 /* Open-RJ Header Files */ 00055 #include <recls.h> 00056 #include <recls_assert.h> 00057 #include <recls_minmax.h> 00058 00059 #if RECLS_VER < RECLS_VER_1_5_3 00060 # error This file now requires version 1.5.3 or later of the recls C API 00061 #endif /* RECLS_VER < 1.5.3 */ 00062 00063 /* Standard C Library Files */ 00064 #include <ctype.h> 00065 #include <stdio.h> 00066 #include <stdlib.h> 00067 #include <string.h> 00068 #if defined(__GNUC__) 00069 # ifdef alloca 00070 # undef alloca 00071 # endif /* !alloca */ 00072 # define alloca __builtin_alloca 00073 #elif defined(WIN32) 00074 # include <malloc.h> 00075 # ifndef alloca 00076 # define alloca _alloca 00077 # endif /* !alloca */ 00078 #else /* ? OS */ 00079 # include <alloca.h> 00080 #endif /* OS */ 00081 00082 00083 #ifdef RECLS_PLATFORM_IS_UNIX 00084 # include <unistd.h> 00085 #endif /* RECLS_PLATFORM_IS_UNIX */ 00086 00087 #ifdef _MSC_VER 00088 # include <crtdbg.h> 00089 #endif /* _MSC_VER */ 00090 00091 /* ///////////////////////////////////////////////////////////////////////////// 00092 * Compiler compatibility 00093 */ 00094 00095 #if defined(RECLS_COMPILER_IS_MSVC) 00096 # if _MSC_VER < 1300 00097 # pragma warning(disable : 4127) 00098 # endif /* _MSC_VER < 1300 */ 00099 #endif /* compiler */ 00100 00101 /* ///////////////////////////////////////////////////////////////////////////// 00102 * Macros 00103 */ 00104 00105 #ifndef NUM_ELEMENTS 00106 # ifdef __DMC__ 00107 # define NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0])) 00108 # else /* ? compiler */ 00109 # define NUM_ELEMENTS(x) (sizeof(x) / sizeof(0[(x)])) 00110 # endif /* compiler */ 00111 #endif /* !NUM_ELEMENTS */ 00112 00113 /* ///////////////////////////////////////////////////////////////////////////// 00114 * Constants and definitions 00115 */ 00116 00117 static size_t path_max(void) 00118 { 00119 #if defined(WIN32) 00120 return 1 + _MAX_PATH; 00121 #elif defined(PATH_MAX) 00122 return 1 + PATH_MAX; 00123 #else /* ? PATH_MAX */ 00124 return 1 + 1 + pathconf("/", _PC_PATH_MAX); 00125 #endif /* PATH_MAX */ 00126 } 00127 00128 #define alloca_path() ((recls_char_t*)alloca(path_max())) 00129 00130 /* ///////////////////////////////////////////////////////////////////////////// 00131 * Forward declarations 00132 */ 00133 00134 static void usage(int bExit, char const *reason); 00135 static int processDirectory(char const *rootDir, char const *pattern, long flags, int bSuccinct); 00136 00137 /* ///////////////////////////////////////////////////////////////////////////// 00138 * Main 00139 */ 00140 00142 int main(int argc, char *argv[]) 00143 { 00144 #if defined(_MSC_VER) && \ 00145 defined(_DEBUG) 00146 _CrtMemState memState; 00147 #endif /* _MSC_VER && _MSC_VER */ 00148 int iRet = 0; 00149 int i; 00150 int totalFound = 0; 00151 int bAllHardDrives = 0; 00152 char const *host = NULL; 00153 char const *username = NULL; 00154 char const *password = NULL; 00155 char const *pattern = NULL; 00156 char const *rootDir = NULL; 00157 recls_uint32_t flags = RECLS_F_RECURSIVE; 00158 int bSuccinct = 0; 00159 00160 for(i = 1; i < argc; ++i) 00161 { 00162 const char *arg = argv[i]; 00163 00164 if(arg[0] == '-') 00165 { 00166 if(arg[1] == '-') 00167 { 00168 /* -- arguments */ 00169 } 00170 else 00171 { 00172 /* - arguments */ 00173 switch(arg[1]) 00174 { 00175 case '?': 00176 usage(1, NULL); 00177 break; 00178 case 'R': /* Do not recurse */ 00179 flags &= ~(RECLS_F_RECURSIVE); 00180 break; 00181 case 'p': /* Show directory parts */ 00182 flags |= RECLS_F_DIRECTORY_PARTS; 00183 break; 00184 case 'f': /* Find files */ 00185 flags |= RECLS_F_FILES; 00186 break; 00187 case 'd': /* Find directories */ 00188 flags |= RECLS_F_DIRECTORIES; 00189 break; 00190 case 'h': /* Searches from hard drives */ 00191 bAllHardDrives = 1; 00192 break; 00193 case 's': /* Show only the full path; WHEREIS functionality */ 00194 bSuccinct = 1; 00195 break; 00196 case 'H': /* FTP host */ 00197 host = arg + 2; 00198 break; 00199 case 'U': /* FTP username */ 00200 username = arg + 2; 00201 break; 00202 case 'P': /* FTP password */ 00203 password = arg + 2; 00204 break; 00205 default: 00206 usage(1, "Invalid argument(s) specified"); 00207 break; 00208 } 00209 } 00210 } 00211 else 00212 { 00213 /* other arguments */ 00214 if(NULL == pattern) 00215 { 00216 pattern = arg; 00217 } 00218 else if(NULL == rootDir) 00219 { 00220 rootDir = arg; 00221 } 00222 else 00223 { 00224 usage(1, "Invalid argument(s) specified"); 00225 } 00226 } 00227 } 00228 00229 if( NULL != password && 00230 ( NULL == host || 00231 NULL == username)) 00232 { 00233 usage(1, "Must specify host and username if specifying password"); 00234 } 00235 00236 if( NULL != username && 00237 NULL == host) 00238 { 00239 usage(1, "Must specify host if specifying username"); 00240 } 00241 00242 if( NULL != host && 00243 bAllHardDrives) 00244 { 00245 usage(1, "-h flag meaningless to FTP searches"); 00246 } 00247 00248 if( NULL != host && 00249 NULL == rootDir) 00250 { 00251 usage(1, "Must specify a root directory for FTP searches"); 00252 } 00253 00254 #if defined(_MSC_VER) && \ 00255 defined(_DEBUG) 00256 _CrtMemCheckpoint(&memState); 00257 #endif /* _MSC_VER && _MSC_VER */ 00258 00259 /* Search for files if neither files or directories specified/ 00260 * 00261 * Even though this is not necessary, because the recls API provides the 00262 * same interpretation, it's best to be explicit. 00263 */ 00264 if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) 00265 { 00266 flags |= RECLS_F_FILES; 00267 } 00268 00269 if(NULL == pattern) 00270 { 00271 pattern = Recls_GetWildcardsAll(); 00272 } 00273 00274 if(NULL == rootDir) 00275 { 00276 rootDir = "."; 00277 } 00278 00279 /* Check the size of rootDir */ 00280 00281 { 00282 char const *filePath = ".."; 00283 char const *fslash = strrchr(filePath, '/'); 00284 #ifdef WIN32 00285 char const *bslash = strrchr(filePath, '\\'); 00286 char const *slash = (NULL == fslash) ? bslash : (NULL == bslash) ? fslash : recls_max(bslash, fslash); 00287 #else /* ? WIN32 */ 00288 char const *slash = fslash; 00289 #endif /* WIN32 */ 00290 size_t cchDir = (NULL == slash) ? 0 : (size_t)(slash - filePath); 00291 char *dir = (char*)alloca(1 + cchDir); 00292 00293 recls_uint32_t flags_ = RECLS_F_FILES | RECLS_F_DIRECTORIES; 00294 hrecls_t hSrch; 00295 recls_rc_t rc; 00296 00297 cchDir[strncpy(dir, filePath, 1 + cchDir)] = '\0'; 00298 00299 if(NULL != slash) 00300 { 00301 filePath += cchDir + 1; 00302 } 00303 00304 rc = Recls_Search(dir, filePath, flags_, &hSrch); 00305 00306 if(RECLS_SUCCEEDED(rc)) 00307 { 00308 recls_info_t entry; 00309 00310 Recls_GetDetails(hSrch, &entry); 00311 00312 Recls_CloseDetails(entry); 00313 00314 Recls_SearchClose(hSrch); 00315 } 00316 } 00317 00318 00319 #ifdef x_DEBUG 00320 { 00321 recls_bool_t bEmpty = Recls_IsDirectoryEmpty(rootDir); 00322 00323 recls_filesize_t size = Recls_CalcDirectorySize(rootDir); 00324 00325 00326 ((void)bEmpty); 00327 ((void)size); 00328 } 00329 00330 { 00331 recls_info_t info; 00332 hrecls_t hSrch; 00333 recls_rc_t rc = Recls_Search(".", ".", RECLS_F_DIRECTORIES, &hSrch); 00334 00335 Recls_GetDetails(hSrch, &info); 00336 00337 Recls_IsDirectoryEntryEmpty(info); 00338 00339 Recls_CloseDetails(info); 00340 Recls_SearchClose(hSrch); 00341 } 00342 00343 #endif /* _DEBUG */ 00344 00345 /* Initiate the search. */ 00346 if(bAllHardDrives) 00347 { 00348 recls_root_t roots[26]; 00349 size_t cRoots = Recls_GetRoots(roots, NUM_ELEMENTS(roots)); 00350 00351 for(i = 0; i < (int)cRoots; ++i) 00352 { 00353 totalFound += processDirectory(roots[i].name, pattern, flags, bSuccinct); 00354 } 00355 } 00356 else 00357 { 00358 totalFound = processDirectory(rootDir, pattern, flags, bSuccinct); 00359 } 00360 00361 fprintf(stdout, "Total found: %d\n", totalFound); 00362 00363 #if defined(_MSC_VER) && \ 00364 defined(_DEBUG) 00365 _CrtMemDumpAllObjectsSince(&memState); 00366 #endif /* _MSC_VER && _MSC_VER */ 00367 00368 return iRet; 00369 } 00370 00371 /* ///////////////////////////////////////////////////////////////////////////// 00372 * Function implementations 00373 */ 00374 00376 void usage(int bExit, char const *reason) 00377 { 00378 fprintf(stderr, "recls C Test Program\n\n"); 00379 00380 if(NULL != reason) 00381 { 00382 fprintf(stderr, " Error: %s\n\n", reason); 00383 } 00384 00385 fprintf(stderr, "Usage: C++ [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]\n"); 00386 fprintf(stderr, "\t-R - does not recurse; recursive search is the default\n"); 00387 fprintf(stderr, "\t-p - evaluate and display directory parts\n"); 00388 fprintf(stderr, "\t-d - search for directories\n"); 00389 fprintf(stderr, "\t-f - search for files (this is the default, if -d is not specified)\n"); 00390 fprintf(stderr, "\t-h - searches from hard drive roots. (Ignore <root-dir>.)\n"); 00391 fprintf(stderr, "\t-s - succinct; shows only the path\n"); 00392 fprintf(stderr, "\t-H<host> - performs an FTP search on the given host\n"); 00393 fprintf(stderr, "\t-U<username> - username to use to log on to <host>\n"); 00394 fprintf(stderr, "\t-P<password> - password to use to log on to <host>\n"); 00395 fprintf(stderr, "\t<pattern> - search pattern, e.g. \"*.cpp\" (default is to search for all files)\n"); 00396 fprintf(stderr, "\t<root-dir> - root directory of search; default is current working directory\n"); 00397 00398 if(bExit) 00399 { 00400 exit(EXIT_FAILURE); 00401 } 00402 } 00403 00404 static int processDirectory(char const *rootDir, char const *pattern, long flags, int bSuccinct) 00405 { 00406 hrecls_t hSrch; 00407 recls_rc_t rc = Recls_Search(rootDir, pattern, flags, &hSrch); 00408 int totalFound = 0; 00409 00410 if(RECLS_FAILED(rc)) 00411 { 00412 recls_char_t err[100]; 00413 00414 Recls_GetErrorString(rc, err, 100); 00415 00416 fprintf(stderr, "Failed to start search, with pattern \"%s\"; recls error: %s\n", pattern, err); 00417 } 00418 else 00419 { 00420 /* Iterate through the items, until done */ 00421 recls_info_t info; 00422 00423 rc = Recls_GetDetails(hSrch, &info); 00424 00425 for(; RECLS_SUCCEEDED(rc); rc = Recls_GetNextDetails(hSrch, &info)) 00426 { 00427 int i; 00428 int off; 00429 int extLen; 00430 int cDirParts; 00431 recls_char_t *path = alloca_path(); 00432 #ifdef RECLS_PLATFORM_API_WIN32 00433 recls_char_t drive; 00434 #endif /* RECLS_PLATFORM_API_WIN32 */ 00435 recls_char_t *dir = alloca_path(); 00436 recls_char_t *dirPath = alloca_path(); 00437 recls_char_t *file = alloca_path(); 00438 recls_char_t *fileName = alloca_path(); 00439 recls_char_t *fileExt = alloca_path(); 00440 00441 recls_char_t *pathCheck = alloca_path(); 00442 recls_char_t *dirCheck = alloca_path(); 00443 recls_char_t *fileCheck = alloca_path(); 00444 00445 ++totalFound; 00446 00447 pathCheck[0] = '\0'; 00448 dirCheck[0] = '\0'; 00449 fileCheck[0] = '\0'; 00450 00451 #ifdef x_DEBUG 00452 { 00453 recls_uint32_t cBlocks; 00454 Recls_OutstandingDetails(hSrch, &cBlocks); 00455 printf("\n%d outstanding blocks\n", cBlocks); 00456 } 00457 #endif /* _DEBUG */ 00458 00459 Recls_GetPathProperty(info, path, path_max()); 00460 printf(" %s\n", path); 00461 if(!bSuccinct) 00462 { 00463 Recls_GetDirectoryPathProperty(info, dirPath, path_max()); 00464 printf(" %s\n", dirPath); 00465 #ifdef RECLS_PLATFORM_API_WIN32 00466 Recls_GetDriveProperty(info, &drive); 00467 if(isupper(path[0])) 00468 { 00469 drive = (recls_char_t)toupper(drive); 00470 } 00471 else 00472 { 00473 drive = (recls_char_t)tolower(drive); 00474 } 00475 printf(" %c\n", drive); 00476 sprintf(pathCheck, "%c:", drive); 00477 off = 2; 00478 #elif defined(RECLS_PLATFORM_IS_WIN32) 00479 off = 2; 00480 #else /* platform */ 00481 off = 0; 00482 #endif /* RECLS_PLATFORM_API_WIN32 */ 00483 Recls_GetDirectoryProperty(info, dir, path_max()); 00484 printf(" %*s\n", off + (int)strlen(dir), dir); 00485 00486 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00487 { 00488 for(i = 0, cDirParts = Recls_GetDirectoryPartProperty(info, -1, NULL, 0); i < cDirParts; ++i) 00489 { 00490 recls_char_t *dirPart = alloca_path(); 00491 00492 off += Recls_GetDirectoryPartProperty(info, i, dirPart, path_max()); 00493 printf(" %*s\n", off, dirPart); 00494 00495 strcat(pathCheck, dirPart); 00496 strcat(dirCheck, dirPart); 00497 } 00498 } 00499 else 00500 { 00501 off += strlen(dir); 00502 strcat(pathCheck, dir); 00503 } 00504 00505 off += Recls_GetFileProperty(info, file, path_max()); 00506 Recls_GetFileNameProperty(info, fileName, path_max()); 00507 extLen = Recls_GetFileExtProperty(info, fileExt, path_max()); 00508 printf(" %*s\n", off, file); 00509 printf(" %*s\n", off - (0 != extLen ? 1 + extLen : 0), fileName); 00510 printf(" %*s\n", off, fileExt); 00511 strcpy(fileCheck, fileName); 00512 if(0 < extLen) 00513 { 00514 strcat(fileCheck, "."); 00515 strcat(fileCheck, fileExt); 00516 } 00517 00518 strcat(pathCheck, file); 00519 00520 /* Now validate the components */ 00521 #if defined(RECLS_PLATFORM_IS_WIN32) && \ 00522 !defined(RECLS_PLATFORM_API_WIN32) 00523 recls_assert(0 == strcmp(path + 2, pathCheck)); 00524 if(0 != strcmp(path + 2, pathCheck)) 00525 #else /* ? RECLS_PLATFORM_API_WIN32 */ 00526 recls_assert(0 == strcmp(path, pathCheck)); 00527 if(0 != strcmp(path, pathCheck)) 00528 #endif /* RECLS_PLATFORM_API_WIN32 || !RECLS_PLATFORM_IS_WIN32 */ 00529 { 00530 fprintf(stderr, "Path is different from path components\n\tpath: %s\n\tparts: %s\n\n", path, pathCheck); 00531 00532 abort(); 00533 } 00534 #if defined(RECLS_PLATFORM_API_WIN32) 00535 recls_assert(dirPath[0] == drive && 0 == strcmp(dir, dirPath + 2)); 00536 if( dirPath[0] != drive || 00537 0 != strcmp(dir, dirPath + 2)) 00538 #elif defined(RECLS_PLATFORM_IS_WIN32) 00539 recls_assert(0 == strcmp(dir, dirPath + 2)); 00540 if(0 != strcmp(dir, dirPath + 2)) 00541 #else 00542 recls_assert(0 == strcmp(dir, dirPath)); 00543 if(0 != strcmp(dir, dirPath)) 00544 #endif /* RECLS_PLATFORM_API_WIN32 || !RECLS_PLATFORM_IS_WIN32 */ 00545 { 00546 fprintf(stderr, "DirectoryPath is different from Directory\n\tdirPath: %s\n\tdir: %s\n\n", dirPath, dir); 00547 00548 abort(); 00549 } 00550 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00551 { 00552 recls_assert(0 == strcmp(dir, dirCheck)); 00553 if(0 != strcmp(dir, dirCheck)) 00554 { 00555 fprintf(stderr, "Directory is different from directory components\n\tpath: %s\n\tparts: %s\n\n", dir, dirCheck); 00556 00557 abort(); 00558 } 00559 } 00560 recls_assert(0 == strcmp(file, fileCheck)); 00561 if(0 != strcmp(file, fileCheck)) 00562 { 00563 fprintf(stderr, "File is different from file components\n\tpath: %s\n\tparts: %s\n\n", file, fileCheck); 00564 00565 abort(); 00566 } 00567 00568 if(Recls_IsFileReadOnly(info)) 00569 { 00570 printf(" - Read-only\n"); 00571 } 00572 if(Recls_IsFileDirectory(info)) 00573 { 00574 printf(" - Directory\n"); 00575 } 00576 else 00577 { 00578 printf(" - File\n"); 00579 } 00580 if(Recls_IsFileLink(info)) 00581 { 00582 printf(" - Link\n"); 00583 } 00584 } 00585 00586 Recls_CloseDetails(info); 00587 } 00588 00589 if(rc != RECLS_RC_NO_MORE_DATA) 00590 { 00591 recls_char_t err[100]; 00592 00593 Recls_GetErrorString(rc, err, 100); 00594 00595 fprintf(stderr, "Search terminated prematurely; recls error: %s\n", err); 00596 } 00597 00598 Recls_SearchClose(hSrch); 00599 } 00600 00601 return totalFound; 00602 } 00603 00604 /* ////////////////////////////////////////////////////////////////////////// 00605 * End of file 00606 */
/* ///////////////////////////////////////////////////////////////////////////// * File: Cpp.cpp * * Purpose: Implementation file for the Cpp project. * * Created: 16th August 2003 * Updated: 5th March 2005 * * Status: Wizard-generated * * License: (Licensed under the Synesis Software Open License) * * Copyright 2003-2005, Synesis Software Pty Ltd. * All rights reserved. * * www: http://www.synesis.com.au/software * * email: software@synesis.com.au * * This source code is placed into the public domain 2003 * by Synesis Software Pty Ltd. There are no restrictions * whatsoever to your use of the software. * * This source code is provided by Synesis Software Pty Ltd "as is" * and any warranties, whether expressed or implied, including, but * not limited to, the implied warranties of merchantability and * fitness for a particular purpose are disclaimed. In no event * shall the Synesis Software Pty Ltd be liable for any direct, * indirect, incidental, special, exemplary, or consequential * damages (including, but not limited to, procurement of * substitute goods or services; loss of use, data, or profits; or * business interruption) however caused and on any theory of * liability, whether in contract, strict liability, or tort * (including negligence or otherwise) arising in any way out of * the use of this software, even if advised of the possibility of * such damage. * * Neither the name of Synesis Software Pty Ltd nor the names of * any subdivisions, employees or agents of Synesis Software Pty * Ltd, nor the names of any other contributors to this software * may be used to endorse or promote products derived from this * software without specific prior written permission. * */ /* Define this definition to retrict use to platform-independent aspects of the API */ //#define RECLS_PURE_API #include <stdio.h> #include <recls.h> #if RECLS_VER < RECLS_VER_1_1_1 # error This file now requires version 1.1.1 or later of the recls C API #endif /* RECLS_VER < 1.1.1 */ #if defined(RECLS_STRICT) && \ !defined(RECLS_COMPILER_IS_DMC) # include <stlsoft_nulldef.h> #endif /* RECLS_STRICT && !RECLS_COMPILER_IS_DMC */ #ifdef RECLS_COMPILER_IS_MSVC # include <crtdbg.h> # pragma warning(disable : 4530) #endif /* _MSC_VER */ #if RECLS_VER >= RECLS_VER_1_5_1 && \ defined(RECLS_PLATFORM_IS_WIN32) # define RECLS_TEST_CPP_USING_FTP #endif /* RECLS_VER >= RECLS_VER_1_5_1 && RECLS_PLATFORM_IS_WIN32 */ #include <recls_assert.h> #include <recls/cpp/filesearch.hpp> #if defined(RECLS_TEST_CPP_USING_FTP) # include <recls/cpp/ftpsearch.hpp> #endif /* RECLS_TEST_CPP_USING_FTP */ /* ///////////////////////////////////////////////////////////////////////////// * Namespace */ using recls::recls_uint32_t; using recls::recls_char_t; using recls::RECLS_F_FILES; using recls::RECLS_F_DIRECTORIES; using recls::RECLS_F_RECURSIVE; using recls::RECLS_F_DIRECTORY_PARTS; /* ///////////////////////////////////////////////////////////////////////////// * Forward declarations */ static void usage(int bExit, char const *reason); static int processSearch(reclspp::Search &search, int bSuccinct, recls_uint32_t flags); /* ///////////////////////////////////////////////////////////////////////////// * Main */ int main(int argc, char *argv[]) { #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemState memState; #endif /* _MSC_VER && _MSC_VER */ int iRet = 0; int i; int totalFound; int bAllHardDrives = 0; char const *host = NULL; char const *username = NULL; char const *password = NULL; char const *pattern = NULL; char const *rootDir = NULL; recls_uint32_t flags = RECLS_F_RECURSIVE; int bSuccinct = false; for(i = 1; i < argc; ++i) { const char *arg = argv[i]; if(arg[0] == '-') { if(arg[1] == '-') { /* -- arguments */ } else { /* - arguments */ switch(arg[1]) { case '?': usage(1, NULL); break; case 'R': /* Do not recurse */ flags &= ~(RECLS_F_RECURSIVE); break; case 'p': /* Show directory parts */ flags |= RECLS_F_DIRECTORY_PARTS; break; case 'f': /* Find files */ flags |= RECLS_F_FILES; break; case 'd': /* Find directories */ flags |= RECLS_F_DIRECTORIES; break; case 'h': /* Searches from hard drives */ bAllHardDrives = true; break; case 's': /* Show only the full path; WHEREIS functionality */ bSuccinct = true; break; case 'H': /* FTP host */ host = arg + 2; break; case 'U': /* FTP username */ username = arg + 2; break; case 'P': /* FTP password */ password = arg + 2; break; default: usage(1, "Invalid argument(s) specified"); break; } } } else { /* other arguments */ if(NULL == pattern) { pattern = arg; } else if(NULL == rootDir) { rootDir = arg; } else { usage(1, "Invalid argument(s) specified"); } } } if( NULL != password && ( NULL == host || NULL == username)) { usage(1, "Must specify host and username if specifying password"); } if( NULL != username && NULL == host) { usage(1, "Must specify host if specifying username"); } if( NULL != host && bAllHardDrives) { usage(1, "-h flag meaningless to FTP searches"); } if( NULL != host && NULL == rootDir) { usage(1, "Must specify a root directory for FTP searches"); } #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemCheckpoint(&memState); #endif /* _MSC_VER && _MSC_VER */ // Search for files if neither files or directories specified // // Even though this is not necessary, because the recls API provides the // same interpretation, it's best to be explicit. if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) { flags |= RECLS_F_FILES; } /* Now conduct the search */ #if defined(RECLS_TEST_CPP_USING_FTP) if(NULL != host) { if(NULL == pattern) { pattern = recls::Recls_GetWildcardsAll(); } if(NULL == rootDir) { rootDir = "/"; } reclspp::FtpSearch search(host, username, password, rootDir, pattern, flags); totalFound = processSearch(search, bSuccinct, flags); } else #endif /* RECLS_TEST_CPP_USING_FTP */ { if(NULL == pattern) { pattern = recls::Recls_GetWildcardsAll(); } if(NULL == rootDir) { rootDir = "."; } reclspp::FileSearch search(rootDir, pattern, flags); totalFound = processSearch(search, bSuccinct, flags); } fprintf(stdout, "Total found: %d\n", totalFound); #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemDumpAllObjectsSince(&memState); #endif /* _MSC_VER && _MSC_VER */ return iRet; } /* ///////////////////////////////////////////////////////////////////////////// * Function implementations */ static void usage(int bExit, char const *reason) { fprintf(stderr, "recls C++ Test Program\n\n"); if(NULL != reason) { fprintf(stderr, " Error: %s\n\n", reason); } fprintf(stderr, "Usage: Cpp [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]\n"); fprintf(stderr, "\t-R - does not recurse; recursive search is the default\n"); fprintf(stderr, "\t-p - evaluate and display directory parts\n"); fprintf(stderr, "\t-d - search for directories\n"); fprintf(stderr, "\t-f - search for files (this is the default, if -d is not specified)\n"); fprintf(stderr, "\t-h - searches from hard drive roots. (Ignore <root-dir>.)\n"); fprintf(stderr, "\t-s - succinct; shows only the path\n"); fprintf(stderr, "\t-H<host> - performs an FTP search on the given host\n"); fprintf(stderr, "\t-U<username> - username to use to log on to <host>\n"); fprintf(stderr, "\t-P<password> - password to use to log on to <host>\n"); fprintf(stderr, "\t<pattern> - search pattern, e.g. \"*.cpp\"; default is to search for all files\n"); fprintf(stderr, "\t<root-dir> - root directory of search; default is current working directory\n"); if(bExit) { exit(EXIT_FAILURE); } } /* ///////////////////////////////////////////////////////////////////////////// * Function implementations */ static int processSearch(reclspp::Search &search, int bSuccinct, recls_uint32_t flags) { int totalFound = 0; for(; search.HasMoreElements(); search.GetNext()) { using reclspp::string_t; ++totalFound; reclspp::FileEntry fileEntry = search.GetCurrentEntry(); #ifdef RECLS_PLATFORM_API_WIN32 recls_char_t drive = fileEntry.GetDrive(); #endif /* RECLS_PLATFORM_API_WIN32 */ string_t path = fileEntry.GetPath(); string_t dir = fileEntry.GetDirectory(); string_t dirPath = fileEntry.GetDirectoryPath(); reclspp::DirectoryParts dirParts = fileEntry.GetDirectoryParts(); string_t file = fileEntry.GetFile(); string_t fileName = fileEntry.GetFileName(); string_t fileExt = fileEntry.GetFileExt(); string_t pathCheck; string_t dirPartsCheck; string_t fileCheck; #ifdef x_DEBUG reclspp::FileEntry entry2; if( fileEntry && !entry2) { fprintf(stderr, "Good entry\n"); } if(!fileEntry) { fprintf(stderr, "Bad entry\n"); } #endif /* _DEBUG */ #ifdef _DEBUG { recls_uint32_t cBlocks = search.GetNumOutstandingDetails(); printf("\n%lu outstanding block(s)\n", cBlocks); } #endif /* _DEBUG */ printf(" %s\n", path.c_str()); if(!bSuccinct) { int off = 0; printf(" %s\n", dirPath.c_str()); #ifdef RECLS_PLATFORM_API_WIN32 if(0 != drive) { if(isupper(path[0])) { drive = (recls_char_t)toupper(drive); } else { drive = (recls_char_t)tolower(drive); } printf(" %c\n", drive); pathCheck += drive; pathCheck += ':'; off += 2; } #endif /* RECLS_PLATFORM_API_WIN32 */ printf(" %s\n", dir.c_str()); if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { int i; int cDirParts; for(i = 0, cDirParts = dirParts.size(); i < cDirParts; ++i) { string_t dirPart = dirParts[i]; off += dirPart.length(); printf(" %*s\n", off, dirPart.c_str()); pathCheck += dirPart; dirPartsCheck += dirPart; } } else { off += dir.length(); pathCheck += dir; } off += file.length(); printf(" %*s\n", off, file.c_str()); if(fileExt.length() > 0) { printf(" %*s\n", off - (1 + fileExt.length()), fileName.c_str()); printf(" %*s\n", off, fileExt.c_str()); } else { printf(" %*s\n", off, fileName.c_str()); } fileCheck = fileName; if(0 < fileExt.length()) { fileCheck += '.'; fileCheck += fileExt; } pathCheck += file; // Now validate the components recls_assert(path == pathCheck); if(path != pathCheck) { fprintf(stderr, "Path is different from path components\n\tpath: %s\n\tparts: %s\n\n", path.c_str(), pathCheck.c_str()); abort(); } #ifdef RECLS_PLATFORM_API_WIN32 recls_assert(0 == drive || (dirPath[0] == drive && dir == dirPath.c_str() + 2)); recls_assert(0 != drive || dir == dirPath); if( ( 0 != drive && ( dirPath[0] != drive || dir != dirPath.c_str() + 2)) || ( 0 == drive && dir != dirPath)) #else recls_assert(dirPath == dir); if(dirPath != dir) #endif /* RECLS_PLATFORM_API_WIN32 */ { fprintf(stderr, "DirectoryPath is different from Directory\n\tdirPath: %s\n\tdir: %s\n\n", dirPath.c_str(), dir.c_str()); abort(); } if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { recls_assert(dir == dirPartsCheck); if(dir != dirPartsCheck) { fprintf(stderr, "Directory is different from directory components\n\tpath: %s\n\tparts: %s\n\n", dir.c_str(), dirPartsCheck.c_str()); abort(); } } recls_assert(file == fileCheck); if(file != fileCheck) { fprintf(stderr, "File is different from file components\n\tpath: %s\n\tparts: %s\n\n", file.c_str(), fileCheck.c_str()); abort(); } if(fileEntry.IsReadOnly()) { printf(" - Read-only\n"); } if(fileEntry.IsDirectory()) { printf(" - Directory\n"); } else { printf(" - File\n"); } if(fileEntry.IsLink()) { printf(" - Link\n"); } } } return totalFound; } /* ////////////////////////////////////////////////////////////////////////// * End of file */
00001 /* ///////////////////////////////////////////////////////////////////////////// 00002 * File: Cpp.cpp 00003 * 00004 * Purpose: Implementation file for the Cpp project. 00005 * 00006 * Created: 16th August 2003 00007 * Updated: 5th March 2005 00008 * 00009 * Status: Wizard-generated 00010 * 00011 * License: (Licensed under the Synesis Software Open License) 00012 * 00013 * Copyright 2003-2005, Synesis Software Pty Ltd. 00014 * All rights reserved. 00015 * 00016 * www: http://www.synesis.com.au/software 00017 * 00018 * email: software@synesis.com.au 00019 * 00020 * This source code is placed into the public domain 2003 00021 * by Synesis Software Pty Ltd. There are no restrictions 00022 * whatsoever to your use of the software. 00023 * 00024 * This source code is provided by Synesis Software Pty Ltd "as is" 00025 * and any warranties, whether expressed or implied, including, but 00026 * not limited to, the implied warranties of merchantability and 00027 * fitness for a particular purpose are disclaimed. In no event 00028 * shall the Synesis Software Pty Ltd be liable for any direct, 00029 * indirect, incidental, special, exemplary, or consequential 00030 * damages (including, but not limited to, procurement of 00031 * substitute goods or services; loss of use, data, or profits; or 00032 * business interruption) however caused and on any theory of 00033 * liability, whether in contract, strict liability, or tort 00034 * (including negligence or otherwise) arising in any way out of 00035 * the use of this software, even if advised of the possibility of 00036 * such damage. 00037 * 00038 * Neither the name of Synesis Software Pty Ltd nor the names of 00039 * any subdivisions, employees or agents of Synesis Software Pty 00040 * Ltd, nor the names of any other contributors to this software 00041 * may be used to endorse or promote products derived from this 00042 * software without specific prior written permission. 00043 * 00044 */ 00045 00048 /* Define this definition to retrict use to platform-independent aspects of the API */ 00049 //#define RECLS_PURE_API 00050 00051 #include <stdio.h> 00052 00053 #include <recls.h> 00054 00055 #if RECLS_VER < RECLS_VER_1_1_1 00056 # error This file now requires version 1.1.1 or later of the recls C API 00057 #endif /* RECLS_VER < 1.1.1 */ 00058 00059 #if defined(RECLS_STRICT) && \ 00060 !defined(RECLS_COMPILER_IS_DMC) 00061 # include <stlsoft_nulldef.h> 00062 #endif /* RECLS_STRICT && !RECLS_COMPILER_IS_DMC */ 00063 00064 #ifdef RECLS_COMPILER_IS_MSVC 00065 # include <crtdbg.h> 00066 # pragma warning(disable : 4530) 00067 #endif /* _MSC_VER */ 00068 00069 #if RECLS_VER >= RECLS_VER_1_5_1 && \ 00070 defined(RECLS_PLATFORM_IS_WIN32) 00071 # define RECLS_TEST_CPP_USING_FTP 00072 #endif /* RECLS_VER >= RECLS_VER_1_5_1 && RECLS_PLATFORM_IS_WIN32 */ 00073 00074 #include <recls_assert.h> 00075 #include <recls/cpp/filesearch.hpp> 00076 #if defined(RECLS_TEST_CPP_USING_FTP) 00077 # include <recls/cpp/ftpsearch.hpp> 00078 #endif /* RECLS_TEST_CPP_USING_FTP */ 00079 00080 /* ///////////////////////////////////////////////////////////////////////////// 00081 * Namespace 00082 */ 00083 00084 using recls::recls_uint32_t; 00085 using recls::recls_char_t; 00086 using recls::RECLS_F_FILES; 00087 using recls::RECLS_F_DIRECTORIES; 00088 using recls::RECLS_F_RECURSIVE; 00089 using recls::RECLS_F_DIRECTORY_PARTS; 00090 00091 /* ///////////////////////////////////////////////////////////////////////////// 00092 * Forward declarations 00093 */ 00094 00095 static void usage(int bExit, char const *reason); 00096 static int processSearch(reclspp::Search &search, int bSuccinct, recls_uint32_t flags); 00097 00098 /* ///////////////////////////////////////////////////////////////////////////// 00099 * Main 00100 */ 00101 00103 int main(int argc, char *argv[]) 00104 { 00105 #if defined(_MSC_VER) && \ 00106 defined(_DEBUG) 00107 _CrtMemState memState; 00108 #endif /* _MSC_VER && _MSC_VER */ 00109 int iRet = 0; 00110 int i; 00111 int totalFound; 00112 int bAllHardDrives = 0; 00113 char const *host = NULL; 00114 char const *username = NULL; 00115 char const *password = NULL; 00116 char const *pattern = NULL; 00117 char const *rootDir = NULL; 00118 recls_uint32_t flags = RECLS_F_RECURSIVE; 00119 int bSuccinct = false; 00120 00121 for(i = 1; i < argc; ++i) 00122 { 00123 const char *arg = argv[i]; 00124 00125 if(arg[0] == '-') 00126 { 00127 if(arg[1] == '-') 00128 { 00129 /* -- arguments */ 00130 } 00131 else 00132 { 00133 /* - arguments */ 00134 switch(arg[1]) 00135 { 00136 case '?': 00137 usage(1, NULL); 00138 break; 00139 case 'R': /* Do not recurse */ 00140 flags &= ~(RECLS_F_RECURSIVE); 00141 break; 00142 case 'p': /* Show directory parts */ 00143 flags |= RECLS_F_DIRECTORY_PARTS; 00144 break; 00145 case 'f': /* Find files */ 00146 flags |= RECLS_F_FILES; 00147 break; 00148 case 'd': /* Find directories */ 00149 flags |= RECLS_F_DIRECTORIES; 00150 break; 00151 case 'h': /* Searches from hard drives */ 00152 bAllHardDrives = true; 00153 break; 00154 case 's': /* Show only the full path; WHEREIS functionality */ 00155 bSuccinct = true; 00156 break; 00157 case 'H': /* FTP host */ 00158 host = arg + 2; 00159 break; 00160 case 'U': /* FTP username */ 00161 username = arg + 2; 00162 break; 00163 case 'P': /* FTP password */ 00164 password = arg + 2; 00165 break; 00166 default: 00167 usage(1, "Invalid argument(s) specified"); 00168 break; 00169 } 00170 } 00171 } 00172 else 00173 { 00174 /* other arguments */ 00175 if(NULL == pattern) 00176 { 00177 pattern = arg; 00178 } 00179 else if(NULL == rootDir) 00180 { 00181 rootDir = arg; 00182 } 00183 else 00184 { 00185 usage(1, "Invalid argument(s) specified"); 00186 } 00187 } 00188 } 00189 00190 if( NULL != password && 00191 ( NULL == host || 00192 NULL == username)) 00193 { 00194 usage(1, "Must specify host and username if specifying password"); 00195 } 00196 00197 if( NULL != username && 00198 NULL == host) 00199 { 00200 usage(1, "Must specify host if specifying username"); 00201 } 00202 00203 if( NULL != host && 00204 bAllHardDrives) 00205 { 00206 usage(1, "-h flag meaningless to FTP searches"); 00207 } 00208 00209 if( NULL != host && 00210 NULL == rootDir) 00211 { 00212 usage(1, "Must specify a root directory for FTP searches"); 00213 } 00214 00215 #if defined(_MSC_VER) && \ 00216 defined(_DEBUG) 00217 _CrtMemCheckpoint(&memState); 00218 #endif /* _MSC_VER && _MSC_VER */ 00219 00220 // Search for files if neither files or directories specified 00221 // 00222 // Even though this is not necessary, because the recls API provides the 00223 // same interpretation, it's best to be explicit. 00224 if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) 00225 { 00226 flags |= RECLS_F_FILES; 00227 } 00228 00229 /* Now conduct the search */ 00230 #if defined(RECLS_TEST_CPP_USING_FTP) 00231 if(NULL != host) 00232 { 00233 if(NULL == pattern) 00234 { 00235 pattern = recls::Recls_GetWildcardsAll(); 00236 } 00237 00238 if(NULL == rootDir) 00239 { 00240 rootDir = "/"; 00241 } 00242 00243 reclspp::FtpSearch search(host, username, password, rootDir, pattern, flags); 00244 00245 totalFound = processSearch(search, bSuccinct, flags); 00246 } 00247 else 00248 #endif /* RECLS_TEST_CPP_USING_FTP */ 00249 { 00250 if(NULL == pattern) 00251 { 00252 pattern = recls::Recls_GetWildcardsAll(); 00253 } 00254 00255 if(NULL == rootDir) 00256 { 00257 rootDir = "."; 00258 } 00259 00260 reclspp::FileSearch search(rootDir, pattern, flags); 00261 00262 totalFound = processSearch(search, bSuccinct, flags); 00263 } 00264 00265 fprintf(stdout, "Total found: %d\n", totalFound); 00266 00267 #if defined(_MSC_VER) && \ 00268 defined(_DEBUG) 00269 _CrtMemDumpAllObjectsSince(&memState); 00270 #endif /* _MSC_VER && _MSC_VER */ 00271 00272 return iRet; 00273 } 00274 00275 /* ///////////////////////////////////////////////////////////////////////////// 00276 * Function implementations 00277 */ 00278 00280 static void usage(int bExit, char const *reason) 00281 { 00282 fprintf(stderr, "recls C++ Test Program\n\n"); 00283 00284 if(NULL != reason) 00285 { 00286 fprintf(stderr, " Error: %s\n\n", reason); 00287 } 00288 00289 fprintf(stderr, "Usage: Cpp [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]\n"); 00290 fprintf(stderr, "\t-R - does not recurse; recursive search is the default\n"); 00291 fprintf(stderr, "\t-p - evaluate and display directory parts\n"); 00292 fprintf(stderr, "\t-d - search for directories\n"); 00293 fprintf(stderr, "\t-f - search for files (this is the default, if -d is not specified)\n"); 00294 fprintf(stderr, "\t-h - searches from hard drive roots. (Ignore <root-dir>.)\n"); 00295 fprintf(stderr, "\t-s - succinct; shows only the path\n"); 00296 fprintf(stderr, "\t-H<host> - performs an FTP search on the given host\n"); 00297 fprintf(stderr, "\t-U<username> - username to use to log on to <host>\n"); 00298 fprintf(stderr, "\t-P<password> - password to use to log on to <host>\n"); 00299 fprintf(stderr, "\t<pattern> - search pattern, e.g. \"*.cpp\"; default is to search for all files\n"); 00300 fprintf(stderr, "\t<root-dir> - root directory of search; default is current working directory\n"); 00301 00302 if(bExit) 00303 { 00304 exit(EXIT_FAILURE); 00305 } 00306 } 00307 00308 /* ///////////////////////////////////////////////////////////////////////////// 00309 * Function implementations 00310 */ 00311 00312 static int processSearch(reclspp::Search &search, int bSuccinct, recls_uint32_t flags) 00313 { 00314 int totalFound = 0; 00315 00316 for(; search.HasMoreElements(); search.GetNext()) 00317 { 00318 using reclspp::string_t; 00319 00320 ++totalFound; 00321 00322 reclspp::FileEntry fileEntry = search.GetCurrentEntry(); 00323 #ifdef RECLS_PLATFORM_API_WIN32 00324 recls_char_t drive = fileEntry.GetDrive(); 00325 #endif /* RECLS_PLATFORM_API_WIN32 */ 00326 string_t path = fileEntry.GetPath(); 00327 string_t dir = fileEntry.GetDirectory(); 00328 string_t dirPath = fileEntry.GetDirectoryPath(); 00329 reclspp::DirectoryParts dirParts = fileEntry.GetDirectoryParts(); 00330 string_t file = fileEntry.GetFile(); 00331 string_t fileName = fileEntry.GetFileName(); 00332 string_t fileExt = fileEntry.GetFileExt(); 00333 00334 string_t pathCheck; 00335 string_t dirPartsCheck; 00336 string_t fileCheck; 00337 00338 #ifdef x_DEBUG 00339 reclspp::FileEntry entry2; 00340 00341 if( fileEntry 00342 && !entry2) 00343 { 00344 fprintf(stderr, "Good entry\n"); 00345 } 00346 if(!fileEntry) 00347 { 00348 fprintf(stderr, "Bad entry\n"); 00349 } 00350 #endif /* _DEBUG */ 00351 00352 #ifdef _DEBUG 00353 { 00354 recls_uint32_t cBlocks = search.GetNumOutstandingDetails(); 00355 00356 printf("\n%lu outstanding block(s)\n", cBlocks); 00357 } 00358 #endif /* _DEBUG */ 00359 00360 printf(" %s\n", path.c_str()); 00361 if(!bSuccinct) 00362 { 00363 int off = 0; 00364 00365 printf(" %s\n", dirPath.c_str()); 00366 #ifdef RECLS_PLATFORM_API_WIN32 00367 if(0 != drive) 00368 { 00369 if(isupper(path[0])) 00370 { 00371 drive = (recls_char_t)toupper(drive); 00372 } 00373 else 00374 { 00375 drive = (recls_char_t)tolower(drive); 00376 } 00377 printf(" %c\n", drive); 00378 pathCheck += drive; 00379 pathCheck += ':'; 00380 00381 off += 2; 00382 } 00383 #endif /* RECLS_PLATFORM_API_WIN32 */ 00384 printf(" %s\n", dir.c_str()); 00385 00386 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00387 { 00388 int i; 00389 int cDirParts; 00390 00391 for(i = 0, cDirParts = dirParts.size(); i < cDirParts; ++i) 00392 { 00393 string_t dirPart = dirParts[i]; 00394 00395 off += dirPart.length(); 00396 printf(" %*s\n", off, dirPart.c_str()); 00397 00398 pathCheck += dirPart; 00399 dirPartsCheck += dirPart; 00400 } 00401 } 00402 else 00403 { 00404 off += dir.length(); 00405 pathCheck += dir; 00406 } 00407 00408 off += file.length(); 00409 printf(" %*s\n", off, file.c_str()); 00410 if(fileExt.length() > 0) 00411 { 00412 printf(" %*s\n", off - (1 + fileExt.length()), fileName.c_str()); 00413 printf(" %*s\n", off, fileExt.c_str()); 00414 } 00415 else 00416 { 00417 printf(" %*s\n", off, fileName.c_str()); 00418 } 00419 00420 fileCheck = fileName; 00421 if(0 < fileExt.length()) 00422 { 00423 fileCheck += '.'; 00424 fileCheck += fileExt; 00425 } 00426 00427 pathCheck += file; 00428 00429 // Now validate the components 00430 recls_assert(path == pathCheck); 00431 if(path != pathCheck) 00432 { 00433 fprintf(stderr, "Path is different from path components\n\tpath: %s\n\tparts: %s\n\n", path.c_str(), pathCheck.c_str()); 00434 00435 abort(); 00436 } 00437 #ifdef RECLS_PLATFORM_API_WIN32 00438 recls_assert(0 == drive || (dirPath[0] == drive && dir == dirPath.c_str() + 2)); 00439 recls_assert(0 != drive || dir == dirPath); 00440 if( ( 0 != drive && 00441 ( dirPath[0] != drive || 00442 dir != dirPath.c_str() + 2)) || 00443 ( 0 == drive && 00444 dir != dirPath)) 00445 #else 00446 recls_assert(dirPath == dir); 00447 if(dirPath != dir) 00448 #endif /* RECLS_PLATFORM_API_WIN32 */ 00449 { 00450 fprintf(stderr, "DirectoryPath is different from Directory\n\tdirPath: %s\n\tdir: %s\n\n", dirPath.c_str(), dir.c_str()); 00451 00452 abort(); 00453 } 00454 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00455 { 00456 recls_assert(dir == dirPartsCheck); 00457 if(dir != dirPartsCheck) 00458 { 00459 fprintf(stderr, "Directory is different from directory components\n\tpath: %s\n\tparts: %s\n\n", dir.c_str(), dirPartsCheck.c_str()); 00460 00461 abort(); 00462 } 00463 } 00464 recls_assert(file == fileCheck); 00465 if(file != fileCheck) 00466 { 00467 fprintf(stderr, "File is different from file components\n\tpath: %s\n\tparts: %s\n\n", file.c_str(), fileCheck.c_str()); 00468 00469 abort(); 00470 } 00471 00472 if(fileEntry.IsReadOnly()) 00473 { 00474 printf(" - Read-only\n"); 00475 } 00476 if(fileEntry.IsDirectory()) 00477 { 00478 printf(" - Directory\n"); 00479 } 00480 else 00481 { 00482 printf(" - File\n"); 00483 } 00484 if(fileEntry.IsLink()) 00485 { 00486 printf(" - Link\n"); 00487 } 00488 } 00489 } 00490 00491 return totalFound; 00492 } 00493 00494 /* ////////////////////////////////////////////////////////////////////////// 00495 * End of file 00496 */
/* ///////////////////////////////////////////////////////////////////////////// * File: STL.cpp * * Purpose: Implementation file for the STL project. * * Created: 16th August 2003 * Updated: 10th January 2005 * * Status: Wizard-generated * * License: (Licensed under the Synesis Software Open License) * * Copyright 2003-2005, Synesis Software Pty Ltd. * All rights reserved. * * www: http://www.synesis.com.au/software * * email: software@synesis.com.au * * This source code is placed into the public domain 2003 * by Synesis Software Pty Ltd. There are no restrictions * whatsoever to your use of the software. * * This source code is provided by Synesis Software Pty Ltd "as is" * and any warranties, whether expressed or implied, including, but * not limited to, the implied warranties of merchantability and * fitness for a particular purpose are disclaimed. In no event * shall the Synesis Software Pty Ltd be liable for any direct, * indirect, incidental, special, exemplary, or consequential * damages (including, but not limited to, procurement of * substitute goods or services; loss of use, data, or profits; or * business interruption) however caused and on any theory of * liability, whether in contract, strict liability, or tort * (including negligence or otherwise) arising in any way out of * the use of this software, even if advised of the possibility of * such damage. * * Neither the name of Synesis Software Pty Ltd nor the names of * any subdivisions, employees or agents of Synesis Software Pty * Ltd, nor the names of any other contributors to this software * may be used to endorse or promote products derived from this * software without specific prior written permission. * * ////////////////////////////////////////////////////////////////////////// */ // Define this definition to retrict use to platform-independent aspects of the API //#define RECLS_PURE_API // Define this to copy file entries into a vector, to exercise the copy semantics //#define TAKE_COPIES_IN_VECTOR #include <recls.h> #ifdef RECLS_PLATFORM_IS_WIN32 # include <recls_ftp.h> #endif /* RECLS_PLATFORM_IS_WIN32 */ #if RECLS_VER < RECLS_VER_1_1_1 # error This file now requires version 1.1.1 or later of the recls C API #endif /* RECLS_VER < 1.1.1 */ #ifdef RECLS_STRICT # include <stlsoft_nulldef.h> #endif // RECLS_STRICT #ifdef _MSC_VER # include <crtdbg.h> # pragma warning(disable : 4530) #endif /* _MSC_VER */ #include <reclstl.h> #include <reclstl_root_sequence.h> #include <reclstl_search_sequence.h> #if !defined(RECLS_COMPILER_IS_DMC) # include <iostream> # include <iomanip> #else /* ? compiler */ # include <iostream.h> # include <iomanip.h> #endif /* compiler */ #include <stdio.h> /* ///////////////////////////////////////////////////////////////////////////// * Namespace */ #if !defined(RECLS_COMPILER_IS_DMC) using std::cout; using std::cerr; using std::endl; using std::setw; #endif /* compiler */ using recls::recls_info_t; using recls::recls_char_t; using recls::RECLS_F_FILES; using recls::RECLS_F_DIRECTORIES; using recls::RECLS_F_RECURSIVE; using recls::RECLS_F_DIRECTORY_PARTS; /* ///////////////////////////////////////////////////////////////////////////// * Forward declarations */ static void usage(int bExit, char const *reason); int processDirectory(char const *host, char const *username, char const *password, char const *rootDir, char const *pattern, long flags, int bSuccinct); /* ///////////////////////////////////////////////////////////////////////////// * Main */ int main(int argc, char *argv[]) { #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemState memState; #endif /* _MSC_VER && _MSC_VER */ int iRet = 0; int i; int totalFound = 0; char const *host = NULL; char const *username = NULL; char const *password = NULL; char const *pattern = NULL; char const *rootDir = NULL; unsigned flags = RECLS_F_RECURSIVE; int bSuccinct = false; int bAllHardDrives = false; for(i = 1; i < argc; ++i) { const char *arg = argv[i]; if(arg[0] == '-') { if(arg[1] == '-') { /* -- arguments */ } else { /* - arguments */ switch(arg[1]) { case '?': usage(1, NULL); break; case 'R': /* Do not recurse */ flags &= ~(RECLS_F_RECURSIVE); break; case 'p': /* Show directory parts */ flags |= RECLS_F_DIRECTORY_PARTS; break; case 'f': /* Find files */ flags |= RECLS_F_FILES; break; case 'd': /* Find directories */ flags |= RECLS_F_DIRECTORIES; break; case 'h': /* Searches from hard drives */ bAllHardDrives = 1; break; case 's': /* Show only the full path; WHEREIS functionality */ bSuccinct = true; break; case 'H': /* FTP host */ host = arg + 2; break; case 'U': /* FTP username */ username = arg + 2; break; case 'P': /* FTP password */ password = arg + 2; break; default: usage(1, "Invalid argument(s) specified"); break; } } } else { /* other arguments */ if(NULL == pattern) { pattern = arg; } else if(NULL == rootDir) { rootDir = arg; } else { usage(1, "Invalid argument(s) specified"); } } } if( NULL != password && ( NULL == host || NULL == username)) { usage(1, "Must specify host and username if specifying password"); } if( NULL != username && NULL == host) { usage(1, "Must specify host if specifying username"); } if( NULL != host && bAllHardDrives) { usage(1, "-h flag meaningless to FTP searches"); } if( NULL != host && NULL == rootDir) { usage(1, "Must specify a root directory for FTP searches"); } #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemCheckpoint(&memState); #endif /* _MSC_VER && _MSC_VER */ // Search for files if neither files or directories specified // // Even though this is not necessary, because the recls API provides the // same interpretation, it's best to be explicit. if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) { flags |= RECLS_F_FILES; } if(NULL == pattern) { pattern = recls::Recls_GetWildcardsAll(); } if(NULL == rootDir) { rootDir = "."; } if(bAllHardDrives) { reclstl::basic_root_sequence<recls_char_t> roots; reclstl::basic_root_sequence<recls_char_t>::const_iterator begin = roots.begin(); reclstl::basic_root_sequence<recls_char_t>::const_iterator end = roots.end(); for(; begin != end; ++begin) { totalFound += processDirectory(host, username, password, *begin, pattern, flags, bSuccinct); } } else { totalFound = processDirectory(host, username, password, rootDir, pattern, flags, bSuccinct); } fprintf(stdout, "Total found: %d\n", totalFound); #if defined(_MSC_VER) && \ defined(_DEBUG) _CrtMemDumpAllObjectsSince(&memState); #endif /* _MSC_VER && _MSC_VER */ return iRet; } /* ///////////////////////////////////////////////////////////////////////////// * Function implementations */ void usage(int bExit, char const *reason) { cerr << "recls STL Test Program:" << endl << endl; if(NULL != reason) { fprintf(stderr, " Error: %s\n\n", reason); } cerr << "Usage: STL [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]" << endl; cerr << "\t-R - does not recurse; recursive search is the default" << endl; cerr << "\t-p - evaluate and display directory parts" << endl; cerr << "\t-d - search for directories" << endl; cerr << "\t-f - search for files (this is the default, if -d is not specified" << endl; cerr << "\t-h - searches from hard drive roots. (Ignore <root-dir>.)" << endl; cerr << "\t-s - succinct; shows only the path" << endl; cerr << "\t-H<host> - performs an FTP search on the given host" << endl; cerr << "\t-U<username> - username to use to log on to <host>" << endl; cerr << "\t-P<password> - password to use to log on to <host>" << endl; cerr << "\t<pattern> - search pattern, e.g. \"*.cpp\"; default is to search for all files" << endl; cerr << "\t<root-dir> - root directory of search; default is current working directory" << endl; if(bExit) { exit(EXIT_FAILURE); } } int processDirectory(char const *host, char const *username, char const *password, char const *rootDir, char const *pattern, long flags, int bSuccinct) { typedef reclstl::basic_search_sequence<recls_char_t> sequence_t; int totalFound = 0; #if defined(RECLS_API_FTP) sequence_t search(host, username, password, rootDir, pattern, flags); #else /* ? RECLS_API_FTP */ STLSOFT_SUPPRESS_UNUSED(host); STLSOFT_SUPPRESS_UNUSED(username); STLSOFT_SUPPRESS_UNUSED(password); sequence_t search(rootDir, pattern, flags); #endif /* RECLS_API_FTP */ sequence_t::const_iterator begin = search.begin(); sequence_t::const_iterator end = search.end(); for(; begin != end; ++begin) { using reclstl::string_t; ++totalFound; sequence_t::value_type fileEntry = *begin; #ifdef RECLS_PLATFORM_API_WIN32 recls_char_t drive = fileEntry.get_drive(); #endif /* RECLS_PLATFORM_API_WIN32 */ string_t path = fileEntry.get_path(); string_t dir = fileEntry.get_directory(); string_t dirPath = fileEntry.get_directory_path(); string_t file = fileEntry.get_file(); string_t fileName = fileEntry.get_filename(); string_t fileExt = fileEntry.get_fileext(); string_t pathCheck; string_t dirCheck; string_t fileCheck; cout << " " << path << endl; if(!bSuccinct) { int off = 0; cout << " " << dirPath << endl; #ifdef RECLS_PLATFORM_API_WIN32 if(0 != drive) { if(isupper(path[0])) { drive = (recls_char_t)toupper(drive); } else { drive = (recls_char_t)tolower(drive); } cout << " " << drive << endl; pathCheck += drive; pathCheck += ':'; off += 2; } #endif /* RECLS_PLATFORM_API_WIN32 */ cout << " " << setw(off) << dir << endl; typedef sequence_t::value_type::directory_parts_type directory_parts_type; if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { directory_parts_type dirParts = fileEntry.get_directory_parts(); int i; directory_parts_type::iterator begin = dirParts.begin(); directory_parts_type::iterator end = dirParts.end(); for(i = 0; begin != end; ++begin, ++i) { string_t dirPart = *begin; off += dirPart.length(); cout << " " << setw(off) << dirPart << setw(1) << endl; pathCheck += dirPart; dirCheck += dirPart; } } else { off += dir.length(); pathCheck += dir; } off += file.length(); cout << " " << setw(off) << file << setw(1) << endl; if(fileExt.length() > 0) { cout << " " << setw(off - (1 + fileExt.length())) << fileName << setw(1) << endl; cout << " " << setw(off) << fileExt << setw(1) << endl; } else { cout << " " << setw(off) << fileName << setw(1) << endl; } fileCheck = fileName; if(0 < fileExt.length()) { fileCheck += '.'; fileCheck += fileExt; } pathCheck += file; // Now validate the components recls_assert(path == pathCheck); if(path != pathCheck) { cerr << "Path is different from path components" << endl << "\tpath: \"" << path << "\"" << endl << "\tparts: \"" << pathCheck << "\"" << endl << endl; abort(); } #ifdef RECLS_PLATFORM_API_WIN32 recls_assert(NULL != host || (dirPath[0] == drive && dir == dirPath.c_str() + 2)); recls_assert(NULL == host || dir == dirPath); if( ( NULL == host && ( dirPath[0] != drive || dir != dirPath.c_str() + 2)) || ( NULL != host && dir != dirPath)) #else recls_assert(dirPath == dir); if(dirPath != dir) #endif /* RECLS_PLATFORM_API_WIN32 */ { cerr << "DirectoryPath is different from Directory" << endl << "\tdirPath: \"" << dirPath << "\"" << endl << "\tdir: \"" << dir << "\"" << endl << endl; abort(); } if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) { recls_assert(dir == dirCheck); if(dir != dirCheck) { cerr << "Directory is different from directory components" << endl << "\tpath: " << dir << "" << endl << "\tparts: " << dirCheck << endl << endl; abort(); } } recls_assert(file == fileCheck); if(file != fileCheck) { cerr << "File is different from file components" << endl << "\tpath: " << "%s" << "" << endl << "\tparts: " << "%s" << endl << endl; abort(); } if(fileEntry.is_readonly()) { cout << " - Read-only" << endl; } if(fileEntry.is_directory()) { cout << " - Directory" << endl; } else { cout << " - File" << endl; } if(fileEntry.is_link()) { cout << " - Link" << endl; } } } return totalFound; } /* ////////////////////////////////////////////////////////////////////////// * End of file */
00001 /* ///////////////////////////////////////////////////////////////////////////// 00002 * File: STL.cpp 00003 * 00004 * Purpose: Implementation file for the STL project. 00005 * 00006 * Created: 16th August 2003 00007 * Updated: 10th January 2005 00008 * 00009 * Status: Wizard-generated 00010 * 00011 * License: (Licensed under the Synesis Software Open License) 00012 * 00013 * Copyright 2003-2005, Synesis Software Pty Ltd. 00014 * All rights reserved. 00015 * 00016 * www: http://www.synesis.com.au/software 00017 * 00018 * email: software@synesis.com.au 00019 * 00020 * This source code is placed into the public domain 2003 00021 * by Synesis Software Pty Ltd. There are no restrictions 00022 * whatsoever to your use of the software. 00023 * 00024 * This source code is provided by Synesis Software Pty Ltd "as is" 00025 * and any warranties, whether expressed or implied, including, but 00026 * not limited to, the implied warranties of merchantability and 00027 * fitness for a particular purpose are disclaimed. In no event 00028 * shall the Synesis Software Pty Ltd be liable for any direct, 00029 * indirect, incidental, special, exemplary, or consequential 00030 * damages (including, but not limited to, procurement of 00031 * substitute goods or services; loss of use, data, or profits; or 00032 * business interruption) however caused and on any theory of 00033 * liability, whether in contract, strict liability, or tort 00034 * (including negligence or otherwise) arising in any way out of 00035 * the use of this software, even if advised of the possibility of 00036 * such damage. 00037 * 00038 * Neither the name of Synesis Software Pty Ltd nor the names of 00039 * any subdivisions, employees or agents of Synesis Software Pty 00040 * Ltd, nor the names of any other contributors to this software 00041 * may be used to endorse or promote products derived from this 00042 * software without specific prior written permission. 00043 * 00044 * ////////////////////////////////////////////////////////////////////////// */ 00045 00046 // Define this definition to retrict use to platform-independent aspects of the API 00047 //#define RECLS_PURE_API 00048 00049 // Define this to copy file entries into a vector, to exercise the copy semantics 00050 //#define TAKE_COPIES_IN_VECTOR 00051 00052 #include <recls.h> 00053 #ifdef RECLS_PLATFORM_IS_WIN32 00054 # include <recls_ftp.h> 00055 #endif /* RECLS_PLATFORM_IS_WIN32 */ 00056 00057 00058 #if RECLS_VER < RECLS_VER_1_1_1 00059 # error This file now requires version 1.1.1 or later of the recls C API 00060 #endif /* RECLS_VER < 1.1.1 */ 00061 00062 #ifdef RECLS_STRICT 00063 # include <stlsoft_nulldef.h> 00064 #endif // RECLS_STRICT 00065 00066 #ifdef _MSC_VER 00067 # include <crtdbg.h> 00068 # pragma warning(disable : 4530) 00069 #endif /* _MSC_VER */ 00070 00071 #include <reclstl.h> 00072 #include <reclstl_root_sequence.h> 00073 #include <reclstl_search_sequence.h> 00074 00075 #if !defined(RECLS_COMPILER_IS_DMC) 00076 # include <iostream> 00077 # include <iomanip> 00078 #else /* ? compiler */ 00079 # include <iostream.h> 00080 # include <iomanip.h> 00081 #endif /* compiler */ 00082 00083 #include <stdio.h> 00084 00085 /* ///////////////////////////////////////////////////////////////////////////// 00086 * Namespace 00087 */ 00088 00089 #if !defined(RECLS_COMPILER_IS_DMC) 00090 using std::cout; 00091 using std::cerr; 00092 using std::endl; 00093 using std::setw; 00094 #endif /* compiler */ 00095 using recls::recls_info_t; 00096 using recls::recls_char_t; 00097 using recls::RECLS_F_FILES; 00098 using recls::RECLS_F_DIRECTORIES; 00099 using recls::RECLS_F_RECURSIVE; 00100 using recls::RECLS_F_DIRECTORY_PARTS; 00101 00102 /* ///////////////////////////////////////////////////////////////////////////// 00103 * Forward declarations 00104 */ 00105 00106 static void usage(int bExit, char const *reason); 00107 int processDirectory(char const *host, char const *username, char const *password, char const *rootDir, char const *pattern, long flags, int bSuccinct); 00108 00109 /* ///////////////////////////////////////////////////////////////////////////// 00110 * Main 00111 */ 00112 00114 int main(int argc, char *argv[]) 00115 { 00116 #if defined(_MSC_VER) && \ 00117 defined(_DEBUG) 00118 _CrtMemState memState; 00119 #endif /* _MSC_VER && _MSC_VER */ 00120 int iRet = 0; 00121 int i; 00122 int totalFound = 0; 00123 char const *host = NULL; 00124 char const *username = NULL; 00125 char const *password = NULL; 00126 char const *pattern = NULL; 00127 char const *rootDir = NULL; 00128 unsigned flags = RECLS_F_RECURSIVE; 00129 int bSuccinct = false; 00130 int bAllHardDrives = false; 00131 00132 for(i = 1; i < argc; ++i) 00133 { 00134 const char *arg = argv[i]; 00135 00136 if(arg[0] == '-') 00137 { 00138 if(arg[1] == '-') 00139 { 00140 /* -- arguments */ 00141 } 00142 else 00143 { 00144 /* - arguments */ 00145 switch(arg[1]) 00146 { 00147 case '?': 00148 usage(1, NULL); 00149 break; 00150 case 'R': /* Do not recurse */ 00151 flags &= ~(RECLS_F_RECURSIVE); 00152 break; 00153 case 'p': /* Show directory parts */ 00154 flags |= RECLS_F_DIRECTORY_PARTS; 00155 break; 00156 case 'f': /* Find files */ 00157 flags |= RECLS_F_FILES; 00158 break; 00159 case 'd': /* Find directories */ 00160 flags |= RECLS_F_DIRECTORIES; 00161 break; 00162 case 'h': /* Searches from hard drives */ 00163 bAllHardDrives = 1; 00164 break; 00165 case 's': /* Show only the full path; WHEREIS functionality */ 00166 bSuccinct = true; 00167 break; 00168 case 'H': /* FTP host */ 00169 host = arg + 2; 00170 break; 00171 case 'U': /* FTP username */ 00172 username = arg + 2; 00173 break; 00174 case 'P': /* FTP password */ 00175 password = arg + 2; 00176 break; 00177 default: 00178 usage(1, "Invalid argument(s) specified"); 00179 break; 00180 } 00181 } 00182 } 00183 else 00184 { 00185 /* other arguments */ 00186 if(NULL == pattern) 00187 { 00188 pattern = arg; 00189 } 00190 else if(NULL == rootDir) 00191 { 00192 rootDir = arg; 00193 } 00194 else 00195 { 00196 usage(1, "Invalid argument(s) specified"); 00197 } 00198 } 00199 } 00200 00201 if( NULL != password && 00202 ( NULL == host || 00203 NULL == username)) 00204 { 00205 usage(1, "Must specify host and username if specifying password"); 00206 } 00207 00208 if( NULL != username && 00209 NULL == host) 00210 { 00211 usage(1, "Must specify host if specifying username"); 00212 } 00213 00214 if( NULL != host && 00215 bAllHardDrives) 00216 { 00217 usage(1, "-h flag meaningless to FTP searches"); 00218 } 00219 00220 if( NULL != host && 00221 NULL == rootDir) 00222 { 00223 usage(1, "Must specify a root directory for FTP searches"); 00224 } 00225 00226 #if defined(_MSC_VER) && \ 00227 defined(_DEBUG) 00228 _CrtMemCheckpoint(&memState); 00229 #endif /* _MSC_VER && _MSC_VER */ 00230 00231 // Search for files if neither files or directories specified 00232 // 00233 // Even though this is not necessary, because the recls API provides the 00234 // same interpretation, it's best to be explicit. 00235 if(0 == (flags & (RECLS_F_FILES | RECLS_F_DIRECTORIES))) 00236 { 00237 flags |= RECLS_F_FILES; 00238 } 00239 00240 if(NULL == pattern) 00241 { 00242 pattern = recls::Recls_GetWildcardsAll(); 00243 } 00244 00245 if(NULL == rootDir) 00246 { 00247 rootDir = "."; 00248 } 00249 00250 if(bAllHardDrives) 00251 { 00252 reclstl::basic_root_sequence<recls_char_t> roots; 00253 reclstl::basic_root_sequence<recls_char_t>::const_iterator begin = roots.begin(); 00254 reclstl::basic_root_sequence<recls_char_t>::const_iterator end = roots.end(); 00255 00256 for(; begin != end; ++begin) 00257 { 00258 totalFound += processDirectory(host, username, password, *begin, pattern, flags, bSuccinct); 00259 } 00260 } 00261 else 00262 { 00263 totalFound = processDirectory(host, username, password, rootDir, pattern, flags, bSuccinct); 00264 } 00265 00266 fprintf(stdout, "Total found: %d\n", totalFound); 00267 00268 #if defined(_MSC_VER) && \ 00269 defined(_DEBUG) 00270 _CrtMemDumpAllObjectsSince(&memState); 00271 #endif /* _MSC_VER && _MSC_VER */ 00272 00273 return iRet; 00274 } 00275 00276 /* ///////////////////////////////////////////////////////////////////////////// 00277 * Function implementations 00278 */ 00279 00281 void usage(int bExit, char const *reason) 00282 { 00283 cerr << "recls STL Test Program:" << endl << endl; 00284 00285 if(NULL != reason) 00286 { 00287 fprintf(stderr, " Error: %s\n\n", reason); 00288 } 00289 00290 cerr << "Usage: STL [-d] [-f] [-h] [-p] [-R] [-s] [-H<host>] [-U<username>] [-P<password>] [<pattern>] [<root-dir>]" << endl; 00291 cerr << "\t-R - does not recurse; recursive search is the default" << endl; 00292 cerr << "\t-p - evaluate and display directory parts" << endl; 00293 cerr << "\t-d - search for directories" << endl; 00294 cerr << "\t-f - search for files (this is the default, if -d is not specified" << endl; 00295 cerr << "\t-h - searches from hard drive roots. (Ignore <root-dir>.)" << endl; 00296 cerr << "\t-s - succinct; shows only the path" << endl; 00297 cerr << "\t-H<host> - performs an FTP search on the given host" << endl; 00298 cerr << "\t-U<username> - username to use to log on to <host>" << endl; 00299 cerr << "\t-P<password> - password to use to log on to <host>" << endl; 00300 cerr << "\t<pattern> - search pattern, e.g. \"*.cpp\"; default is to search for all files" << endl; 00301 cerr << "\t<root-dir> - root directory of search; default is current working directory" << endl; 00302 00303 if(bExit) 00304 { 00305 exit(EXIT_FAILURE); 00306 } 00307 } 00308 00309 00310 int processDirectory(char const *host, char const *username, char const *password, char const *rootDir, char const *pattern, long flags, int bSuccinct) 00311 { 00312 typedef reclstl::basic_search_sequence<recls_char_t> sequence_t; 00313 00314 int totalFound = 0; 00315 #if defined(RECLS_API_FTP) 00316 sequence_t search(host, username, password, rootDir, pattern, flags); 00317 #else /* ? RECLS_API_FTP */ 00318 STLSOFT_SUPPRESS_UNUSED(host); 00319 STLSOFT_SUPPRESS_UNUSED(username); 00320 STLSOFT_SUPPRESS_UNUSED(password); 00321 sequence_t search(rootDir, pattern, flags); 00322 #endif /* RECLS_API_FTP */ 00323 00324 sequence_t::const_iterator begin = search.begin(); 00325 sequence_t::const_iterator end = search.end(); 00326 00327 for(; begin != end; ++begin) 00328 { 00329 using reclstl::string_t; 00330 00331 ++totalFound; 00332 00333 sequence_t::value_type fileEntry = *begin; 00334 00335 #ifdef RECLS_PLATFORM_API_WIN32 00336 recls_char_t drive = fileEntry.get_drive(); 00337 #endif /* RECLS_PLATFORM_API_WIN32 */ 00338 string_t path = fileEntry.get_path(); 00339 string_t dir = fileEntry.get_directory(); 00340 string_t dirPath = fileEntry.get_directory_path(); 00341 string_t file = fileEntry.get_file(); 00342 string_t fileName = fileEntry.get_filename(); 00343 string_t fileExt = fileEntry.get_fileext(); 00344 00345 string_t pathCheck; 00346 string_t dirCheck; 00347 string_t fileCheck; 00348 00349 cout << " " << path << endl; 00350 if(!bSuccinct) 00351 { 00352 int off = 0; 00353 00354 cout << " " << dirPath << endl; 00355 #ifdef RECLS_PLATFORM_API_WIN32 00356 if(0 != drive) 00357 { 00358 if(isupper(path[0])) 00359 { 00360 drive = (recls_char_t)toupper(drive); 00361 } 00362 else 00363 { 00364 drive = (recls_char_t)tolower(drive); 00365 } 00366 cout << " " << drive << endl; 00367 pathCheck += drive; 00368 pathCheck += ':'; 00369 00370 off += 2; 00371 } 00372 #endif /* RECLS_PLATFORM_API_WIN32 */ 00373 cout << " " << setw(off) << dir << endl; 00374 00375 typedef sequence_t::value_type::directory_parts_type directory_parts_type; 00376 00377 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00378 { 00379 directory_parts_type dirParts = fileEntry.get_directory_parts(); 00380 int i; 00381 00382 directory_parts_type::iterator begin = dirParts.begin(); 00383 directory_parts_type::iterator end = dirParts.end(); 00384 00385 for(i = 0; begin != end; ++begin, ++i) 00386 { 00387 string_t dirPart = *begin; 00388 00389 off += dirPart.length(); 00390 cout << " " << setw(off) << dirPart << setw(1) << endl; 00391 00392 pathCheck += dirPart; 00393 dirCheck += dirPart; 00394 } 00395 } 00396 else 00397 { 00398 off += dir.length(); 00399 pathCheck += dir; 00400 } 00401 00402 off += file.length(); 00403 cout << " " << setw(off) << file << setw(1) << endl; 00404 if(fileExt.length() > 0) 00405 { 00406 cout << " " << setw(off - (1 + fileExt.length())) << fileName << setw(1) << endl; 00407 cout << " " << setw(off) << fileExt << setw(1) << endl; 00408 } 00409 else 00410 { 00411 cout << " " << setw(off) << fileName << setw(1) << endl; 00412 } 00413 00414 fileCheck = fileName; 00415 if(0 < fileExt.length()) 00416 { 00417 fileCheck += '.'; 00418 fileCheck += fileExt; 00419 } 00420 00421 pathCheck += file; 00422 00423 // Now validate the components 00424 recls_assert(path == pathCheck); 00425 if(path != pathCheck) 00426 { 00427 cerr << "Path is different from path components" << endl 00428 << "\tpath: \"" << path << "\"" << endl 00429 << "\tparts: \"" << pathCheck << "\"" << endl 00430 << endl; 00431 00432 abort(); 00433 } 00434 #ifdef RECLS_PLATFORM_API_WIN32 00435 recls_assert(NULL != host || (dirPath[0] == drive && dir == dirPath.c_str() + 2)); 00436 recls_assert(NULL == host || dir == dirPath); 00437 if( ( NULL == host && 00438 ( dirPath[0] != drive || 00439 dir != dirPath.c_str() + 2)) || 00440 ( NULL != host && 00441 dir != dirPath)) 00442 #else 00443 recls_assert(dirPath == dir); 00444 if(dirPath != dir) 00445 #endif /* RECLS_PLATFORM_API_WIN32 */ 00446 { 00447 cerr << "DirectoryPath is different from Directory" << endl 00448 << "\tdirPath: \"" << dirPath << "\"" << endl 00449 << "\tdir: \"" << dir << "\"" << endl 00450 << endl; 00451 00452 abort(); 00453 } 00454 if((flags & RECLS_F_DIRECTORY_PARTS) == RECLS_F_DIRECTORY_PARTS) 00455 { 00456 recls_assert(dir == dirCheck); 00457 if(dir != dirCheck) 00458 { 00459 cerr << "Directory is different from directory components" << endl << "\tpath: " << dir << "" << endl << "\tparts: " << dirCheck << endl << endl; 00460 00461 abort(); 00462 } 00463 } 00464 recls_assert(file == fileCheck); 00465 if(file != fileCheck) 00466 { 00467 cerr << "File is different from file components" << endl << "\tpath: " << "%s" << "" << endl << "\tparts: " << "%s" << endl << endl; 00468 00469 abort(); 00470 } 00471 00472 if(fileEntry.is_readonly()) 00473 { 00474 cout << " - Read-only" << endl; 00475 } 00476 if(fileEntry.is_directory()) 00477 { 00478 cout << " - Directory" << endl; 00479 } 00480 else 00481 { 00482 cout << " - File" << endl; 00483 } 00484 if(fileEntry.is_link()) 00485 { 00486 cout << " - Link" << endl; 00487 } 00488 } 00489 } 00490 00491 return totalFound; 00492 } 00493 00494 /* ////////////////////////////////////////////////////////////////////////// 00495 * End of file 00496 */
|
recls Library documentation © Synesis Software Pty Ltd, 2001-2005 |