l10n: Update translation catalogues
[libisds.git] / src / utils.h
blobaa1fa84b2ab6f5a6a57b9b0d04198bd29c97c6c2
1 #ifndef __ISDS_UTILS_H__
2 #define __ISDS_UTILS_H__
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <time.h>
9 /* _hidden macro marks library private symbols. GCC can exclude them from global
10 * symbols table */
11 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(_WIN32)
12 #define _hidden __attribute__((visibility("hidden")))
13 #else
14 #define _hidden
15 #endif
17 /* PANIC macro aborts current process without any clean up.
18 * Use it as last resort fatal error solution */
19 #define PANIC(message) { \
20 if (stderr != NULL ) fprintf(stderr, \
21 "LIBISDS PANIC (%s:%d): %s\n", __FILE__, __LINE__, (message)); \
22 abort(); \
25 /* Concatenate two strings into newly allocated buffer.
26 * You must free() them, when you don't need it anymore.
27 * Any of the arguments can be NULL meaning empty string.
28 * In case of error returns NULL.
29 * Empty string is always returned as allocated empty string. */
30 char *_isds_astrcat(const char *first, const char *second);
32 /* Concatenate three strings into newly allocated buffer.
33 * You must free() them, when you don't need it anymore.
34 * Any of the arguments can be NULL meaning empty string.
35 * In case of error returns NULL.
36 * Empty string is always returned as allocated empty string. */
37 char *_isds_astrcat3(const char *first, const char *second,
38 const char *third);
40 /* Print formatted string into automatically reallocated @buffer.
41 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
42 * memory.
43 * @format format string as for printf(3)
44 * @ap list of variadic arguments, after call will be in undefined state
45 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer*/
46 int isds_vasprintf(char **buffer, const char *format, va_list ap);
48 /* Print formatted string into automatically reallocated @buffer.
49 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
50 * memory.
51 * @format format string as for printf(3)
52 * @... variadic arguments
53 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer */
54 int isds_asprintf(char **buffer, const char *format, ...);
56 /* Converts a block from charset to charset.
57 * @from is input charset of @input block as known to iconv
58 * @to is output charset @input will be converted to @output
59 * @input is block in @from charset/encoding of length @input_length
60 * @input_length is size of @input block in bytes
61 * @output is automatically allocated block of data converted from @input. No
62 * NUL is apended. Can be NULL, if resulting size is 0. You must free it.
63 * @return size of @output in bytes. In case of error returns (size_t) -1 and
64 * deallocates @output if this function allocated it in this call. */
65 _hidden size_t _isds_any2any(const char *from, const char *to,
66 const void *input, size_t input_length, void **output);
68 /* Converts UTF8 string into locale encoded string.
69 * @utf string int UTF-8 terminated by zero byte
70 * @return allocated string encoded in locale specific encoding. You must free
71 * it. In case of error or NULL @utf returns NULL. */
72 char *_isds_utf82locale(const char *utf);
74 /* Encode given data into MIME Base64 encoded zero terminated string.
75 * @plain are input data (binary stream)
76 * @length is length of @plain data in bytes
77 * @return allocated string of base64 encoded plain data or NULL in case of
78 * error. You must free it. */
79 char *_isds_b64encode(const void *plain, const size_t length);
81 /* Decode given data from MIME Base64 encoded zero terminated string to binary
82 * stream. Invalid Base64 symbols are skipped.
83 * @encoded are input data (Base64 zero terminated string)
84 * @plain are automatically reallocated output data (binary stream). You must
85 * free it. Will be freed in case of error.
86 * @return length of @plain data in bytes or (size_t) -1 in case of memory
87 * allocation failure. */
88 size_t _isds_b64decode(const char *encoded, void **plain);
90 /* Convert hexadecimal digit to integer. Return negative value if character is
91 * not valid hexadecimal digit. */
92 int _isds_hex2i(char digit);
94 /* Convert UTC broken time to time_t.
95 * @broken_utc it time in UTC in broken format. Despite its content is not
96 * touched, it'sw not-const because underlying POSIX function has non-const
97 * signature.
98 * @return (time_t) -1 in case of error */
99 time_t _isds_timegm(struct tm *broken_utc);
101 /* Convert size_t to int.
102 * @val Value to be converted to int.
103 * @return value converted to int or -1 when the supplied value is too large
104 * to fit into integer. */
105 int _isds_sizet2int(size_t val);
107 /* Free() and set to NULL pointed memory */
108 #define zfree(memory) { \
109 free(memory); \
110 (memory) = NULL; \
113 #endif