Skip to content
Snippets Groups Projects
functions.cc 2.34 KiB
Newer Older
David Flitney's avatar
David Flitney committed
#include "options.h"

David Flitney's avatar
David Flitney committed
// $Id$

David Flitney's avatar
David Flitney committed
namespace Utilities {

David Flitney's avatar
David Flitney committed
  bool string_to_T(bool &b, const string& s) {
    if(s.length() == 0)
      {
	b = !b;
	return true;
      }
    else if (s == "true")
      {
	b = true;
	return true;
      }
    else if (s == "false")
      {
	b = false;
	return true;
      }
David Flitney's avatar
David Flitney committed
    else
David Flitney's avatar
David Flitney committed
      return false;
David Flitney's avatar
David Flitney committed
  }

David Flitney's avatar
David Flitney committed
  bool string_to_T(string& d, const string& s) {
David Flitney's avatar
David Flitney committed
    d = s;
David Flitney's avatar
David Flitney committed
    return true;
David Flitney's avatar
David Flitney committed
  }

David Flitney's avatar
David Flitney committed
  bool string_to_T(int& i, const string& s) {
    char *endptr = 0; const char *str = s.c_str();
    i = strtol(str, &endptr, 0);
    if(*endptr == str[s.length()])
      return true;
    else
      return false;
David Flitney's avatar
David Flitney committed
  }

David Flitney's avatar
David Flitney committed
  bool string_to_T(float& v, const string& s) {
    char *endptr = 0; const char *str = s.c_str();
    v = strtod(str, &endptr);
    if(*endptr == str[s.length()])
      return true;
    else
      return false;
David Flitney's avatar
David Flitney committed
  }

  bool string_to_T(vector<int>& vi, const string& s) {
    string str=s;
    if(str.find(":")!=string::npos){
      int a = atoi(str.substr(0,str.find(":")).c_str());
      str = str.substr(str.find(":")+1,str.length()-str.find(":")-1);
      int b = atoi(str.substr(0,str.find(":")).c_str());
      str = str.substr(str.find(":")+1,str.length()-str.find(":")-1);
      int c = atoi(str.c_str());
      while(a<=c){
	vi.push_back(a);
	a+=b;
      }
    } else {
      str=str+',';
      vi.assign(0,0);
      while(str.size()) {
	int v = atoi(str.substr(0,str.find(",")).c_str());
	vi.push_back(v);
	str = str.substr(str.find(",")+1,str.length()-str.find(",")-1);
      }
    }
    return true;
  }

  bool string_to_T(vector<float>& vi, const string& s) {
    string str=s;
    if(str.find(":")!=string::npos){
      float a = atof(str.substr(0,str.find(":")).c_str());
      str = str.substr(str.find(":")+1,str.length()-str.find(":")-1);
      float b = atof(str.substr(0,str.find(":")).c_str());
      str = str.substr(str.find(":")+1,str.length()-str.find(":")-1);
      float c = atof(str.c_str());
      while(a<=c){
	vi.push_back(a);
	a+=b;
      }
    } else {
      str=str+',';
      vi.assign(0,0);
      while(str.size()) {
	float v = atof(str.substr(0,str.find(",")).c_str());
	vi.push_back(v);
	str = str.substr(str.find(",")+1,str.length()-str.find(",")-1);
      }
David Flitney's avatar
David Flitney committed
  ostream& operator<<(ostream &os, const BaseOption& o) {
    return os << 
      "\t" << o.key() << 
      "\t" << o.help_text();
  }

David Flitney's avatar
David Flitney committed
}