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:
Bill Kendrick 2017-10-15 11:09:17 -07:00
parent 4aabc2da43
commit f8c4dc626c
2 changed files with 192 additions and 192 deletions

View file

@ -35,94 +35,91 @@
#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));
if (!pDir)
return NULL; return NULL;
strcpy(pathname, pSpec); strcpy(pathname, pSpec);
strcat(pathname, "/*"); strcat(pathname, "/*");
pDir->hFind = FindFirstFile(pathname, &pDir->wfd); pDir->hFind = FindFirstFile(pathname, &pDir->wfd);
if (pDir->hFind == INVALID_HANDLE_VALUE) if (pDir->hFind == INVALID_HANDLE_VALUE)
{
free(pDir);
pDir = NULL;
}
return pDir;
}
void closedir(DIR * pDir)
{
assert(pDir != NULL);
free(pDir);
} struct dirent *readdir(struct DIR *pDir)
{
assert(pDir != NULL);
if (pDir->hFind)
{
strcpy(pDir->de.d_name, (const char *) pDir->wfd.cFileName);
if (!FindNextFile(pDir->hFind, &pDir->wfd))
{ {
FindClose(pDir->hFind); free(pDir);
pDir->hFind = NULL; pDir = NULL;
} }
return &pDir->de; return pDir;
} }
return NULL;
}
int alphasort(const void *a, const void *b)
{
return (strcmp
((*(const struct dirent **) a)->d_name,
(*(const struct dirent **) b)->d_name));
} static int addToList(int i, struct dirent ***namelist,
struct dirent *entry)
{
int size;
struct dirent *block;
*namelist =
(struct dirent **) realloc((void *) (*namelist),
(size_t) ((i + 1) * sizeof(struct dirent *)));
if (*namelist == NULL)
return -1;
size =
(((char *) &entry->d_name) - ((char *) entry)) + strlen(entry->d_name) +
1;
block = (struct dirent *) malloc(size);
if (block == NULL)
return -1;
(*namelist)[i] = block;
memcpy(block, entry, size);
return ++i;
}
int scandir(const char *dir, struct dirent ***namelist, selectCB select,
comparCB compar)
{
DIR * pDir;
int count;
struct dirent *entry;
assert((dir != NULL) && (namelist != NULL));
pDir = opendir(dir);
if (!pDir)
return -1;
count = 0;
while ((entry = readdir(pDir)) != NULL)
{ void closedir(DIR * pDir)
if (select == NULL || (select != NULL && select(entry))) {
if ((count = addToList(count, namelist, entry)) < 0) assert(pDir != NULL);
break; free(pDir);
} } struct dirent *readdir(struct DIR *pDir)
closedir(pDir); {
if (count <= 0) assert(pDir != NULL);
if (pDir->hFind)
{
strcpy(pDir->de.d_name, (const char *)pDir->wfd.cFileName);
if (!FindNextFile(pDir->hFind, &pDir->wfd))
{
FindClose(pDir->hFind);
pDir->hFind = NULL;
}
return &pDir->de;
}
return NULL;
}
int alphasort(const void *a, const void *b)
{
return (strcmp((*(const struct dirent **)a)->d_name, (*(const struct dirent **)b)->d_name));
} static int addToList(int i, struct dirent ***namelist, struct dirent *entry)
{
int size;
struct dirent *block;
*namelist = (struct dirent **)realloc((void *)(*namelist), (size_t) ((i + 1) * sizeof(struct dirent *)));
if (*namelist == NULL)
return -1; return -1;
if (compar != NULL) size = (((char *)&entry->d_name) - ((char *)entry)) + strlen(entry->d_name) + 1;
qsort((void *) (*namelist), (size_t) count, sizeof(struct dirent *), block = (struct dirent *)malloc(size);
compar); if (block == NULL)
return count; return -1;
} (*namelist)[i] = block;
memcpy(block, entry, size);
return ++i;
}
int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar)
{
DIR * pDir;
int count;
struct dirent *entry;
assert((dir != NULL) && (namelist != NULL));
pDir = opendir(dir);
if (!pDir)
return -1;
count = 0;
while ((entry = readdir(pDir)) != NULL)
{
if (select == NULL || (select != NULL && select(entry)))
if ((count = addToList(count, namelist, entry)) < 0)
break;
}
closedir(pDir);
if (count <= 0)
return -1;
if (compar != NULL)
qsort((void *)(*namelist), (size_t) count, sizeof(struct dirent *), compar);
return count;
}

View file

@ -8,32 +8,34 @@
/* */ /* */
/****************************************************/ /****************************************************/
/* $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 MAX_PATH 256
#define INVALID_HANDLE_VALUE ((HANDLE)(-1)) #define INVALID_HANDLE_VALUE ((HANDLE)(-1))
#define WINAPI __stdcall #define WINAPI __stdcall
typedef struct 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 FindFirstFile FindFirstFileA
#define FindNextFile FindNextFileA #define FindNextFile FindNextFileA
@ -43,31 +45,32 @@
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; {
HANDLE hFind; WIN32_FIND_DATA wfd;
struct dirent de; HANDLE hFind;
} DIR; struct dirent de;
extern DIR *opendir(const char *pSpec); } DIR;
extern void closedir(DIR * pDir); extern DIR *opendir(const char *pSpec);
extern struct dirent *readdir(struct DIR *pDir); extern void closedir(DIR * pDir);
typedef int (*selectCB) (const struct dirent *); extern struct dirent *readdir(struct DIR *pDir);
typedef int (*comparCB) (const void *, const void *); typedef int (*selectCB) (const struct dirent *);
extern int alphasort(const void *a, const void *b); typedef int (*comparCB) (const void *, const void *);
extern int scandir(const char *dir, struct dirent ***namelist, extern int alphasort(const void *a, const void *b);
selectCB select, comparCB compar); extern int scandir(const char *dir, struct dirent ***namelist, selectCB select, comparCB compar);