1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/**********************************************************************
* $Id: cpl_dir.c,v 1.2 2005/06/03 03:49:59 daniel Exp $
*
* Name: cpl_dir.cpp
* Project: CPL - Common Portability Library
* Purpose: Directory manipulation.
* Author: Daniel Morissette, dmorissette@dmsolutions.ca
*
**********************************************************************
* Copyright (c) 1998, Daniel Morissette
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************
*
* $Log: cpl_dir.c,v $
* Revision 1.2 2005/06/03 03:49:59 daniel
* Update email address, website url, and copyright dates
*
* Revision 1.1 1999/08/27 14:09:55 daniel
* Update CPL files
*
* Revision 1.2 1999/05/20 02:54:38 warmerda
* Added API documentation
*
* Revision 1.1 1999/02/25 04:52:00 danmo
* *** empty log message ***
*
**********************************************************************/
#include "cpl_conv.h"
#include "cpl_string.h"
#ifdef _WIN32
/*=====================================================================
WIN32 / MSVC++ implementation
*====================================================================*/
#include <io.h>
/**********************************************************************
* CPLReadDir()
*
* Return a stringlist with the list of files in a directory.
* The returned stringlist should be freed with CSLDestroy().
*
* Returns NULL if an error happened or if the directory could not
* be read.
**********************************************************************/
/**
* Read names in a directory.
*
* This function abstracts access to directory contains. It returns a
* list of strings containing the names of files, and directories in this
* directory. The resulting string list becomes the responsibility of the
* application and should be freed with CSLDestroy() when no longer needed.
*
* Note that no error is issued via CPLError() if the directory path is
* invalid, though NULL is returned.
*
* @param pszPath the relative, or absolute path of a directory to read.
* @return The list of entries in the directory, or NULL if the directory
* doesn't exist.
*/
char **CPLReadDir(const char *pszPath)
{
struct _finddata_t c_file;
long hFile;
char *pszFileSpec, **papszDir = NULL;
if (strlen(pszPath) == 0)
pszPath = ".";
pszFileSpec = CPLStrdup(CPLSPrintf("%s\\*.*", pszPath));
if ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L )
{
do
{
papszDir = CSLAddString(papszDir, c_file.name);
} while( _findnext( hFile, &c_file ) == 0 );
_findclose( hFile );
}
else
{
/* Should we generate an error???
* For now we'll just return NULL (at the end of the function)
*/
}
CPLFree(pszFileSpec);
return papszDir;
}
#else
/*=====================================================================
POSIX (Unix) implementation
*====================================================================*/
#include <sys/types.h>
#include <dirent.h>
/**********************************************************************
* CPLReadDir()
*
* Return a stringlist with the list of files in a directory.
* The returned stringlist should be freed with CSLDestroy().
*
* Returns NULL if an error happened or if the directory could not
* be read.
**********************************************************************/
char **CPLReadDir(const char *pszPath)
{
DIR *hDir;
struct dirent *psDirEntry;
char **papszDir = NULL;
if (strlen(pszPath) == 0)
pszPath = ".";
if ( (hDir = opendir(pszPath)) != NULL )
{
while( (psDirEntry = readdir(hDir)) != NULL )
{
papszDir = CSLAddString(papszDir, psDirEntry->d_name);
}
closedir( hDir );
}
else
{
/* Should we generate an error???
* For now we'll just return NULL (at the end of the function)
*/
}
return papszDir;
}
#endif
|