Subversion Repositories HelenOS

Rev

Rev 4208 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4208 Rev 4213
Line 42... Line 42...
42
    size_t size;    /* Total size of the buffer (in bytes) */
42
    size_t size;    /* Total size of the buffer (in bytes) */
43
    size_t len;     /* Number of already used bytes */
43
    size_t len;     /* Number of already used bytes */
44
    char *dst;      /* Destination */
44
    char *dst;      /* Destination */
45
} vsnprintf_data_t;
45
} vsnprintf_data_t;
46
 
46
 
47
/** Write UTF-8 string to given buffer.
47
/** Write string to given buffer.
48
 *
48
 *
49
 * Write at most data->size plain characters including trailing zero.
49
 * Write at most data->size plain characters including trailing zero.
50
 * According to C99, snprintf() has to return number of characters that
50
 * According to C99, snprintf() has to return number of characters that
51
 * would have been written if enough space had been available. Hence
51
 * would have been written if enough space had been available. Hence
52
 * the return value is not the number of actually printed characters
52
 * the return value is not the number of actually printed characters
53
 * but size of the input string.
53
 * but size of the input string.
54
 *
54
 *
55
 * @param str  Source UTF-8 string to print.
55
 * @param str  Source string to print.
56
 * @param size Number of plain characters in str.
56
 * @param size Number of plain characters in str.
57
 * @param data Structure describing destination string, counter
57
 * @param data Structure describing destination string, counter
58
 *             of used space and total string size.
58
 *             of used space and total string size.
59
 *
59
 *
60
 * @return Number of UTF-8 characters to print (not characters actually
60
 * @return Number of characters to print (not characters actually
61
 *         printed).
61
 *         printed).
62
 *
62
 *
63
 */
63
 */
64
static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data)
64
static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data)
65
{
65
{
66
    size_t left = data->size - data->len;
66
    size_t left = data->size - data->len;
67
   
67
   
68
    if (left == 0)
68
    if (left == 0)
69
        return ((int) size);
69
        return ((int) size);
Line 83... Line 83...
83
         * of string
83
         * of string
84
         */
84
         */
85
        index_t index = 0;
85
        index_t index = 0;
86
       
86
       
87
        while (index < size) {
87
        while (index < size) {
88
            wchar_t uc = chr_decode(str, &index, size);
88
            wchar_t uc = str_decode(str, &index, size);
89
 
89
           
90
            if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
90
            if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
91
                break;
91
                break;
92
        }
92
        }
93
       
93
       
94
        /* Put trailing zero at end, but not count it
94
        /* Put trailing zero at end, but not count it
Line 109... Line 109...
109
    data->dst[data->len] = 0;
109
    data->dst[data->len] = 0;
110
   
110
   
111
    return ((int) size);
111
    return ((int) size);
112
}
112
}
113
 
113
 
114
/** Write UTF-32 string to given buffer.
114
/** Write wide string to given buffer.
115
 *
115
 *
116
 * Write at most data->size plain characters including trailing zero.
116
 * Write at most data->size plain characters including trailing zero.
117
 * According to C99, snprintf() has to return number of characters that
117
 * According to C99, snprintf() has to return number of characters that
118
 * would have been written if enough space had been available. Hence
118
 * would have been written if enough space had been available. Hence
119
 * the return value is not the number of actually printed characters
119
 * the return value is not the number of actually printed characters
120
 * but size of the input string.
120
 * but size of the input string.
121
 *
121
 *
122
 * @param str  Source UTF-32 string to print.
122
 * @param str  Source wide string to print.
123
 * @param size Number of bytes in str.
123
 * @param size Number of bytes in str.
124
 * @param data Structure describing destination string, counter
124
 * @param data Structure describing destination string, counter
125
 *             of used space and total string size.
125
 *             of used space and total string size.
126
 *
126
 *
127
 * @return Number of UTF-8 characters to print (not characters actually
127
 * @return Number of wide characters to print (not characters actually
128
 *         printed).
128
 *         printed).
129
 *
129
 *
130
 */
130
 */
131
static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data)
131
static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
132
{
132
{
133
    index_t index = 0;
133
    index_t index = 0;
134
   
134
   
135
    while (index < (size / sizeof(wchar_t))) {
135
    while (index < (size / sizeof(wchar_t))) {
136
        size_t left = data->size - data->len;
136
        size_t left = data->size - data->len;
Line 167... Line 167...
167
        size,
167
        size,
168
        0,
168
        0,
169
        str
169
        str
170
    };
170
    };
171
    printf_spec_t ps = {
171
    printf_spec_t ps = {
172
        (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8,
172
        (int(*) (const char *, size_t, void *)) vsnprintf_str_write,
173
        (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32,
173
        (int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write,
174
        &data
174
        &data
175
    };
175
    };
176
   
176
   
177
    /* Print 0 at end of string - fix the case that nothing will be printed */
177
    /* Print 0 at end of string - fix the case that nothing will be printed */
178
    if (size > 0)
178
    if (size > 0)