Browse Source

include minirent.h for windows

rexim 5 years ago
parent
commit
da8f245bf7
1 changed files with 132 additions and 0 deletions
  1. 132 0
      nobuild2.h

+ 132 - 0
nobuild2.h

@@ -16,6 +16,59 @@
 #    include <process.h>
 #    define PATH_SEP "\\"
      typedef HANDLE Pid;
+// minirent.h HEADER BEGIN ////////////////////////////////////////
+    // Copyright 2021 Alexey Kutepov <[email protected]>
+    //
+    // Permission is hereby granted, free of charge, to any person obtaining
+    // a copy of this software and associated documentation files (the
+    // "Software"), to deal in the Software without restriction, including
+    // without limitation the rights to use, copy, modify, merge, publish,
+    // distribute, sublicense, and/or sell copies of the Software, and to
+    // permit persons to whom the Software is furnished to do so, subject to
+    // the following conditions:
+    //
+    // The above copyright notice and this permission notice shall be
+    // included in all copies or substantial portions of the Software.
+    //
+    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+    // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+    // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+    // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+    // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+    //
+    // ============================================================
+    //
+    // minirent — 0.0.1 — A subset of dirent interface for Windows.
+    //
+    // https://github.com/tsoding/minirent
+    //
+    // ============================================================
+    //
+    // ChangeLog (https://semver.org/ is implied)
+    //
+    //    0.0.1 First Official Release
+
+    #ifndef MINIRENT_H_
+    #define MINIRENT_H_
+
+    #define WIN32_LEAN_AND_MEAN
+    #include "windows.h"
+
+    struct dirent
+    {
+        char d_name[MAX_PATH+1];
+    };
+
+    typedef struct DIR DIR;
+
+    DIR *opendir(const char *dirpath);
+    struct dirent *readdir(DIR *dirp);
+    int closedir(DIR *dirp);
+
+    #endif  // MINIRENT_H_
+// minirent.h HEADER END ////////////////////////////////////////
 #endif  // _WIN32
 
 #include <stdio.h>
@@ -115,6 +168,85 @@ void PANIC(Cstr fmt, ...);
 
 #ifdef NOBUILD_IMPLEMENTATION
 
+#ifdef _WIN32
+// minirent.h IMPLEMENTATION BEGIN ////////////////////////////////////////
+    struct DIR
+    {
+        HANDLE hFind;
+        WIN32_FIND_DATA data;
+        struct dirent *dirent;
+    };
+
+    DIR *opendir(const char *dirpath)
+    {
+        assert(dirpath);
+
+        char buffer[MAX_PATH];
+        snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
+
+        DIR *dir = (DIR*)calloc(1, sizeof(DIR));
+
+        dir->hFind = FindFirstFile(buffer, &dir->data);
+        if (dir->hFind == INVALID_HANDLE_VALUE) {
+            errno = ENOSYS;
+            goto fail;
+        }
+
+        return dir;
+
+    fail:
+        if (dir) {
+            free(dir);
+        }
+
+        return NULL;
+    }
+
+    struct dirent *readdir(DIR *dirp)
+    {
+        assert(dirp);
+
+        if (dirp->dirent == NULL) {
+            dirp->dirent = (struct dirent*)calloc(1, sizeof(struct dirent));
+        } else {
+            if(!FindNextFile(dirp->hFind, &dirp->data)) {
+                if (GetLastError() != ERROR_NO_MORE_FILES) {
+                    errno = ENOSYS;
+                }
+
+                return NULL;
+            }
+        }
+
+        memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
+
+        strncpy(
+            dirp->dirent->d_name,
+            dirp->data.cFileName,
+            sizeof(dirp->dirent->d_name) - 1);
+
+        return dirp->dirent;
+    }
+
+    int closedir(DIR *dirp)
+    {
+        assert(dirp);
+
+        if(!FindClose(dirp->hFind)) {
+            errno = ENOSYS;
+            return -1;
+        }
+
+        if (dirp->dirent) {
+            free(dirp->dirent);
+        }
+        free(dirp);
+
+        return 0;
+    }
+// minirent.h IMPLEMENTATION END ////////////////////////////////////////
+#endif // _WIN32
+
 Cstr_Array cstr_array_append(Cstr_Array cstrs, Cstr cstr)
 {
     Cstr_Array result = {