0f2ed05e57be7efd7fa01146e916ed721d28a31f
[framework/graphics/cairo.git] / perf / dirent-win32.h
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /*
3  * Copyright 2011 Andrea Canciani
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * the authors not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. The authors make no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: Andrea Canciani <ranma42@gmail.com>
25  */
26
27 #include "cairo-compiler-private.h"
28
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
31
32 #define stat _stat
33
34 #define S_ISDIR(s) ((s) & _S_IFDIR)
35
36 struct dirent {
37     ino_t d_ino;
38     char d_name[FILENAME_MAX + 1];
39 };
40
41 typedef struct _DIR {
42     HANDLE handle;
43     cairo_bool_t has_next;
44     WIN32_FIND_DATA data;
45     struct dirent de;
46 } DIR;
47
48 static DIR *
49 opendir(const char *dirname)
50 {
51     DIR *dirp;
52
53     dirp = malloc (sizeof (*dirp));
54     if (unlikely (dirp == NULL))
55         return NULL;
56
57     dirp->handle = FindFirstFile(dirname, &dirp->data);
58
59     if (unlikely (dirp->handle == INVALID_HANDLE_VALUE)) {
60         free (dirp);
61         return NULL;
62     }
63
64     memcpy (dirp->de.d_name, dirp->data.cFileName,
65             sizeof (dirp->data.cFileName));
66     dirp->de.d_name[FILENAME_MAX] = '\0';
67
68     dirp->has_next = TRUE;
69
70     return dirp;
71 }
72
73 static int
74 closedir(DIR *dirp)
75 {
76     int ret;
77
78     ret = ! FindClose (dirp->handle);
79
80     free (dirp);
81
82     /* TODO: set errno */
83
84     return ret;
85 }
86
87 static struct dirent *
88 readdir(DIR *dirp)
89 {
90     if (! dirp->has_next)
91         return NULL;
92
93     /* COMPILE_TIME_ASSERT (FILENAME_MAX == sizeof (dirp->data.cFileName)); */
94
95     memcpy (dirp->de.d_name, dirp->data.cFileName,
96             sizeof (dirp->data.cFileName));
97     dirp->de.d_name[FILENAME_MAX] = '\0';
98
99     dirp->has_next = FindNextFile (dirp->handle, &dirp->data);
100
101     return &dirp->de;
102 }