blob: 75f6391912267fb33766aabb0d62bb49491e321d [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 Hinnant34388892011-09-28 21:39:20 +000011#include <support/win32/support.h>
12#include <stdarg.h> // va_start, va_end
Howard Hinnantdbe81112011-09-23 16:11:27 +000013#include <stddef.h> // size_t
14#include <stdlib.h> // malloc
15#include <stdio.h> // vsprintf, vsnprintf
16#include <string.h> // strcpy, wcsncpy
Howard Hinnantdbe81112011-09-23 16:11:27 +000017
Howard Hinnante4383372011-10-22 20:59:45 +000018int asprintf(char **sptr, const char *__restrict fmt, ...)
Howard Hinnant34388892011-09-28 21:39:20 +000019{
20 va_list ap;
21 va_start(ap, fmt);
22 int result = vasprintf(sptr, fmt, ap);
23 va_end(ap);
Howard Hinnanta8929662011-10-17 20:08:59 +000024 return result;
Howard Hinnant34388892011-09-28 21:39:20 +000025}
Howard Hinnant39e95062013-04-02 15:46:31 +000026
27// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
28// If return >= 0, use free to delete *sptr.
Howard Hinnante4383372011-10-22 20:59:45 +000029int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
Howard Hinnant3c78ca02011-09-22 19:10:18 +000030{
Howard Hinnantdbe81112011-09-23 16:11:27 +000031 *sptr = NULL;
Howard Hinnant39e95062013-04-02 15:46:31 +000032 int count = vsnprintf( NULL, 0, fmt, ap ); // Query the buffer size required.
33 if( count >= 0 ) {
34 char* p = static_cast<char*>(malloc(count+1)); // Allocate memory for it and the terminator.
35 if ( p == NULL )
36 return -1;
37 if ( vsnprintf( p, count+1, fmt, ap ) == count ) // We should have used exactly what was required.
38 *sptr = p;
39 else { // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
40 free(p);
41 return -1;
42 }
Howard Hinnant3c78ca02011-09-22 19:10:18 +000043 }
44
45 return count;
46}
Howard Hinnantdbe81112011-09-23 16:11:27 +000047
Howard Hinnant45944822013-04-02 22:14:51 +000048// 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.
51// 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.
Howard Hinnante4383372011-10-22 20:59:45 +000053size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +000054 size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +000055{
Howard Hinnant45944822013-04-02 22:14:51 +000056 const size_t terminated_sequence = static_cast<size_t>(0);
57 //const size_t invalid_sequence = static_cast<size_t>(-1);
58 const size_t incomplete_sequence = static_cast< size_t>(-2);
59
60 size_t dest_converted = 0;
61 size_t source_converted = 0;
62 size_t source_remaining = src_size_bytes;
63 size_t result = 0;
64 bool have_result = false;
65
66 while ( source_remaining ) {
67 if ( dst && dest_converted >= max_dest_chars )
68 break;
69 // Converts one multi byte character.
70 // if result > 0, it's the size in bytes of that character.
71 // othewise if result is zero it indicates the null character has been found.
72 // otherwise it's an error and errno may be set.
73 size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
74 // Don't do anything to change errno from here on.
75 if ( char_size > 0 ) {
76 source_remaining -= char_size;
77 source_converted += char_size;
78 ++dest_converted;
79 continue;
80 }
81 result = char_size;
82 have_result = true;
83 break;
84 }
85 if ( dst ) {
86 if ( have_result && result == terminated_sequence )
87 *src = NULL;
88 else
89 *src += source_converted;
90 }
91 if ( have_result && result != terminated_sequence && result != incomplete_sequence )
92 return static_cast<size_t>(-1);
93
94 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +000095}
Howard Hinnant45944822013-04-02 22:14:51 +000096
97// 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.
100// 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.
Howard Hinnante4383372011-10-22 20:59:45 +0000102size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
Howard Hinnant45944822013-04-02 22:14:51 +0000103 size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
Howard Hinnantdbe81112011-09-23 16:11:27 +0000104{
Howard Hinnant45944822013-04-02 22:14:51 +0000105 //const size_t invalid_sequence = static_cast<size_t>(-1);
106
107 size_t source_converted = 0;
108 size_t dest_converted = 0;
109 size_t dest_remaining = dst_size_bytes;
110 size_t char_size = 0;
111 const errno_t no_error = ( errno_t) 0;
112 errno_t result = ( errno_t ) 0;
113 bool have_result = false;
114 bool terminator_found = false;
115
116 while ( source_converted != max_source_chars ) {
117 if ( ! dest_remaining )
118 break;
119 wchar_t c = (*src)[source_converted];
120 if ( dst )
121 result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
122 else
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.
125 // Otherwise result indicates an errno type error.
126 if ( result == no_error ) {
127 if ( c == L'\0' ) {
128 terminator_found = true;
129 break;
130 }
131 ++source_converted;
132 if ( dst )
133 dest_remaining -= char_size;
134 dest_converted += char_size;
135 continue;
136 }
137 have_result = true;
138 break;
139 }
140 if ( dst ) {
141 if ( terminator_found )
142 *src = NULL;
143 else
144 *src = *src + source_converted;
145 }
146 if ( have_result && result != no_error ) {
147 errno = result;
148 return static_cast<size_t>(-1);
149 }
150
151 return dest_converted;
Howard Hinnantdbe81112011-09-23 16:11:27 +0000152}