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