]> wimlib.net Git - wimlib/blob - src/util.h
Use WARNING() for warnings instead of ERROR().
[wimlib] / src / util.h
1 /*
2  * util.h
3  *
4  * Header for util.c.
5  *
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24 #ifndef _WIMLIB_UTIL_H
25 #define _WIMLIB_UTIL_H
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <inttypes.h>
31 #include <sys/types.h>
32 #include "config.h"
33
34
35 typedef uint8_t  u8;
36 typedef uint16_t u16;
37 typedef uint32_t u32;
38 typedef uint64_t u64;
39 typedef unsigned uint;
40
41 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
42                                         (__a < __b) ? __a : __b; })
43 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
44                                         (__a > __b) ? __a : __b; })
45 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
46
47 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
48
49 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
50
51 #ifdef ENABLE_ERROR_MESSAGES
52 extern bool __wimlib_print_errors;
53 extern void wimlib_error(const char *format, ...);
54 extern void wimlib_warning(const char *format, ...);
55 #  define ERROR wimlib_error
56 #  define WARNING wimlib_warning
57 #else
58 #  define ERROR(format, ...)
59 #  define WARNING(format, ...)
60 #endif /* ENABLE_ERROR_MESSAGES */
61
62 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
63 #include <errno.h>
64 #  define DEBUG(format, ...)  \
65 ({ \
66         int __errno_save = errno; \
67         fprintf(stdout, "[%s %d] %s(): " format, \
68                 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
69         fflush(stdout); \
70         errno = __errno_save; \
71         })
72
73 #else
74 #  define DEBUG(format, ...)
75 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
76
77 #ifdef ENABLE_MORE_DEBUG
78 #  define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
79 #else
80 #  define DEBUG2(format, ...)
81 #endif /* ENABLE_DEBUG */
82
83 #ifdef ENABLE_ASSERTIONS
84 #include <assert.h>
85 #       define wimlib_assert(expr) assert(expr)
86 #else
87 #       define wimlib_assert(expr)
88 #endif
89
90 #ifdef __GNUC__
91 #  define WIMLIBAPI __attribute__((visibility("default")))
92 #  define NOINLINE __attribute__((noinline))
93 #  define ALWAYS_INLINE inline __attribute__((always_inline))
94 #  define COLD     __attribute__((cold))
95 #  define HOT      __attribute__((hot))
96 #else
97 #  define WIMLIBAPI
98 #  define NOINLINE
99 #  define ALWAYS_INLINE inline
100 #  define COLD
101 #  define HOT
102 #endif /* __GNUC__ */
103
104 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
105 extern void *(*wimlib_malloc_func)(size_t);
106 extern void (*wimlib_free_func)(void *);
107 extern void *(*wimlib_realloc)(void *, size_t);
108 extern void *wimlib_calloc(size_t nmemb, size_t size);
109 extern char *wimlib_strdup(const char *str);
110 #  define MALLOC wimlib_malloc_func
111 #  define FREE wimlib_free_func
112 #  define REALLOC wimlib_realloc_func
113 #  define CALLOC wimlib_calloc
114 #  define STRDUP wimlib_strdup
115 #else
116 #include <stdlib.h>
117 #include <string.h>
118 #  define MALLOC malloc
119 #  define FREE free
120 #  define REALLOC realloc
121 #  define CALLOC calloc
122 #  define STRDUP strdup
123 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
124
125
126 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
127                            size_t *utf8_len_ret);
128
129 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len, 
130                            size_t *utf16_len_ret);
131
132 extern void randomize_byte_array(void *p, size_t n);
133
134 extern void randomize_char_array_with_alnum(char p[], size_t n);
135
136 extern int sha1sum(const char *filename, void *buf);
137
138 extern const char *path_next_part(const char *path, 
139                                   size_t *first_part_len_ret);
140
141 extern const char *path_basename(const char *path);
142
143 extern void to_parent_name(char buf[], size_t len);
144
145 extern void print_string(const void *string, size_t len);
146
147 extern int get_num_path_components(const char *path);
148
149 extern ssize_t full_write(int fd, const void *buf, size_t n);
150
151
152 static inline void print_byte_field(const u8 field[], size_t len)
153 {
154         while (len--)
155                 printf("%02hhx", *field++);
156 }
157
158
159 #endif /* _WIMLIB_UTIL_H */