CASToR  2.0
Tomographic Reconstruction (PET/SPECT/CT)
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oDirentWin32.hh
Go to the documentation of this file.
1 /*
2 This file is part of CASToR.
3 
4  CASToR is free software: you can redistribute it and/or modify it under the
5  terms of the GNU General Public License as published by the Free Software
6  Foundation, either version 3 of the License, or (at your option) any later
7  version.
8 
9  CASToR is distributed in the hope that it will be useful, but WITHOUT ANY
10  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  details.
13 
14  You should have received a copy of the GNU General Public License along with
15  CASToR (in file GNU_GPL.TXT). If not, see <http://www.gnu.org/licenses/>.
16 
17 Copyright 2017-2018 all CASToR contributors listed below:
18 
19  --> current contributors: Thibaut MERLIN, Simon STUTE, Didier BENOIT, Claude COMTAT, Marina FILIPOVIC, Mael MILLARDET
20  --> past contributors: Valentin VIELZEUF
21 
22 This is CASToR version 2.0.
23 */
24 
25 /*
26  * Dirent interface for Microsoft Visual Studio
27  * Version 1.21
28  *
29  * Copyright (C) 2006-2012 Toni Ronkko
30  * This file is part of dirent. Dirent may be freely distributed
31  * under the MIT license. For all details and documentation, see
32  * https://github.com/tronkko/dirent
33  */
34 
35 #ifndef DIRENT_H
36 #define DIRENT_H
37 
38 /*
39  * Define architecture flags so we don't need to include windows.h.
40  * Avoiding windows.h makes it simpler to use windows sockets in conjunction
41  * with dirent.h.
42  */
43 #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_IX86)
44 # define _X86_
45 #endif
46 #if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_AMD64)
47 #define _AMD64_
48 #endif
49 
50 #include <stdio.h>
51 #include <stdarg.h>
52 #include <windef.h>
53 #include <winbase.h>
54 #include <wchar.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <malloc.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <errno.h>
61 
62 /* Indicates that d_type field is available in dirent structure */
63 #define _DIRENT_HAVE_D_TYPE
64 
65 /* Indicates that d_namlen field is available in dirent structure */
66 #define _DIRENT_HAVE_D_NAMLEN
67 
68 /* Entries missing from MSVC 6.0 */
69 #if !defined(FILE_ATTRIBUTE_DEVICE)
70 # define FILE_ATTRIBUTE_DEVICE 0x40
71 #endif
72 
73 /* File type and permission flags for stat(), general mask */
74 #if !defined(S_IFMT)
75 # define S_IFMT _S_IFMT
76 #endif
77 
78 /* Directory bit */
79 #if !defined(S_IFDIR)
80 # define S_IFDIR _S_IFDIR
81 #endif
82 
83 /* Character device bit */
84 #if !defined(S_IFCHR)
85 # define S_IFCHR _S_IFCHR
86 #endif
87 
88 /* Pipe bit */
89 #if !defined(S_IFFIFO)
90 # define S_IFFIFO _S_IFFIFO
91 #endif
92 
93 /* Regular file bit */
94 #if !defined(S_IFREG)
95 # define S_IFREG _S_IFREG
96 #endif
97 
98 /* Read permission */
99 #if !defined(S_IREAD)
100 # define S_IREAD _S_IREAD
101 #endif
102 
103 /* Write permission */
104 #if !defined(S_IWRITE)
105 # define S_IWRITE _S_IWRITE
106 #endif
107 
108 /* Execute permission */
109 #if !defined(S_IEXEC)
110 # define S_IEXEC _S_IEXEC
111 #endif
112 
113 /* Pipe */
114 #if !defined(S_IFIFO)
115 # define S_IFIFO _S_IFIFO
116 #endif
117 
118 /* Block device */
119 #if !defined(S_IFBLK)
120 # define S_IFBLK 0
121 #endif
122 
123 /* Link */
124 #if !defined(S_IFLNK)
125 # define S_IFLNK 0
126 #endif
127 
128 /* Socket */
129 #if !defined(S_IFSOCK)
130 # define S_IFSOCK 0
131 #endif
132 
133 /* Read user permission */
134 #if !defined(S_IRUSR)
135 # define S_IRUSR S_IREAD
136 #endif
137 
138 /* Write user permission */
139 #if !defined(S_IWUSR)
140 # define S_IWUSR S_IWRITE
141 #endif
142 
143 /* Execute user permission */
144 #if !defined(S_IXUSR)
145 # define S_IXUSR 0
146 #endif
147 
148 /* Read group permission */
149 #if !defined(S_IRGRP)
150 # define S_IRGRP 0
151 #endif
152 
153 /* Write group permission */
154 #if !defined(S_IWGRP)
155 # define S_IWGRP 0
156 #endif
157 
158 /* Execute group permission */
159 #if !defined(S_IXGRP)
160 # define S_IXGRP 0
161 #endif
162 
163 /* Read others permission */
164 #if !defined(S_IROTH)
165 # define S_IROTH 0
166 #endif
167 
168 /* Write others permission */
169 #if !defined(S_IWOTH)
170 # define S_IWOTH 0
171 #endif
172 
173 /* Execute others permission */
174 #if !defined(S_IXOTH)
175 # define S_IXOTH 0
176 #endif
177 
178 /* Maximum length of file name */
179 #if !defined(PATH_MAX)
180 # define PATH_MAX MAX_PATH
181 #endif
182 #if !defined(FILENAME_MAX)
183 # define FILENAME_MAX MAX_PATH
184 #endif
185 #if !defined(NAME_MAX)
186 # define NAME_MAX FILENAME_MAX
187 #endif
188 
189 /* File type flags for d_type */
190 #define DT_UNKNOWN 0
191 #define DT_REG S_IFREG
192 #define DT_DIR S_IFDIR
193 #define DT_FIFO S_IFIFO
194 #define DT_SOCK S_IFSOCK
195 #define DT_CHR S_IFCHR
196 #define DT_BLK S_IFBLK
197 #define DT_LNK S_IFLNK
198 
199 /* Macros for converting between st_mode and d_type */
200 #define IFTODT(mode) ((mode) & S_IFMT)
201 #define DTTOIF(type) (type)
202 
203 /*
204  * File type macros. Note that block devices, sockets and links cannot be
205  * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
206  * only defined for compatibility. These macros should always return false
207  * on Windows.
208  */
209 #if !defined(S_ISFIFO)
210 # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
211 #endif
212 #if !defined(S_ISDIR)
213 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
214 #endif
215 #if !defined(S_ISREG)
216 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
217 #endif
218 #if !defined(S_ISLNK)
219 # define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
220 #endif
221 #if !defined(S_ISSOCK)
222 # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
223 #endif
224 #if !defined(S_ISCHR)
225 # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
226 #endif
227 #if !defined(S_ISBLK)
228 # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
229 #endif
230 
231 /* Return the exact length of d_namlen without zero terminator */
232 #define _D_EXACT_NAMLEN(p) ((p)->d_namlen)
233 
234 /* Return number of bytes needed to store d_namlen */
235 #define _D_ALLOC_NAMLEN(p) (PATH_MAX)
236 
237 
238 #ifdef __cplusplus
239 extern "C" {
240 #endif
241 
242 
243 /* Wide-character version */
244 struct _wdirent {
245  /* Always zero */
246  long d_ino;
247 
248  /* Structure size */
249  unsigned short d_reclen;
250 
251  /* Length of name without \0 */
252  size_t d_namlen;
253 
254  /* File type */
255  int d_type;
256 
257  /* File name */
258  wchar_t d_name[PATH_MAX];
259 };
260 typedef struct _wdirent _wdirent;
261 
262 struct _WDIR {
263  /* Current directory entry */
264  struct _wdirent ent;
265 
266  /* Private file data */
267  WIN32_FIND_DATAW data;
268 
269  /* True if data is valid */
270  int cached;
271 
272  /* Win32 search handle */
273  HANDLE handle;
274 
275  /* Initial directory name */
276  wchar_t *patt;
277 };
278 typedef struct _WDIR _WDIR;
279 
280 static _WDIR *_wopendir (const wchar_t *dirname);
281 static struct _wdirent *_wreaddir (_WDIR *dirp);
282 static int _wclosedir (_WDIR *dirp);
283 static void _wrewinddir (_WDIR* dirp);
284 
285 
286 /* For compatibility with Symbian */
287 #define wdirent _wdirent
288 #define WDIR _WDIR
289 #define wopendir _wopendir
290 #define wreaddir _wreaddir
291 #define wclosedir _wclosedir
292 #define wrewinddir _wrewinddir
293 
294 
295 /* Multi-byte character versions */
296 struct dirent {
297  /* Always zero */
298  long d_ino;
299 
300  /* Structure size */
301  unsigned short d_reclen;
302 
303  /* Length of name without \0 */
304  size_t d_namlen;
305 
306  /* File type */
307  int d_type;
308 
309  /* File name */
311 };
312 typedef struct dirent dirent;
313 
314 struct DIR {
315  struct dirent ent;
316  struct _WDIR *wdirp;
317 };
318 typedef struct DIR DIR;
319 
320 static DIR *opendir (const char *dirname);
321 static struct dirent *readdir (DIR *dirp);
322 static int closedir (DIR *dirp);
323 static void rewinddir (DIR* dirp);
324 
325 
326 /* Internal utility functions */
327 static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp);
328 static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp);
329 
330 static int dirent_mbstowcs_s(
331  size_t *pReturnValue,
332  wchar_t *wcstr,
333  size_t sizeInWords,
334  const char *mbstr,
335  size_t count);
336 
337 static int dirent_wcstombs_s(
338  size_t *pReturnValue,
339  char *mbstr,
340  size_t sizeInBytes,
341  const wchar_t *wcstr,
342  size_t count);
343 
344 static void dirent_set_errno (int error);
345 
346 /*
347  * Open directory stream DIRNAME for read and return a pointer to the
348  * internal working area that is used to retrieve individual directory
349  * entries.
350  */
351 static _WDIR*
353  const wchar_t *dirname)
354 {
355  _WDIR *dirp = NULL;
356  int error;
357 
358  /* Must have directory name */
359  if (dirname == NULL || dirname[0] == '\0') {
360  dirent_set_errno (ENOENT);
361  return NULL;
362  }
363 
364  /* Allocate new _WDIR structure */
365  dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
366  if (dirp != NULL) {
367  DWORD n;
368 
369  /* Reset _WDIR structure */
370  dirp->handle = INVALID_HANDLE_VALUE;
371  dirp->patt = NULL;
372  dirp->cached = 0;
373 
374  /* Compute the length of full path plus zero terminator */
375  n = GetFullPathNameW (dirname, 0, NULL, NULL);
376 
377  /* Allocate room for absolute directory name and search pattern */
378  dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16);
379  if (dirp->patt) {
380 
381  /*
382  * Convert relative directory name to an absolute one. This
383  * allows rewinddir() to function correctly even when current
384  * working directory is changed between opendir() and rewinddir().
385  */
386  n = GetFullPathNameW (dirname, n, dirp->patt, NULL);
387  if (n > 0) {
388  wchar_t *p;
389 
390  /* Append search pattern \* to the directory name */
391  p = dirp->patt + n;
392  if (dirp->patt < p) {
393  switch (p[-1]) {
394  case '\\':
395  case '/':
396  case ':':
397  /* Directory ends in path separator, e.g. c:\temp\ */
398  /*NOP*/;
399  break;
400 
401  default:
402  /* Directory name doesn't end in path separator */
403  *p++ = '\\';
404  }
405  }
406  *p++ = '*';
407  *p = '\0';
408 
409  /* Open directory stream and retrieve the first entry */
410  if (dirent_first (dirp)) {
411  /* Directory stream opened successfully */
412  error = 0;
413  } else {
414  /* Cannot retrieve first entry */
415  error = 1;
416  dirent_set_errno (ENOENT);
417  }
418 
419  } else {
420  /* Cannot retrieve full path name */
421  dirent_set_errno (ENOENT);
422  error = 1;
423  }
424 
425  } else {
426  /* Cannot allocate memory for search pattern */
427  error = 1;
428  }
429 
430  } else {
431  /* Cannot allocate _WDIR structure */
432  error = 1;
433  }
434 
435  /* Clean up in case of error */
436  if (error && dirp) {
437  _wclosedir (dirp);
438  dirp = NULL;
439  }
440 
441  return dirp;
442 }
443 
444 /*
445  * Read next directory entry. The directory entry is returned in dirent
446  * structure in the d_name field. Individual directory entries returned by
447  * this function include regular files, sub-directories, pseudo-directories
448  * "." and ".." as well as volume labels, hidden files and system files.
449  */
450 static struct _wdirent*
452  _WDIR *dirp)
453 {
454  WIN32_FIND_DATAW *datap;
455  struct _wdirent *entp;
456 
457  /* Read next directory entry */
458  datap = dirent_next (dirp);
459  if (datap) {
460  size_t n;
461  DWORD attr;
462 
463  /* Pointer to directory entry to return */
464  entp = &dirp->ent;
465 
466  /*
467  * Copy file name as wide-character string. If the file name is too
468  * long to fit in to the destination buffer, then truncate file name
469  * to PATH_MAX characters and zero-terminate the buffer.
470  */
471  n = 0;
472  while (n + 1 < PATH_MAX && datap->cFileName[n] != 0) {
473  entp->d_name[n] = datap->cFileName[n];
474  n++;
475  }
476  dirp->ent.d_name[n] = 0;
477 
478  /* Length of file name excluding zero terminator */
479  entp->d_namlen = n;
480 
481  /* File type */
482  attr = datap->dwFileAttributes;
483  if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
484  entp->d_type = DT_CHR;
485  } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
486  entp->d_type = DT_DIR;
487  } else {
488  entp->d_type = DT_REG;
489  }
490 
491  /* Reset dummy fields */
492  entp->d_ino = 0;
493  entp->d_reclen = sizeof (struct _wdirent);
494 
495  } else {
496 
497  /* Last directory entry read */
498  entp = NULL;
499 
500  }
501 
502  return entp;
503 }
504 
505 /*
506  * Close directory stream opened by opendir() function. This invalidates the
507  * DIR structure as well as any directory entry read previously by
508  * _wreaddir().
509  */
510 static int
512  _WDIR *dirp)
513 {
514  int ok;
515  if (dirp) {
516 
517  /* Release search handle */
518  if (dirp->handle != INVALID_HANDLE_VALUE) {
519  FindClose (dirp->handle);
520  dirp->handle = INVALID_HANDLE_VALUE;
521  }
522 
523  /* Release search pattern */
524  if (dirp->patt) {
525  free (dirp->patt);
526  dirp->patt = NULL;
527  }
528 
529  /* Release directory structure */
530  free (dirp);
531  ok = /*success*/0;
532 
533  } else {
534  /* Invalid directory stream */
535  dirent_set_errno (EBADF);
536  ok = /*failure*/-1;
537  }
538  return ok;
539 }
540 
541 /*
542  * Rewind directory stream such that _wreaddir() returns the very first
543  * file name again.
544  */
545 static void
547  _WDIR* dirp)
548 {
549  if (dirp) {
550  /* Release existing search handle */
551  if (dirp->handle != INVALID_HANDLE_VALUE) {
552  FindClose (dirp->handle);
553  }
554 
555  /* Open new search handle */
556  dirent_first (dirp);
557  }
558 }
559 
560 /* Get first directory entry (internal) */
561 static WIN32_FIND_DATAW*
563  _WDIR *dirp)
564 {
565  WIN32_FIND_DATAW *datap;
566 
567  /* Open directory and retrieve the first entry */
568  dirp->handle = FindFirstFileW (dirp->patt, &dirp->data);
569  if (dirp->handle != INVALID_HANDLE_VALUE) {
570 
571  /* a directory entry is now waiting in memory */
572  datap = &dirp->data;
573  dirp->cached = 1;
574 
575  } else {
576 
577  /* Failed to re-open directory: no directory entry in memory */
578  dirp->cached = 0;
579  datap = NULL;
580 
581  }
582  return datap;
583 }
584 
585 /* Get next directory entry (internal) */
586 static WIN32_FIND_DATAW*
588  _WDIR *dirp)
589 {
590  WIN32_FIND_DATAW *p;
591 
592  /* Get next directory entry */
593  if (dirp->cached != 0) {
594 
595  /* A valid directory entry already in memory */
596  p = &dirp->data;
597  dirp->cached = 0;
598 
599  } else if (dirp->handle != INVALID_HANDLE_VALUE) {
600 
601  /* Get the next directory entry from stream */
602  if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) {
603  /* Got a file */
604  p = &dirp->data;
605  } else {
606  /* The very last entry has been processed or an error occured */
607  FindClose (dirp->handle);
608  dirp->handle = INVALID_HANDLE_VALUE;
609  p = NULL;
610  }
611 
612  } else {
613 
614  /* End of directory stream reached */
615  p = NULL;
616 
617  }
618 
619  return p;
620 }
621 
622 /*
623  * Open directory stream using plain old C-string.
624  */
625 static DIR*
627  const char *dirname)
628 {
629  struct DIR *dirp;
630  int error;
631 
632  /* Must have directory name */
633  if (dirname == NULL || dirname[0] == '\0') {
634  dirent_set_errno (ENOENT);
635  return NULL;
636  }
637 
638  /* Allocate memory for DIR structure */
639  dirp = (DIR*) malloc (sizeof (struct DIR));
640  if (dirp) {
641  wchar_t wname[PATH_MAX];
642  size_t n;
643 
644  /* Convert directory name to wide-character string */
645  error = dirent_mbstowcs_s (&n, wname, PATH_MAX, dirname, PATH_MAX);
646  if (!error) {
647 
648  /* Open directory stream using wide-character name */
649  dirp->wdirp = _wopendir (wname);
650  if (dirp->wdirp) {
651  /* Directory stream opened */
652  error = 0;
653  } else {
654  /* Failed to open directory stream */
655  error = 1;
656  }
657 
658  } else {
659  /*
660  * Cannot convert file name to wide-character string. This
661  * occurs if the string contains invalid multi-byte sequences or
662  * the output buffer is too small to contain the resulting
663  * string.
664  */
665  error = 1;
666  }
667 
668  } else {
669  /* Cannot allocate DIR structure */
670  error = 1;
671  }
672 
673  /* Clean up in case of error */
674  if (error && dirp) {
675  free (dirp);
676  dirp = NULL;
677  }
678 
679  return dirp;
680 }
681 
682 /*
683  * Read next directory entry.
684  *
685  * When working with text consoles, please note that file names returned by
686  * readdir() are represented in the default ANSI code page while any output to
687  * console is typically formatted on another code page. Thus, non-ASCII
688  * characters in file names will not usually display correctly on console. The
689  * problem can be fixed in two ways: (1) change the character set of console
690  * to 1252 using chcp utility and use Lucida Console font, or (2) use
691  * _cprintf function when writing to console. The _cprinf() will re-encode
692  * ANSI strings to the console code page so many non-ASCII characters will
693  * display correcly.
694  */
695 static struct dirent*
697  DIR *dirp)
698 {
699  WIN32_FIND_DATAW *datap;
700  struct dirent *entp;
701 
702  /* Read next directory entry */
703  datap = dirent_next (dirp->wdirp);
704  if (datap) {
705  size_t n;
706  int error;
707 
708  /* Attempt to convert file name to multi-byte string */
709  error = dirent_wcstombs_s(
710  &n, dirp->ent.d_name, PATH_MAX, datap->cFileName, PATH_MAX);
711 
712  /*
713  * If the file name cannot be represented by a multi-byte string,
714  * then attempt to use old 8+3 file name. This allows traditional
715  * Unix-code to access some file names despite of unicode
716  * characters, although file names may seem unfamiliar to the user.
717  *
718  * Be ware that the code below cannot come up with a short file
719  * name unless the file system provides one. At least
720  * VirtualBox shared folders fail to do this.
721  */
722  if (error && datap->cAlternateFileName[0] != '\0') {
723  error = dirent_wcstombs_s(
724  &n, dirp->ent.d_name, PATH_MAX,
725  datap->cAlternateFileName, PATH_MAX);
726  }
727 
728  if (!error) {
729  DWORD attr;
730 
731  /* Initialize directory entry for return */
732  entp = &dirp->ent;
733 
734  /* Length of file name excluding zero terminator */
735  entp->d_namlen = n - 1;
736 
737  /* File attributes */
738  attr = datap->dwFileAttributes;
739  if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
740  entp->d_type = DT_CHR;
741  } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
742  entp->d_type = DT_DIR;
743  } else {
744  entp->d_type = DT_REG;
745  }
746 
747  /* Reset dummy fields */
748  entp->d_ino = 0;
749  entp->d_reclen = sizeof (struct dirent);
750 
751  } else {
752  /*
753  * Cannot convert file name to multi-byte string so construct
754  * an errornous directory entry and return that. Note that
755  * we cannot return NULL as that would stop the processing
756  * of directory entries completely.
757  */
758  entp = &dirp->ent;
759  entp->d_name[0] = '?';
760  entp->d_name[1] = '\0';
761  entp->d_namlen = 1;
762  entp->d_type = DT_UNKNOWN;
763  entp->d_ino = 0;
764  entp->d_reclen = 0;
765  }
766 
767  } else {
768  /* No more directory entries */
769  entp = NULL;
770  }
771 
772  return entp;
773 }
774 
775 /*
776  * Close directory stream.
777  */
778 static int
780  DIR *dirp)
781 {
782  int ok;
783  if (dirp) {
784 
785  /* Close wide-character directory stream */
786  ok = _wclosedir (dirp->wdirp);
787  dirp->wdirp = NULL;
788 
789  /* Release multi-byte character version */
790  free (dirp);
791 
792  } else {
793 
794  /* Invalid directory stream */
795  dirent_set_errno (EBADF);
796  ok = /*failure*/-1;
797 
798  }
799  return ok;
800 }
801 
802 /*
803  * Rewind directory stream to beginning.
804  */
805 static void
807  DIR* dirp)
808 {
809  /* Rewind wide-character string directory stream */
810  _wrewinddir (dirp->wdirp);
811 }
812 
813 /* Convert multi-byte string to wide character string */
814 static int
816  size_t *pReturnValue,
817  wchar_t *wcstr,
818  size_t sizeInWords,
819  const char *mbstr,
820  size_t count)
821 {
822  int error;
823 
824 #if defined(_MSC_VER) && _MSC_VER >= 1400
825 
826  /* Microsoft Visual Studio 2005 or later */
827  error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count);
828 
829 #else
830 
831  /* Older Visual Studio or non-Microsoft compiler */
832  size_t n;
833 
834  /* Convert to wide-character string (or count characters) */
835  n = mbstowcs (wcstr, mbstr, sizeInWords);
836  if (!wcstr || n < count) {
837 
838  /* Zero-terminate output buffer */
839  if (wcstr && sizeInWords) {
840  if (n >= sizeInWords) {
841  n = sizeInWords - 1;
842  }
843  wcstr[n] = 0;
844  }
845 
846  /* Length of resuting multi-byte string WITH zero terminator */
847  if (pReturnValue) {
848  *pReturnValue = n + 1;
849  }
850 
851  /* Success */
852  error = 0;
853 
854  } else {
855 
856  /* Could not convert string */
857  error = 1;
858 
859  }
860 
861 #endif
862 
863  return error;
864 }
865 
866 /* Convert wide-character string to multi-byte string */
867 static int
869  size_t *pReturnValue,
870  char *mbstr,
871  size_t sizeInBytes, /* max size of mbstr */
872  const wchar_t *wcstr,
873  size_t count)
874 {
875  int error;
876 
877 #if defined(_MSC_VER) && _MSC_VER >= 1400
878 
879  /* Microsoft Visual Studio 2005 or later */
880  error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count);
881 
882 #else
883 
884  /* Older Visual Studio or non-Microsoft compiler */
885  size_t n;
886 
887  /* Convert to multi-byte string (or count the number of bytes needed) */
888  n = wcstombs (mbstr, wcstr, sizeInBytes);
889  if (!mbstr || n < count) {
890 
891  /* Zero-terminate output buffer */
892  if (mbstr && sizeInBytes) {
893  if (n >= sizeInBytes) {
894  n = sizeInBytes - 1;
895  }
896  mbstr[n] = '\0';
897  }
898 
899  /* Lenght of resulting multi-bytes string WITH zero-terminator */
900  if (pReturnValue) {
901  *pReturnValue = n + 1;
902  }
903 
904  /* Success */
905  error = 0;
906 
907  } else {
908 
909  /* Cannot convert string */
910  error = 1;
911 
912  }
913 
914 #endif
915 
916  return error;
917 }
918 
919 /* Set errno variable */
920 static void
922  int error)
923 {
924 #if defined(_MSC_VER) && _MSC_VER >= 1400
925 
926  /* Microsoft Visual Studio 2005 and later */
927  _set_errno (error);
928 
929 #else
930 
931  /* Non-Microsoft compiler or older Microsoft compiler */
932  errno = error;
933 
934 #endif
935 }
936 
937 
938 #ifdef __cplusplus
939 }
940 #endif
941 #endif /*DIRENT_H*/
942 
#define DT_REG
WIN32_FIND_DATAW data
#define DT_CHR
HANDLE handle
static void dirent_set_errno(int error)
unsigned short d_reclen
static int dirent_mbstowcs_s(size_t *pReturnValue, wchar_t *wcstr, size_t sizeInWords, const char *mbstr, size_t count)
char d_name[PATH_MAX]
int cached
int d_type
#define DT_DIR
size_t d_namlen
static WIN32_FIND_DATAW * dirent_first(_WDIR *dirp)
static struct _wdirent * _wreaddir(_WDIR *dirp)
#define PATH_MAX
unsigned short d_reclen
#define DT_UNKNOWN
long d_ino
static int closedir(DIR *dirp)
static void rewinddir(DIR *dirp)
static void _wrewinddir(_WDIR *dirp)
static DIR * opendir(const char *dirname)
wchar_t * patt
static int dirent_wcstombs_s(size_t *pReturnValue, char *mbstr, size_t sizeInBytes, const wchar_t *wcstr, size_t count)
static _WDIR * _wopendir(const wchar_t *dirname)
struct dirent ent
static struct dirent * readdir(DIR *dirp)
size_t d_namlen
static int _wclosedir(_WDIR *dirp)
struct _WDIR * wdirp
static WIN32_FIND_DATAW * dirent_next(_WDIR *dirp)
#define FILE_ATTRIBUTE_DEVICE
Definition: oDirentWin32.hh:70
struct _wdirent ent
wchar_t d_name[PATH_MAX]