src/lib/str.c
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 00:35:02 +0300
branchnew-lib-errors
changeset 218 5229a5d098b2
parent 216 a10ba529ae39
permissions -rw-r--r--
some of spbot and lib compiles
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
#include "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
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <ctype.h>
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <stdio.h>
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <string.h>
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
     6
#include <stdbool.h>
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
     7
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include <assert.h>
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    10
const struct error_list str_errors = ERROR_LIST("str",
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    11
    ERROR_TYPE(     ERR_STR_FMT_TAG,            "invalid parameter tag syntax"      ),
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    12
    ERROR_TYPE(     ERR_STR_FMT_NAME_LEN,       "invalid parameter name length"     ),
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    13
    ERROR_TYPE(     ERR_STR_FMT_NAME,           "invalid/unknown parameter name"    ),
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    14
    ERROR_TYPE(     ERR_STR_FMT_FLAGS_LEN,      "invalid paramter flags length"     ),
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    15
    ERROR_TYPE(     ERR_STR_FMT_FLAG,           "invalid paramter flag"             ),
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    16
    ERROR_TYPE(     ERR_STR_FMT_BUF_LEN,        "output buffer ran out"             )
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    17
);
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    18
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    19
size_t str_append_num (char *buf, size_t buf_size, const char *str, ssize_t len)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    size_t str_len;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    // copy the bytes
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    24
    // len will either be >0 or <0
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    25
    for (str_len = 0; *str && buf_size > 1 && len != 0; str_len++, buf_size--, len--)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        *buf++ = *str++;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    // count the rest
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    29
    for (; *str && len != 0; str++, len--)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        str_len++;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    if (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
    33
        // NUL-terminate
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        *buf = '\0';
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    // ok
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    return str_len;
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    38
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
size_t str_append (char *buf, size_t buf_size, const char *str)
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
    return str_append_num(buf, buf_size, str, -1);
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
128
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)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    if (buf_size > 1)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        *buf++ = c;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    if (buf_size > 0)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        *buf = '\0';
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    return 1;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    57
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: 132
diff changeset
    58
{
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    59
    int ret;
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    60
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    61
    // do the formatting
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    62
    ret = vsnprintf(buf, buf_size, fmt, vargs);
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    63
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    64
    // XXX: not all vsnprintf implementations do this
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    65
    assert(ret >= 0);
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    66
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    67
    // ok
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    68
    return ret;
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    69
}
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    70
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    71
size_t str_append_fmt (char *buf, size_t buf_size, const char *fmt, ...)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    va_list vargs;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    int ret;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    va_start(vargs, fmt);
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    77
    ret = str_append_fmt_va(buf, buf_size, fmt, vargs);
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
    va_end(vargs);
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
    return ret;
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
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
static const struct str_quote_item {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
    /** The char value to quote */
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    char c;
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
    /** The quoted value */
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    const char *out;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
} _quote_table[] = {
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
    { '\\', "\\\\"  },
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    { '\0', "\\0"   },
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    { '\r', "\\r"   },
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    { '\n', "\\n"   },
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    { 0,    NULL    }
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
};
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    98
size_t str_quote_char (char *buf, size_t buf_size, char c)
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
    const struct str_quote_item *quote_item = _quote_table;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    // special quote?
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    for (quote_item = _quote_table; quote_item->c || quote_item->out; quote_item++) {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
        if (quote_item->c == c)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
            return str_append(buf, buf_size, quote_item->out);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    }
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    // output directly?
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    if (isprint(c))
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
        return str_append_char(buf, buf_size, c); 
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    else 
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        return str_append_fmt(buf, buf_size, "\\x%02x", c);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
size_t str_advance (size_t *data_size, size_t *buf_size, size_t len)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    if (data_size)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
        *data_size += len;
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
    if (len > *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
   122
        *buf_size = 0;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
    else
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
        *buf_size -= len;
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
    return len;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   130
/**
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   131
 * Number of bytes reserved by str_quote for the trailing overflow stuff
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   132
 */
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
#define STR_QUOTE_RESERVED 5
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
size_t str_quote (char *buf, size_t buf_size, const char *str, ssize_t str_len)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    size_t data_size = 0;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    // NULL?
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    if (str == NULL) {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
        return str_append(buf, buf_size, "NULL");
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    } else {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        // calc length?
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
        if (str_len < 0)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
            str_len = strlen(str);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
        // quote
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
        buf += str_advance(&data_size, &buf_size, str_append_char(buf, 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
   150
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
        // dump each char
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
        while (str_len--) { 
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
            if (buf_size > STR_QUOTE_RESERVED) {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
                size_t char_size;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
                // output and count the chars
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
                char_size = str_quote_char(buf, buf_size, *str++);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
                
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
                // if there's still enough room left, commit this and continue
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
                if (buf_size > char_size && buf_size - char_size >= STR_QUOTE_RESERVED)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
                    buf += str_advance(&data_size, &buf_size, char_size);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
                
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
                else
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
                    // keep the buf pointer intact, we'll overwrite it now
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
                    data_size += char_size;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
            } else {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
                // continue counting the chars, but don't output anything anymore
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
                data_size += str_quote_char(NULL, 0, *str++);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
            }
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
        }
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
        // end quote
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
        buf += str_advance(&data_size, &buf_size, str_append_char(buf, 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
   175
    }        
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
    // overflow -> mark
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
    if (buf_size < STR_QUOTE_RESERVED)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
        buf += str_advance(NULL, &buf_size, str_append(buf, 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
   180
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
    // the total outputted chars
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    return data_size;
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   185
/**
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   186
 * Output the data for a single parameter
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   187
 */
218
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 216
diff changeset
   188
static err_t str_format_param (char **buf, size_t *buf_size, const char *name, const char *flags, str_format_cb func, void *arg, error_t *err)
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   189
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   190
    const char *value;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   191
    ssize_t value_len = -1;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   192
    char flag;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   193
    bool use_quote = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   194
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   195
    // look it up
218
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 216
diff changeset
   196
    if (func(name, &value, &value_len, arg, err))
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 216
diff changeset
   197
        return PUSH_ERROR(err, &str_errors, ERR_STR_FMT_VALUE);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   198
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   199
    // not found?
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   200
    if (!value)
218
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 216
diff changeset
   201
        return SET_ERROR(err, &str_errors, ERR_STR_FMT_NAME);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   202
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   203
    // parse flags
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   204
    while ((flag = *flags++)) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   205
        switch (flag) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   206
            case 'r':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   207
                // quote
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   208
                use_quote = true;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   209
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   210
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   211
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   212
            default:
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   213
                // unknown flag
218
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 216
diff changeset
   214
                return SET_ERROR(err, &str_errors, ERR_STR_FMT_FLAG);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   215
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   216
        }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   217
    }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   218
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   219
    // output value
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   220
    if (use_quote)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   221
        *buf += str_advance(NULL, buf_size, str_quote(*buf, *buf_size, value, value_len));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   222
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   223
    else
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   224
        *buf += str_advance(NULL, buf_size, str_append_num(*buf, *buf_size, value, value_len));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   225
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   226
    // ok
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   227
    return SUCCESS;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   228
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   229
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   230
err_t str_format (char *buf, size_t buf_size, const char *format, str_format_cb func, void *arg, error_t *err)
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   231
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   232
    char name_buf[STR_FORMAT_PARAM_MAX + 1], *name_ptr;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   233
    size_t name_size;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   234
    char flags_buf[STR_FORMAT_FLAGS_MAX + 1], *flags_ptr;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   235
    size_t flags_size;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   236
    bool in_param = false, in_param_flags = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   237
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   238
    // iterate over the format string
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   239
    do {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   240
        // check buffer state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   241
        if (!buf_size)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   242
            return SET_ERROR(err, &str_errors, ERR_STR_FMT_BUF_LEN);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   243
        
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   244
        // inspect this char
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   245
        switch (*format) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   246
            case '{':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   247
                // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   248
                if (in_param)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   249
                    return SET_ERROR(err, &str_errors, ERR_STR_FMT_TAG);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   250
                
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   251
                // init state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   252
                in_param = true;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   253
                name_ptr = name_buf;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   254
                name_size = sizeof(name_buf);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   255
                flags_ptr = flags_buf;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   256
                flags_size = sizeof(flags_buf);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   257
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   258
                *name_ptr = *flags_ptr = '\0';
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   259
                
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   260
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   261
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   262
            case '}':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   263
                // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   264
                if (!in_param)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   265
                    return SET_ERROR(err, &str_errors, ERR_STR_FMT_TAG);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   266
                
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   267
                // reset state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   268
                in_param = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   269
                in_param_flags = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   270
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   271
                // output token
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   272
                if (str_format_param(&buf, &buf_size, name_buf, flags_buf, func, arg, err))
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   273
                    return error_code(err);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   274
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   275
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   276
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   277
            case ':':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   278
                if (in_param) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   279
                    // set state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   280
                    in_param_flags = true;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   281
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   282
                    break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   283
                }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   284
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   285
                /* Fallthrough */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   286
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   287
            default:
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   288
                if (in_param && in_param_flags ) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   289
                    // add to param flags
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   290
                    flags_ptr += str_advance(NULL, &flags_size, str_append_char(flags_ptr, flags_size, *format));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   291
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   292
                    if (!flags_size)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   293
                        return SET_ERROR(err, &str_errors, ERR_STR_FMT_FLAGS_LEN);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   294
               
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   295
                } else if (in_param) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   296
                    // add to param name
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   297
                    name_ptr += str_advance(NULL, &name_size, str_append_char(name_ptr, name_size, *format));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   298
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   299
                    if (!name_size)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   300
                        return SET_ERROR(err, &str_errors, ERR_STR_FMT_NAME_LEN);
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   301
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   302
                } else {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   303
                    // add to output
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   304
                    buf += str_advance(NULL, &buf_size, str_append_char(buf, buf_size, *format));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   305
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   306
                }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   307
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   308
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   309
        }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   310
    } while (*format++);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   311
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   312
    // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   313
    if (in_param)
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   314
        return SET_ERROR(err, &str_errors, ERR_STR_FMT_TAG);
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   315
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   316
    // ok
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   317
    return SUCCESS;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   318
}
216
a10ba529ae39 initial error code
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   319