9
9
//===----------------------------------------------------------------------===//
11
#include <support/win32/support.h>
12
#include <stdarg.h> // va_start, va_end
13
#include <stddef.h> // size_t
14
#include <stdlib.h> // malloc
15
#include <stdio.h> // vsprintf, vsnprintf
16
#include <string.h> // strcpy, wcsncpy
18
int asprintf(char **sptr, const char *__restrict fmt, ...)
11
#include <cstdarg> // va_start, va_end
12
#include <cstddef> // size_t
13
#include <cstdlib> // malloc
14
#include <cstdio> // vsprintf, vsnprintf
15
#include <cstring> // strcpy, wcsncpy
16
#include <cwchar> // mbstate_t
17
#include <memory> // unique_ptr
19
namespace { // Private
22
inline void operator()(char* p) { free(p); }
25
// Some of these functions aren't standard or if they conform, the name does not.
27
int asprintf(char **sptr, const char *__restrict format, ...)
22
int result = vasprintf(sptr, fmt, ap);
32
#ifndef _LIBCPP_NO_EXCEPTIONS
35
result = vasprintf(sptr, format, ap);
36
#ifndef _LIBCPP_NO_EXCEPTIONS
26
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
46
// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
47
// If return >= 0, use free to delete *sptr.
48
int vasprintf( char **sptr, const char *__restrict format, va_list ap )
29
int count = vsnprintf( *sptr, 0, fmt, ap );
30
if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) )
32
vsprintf( *sptr, fmt, ap );
51
int count = _vsnprintf( NULL, 0, format, ap ); // Query the buffer size required.
53
std::unique_ptr<char, free_deleter> p( static_cast<char*>(malloc(count+1)) );
56
if ( vsnprintf( p.get(), count+1, format, ap ) == count ) // We should have used exactly what was required.
58
else // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
59
return -1; // Pointer will get automaticlaly deleted.
39
// FIXME: use wcrtomb and avoid copy
40
// use mbsrtowcs which is available, first copy first nwc elements of src
65
// Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
66
// that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
67
// When dst is NULL, no characters are copied and no "out" parameters are updated.
68
// Returns (size_t) -1: an incomplete sequence encountered.
69
// Leaves *src pointing the next character to convert or NULL if a null character was converted from *src.
41
70
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
42
size_t nmc, size_t len, mbstate_t *__restrict ps )
71
size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
44
char* local_src = new char[nmc+1];
45
char* nmcsrc = local_src;
46
strncpy( nmcsrc, *src, nmc );
48
const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
73
const size_t terminated_sequence = static_cast<size_t>(0);
74
//const size_t invalid_sequence = static_cast<size_t>(-1);
75
const size_t incomplete_sequence = static_cast< size_t>(-2);
77
size_t dest_converted = 0;
78
size_t source_converted = 0;
79
size_t source_remaining = src_size_bytes;
81
bool have_result = false;
83
while ( source_remaining ) {
84
if ( dst && dest_converted >= max_dest_chars )
86
// Converts one multi byte character.
87
// if result > 0, it's the size in bytes of that character.
88
// othewise if result is zero it indicates the null character has been found.
89
// otherwise it's an error and errno may be set.
90
size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
91
// Don't do anything to change errno from here on.
92
if ( char_size > 0 ) {
93
source_remaining -= char_size;
94
source_converted += char_size;
103
if ( have_result && result == terminated_sequence )
106
*src += source_converted;
108
if ( have_result && result != terminated_sequence && result != incomplete_sequence )
109
return static_cast<size_t>(-1);
111
return dest_converted;
55
// FIXME: use wcrtomb and avoid copy
56
// use wcsrtombs which is available, first copy first nwc elements of src
114
// Converts max_source_chars from the wide character buffer pointer to by *src,
115
// into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
116
// Returns >= 0: the number of bytes in the sequence sequence converted frome *src, excluding the null terminator.
117
// Returns size_t(-1) if an error occurs, also sets errno.
118
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst and no "out" parameters are updated.
57
119
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
58
size_t nwc, size_t len, mbstate_t *__restrict ps )
120
size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
60
wchar_t* local_src = new wchar_t[nwc];
61
wchar_t* nwcsrc = local_src;
62
wcsncpy(nwcsrc, *src, nwc);
64
const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps );
122
//const size_t invalid_sequence = static_cast<size_t>(-1);
124
size_t source_converted = 0;
125
size_t dest_converted = 0;
126
size_t dest_remaining = dst_size_bytes;
127
size_t char_size = 0;
128
const errno_t no_error = ( errno_t) 0;
129
errno_t result = ( errno_t ) 0;
130
bool have_result = false;
131
bool terminator_found = false;
133
while ( source_converted != max_source_chars ) {
134
if ( ! dest_remaining )
136
wchar_t c = (*src)[source_converted];
138
result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
140
result = wcrtomb_s( &char_size, NULL, 0, c, ps);
141
// If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
142
// Otherwise result indicates an errno type error.
143
if ( result == no_error ) {
145
terminator_found = true;
150
dest_remaining -= char_size;
151
dest_converted += char_size;
158
if ( terminator_found )
161
*src = *src + source_converted;
163
if ( have_result && result != no_error ) {
165
return static_cast<size_t>(-1);
168
return dest_converted;