ELFIO/elfio/elfio_strings.hpp

98 lines
3.4 KiB
C++
Raw Normal View History

2012-11-27 09:45:28 +00:00
/*
2021-01-19 07:43:01 +00:00
Copyright (C) 2001-present by Serge Lamikhov-Center
2012-11-27 09:45:28 +00:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ELFIO_STRINGS_HPP
#define ELFIO_STRINGS_HPP
#include <cstdlib>
#include <cstring>
#include <string>
namespace ELFIO {
//------------------------------------------------------------------------------
template <class S> class string_section_accessor_template
2012-11-27 09:45:28 +00:00
{
public:
//------------------------------------------------------------------------------
2021-09-14 15:35:54 +00:00
explicit string_section_accessor_template( S* section )
: string_section( section )
2012-11-27 09:45:28 +00:00
{
}
//------------------------------------------------------------------------------
const char* get_string( Elf_Word index ) const
2012-11-27 09:45:28 +00:00
{
if ( string_section ) {
2022-09-01 15:49:56 +00:00
const char* data = string_section->get_data();
if ( index < string_section->get_size() && nullptr != data ) {
size_t string_length = strnlength(
data + index, string_section->get_size() - index );
2022-09-01 15:49:56 +00:00
if ( string_length < ( string_section->get_size() - index ) )
return data + index;
2012-11-27 09:45:28 +00:00
}
2016-04-23 14:35:32 +00:00
}
2012-11-27 09:45:28 +00:00
2021-08-26 09:52:23 +00:00
return nullptr;
2012-11-27 09:45:28 +00:00
}
//------------------------------------------------------------------------------
Elf_Word add_string( const char* str )
2012-11-27 09:45:28 +00:00
{
2016-04-23 14:35:32 +00:00
Elf_Word current_position = 0;
if ( string_section ) {
2016-04-23 14:35:32 +00:00
// Strings are addeded to the end of the current section data
2021-09-14 15:35:54 +00:00
current_position =
static_cast<Elf_Word>( string_section->get_size() );
2016-04-23 14:35:32 +00:00
if ( current_position == 0 ) {
char empty_string = '\0';
string_section->append_data( &empty_string, 1 );
current_position++;
}
2021-09-14 15:35:54 +00:00
string_section->append_data(
str, static_cast<Elf_Word>( std::strlen( str ) + 1 ) );
2012-11-27 09:45:28 +00:00
}
return current_position;
}
//------------------------------------------------------------------------------
Elf_Word add_string( const std::string& str )
2012-11-27 09:45:28 +00:00
{
2015-02-21 11:50:32 +00:00
return add_string( str.c_str() );
2012-11-27 09:45:28 +00:00
}
//------------------------------------------------------------------------------
2012-11-27 09:45:28 +00:00
private:
S* string_section;
2012-11-27 09:45:28 +00:00
};
using string_section_accessor = string_section_accessor_template<section>;
using const_string_section_accessor =
string_section_accessor_template<const section>;
2012-11-27 09:45:28 +00:00
} // namespace ELFIO
#endif // ELFIO_STRINGS_HPP