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.1 2000/09/25 20:50:11 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.1  2000/09/25 20:50:11  warmerda
00031  * New
00031  *
00032  * Revision 1.2  1999/05/20 02:54:38  warmerda
00033  * Added API documentation
00034  *
00035  * Revision 1.1  1999/02/25 04:52:00  danmo
00036  * *** empty log message ***
00037  *
00038  **********************************************************************/
00039 
00040 #include "cpl_conv.h"
00041 #include "cpl_string.h"
00042 
00043 
00044 #ifdef _WIN32
00045 
00046 /*=====================================================================
00047                    WIN32 / MSVC++ implementation
00048  *====================================================================*/
00049 
00050 #include <io.h>
00051 
00052 /**********************************************************************
00053  *                          CPLReadDir()
00054  *
00055  * Return a stringlist with the list of files in a directory.
00056  * The returned stringlist should be freed with CSLDestroy().
00057  *
00058  * Returns NULL if an error happened or if the directory could not
00059  * be read.
00060  **********************************************************************/
00061 
00078 char **CPLReadDir(const char *pszPath)
00079 {
00080     struct _finddata_t c_file;
00081     long    hFile;
00082     char    *pszFileSpec, **papszDir = NULL;
00083 
00084     if (strlen(pszPath) == 0)
00085         pszPath = ".";
00086 
00087     pszFileSpec = CPLStrdup(CPLSPrintf("%s\\*.*", pszPath));
00088 
00089     if ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L )
00090     {
00091         do
00092         {
00093             papszDir = CSLAddString(papszDir, c_file.name);
00094         } while( _findnext( hFile, &c_file ) == 0 );
00095 
00096         _findclose( hFile );
00097     }
00098     else
00099     {
00100         /* Should we generate an error???  
00101          * For now we'll just return NULL (at the end of the function)
00102          */
00103     }
00104 
00105     CPLFree(pszFileSpec);
00106 
00107     return papszDir;
00108 }
00109 
00110 #else
00111 
00112 /*=====================================================================
00113                       POSIX (Unix) implementation
00114  *====================================================================*/
00115 
00116 #include <sys/types.h>
00117 #include <dirent.h>
00118 
00119 /**********************************************************************
00120  *                          CPLReadDir()
00121  *
00122  * Return a stringlist with the list of files in a directory.
00123  * The returned stringlist should be freed with CSLDestroy().
00124  *
00125  * Returns NULL if an error happened or if the directory could not
00126  * be read.
00127  **********************************************************************/
00128 char **CPLReadDir(const char *pszPath)
00129 {
00130     DIR           *hDir;
00131     struct dirent *psDirEntry;
00132     char          **papszDir = NULL;
00133 
00134     if (strlen(pszPath) == 0)
00135         pszPath = ".";
00136 
00137     if ( (hDir = opendir(pszPath)) != NULL )
00138     {
00139         while( (psDirEntry = readdir(hDir)) != NULL )
00140         {
00141             papszDir = CSLAddString(papszDir, psDirEntry->d_name);
00142         }
00143 
00144         closedir( hDir );
00145     }
00146     else
00147     {
00148         /* Should we generate an error???  
00149          * For now we'll just return NULL (at the end of the function)
00150          */
00151     }
00152 
00153     return papszDir;
00154 }
00155 
00156 #endif

doxygen1.2.2 Dimitri van Heesch, © 1997-2000