This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
system_file_io.cpp
111 lines (96 loc) · 3.72 KB
/
system_file_io.cpp
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
106
107
108
109
110
111
// system_file_io.cpp
#include "system_file_io.h"
#include <cstdlib>
#include <cwchar>
#include <vector>
// Windows-Specific Code
#include <windows.h>
#include <windowsx.h>
#include <shobjidl.h>
bool get_dir_contents_win32(const char *dir, int nbuffers, int bufferSize,
char **fnames, int *nnames, int *nameLengths, char **dirnames, int *ndir,
int *dirLengths);
// End Windows-Specific Code
using namespace std;
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
/* ************************************************************************** */
/* SPLIT_FILE_PATH */
/* ************************************************************************** */
void split_file_path_c(const char *path, char *drive, char *dir, char *fname,
char *ext)
{
_splitpath(path, drive, dir, fname, ext);
}
/* ************************************************************************** */
/* GET_DIRECTORY_CONTENTS */
/* ************************************************************************** */
bool get_directory_contents_c(const char *dir, int nbuffers, int bufferSize,
char **fnames, int *nnames, int *nameLengths, char **dirnames, int *ndir,
int *dirLengths)
{
// Windows-Specific Code
return get_dir_contents_win32(dir, nbuffers, bufferSize, fnames, nnames,
nameLengths, dirnames, ndir, dirLengths);
// End Windows-Specific Code
}
// ---------------------------- Windows Version ----------------------------- //
bool get_dir_contents_win32(const char *dir, int nbuffers, int bufferSize,
char **fnames, int *nnames, int *nameLengths, char **dirnames, int *ndir,
int *dirLengths)
{
// Local Variables
const size_t SPATH_SIZE = 2048;
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
wchar_t spath[SPATH_SIZE];
size_t n;
// Initialize the output variables
*nnames = 0;
*ndir = 0;
// Get everything in the directory
n = strlen(dir);
if (dir[n-1] == 92) swprintf(spath, SPATH_SIZE, L"%s*.*", dir);
else swprintf(spath, SPATH_SIZE, L"%s\\*.*", dir);
if ((hFind = FindFirstFile(spath, &fdFile)) == INVALID_HANDLE_VALUE) {
// ERROR: Path not found
return false;
}
do {
// FindFirstFile always returns '.' and '..'
size_t fdFileSize = wcslen(fdFile.cFileName);
if (wcsncmp(fdFile.cFileName, L".", fdFileSize) != 0 &&
wcsncmp(fdFile.cFileName, L"..", fdFileSize) != 0)
{
// Build up the file path using the passed-in info
n = strlen(dir);
if (dir[n-1] == 92)
swprintf(spath, SPATH_SIZE, L"%s%ls", dir, fdFile.cFileName);
else
swprintf(spath, SPATH_SIZE, L"%s\\%ls", dir, fdFile.cFileName);
// Is the entry a file, or a folder
if (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// We've got a directory
if (*ndir >= nbuffers) continue;
size_t lstr = MIN(wcslen(spath), bufferSize);
dirLengths[*ndir] = (int)wcstombs(dirnames[*ndir], spath, lstr);
*ndir += 1;
}
else {
// We've got a file
if (*nnames >= nbuffers) continue;
size_t lstr = MIN(wcslen(spath), bufferSize);
nameLengths[*nnames] =
(int)wcstombs(fnames[*nnames], spath, lstr);
*nnames += 1;
}
}
} while (FindNextFile(hFind, &fdFile));
// Clean up after ourselves
FindClose(hFind);
// End
return true;
}
// End Windows Version
/* ************************************************************************** */