indent, and tr -d "\r", win32_dirent.c & .h
Ran indent on win32_dirent.* and also removed MSDOS CR ("^M" aka "\r")
This commit is contained in:
parent
4aabc2da43
commit
f8c4dc626c
2 changed files with 192 additions and 192 deletions
|
|
@ -1,128 +1,125 @@
|
||||||
/****************************************************/
|
/****************************************************/
|
||||||
/* */
|
/* */
|
||||||
/* For Win32 that lacks Unix direct support. */
|
/* For Win32 that lacks Unix direct support. */
|
||||||
/* - avoids including "windows.h" */
|
/* - avoids including "windows.h" */
|
||||||
/* */
|
/* */
|
||||||
/* Copyright (c) 2002 John Popplewell */
|
/* Copyright (c) 2002 John Popplewell */
|
||||||
/* john@johnnypops.demon.co.uk */
|
/* john@johnnypops.demon.co.uk */
|
||||||
/* */
|
/* */
|
||||||
/* Version 1.0.1 - fixed bug in opendir() */
|
/* Version 1.0.1 - fixed bug in opendir() */
|
||||||
/* Version 1.0.0 - initial version */
|
/* Version 1.0.0 - initial version */
|
||||||
/* */
|
/* */
|
||||||
/****************************************************/
|
/****************************************************/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
the Free Software Foundation; either version 2 of the License, or
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "win32_dirent.h"
|
#include "win32_dirent.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
DIR * opendir(const char *pSpec)
|
DIR * opendir(const char *pSpec)
|
||||||
{
|
{
|
||||||
char pathname[MAX_PATH + 2];
|
char pathname[MAX_PATH + 2];
|
||||||
DIR * pDir = calloc(1, sizeof(DIR));
|
|
||||||
if (!pDir)
|
DIR * pDir = calloc(1, sizeof(DIR));
|
||||||
return NULL;
|
if (!pDir)
|
||||||
strcpy(pathname, pSpec);
|
return NULL;
|
||||||
strcat(pathname, "/*");
|
strcpy(pathname, pSpec);
|
||||||
pDir->hFind = FindFirstFile(pathname, &pDir->wfd);
|
strcat(pathname, "/*");
|
||||||
if (pDir->hFind == INVALID_HANDLE_VALUE)
|
pDir->hFind = FindFirstFile(pathname, &pDir->wfd);
|
||||||
|
if (pDir->hFind == INVALID_HANDLE_VALUE)
|
||||||
{
|
|
||||||
free(pDir);
|
{
|
||||||
pDir = NULL;
|
free(pDir);
|
||||||
}
|
pDir = NULL;
|
||||||
return pDir;
|
}
|
||||||
}
|
return pDir;
|
||||||
void closedir(DIR * pDir)
|
}
|
||||||
{
|
|
||||||
assert(pDir != NULL);
|
void closedir(DIR * pDir)
|
||||||
free(pDir);
|
{
|
||||||
}
struct dirent *readdir(struct DIR *pDir)
|
assert(pDir != NULL);
|
||||||
{
|
free(pDir);
|
||||||
assert(pDir != NULL);
|
} struct dirent *readdir(struct DIR *pDir)
|
||||||
if (pDir->hFind)
|
{
|
||||||
|
assert(pDir != NULL);
|
||||||
{
|
if (pDir->hFind)
|
||||||
strcpy(pDir->de.d_name, (const char *) pDir->wfd.cFileName);
|
|
||||||
if (!FindNextFile(pDir->hFind, &pDir->wfd))
|
{
|
||||||
|
strcpy(pDir->de.d_name, (const char *)pDir->wfd.cFileName);
|
||||||
{
|
if (!FindNextFile(pDir->hFind, &pDir->wfd))
|
||||||
FindClose(pDir->hFind);
|
|
||||||
pDir->hFind = NULL;
|
{
|
||||||
}
|
FindClose(pDir->hFind);
|
||||||
return &pDir->de;
|
pDir->hFind = NULL;
|
||||||
}
|
}
|
||||||
return NULL;
|
return &pDir->de;
|
||||||
}
|
}
|
||||||
int alphasort(const void *a, const void *b)
|
return NULL;
|
||||||
{
|
}
|
||||||
return (strcmp
|
|
||||||
((*(const struct dirent **) a)->d_name,
|
int alphasort(const void *a, const void *b)
|
||||||
(*(const struct dirent **) b)->d_name));
|
{
|
||||||
}
static int addToList(int i, struct dirent ***namelist,
|
return (strcmp((*(const struct dirent **)a)->d_name, (*(const struct dirent **)b)->d_name));
|
||||||
struct dirent *entry)
|
} static int addToList(int i, struct dirent ***namelist, struct dirent *entry)
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
struct dirent *block;
|
struct dirent *block;
|
||||||
*namelist =
|
|
||||||
(struct dirent **) realloc((void *) (*namelist),
|
*namelist = (struct dirent **)realloc((void *)(*namelist), (size_t) ((i + 1) * sizeof(struct dirent *)));
|
||||||
(size_t) ((i + 1) * sizeof(struct dirent *)));
|
if (*namelist == NULL)
|
||||||
if (*namelist == NULL)
|
return -1;
|
||||||
return -1;
|
size = (((char *)&entry->d_name) - ((char *)entry)) + strlen(entry->d_name) + 1;
|
||||||
size =
|
block = (struct dirent *)malloc(size);
|
||||||
(((char *) &entry->d_name) - ((char *) entry)) + strlen(entry->d_name) +
|
if (block == NULL)
|
||||||
1;
|
return -1;
|
||||||
block = (struct dirent *) malloc(size);
|
(*namelist)[i] = block;
|
||||||
if (block == NULL)
|
memcpy(block, entry, size);
|
||||||
return -1;
|
return ++i;
|
||||||
(*namelist)[i] = block;
|
}
|
||||||
memcpy(block, entry, size);
|
|
||||||
return ++i;
|
int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar)
|
||||||
}
|
{
|
||||||
int scandir(const char *dir, struct dirent ***namelist, selectCB select,
|
DIR * pDir;
|
||||||
comparCB compar)
|
int count;
|
||||||
{
|
struct dirent *entry;
|
||||||
DIR * pDir;
|
|
||||||
int count;
|
assert((dir != NULL) && (namelist != NULL));
|
||||||
struct dirent *entry;
|
pDir = opendir(dir);
|
||||||
assert((dir != NULL) && (namelist != NULL));
|
if (!pDir)
|
||||||
pDir = opendir(dir);
|
return -1;
|
||||||
if (!pDir)
|
count = 0;
|
||||||
return -1;
|
while ((entry = readdir(pDir)) != NULL)
|
||||||
count = 0;
|
|
||||||
while ((entry = readdir(pDir)) != NULL)
|
{
|
||||||
|
if (select == NULL || (select != NULL && select(entry)))
|
||||||
{
|
if ((count = addToList(count, namelist, entry)) < 0)
|
||||||
if (select == NULL || (select != NULL && select(entry)))
|
break;
|
||||||
if ((count = addToList(count, namelist, entry)) < 0)
|
}
|
||||||
break;
|
closedir(pDir);
|
||||||
}
|
if (count <= 0)
|
||||||
closedir(pDir);
|
return -1;
|
||||||
if (count <= 0)
|
if (compar != NULL)
|
||||||
return -1;
|
qsort((void *)(*namelist), (size_t) count, sizeof(struct dirent *), compar);
|
||||||
if (compar != NULL)
|
return count;
|
||||||
qsort((void *) (*namelist), (size_t) count, sizeof(struct dirent *),
|
}
|
||||||
compar);
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,73 +1,76 @@
|
||||||
/****************************************************/
|
/****************************************************/
|
||||||
/* */
|
/* */
|
||||||
/* For Win32 that lacks Unix direct support. */
|
/* For Win32 that lacks Unix direct support. */
|
||||||
/* - avoids including "windows.h" */
|
/* - avoids including "windows.h" */
|
||||||
/* */
|
/* */
|
||||||
/* Copyright (c) 2002 John Popplewell */
|
/* Copyright (c) 2002 John Popplewell */
|
||||||
/* john@johnnypops.demon.co.uk */
|
/* john@johnnypops.demon.co.uk */
|
||||||
/* */
|
/* */
|
||||||
/****************************************************/
|
/****************************************************/
|
||||||
/* $Id$ */
|
/* $Id$ */
|
||||||
typedef long BOOL;
|
typedef long BOOL;
|
||||||
typedef unsigned int DWORD;
|
typedef unsigned int DWORD;
|
||||||
typedef wchar_t TCHAR;
|
typedef wchar_t TCHAR;
|
||||||
typedef void *HANDLE;
|
typedef void *HANDLE;
|
||||||
|
|
||||||
#define MAX_PATH 256
|
|
||||||
#define INVALID_HANDLE_VALUE ((HANDLE)(-1))
|
#define MAX_PATH 256
|
||||||
#define WINAPI __stdcall
|
#define INVALID_HANDLE_VALUE ((HANDLE)(-1))
|
||||||
typedef struct
|
#define WINAPI __stdcall
|
||||||
|
typedef struct
|
||||||
{
|
{
|
||||||
DWORD dwLowDateTime;
|
DWORD dwLowDateTime;
|
||||||
DWORD dwHighDateTime;
|
DWORD dwHighDateTime;
|
||||||
} FILETIME;
|
} FILETIME;
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
DWORD dwFileAttributes;
|
DWORD dwFileAttributes;
|
||||||
FILETIME ftCreationTime;
|
FILETIME ftCreationTime;
|
||||||
FILETIME ftLastAccessTime;
|
FILETIME ftLastAccessTime;
|
||||||
FILETIME ftLastWriteTime;
|
FILETIME ftLastWriteTime;
|
||||||
DWORD nFileSizeHigh;
|
DWORD nFileSizeHigh;
|
||||||
DWORD nFileSizeLow;
|
DWORD nFileSizeLow;
|
||||||
DWORD dwReserved0;
|
DWORD dwReserved0;
|
||||||
DWORD dwReserved1;
|
DWORD dwReserved1;
|
||||||
TCHAR cFileName[MAX_PATH];
|
TCHAR cFileName[MAX_PATH];
|
||||||
TCHAR cAlternateFileName[14];
|
TCHAR cAlternateFileName[14];
|
||||||
} WIN32_FIND_DATA;
|
} WIN32_FIND_DATA;
|
||||||
|
|
||||||
#define FindFirstFile FindFirstFileA
|
|
||||||
#define FindNextFile FindNextFileA
|
#define FindFirstFile FindFirstFileA
|
||||||
#define FindClose FindClose
|
#define FindNextFile FindNextFileA
|
||||||
|
#define FindClose FindClose
|
||||||
#ifdef __cplusplus
|
|
||||||
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
|
|
||||||
#endif /*
*/
|
#endif /* */
|
||||||
extern HANDLE WINAPI FindFirstFile(const char *, WIN32_FIND_DATA *);
|
extern HANDLE WINAPI FindFirstFile(const char *, WIN32_FIND_DATA *);
|
||||||
extern BOOL WINAPI FindNextFile(HANDLE, WIN32_FIND_DATA *);
|
extern BOOL WINAPI FindNextFile(HANDLE, WIN32_FIND_DATA *);
|
||||||
extern BOOL WINAPI FindClose(HANDLE);
|
extern BOOL WINAPI FindClose(HANDLE);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*
*/
|
|
||||||
struct dirent
|
#endif /* */
|
||||||
|
struct dirent
|
||||||
{
|
{
|
||||||
char d_name[MAX_PATH];
|
char d_name[MAX_PATH];
|
||||||
};
|
};
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
WIN32_FIND_DATA wfd;
|
WIN32_FIND_DATA wfd;
|
||||||
HANDLE hFind;
|
HANDLE hFind;
|
||||||
struct dirent de;
|
struct dirent de;
|
||||||
} DIR;
|
} DIR;
|
||||||
extern DIR *opendir(const char *pSpec);
|
extern DIR *opendir(const char *pSpec);
|
||||||
extern void closedir(DIR * pDir);
|
extern void closedir(DIR * pDir);
|
||||||
extern struct dirent *readdir(struct DIR *pDir);
|
extern struct dirent *readdir(struct DIR *pDir);
|
||||||
typedef int (*selectCB) (const struct dirent *);
|
typedef int (*selectCB) (const struct dirent *);
|
||||||
typedef int (*comparCB) (const void *, const void *);
|
typedef int (*comparCB) (const void *, const void *);
|
||||||
extern int alphasort(const void *a, const void *b);
|
extern int alphasort(const void *a, const void *b);
|
||||||
extern int scandir(const char *dir, struct dirent ***namelist,
|
extern int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar);
|
||||||
selectCB select, comparCB compar);
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue