pre-release commit
[asdcplib.git] / DirScanner.cpp
1 /*
2 Copyright (c) 2004-2005, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    DirScanner.cpp
28     \version $Id$
29     \brief   Cross-platform, simple non-recursive directory scanner
30 */
31
32 #include <DirScanner.h>
33 #include <errno.h>
34 #include <assert.h>
35
36 // Win32 directory scanner
37 //
38 #ifdef WIN32
39
40 //
41 //
42 ASDCP::Result_t
43 DirScanner::Open(const char* filename)
44 {
45   ASDCP_TEST_NULL_STR(filename);
46
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);
50
51   if ( tmp_file == 0 )
52     return ASDCP::RESULT_ALLOC;
53
54   strcpy(tmp_file, filename);
55   char* p = &tmp_file[fn_len] - 1;
56
57   if ( *p != '/' && *p != '\\' )
58     {
59       p++;
60       *p++ = '/';
61     }
62
63   *p++ = '*';
64   *p = 0;
65   // whew...
66
67   m_Handle = _findfirsti64(tmp_file, &m_FileInfo);
68   ASDCP::Result_t result = ASDCP::RESULT_OK;
69
70   if ( m_Handle == -1 )
71     result = ASDCP::RESULT_NOT_FOUND;
72
73   return result;
74 }
75
76
77 //
78 //
79 ASDCP::Result_t
80 DirScanner::Close()
81 {
82   if ( m_Handle == -1 )
83     return ASDCP::RESULT_FILEOPEN;
84
85   if ( _findclose((long)m_Handle) == -1 )
86     return ASDCP::RESULT_FAIL;
87
88   m_Handle = -1;
89   return ASDCP::RESULT_OK;
90 }
91
92
93 // This sets filename param to the same per-instance buffer every time, so
94 // the value will change on the next call
95 ASDCP::Result_t
96 DirScanner::GetNext(char* filename)
97 {
98   ASDCP_TEST_NULL(filename);
99
100   if ( m_Handle == -1 )
101     return ASDCP::RESULT_FILEOPEN;
102
103   if ( m_FileInfo.name[0] == '\0' )
104     return ASDCP::RESULT_ENDOFFILE;
105
106   strncpy(filename, m_FileInfo.name, ASDCP_MAX_PATH);
107   ASDCP::Result_t result = ASDCP::RESULT_OK;
108
109   if ( _findnexti64((long)m_Handle, &m_FileInfo) == -1 )
110     {
111       m_FileInfo.name[0] = '\0';
112           
113       if ( errno != ENOENT )
114         result = ASDCP::RESULT_FAIL;
115     }
116
117   return result;
118 }
119
120
121 #else // WIN32
122
123 // POSIX directory scanner
124
125 //
126 ASDCP::Result_t
127 DirScanner::Open(const char* filename)
128 {
129   ASDCP_TEST_NULL_STR(filename);
130
131   ASDCP::Result_t result = ASDCP::RESULT_OK;
132
133   if ( ( m_Handle = opendir(filename) ) == NULL )
134     {
135       if ( errno == ENOENT )
136         result = ASDCP::RESULT_ENDOFFILE;
137
138       else
139         result = ASDCP::RESULT_FAIL;
140     }
141
142   return result;
143 }
144
145
146 //
147 ASDCP::Result_t
148 DirScanner::Close()
149 {
150   if ( m_Handle == NULL )
151     return ASDCP::RESULT_FILEOPEN;
152
153   if ( closedir(m_Handle) == -1 )
154     return ASDCP::RESULT_FAIL;
155
156   m_Handle = NULL;
157   return ASDCP::RESULT_OK;
158 }
159
160
161 //
162 ASDCP::Result_t
163 DirScanner::GetNext(char* filename)
164 {
165   ASDCP_TEST_NULL(filename);
166
167   if ( m_Handle == NULL )
168     return ASDCP::RESULT_FILEOPEN;
169
170   struct dirent* entry;
171
172   for (;;)
173     {
174       if ( ( entry = readdir(m_Handle)) == NULL )
175         return ASDCP::RESULT_ENDOFFILE;
176
177       break;
178     }
179
180   strncpy(filename, entry->d_name, ASDCP_MAX_PATH);
181   return ASDCP::RESULT_OK;
182 }
183
184
185 #endif // WIN32
186
187 //
188 // end DirScanner.cpp
189 //