/***************************************************************************
                          CmdLine.h  -  description
                             -------------------
    begin                : Don Jan 29 2004
    copyright            : (C) 2004 by klaus frieler
    email                : kgf@omniversum.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef CMD_LINE
#define CMD_LINE

#include "Types.h"
using namespace  std;

struct Option
{
   Option(const int index, const char* shortOpt, const char *longOpt, const int paramCount = 0)
   :m_nIndex(index), m_sShortOpt(shortOpt), m_sLongOpt(longOpt), m_nParamCount(paramCount){};

   int      m_nIndex;
   string   m_sShortOpt;
   string   m_sLongOpt;
   int      m_nParamCount;
};

struct OptionEntry{
   int   m_nIndex;
   int   m_nStart;
   int   m_nCount;
};

class CmdLine
{
   typedef enum errorType{ ok = 0, notfound, missingparam, doubleopt};
public:
   CmdLine(const Option options[], const uint nOptions , int argc, char *argv[])
   :m_nArgCount(argc), m_sArgv(argv), m_Options(options), m_nOptCount(nOptions), error(ok)
   {}

   bool                 parse();
   bool                 hasOption(const int optIdx) const;
   bool                 getFreeParam(const uint paramIdx, string& param) const;
   bool                 getStringParam(const int optIdx,const uint paramIdx, string &param) const;
   bool                 getIntParam(const int opIdx,const uint paramIdx, int& param) const;
   bool                 getUIntParam(const int opIdx,const uint paramIdx, uint& param) const;
   bool                 getFloatParam(const int opIdx, const uint paramIdx, double& param) const;
   int                  getFreeParamCount() const;
   int                  getParamCount(const int optIdx) const;

   void                 dumpOptions() const;
   
protected:
   int                  m_nArgCount;
   char**               m_sArgv;
   const Option*        m_Options;
   uint                 m_nOptCount;
   vector<OptionEntry>  m_OptList;
   int                  error;

   char                *getParam(const int opIdx, const uint paramIdx) const;
   int                  findOption(const string& option);
   const Option*        getOptionIdx(const char *option);
   int                  getIndex(const int optIdx) const;
   int                  findNextOption(const int start);

};
#endif
