examples: consolidate includes into "common.h" Consolidate all standard includes and defines into "common.h". This lets us avoid having to handle platform-specific things in multiple places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
diff --git a/examples/common.h b/examples/common.h
index 204b175..a1f3ec4 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -12,10 +12,25 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
+#include <sys/types.h>
+#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <git2.h>
+#include <fcntl.h>
+
+#ifdef _WIN32
+# include <io.h>
+# include <Windows.h>
+# define open _open
+# define read _read
+# define close _close
+# define ssize_t int
+# define sleep(a) Sleep(a * 1000)
+#else
+# include <unistd.h>
+#endif
#ifndef PRIuZ
/* Define the printf format specifer to use for size_t output */
diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c
index eea73d2..900792c 100644
--- a/examples/for-each-ref.c
+++ b/examples/for-each-ref.c
@@ -1,5 +1,4 @@
#include <git2.h>
-#include <stdio.h>
#include "common.h"
static int show_ref(git_reference *ref, void *data)
diff --git a/examples/general.c b/examples/general.c
index 4bd1dac..ddc53c3 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -36,6 +36,8 @@
* [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
*/
+#include "common.h"
+
/**
* ### Includes
*
@@ -43,9 +45,7 @@
* that you need. It should be the only thing you need to include in order
* to compile properly and get all the libgit2 API.
*/
-#include <git2.h>
-#include <stdio.h>
-#include <string.h>
+#include "git2.h"
static void oid_parsing(git_oid *out);
static void object_database(git_repository *repo, git_oid *oid);
diff --git a/examples/index-pack.c b/examples/index-pack.c
index 0941e09..2181f43 100644
--- a/examples/index-pack.c
+++ b/examples/index-pack.c
@@ -1,21 +1,5 @@
#include "common.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#ifdef _WIN32
-# include <io.h>
-# include <Windows.h>
-
-# define open _open
-# define read _read
-# define close _close
-
-#define ssize_t int
-#else
-# include <unistd.h>
-#endif
-
/*
* This could be run in the main loop whilst the application waits for
* the indexing to finish in a worker thread
diff --git a/examples/status.c b/examples/status.c
index 38e5120..979ab7c 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -13,12 +13,6 @@
*/
#include "common.h"
-#ifdef _WIN32
-# include <Windows.h>
-# define sleep(a) Sleep(a * 1000)
-#else
-# include <unistd.h>
-#endif
/**
* This example demonstrates the use of the libgit2 status APIs,