blob: aa636fba50bda311fa63d3d2be2a9d807604c8b5 [file] [log] [blame]
Howard Hinnant3c78ca02011-09-22 19:10:18 +00001// -*- C++ -*-
Howard Hinnant34388892011-09-28 21:39:20 +00002//===----------------------- support/win32/support.h ----------------------===//
Howard Hinnant3c78ca02011-09-22 19:10:18 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
Howard Hinnant9daaf572013-05-16 17:13:40 +000011#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
Howard Hinnantdbe81112011-09-23 16:11:27 +000017
Howard Hinnant39e95062013-04-02 15:46:31 +000018
Howard Hinnant0ed0d692013-08-26 20:18:01 +000019// Like sprintf, but when return value >= 0 it returns
20// a pointer to a malloc'd string in *sptr.
Howard Hinnant39e95062013-04-02 15:46:31 +000021// If return >= 0, use free to delete *sptr.
Peter Collingbourne2f97aab2018-01-23 18:53:33 +000022int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap )
Howard Hinnant3c78ca02011-09-22 19:10:18 +000023{
Howard Hinnantdbe81112011-09-23 16:11:27 +000024 *sptr = NULL;
Howard Hinnant0ed0d692013-08-26 20:18:01 +000025 // Query the count required.
26 int count = _vsnprintf( NULL, 0, format, ap );
27 if (count < 0)
28 return count;
29 size_t buffer_size = static_cast<size_t>(count) + 1;
30 char* p = static_cast<char*>(malloc(buffer_size));
31 if ( ! p )
32 return -1;
33 // If we haven't used exactly what was required, something is wrong.
34 // Maybe bug in vsnprintf. Report the error and return.
35 if (_vsnprintf(p, buffer_size, format, ap) != count) {
36 free(p);
37 return -1;
Howard Hinnant3c78ca02011-09-22 19:10:18 +000038 }
Howard Hinnant0ed0d692013-08-26 20:18:01 +000039 // All good. This is returning memory to the caller not freeing it.
40 *sptr = p;
Howard Hinnant3c78ca02011-09-22 19:10:18 +000041 return count;
42}
Howard Hinnantdbe81112011-09-23 16:11:27 +000043
Howard Hinnant0ed0d692013-08-26 20:18:01 +000044// Returns >= 0: the number of wide characters found in the
45// multi byte sequence src (of src_size_bytes), that fit in the buffer dst
46// (of max_dest_chars elements size). The count returned excludes the
47// null terminator. When dst is NULL, no characters are copied
48// and no "out" parameters are updated.
Howard Hinnant45944822013-04-02 22:14:51 +000049// Returns (size_t) -1: an incomplete sequence encountered.
Howard Hinnant0ed0d692013-08-26 20:18:01 +000050// Leaves *src pointing the next character to convert or NULL
51// if a null character was converted from *src.
Howard Hinnante4383372011-10-22 20:59:45 +000052size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +000053 size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +000054{
Howard Hinnant45944822013-04-02 22:14:51 +000055 const size_t terminated_sequence = static_cast<size_t>(0);
56 //const size_t invalid_sequence = static_cast<size_t>(-1);
57 const size_t incomplete_sequence = static_cast< size_t>(-2);
58
59 size_t dest_converted = 0;
60 size_t source_converted = 0;
61 size_t source_remaining = src_size_bytes;
62 size_t result = 0;
63 bool have_result = false;
64
65 while ( source_remaining ) {
66 if ( dst && dest_converted >= max_dest_chars )
67 break;
68 // Converts one multi byte character.
69 // if result > 0, it's the size in bytes of that character.
70 // othewise if result is zero it indicates the null character has been found.
71 // otherwise it's an error and errno may be set.
72 size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
73 // Don't do anything to change errno from here on.
74 if ( char_size > 0 ) {
75 source_remaining -= char_size;
76 source_converted += char_size;
77 ++dest_converted;
78 continue;
79 }
80 result = char_size;
81 have_result = true;
82 break;
83 }
84 if ( dst ) {
85 if ( have_result && result == terminated_sequence )
86 *src = NULL;
87 else
88 *src += source_converted;
89 }
90 if ( have_result && result != terminated_sequence && result != incomplete_sequence )
91 return static_cast<size_t>(-1);
92
93 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +000094}
Howard Hinnant45944822013-04-02 22:14:51 +000095
96// Converts max_source_chars from the wide character buffer pointer to by *src,
Howard Hinnant0ed0d692013-08-26 20:18:01 +000097// into the multi byte character sequence buffer stored at dst which must be
98// dst_size_bytes bytes in size.
Alp Tokerf03763a2014-05-15 11:27:39 +000099// Returns >= 0: the number of bytes in the sequence
100// converted from *src, excluding the null terminator.
Howard Hinnant45944822013-04-02 22:14:51 +0000101// Returns size_t(-1) if an error occurs, also sets errno.
Howard Hinnant0ed0d692013-08-26 20:18:01 +0000102// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst
103// and no "out" parameters are updated.
Howard Hinnante4383372011-10-22 20:59:45 +0000104size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +0000105 size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +0000106{
Howard Hinnant45944822013-04-02 22:14:51 +0000107 //const size_t invalid_sequence = static_cast<size_t>(-1);
108
109 size_t source_converted = 0;
110 size_t dest_converted = 0;
111 size_t dest_remaining = dst_size_bytes;
112 size_t char_size = 0;
113 const errno_t no_error = ( errno_t) 0;
114 errno_t result = ( errno_t ) 0;
115 bool have_result = false;
116 bool terminator_found = false;
117
118 while ( source_converted != max_source_chars ) {
119 if ( ! dest_remaining )
120 break;
121 wchar_t c = (*src)[source_converted];
122 if ( dst )
123 result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
124 else
Howard Hinnant9daaf572013-05-16 17:13:40 +0000125 result = wcrtomb_s( &char_size, NULL, 0, c, ps);
Howard Hinnant0ed0d692013-08-26 20:18:01 +0000126 // If result is zero there is no error and char_size contains the
127 // size of the multi-byte-sequence converted.
Howard Hinnant45944822013-04-02 22:14:51 +0000128 // Otherwise result indicates an errno type error.
129 if ( result == no_error ) {
130 if ( c == L'\0' ) {
131 terminator_found = true;
132 break;
133 }
134 ++source_converted;
135 if ( dst )
136 dest_remaining -= char_size;
137 dest_converted += char_size;
138 continue;
139 }
140 have_result = true;
141 break;
142 }
143 if ( dst ) {
144 if ( terminator_found )
145 *src = NULL;
146 else
147 *src = *src + source_converted;
148 }
149 if ( have_result && result != no_error ) {
150 errno = result;
151 return static_cast<size_t>(-1);
152 }
153
154 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +0000155}