Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

cpl_dir.cpp

00001 /**********************************************************************
00002  * $Id: cpl_dir_cpp-source.html,v 1.10 2002/04/16 13:11:47 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.10  2002/04/16 13:11:47  warmerda
00031  * updated
00031  *
00032  * Revision 1.4  2001/07/18 04:00:49  warmerda
00033  * added CPL_CVSID
00034  *
00035  * Revision 1.3  2000/09/25 19:59:03  warmerda
00036  * look for WIN32 not _WIN32
00037  *
00038  * Revision 1.2  1999/05/20 02:54:38  warmerda
00039  * Added API documentation
00040  *
00041  * Revision 1.1  1999/02/25 04:52:00  danmo
00042  * *** empty log message ***
00043  *
00044  **********************************************************************/
00045 
00046 #include "cpl_conv.h"
00047 #include "cpl_string.h"
00048 
00049 CPL_CVSID("$Id: cpl_dir_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $");
00050 
00051 #ifdef WIN32
00052 
00053 /*=====================================================================
00054                    WIN32 / MSVC++ implementation
00055  *====================================================================*/
00056 
00057 #include <io.h>
00058 
00059 /**********************************************************************
00060  *                          CPLReadDir()
00061  *
00062  * Return a stringlist with the list of files in a directory.
00063  * The returned stringlist should be freed with CSLDestroy().
00064  *
00065  * Returns NULL if an error happened or if the directory could not
00066  * be read.
00067  **********************************************************************/
00068 
00069 char **CPLReadDir(const char *pszPath)
00070 {
00071     struct _finddata_t c_file;
00072     long    hFile;
00073     char    *pszFileSpec, **papszDir = NULL;
00074 
00075     if (strlen(pszPath) == 0)
00076         pszPath = ".";
00077 
00078     pszFileSpec = CPLStrdup(CPLSPrintf("%s\\*.*", pszPath));
00079 
00080     if ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L )
00081     {
00082         do
00083         {
00084             papszDir = CSLAddString(papszDir, c_file.name);
00085         } while( _findnext( hFile, &c_file ) == 0 );
00086 
00087         _findclose( hFile );
00088     }
00089     else
00090     {
00091         /* Should we generate an error???  
00092          * For now we'll just return NULL (at the end of the function)
00093          */
00094     }
00095 
00096     CPLFree(pszFileSpec);
00097 
00098     return papszDir;
00099 }
00100 
00101 #else
00102 
00103 /*=====================================================================
00104                       POSIX (Unix) implementation
00105  *====================================================================*/
00106 
00107 #include <sys/types.h>
00108 #include <dirent.h>
00109 
00110 /**********************************************************************
00111  *                          CPLReadDir()
00112  *
00113  * Return a stringlist with the list of files in a directory.
00114  * The returned stringlist should be freed with CSLDestroy().
00115  *
00116  * Returns NULL if an error happened or if the directory could not
00117  * be read.
00118  **********************************************************************/
00119 
00136 char **CPLReadDir(const char *pszPath)
00137 {
00138     DIR           *hDir;
00139     struct dirent *psDirEntry;
00140     char          **papszDir = NULL;
00141 
00142     if (strlen(pszPath) == 0)
00143         pszPath = ".";
00144 
00145     if ( (hDir = opendir(pszPath)) != NULL )
00146     {
00147         while( (psDirEntry = readdir(hDir)) != NULL )
00148         {
00149             papszDir = CSLAddString(papszDir, psDirEntry->d_name);
00150         }
00151 
00152         closedir( hDir );
00153     }
00154     else
00155     {
00156         /* Should we generate an error???  
00157          * For now we'll just return NULL (at the end of the function)
00158          */
00159     }
00160 
00161     return papszDir;
00162 }
00163 
00164 #endif

Generated at Thu Mar 28 09:47:27 2002 for GDAL by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000