| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stdarg.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
|
|
| #include "wcsprintf.h" |
|
|
| static FILE *wcsprintf_file = 0x0; |
| static char *wcsprintf_buff = 0x0; |
| static char *wcsprintf_bufp = 0x0; |
| static size_t wcsprintf_size = 0; |
|
|
| |
|
|
| int wcsprintf_set(FILE *wcsout) |
|
|
| { |
| if (wcsout != 0x0) { |
| |
| wcsprintf_file = wcsout; |
|
|
| if (wcsprintf_buff != 0x0) { |
| |
| free(wcsprintf_buff); |
| wcsprintf_buff = 0x0; |
| } |
|
|
| } else { |
| |
| if (wcsprintf_buff == 0x0) { |
| |
| wcsprintf_buff = malloc(1024); |
| if (wcsprintf_buff == NULL) { |
| return 1; |
| } |
| wcsprintf_size = 1024; |
| } |
|
|
| |
| wcsprintf_bufp = wcsprintf_buff; |
| *wcsprintf_bufp = '\0'; |
| } |
|
|
| return 0; |
| } |
|
|
| |
|
|
| const char *wcsprintf_buf(void) |
|
|
| { |
| return wcsprintf_buff; |
| } |
|
|
| |
|
|
| int wcsprintf(const char *format, ...) |
|
|
| { |
| char *realloc_buff; |
| int nbytes; |
| size_t used; |
| va_list arg_list; |
|
|
| if (wcsprintf_buff == 0x0 && wcsprintf_file == 0x0) { |
| |
| wcsprintf_file = stdout; |
| } |
|
|
| va_start(arg_list, format); |
|
|
| if (wcsprintf_file) { |
| |
| nbytes = vfprintf(wcsprintf_file, format, arg_list); |
|
|
| } else { |
| |
| used = wcsprintf_bufp - wcsprintf_buff; |
| if (wcsprintf_size - used < 128) { |
| |
| wcsprintf_size += 1024; |
| realloc_buff = realloc(wcsprintf_buff, wcsprintf_size); |
| if (realloc_buff == NULL) { |
| free(wcsprintf_buff); |
| wcsprintf_buff = 0x0; |
| return 1; |
| } |
| wcsprintf_buff = realloc_buff; |
| wcsprintf_bufp = wcsprintf_buff + used; |
| } |
|
|
| nbytes = vsprintf(wcsprintf_bufp, format, arg_list); |
| wcsprintf_bufp += nbytes; |
| } |
|
|
| va_end(arg_list); |
|
|
| return nbytes; |
| } |
|
|
| |
|
|
| int wcsfprintf(FILE *stream, const char *format, ...) |
|
|
| { |
| char *realloc_buff; |
| int nbytes; |
| size_t used; |
| va_list arg_list; |
|
|
| if (wcsprintf_buff == 0x0 && wcsprintf_file == 0x0) { |
| |
| wcsprintf_file = stream; |
| } |
|
|
| va_start(arg_list, format); |
|
|
| if (wcsprintf_file) { |
| |
| nbytes = vfprintf(wcsprintf_file, format, arg_list); |
|
|
| } else { |
| |
| used = wcsprintf_bufp - wcsprintf_buff; |
| if (wcsprintf_size - used < 128) { |
| |
| wcsprintf_size += 1024; |
| realloc_buff = realloc(wcsprintf_buff, wcsprintf_size); |
| if (realloc_buff == NULL) { |
| free(wcsprintf_buff); |
| wcsprintf_buff = 0x0; |
| return 1; |
| } |
| wcsprintf_buff = realloc_buff; |
| wcsprintf_bufp = wcsprintf_buff + used; |
| } |
|
|
| nbytes = vsprintf(wcsprintf_bufp, format, arg_list); |
| wcsprintf_bufp += nbytes; |
| } |
|
|
| va_end(arg_list); |
|
|
| return nbytes; |
| } |
|
|