Merge branch 't/gui_improvements'
[paraslash.git] / string.h
1 /*
2 * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file string.h exported sybmols from string.c */
8
9 /** Flags that change how content is printed into the buffer. */
10 enum para_buffer_flags {
11 /** Prefix each buffer with its length. */
12 PBF_SIZE_PREFIX = 1,
13 };
14
15 /** A string buffer used for para_printf(). */
16 struct para_buffer {
17 /** The buffer. May be \p NULL. */
18 char *buf;
19 /** The size of \a buf. */
20 size_t size;
21 /** The maximal size this buffer may grow. Zero means unlimited. */
22 size_t max_size;
23 /** \sa para_buffer_flags. */
24 unsigned flags;
25 /** The next para_printf() will write at this offset. */
26 size_t offset;
27 /**
28 * If an attempt to print into this buffer is made that would need to
29 * grow \a buf to a size larger than \a max_size, then this function is
30 * called.
31 */
32 int (*max_size_handler)(char *buf, size_t size, void *private_data);
33 /** Passed to max_size_handler(). */
34 void *private_data;
35 };
36
37 /**
38 * Controls the behavior of for_each_line().
39 *
40 * \sa for_each_line().
41 */
42 enum for_each_line_flags {
43 /** Activate read-only mode. */
44 FELF_READ_ONLY = 1 << 0,
45 /** Don't call line handler for the first input line. */
46 FELF_DISCARD_FIRST = 1 << 1,
47 };
48
49 /** Used for \ref for_each_line(). */
50 typedef int line_handler_t(char *, void *);
51 int for_each_line(unsigned flags, char *buf, size_t size,
52 line_handler_t *line_handler, void *private_data);
53
54 /**
55 * Write the contents of a status item to a para_buffer.
56 *
57 * \param b The para_buffer.
58 * \param n The number of the status item.
59 * \param f A format string.
60 *
61 * \return The return value of the underlying call to para_printf().
62 */
63 #define WRITE_STATUS_ITEM(b, n, f, ...) (\
64 { \
65 int _ret; \
66 if ((b)->flags & PBF_SIZE_PREFIX) { \
67 _ret = para_printf((b), "%02x:" f, n, ## __VA_ARGS__); \
68 } else { \
69 _ret = para_printf((b), "%s: " f, status_item_list[(n)], \
70 ## __VA_ARGS__); \
71 } \
72 _ret; \
73 } \
74 )
75
76 __must_check __malloc void *para_realloc(void *p, size_t size);
77 __must_check __malloc void *para_malloc(size_t size);
78 __must_check __malloc void *para_calloc(size_t size);
79 __must_check __malloc char *para_strdup(const char *s);
80
81 __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap);
82 __printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...);
83 __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
84 __must_check __malloc char *para_strcat(char *a, const char *b);
85 __must_check __malloc char *para_dirname(const char *name);
86 __must_check char *para_basename(const char *name);
87 void chop(char *buf);
88 __must_check __malloc char *para_logname(void);
89 __must_check __malloc char *para_homedir(void);
90 __malloc char *para_hostname(void);
91 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...);
92 int para_atoi64(const char *str, int64_t *result);
93 int para_atoi32(const char *str, int32_t *value);
94 int get_loglevel_by_name(const char *txt);
95 int read_size_header(const char *buf);
96 int create_argv(const char *buf, const char *delim, char ***result);
97 int create_shifted_argv(const char *buf, const char *delim, char ***result);
98 int find_arg(const char *arg, char **argv);
99 void free_argv(char **argv);
100 int para_regcomp(regex_t *preg, const char *regex, int cflags);
101 void freep(void *arg);
102 int compute_word_num(const char *buf, const char *delim, int offset);
103 char *safe_strdup(const char *src, size_t len);
104 char *key_value_copy(const char *src, size_t len, const char *key);
105 int skip_cells(const char *s, size_t cells_to_skip, size_t *result);
106 __must_check int strwidth(const char *s, size_t *result);