1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/libc/stdio/vasprintf.cpp

78 lines
2.2 KiB
C++
Raw Normal View History

2014-01-20 12:22:38 -05:00
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014, 2015.
2014-01-20 12:22:38 -05:00
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
stdio/vasprintf.cpp
Prints a string to a newly allocated buffer.
*******************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct vasprintf
2014-01-20 12:22:38 -05:00
{
char* buffer;
size_t used;
size_t size;
2014-01-20 12:22:38 -05:00
};
static size_t vasprintf_callback(void* ctx, const char* string, size_t length)
2014-01-20 12:22:38 -05:00
{
struct vasprintf* state = (struct vasprintf*) ctx;
size_t needed_size = state->used + length + 1;
if ( state->size < needed_size )
2014-01-20 12:22:38 -05:00
{
// TODO: Overflow check.
size_t new_size = 2 * state->size;
if ( new_size < needed_size )
new_size = needed_size;
char* new_buffer = (char*) realloc(state->buffer, new_size);
if ( !new_buffer )
2014-01-20 12:22:38 -05:00
{
free(state->buffer);
state->buffer = NULL;
2014-01-20 12:22:38 -05:00
return 0;
}
state->buffer = new_buffer;
state->size = new_size;
2014-01-20 12:22:38 -05:00
}
memcpy(state->buffer + state->used, string, length);
state->used += length;
2014-01-20 12:22:38 -05:00
return length;
}
extern "C"
int vasprintf(char** restrict result_ptr,
const char* restrict format,
va_list list)
{
struct vasprintf state;
state.used = 0;
state.size = 32;
if ( !(state.buffer = (char*) malloc(state.size)) )
return -1;
int result = vcbprintf(&state, vasprintf_callback, format, list);
if ( !state.buffer )
return -1;
state.buffer[state.used] = '\0';
return *result_ptr = state.buffer, result;
2014-01-20 12:22:38 -05:00
}