src/str.h
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 17:08:47 +0300
changeset 214 0d5d46ab49d5
parent 133 e2d0c0c23b39
permissions -rw-r--r--
merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
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 {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    17
    _ERR_STR_BEGIN = _ERR_STR,
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
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    23
    ERR_STR_FMT_BUF_LEN,            ///< output buffer ran out
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    24
};
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
 * 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
    28
 * buf_size bytes, including the terminating NUL.
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    29
 *
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    30
 * 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
    31
 */
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    32
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
    33
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    34
/**
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    35
 * Like str_append_num with len = -1.
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    36
 *
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    37
 * @see str_append_num()
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    38
 */
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    39
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
    40
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    41
/**
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    42
 * Like str_append, but only a single char.
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    43
 *
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    44
 * @see str_append()
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    45
 */
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    46
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
    47
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    48
/**
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    49
 * Like str_append, but using formatted input instead.
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    50
 *
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    51
 * @see str_append()
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    52
 */
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    53
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
    54
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    55
/**
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
    56
 * 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
    57
 *
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
    58
 * @see str_append_fmt()
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
    59
 */
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
    60
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
    61
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
    62
/**
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    63
 * 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
    64
 */
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    65
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
    66
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    67
/**
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    68
 * 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
    69
 *
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    70
 * Intended to be used like:
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    71
 * \code
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    72
 *  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
    73
 * \endcode
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    74
 */
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    75
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
    76
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    77
/**
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
 * 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
    79
 *
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
 * 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
    81
 *
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
 * 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
    83
 * 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
    84
 *
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
 * 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
    86
 *
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
 * @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
    88
 * @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
    89
 * @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
    90
 * @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
    91
 * @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
    92
 */
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    93
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
    94
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    95
/**
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    96
 * 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
    97
 *
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    98
 * @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
    99
 * @param value returned pointer to param value
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   100
 * @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
   101
 * @param arg the context arg given to str_format
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   102
 * @return the parameter value, or NULL to error out
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
typedef err_t (*str_format_cb) (const char *name, const char **value, ssize_t *value_len, void *arg);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   105
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   106
/**
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   107
 * Maximum length of a parameter name
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   108
 */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   109
#define STR_FORMAT_PARAM_MAX 32
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   110
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
 * Maximum length of a parameter flags section
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   113
 */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   114
#define STR_FORMAT_FLAGS_MAX 8
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   115
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
 * 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
   118
 */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   119
err_t str_format (char *buf, size_t buf_size, const char *format, str_format_cb func, void *arg);
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
#endif