// file: $isip/util/diagnostics/diagnose_class/diagnose_class.h
// version: $Id:


// the dmalloc package defines the strdup as a macro which causes parse
// errors when parsing string.h. We simply undef strdup before including
// strings.h. The #include of diagnose_class.h later will redefine
// strdup the dmalloc way if necessary.
//
#ifdef ISIP_DMALLOC
#undef strdup
#endif

// system include files
//
#include <strings.h>

// isip include files 
//
#include "diagnose_class.h"

// method: readCmdline
// 
// arguments:
//  int argc: (input) the argument count from the command line
//  char** argv: (input) the arguments from the command line
//  Integral::DEBUG& debug_level: (output) the debug level
//  long& iteration: (output) the diagnose iterations
//
boolean readCmdline(int argc_a, char** argv_a, Integral::DEBUG& debug_level_a,
		    long& iteration_a) {
    
  // if no optional arguments or too many arguments, return 
  // go to use the default values
  //
  if (argc_a == 1 || argc_a > 4) {
    return true;
  }
  
  // parse the command line arguments
  //
  
  // if the debug_level option exists
  //
  if (strcmp(DEBUG_OPT, (char*)argv_a[1]) == 0) {
    
    // branch on the debug level
    //
    if (strcmp(DEBUG_OPT_NONE, (char*)argv_a[2]) == 0) {
      debug_level_a = Integral::NONE;
    }
    else if (strcmp(DEBUG_OPT_BRIEF, (char*)argv_a[2]) == 0) {
      debug_level_a = Integral::BRIEF;
    }
    else if (strcmp(DEBUG_OPT_DETAILED, (char*)argv_a[2]) == 0) {
      debug_level_a = Integral::DETAILED;
    }
    else if (strcmp(DEBUG_OPT_ALL, (char*)argv_a[2]) == 0) {
      debug_level_a = Integral::ALL;
    }
    else {
      fprintf(stdout, "invalid debug level: try %s, %s, %s, or %s.\n",
	      DEBUG_OPT_NONE, DEBUG_OPT_BRIEF, DEBUG_OPT_DETAILED,
	      DEBUG_OPT_ALL);
      fprintf(stdout, "setting debug level to %s\n\n", DEBUG_OPT_BRIEF);
      debug_level_a = Integral::BRIEF;
    }

    // read the iteration number
    //
    if (argc_a > 3) { 
      iteration_a = atoi(argv_a[3]);
    }
  }
  
  // read the iteration number
  //
  else {
    if (argc_a > 1) { 
      long iteration = atoi(argv_a[1]);
      if (iteration > 0) {
	iteration_a = iteration;
      }
    }
  }
  
  // exit gracefully
  //
  return true;
}
