author | Tero Marttila <terom@fixme.fi> |
Thu, 28 May 2009 00:35:02 +0300 | |
branch | new-lib-errors |
changeset 218 | 5229a5d098b2 |
parent 216 | a10ba529ae39 |
permissions | -rw-r--r-- |
125
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#ifndef STR_H |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#define STR_H |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
/** |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
* @file |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
* |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
* Miscellaneous string utility functions |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
*/ |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
#include <sys/types.h> |
133
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
10 |
#include <stdarg.h> |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
11 |
#include "error.h" |
125
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
|
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
/** |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
14 |
* Error codes |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
15 |
*/ |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
16 |
enum str_error_code { |
216 | 17 |
ERR_STR_NONE, |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
18 |
ERR_STR_FMT_TAG, ///< invalid parameter tag syntax |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
19 |
ERR_STR_FMT_NAME_LEN, ///< invalid parameter name length |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
20 |
ERR_STR_FMT_NAME, ///< invalid/unknown parameter name |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
21 |
ERR_STR_FMT_FLAGS_LEN, ///< invalid paramter flags length |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
22 |
ERR_STR_FMT_FLAG, ///< invalid paramter flag |
218
5229a5d098b2
some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents:
216
diff
changeset
|
23 |
ERR_STR_FMT_VALUE, ///< parameter value |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
24 |
ERR_STR_FMT_BUF_LEN, ///< output buffer ran out |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
25 |
}; |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
26 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
27 |
/** |
216 | 28 |
* Error list |
29 |
*/ |
|
30 |
const struct error_list str_errors; |
|
31 |
||
32 |
/** |
|
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
33 |
* Writes the given string into the given buffer, reading at most len bytes (or up to NUL if -1), and writing at most |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
34 |
* buf_size bytes, including the terminating NUL. |
128
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
35 |
* |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
36 |
* Returns the number of input bytes that would have been written, not including the terminating NUL. |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
37 |
*/ |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
38 |
size_t str_append_num (char *buf, size_t buf_size, const char *str, ssize_t len); |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
39 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
40 |
/** |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
41 |
* Like str_append_num with len = -1. |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
42 |
* |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
43 |
* @see str_append_num() |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
44 |
*/ |
128
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
45 |
size_t str_append (char *buf, size_t buf_size, const char *str); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
46 |
|
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
47 |
/** |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
48 |
* Like str_append, but only a single char. |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
49 |
* |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
50 |
* @see str_append() |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
51 |
*/ |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
52 |
size_t str_append_char (char *buf, size_t buf_size, char c); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
53 |
|
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
54 |
/** |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
55 |
* Like str_append, but using formatted input instead. |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
56 |
* |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
57 |
* @see str_append() |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
58 |
*/ |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
59 |
size_t str_append_fmt (char *buf, size_t buf_size, const char *fmt, ...); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
60 |
|
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
61 |
/** |
133
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
62 |
* Like str_append_fmt, but using a vargs list instead. |
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
63 |
* |
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
64 |
* @see str_append_fmt() |
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
65 |
*/ |
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
66 |
size_t str_append_fmt_va (char *buf, size_t buf_size, const char *fmt, va_list vargs); |
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
67 |
|
e2d0c0c23b39
implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents:
129
diff
changeset
|
68 |
/** |
128
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
69 |
* Use str_append* to write out the quoted char value to the given buffer. |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
70 |
*/ |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
71 |
size_t str_quote_char (char *buf, size_t buf_size, char c); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
72 |
|
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
73 |
/** |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
74 |
* Update data_size to add len (if given), then update buf_size to remove to min(buf_size, len), and return len. |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
75 |
* |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
76 |
* Intended to be used like: |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
77 |
* \code |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
78 |
* buf += str_advance(&data_size, &buf_size, str_append_fmt(buf, buf_size, "...", ...)); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
79 |
* \endcode |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
80 |
*/ |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
81 |
size_t str_advance (size_t *data_size, size_t *buf_size, size_t len); |
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
82 |
|
6c5a5fdfd1d5
update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents:
125
diff
changeset
|
83 |
/** |
125
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
* Copy the given \a str into \buf, surrounding it with quotes and escaping any data inside. |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
* |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
* At most \a str_len bytes of input will be read, unless given as -1, whereupon str will be read up to the first \0 byte. |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
* |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
* At most \a buf_size bytes of output will be written, if the output string was truncated, it will end in '...', and a |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
* value larger than \a buf_size will be returned. |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
* |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
* As a special case, if \a str is NULL, only the string "NULL" will be output. |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
* |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
* @param buf the buffer to write the output to |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
* @param buf_size the size of the given output buffer |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
* @param str the input string |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
* @param str_len number of bytes of input to process, or -1 to use strlen() |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
* @return the total number of bytes that would have been written out, may be more than buf_size |
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
*/ |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
99 |
size_t str_quote (char *buf, size_t buf_size, const char *str, ssize_t str_len); |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
100 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
101 |
/** |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
102 |
* Callback function used by str_format to look up a value for a parameter. |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
103 |
* |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
104 |
* @param name the name of the paramter in the format string |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
105 |
* @param value returned pointer to param value |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
106 |
* @param value_len returned param value length, or -1 for strlen |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
107 |
* @param arg the context arg given to str_format |
218
5229a5d098b2
some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents:
216
diff
changeset
|
108 |
* @param err returned error info |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
109 |
*/ |
218
5229a5d098b2
some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents:
216
diff
changeset
|
110 |
typedef err_t (*str_format_cb) (const char *name, const char **value, ssize_t *value_len, void *arg, error_t *err); |
129
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
111 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
112 |
/** |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
113 |
* Maximum length of a parameter name |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
114 |
*/ |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
115 |
#define STR_FORMAT_PARAM_MAX 32 |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
116 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
117 |
/** |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
118 |
* Maximum length of a parameter flags section |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
119 |
*/ |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
120 |
#define STR_FORMAT_FLAGS_MAX 8 |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
121 |
|
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
122 |
/** |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
123 |
* Format an output string based on the given template, filling in parameter values using a callback function. |
361740b82fe5
implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents:
128
diff
changeset
|
124 |
*/ |
216 | 125 |
err_t str_format (char *buf, size_t buf_size, const char *format, str_format_cb func, void *arg, error_t *err); |
125
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
|
5c70fb2d6793
move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
#endif |