📄 mgccommand.cpp
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement. The various license agreements may be found at
// the Magic Software web site. This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License/free.pdf
#include "MgcRTLib.h"
#include "MgcCommand.h"
char MgcCommand::ms_pOptionNotFound[] = "option not found";
char MgcCommand::ms_pArgumentRequired[] = "option requires an argument";
char MgcCommand::ms_pArgumentOutOfRange[] = "argument out of range";
char MgcCommand::ms_pFilenameNotFound[] = "filename not found";
//---------------------------------------------------------------------------
MgcCommand::MgcCommand (int iArgc, char** ppArgv)
{
m_iArgc = iArgc;
m_ppArgv = ppArgv;
m_pCmdline = 0;
Initialize();
}
//---------------------------------------------------------------------------
MgcCommand::MgcCommand (char* pCmdline)
{
typedef struct Argument
{
char* pItem;
Argument* pNext;
}
Argument;
m_iArgc = 0;
m_ppArgv = 0;
if ( pCmdline == 0 || strlen(pCmdline) == 0 )
return;
m_pCmdline = new char[strlen(pCmdline)+1];
strcpy(m_pCmdline,pCmdline);
char* pToken = strtok(m_pCmdline," \t");
Argument* pList = 0;
while ( pToken )
{
m_iArgc++;
Argument* pCurrent = new Argument;
pCurrent->pItem = pToken;
pCurrent->pNext = pList;
pList = pCurrent;
pToken = strtok(0," \t");
}
m_iArgc++;
m_ppArgv = new char*[m_iArgc];
m_ppArgv[0] = m_pCmdline;
int i = m_iArgc-1;
while ( pList )
{
m_ppArgv[i--] = pList->pItem;
Argument* pSave = pList->pNext;
delete pList;
pList = pSave;
}
Initialize();
}
//---------------------------------------------------------------------------
MgcCommand::~MgcCommand ()
{
delete[] m_pUsed;
if ( m_pCmdline )
{
delete[] m_ppArgv;
delete[] m_pCmdline;
}
}
//---------------------------------------------------------------------------
void MgcCommand::Initialize ()
{
m_pUsed = new bool[m_iArgc];
memset(m_pUsed,false,m_iArgc*sizeof(bool));
m_dSmall = 0.0;
m_dLarge = 0.0;
m_bMinSet = false;
m_bMaxSet = false;
m_bInfSet = false;
m_bSupSet = false;
}
//---------------------------------------------------------------------------
int MgcCommand::ExcessArguments ()
{
// checks to see if any command line arguments were not processed
for (int k = 1; k < m_iArgc; k++)
{
if ( !m_pUsed[k] )
return k;
}
return 0;
}
//---------------------------------------------------------------------------
MgcCommand& MgcCommand::Min (double dValue)
{
m_dSmall = dValue;
m_bMinSet = true;
return *this;
}
//---------------------------------------------------------------------------
MgcCommand& MgcCommand::Max (double dValue)
{
m_dLarge = dValue;
m_bMaxSet = true;
return *this;
}
//---------------------------------------------------------------------------
MgcCommand& MgcCommand::Inf (double dValue)
{
m_dSmall = dValue;
m_bInfSet = true;
return *this;
}
//---------------------------------------------------------------------------
MgcCommand& MgcCommand::Sup (double dValue)
{
m_dLarge = dValue;
m_bSupSet = true;
return *this;
}
//---------------------------------------------------------------------------
int MgcCommand::Boolean (char* pName)
{
bool bValue = false;
return Boolean(pName,bValue);
}
//---------------------------------------------------------------------------
int MgcCommand::Boolean (char* pName, bool& bValue)
{
int iMatchFound = 0;
bValue = false;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] == '-' && strcmp(pName,++ptr) == 0 )
{
m_pUsed[k] = true;
iMatchFound = k;
bValue = true;
break;
}
}
if ( iMatchFound == 0 )
m_pLastError = ms_pOptionNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
int MgcCommand::Integer (char* pName, int& iValue)
{
int iMatchFound = 0;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] == '-' && strcmp(pName,++ptr) == 0 )
{
// get argument
ptr = m_ppArgv[k+1];
if ( m_pUsed[k+1] || (ptr[0] == '-' && !isdigit(ptr[1])) )
{
m_pLastError = ms_pArgumentRequired;
return 0;
}
sscanf(ptr,"%d",&iValue);
if ( (m_bMinSet && iValue < m_dSmall)
|| (m_bMaxSet && iValue > m_dLarge)
|| (m_bInfSet && iValue <= m_dSmall)
|| (m_bSupSet && iValue >= m_dLarge) )
{
m_pLastError = ms_pArgumentOutOfRange;
return 0;
}
m_pUsed[k] = true;
m_pUsed[k+1] = true;
iMatchFound = k;
break;
}
}
m_bMinSet = false;
m_bMaxSet = false;
m_bInfSet = false;
m_bSupSet = false;
if ( iMatchFound == 0 )
m_pLastError = ms_pOptionNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
int MgcCommand::Float (char* pName, float& fValue)
{
int iMatchFound = 0;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] == '-' && strcmp(pName,++ptr) == 0 )
{
// get argument
ptr = m_ppArgv[k+1];
if ( m_pUsed[k+1] || (ptr[0] == '-' && !isdigit(ptr[1])) )
{
m_pLastError = ms_pArgumentRequired;
return 0;
}
sscanf(ptr,"%f",&fValue);
if ( (m_bMinSet && fValue < m_dSmall)
|| (m_bMaxSet && fValue > m_dLarge)
|| (m_bInfSet && fValue <= m_dSmall)
|| (m_bSupSet && fValue >= m_dLarge) )
{
m_pLastError = ms_pArgumentOutOfRange;
return 0;
}
m_pUsed[k] = true;
m_pUsed[k+1] = true;
iMatchFound = k;
break;
}
}
m_bMinSet = false;
m_bMaxSet = false;
m_bInfSet = false;
m_bSupSet = false;
if ( iMatchFound == 0 )
m_pLastError = ms_pOptionNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
int MgcCommand::Double (char* pName, double& dValue)
{
int iMatchFound = 0;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] == '-' && strcmp(pName,++ptr) == 0 )
{
// get argument
ptr = m_ppArgv[k+1];
if ( m_pUsed[k+1] || (ptr[0] == '-' && !isdigit(ptr[1])) )
{
m_pLastError = ms_pArgumentRequired;
return 0;
}
sscanf(ptr,"%lf",&dValue);
if ( (m_bMinSet && dValue < m_dSmall)
|| (m_bMaxSet && dValue > m_dLarge)
|| (m_bInfSet && dValue <= m_dSmall)
|| (m_bSupSet && dValue >= m_dLarge) )
{
m_pLastError = ms_pArgumentOutOfRange;
return 0;
}
m_pUsed[k] = true;
m_pUsed[k+1] = true;
iMatchFound = k;
break;
}
}
m_bMinSet = false;
m_bMaxSet = false;
m_bInfSet = false;
m_bSupSet = false;
if ( iMatchFound == 0 )
m_pLastError = ms_pOptionNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
int MgcCommand::String (char* pName, char* pValue)
{
int iMatchFound = 0;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] == '-' && strcmp(pName,++ptr) == 0 )
{
// get argument
ptr = m_ppArgv[k+1];
if ( m_pUsed[k+1] || ptr[0] == '-' )
{
m_pLastError = ms_pArgumentRequired;
return 0;
}
strcpy(pValue,ptr);
m_pUsed[k] = true;
m_pUsed[k+1] = true;
iMatchFound = k;
break;
}
}
if ( iMatchFound == 0 )
m_pLastError = ms_pOptionNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
int MgcCommand::Filename (char* pName)
{
int iMatchFound = 0;
for (int k = 1; k < m_iArgc; k++)
{
char* ptr = m_ppArgv[k];
if ( !m_pUsed[k] && ptr[0] != '-' )
{
strcpy(pName,ptr);
m_pUsed[k] = true;
iMatchFound = k;
break;
}
}
if ( iMatchFound == 0 )
m_pLastError = ms_pFilenameNotFound;
return iMatchFound;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -