common.h 2.44 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9
#ifndef INCLUDE_common_h__
#define INCLUDE_common_h__

10
#include "git2/common.h"
11
#include "cc-compat.h"
12

13
#include <assert.h>
14
#include <errno.h>
15
#include <limits.h>
16
#include <stdlib.h>
17
#include <stdio.h>
18 19
#include <string.h>

20
#include <sys/types.h>
21
#include <sys/stat.h>
22 23 24 25

#ifdef GIT_WIN32

# include <io.h>
26
# include <direct.h>
27
# include <winsock2.h>
28
# include <windows.h>
29 30
# include "win32/msvc-compat.h"
# include "win32/mingw-compat.h"
31
# include "win32/error.h"
32
# include "win32/version.h"
Vicent Marti committed
33
# ifdef GIT_THREADS
Vicent Marti committed
34
#	include "win32/pthread.h"
35
# endif
36

37 38
#else

39
# include <unistd.h>
Vicent Marti committed
40
# ifdef GIT_THREADS
Vicent Marti committed
41
#	include <pthread.h>
Vicent Marti committed
42
# endif
Russell Belfer committed
43 44
#define GIT_STDLIB_CALL

45 46
#endif

47
#include "git2/types.h"
48
#include "git2/errors.h"
49
#include "thread-utils.h"
50
#include "bswap.h"
51

52 53
#include <regex.h>

54 55 56
/**
 * Check a pointer allocation result, returning -1 if it failed.
 */
57 58
#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }

59
/**
60 61 62 63 64 65
 * Check a return value and propogate result if non-zero.
 */
#define GITERR_CHECK_ERROR(code) \
	do { int _err = (code); if (_err < 0) return _err; } while (0)

/**
66 67
 * Set the error message for this thread, formatting as needed.
 */
68
void giterr_set(int error_class, const char *string, ...);
69 70 71

/**
 * Set the error message for a regex failure, using the internal regex
72
 * error code lookup and return a libgit error code.
73
 */
74
int giterr_set_regex(const regex_t *regex, int error_code);
75

Ben Straub committed
76 77 78
/**
 * Check a versioned structure for validity
 */
79
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Ben Straub committed
80
{
Vicent Marti committed
81
	unsigned int actual;
Vicent Marti committed
82

Ben Straub committed
83
	if (!structure)
84
		return 0;
Ben Straub committed
85

Vicent Marti committed
86
	actual = *(const unsigned int*)structure;
Ben Straub committed
87
	if (actual > 0 && actual <= expected_max)
88
		return 0;
Ben Straub committed
89 90

	giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name);
91
	return -1;
Ben Straub committed
92
}
93
#define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1
Ben Straub committed
94

Ben Straub committed
95 96 97 98 99 100 101 102 103 104
/**
 * Initialize a structure with a version.
 */
GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version)
{
	memset(structure, 0, len);
	*((int*)structure) = version;
}
#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)

105 106
/* NOTE: other giterr functions are in the public errors.h header file */

107 108
#include "util.h"

109
#endif /* INCLUDE_common_h__ */