inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_pointer 00005 #define inlib_pointer 00006 00007 //WARNING : touchy 00008 00009 //NOTE : on 32 or 64 bits machine, a pointer matches an unsigned long. 00010 00011 #include "typedefs" 00012 00013 #include <string> 00014 #include <cstdio> 00015 00016 namespace inlib { 00017 00018 inline bool to_pointer(const std::string& a_string,void*& a_value){ 00019 unsigned long v = 0L; 00020 if(::sscanf(a_string.c_str(),"0x%lx",&v)!=1) { 00021 if(::sscanf(a_string.c_str(),"%lu",&v)!=1) { 00022 a_value = 0; 00023 return false; 00024 } 00025 } 00026 a_value = (void*)v; 00027 return true; 00028 } 00029 00030 inline std::string p2s(void* a_value){ 00031 char s[512]; 00032 #ifdef WIN32 00033 _snprintf(s,sizeof(s),"%lu",(unsigned long)a_value); 00034 #else 00035 ::snprintf(s,sizeof(s),"%lu",(unsigned long)a_value); 00036 #endif 00037 return s; 00038 } 00039 00040 inline std::string p2sx(void* a_value){ 00041 char s[512]; 00042 #ifdef WIN32 00043 _snprintf(s,sizeof(s),"0x%lx",(unsigned long)a_value); 00044 #else 00045 ::snprintf(s,sizeof(s),"0x%lx",(unsigned long)a_value); 00046 #endif 00047 return s; 00048 } 00049 00050 inline std::string char_p2s(const char* a_value) { 00051 char s[512]; 00052 #ifdef WIN32 00053 _snprintf(s,sizeof(s),"%lu",(unsigned long)a_value); 00054 #else 00055 ::snprintf(s,sizeof(s),"%lu",(unsigned long)a_value); 00056 #endif 00057 return std::string(s); 00058 } 00059 00060 inline std::string long2s(const long a_value) { 00061 char s[512]; 00062 #ifdef WIN32 00063 _snprintf(s,sizeof(s),"%ld",a_value); 00064 #else 00065 ::snprintf(s,sizeof(s),"%ld",a_value); 00066 #endif 00067 return std::string(s); 00068 } 00069 00070 } 00071 00072 #endif