// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_pointer #define tools_pointer //WARNING : touchy #include "snpf" #include namespace tools { inline bool to_pointer(const std::string& a_string,void*& a_value){ if(sizeof(unsigned long)==sizeof(void*)) { //majority of cases. unsigned long v = 0L; if(::sscanf(a_string.c_str(),"0x%lx",&v)!=1) { if(::sscanf(a_string.c_str(),"%lu",&v)!=1) { a_value = 0; return false; } } a_value = (void*)v; return true; } else if(sizeof(unsigned long long)==sizeof(void*)) { //but not on my Windows-8/64bits ! unsigned long long v = 0L; if(::sscanf(a_string.c_str(),"0x%llx",&v)!=1) { if(::sscanf(a_string.c_str(),"%llu",&v)!=1) { a_value = 0; return false; } } a_value = (void*)v; return true; } a_value = 0; return false; } inline bool p2s(const void* a_value,std::string& a_s){ char s[512]; if(sizeof(unsigned long)==sizeof(void*)) { //majority of cases. snpf(s,sizeof(s),"%lu",(unsigned long)a_value); a_s = s; return true; } else if(sizeof(unsigned long long)==sizeof(void*)) { //but not on my Windows-8/64bits ! snpf(s,sizeof(s),"%llu",(unsigned long long)a_value); a_s = s; return true; } a_s.clear(); return false; } inline bool p2sx(const void* a_value,std::string& a_s){ char s[512]; if(sizeof(unsigned long)==sizeof(void*)) { //majority of cases. snpf(s,sizeof(s),"0x%lx",(unsigned long)a_value); a_s = s; return true; } else if(sizeof(unsigned long long)==sizeof(void*)) { //but not on my Windows-8/64bits ! snpf(s,sizeof(s),"0x%llx",(unsigned long long)a_value); a_s = s; return true; } a_s.clear(); return false; } /* inline std::string p2s(const void* a_value){ char s[512]; snpf(s,sizeof(s),"%lu",(unsigned long)a_value); return s; } inline std::string p2sx(const void* a_value){ char s[512]; snpf(s,sizeof(s),"0x%lx",(unsigned long)a_value); return s; } inline std::string char_p2s(const char* a_value) { char s[512]; snpf(s,sizeof(s),"%lu",(unsigned long)a_value); return std::string(s); } inline std::string long2s(const long a_value) { char s[512]; snpf(s,sizeof(s),"%ld",a_value); return std::string(s); } */ } #endif