src/str.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:23:50 +0300
branchlua-threads
changeset 207 3fa22abb5421
parent 133 e2d0c0c23b39
permissions -rw-r--r--
fix lua_nexus_sleep to use EV_TIMEOUT + misc
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
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    10
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
    11
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    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
    13
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    // copy the bytes
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    15
    // len will either be >0 or <0
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    16
    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
    17
        *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
    18
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    // count the rest
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    20
    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
    21
        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
    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
    24
        // 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
    25
        *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
    26
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    // ok
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    return str_len;
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    29
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    30
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    31
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    32
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
    33
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
    34
    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
    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
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    37
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
    38
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    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
    40
        *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
    41
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    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
    43
        *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
    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
    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
    46
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    48
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
    49
{
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    50
    int ret;
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    51
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    52
    // do the formatting
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    53
    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
    54
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    55
    // 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
    56
    assert(ret >= 0);
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    57
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    58
    // ok
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    59
    return 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
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    62
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
    63
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    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
    65
    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
    66
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
    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
    68
    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
    69
    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
    70
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    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
    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
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
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
    75
    /** 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
    76
    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
    77
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    /** 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
    79
    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
    80
} _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
    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
    { '\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
    84
    { '\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
    85
    { '\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
    86
    { 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
    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
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
    89
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
    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
    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
    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
    // 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
    94
    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
    95
        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
    96
            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
    97
    }
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    // 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
   100
    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
   101
        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
   102
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    else 
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
        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
   105
}
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
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
   108
{
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 (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
   110
        *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
   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
    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
   113
        *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
   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
    else
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        *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
   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
    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
   119
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   121
/**
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   122
 * 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
   123
 */
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
#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
   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
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
   127
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    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
   129
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    // NULL?
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
    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
   132
        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
   133
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
    } else {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
        // 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
   136
        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
   137
            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
   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
        // quote
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
        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
   141
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
        // 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
   143
        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
   144
            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
   145
                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
   146
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
                // 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
   148
                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
   149
                
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
                // 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
   151
                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
   152
                    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
   153
                
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
                else
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
                    // 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
   156
                    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
   157
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
            } else {
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
                // 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
   160
                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
   161
            }
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
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
        // 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
   165
        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
   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
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    // 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
   169
    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
   170
        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
   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
    // 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
   173
    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
   174
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   176
/**
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   177
 * Output the data for a single parameter
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   178
 */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   179
static err_t str_format_param (char **buf, size_t *buf_size, const char *name, const char *flags, str_format_cb func, void *arg)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   180
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   181
    const char *value;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   182
    ssize_t value_len = -1;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   183
    char flag;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   184
    bool use_quote = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   185
    err_t err;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   186
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   187
    // look it up
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   188
    if ((err = func(name, &value, &value_len, arg)))
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   189
        return err;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   190
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   191
    // not found?
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   192
    if (!value)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   193
        return ERR_STR_FMT_NAME;
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
    // parse flags
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   196
    while ((flag = *flags++)) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   197
        switch (flag) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   198
            case 'r':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   199
                // quote
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   200
                use_quote = true;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   201
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   202
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   203
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   204
            default:
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   205
                // unknown flag
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   206
                return ERR_STR_FMT_FLAG;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   207
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   208
        }
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
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   211
    // output value
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   212
    if (use_quote)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   213
        *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
   214
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   215
    else
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   216
        *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
   217
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   218
    // ok
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   219
    return SUCCESS;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   220
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   221
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   222
err_t str_format (char *buf, size_t buf_size, const char *format, str_format_cb func, void *arg)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   223
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   224
    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
   225
    size_t name_size;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   226
    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
   227
    size_t flags_size;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   228
    bool in_param = false, in_param_flags = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   229
    err_t err;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   230
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   231
    // iterate over the format string
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   232
    do {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   233
        // check buffer state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   234
        if (!buf_size)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   235
            return ERR_STR_FMT_BUF_LEN;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   236
        
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   237
        // inspect this char
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   238
        switch (*format) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   239
            case '{':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   240
                // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   241
                if (in_param)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   242
                    return ERR_STR_FMT_TAG;
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
                // init state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   245
                in_param = true;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   246
                name_ptr = name_buf;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   247
                name_size = sizeof(name_buf);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   248
                flags_ptr = flags_buf;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   249
                flags_size = sizeof(flags_buf);
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
                *name_ptr = *flags_ptr = '\0';
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   252
                
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   253
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   254
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   255
            case '}':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   256
                // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   257
                if (!in_param)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   258
                    return ERR_STR_FMT_TAG;
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
                // reset state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   261
                in_param = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   262
                in_param_flags = false;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   263
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   264
                // output token
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   265
                if ((err = str_format_param(&buf, &buf_size, name_buf, flags_buf, func, arg)))
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   266
                    return err;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   267
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   268
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   269
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   270
            case ':':
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   271
                if (in_param) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   272
                    // set state
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   273
                    in_param_flags = true;
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
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   278
                /* Fallthrough */
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   279
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   280
            default:
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   281
                if (in_param && in_param_flags ) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   282
                    // add to param flags
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   283
                    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
   284
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   285
                    if (!flags_size)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   286
                        return ERR_STR_FMT_FLAGS_LEN;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   287
               
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   288
                } else if (in_param) {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   289
                    // add to param name
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   290
                    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
   291
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   292
                    if (!name_size)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   293
                        return ERR_STR_FMT_NAME_LEN;
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   294
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   295
                } else {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   296
                    // add to output
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   297
                    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
   298
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   299
                }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   300
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   301
                break;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   302
        }
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   303
    } while (*format++);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   304
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   305
    // syntax
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   306
    if (in_param)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   307
        return ERR_STR_FMT_TAG;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   308
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   309
    // ok
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   310
    return SUCCESS;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   311
}