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