XGOptions

// Author: Joel Snow
// Date:   2000/11/30
//
// Latest Revision: 2002/07/13 - jms
//
// $Author: snow $
// $Date: 2002/07/16 01:40:21 $
// $Id: XGOptions.cpp.html,v 1.4 2002/07/16 01:40:21 snow Exp $
//
// Based on Jim Kowalkowski's:
// Id: Options.hpp,v 1.14 2000/04/24 03:39:14 jbk Exp

#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <sstream>

using namespace std;
#ifndef NO_SRT

#include "framework/environment.hpp"
#include "framework/utilities/Debug.hpp"
#include "framework/utilities/Utils.hpp"
#include "framework/Error.hpp"

#include "root_gui/private/XGOptions.hpp"

namespace fwk {
#else
#include "XGOptions.hpp"
#endif

static char *rcsid = "@(#) $Id: XGOptions.cpp.html,v 1.4 2002/07/16 01:40:21 snow Exp $";

void XGOptions::usage(ostream& ost) {
  ost << "Help:" << endl;
  ostream_iterator<OptionBase*> oi(ost,"n");
  copy(opts.begin(), opts.end(), oi);
}

XGOptions::~XGOptions() { delete xargv; }

XGOptions::XGOptions():
  is_inited(false),
  geometry("geometry", "WIDTHxHEIGHT+XOFF+YOFF (X geometry)"),
  display("display", "hostname:displaynumber.screennumber (X display)"),
  font("font", "font_name (X display font)"),
  debugLevel("debug","level_number (higher number=more info)"),
  help_set("help", "(print this message)", false),
  width("width", "Frame width (pixels)"),
  height("height", "Frame height (pixels)"),
  xoff("xoff", "Frame x-offset (pixels)"),
  yoff("yoff", "Frame y-offset (pixels)"),
  geoc(0)

{
  opts.push_back(&debugLevel);
  opts.push_back(&help_set);
  opts.push_back(&geometry);
  opts.push_back(&display);
  opts.push_back(&font);

  geo.push_back(&width);
  geo.push_back(&height);
  geo.push_back(&xoff);
  geo.push_back(&yoff);
  xargv = 0;
}

struct runloc
{
  runloc(ostream& o,int ac, char* av[]):ost(o),argc(ac),argv(av) { }
  int operator()(OptionBase* b)
    {
      if(b==0) { ost << "BAD Option Object!" << endl; return 0; }
      if(b->locate(argc,argv)==false)
	{
	  ost << "INVALID OPTION:" << endl << b << endl;
	}
      return 0;
    }
  ostream& ost;
  int argc;
  char** argv;
};

struct geoloc
{
  geoloc(ostream& o,int ac, char* av[]):ost(o),argc(ac),argv(av) { }
  int operator()(OptionBase* b)
    {
      if(b==0) { ost << "BAD Option Object!" << endl; return 0; }
      if(b->locate(argc,argv)==false) { }
      return 0;
    }
  ostream& ost;
  int argc;
  char** argv;
};

int XGOptions::initialize(int argc, char* argv[])
{
  if(is_inited==true) return -1;

  bool failed = false;

  for_each(opts.begin(),opts.end(),runloc(cout,argc,argv));

  if(wantHelp()==true) {
    usage(cerr);
    return -1;
  }

  if (!debugLevel.check()) { 
      cerr << "Bad debug flag syntax!" << endl;
      failed=true;
  } else if(debugLevel>0) {
    int DebugLevel = debugLevel;
    cerr << "****** DebugLevel set to " << DebugLevel << endl;
  }

  if (geometry.isPresent()) {
//      cout << geometry.isValid() << " " << parseGeometry() << endl;
    if (!geometry.isValid() || !parseGeometry()) {
      cerr << "Bad geometry syntax!" << endl;
      failed=true;
    }
  }

  if (display.isPresent()) {
    if (!parseDisplay()) {
      cerr << "Bad display syntax!" << endl;
      failed=true;
    }
  }

  if (!font.check()) { 
      cerr << "Bad font flag syntax!" << endl;
      failed=true;
  }

  if(failed==true) {
    cerr << "For help use the -help option." << endl;
    return -1;
  }
  //+++ prune options from arg list
  xargc = 0;
  if (argc > 1) {
    xargv = new char*[argc];
    for (int i = 1; i<argc; i++) {
      bool found = false;
      if (*argv[i] == '-') {
	list<OptionBase*>::const_iterator p = opts.begin();
	while (p != opts.end()) {
	  if (!strcmp((*p)->getName().c_str(), argv[i])) {
	    i++;
	    found = true;
	    break;
	  }
	  p++;
	}
      }
      if (!found) {
	xargv[xargc] = argv[i];
	xargc++;
      }
    }
  }
  //---

  return 0;
}

void XGOptions::getxargs(int *xc, char* xv[])
{
  for (int i=0; i<xargc; i++) {
    xv[i] = xargv[i];
  }
  *xc = xargc;
}

int XGOptions::pWidth(unsigned int s)
{
  static string width;
  string geometry = Geometry();
  unsigned int i = geometry.find('x',s);
  if (i != string::npos
    width = geometry.substr(0,i);
    s = i;
  } else {
    width = geometry;
    s = geometry.size();
  }
  if (width.size() > 0) { 
    geov[geoc++] = "-width";
    geov[geoc++] = (char*)width.c_str();
  }
  return s;
}

int XGOptions::pHeight(unsigned int s)
{
  static string height;
  string geometry = Geometry();
  if (s == geometry.size()-1) {
    height = "x";
    s = geometry.size();
  } else if (s < geometry.size()) {
    unsigned int j = geometry.find('+',s);
    unsigned int k = geometry.find('-',s);    
    if ((j != string::npos
	(j == string::npos
    if (j == string::npos
      height = geometry.substr(s+1);
      s = geometry.size();
    } else {
      height = geometry.substr(s+1,j-s-1);
      s = j;
    }
  }
  if (height.size() > 0) { 
    geov[geoc++] = "-height";
    geov[geoc++] = (char*)height.c_str();
  }
  return s;
}

int XGOptions::pOffsets(unsigned int s)
{
  static string xoff, yoff;
  string geometry = Geometry();
  if (s < geometry.size()) {
    unsigned int k = geometry.find('+',s+1);
    unsigned int l = geometry.find('-',s+1);
    if ((k != string::npos
	(k == string::npos
      k = l;
    } else if (k == string::npos
      k = geometry.size();
    }
    xoff = geometry.substr(s,k-s);
    if (xoff.size() > 0) { 
      geov[geoc++] = "-xoff";
      geov[geoc++] = (char*)xoff.c_str();
    }

    if (k != string::npos
      yoff = geometry.substr(k);
    if (yoff.size() > 0) { 
      geov[geoc++] = "-yoff";
      geov[geoc++] = (char*)yoff.c_str();
    }
  }
  return s;
}

// parse X geometry string
bool XGOptions::parseGeometry()
{
  geoc = 0;
  string geometry = Geometry();
  int s = 0;
  char c = geometry[s];
  switch (c) {
  case '0': case '1': case '2': case '3': case '4': 
  case '5': case '6': case '7': case '8': 
  case '9': {
    s = pWidth(s);
//      if (s < 0) return false;
  }
  case 'x': {
    s = pHeight(s);
//      if (s < 0) return false;
  }
  case '+':
  case '-': {
    s = pOffsets(s);
    if (s < 0) return false;
    break;
  }
  default: {
//      cout << "syntax error!" << endl;
    return false;
  }
  }

//    for (int z=0; z< geoc; z++) { cout << geov[z] << endl; }
  for_each(geo.begin(),geo.end(),geoloc(cout,geoc,geov));
  return true;
}

bool XGOptions::parseDisplay()
{
  string screen, dspno, host;
  string display = Display();
  unsigned int i = display.find(':');
  if (i != string::npos
    host = display.substr(0,i);
    unsigned int j = display.find('.');
    if (j != string::npos
      dspno = display.substr(i+1,j);
      screen = display.substr(j+1);
    } else {
      dspno = display.substr(i+1);
    }
    char c;
    for (unsigned int k = 0; k<dspno.size(); k++) {
      c = dspno[i];
      if (!isdigit(c)) {
	return false;
      }
    }
    return true;
  } else {
    return false;
  }
}

char* XGOptions::getRCSID()
{
  return rcsid;
}

#ifndef NO_SRT
}
#endif


ROOT page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.