Skip to content

Commit fe1fb36

Browse files
committed
win32: move type definitions for improved inclusion
Move some win32 type definitions to a standalone file so that they can be included before other header files try to use the definitions.
1 parent e4b2ef8 commit fe1fb36

File tree

4 files changed

+129
-103
lines changed

4 files changed

+129
-103
lines changed

src/common.h

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
# include <ws2tcpip.h>
4848
# include "win32/msvc-compat.h"
4949
# include "win32/mingw-compat.h"
50+
# include "win32/w32_common.h"
5051
# include "win32/win32-compat.h"
5152
# include "win32/error.h"
5253
# include "win32/version.h"
@@ -76,6 +77,7 @@
7677

7778
#include "git2/types.h"
7879
#include "git2/errors.h"
80+
#include "errors.h"
7981
#include "thread-utils.h"
8082
#include "integer.h"
8183

@@ -109,80 +111,6 @@
109111
#define GIT_ERROR_CHECK_ERROR(code) \
110112
do { int _err = (code); if (_err) return _err; } while (0)
111113

112-
/**
113-
* Set the error message for this thread, formatting as needed.
114-
*/
115-
116-
void git_error_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
117-
118-
/**
119-
* Set the error message for a regex failure, using the internal regex
120-
* error code lookup and return a libgit error code.
121-
*/
122-
int git_error_set_regex(const p_regex_t *regex, int error_code);
123-
124-
/**
125-
* Set error message for user callback if needed.
126-
*
127-
* If the error code in non-zero and no error message is set, this
128-
* sets a generic error message.
129-
*
130-
* @return This always returns the `error_code` parameter.
131-
*/
132-
GIT_INLINE(int) git_error_set_after_callback_function(
133-
int error_code, const char *action)
134-
{
135-
if (error_code) {
136-
const git_error *e = git_error_last();
137-
if (!e || !e->message)
138-
git_error_set(e ? e->klass : GIT_ERROR_CALLBACK,
139-
"%s callback returned %d", action, error_code);
140-
}
141-
return error_code;
142-
}
143-
144-
#ifdef GIT_WIN32
145-
#define git_error_set_after_callback(code) \
146-
git_error_set_after_callback_function((code), __FUNCTION__)
147-
#else
148-
#define git_error_set_after_callback(code) \
149-
git_error_set_after_callback_function((code), __func__)
150-
#endif
151-
152-
/**
153-
* Gets the system error code for this thread.
154-
*/
155-
int git_error_system_last(void);
156-
157-
/**
158-
* Sets the system error code for this thread.
159-
*/
160-
void git_error_system_set(int code);
161-
162-
/**
163-
* Structure to preserve libgit2 error state
164-
*/
165-
typedef struct {
166-
int error_code;
167-
unsigned int oom : 1;
168-
git_error error_msg;
169-
} git_error_state;
170-
171-
/**
172-
* Capture current error state to restore later, returning error code.
173-
* If `error_code` is zero, this does not clear the current error state.
174-
* You must either restore this error state, or free it.
175-
*/
176-
extern int git_error_state_capture(git_error_state *state, int error_code);
177-
178-
/**
179-
* Restore error state to a previous value, returning saved error code.
180-
*/
181-
extern int git_error_state_restore(git_error_state *state);
182-
183-
/** Free an error state. */
184-
extern void git_error_state_free(git_error_state *state);
185-
186114
/**
187115
* Check a versioned structure for validity
188116
*/

src/errors.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_errors_h__
9+
#define INCLUDE_errors_h__
10+
11+
#include "posix_regex.h"
12+
#include "common.h"
13+
14+
/*
15+
* Set the error message for this thread, formatting as needed.
16+
*/
17+
18+
void git_error_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
19+
20+
/**
21+
* Set the error message for a regex failure, using the internal regex
22+
* error code lookup and return a libgit error code.
23+
*/
24+
int git_error_set_regex(const p_regex_t *regex, int error_code);
25+
26+
/**
27+
* Set error message for user callback if needed.
28+
*
29+
* If the error code in non-zero and no error message is set, this
30+
* sets a generic error message.
31+
*
32+
* @return This always returns the `error_code` parameter.
33+
*/
34+
GIT_INLINE(int) git_error_set_after_callback_function(
35+
int error_code, const char *action)
36+
{
37+
if (error_code) {
38+
const git_error *e = git_error_last();
39+
if (!e || !e->message)
40+
git_error_set(e ? e->klass : GIT_ERROR_CALLBACK,
41+
"%s callback returned %d", action, error_code);
42+
}
43+
return error_code;
44+
}
45+
46+
#ifdef GIT_WIN32
47+
#define git_error_set_after_callback(code) \
48+
git_error_set_after_callback_function((code), __FUNCTION__)
49+
#else
50+
#define git_error_set_after_callback(code) \
51+
git_error_set_after_callback_function((code), __func__)
52+
#endif
53+
54+
/**
55+
* Gets the system error code for this thread.
56+
*/
57+
int git_error_system_last(void);
58+
59+
/**
60+
* Sets the system error code for this thread.
61+
*/
62+
void git_error_system_set(int code);
63+
64+
/**
65+
* Structure to preserve libgit2 error state
66+
*/
67+
typedef struct {
68+
int error_code;
69+
unsigned int oom : 1;
70+
git_error error_msg;
71+
} git_error_state;
72+
73+
/**
74+
* Capture current error state to restore later, returning error code.
75+
* If `error_code` is zero, this does not clear the current error state.
76+
* You must either restore this error state, or free it.
77+
*/
78+
extern int git_error_state_capture(git_error_state *state, int error_code);
79+
80+
/**
81+
* Restore error state to a previous value, returning saved error code.
82+
*/
83+
extern int git_error_state_restore(git_error_state *state);
84+
85+
/** Free an error state. */
86+
extern void git_error_state_free(git_error_state *state);
87+
88+
#endif

