| | @@ -61,71 +61,70 @@ |
| 61 | 61 | #include <windows.h> |
| 62 | 62 | #include <string.h> |
| 63 | 63 | #include <assert.h> |
| 64 | 64 | |
| 65 | 65 | |
| 66 | | -typedef struct dirent |
| 67 | | -{ |
| 68 | | - char d_name[MAX_PATH + 1]; /* current dir entry (multi-byte char string) */ |
| 69 | | - WIN32_FIND_DATAA data; /* file attributes */ |
| 70 | | -} dirent; |
| 71 | | - |
| 72 | | - |
| 73 | | -typedef struct DIR |
| 74 | | -{ |
| 75 | | - dirent current; /* Current directory entry */ |
| 66 | +typedef struct _wdirent |
| 67 | +{ |
| 68 | + wchar_t d_name[MAX_PATH + 1]; /* current dir entry (unicode char string) */ |
| 69 | + WIN32_FIND_DATAW data; /* file attributes */ |
| 70 | +} _wdirent; |
| 71 | + |
| 72 | + |
| 73 | +typedef struct _WDIR |
| 74 | +{ |
| 75 | + _wdirent current; /* Current directory entry */ |
| 76 | 76 | int cached; /* Indicates un-processed entry in memory */ |
| 77 | 77 | HANDLE search_handle; /* File search handle */ |
| 78 | | - char patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */ |
| 79 | | -} DIR; |
| 78 | + wchar_t patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */ |
| 79 | +} _WDIR; |
| 80 | 80 | |
| 81 | 81 | |
| 82 | 82 | /* Forward declarations */ |
| 83 | | -static DIR *opendir (const char *dirname); |
| 84 | | -static struct dirent *readdir (DIR *dirp); |
| 85 | | -static int closedir (DIR *dirp); |
| 86 | | -static void rewinddir(DIR* dirp); |
| 83 | +static _WDIR *_wopendir (const wchar_t *dirname); |
| 84 | +static struct _wdirent *_wreaddir (_WDIR *dirp); |
| 85 | +static int _wclosedir (_WDIR *dirp); |
| 87 | 86 | |
| 88 | 87 | |
| 89 | 88 | /* Use the new safe string functions introduced in Visual Studio 2005 */ |
| 90 | 89 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 91 | | -# define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE) |
| 90 | +# define STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE) |
| 92 | 91 | #else |
| 93 | | -# define STRNCPY(dest,src,size) strncpy((dest),(src),(size)) |
| 92 | +# define STRNCPY(dest,src,size) wcsncpy((dest),(src),(size)) |
| 94 | 93 | #endif |
| 95 | 94 | |
| 96 | 95 | |
| 97 | 96 | /***************************************************************************** |
| 98 | 97 | * Open directory stream DIRNAME for read and return a pointer to the |
| 99 | 98 | * internal working area that is used to retrieve individual directory |
| 100 | 99 | * entries. |
| 101 | 100 | */ |
| 102 | | -static DIR *opendir(const char *dirname) |
| 101 | +static _WDIR *_wopendir(const wchar_t *dirname) |
| 103 | 102 | { |
| 104 | | - DIR *dirp; |
| 103 | + _WDIR *dirp; |
| 105 | 104 | assert (dirname != NULL); |
| 106 | | - assert (strlen (dirname) < MAX_PATH); |
| 105 | + assert (wcslen (dirname) < MAX_PATH); |
| 107 | 106 | |
| 108 | | - /* construct new DIR structure */ |
| 109 | | - dirp = (DIR*) malloc (sizeof (struct DIR)); |
| 107 | + /* construct new _WDIR structure */ |
| 108 | + dirp = (_WDIR*) malloc (sizeof (struct _WDIR)); |
| 110 | 109 | if (dirp != NULL) { |
| 111 | | - char *p; |
| 110 | + wchar_t *p; |
| 112 | 111 | |
| 113 | 112 | /* take directory name... */ |
| 114 | 113 | STRNCPY (dirp->patt, dirname, sizeof(dirp->patt)); |
| 115 | 114 | dirp->patt[MAX_PATH] = '\0'; |
| 116 | 115 | |
| 117 | 116 | /* ... and append search pattern to it */ |
| 118 | | - p = strchr (dirp->patt, '\0'); |
| 117 | + p = wcschr (dirp->patt, '\0'); |
| 119 | 118 | if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') { |
| 120 | 119 | *p++ = '\\'; |
| 121 | 120 | } |
| 122 | 121 | *p++ = '*'; |
| 123 | 122 | *p = '\0'; |
| 124 | 123 | |
| 125 | 124 | /* open stream and retrieve first file */ |
| 126 | | - dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data); |
| 125 | + dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->current.data); |
| 127 | 126 | if (dirp->search_handle == INVALID_HANDLE_VALUE) { |
| 128 | 127 | /* invalid search pattern? */ |
| 129 | 128 | free (dirp); |
| 130 | 129 | return NULL; |
| 131 | 130 | } |
| | @@ -143,11 +142,11 @@ |
| 143 | 142 | * containing the name of the entry in d_name field. Individual directory |
| 144 | 143 | * entries returned by this very function include regular files, |
| 145 | 144 | * sub-directories, pseudo-directories "." and "..", but also volume labels, |
| 146 | 145 | * hidden files and system files may be returned. |
| 147 | 146 | */ |
| 148 | | -static struct dirent *readdir(DIR *dirp) |
| 147 | +static struct _wdirent *_wreaddir(_WDIR *dirp) |
| 149 | 148 | { |
| 150 | 149 | assert (dirp != NULL); |
| 151 | 150 | |
| 152 | 151 | if (dirp->search_handle == INVALID_HANDLE_VALUE) { |
| 153 | 152 | /* directory stream was opened/rewound incorrectly or ended normally */ |
| | @@ -158,11 +157,11 @@ |
| 158 | 157 | if (dirp->cached != 0) { |
| 159 | 158 | /* a valid directory entry already in memory */ |
| 160 | 159 | dirp->cached = 0; |
| 161 | 160 | } else { |
| 162 | 161 | /* read next directory entry from disk */ |
| 163 | | - if (FindNextFileA (dirp->search_handle, &dirp->current.data) == FALSE) { |
| 162 | + if (FindNextFileW (dirp->search_handle, &dirp->current.data) == FALSE) { |
| 164 | 163 | /* the very last file has been processed or an error occured */ |
| 165 | 164 | FindClose (dirp->search_handle); |
| 166 | 165 | dirp->search_handle = INVALID_HANDLE_VALUE; |
| 167 | 166 | return NULL; |
| 168 | 167 | } |
| | @@ -181,11 +180,11 @@ |
| 181 | 180 | /***************************************************************************** |
| 182 | 181 | * Close directory stream opened by opendir() function. Close of the |
| 183 | 182 | * directory stream invalidates the DIR structure as well as any previously |
| 184 | 183 | * read directory entry. |
| 185 | 184 | */ |
| 186 | | -static int closedir(DIR *dirp) |
| 185 | +static int _wclosedir(_WDIR *dirp) |
| 187 | 186 | { |
| 188 | 187 | assert (dirp != NULL); |
| 189 | 188 | |
| 190 | 189 | /* release search handle */ |
| 191 | 190 | if (dirp->search_handle != INVALID_HANDLE_VALUE) { |
| | @@ -195,36 +194,7 @@ |
| 195 | 194 | |
| 196 | 195 | /* release directory handle */ |
| 197 | 196 | free (dirp); |
| 198 | 197 | return 0; |
| 199 | 198 | } |
| 200 | | - |
| 201 | | - |
| 202 | | -/***************************************************************************** |
| 203 | | - * Resets the position of the directory stream to which dirp refers to the |
| 204 | | - * beginning of the directory. It also causes the directory stream to refer |
| 205 | | - * to the current state of the corresponding directory, as a call to opendir() |
| 206 | | - * would have done. If dirp does not refer to a directory stream, the effect |
| 207 | | - * is undefined. |
| 208 | | - */ |
| 209 | | -static void rewinddir(DIR* dirp) |
| 210 | | -{ |
| 211 | | - /* release search handle */ |
| 212 | | - if (dirp->search_handle != INVALID_HANDLE_VALUE) { |
| 213 | | - FindClose (dirp->search_handle); |
| 214 | | - dirp->search_handle = INVALID_HANDLE_VALUE; |
| 215 | | - } |
| 216 | | - |
| 217 | | - /* open new search handle and retrieve first file */ |
| 218 | | - dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data); |
| 219 | | - if (dirp->search_handle == INVALID_HANDLE_VALUE) { |
| 220 | | - /* invalid search pattern? */ |
| 221 | | - free (dirp); |
| 222 | | - return; |
| 223 | | - } |
| 224 | | - |
| 225 | | - /* there is an un-processed directory entry in memory now */ |
| 226 | | - dirp->cached = 1; |
| 227 | | -} |
| 228 | | - |
| 229 | 199 | |
| 230 | 200 | #endif /*DIRENT_H*/ |
| 231 | 201 | |