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