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
|
#include "McDirectory.h"
#include "McSorter.h"
#include "McString.h"
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#include <MAPIWIN.h>
int McDirectory::scan(const char *dirname, McDArray<char *> &list,
const char *pattern)
{
WIN32_FIND_DATA findFileData;
char buf[1024];
if (!pattern) {
pattern="*";
}
sprintf(buf,"%s\\%s",dirname,pattern);
HANDLE searchHandle = FindFirstFile(buf, &findFileData);
if (searchHandle==INVALID_HANDLE_VALUE) {
printf("No files matching %s found.\n",buf);
return(0);
}
int next=1;
while (searchHandle!=INVALID_HANDLE_VALUE && next) {
if (strcmp(".",findFileData.cFileName)!=0 &&
strcmp("..",findFileData.cFileName)!=0 )
list.append(_strdup(findFileData.cFileName));
next=FindNextFile(searchHandle,&findFileData);
}
FindClose(searchHandle);
StringCompare comp;
if (list.size())
sort((char **)list,list.size(),comp);
return(0);
}
int McDirectory::isDirectory(const char *dirname)
{
McString d(dirname);
if (!d.length())
return(0);
while (d.length() && d[d.length()-1]==pathDelimiter()) {
d.remove(d.length()-1);
}
WIN32_FIND_DATA findFileData;
HANDLE searchHandle = FindFirstFile((const char *)d, &findFileData);
if (searchHandle==INVALID_HANDLE_VALUE) {
return(0);
}
int ret = ((findFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0);
FindClose(searchHandle);
return(ret);
}
#else
#include <dirent.h>
/* Great. We're doing UNIX, not Windows ! */
int McDirectory::scan(const char *dirname, McDArray<char *> &list,
const char *pattern)
{
if (!pattern) {
pattern="*";
}
DIR *dir = opendir(dirname);
struct dirent *entry;
if (!dir) {
fprintf(stderr,"Can't open dir %s\n",dirname);
return(0);
}
while (entry = readdir(dir)) {
if (entry->d_name[0]!='.')
if (mcWildMatch(entry->d_name,pattern))
list.append(strdup(entry->d_name));
};
closedir(dir);
StringCompare comp;
if (list.size())
sort((char **)list,list.size(),comp);
return(list.size());
}
int McDirectory::isDirectory(const char *dirname)
{
McString d(dirname);
if (!d.length())
return(0);
while (d.length() && d[d.length()-1]==pathDelimiter()) {
d.remove(d.length()-1);
}
DIR *dir = opendir(dirname);
if (!dir)
return(0);
closedir(dir);
return(1);
}
#endif
|