Subversion Repositories HelenOS

Rev

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

Rev 3022 Rev 4201
Line 24... Line 24...
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup generic
29
/** @addtogroup generic
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <print.h>
35
#include <print.h>
36
#include <printf/printf_core.h>
36
#include <printf/printf_core.h>
-
 
37
#include <string.h>
37
#include <memstr.h>
38
#include <memstr.h>
38
 
39
 
39
struct vsnprintf_data {
40
typedef struct {
40
    size_t size; /* total space for string */
41
    size_t size;    /* Total size of the buffer (in bytes) */
41
    size_t len; /* count of currently used characters */
42
    size_t len;     /* Number of already used bytes */
42
    char *string; /* destination string */
43
    char *dst;      /* Destination */
43
};
44
} vsnprintf_data_t;
44
 
45
 
45
/** Write string to given buffer.
46
/** Write UTF-8 string to given buffer.
-
 
47
 *
46
 * Write at most data->size characters including trailing zero. According to C99, snprintf() has to return number
48
 * Write at most data->size plain characters including trailing zero.
-
 
49
 * According to C99, snprintf() has to return number of characters that
47
 * of characters that would have been written if enough space had been available. Hence the return value is not
50
 * would have been written if enough space had been available. Hence
48
 * number of really printed characters but size of the input string. Number of really used characters
51
 * the return value is not the number of actually printed characters
49
 * is stored in data->len.
52
 * but size of the input string.
-
 
53
 *
50
 * @param str source string to print
54
 * @param str  Source UTF-8 string to print.
51
 * @param count size of source string
55
 * @param size Number of plain characters in str.
52
 * @param data structure with destination string, counter of used space and total string size.
56
 * @param data Structure describing destination string, counter
-
 
57
 *             of used space and total string size.
-
 
58
 *
53
 * @return number of characters to print (not characters really printed!)
59
 * @return Number of UTF-8 characters to print (not characters actually
-
 
60
 *         printed).
-
 
61
 *
54
 */
62
 */
55
static int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
63
static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data)
56
{
64
{
57
    size_t i;
-
 
58
    i = data->size - data->len;
65
    size_t left = data->size - data->len;
59
 
-
 
60
    if (i == 0) {
-
 
61
        return count;
-
 
62
    }
-
 
63
   
66
   
-
 
67
    if (left == 0)
-
 
68
        return ((int) size);
-
 
69
   
64
    if (i == 1) {
70
    if (left == 1) {
65
        /* We have only one free byte left in buffer => write there trailing zero */
71
        /* We have only one free byte left in buffer
-
 
72
         * -> store trailing zero
-
 
73
         */
66
        data->string[data->size - 1] = 0;
74
        data->dst[data->size - 1] = 0;
67
        data->len = data->size;
75
        data->len = data->size;
68
        return count;
76
        return ((int) size);
69
    }
77
    }
70
   
78
   
71
    if (i <= count) {
79
    if (left <= size) {
-
 
80
        /* We have not enought space for whole string
72
        /* We have not enought space for whole string with the trailing zero => print only a part of string */
81
         * with the trailing zero => print only a part
-
 
82
         * of string
-
 
83
         */
-
 
84
        index_t index = 0;
-
 
85
       
-
 
86
        while (index < size) {
-
 
87
            wchar_t uc = chr_decode(str, &index, size);
-
 
88
 
73
            memcpy((void *)(data->string + data->len), (void *)str, i - 1);
89
            if (!chr_encode(uc, data->dst, &data->len, data->size - 1))
-
 
90
                break;
-
 
91
        }
-
 
92
       
74
            data->string[data->size - 1] = 0;
93
        /* Put trailing zero at end, but not count it
-
 
94
         * into data->len so it could be rewritten next time
-
 
95
         */
75
            data->len = data->size;
96
        data->dst[data->len] = 0;
-
 
97
       
76
            return count;
98
        return ((int) size);
77
    }
99
    }
78
   
100
   
79
    /* Buffer is big enought to print whole string */
101
    /* Buffer is big enought to print the whole string */
80
    memcpy((void *)(data->string + data->len), (void *)str, count);
102
    memcpy((void *)(data->dst + data->len), (void *) str, size);
81
    data->len += count;
103
    data->len += size;
-
 
104
   
-
 
105
    /* Put trailing zero at end, but not count it
82
    /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
106
     * into data->len so it could be rewritten next time
-
 
107
     */
83
    data->string[data->len] = 0;
108
    data->dst[data->len] = 0;
-
 
109
   
-
 
110
    return ((int) size);
-
 
111
}
84
 
112
 
-
 
113
/** Write UTF-32 string to given buffer.
-
 
114
 *
-
 
115
 * Write at most data->size plain characters including trailing zero.
-
 
116
 * According to C99, snprintf() has to return number of characters that
-
 
117
 * would have been written if enough space had been available. Hence
-
 
118
 * the return value is not the number of actually printed characters
-
 
119
 * but size of the input string.
-
 
120
 *
-
 
121
 * @param str  Source UTF-32 string to print.
-
 
122
 * @param size Number of bytes in str.
-
 
123
 * @param data Structure describing destination string, counter
-
 
124
 *             of used space and total string size.
-
 
125
 *
-
 
126
 * @return Number of UTF-8 characters to print (not characters actually
-
 
127
 *         printed).
-
 
128
 *
-
 
129
 */
-
 
130
static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data)
-
 
131
{
-
 
132
    index_t index = 0;
-
 
133
   
-
 
134
    while (index < (size / sizeof(wchar_t))) {
-
 
135
        size_t left = data->size - data->len;
-
 
136
       
-
 
137
        if (left == 0)
-
 
138
            return ((int) size);
-
 
139
       
-
 
140
        if (left == 1) {
-
 
141
            /* We have only one free byte left in buffer
-
 
142
             * -> store trailing zero
-
 
143
             */
-
 
144
            data->dst[data->size - 1] = 0;
-
 
145
            data->len = data->size;
-
 
146
            return ((int) size);
-
 
147
        }
-
 
148
       
-
 
149
        if (!chr_encode(str[index], data->dst, &data->len, data->size - 1))
-
 
150
            break;
-
 
151
       
-
 
152
        index++;
-
 
153
    }
-
 
154
   
-
 
155
    /* Put trailing zero at end, but not count it
-
 
156
     * into data->len so it could be rewritten next time
-
 
157
     */
-
 
158
    data->dst[data->len] = 0;
-
 
159
   
85
    return count;  
160
    return ((int) size);
86
}
161
}
87
 
162
 
88
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
163
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
89
{
164
{
90
    struct vsnprintf_data data = {size, 0, str};
165
    vsnprintf_data_t data = {
-
 
166
        size,
-
 
167
        0,
-
 
168
        str
-
 
169
    };
-
 
170
    printf_spec_t ps = {
-
 
171
        (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8,
91
    struct printf_spec ps = {(int(*)(void *, size_t, void *))vsnprintf_write, &data};
172
        (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32,
-
 
173
        &data
-
 
174
    };
92
 
175
   
93
    /* Print 0 at end of string - fix the case that nothing will be printed */
176
    /* Print 0 at end of string - fix the case that nothing will be printed */
94
    if (size > 0)
177
    if (size > 0)
95
        str[0] = 0;
178
        str[0] = 0;
96
   
179
   
97
    /* vsnprintf_write ensures that str will be terminated by zero. */
180
    /* vsnprintf_write ensures that str will be terminated by zero. */