]> git.proxmox.com Git - libgit2.git/blob - src/util.h
I broke your bindings
[libgit2.git] / src / util.h
1 #ifndef INCLUDE_util_h__
2 #define INCLUDE_util_h__
3
4 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
6 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
7
8 /*
9 * Don't wrap malloc/calloc.
10 * Use the default versions in glibc, and make
11 * sure that any methods that allocate memory
12 * return a GIT_ENOMEM error when allocation
13 * fails.
14 */
15 #define git__malloc malloc
16 #define git__calloc calloc
17 #define git__strdup strdup
18
19 extern int git__fmt(char *, size_t, const char *, ...)
20 GIT_FORMAT_PRINTF(3, 4);
21 extern int git__prefixcmp(const char *str, const char *prefix);
22 extern int git__suffixcmp(const char *str, const char *suffix);
23
24 /*
25 * The dirname() function shall take a pointer to a character string
26 * that contains a pathname, and return a pointer to a string that is a
27 * pathname of the parent directory of that file. Trailing '/' characters
28 * in the path are not counted as part of the path.
29 *
30 * If path does not contain a '/', then dirname() shall return a pointer to
31 * the string ".". If path is a null pointer or points to an empty string,
32 * dirname() shall return a pointer to the string "." .
33 *
34 * The `git__dirname` implementation is thread safe. The returned
35 * string must be manually free'd.
36 *
37 * The `git__dirname_r` implementation expects a string allocated
38 * by the user with big enough size.
39 */
40 extern char *git__dirname(const char *path);
41 extern int git__dirname_r(char *buffer, size_t bufflen, const char *path);
42
43 /*
44 * This function returns the basename of the file, which is the last
45 * part of its full name given by fname, with the drive letter and
46 * leading directories stripped off. For example, the basename of
47 * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo.
48 *
49 * Trailing slashes and backslashes are significant: the basename of
50 * c:/foo/bar/ is an empty string after the rightmost slash.
51 *
52 * The `git__basename` implementation is thread safe. The returned
53 * string must be manually free'd.
54 *
55 * The `git__basename_r` implementation expects a string allocated
56 * by the user with big enough size.
57 */
58 extern char *git__basename(const char *path);
59 extern int git__basename_r(char *buffer, size_t bufflen, const char *path);
60
61 extern const char *git__topdir(const char *path);
62
63 /**
64 * Join two paths together. Takes care of properly fixing the
65 * middle slashes and everything
66 *
67 * The paths are joined together into buffer_out; this is expected
68 * to be an user allocated buffer of `GIT_PATH_MAX` size
69 */
70 extern void git__joinpath_n(char *buffer_out, int npath, ...);
71
72 GIT_INLINE(void) git__joinpath(char *buffer_out, const char *path_a, const char *path_b)
73 {
74 git__joinpath_n(buffer_out, 2, path_a, path_b);
75 }
76
77 extern void git__hexdump(const char *buffer, size_t n);
78 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
79
80
81 /** @return true if p fits into the range of a size_t */
82 GIT_INLINE(int) git__is_sizet(git_off_t p)
83 {
84 size_t r = (size_t)p;
85 return p == (git_off_t)r;
86 }
87
88 /* 32-bit cross-platform rotl */
89 #ifdef _MSC_VER /* use built-in method in MSVC */
90 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
91 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
92 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
93 #endif
94
95 extern char *git__strtok(char *output, char *src, char *delimit);
96 extern char *git__strtok_keep(char *output, char *src, char *delimit);
97
98 #define STRLEN(str) (sizeof(str) - 1)
99
100 #define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
101
102 /*
103 * Realloc the buffer pointed at by variable 'x' so that it can hold
104 * at least 'nr' entries; the number of entries currently allocated
105 * is 'alloc', using the standard growing factor alloc_nr() macro.
106 *
107 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
108 */
109 #define alloc_nr(x) (((x)+16)*3/2)
110 #define ALLOC_GROW(x, nr, alloc) \
111 do { \
112 if ((nr) > alloc) { \
113 if (alloc_nr(alloc) < (nr)) \
114 alloc = (nr); \
115 else \
116 alloc = alloc_nr(alloc); \
117 x = xrealloc((x), alloc * sizeof(*(x))); \
118 } \
119 } while (0)
120
121 #endif /* INCLUDE_util_h__ */