2 Copyright (c) 2004-2005, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file DirScanner.cpp
29 \brief Cross-platform, simple non-recursive directory scanner
32 #include <DirScanner.h>
36 // Win32 directory scanner
43 DirScanner::Open(const char* filename)
45 ASDCP_TEST_NULL_STR(filename);
47 // we need to append a '*' to read the entire directory
48 ui32_t fn_len = strlen(filename);
49 char* tmp_file = (char*)malloc(fn_len + 8);
52 return ASDCP::RESULT_ALLOC;
54 strcpy(tmp_file, filename);
55 char* p = &tmp_file[fn_len] - 1;
57 if ( *p != '/' && *p != '\\' )
67 m_Handle = _findfirsti64(tmp_file, &m_FileInfo);
68 ASDCP::Result_t result = ASDCP::RESULT_OK;
71 result = ASDCP::RESULT_NOT_FOUND;
83 return ASDCP::RESULT_FILEOPEN;
85 if ( _findclose((long)m_Handle) == -1 )
86 return ASDCP::RESULT_FAIL;
89 return ASDCP::RESULT_OK;
93 // This sets filename param to the same per-instance buffer every time, so
94 // the value will change on the next call
96 DirScanner::GetNext(char* filename)
98 ASDCP_TEST_NULL(filename);
100 if ( m_Handle == -1 )
101 return ASDCP::RESULT_FILEOPEN;
103 if ( m_FileInfo.name[0] == '\0' )
104 return ASDCP::RESULT_ENDOFFILE;
106 strncpy(filename, m_FileInfo.name, ASDCP_MAX_PATH);
107 ASDCP::Result_t result = ASDCP::RESULT_OK;
109 if ( _findnexti64((long)m_Handle, &m_FileInfo) == -1 )
111 m_FileInfo.name[0] = '\0';
113 if ( errno != ENOENT )
114 result = ASDCP::RESULT_FAIL;
123 // POSIX directory scanner
127 DirScanner::Open(const char* filename)
129 ASDCP_TEST_NULL_STR(filename);
131 ASDCP::Result_t result = ASDCP::RESULT_OK;
133 if ( ( m_Handle = opendir(filename) ) == NULL )
135 if ( errno == ENOENT )
136 result = ASDCP::RESULT_ENDOFFILE;
139 result = ASDCP::RESULT_FAIL;
150 if ( m_Handle == NULL )
151 return ASDCP::RESULT_FILEOPEN;
153 if ( closedir(m_Handle) == -1 )
154 return ASDCP::RESULT_FAIL;
157 return ASDCP::RESULT_OK;
163 DirScanner::GetNext(char* filename)
165 ASDCP_TEST_NULL(filename);
167 if ( m_Handle == NULL )
168 return ASDCP::RESULT_FILEOPEN;
170 struct dirent* entry;
174 if ( ( entry = readdir(m_Handle)) == NULL )
175 return ASDCP::RESULT_ENDOFFILE;
180 strncpy(filename, entry->d_name, ASDCP_MAX_PATH);
181 return ASDCP::RESULT_OK;
188 // end DirScanner.cpp