add to_str(...) methods to the TextStreamBase<> class
add such methods: bool to_str(char * str, size_t max_buf_len) const; bool to_str(wchar_t * str, size_t max_buf_len) const;
This commit is contained in:
parent
78d31861de
commit
172c2fcee7
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* Copyright (c) 2012-2023, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -155,6 +155,13 @@ public:
|
|||
std::string to_str() const;
|
||||
std::wstring to_wstr() const;
|
||||
|
||||
/*
|
||||
* returns true if the buffer was sufficient large and there were no convertion errors
|
||||
*/
|
||||
bool to_str(char * str, size_t max_buf_len) const;
|
||||
bool to_str(wchar_t * str, size_t max_buf_len) const;
|
||||
|
||||
|
||||
char get_char(size_t index) const;
|
||||
wchar_t get_wchar(size_t index) const;
|
||||
|
||||
|
@ -597,7 +604,6 @@ void TextStreamBase<char_type, stack_size, heap_block_size>::to_str(std::string
|
|||
if( str.capacity() < str.size() + size() )
|
||||
str.reserve(str.size() + size());
|
||||
|
||||
|
||||
if constexpr (sizeof(char_type) == sizeof(char) )
|
||||
{
|
||||
const_iterator i = begin();
|
||||
|
@ -658,6 +664,89 @@ std::wstring TextStreamBase<char_type, stack_size, heap_block_size>::to_wstr() c
|
|||
}
|
||||
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
bool TextStreamBase<char_type, stack_size, heap_block_size>::to_str(char * str, size_t max_buf_len) const
|
||||
{
|
||||
bool converted_correctly = false;
|
||||
|
||||
if( max_buf_len > 0 )
|
||||
{
|
||||
if constexpr (sizeof(char_type) == sizeof(char) )
|
||||
{
|
||||
converted_correctly = true;
|
||||
const_iterator i = begin();
|
||||
const_iterator i_end = end();
|
||||
size_t len = 0;
|
||||
max_buf_len -= 1; // for terminating null character
|
||||
|
||||
for( ; i != i_end ; ++i, ++len)
|
||||
{
|
||||
if( len < max_buf_len )
|
||||
{
|
||||
str[len] = *i;
|
||||
}
|
||||
else
|
||||
{
|
||||
converted_correctly = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
str[len] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
converted_correctly = wide_stream_to_utf8(*this, str, max_buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
return converted_correctly;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
bool TextStreamBase<char_type, stack_size, heap_block_size>::to_str(wchar_t * str, size_t max_buf_len) const
|
||||
{
|
||||
bool converted_correctly = false;
|
||||
|
||||
if( max_buf_len > 0 )
|
||||
{
|
||||
if constexpr (sizeof(char_type) == sizeof(wchar_t) )
|
||||
{
|
||||
converted_correctly = true;
|
||||
const_iterator i = begin();
|
||||
const_iterator i_end = end();
|
||||
size_t len = 0;
|
||||
max_buf_len -= 1; // for terminating null character
|
||||
|
||||
for( ; i != i_end ; ++i, ++len)
|
||||
{
|
||||
if( len < max_buf_len )
|
||||
{
|
||||
str[len] = *i;
|
||||
}
|
||||
else
|
||||
{
|
||||
converted_correctly = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
str[len] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
converted_correctly = utf8_to_wide(*this, str, max_buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
return converted_correctly;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
char TextStreamBase<char_type, stack_size, heap_block_size>::get_char(size_t index) const
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue