Howard Hinnant 2013-04-02 22:14:51 +00:00
parent b777d6a4b2
commit cf31d3864e
2 changed files with 115 additions and 26 deletions

View File

@ -807,9 +807,15 @@ basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
default:
return pos_type(off_type(-1));
}
#if _WIN32
if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
return pos_type(off_type(-1));
pos_type __r = ftell(__file_);
#else
if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
return pos_type(off_type(-1));
pos_type __r = ftello(__file_);
#endif
__r.state(__st_);
return __r;
}
@ -820,8 +826,13 @@ basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
{
if (__file_ == 0 || sync())
return pos_type(off_type(-1));
#if _WIN32
if (fseek(__file_, __sp, SEEK_SET))
return pos_type(off_type(-1));
#else
if (fseeko(__file_, __sp, SEEK_SET))
return pos_type(off_type(-1));
#endif
__st_ = __sp.state();
return __sp;
}
@ -880,8 +891,13 @@ basic_filebuf<_CharT, _Traits>::sync()
}
}
}
#if _WIN32
if (fseek(__file_, -__c, SEEK_CUR))
return -1;
#else
if (fseeko(__file_, -__c, SEEK_CUR))
return -1;
#endif
if (__update_st)
__st_ = __state;
__extbufnext_ = __extbufend_ = __extbuf_;

View File

@ -45,35 +45,108 @@ int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
return count;
}
// FIXME: use wcrtomb and avoid copy
// use mbsrtowcs which is available, first copy first nwc elements of src
// Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
// that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
// When dst is NULL, no characters are copied and no "out" parameters are updated.
// Returns (size_t) -1: an incomplete sequence encountered.
// Leaves *src pointing the next character to convert or NULL if a null character was converted from *src.
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
size_t nmc, size_t len, mbstate_t *__restrict ps )
size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
{
char* local_src = new char[nmc+1];
char* nmcsrc = local_src;
strncpy( nmcsrc, *src, nmc );
nmcsrc[nmc] = '\0';
const size_t result = mbsrtowcs( dst, const_cast<const char **>(&nmcsrc), len, ps );
// propagate error
if( nmcsrc == NULL )
*src = NULL;
delete[] local_src;
return result;
const size_t terminated_sequence = static_cast<size_t>(0);
//const size_t invalid_sequence = static_cast<size_t>(-1);
const size_t incomplete_sequence = static_cast< size_t>(-2);
size_t dest_converted = 0;
size_t source_converted = 0;
size_t source_remaining = src_size_bytes;
size_t result = 0;
bool have_result = false;
while ( source_remaining ) {
if ( dst && dest_converted >= max_dest_chars )
break;
// Converts one multi byte character.
// if result > 0, it's the size in bytes of that character.
// othewise if result is zero it indicates the null character has been found.
// otherwise it's an error and errno may be set.
size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
// Don't do anything to change errno from here on.
if ( char_size > 0 ) {
source_remaining -= char_size;
source_converted += char_size;
++dest_converted;
continue;
}
result = char_size;
have_result = true;
break;
}
if ( dst ) {
if ( have_result && result == terminated_sequence )
*src = NULL;
else
*src += source_converted;
}
if ( have_result && result != terminated_sequence && result != incomplete_sequence )
return static_cast<size_t>(-1);
return dest_converted;
}
// FIXME: use wcrtomb and avoid copy
// use wcsrtombs which is available, first copy first nwc elements of src
// Converts max_source_chars from the wide character buffer pointer to by *src,
// into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
// Returns >= 0: the number of bytes in the sequence sequence converted frome *src, excluding the null terminator.
// Returns size_t(-1) if an error occurs, also sets errno.
// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst and no "out" parameters are updated.
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
size_t nwc, size_t len, mbstate_t *__restrict ps )
size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
{
wchar_t* local_src = new wchar_t[nwc];
wchar_t* nwcsrc = local_src;
wcsncpy(nwcsrc, *src, nwc);
nwcsrc[nwc] = '\0';
const size_t result = wcsrtombs( dst, const_cast<const wchar_t **>(&nwcsrc), len, ps );
// propogate error
if( nwcsrc == NULL )
*src = NULL;
delete[] nwcsrc;
return result;
//const size_t invalid_sequence = static_cast<size_t>(-1);
size_t source_converted = 0;
size_t dest_converted = 0;
size_t dest_remaining = dst_size_bytes;
size_t char_size = 0;
const errno_t no_error = ( errno_t) 0;
errno_t result = ( errno_t ) 0;
bool have_result = false;
bool terminator_found = false;
while ( source_converted != max_source_chars ) {
if ( ! dest_remaining )
break;
wchar_t c = (*src)[source_converted];
if ( dst )
result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
else
result = wcrtomb_s( &char_size, NULL, 0, c, ps);
// If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
// Otherwise result indicates an errno type error.
if ( result == no_error ) {
if ( c == L'\0' ) {
terminator_found = true;
break;
}
++source_converted;
if ( dst )
dest_remaining -= char_size;
dest_converted += char_size;
continue;
}
have_result = true;
break;
}
if ( dst ) {
if ( terminator_found )
*src = NULL;
else
*src = *src + source_converted;
}
if ( have_result && result != no_error ) {
errno = result;
return static_cast<size_t>(-1);
}
return dest_converted;
}