1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <cstdint>
#include <iomanip>
using namespace boost::bimaps ;
struct name_tag {};
struct address_tag {};
using DWORD = std::uint32_t ;
using name_address_map_type = bimap< unordered_set_of< tagged<std::string,name_tag> >,
unordered_set_of< tagged<DWORD,address_tag> > >;
const name_address_map_type& name_address_map()
{
static const name_address_map_type::value_type entries[] =
{
{ "ADRFCH", 0X0004 },
{ "OVRSTK", 0X0018 },
{ "FCHRTN", 0X0020 },
{ "X<>ROW", 0X0026 },
{ "XROW0", 0X002F },
{ "XROW10", 0X0031 },
// ...
{ "POWOFP", 0X7FF7 },
{ "I/OSVP", 0X7FF8 },
{ "DEEPSP", 0X7FF9 },
{ "COLDSP", 0X7FFA },
{ "PRTID", 0X7FFB },
{ "CKSUMP", 0X7FFF },
};
static const name_address_map_type map { std::begin(entries), std::end(entries) } ;
return map ;
}
const std::string& find_global_name( DWORD address )
{
static const name_address_map_type& map = name_address_map() ;
static const auto view = map.by<address_tag>() ;
static const std::string nothing ; // return this (empty) string if entry is not found
const auto iter = view.find(address) ;
return iter != view.end() ? iter->get<name_tag>() : nothing ;
}
DWORD find_global_address( const std::string& name )
{
static const name_address_map_type& map = name_address_map() ;
static const auto view = map.by<name_tag>() ;
const auto iter = view.find(name) ;
return iter != view.end() ? iter->get<address_tag>() : 0 ; // return zero if entry is not found
}
int main()
{
std::cout << std::hex << std::showbase << std::uppercase << std::internal << std::setfill('0') ;
for( DWORD addr : { 0X0018, 0X7FF8, 0X0026, 0XABCD } )
std::cout << std::setw(6) << addr << " => " << std::quoted( find_global_name(addr) ) << '\n' ;
std::cout << '\n' ;
for( std::string name : { "OVRSTK", "I/OSVP", "X<>ROW", "no such name" } )
std::cout << std::quoted(name) << " => " << std::setw(6) << find_global_address(name) << '\n' ;
}
|