1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Matt J. Weinstein
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#include "CLucene/_ApiHeader.h"
9
#if !defined(_CL_HAVE_DIRENT_H) && !defined(_CL_HAVE_SYS_NDIR_H) && !defined(_CL_HAVE_SYS_DIR_H) && !defined(_CL_HAVE_NDIR_H)
16
opendir (const char *szPath)
19
char szFullPath[CL_MAX_PATH];
29
if (szPath[0] == '\0')
35
/* Attempt to determine if the given path really is a directory. */
37
if ( fileStat(szPath,&rcs) == -1)
39
/* call GetLastError for more error info */
43
if (!(rcs.st_mode & _S_IFDIR))
45
/* Error, entry exists but not a directory. */
50
/* Make an absolute pathname. */
51
_realpath(szPath,szFullPath);
53
/* Allocate enough space to store DIR structure and the complete
54
* directory path given. */
55
//nd = (DIR *) malloc (sizeof (DIR) + _tcslen (szFullPath) + _tcslen (DIRENT_SLASH) +
56
// _tcslen (DIRENT_SEARCH_SUFFIX)+1);
61
/* Error, out of memory. */
66
/* Create the search expression. */
67
strcpy (nd->dd_name, szFullPath);
69
/* Add on a slash if the path does not end with one. */
70
if (nd->dd_name[0] != '\0' &&
71
nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
72
nd->dd_name[strlen (nd->dd_name) - 1] != '\\')
74
strcat (nd->dd_name, DIRENT_SLASH);
77
/* Add on the search pattern */
78
strcat (nd->dd_name, DIRENT_SEARCH_SUFFIX);
80
/* Initialize handle to -1 so that a premature closedir doesn't try
81
* to call _findclose on it. */
84
/* Initialize the status. */
87
/* Initialize the dirent structure. ino and reclen are invalid under
88
* Win32, and name simply points at the appropriate part of the
89
* findfirst_t structure. */
90
//nd->dd_dir.d_ino = 0;
91
//nd->dd_dir.d_reclen = 0;
92
nd->dd_dir.d_namlen = 0;
93
nd->dd_dir.d_name = nd->dd_dta.name;
99
struct dirent * readdir (DIR * dirp)
103
/* Check for valid DIR struct. */
110
if (dirp->dd_dir.d_name != dirp->dd_dta.name)
112
/* The structure does not seem to be set up correctly. */
117
bool bCallFindNext = true;
119
if (dirp->dd_stat < 0)
121
/* We have already returned all files in the directory
122
* (or the structure has an invalid dd_stat). */
125
else if (dirp->dd_stat == 0)
127
/* We haven't started the search yet. */
128
/* Start the search */
129
dirp->dd_handle = _findfirst (dirp->dd_name, &(dirp->dd_dta));
131
if (dirp->dd_handle == -1)
133
/* Whoops! Seems there are no files in that
142
/* Dont call _findnext first time. */
143
bCallFindNext = false;
146
while (dirp->dd_stat > 0)
150
/* Get the next search entry. */
151
if (_findnext (dirp->dd_handle, &(dirp->dd_dta)))
153
/* We are off the end or otherwise error. */
154
_findclose (dirp->dd_handle);
155
dirp->dd_handle = -1;
161
/* Update the status to indicate the correct
167
/* Successfully got an entry. Everything about the file is
168
* already appropriately filled in except the length of the
170
dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name);
172
bool bThisFolderOrUpFolder = dirp->dd_dir.d_name[0] == '.' &&
173
(dirp->dd_dir.d_name[1] == 0 || (dirp->dd_dir.d_name[1] == '.' && dirp->dd_dir.d_name[2] == 0));
175
if (!bThisFolderOrUpFolder)
177
struct cl_stat_t buf;
178
char buffer[CL_MAX_DIR];
179
size_t bl = strlen(dirp->dd_name)-strlen(DIRENT_SEARCH_SUFFIX);
180
strncpy(buffer,dirp->dd_name,bl);
182
strcat(buffer, dirp->dd_dir.d_name);
183
if ( fileStat(buffer,&buf) == 0 )
185
/* Finally we have a valid entry. */
186
return &dirp->dd_dir;
190
/* Allow to find next file. */
191
bCallFindNext = true;
200
closedir (DIR * dirp)
213
if (dirp->dd_handle != -1)
215
rc = _findclose (dirp->dd_handle);
218
/* Delete the dir structure. */
223
#endif //HAVE_DIRENT_H