<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>cpl_dir.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.3-20001105 on Thu Mar 28 09:47:27 2002 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>cpl_dir.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/**********************************************************************</font> 00002 <font class="comment"> * $Id: cpl_dir_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $</font> 00003 <font class="comment"> *</font> 00004 <font class="comment"> * Name: cpl_dir.cpp</font> 00005 <font class="comment"> * Project: CPL - Common Portability Library</font> 00006 <font class="comment"> * Purpose: Directory manipulation.</font> 00007 <font class="comment"> * Author: Daniel Morissette, danmo@videotron.ca</font> 00008 <font class="comment"> *</font> 00009 <font class="comment"> **********************************************************************</font> 00010 <font class="comment"> * Copyright (c) 1998, Daniel Morissette</font> 00011 <font class="comment"> *</font> 00012 <font class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</font> 00013 <font class="comment"> * copy of this software and associated documentation files (the "Software"),</font> 00014 <font class="comment"> * to deal in the Software without restriction, including without limitation</font> 00015 <font class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</font> 00016 <font class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</font> 00017 <font class="comment"> * Software is furnished to do so, subject to the following conditions:</font> 00018 <font class="comment"> * </font> 00019 <font class="comment"> * The above copyright notice and this permission notice shall be included</font> 00020 <font class="comment"> * in all copies or substantial portions of the Software.</font> 00021 <font class="comment"> * </font> 00022 <font class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</font> 00023 <font class="comment"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font> 00024 <font class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</font> 00025 <font class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font> 00026 <font class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</font> 00027 <font class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER </font> 00028 <font class="comment"> * DEALINGS IN THE SOFTWARE.</font> 00029 <font class="comment"> **********************************************************************</font> 00030 <font class="comment"> *</font> 00031 <font class="comment"> * $Log: cpl_dir_cpp-source.html,v $ 00031 <font class="comment"> * Revision 1.10 2002/04/16 13:11:47 warmerda 00031 <font class="comment"> * updated 00031 <font class="comment"> *</font> 00032 <font class="comment"> * Revision 1.4 2001/07/18 04:00:49 warmerda</font> 00033 <font class="comment"> * added CPL_CVSID</font> 00034 <font class="comment"> *</font> 00035 <font class="comment"> * Revision 1.3 2000/09/25 19:59:03 warmerda</font> 00036 <font class="comment"> * look for WIN32 not _WIN32</font> 00037 <font class="comment"> *</font> 00038 <font class="comment"> * Revision 1.2 1999/05/20 02:54:38 warmerda</font> 00039 <font class="comment"> * Added API documentation</font> 00040 <font class="comment"> *</font> 00041 <font class="comment"> * Revision 1.1 1999/02/25 04:52:00 danmo</font> 00042 <font class="comment"> * *** empty log message ***</font> 00043 <font class="comment"> *</font> 00044 <font class="comment"> **********************************************************************/</font> 00045 00046 <font class="preprocessor">#include "<a class="code" href="cpl_conv_h.html">cpl_conv.h</a>"</font> 00047 <font class="preprocessor">#include "cpl_string.h"</font> 00048 00049 CPL_CVSID(<font class="stringliteral">"$Id: cpl_dir_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $"</font>); 00050 00051 <font class="preprocessor">#ifdef WIN32</font> 00052 <font class="preprocessor"></font> 00053 <font class="comment">/*=====================================================================</font> 00054 <font class="comment"> WIN32 / MSVC++ implementation</font> 00055 <font class="comment"> *====================================================================*/</font> 00056 00057 <font class="preprocessor">#include <io.h></font> 00058 00059 <font class="comment">/**********************************************************************</font> 00060 <font class="comment"> * CPLReadDir()</font> 00061 <font class="comment"> *</font> 00062 <font class="comment"> * Return a stringlist with the list of files in a directory.</font> 00063 <font class="comment"> * The returned stringlist should be freed with CSLDestroy().</font> 00064 <font class="comment"> *</font> 00065 <font class="comment"> * Returns NULL if an error happened or if the directory could not</font> 00066 <font class="comment"> * be read.</font> 00067 <font class="comment"> **********************************************************************/</font> 00068 00069 <font class="keywordtype">char</font> **<a class="code" href="cpl_conv_h.html#a9">CPLReadDir</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *pszPath)<font class="keyword"></font> 00070 <font class="keyword"></font>{ 00071 <font class="keyword">struct </font>_finddata_t c_file; 00072 <font class="keywordtype">long</font> hFile; 00073 <font class="keywordtype">char</font> *pszFileSpec, **papszDir = NULL; 00074 00075 <font class="keywordflow">if</font> (strlen(pszPath) == 0) 00076 pszPath = <font class="stringliteral">"."</font>; 00077 00078 pszFileSpec = <a class="code" href="cpl_conv_h.html#a6">CPLStrdup</a>(CPLSPrintf(<font class="stringliteral">"%s\\*.*"</font>, pszPath)); 00079 00080 <font class="keywordflow">if</font> ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L ) 00081 { 00082 <font class="keywordflow">do</font> 00083 { 00084 papszDir = CSLAddString(papszDir, c_file.name); 00085 } <font class="keywordflow">while</font>( _findnext( hFile, &c_file ) == 0 ); 00086 00087 _findclose( hFile ); 00088 } 00089 <font class="keywordflow">else</font> 00090 { 00091 <font class="comment">/* Should we generate an error??? </font> 00092 <font class="comment"> * For now we'll just return NULL (at the end of the function)</font> 00093 <font class="comment"> */</font> 00094 } 00095 00096 CPLFree(pszFileSpec); 00097 00098 <font class="keywordflow">return</font> papszDir; 00099 } 00100 00101 <font class="preprocessor">#else</font> 00102 <font class="preprocessor"></font> 00103 <font class="comment">/*=====================================================================</font> 00104 <font class="comment"> POSIX (Unix) implementation</font> 00105 <font class="comment"> *====================================================================*/</font> 00106 00107 <font class="preprocessor">#include <sys/types.h></font> 00108 <font class="preprocessor">#include <dirent.h></font> 00109 00110 <font class="comment">/**********************************************************************</font> 00111 <font class="comment"> * CPLReadDir()</font> 00112 <font class="comment"> *</font> 00113 <font class="comment"> * Return a stringlist with the list of files in a directory.</font> 00114 <font class="comment"> * The returned stringlist should be freed with CSLDestroy().</font> 00115 <font class="comment"> *</font> 00116 <font class="comment"> * Returns NULL if an error happened or if the directory could not</font> 00117 <font class="comment"> * be read.</font> 00118 <font class="comment"> **********************************************************************/</font> 00119 00136 <font class="keywordtype">char</font> **<a class="code" href="cpl_conv_h.html#a9">CPLReadDir</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *pszPath)<font class="keyword"></font> 00137 <font class="keyword"></font>{ 00138 DIR *hDir; 00139 <font class="keyword">struct </font>dirent *psDirEntry; 00140 <font class="keywordtype">char</font> **papszDir = NULL; 00141 00142 <font class="keywordflow">if</font> (strlen(pszPath) == 0) 00143 pszPath = <font class="stringliteral">"."</font>; 00144 00145 <font class="keywordflow">if</font> ( (hDir = opendir(pszPath)) != NULL ) 00146 { 00147 <font class="keywordflow">while</font>( (psDirEntry = readdir(hDir)) != NULL ) 00148 { 00149 papszDir = CSLAddString(papszDir, psDirEntry->d_name); 00150 } 00151 00152 closedir( hDir ); 00153 } 00154 <font class="keywordflow">else</font> 00155 { 00156 <font class="comment">/* Should we generate an error??? </font> 00157 <font class="comment"> * For now we'll just return NULL (at the end of the function)</font> 00158 <font class="comment"> */</font> 00159 } 00160 00161 <font class="keywordflow">return</font> papszDir; 00162 } 00163 00164 <font class="preprocessor">#endif</font> </font></div></pre><hr><address><small>Generated at Thu Mar 28 09:47:27 2002 for GDAL by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.3-20001105 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>