// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_pointer
#define tools_pointer

//WARNING : touchy.
//WARNING : _MSC_VER && _WIN64 : sizeof(void*) is NOT sizeof(unsigned long).

#include "typedefs"

#include "snpf"

#include <string>

namespace tools {

inline bool to_pointer(const std::string& a_string,void*& a_value){
  upointer v = 0;
  if(::sscanf(a_string.c_str(),upointer_format_x(),&v)!=1) {
    if(::sscanf(a_string.c_str(),upointer_format(),&v)!=1) {
      a_value = 0;
      return false;
    }
  }
  a_value = (void*)v;
  return true;
}

inline bool to_pointer(const char* a_string,void*& a_value){
  upointer v = 0;
  if(::sscanf(a_string,upointer_format_x(),&v)!=1) {
    if(::sscanf(a_string,upointer_format(),&v)!=1) {
      a_value = 0;
      return false;
    }
  }
  a_value = (void*)v;
  return true;
}

inline bool p2s(const void* a_value,std::string& a_s){
  char _s[512];
  snpf(_s,sizeof(_s),upointer_format(),(upointer)a_value);
  a_s = _s;
  return true;
}

inline bool p2sx(const void* a_value,std::string& a_s){
  char _s[512];
  snpf(_s,sizeof(_s),upointer_format_x(),(upointer)a_value);
  a_s = _s;
  return true;
}

/*
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
