Rev 1272 | Rev 1623 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 1272 | Rev 1617 | ||
|---|---|---|---|
| Line 50... | Line 50... | ||
| 50 | * @param data structure with destination string, counter of used space and total string size. |
50 | * @param data structure with destination string, counter of used space and total string size. |
| 51 | * @return number of characters to print (not characters really printed!) |
51 | * @return number of characters to print (not characters really printed!) |
| 52 | */ |
52 | */ |
| 53 | int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data) |
53 | int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data) |
| 54 | { |
54 | { |
| 55 | size_t i,j; |
55 | size_t i; |
| 56 | i = data->size - data->len; |
56 | i = data->size - data->len; |
| 57 | j = count; |
- | |
| 58 | 57 | ||
| 59 | if (i > 0) { |
58 | if (i == 0) { |
| 60 | if (i <= j) { |
- | |
| 61 | if (i == 1) { |
- | |
| 62 | /* We have only one free byte left in buffer => write there trailing zero */ |
- | |
| 63 | data->string[data->size - 1] = 0; |
- | |
| 64 | data->len = data->size; |
- | |
| 65 | } else { |
- | |
| 66 | /* We have not enought space for whole string with the trailing zero => print only a part of string */ |
- | |
| 67 | memcpy((void *)(data->string + data->len), (void *)str, i - 1); |
- | |
| 68 | data->string[data->size - 1] = 0; |
- | |
| 69 | data->len = data->size; |
- | |
| 70 | } |
- | |
| 71 | } else { |
- | |
| 72 | /* Buffer is big enought to print whole string */ |
- | |
| 73 | memcpy((void *)(data->string + data->len), (void *)str, j); |
- | |
| 74 | data->len += j; |
59 | return count; |
| 75 | /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */ |
- | |
| 76 | data->string[data->len] = 0; |
- | |
| 77 | } |
- | |
| 78 | } |
60 | } |
| 79 | 61 | ||
| - | 62 | if (i == 1) { |
|
| - | 63 | /* We have only one free byte left in buffer => write there trailing zero */ |
|
| - | 64 | data->string[data->size - 1] = 0; |
|
| - | 65 | data->len = data->size; |
|
| - | 66 | return count; |
|
| - | 67 | } |
|
| - | 68 | ||
| - | 69 | if (i <= count) { |
|
| - | 70 | /* We have not enought space for whole string with the trailing zero => print only a part of string */ |
|
| - | 71 | memcpy((void *)(data->string + data->len), (void *)str, i - 1); |
|
| - | 72 | data->string[data->size - 1] = 0; |
|
| - | 73 | data->len = data->size; |
|
| - | 74 | return count; |
|
| - | 75 | } |
|
| - | 76 | ||
| - | 77 | /* Buffer is big enought to print whole string */ |
|
| - | 78 | memcpy((void *)(data->string + data->len), (void *)str, count); |
|
| - | 79 | data->len += count; |
|
| - | 80 | /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */ |
|
| - | 81 | data->string[data->len] = 0; |
|
| - | 82 | ||
| 80 | return count; |
83 | return count; |
| 81 | } |
84 | } |
| 82 | 85 | ||
| 83 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
86 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
| 84 | { |
87 | { |