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