Add commercialcredit command
[shigofumi.git] / src / utils.h
blobec89ff9f166f863ca21be35e4304b9a554b11029
1 #ifndef __ISDS_UTILS_H__
2 #define __ISDS_UTILS_H__
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <stdio.h>
8 /* _hidden macro marks library private symbols. GCC can exclude them from global
9 * symbols table */
10 #if defined(__GNUC__) && (__GNUC__ >= 4)
11 #define _hidden __attribute__((visibility("hidden")))
12 #else
13 #define _hidden
14 #endif
16 /* PANIC macro aborts current process without any clean up.
17 * Use it as last resort fatal error solution */
18 #define PANIC(message) { \
19 if (stderr != NULL ) fprintf(stderr, \
20 "LIBISDS PANIC (%s:%d): %s\n", __FILE__, __LINE__, (message)); \
21 abort(); \
24 /* Concatenate two strings into newly allocated buffer.
25 * You must free() them, when you don't need it anymore.
26 * Any of the arguments can be NULL meaning empty string.
27 * In case of error returns NULL.
28 * Empty string is always returned as allocated empty string. */
29 char *astrcat(const char *first, const char *second);
31 /* Concatenate three strings into newly allocated buffer.
32 * You must free() them, when you don't need it anymore.
33 * Any of the arguments can be NULL meaning empty string.
34 * In case of error returns NULL.
35 * Empty string is always returned as allocated empty string. */
36 char *astrcat3(const char *first, const char *second,
37 const char *third);
39 /* Print formatted string into automatically reallocated @buffer.
40 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
41 * memory.
42 * @format format string as for printf(3)
43 * @ap list of variadic arguments, after call will be in undefined state
44 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer*/
45 int shi_vasprintf(char **buffer, const char *format, va_list ap);
47 /* Print formatted string into automatically reallocated @buffer.
48 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
49 * memory.
50 * @format format string as for printf(3)
51 * @... variadic arguments
52 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer*/
53 int shi_asprintf(char **buffer, const char *format, ...);
55 /* Converts UTF8 string into locale encoded string.
56 * @utf string int UTF-8 terminated by zero byte
57 * @return allocated string encoded in locale specific encoding. You must free
58 * it. In case of error or NULL @utf returns NULL. */
59 char *utf82locale(const char *utf);
61 /* Converts locale encoded string into UTF8.
62 * @locale string in locale encoded string terminated by zero byte
63 * @return allocated string encoded in UTF-8 encoding. You must free
64 * it. In case of error or NULL @locale returns NULL. */
65 char *locale2utf8(const char *locale);
67 /* Determine how many columns occupies given UTF-8 encoded string.
68 * Return number of columns or -1 */
69 int utf8width(const char *string);
71 /* Determine how many columns occupies given number.
72 * Return number of columns or -1 */
73 int numberwidth(const size_t number);
75 /* Print locale-encoded string occupying exactly given terminal width */
76 void fnprint(FILE *stream, const char *locale_string, int width);
78 /* Print locale-encoded string occupying at lest given terminal width.
79 * If the string is shorter, it will pad the string with spaces.
80 * If the string is wider, it will move cursor to next line on column with
81 * given width. */
82 void fhprint(FILE *stream, const char *locale_string, size_t width);
84 /* Switches time zone to UTC.
85 * XXX: This is not reentrant and not thread-safe */
86 void switch_tz_to_utc(void);
88 /* Switches time zone to original value.
89 * XXX: This is not reentrant and not thread-safe */
90 void switch_tz_to_native(void);
92 /* Free() and set to NULL pointed memory */
93 #define zfree(memory) do { \
94 free(memory); \
95 (memory) = NULL; \
96 } while (0)
98 #endif