-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.c
More file actions
67 lines (58 loc) · 1.3 KB
/
platform.c
File metadata and controls
67 lines (58 loc) · 1.3 KB
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
#include "platform.h"
#ifdef _WIN32
#ifndef __MINGW32__ // Only define these functions if not using MinGW
#include <windows.h>
DIR_HANDLE *opendir(const char *dirname)
{
DIR_HANDLE *dir = malloc(sizeof(DIR_HANDLE));
if (!dir)
return NULL;
char searchPath[MAX_PATH];
snprintf(searchPath, MAX_PATH, "%s\\*", dirname);
dir->hFind = FindFirstFile(searchPath, &dir->findData);
if (dir->hFind == INVALID_HANDLE_VALUE)
{
free(dir);
return NULL;
}
return dir;
}
struct dirent *readdir(DIR_HANDLE *dir)
{
if (!dir || dir->hFind == INVALID_HANDLE_VALUE)
return NULL;
static struct dirent entry;
strncpy(entry.d_name, dir->findData.cFileName, sizeof(entry.d_name) - 1);
entry.d_name[sizeof(entry.d_name) - 1] = '\0';
if (!FindNextFile(dir->hFind, &dir->findData))
{
return NULL;
}
return &entry;
}
void closedir(DIR_HANDLE *dir)
{
if (dir && dir->hFind != INVALID_HANDLE_VALUE)
{
FindClose(dir->hFind);
}
free(dir);
}
#endif
#endif
void normalize_path(char *path)
{
#ifdef _WIN32
for (int i = 0; path[i]; i++)
{
if (path[i] == '/')
path[i] = '\\';
}
#else
for (int i = 0; path[i]; i++)
{
if (path[i] == '\\')
path[i] = '/';
}
#endif
}