-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpl_dir.cpp
176 lines (150 loc) · 5.43 KB
/
cpl_dir.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**********************************************************************
* $Id: cpl_dir.cpp,v 1.6 2006/02/19 21:54:34 mloskot Exp $
*
* Name: cpl_dir.cpp
* Project: CPL - Common Portability Library
* Purpose: Directory manipulation.
* Author: Daniel Morissette, [email protected]
*
**********************************************************************
* 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.cpp,v $
* Revision 1.6 2006/02/19 21:54:34 mloskot
* [WINCE] Changes related to Windows CE port of CPL. Most changes are #ifdef wrappers.
*
* Revision 1.5 2002/03/28 14:52:32 warmerda
* moved docs to non-WIN32 section
*
* Revision 1.4 2001/07/18 04:00:49 warmerda
* added CPL_CVSID
*
* Revision 1.3 2000/09/25 19:59:03 warmerda
* look for WIN32 not _WIN32
*
* 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"
CPL_CVSID("$Id: cpl_dir.cpp,v 1.6 2006/02/19 21:54:34 mloskot Exp $");
#if defined(WIN32) || defined(WIN32CE)
/*=====================================================================
WIN32 / MSVC++ implementation
*====================================================================*/
#ifndef WIN32CE
# include <io.h>
#else
# include <wce_io.h>
#endif
/**********************************************************************
* 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)
{
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.
**********************************************************************/
/**
* 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)
{
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