1 /**
2 * @file re_fmt.h Interface to formatted text functions
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6
7
8 #include <stdarg.h>
9 #include <stdio.h>
10
11
12 struct mbuf;
13
14
15 /** Defines a pointer-length string type */
16 struct pl {
17 const char *p; /**< Pointer to string */
18 size_t l; /**< Length of string */
19 };
20
21 /** Initialise a pointer-length object from a constant string */
22 #define PL(s) {(s), sizeof((s))-1}
23
24 /** Pointer-length Initializer */
25 #define PL_INIT {NULL, 0}
26
27 extern const struct pl pl_null;
28
29 void pl_set_str(struct pl *pl, const char *str);
30 void pl_set_mbuf(struct pl *pl, const struct mbuf *mb);
31 uint32_t pl_u32(const struct pl *pl);
32 uint32_t pl_x32(const struct pl *pl);
33 uint64_t pl_u64(const struct pl *pl);
34 uint64_t pl_x64(const struct pl *pl);
35 double pl_float(const struct pl *pl);
36 bool pl_isset(const struct pl *pl);
37 int pl_strcpy(const struct pl *pl, char *str, size_t size);
38 int pl_strdup(char **dst, const struct pl *src);
39 int pl_dup(struct pl *dst, const struct pl *src);
40 int pl_strcmp(const struct pl *pl, const char *str);
41 int pl_strcasecmp(const struct pl *pl, const char *str);
42 int pl_cmp(const struct pl *pl1, const struct pl *pl2);
43 int pl_casecmp(const struct pl *pl1, const struct pl *pl2);
44 const char *pl_strchr(const struct pl *pl, char c);
45
46 /** Advance pl position/length by +/- N bytes */
pl_advance(struct pl * pl,ssize_t n)47 static inline void pl_advance(struct pl *pl, ssize_t n)
48 {
49 pl->p += n;
50 pl->l -= n;
51 }
52
53
54 /* Formatted printing */
55
56 /**
57 * Defines the re_vhprintf() print handler
58 *
59 * @param p String to print
60 * @param size Size of string to print
61 * @param arg Handler argument
62 *
63 * @return 0 for success, otherwise errorcode
64 */
65 typedef int(re_vprintf_h)(const char *p, size_t size, void *arg);
66
67 /** Defines a print backend */
68 struct re_printf {
69 re_vprintf_h *vph; /**< Print handler */
70 void *arg; /**< Handler agument */
71 };
72
73 /**
74 * Defines the %H print handler
75 *
76 * @param pf Print backend
77 * @param arg Handler argument
78 *
79 * @return 0 for success, otherwise errorcode
80 */
81 typedef int(re_printf_h)(struct re_printf *pf, void *arg);
82
83 int re_vhprintf(const char *fmt, va_list ap, re_vprintf_h *vph, void *arg);
84 int re_vfprintf(FILE *stream, const char *fmt, va_list ap);
85 int re_vprintf(const char *fmt, va_list ap);
86 int re_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
87 int re_vsdprintf(char **strp, const char *fmt, va_list ap);
88
89 int re_hprintf(struct re_printf *pf, const char *fmt, ...);
90 int re_fprintf(FILE *stream, const char *fmt, ...);
91 int re_printf(const char *fmt, ...);
92 int re_snprintf(char *str, size_t size, const char *fmt, ...);
93 int re_sdprintf(char **strp, const char *fmt, ...);
94
95
96 /* Regular expressions */
97 int re_regex(const char *ptr, size_t len, const char *expr, ...);
98
99
100 /* Character functions */
101 uint8_t ch_hex(char ch);
102
103
104 /* String functions */
105 int str_hex(uint8_t *hex, size_t len, const char *str);
106 void str_ncpy(char *dst, const char *src, size_t n);
107 int str_dup(char **dst, const char *src);
108 int str_cmp(const char *s1, const char *s2);
109 int str_casecmp(const char *s1, const char *s2);
110 size_t str_len(const char *s);
111 const char *str_error(int errnum, char *buf, size_t sz);
112
113
114 /**
115 * Check if string is set
116 *
117 * @param s Zero-terminated string
118 *
119 * @return true if set, false if not set
120 */
str_isset(const char * s)121 static inline bool str_isset(const char *s)
122 {
123 return s && s[0] != '\0';
124 }
125
126
127 /* time */
128 int fmt_gmtime(struct re_printf *pf, void *ts);
129 int fmt_human_time(struct re_printf *pf, const uint32_t *seconds);
130
131
132 void hexdump(FILE *f, const void *p, size_t len);
133
134
135 /* param */
136 typedef void (fmt_param_h)(const struct pl *name, const struct pl *val,
137 void *arg);
138
139 bool fmt_param_exists(const struct pl *pl, const char *pname);
140 bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val);
141 void fmt_param_apply(const struct pl *pl, fmt_param_h *ph, void *arg);
142
143
144 /* unicode */
145 int utf8_encode(struct re_printf *pf, const char *str);
146 int utf8_decode(struct re_printf *pf, const struct pl *pl);
147 size_t utf8_byteseq(char u[4], unsigned cp);
148