src/win32/path_w32.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,8 @@
88
#define INCLUDE_win32_path_w32_h__
99

1010
#include "common.h"
11-
1211
#include "vector.h"
1312

14-
/*
15-
* Provides a large enough buffer to support Windows paths: MAX_PATH is
16-
* 260, corresponding to a maximum path length of 259 characters plus a
17-
* NULL terminator. Prefixing with "\\?\" adds 4 characters, but if the
18-
* original was a UNC path, then we turn "\\server\share" into
19-
* "\\?\UNC\server\share". So we replace the first two characters with
20-
* 8 characters, a net gain of 6, so the maximum length is MAX_PATH+6.
21-
*/
22-
#define GIT_WIN_PATH_UTF16 MAX_PATH+6
23-
24-
/* Maximum size of a UTF-8 Win32 path. We remove the "\\?\" or "\\?\UNC\"
25-
* prefixes for presentation, bringing us back to 259 (non-NULL)
26-
* characters. UTF-8 does have 4-byte sequences, but they are encoded in
27-
* UTF-16 using surrogate pairs, which takes up the space of two characters.
28-
* Two characters in the range U+0800 -> U+FFFF take up more space in UTF-8
29-
* (6 bytes) than one surrogate pair (4 bytes).
30-
*/
31-
#define GIT_WIN_PATH_UTF8 (259 * 3 + 1)
32-
33-
/*
34-
* The length of a Windows "shortname", for 8.3 compatibility.
35-
*/
36-
#define GIT_WIN_PATH_SHORTNAME 13
37-
38-
/* Win32 path types */
39-
typedef wchar_t git_win32_path[GIT_WIN_PATH_UTF16];
40-
typedef char git_win32_utf8_path[GIT_WIN_PATH_UTF8];
41-
4213
/**
4314
* Create a Win32 path (in UCS-2 format) from a UTF-8 string.
4415
*

src/win32/w32_common.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_win32_w32_common_h__
9+
#define INCLUDE_win32_w32_common_h__
10+
11+
/*
12+
* Provides a large enough buffer to support Windows paths: MAX_PATH is
13+
* 260, corresponding to a maximum path length of 259 characters plus a
14+
* NULL terminator. Prefixing with "\\?\" adds 4 characters, but if the
15+
* original was a UNC path, then we turn "\\server\share" into
16+
* "\\?\UNC\server\share". So we replace the first two characters with
17+
* 8 characters, a net gain of 6, so the maximum length is MAX_PATH+6.
18+
*/
19+
#define GIT_WIN_PATH_UTF16 MAX_PATH+6
20+
21+
/* Maximum size of a UTF-8 Win32 path. We remove the "\\?\" or "\\?\UNC\"
22+
* prefixes for presentation, bringing us back to 259 (non-NULL)
23+
* characters. UTF-8 does have 4-byte sequences, but they are encoded in
24+
* UTF-16 using surrogate pairs, which takes up the space of two characters.
25+
* Two characters in the range U+0800 -> U+FFFF take up more space in UTF-8
26+
* (6 bytes) than one surrogate pair (4 bytes).
27+
*/
28+
#define GIT_WIN_PATH_UTF8 (259 * 3 + 1)
29+
30+
/*
31+
* The length of a Windows "shortname", for 8.3 compatibility.
32+
*/
33+
#define GIT_WIN_PATH_SHORTNAME 13
34+
35+
/* Win32 path types */
36+
typedef wchar_t git_win32_path[GIT_WIN_PATH_UTF16];
37+
typedef char git_win32_utf8_path[GIT_WIN_PATH_UTF8];
38+
39+
#endif

0 commit comments

Comments
 (0)