blob: e989681a6d5f2cd1179f223ed692be35744f1d82 [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 Hinnant9daaf572013-05-16 17:13:40 +000018// Some of these functions aren't standard or if they conform, the name does not.
19
20int asprintf(char **sptr, const char *__restrict format, ...)
Howard Hinnant34388892011-09-28 21:39:20 +000021{
22 va_list ap;
Howard Hinnant9daaf572013-05-16 17:13:40 +000023 va_start(ap, format);
24 int result;
Howard Hinnant0ed0d692013-08-26 20:18:01 +000025 result = vasprintf(sptr, format, ap);
Howard Hinnant34388892011-09-28 21:39:20 +000026 va_end(ap);
Howard Hinnanta8929662011-10-17 20:08:59 +000027 return result;
Howard Hinnant34388892011-09-28 21:39:20 +000028}
Howard Hinnant39e95062013-04-02 15:46:31 +000029
Howard Hinnant0ed0d692013-08-26 20:18:01 +000030// Like sprintf, but when return value >= 0 it returns
31// a pointer to a malloc'd string in *sptr.
Howard Hinnant39e95062013-04-02 15:46:31 +000032// If return >= 0, use free to delete *sptr.
Howard Hinnant9daaf572013-05-16 17:13:40 +000033int vasprintf( char **sptr, const char *__restrict format, va_list ap )
Howard Hinnant3c78ca02011-09-22 19:10:18 +000034{
Howard Hinnantdbe81112011-09-23 16:11:27 +000035 *sptr = NULL;
Howard Hinnant0ed0d692013-08-26 20:18:01 +000036 // Query the count required.
37 int count = _vsnprintf( NULL, 0, format, ap );
38 if (count < 0)
39 return count;
40 size_t buffer_size = static_cast<size_t>(count) + 1;
41 char* p = static_cast<char*>(malloc(buffer_size));
42 if ( ! p )
43 return -1;
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) {
47 free(p);
48 return -1;
Howard Hinnant3c78ca02011-09-22 19:10:18 +000049 }
Howard Hinnant0ed0d692013-08-26 20:18:01 +000050 // All good. This is returning memory to the caller not freeing it.
51 *sptr = p;
Howard Hinnant3c78ca02011-09-22 19:10:18 +000052 return count;
53}
Howard Hinnantdbe81112011-09-23 16:11:27 +000054
Howard Hinnant0ed0d692013-08-26 20:18:01 +000055// 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.
Howard Hinnant45944822013-04-02 22:14:51 +000060// Returns (size_t) -1: an incomplete sequence encountered.
Howard Hinnant0ed0d692013-08-26 20:18:01 +000061// Leaves *src pointing the next character to convert or NULL
62// if a null character was converted from *src.
Howard Hinnante4383372011-10-22 20:59:45 +000063size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +000064 size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +000065{
Howard Hinnant45944822013-04-02 22:14:51 +000066 const size_t terminated_sequence = static_cast<size_t>(0);
67 //const size_t invalid_sequence = static_cast<size_t>(-1);
68 const size_t incomplete_sequence = static_cast< size_t>(-2);
69
70 size_t dest_converted = 0;
71 size_t source_converted = 0;
72 size_t source_remaining = src_size_bytes;
73 size_t result = 0;
74 bool have_result = false;
75
76 while ( source_remaining ) {
77 if ( dst && dest_converted >= max_dest_chars )
78 break;
79 // Converts one multi byte character.
80 // if result > 0, it's the size in bytes of that character.
81 // othewise if result is zero it indicates the null character has been found.
82 // otherwise it's an error and errno may be set.
83 size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
84 // Don't do anything to change errno from here on.
85 if ( char_size > 0 ) {
86 source_remaining -= char_size;
87 source_converted += char_size;
88 ++dest_converted;
89 continue;
90 }
91 result = char_size;
92 have_result = true;
93 break;
94 }
95 if ( dst ) {
96 if ( have_result && result == terminated_sequence )
97 *src = NULL;
98 else
99 *src += source_converted;
100 }
101 if ( have_result && result != terminated_sequence && result != incomplete_sequence )
102 return static_cast<size_t>(-1);
103
104 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +0000105}
Howard Hinnant45944822013-04-02 22:14:51 +0000106
107// Converts max_source_chars from the wide character buffer pointer to by *src,
Howard Hinnant0ed0d692013-08-26 20:18:01 +0000108// into the multi byte character sequence buffer stored at dst which must be
109// dst_size_bytes bytes in size.
Alp Tokerf03763a2014-05-15 11:27:39 +0000110// Returns >= 0: the number of bytes in the sequence
111// converted from *src, excluding the null terminator.
Howard Hinnant45944822013-04-02 22:14:51 +0000112// Returns size_t(-1) if an error occurs, also sets errno.
Howard Hinnant0ed0d692013-08-26 20:18:01 +0000113// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst
114// and no "out" parameters are updated.
Howard Hinnante4383372011-10-22 20:59:45 +0000115size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +0000116 size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +0000117{
Howard Hinnant45944822013-04-02 22:14:51 +0000118 //const size_t invalid_sequence = static_cast<size_t>(-1);
119
120 size_t source_converted = 0;
121 size_t dest_converted = 0;
122 size_t dest_remaining = dst_size_bytes;
123 size_t char_size = 0;
124 const errno_t no_error = ( errno_t) 0;
125 errno_t result = ( errno_t ) 0;
126 bool have_result = false;
127 bool terminator_found = false;
128
129 while ( source_converted != max_source_chars ) {
130 if ( ! dest_remaining )
131 break;
132 wchar_t c = (*src)[source_converted];
133 if ( dst )
134 result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
135 else
Howard Hinnant9daaf572013-05-16 17:13:40 +0000136 result = wcrtomb_s( &char_size, NULL, 0, c, ps);
Howard Hinnant0ed0d692013-08-26 20:18:01 +0000137 // If result is zero there is no error and char_size contains the
138 // size of the multi-byte-sequence converted.
Howard Hinnant45944822013-04-02 22:14:51 +0000139 // Otherwise result indicates an errno type error.
140 if ( result == no_error ) {
141 if ( c == L'\0' ) {
142 terminator_found = true;
143 break;
144 }
145 ++source_converted;
146 if ( dst )
147 dest_remaining -= char_size;
148 dest_converted += char_size;
149 continue;
150 }
151 have_result = true;
152 break;
153 }
154 if ( dst ) {
155 if ( terminator_found )
156 *src = NULL;
157 else
158 *src = *src + source_converted;
159 }
160 if ( have_result && result != no_error ) {
161 errno = result;
162 return static_cast<size_t>(-1);
163 }
164
165 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +0000166}