Subversion Repositories HelenOS

Rev

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

Rev 2787 Rev 4377
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>
-
 
39
#include <errno.h>
38
 
40
 
39
struct vsnprintf_data {
41
typedef struct {
40
    size_t size; /* total space for string */
42
    size_t size;    /* Total size of the buffer (in bytes) */
41
    size_t len; /* count of currently used characters */
43
    size_t len;     /* Number of already used bytes */
42
    char *string; /* destination string */
44
    char *dst;      /* Destination */
43
};
45
} vsnprintf_data_t;
44
 
46
 
45
/** Write string to given buffer.
47
/** Write string to given buffer.
-
 
48
 *
46
 * Write at most data->size characters including trailing zero. According to C99, snprintf() has to return number
49
 * Write at most data->size plain characters including trailing zero.
-
 
50
 * 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
51
 * 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
52
 * the return value is not the number of actually printed characters
49
 * is stored in data->len.
53
 * but size of the input string.
-
 
54
 *
50
 * @param str source string to print
55
 * @param str  Source string to print.
51
 * @param count size of source string
56
 * @param size Number of plain characters in str.
52
 * @param data structure with destination string, counter of used space and total string size.
57
 * @param data Structure describing destination string, counter
-
 
58
 *             of used space and total string size.
-
 
59
 *
53
 * @return number of characters to print (not characters really printed!)
60
 * @return Number of characters to print (not characters actually
-
 
61
 *         printed).
-
 
62
 *
54
 */
63
 */
55
static int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
64
static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data)
56
{
65
{
57
    size_t i;
-
 
58
    i = data->size - data->len;
66
    size_t left = data->size - data->len;
59
 
-
 
60
    if (i == 0) {
-
 
61
        return count;
-
 
62
    }
-
 
63
   
67
   
-
 
68
    if (left == 0)
-
 
69
        return ((int) size);
-
 
70
   
64
    if (i == 1) {
71
    if (left == 1) {
65
        /* We have only one free byte left in buffer => write there trailing zero */
72
        /* We have only one free byte left in buffer
-
 
73
         * -> store trailing zero
-
 
74
         */
66
        data->string[data->size - 1] = 0;
75
        data->dst[data->size - 1] = 0;
67
        data->len = data->size;
76
        data->len = data->size;
68
        return count;
77
        return ((int) size);
69
    }
78
    }
70
   
79
   
71
    if (i <= count) {
80
    if (left <= size) {
-
 
81
        /* We do not have enough space for the whole string
72
        /* We have not enought space for whole string with the trailing zero => print only a part of string */
82
         * with the trailing zero => print only a part
-
 
83
         * of string
-
 
84
         */
-
 
85
        index_t index = 0;
-
 
86
       
-
 
87
        while (index < size) {
-
 
88
            wchar_t uc = str_decode(str, &index, size);
-
 
89
           
73
            memcpy((void *)(data->string + data->len), (void *)str, i - 1);
90
            if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
-
 
91
                break;
-
 
92
        }
-
 
93
       
74
            data->string[data->size - 1] = 0;
94
        /* Put trailing zero at end, but not count it
-
 
95
         * into data->len so it could be rewritten next time
-
 
96
         */
75
            data->len = data->size;
97
        data->dst[data->len] = 0;
-
 
98
       
76
            return count;
99
        return ((int) size);
77
    }
100
    }
78
   
101
   
79
    /* Buffer is big enought to print whole string */
102
    /* Buffer is big enought to print the whole string */
80
    memcpy((void *)(data->string + data->len), (void *)str, count);
103
    memcpy((void *)(data->dst + data->len), (void *) str, size);
81
    data->len += count;
104
    data->len += size;
-
 
105
   
-
 
106
    /* 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 */
107
     * into data->len so it could be rewritten next time
-
 
108
     */
83
    data->string[data->len] = 0;
109
    data->dst[data->len] = 0;
-
 
110
   
-
 
111
    return ((int) size);
-
 
112
}
84
 
113
 
-
 
114
/** Write wide string to given buffer.
-
 
115
 *
-
 
116
 * Write at most data->size plain characters including trailing zero.
-
 
117
 * According to C99, snprintf() has to return number of characters that
-
 
118
 * would have been written if enough space had been available. Hence
-
 
119
 * the return value is not the number of actually printed characters
-
 
120
 * but size of the input string.
-
 
121
 *
-
 
122
 * @param str  Source wide string to print.
-
 
123
 * @param size Number of bytes in str.
-
 
124
 * @param data Structure describing destination string, counter
-
 
125
 *             of used space and total string size.
-
 
126
 *
-
 
127
 * @return Number of wide characters to print (not characters actually
-
 
128
 *         printed).
-
 
129
 *
-
 
130
 */
-
 
131
static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
-
 
132
{
-
 
133
    index_t index = 0;
-
 
134
   
-
 
135
    while (index < (size / sizeof(wchar_t))) {
-
 
136
        size_t left = data->size - data->len;
-
 
137
       
-
 
138
        if (left == 0)
-
 
139
            return ((int) size);
-
 
140
       
-
 
141
        if (left == 1) {
-
 
142
            /* We have only one free byte left in buffer
-
 
143
             * -> store trailing zero
-
 
144
             */
-
 
145
            data->dst[data->size - 1] = 0;
-
 
146
            data->len = data->size;
-
 
147
            return ((int) size);
-
 
148
        }
-
 
149
       
-
 
150
        if (chr_encode(str[index], data->dst, &data->len, data->size - 1) != EOK)
-
 
151
            break;
-
 
152
       
-
 
153
        index++;
-
 
154
    }
-
 
155
   
-
 
156
    /* Put trailing zero at end, but not count it
-
 
157
     * into data->len so it could be rewritten next time
-
 
158
     */
-
 
159
    data->dst[data->len] = 0;
-
 
160
   
85
    return count;  
161
    return ((int) size);
86
}
162
}
87
 
163
 
88
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
164
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
89
{
165
{
90
    struct vsnprintf_data data = {size, 0, str};
166
    vsnprintf_data_t data = {
-
 
167
        size,
-
 
168
        0,
-
 
169
        str
-
 
170
    };
-
 
171
    printf_spec_t ps = {
-
 
172
        (int(*) (const char *, size_t, void *)) vsnprintf_str_write,
91
    struct printf_spec ps = {(int(*)(void *, size_t, void *))vsnprintf_write, &data};
173
        (int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write,
-
 
174
        &data
-
 
175
    };
92
 
176
   
93
    /* 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 */
94
    if (size > 0)
178
    if (size > 0)
95
        str[0] = 0;
179
        str[0] = 0;
96
   
180
   
97
    /* vsnprintf_write ensures that str will be terminated by zero. */
181
    /* vsnprintf_write ensures that str will be terminated by zero. */