Browse Source

Simplify minirent consumption

This change simplifies the integration with this library. It refactors
`#if` guards to automatically include `<dirent.h>` on non-WIN32
platforms.

Therefore, usages of this library can be changed from this:

```c
 #ifdef _WIN32
 #  define MINIRENT_IMPLEMENTATION
 #  include "minirent.h"
 #else
 #  include <dirent.h>
 #endif
```

to this:

```c
 #define MINIRENT_IMPLEMENTATION
 #include "minirent.h"
```

No fuss, no wuss. This should be backwards compatible. README and CI
were updated to reflect it.
Eduardo Costa 3 năm trước cách đây
mục cha
commit
3c6603fbbd
4 tập tin đã thay đổi với 16 bổ sung18 xóa
  1. 3 3
      .github/workflows/ci.yml
  2. 4 6
      README.md
  3. 2 6
      examples/ls.c
  4. 7 3
      minirent.h

+ 3 - 3
.github/workflows/ci.yml

@@ -8,7 +8,7 @@ jobs:
       - uses: actions/checkout@v1
       - uses: actions/checkout@v1
       - name: build all and examples
       - name: build all and examples
         run: |
         run: |
-          $CC -o ls ./examples/ls.c
+          $CC -I . -o ls ./examples/ls.c
           ./ls
           ./ls
         env:
         env:
           CC: gcc
           CC: gcc
@@ -19,7 +19,7 @@ jobs:
       - uses: actions/checkout@v1
       - uses: actions/checkout@v1
       - name: build all and examples
       - name: build all and examples
         run: |
         run: |
-          $CC -o ls ./examples/ls.c
+          $CC -I . -o ls ./examples/ls.c
           ./ls
           ./ls
         env:
         env:
           CC: clang
           CC: clang
@@ -30,7 +30,7 @@ jobs:
       - uses: actions/checkout@v1
       - uses: actions/checkout@v1
       - name: build all and examples
       - name: build all and examples
         run: |
         run: |
-          $CC -o ls ./examples/ls.c
+          $CC -I . -o ls ./examples/ls.c
           ./ls
           ./ls
         env:
         env:
           CC: clang
           CC: clang

+ 4 - 6
README.md

@@ -6,6 +6,8 @@ A subset of [dirent](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/d
 
 
 The code that works with minirent must work with the dirent.
 The code that works with minirent must work with the dirent.
 
 
+Use minirent as a cross-platform replacement of dirent if you only need the subset interface.
+
 ## Usage
 ## Usage
 
 
 [minirent.h](./minirent.h) is an [stb-style](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) header-only library. That means that when you just include it it does not include the implementations of the functions. You have to define `MINIRENT_IMPLEMENTATION` macro:
 [minirent.h](./minirent.h) is an [stb-style](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) header-only library. That means that when you just include it it does not include the implementations of the functions. You have to define `MINIRENT_IMPLEMENTATION` macro:
@@ -15,12 +17,8 @@ The code that works with minirent must work with the dirent.
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 
 
-#ifdef _WIN32
-#    define MINIRENT_IMPLEMENTATION
-#    include <minirent.h>
-#else
-#    include <dirent.h>
-#endif // _WIN32
+#define MINIRENT_IMPLEMENTATION
+#include <minirent.h>
 
 
 int main(void)
 int main(void)
 {
 {

+ 2 - 6
examples/ls.c

@@ -4,12 +4,8 @@
 #include <string.h>
 #include <string.h>
 #include <errno.h>
 #include <errno.h>
 
 
-#ifdef _WIN32
-#    define MINIRENT_IMPLEMENTATION
-#    include <minirent.h>
-#else
-#    include <dirent.h>
-#endif // _WIN32
+#define MINIRENT_IMPLEMENTATION
+#include <minirent.h>
 
 
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {

+ 7 - 3
minirent.h

@@ -34,6 +34,10 @@
 #ifndef MINIRENT_H_
 #ifndef MINIRENT_H_
 #define MINIRENT_H_
 #define MINIRENT_H_
 
 
+#ifndef _WIN32
+#include <dirent.h>
+#else // _WIN32
+
 #define WIN32_LEAN_AND_MEAN
 #define WIN32_LEAN_AND_MEAN
 #include "windows.h"
 #include "windows.h"
 
 
@@ -48,8 +52,6 @@ DIR *opendir(const char *dirpath);
 struct dirent *readdir(DIR *dirp);
 struct dirent *readdir(DIR *dirp);
 int closedir(DIR *dirp);
 int closedir(DIR *dirp);
 
 
-#endif  // MINIRENT_H_
-
 #ifdef MINIRENT_IMPLEMENTATION
 #ifdef MINIRENT_IMPLEMENTATION
 
 
 struct DIR
 struct DIR
@@ -133,4 +135,6 @@ int closedir(DIR *dirp)
     return 0;
     return 0;
 }
 }
 
 
-#endif  // MINIRENT_IMPLEMENTATION
+#endif // MINIRENT_IMPLEMENTATION
+#endif // _WIN32
+#endif // MINIRENT_H_