00001 /********************************************************************** 00002 * $Id: cpl_dir_cpp-source.html,v 1.13 2002/12/21 19:13:12 warmerda Exp $ 00003 * 00004 * Name: cpl_dir.cpp 00005 * Project: CPL - Common Portability Library 00006 * Purpose: Directory manipulation. 00007 * Author: Daniel Morissette, danmo@videotron.ca 00008 * 00009 ********************************************************************** 00010 * Copyright (c) 1998, Daniel Morissette 00011 * 00012 * Permission is hereby granted, free of charge, to any person obtaining a 00013 * copy of this software and associated documentation files (the "Software"), 00014 * to deal in the Software without restriction, including without limitation 00015 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00016 * and/or sell copies of the Software, and to permit persons to whom the 00017 * Software is furnished to do so, subject to the following conditions: 00018 * 00019 * The above copyright notice and this permission notice shall be included 00020 * in all copies or substantial portions of the Software. 00021 * 00022 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00023 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00025 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 * DEALINGS IN THE SOFTWARE. 00029 ********************************************************************** 00030 * 00031 * $Log: cpl_dir_cpp-source.html,v $ 00031 * Revision 1.13 2002/12/21 19:13:12 warmerda 00031 * updated 00031 * 00032 * Revision 1.5 2002/03/28 14:52:32 warmerda 00033 * moved docs to non-WIN32 section 00034 * 00035 * Revision 1.4 2001/07/18 04:00:49 warmerda 00036 * added CPL_CVSID 00037 * 00038 * Revision 1.3 2000/09/25 19:59:03 warmerda 00039 * look for WIN32 not _WIN32 00040 * 00041 * Revision 1.2 1999/05/20 02:54:38 warmerda 00042 * Added API documentation 00043 * 00044 * Revision 1.1 1999/02/25 04:52:00 danmo 00045 * *** empty log message *** 00046 * 00047 **********************************************************************/ 00048 00049 #include "cpl_conv.h" 00050 #include "cpl_string.h" 00051 00052 CPL_CVSID("$Id: cpl_dir_cpp-source.html,v 1.13 2002/12/21 19:13:12 warmerda Exp $"); 00053 00054 #ifdef WIN32 00055 00056 /*===================================================================== 00057 WIN32 / MSVC++ implementation 00058 *====================================================================*/ 00059 00060 #include <io.h> 00061 00062 /********************************************************************** 00063 * CPLReadDir() 00064 * 00065 * Return a stringlist with the list of files in a directory. 00066 * The returned stringlist should be freed with CSLDestroy(). 00067 * 00068 * Returns NULL if an error happened or if the directory could not 00069 * be read. 00070 **********************************************************************/ 00071 00072 char **CPLReadDir(const char *pszPath) 00073 { 00074 struct _finddata_t c_file; 00075 long hFile; 00076 char *pszFileSpec, **papszDir = NULL; 00077 00078 if (strlen(pszPath) == 0) 00079 pszPath = "."; 00080 00081 pszFileSpec = CPLStrdup(CPLSPrintf("%s\\*.*", pszPath)); 00082 00083 if ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L ) 00084 { 00085 do 00086 { 00087 papszDir = CSLAddString(papszDir, c_file.name); 00088 } while( _findnext( hFile, &c_file ) == 0 ); 00089 00090 _findclose( hFile ); 00091 } 00092 else 00093 { 00094 /* Should we generate an error??? 00095 * For now we'll just return NULL (at the end of the function) 00096 */ 00097 } 00098 00099 CPLFree(pszFileSpec); 00100 00101 return papszDir; 00102 } 00103 00104 #else 00105 00106 /*===================================================================== 00107 POSIX (Unix) implementation 00108 *====================================================================*/ 00109 00110 #include <sys/types.h> 00111 #include <dirent.h> 00112 00113 /********************************************************************** 00114 * CPLReadDir() 00115 * 00116 * Return a stringlist with the list of files in a directory. 00117 * The returned stringlist should be freed with CSLDestroy(). 00118 * 00119 * Returns NULL if an error happened or if the directory could not 00120 * be read. 00121 **********************************************************************/ 00122 00139 char **CPLReadDir(const char *pszPath) 00140 { 00141 DIR *hDir; 00142 struct dirent *psDirEntry; 00143 char **papszDir = NULL; 00144 00145 if (strlen(pszPath) == 0) 00146 pszPath = "."; 00147 00148 if ( (hDir = opendir(pszPath)) != NULL ) 00149 { 00150 while( (psDirEntry = readdir(hDir)) != NULL ) 00151 { 00152 papszDir = CSLAddString(papszDir, psDirEntry->d_name); 00153 } 00154 00155 closedir( hDir ); 00156 } 00157 else 00158 { 00159 /* Should we generate an error??? 00160 * For now we'll just return NULL (at the end of the function) 00161 */ 00162 } 00163 00164 return papszDir; 00165 } 00166 00167 #endif