Logo

index : raylib-jai

Bindings from https://solarium.technology

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/raylib-jai.git/html/Raylib/raylib/src/external/dirent.h blob: c6113765e44c6e62fcbfbb4da621d369d6a75946 [raw] [clear marker]

        
0/****************************************************************************
1
2 Declaration of POSIX directory browsing functions and types for Win32.
3
4 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
5 History: Created March 1997. Updated June 2003.
6 Reviewed by Ramon Santamaria for raylib on January 2020.
7
8 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
9
10 Permission to use, copy, modify, and distribute this software and its
11 documentation for any purpose is hereby granted without fee, provided
12 that this copyright and permissions notice appear in all copies and
13 derivatives.
14
15 This software is supplied "as is" without express or implied warranty.
16
17 But that said, if there are any problems please get in touch.
18
19****************************************************************************/
20
21#ifndef DIRENT_H
22#define DIRENT_H
23
24// Allow custom memory allocators
25#ifndef DIRENT_MALLOC
26 #define DIRENT_MALLOC(sz) malloc(sz)
27#endif
28#ifndef DIRENT_FREE
29 #define DIRENT_FREE(p) free(p)
30#endif
31
32//----------------------------------------------------------------------------------
33// Types and Structures Definition
34//----------------------------------------------------------------------------------
35
36// Fordward declaration of DIR, implementation below
37typedef struct DIR DIR;
38
39struct dirent {
40 char *d_name;
41};
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47//------------------------------------------------------------------------------------
48// Functions Declaration
49//------------------------------------------------------------------------------------
50DIR *opendir(const char *name);
51int closedir(DIR *dir);
52struct dirent *readdir(DIR *dir);
53void rewinddir(DIR *dir);
54
55#ifdef __cplusplus
56}
57#endif
58
59#endif // DIRENT_H
60
61/****************************************************************************
62
63 Implementation of POSIX directory browsing functions and types for Win32.
64
65 Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com)
66 History: Created March 1997. Updated June 2003.
67 Reviewed by Ramon Santamaria for raylib on January 2020.
68
69 Copyright Kevlin Henney, 1997, 2003. All rights reserved.
70
71 Permission to use, copy, modify, and distribute this software and its
72 documentation for any purpose is hereby granted without fee, provided
73 that this copyright and permissions notice appear in all copies and
74 derivatives.
75
76 This software is supplied "as is" without express or implied warranty.
77
78 But that said, if there are any problems please get in touch.
79
80****************************************************************************/
81
82#include <io.h> // _findfirst and _findnext set errno iff they return -1
83#include <stdlib.h>
84#include <string.h>
85#include <errno.h>
86
87//----------------------------------------------------------------------------------
88// Types and Structures Definition
89//----------------------------------------------------------------------------------
90typedef ptrdiff_t handle_type; // C99's intptr_t not sufficiently portable
91
92struct DIR {
93 handle_type handle; // -1 for failed rewind
94 struct _finddata_t info;
95 struct dirent result; // d_name null iff first time
96 char *name; // null-terminated char string
97};
98
99DIR *opendir(const char *name)
100{
101 DIR *dir = 0;
102
103 if (name && name[0])
104 {
105 size_t base_length = strlen(name);
106
107 // Search pattern must end with suitable wildcard
108 const char *all = strchr("/\\", name[base_length - 1]) ? "*" : "/*";
109
110 if ((dir = (DIR *)DIRENT_MALLOC(sizeof *dir)) != 0 &&
111 (dir->name = (char *)DIRENT_MALLOC(base_length + strlen(all) + 1)) != 0)
112 {
113 strcat(strcpy(dir->name, name), all);
114
115 if ((dir->handle = (handle_type) _findfirst(dir->name, &dir->info)) != -1)
116 {
117 dir->result.d_name = 0;
118 }
119 else // rollback
120 {
121 DIRENT_FREE(dir->name);
122 DIRENT_FREE(dir);
123 dir = 0;
124 }
125 }
126 else // rollback
127 {
128 DIRENT_FREE(dir);
129 dir = 0;
130 errno = ENOMEM;
131 }
132 }
133 else errno = EINVAL;
134
135 return dir;
136}
137
138int closedir(DIR *dir)
139{
140 int result = -1;
141
142 if (dir)
143 {
144 if (dir->handle != -1) result = _findclose(dir->handle);
145
146 DIRENT_FREE(dir->name);
147 DIRENT_FREE(dir);
148 }
149
150 // NOTE: All errors ampped to EBADF
151 if (result == -1) errno = EBADF;
152
153 return result;
154}
155
156struct dirent *readdir(DIR *dir)
157{
158 struct dirent *result = 0;
159
160 if (dir && dir->handle != -1)
161 {
162 if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1)
163 {
164 result = &dir->result;
165 result->d_name = dir->info.name;
166 }
167 }
168 else errno = EBADF;
169
170 return result;
171}
172
173void rewinddir(DIR *dir)
174{
175 if (dir && dir->handle != -1)
176 {
177 _findclose(dir->handle);
178 dir->handle = (handle_type) _findfirst(dir->name, &dir->info);
179 dir->result.d_name = 0;
180 }
181 else errno = EBADF;
182}
183
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit