2 //===----------------------- support/win32/support.h ----------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
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, ...)
22 int result = vasprintf(sptr, fmt, ap);
26 int vasprintf( char **sptr, const char *__restrict fmt, 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 );
39 // FIXME: use wcrtomb and avoid copy
40 // use mbsrtowcs which is available, first copy first nwc elements of src
41 size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
42 size_t nmc, size_t len, 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 );
55 // FIXME: use wcrtomb and avoid copy
56 // use wcsrtombs which is available, first copy first nwc elements of src
57 size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
58 size_t nwc, size_t len, 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 );