Merge branch 't/map_populate'
[paraslash.git] / string.h
1 /*
2  * Copyright (C) 2006-2011 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   * Write the contents of a status item to a para_buffer.
39   *
40   * \param b The para_buffer.
41   * \param n The number of the status item.
42   * \param f A format string.
43   *
44   * \return The return value of the underlying call to para_printf().
45   */
46 #define WRITE_STATUS_ITEM(b, n, f, ...) (\
47 { \
48         int _ret; \
49         if ((b)->flags & PBF_SIZE_PREFIX) { \
50                 _ret = para_printf((b), "%02x:" f, n, ## __VA_ARGS__); \
51         } else { \
52                 _ret = para_printf((b), "%s: " f, status_item_list[(n)], \
53                         ## __VA_ARGS__); \
54         } \
55         _ret; \
56 } \
57 )
58
59 __must_check __malloc void *para_realloc(void *p, size_t size);
60 __must_check __malloc void *para_malloc(size_t size);
61 __must_check __malloc void *para_calloc(size_t size);
62 __must_check __malloc char *para_strdup(const char *s);
63 __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
64 __must_check __malloc char *para_strcat(char *a, const char *b);
65 __must_check __malloc char *para_dirname(const char *name);
66 __must_check char *para_basename(const char *name);
67 void chop(char *buf);
68 __must_check __malloc char *para_logname(void);
69 __must_check __malloc char *para_homedir(void);
70 __malloc char *para_hostname(void);
71 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...);
72 /** Used for for_each_line() and for_each_line_ro(). */
73 typedef int line_handler_t(char *, void *);
74 int for_each_line(char *buf, size_t size, line_handler_t *line_handler,
75         void *private_data);
76 int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler,
77         void *private_data);
78 int para_atoi64(const char *str, int64_t *result);
79 int para_atoi32(const char *str, int32_t *value);
80 int get_loglevel_by_name(const char *txt);
81 int read_size_header(const char *buf);
82 int create_argv(const char *buf, const char *delim, char ***result);
83 void free_argv(char **argv);
84 int para_regcomp(regex_t *preg, const char *regex, int cflags);
85 void freep(void *arg);