|
6 | 6 | /* application includes */
|
7 | 7 | #include "filecalls.h"
|
8 | 8 |
|
9 |
| -static bool File_GetInfo(const char *sPath, FileInfo *phFileInfo); |
10 |
| - |
11 |
| -/** |
12 |
| - * Directory_Open |
13 |
| - * |
14 |
| - * Open a directory so that it's contents can be scanned |
15 |
| - * |
16 |
| - * @param sPath Location of directory to scan |
17 |
| - * @param hDir Pointer to a Directory struct to fill in |
18 |
| - * @returns true on success false on failure |
19 |
| - */ |
20 |
| -bool Directory_Open(const char *sPath, Directory *hDirectory) |
21 |
| -{ |
22 |
| - assert(sPath); |
23 |
| - assert(*sPath); |
24 |
| - assert(hDirectory); |
25 |
| - |
26 |
| - hDirectory->sPathLen = strlen(sPath); |
27 |
| - hDirectory->sPath = malloc(hDirectory->sPathLen + 1); |
28 |
| - |
29 |
| - if(NULL == hDirectory->sPath) { |
30 |
| - return false; |
31 |
| - } else { |
32 |
| - strcpy(hDirectory->sPath, sPath); |
33 |
| - } |
34 |
| - |
35 |
| - hDirectory->hDir = opendir(sPath); |
36 |
| - |
37 |
| - if(NULL == hDirectory->hDir) { |
38 |
| - return false; |
39 |
| - } else { |
40 |
| - return true; |
41 |
| - } |
42 |
| -} |
43 |
| - |
44 |
| -/** |
45 |
| - * Directory_Close |
46 |
| - * |
47 |
| - * Close a previously opened Directory |
48 |
| - * |
49 |
| - * @param hDirectory Directory to close |
50 |
| - */ |
51 |
| -void Directory_Close(Directory hDirectory) |
52 |
| -{ |
53 |
| - closedir(hDirectory.hDir); |
54 |
| - free(hDirectory.sPath); |
55 |
| -} |
56 |
| - |
57 |
| -/** |
58 |
| - * Directory_GetNextEntry |
59 |
| - * |
60 |
| - * Get the next entry in a directory |
61 |
| - * |
62 |
| - * @param hDirectory pointer to Directory to get entry from |
63 |
| - * @returns String of filename or NULL on EndOfDirectory |
64 |
| - */ |
65 |
| -char *Directory_GetNextEntry(Directory *hDirectory, FileInfo *phFileInfo) |
66 |
| -{ |
67 |
| - struct dirent *phDirEntry; |
68 |
| - |
69 |
| - assert(hDirectory); |
70 |
| - |
71 |
| - phDirEntry = readdir(hDirectory->hDir); |
72 |
| - if(!phDirEntry) { |
73 |
| - return NULL; |
74 |
| - } |
75 |
| - |
76 |
| - if (phFileInfo) { |
77 |
| - phFileInfo->bIsRegularFile = false; |
78 |
| - phFileInfo->bIsDirectory = false; |
79 |
| - |
80 |
| -#ifdef _DIRENT_HAVE_D_TYPE |
81 |
| - if (phDirEntry->d_type == DT_REG || phDirEntry->d_type == DT_DIR) { |
82 |
| - phFileInfo->bIsRegularFile = (phDirEntry->d_type == DT_REG); |
83 |
| - phFileInfo->bIsDirectory = (phDirEntry->d_type == DT_DIR); |
84 |
| - } else |
85 |
| -#endif |
86 |
| - { |
87 |
| - char *path = Directory_GetFullPath(hDirectory, phDirEntry->d_name); |
88 |
| - if (path) { |
89 |
| - File_GetInfo(path, phFileInfo); |
90 |
| - free(path); |
91 |
| - } |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - return phDirEntry->d_name; |
96 |
| -} |
97 |
| - |
98 |
| -/** |
99 |
| - * Get the full path of a file in a directory |
100 |
| - * |
101 |
| - * @param hDirectory pointer to Directory to get the base path from |
102 |
| - * @returns String of the full path or `NULL` on EndOfDirectory |
103 |
| - */ |
104 |
| -char *Directory_GetFullPath(Directory *hDirectory, const char *leaf) { |
105 |
| - size_t len = hDirectory->sPathLen + strlen(leaf) + 1; |
106 |
| - char *path = malloc(len + 1); |
107 |
| - if (!path) { |
108 |
| - return NULL; |
109 |
| - } |
110 |
| - |
111 |
| - strcpy(path, hDirectory->sPath); |
112 |
| - strcat(path, "/"); |
113 |
| - strcat(path, leaf); |
114 |
| - return path; |
115 |
| -} |
116 |
| - |
117 | 9 | /**
|
118 | 10 | * Open the specified file in the application data directory
|
119 | 11 | *
|
|
0 commit comments