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

#ifndef tools_get_env
#define tools_get_env

#include <cstdlib>

#include "sto"

namespace tools {

inline bool is_env(const std::string& a_string){
  const char* env = ::getenv(a_string.c_str());
  return (env?true:false);
}

inline bool get_env(const std::string& a_string,std::string& a_value){
  const char* env = ::getenv(a_string.c_str());
  if(env) {
    a_value = std::string(env?env:"");
    return true;
  } else {
    a_value.clear();
    return false;
  }
}

template <class T>
inline bool get_env(const std::string& a_string,T& a_v,const T& a_def = T()){
  std::string _s;
  if(!get_env(a_string,_s)) {a_v = a_def;return false;}
  return to<T>(_s,a_v,a_def);
}

inline bool get_env_bool(const std::string& a_string,bool& a_v){
  std::string _s;
  if(!get_env(a_string,_s)) return false;
  return to(_s,a_v);
}

}

#endif
