common.h 2.21 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
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"
Vicent Marti committed
31
# ifdef GIT_THREADS
Vicent Marti committed
32
#	include "win32/pthread.h"
Vicent Marti committed
33
#endif
34

35
# define snprintf _snprintf
36 37 38 39

#else
# include <unistd.h>

Vicent Marti committed
40
# ifdef GIT_THREADS
Vicent Marti committed
41
#	include <pthread.h>
Vicent Marti committed
42
# endif
43 44
#endif

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

50 51
#include <regex.h>

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

57 58 59
/**
 * Set the error message for this thread, formatting as needed.
 */
60
void giterr_set(int error_class, const char *string, ...);
61 62 63

/**
 * Set the error message for a regex failure, using the internal regex
64
 * error code lookup and return a libgit error code.
65
 */
66
int giterr_set_regex(const regex_t *regex, int error_code);
67

Ben Straub committed
68 69 70
/**
 * Check a versioned structure for validity
 */
71
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Ben Straub committed
72
{
Vicent Marti committed
73
	unsigned int actual;
Vicent Marti committed
74

Ben Straub committed
75
	if (!structure)
76
		return 0;
Ben Straub committed
77

Vicent Marti committed
78
	actual = *(const unsigned int*)structure;
Ben Straub committed
79
	if (actual > 0 && actual <= expected_max)
80
		return 0;
Ben Straub committed
81 82

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

Ben Straub committed
87 88 89 90 91 92 93 94 95 96
/**
 * 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)

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

99 100
#include "util.h"

101
#endif /* INCLUDE_common_h__ */