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
18
// Some of these functions aren't standard or if they conform, the name does not.
20
int asprintf(char **sptr, const char *__restrict format, ...)
22
int result = vasprintf(sptr, fmt, ap);
25
result = vasprintf(sptr, format, ap);
27
// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
30
// Like sprintf, but when return value >= 0 it returns
31
// a pointer to a malloc'd string in *sptr.
28
32
// If return >= 0, use free to delete *sptr.
29
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
33
int vasprintf( char **sptr, const char *__restrict format, va_list ap )
32
int count = vsnprintf( NULL, 0, fmt, ap ); // Query the buffer size required.
34
char* p = static_cast<char*>(malloc(count+1)); // Allocate memory for it and the terminator.
37
if ( vsnprintf( p, count+1, fmt, ap ) == count ) // We should have used exactly what was required.
39
else { // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
36
// Query the count required.
37
int count = _vsnprintf( NULL, 0, format, ap );
40
size_t buffer_size = static_cast<size_t>(count) + 1;
41
char* p = static_cast<char*>(malloc(buffer_size));
44
// If we haven't used exactly what was required, something is wrong.
45
// Maybe bug in vsnprintf. Report the error and return.
46
if (_vsnprintf(p, buffer_size, format, ap) != count) {
50
// All good. This is returning memory to the caller not freeing it.
48
// Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
49
// that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
50
// When dst is NULL, no characters are copied and no "out" parameters are updated.
55
// Returns >= 0: the number of wide characters found in the
56
// multi byte sequence src (of src_size_bytes), that fit in the buffer dst
57
// (of max_dest_chars elements size). The count returned excludes the
58
// null terminator. When dst is NULL, no characters are copied
59
// and no "out" parameters are updated.
51
60
// Returns (size_t) -1: an incomplete sequence encountered.
52
// Leaves *src pointing the next character to convert or NULL if a null character was converted from *src.
61
// Leaves *src pointing the next character to convert or NULL
62
// if a null character was converted from *src.
53
63
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
54
64
size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
97
107
// Converts max_source_chars from the wide character buffer pointer to by *src,
98
// into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
99
// Returns >= 0: the number of bytes in the sequence sequence converted frome *src, excluding the null terminator.
108
// into the multi byte character sequence buffer stored at dst which must be
109
// dst_size_bytes bytes in size.
110
// Returns >= 0: the number of bytes in the sequence sequence
111
// converted frome *src, excluding the null terminator.
100
112
// Returns size_t(-1) if an error occurs, also sets errno.
101
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst and no "out" parameters are updated.
113
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst
114
// and no "out" parameters are updated.
102
115
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
103
116
size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
121
134
result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
123
result = wcrtomb_s( &char_size, NULL, 0, c, ps);
124
// If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
136
result = wcrtomb_s( &char_size, NULL, 0, c, ps);
137
// If result is zero there is no error and char_size contains the
138
// size of the multi-byte-sequence converted.
125
139
// Otherwise result indicates an errno type error.
126
140
if ( result == no_error ) {
127
141
if ( c == L'\0' ) {