char *wcs2str( const wchar_t *in ) { if (! in) return NULL; char *out; size_t desired_size = MAX_UTF8_BYTES*wcslen(in)+1; char local_buff[512]; if (desired_size <= sizeof local_buff / sizeof *local_buff) { // convert into local buff, then use strdup() so we don't waste malloc'd space char *result = wcs2str_internal(in, local_buff); if (result) { // It converted into the local buffer, so copy it result = strdup(result); if (! result) { DIE_MEM(); } } return result; } else { // here we fall into the bad case of allocating a buffer probably much larger than necessary out = (char *)malloc( MAX_UTF8_BYTES*wcslen(in)+1 ); if (!out) { DIE_MEM(); } return wcs2str_internal( in, out ); } return wcs2str_internal( in, out ); }
char *wcs2str( const wchar_t *in ) { char *out; out = malloc( MAX_UTF8_BYTES*wcslen(in)+1 ); if( !out ) { DIE_MEM(); } return wcs2str_internal( in, out ); }
/** For wgettext: Wide to narrow character conversion. Internal implementation that avoids exessive calls to malloc */ static char *wgettext_wcs2str( const wchar_t *in ) { size_t len = MAX_UTF8_BYTES*wcslen(in)+1; if( len > wcs2str_buff_count ) { wcs2str_buff = realloc( wcs2str_buff, len ); if( !wcs2str_buff ) { DIE_MEM(); } } return wcs2str_internal( in, wcs2str_buff); }
/** Convert the specified wide aharacter string to a narrow character string. This function uses an internal temporary buffer for storing the result so subsequent results will overwrite previous results. */ static char *wutil_wcs2str( const wchar_t *in ) { size_t new_sz; wutil_calls++; new_sz =MAX_UTF8_BYTES*wcslen(in)+1; if( tmp_len < new_sz ) { new_sz = maxi( new_sz, TMP_LEN_MIN ); tmp = realloc( tmp, new_sz ); if( !tmp ) { DIE_MEM(); } tmp_len = new_sz; } return wcs2str_internal( in, tmp ); }