| | @@ -20,12 +20,12 @@ |
| 20 | 20 | * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | 21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | 23 | * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | 24 | * |
| 25 | | - * Sept 22, 2012, Joe Mistachkin |
| 26 | | - * Check for DIRENT_UNICODE define in addition to UNICODE. |
| 25 | + * Sept 23, 2012, Joe Mistachkin |
| 26 | + * Modified to use wide-character APIs. |
| 27 | 27 | * |
| 28 | 28 | * Sept 12, 2012, Jan Nijtmans |
| 29 | 29 | * Switchable wide-character variant. |
| 30 | 30 | * |
| 31 | 31 | * Mar 15, 2011, Toni Ronkko |
| | @@ -87,11 +87,10 @@ |
| 87 | 87 | #include <string.h> |
| 88 | 88 | #include <stdlib.h> |
| 89 | 89 | #include <sys/types.h> |
| 90 | 90 | #include <sys/stat.h> |
| 91 | 91 | #include <errno.h> |
| 92 | | -#include <tchar.h> |
| 93 | 92 | |
| 94 | 93 | /* Entries missing from MSVC 6.0 */ |
| 95 | 94 | #if !defined(FILE_ATTRIBUTE_DEVICE) |
| 96 | 95 | # define FILE_ATTRIBUTE_DEVICE 0x40 |
| 97 | 96 | #endif |
| | @@ -151,53 +150,44 @@ |
| 151 | 150 | #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) |
| 152 | 151 | #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) |
| 153 | 152 | #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) |
| 154 | 153 | #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) |
| 155 | 154 | |
| 156 | | -#if defined(UNICODE) || defined(DIRENT_UNICODE) |
| 157 | | -# define dirent _wdirent |
| 158 | | -# define opendir _wopendir |
| 159 | | -# define readdir _wreaddir |
| 160 | | -# define closedir _wclosedir |
| 161 | | -# define rewinddir _wrewinddir |
| 162 | | -# define DIR _WDIR |
| 163 | | -#endif |
| 164 | | - |
| 165 | 155 | #ifdef __cplusplus |
| 166 | 156 | extern "C" { |
| 167 | 157 | #endif |
| 168 | 158 | |
| 169 | 159 | typedef struct dirent |
| 170 | 160 | { |
| 171 | | - TCHAR d_name[MAX_PATH + 1]; /* File name */ |
| 161 | + WCHAR d_name[MAX_PATH + 1]; /* File name */ |
| 172 | 162 | size_t d_namlen; /* Length of name without \0 */ |
| 173 | 163 | int d_type; /* File type */ |
| 174 | 164 | } dirent; |
| 175 | 165 | |
| 176 | 166 | |
| 177 | 167 | typedef struct DIR |
| 178 | 168 | { |
| 179 | 169 | dirent curentry; /* Current directory entry */ |
| 180 | | - WIN32_FIND_DATA find_data; /* Private file data */ |
| 170 | + WIN32_FIND_DATAW find_data; /* Private file data */ |
| 181 | 171 | int cached; /* True if data is valid */ |
| 182 | 172 | HANDLE search_handle; /* Win32 search handle */ |
| 183 | | - TCHAR patt[MAX_PATH + 3]; /* Initial directory name */ |
| 173 | + WCHAR patt[MAX_PATH + 3]; /* Initial directory name */ |
| 184 | 174 | } DIR; |
| 185 | 175 | |
| 186 | 176 | |
| 187 | 177 | /* Forward declarations */ |
| 188 | | -static DIR *opendir(const TCHAR *dirname); |
| 178 | +static DIR *opendir(const WCHAR *dirname); |
| 189 | 179 | static struct dirent *readdir(DIR *dirp); |
| 190 | 180 | static int closedir(DIR *dirp); |
| 191 | 181 | static void rewinddir(DIR* dirp); |
| 192 | 182 | |
| 193 | 183 | |
| 194 | 184 | /* Use the new safe string functions introduced in Visual Studio 2005 */ |
| 195 | 185 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 196 | | -# define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE) |
| 186 | +# define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE) |
| 197 | 187 | #else |
| 198 | | -# define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size)) |
| 188 | +# define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size)) |
| 199 | 189 | #endif |
| 200 | 190 | |
| 201 | 191 | /* Set errno variable */ |
| 202 | 192 | #if defined(_MSC_VER) |
| 203 | 193 | #define DIRENT_SET_ERRNO(x) _set_errno (x) |
| | @@ -209,20 +199,20 @@ |
| 209 | 199 | /***************************************************************************** |
| 210 | 200 | * Open directory stream DIRNAME for read and return a pointer to the |
| 211 | 201 | * internal working area that is used to retrieve individual directory |
| 212 | 202 | * entries. |
| 213 | 203 | */ |
| 214 | | -static DIR *opendir(const TCHAR *dirname) |
| 204 | +static DIR *opendir(const WCHAR *dirname) |
| 215 | 205 | { |
| 216 | 206 | DIR *dirp; |
| 217 | 207 | |
| 218 | 208 | /* ensure that the resulting search pattern will be a valid file name */ |
| 219 | 209 | if (dirname == NULL) { |
| 220 | 210 | DIRENT_SET_ERRNO (ENOENT); |
| 221 | 211 | return NULL; |
| 222 | 212 | } |
| 223 | | - if (_tcslen (dirname) + 3 >= MAX_PATH) { |
| 213 | + if (wcslen (dirname) + 3 >= MAX_PATH) { |
| 224 | 214 | DIRENT_SET_ERRNO (ENAMETOOLONG); |
| 225 | 215 | return NULL; |
| 226 | 216 | } |
| 227 | 217 | |
| 228 | 218 | /* construct new DIR structure */ |
| | @@ -233,23 +223,23 @@ |
| 233 | 223 | /* |
| 234 | 224 | * Convert relative directory name to an absolute one. This |
| 235 | 225 | * allows rewinddir() to function correctly when the current working |
| 236 | 226 | * directory is changed between opendir() and rewinddir(). |
| 237 | 227 | */ |
| 238 | | - if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) { |
| 239 | | - TCHAR *p; |
| 228 | + if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) { |
| 229 | + WCHAR *p; |
| 240 | 230 | |
| 241 | 231 | /* append the search pattern "\\*\0" to the directory name */ |
| 242 | | - p = _tcschr (dirp->patt, '\0'); |
| 232 | + p = wcschr (dirp->patt, '\0'); |
| 243 | 233 | if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') { |
| 244 | 234 | *p++ = '\\'; |
| 245 | 235 | } |
| 246 | 236 | *p++ = '*'; |
| 247 | 237 | *p = '\0'; |
| 248 | 238 | |
| 249 | 239 | /* open directory stream and retrieve the first entry */ |
| 250 | | - dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data); |
| 240 | + dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data); |
| 251 | 241 | if (dirp->search_handle != INVALID_HANDLE_VALUE) { |
| 252 | 242 | /* a directory entry is now waiting in memory */ |
| 253 | 243 | dirp->cached = 1; |
| 254 | 244 | error = 0; |
| 255 | 245 | } else { |
| | @@ -296,11 +286,11 @@ |
| 296 | 286 | } else { |
| 297 | 287 | /* get the next directory entry from stream */ |
| 298 | 288 | if (dirp->search_handle == INVALID_HANDLE_VALUE) { |
| 299 | 289 | return NULL; |
| 300 | 290 | } |
| 301 | | - if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) { |
| 291 | + if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) { |
| 302 | 292 | /* the very last entry has been processed or an error occured */ |
| 303 | 293 | FindClose (dirp->search_handle); |
| 304 | 294 | dirp->search_handle = INVALID_HANDLE_VALUE; |
| 305 | 295 | return NULL; |
| 306 | 296 | } |
| | @@ -311,11 +301,11 @@ |
| 311 | 301 | dirp->find_data.cFileName, |
| 312 | 302 | sizeof(dirp->curentry.d_name)/sizeof(TCHAR) ); |
| 313 | 303 | dirp->curentry.d_name[MAX_PATH] = '\0'; |
| 314 | 304 | |
| 315 | 305 | /* compute the length of name */ |
| 316 | | - dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name); |
| 306 | + dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name); |
| 317 | 307 | |
| 318 | 308 | /* determine file type */ |
| 319 | 309 | attr = dirp->find_data.dwFileAttributes; |
| 320 | 310 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { |
| 321 | 311 | dirp->curentry.d_type = DT_CHR; |
| | @@ -366,11 +356,11 @@ |
| 366 | 356 | if (dirp->search_handle != INVALID_HANDLE_VALUE) { |
| 367 | 357 | FindClose (dirp->search_handle); |
| 368 | 358 | } |
| 369 | 359 | |
| 370 | 360 | /* open new search handle and retrieve the first entry */ |
| 371 | | - dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data); |
| 361 | + dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data); |
| 372 | 362 | if (dirp->search_handle != INVALID_HANDLE_VALUE) { |
| 373 | 363 | /* a directory entry is now waiting in memory */ |
| 374 | 364 | dirp->cached = 1; |
| 375 | 365 | } else { |
| 376 | 366 | /* failed to re-open directory: no directory entry in memory */ |
| | @@ -377,17 +367,9 @@ |
| 377 | 367 | dirp->cached = 0; |
| 378 | 368 | } |
| 379 | 369 | } |
| 380 | 370 | } |
| 381 | 371 | |
| 382 | | -#ifdef UNICODE |
| 383 | | -# undef dirent |
| 384 | | -# undef opendir |
| 385 | | -# undef readdir |
| 386 | | -# undef closedir |
| 387 | | -# undef DIR |
| 388 | | -#endif |
| 389 | | - |
| 390 | 372 | #ifdef __cplusplus |
| 391 | 373 | } |
| 392 | 374 | #endif |
| 393 | 375 | #endif /*DIRENT_H*/ |
| 394 | 376 | |