1 // -*- C++ -*- 2 //===----------------------- support/win32/support.h ----------------------===// 3 // 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 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 17 18 int asprintf(char **sptr, const char *__restrict__ fmt, ...) 19 { 20 va_list ap; 21 va_start(ap, fmt); 22 int result = vasprintf(sptr, fmt, ap); 23 va_end(ap); 24 return result; 25 } 26 int vasprintf( char **sptr, const char *__restrict__ fmt, va_list ap ) 27 { 28 *sptr = NULL; 29 int count = vsnprintf( *sptr, 0, fmt, ap ); 30 if( (count >= 0) && ((*sptr = (char*)malloc(count+1)) != NULL) ) 31 { 32 vsprintf( *sptr, fmt, ap ); 33 sptr[count] = '\0'; 34 } 35 36 return count; 37 } 38 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 ) 43 { 44 char* local_src = new char[nmc+1]; 45 char* nmcsrc = local_src; 46 strncpy( nmcsrc, *src, nmc ); 47 nmcsrc[nmc] = '\0'; 48 const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps ); 49 // propagate error 50 if( nmcsrc == NULL ) 51 *src = NULL; 52 delete[] local_src; 53 return result; 54 } 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 ) 59 { 60 wchar_t* local_src = new wchar_t[nwc]; 61 wchar_t* nwcsrc = local_src; 62 wcsncpy(nwcsrc, *src, nwc); 63 nwcsrc[nwc] = '\0'; 64 const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps ); 65 // propogate error 66 if( nwcsrc == NULL ) 67 *src = NULL; 68 delete[] nwcsrc; 69 return result; 70 } 71