Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Jamulus.pro
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ HEADERS += src/buffer.h \
src/recorder/jamrecorder.h \
src/recorder/creaperproject.h \
src/recorder/cwavestream.h \
src/signalhandler.h
src/signalhandler.h \
src/cmdline.h


!contains(CONFIG, "serveronly") {
HEADERS += src/client.h \
Expand Down Expand Up @@ -490,7 +492,8 @@ SOURCES += src/buffer.cpp \
src/util.cpp \
src/recorder/jamrecorder.cpp \
src/recorder/creaperproject.cpp \
src/recorder/cwavestream.cpp
src/recorder/cwavestream.cpp \
src/cmdline.cpp

!contains(CONFIG, "serveronly") {
SOURCES += src/client.cpp \
Expand Down
226 changes: 226 additions & 0 deletions src/cmdline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/******************************************************************************\
* Copyright (c) 2022
*
* Author(s):
* Peter Goderie (pgScorpio)
*
******************************************************************************
*
* 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.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/

#include "cmdline.h"
#include <QDebug>

/************************************************
Statics to be assigned from main ()
************************************************/

int CCommandline::argc = 0;
char** CCommandline::argv = NULL;

CCommandline::CCommandline ( tOnArgumentError* onArgumentErrorFunc ) : onArgumentError ( onArgumentErrorFunc )
{
if ( ( argc == 0 ) || ( argv == NULL ) )
{
QString errmsg ( "CCommandline::argc and CCommandline::argv are not initialized from main() !" );

if ( onArgumentErrorFunc )
{
onArgumentErrorFunc ( errmsg );
}
else
{
qCritical() << errmsg;
exit ( 1 );
}
}

reset();
}

int CCommandline::GetArgumentList ( QStringList& argumentList )
{
argumentList.clear();

for ( int i = 1; i < argc; i++ )
{
argumentList.append ( argv[i] );
}

return argumentList.size();
}

/************************************************
Get full command line as a single string
************************************************/

QString CCommandline::Commandline()
{
QString cmdline = argv[0];

for ( int i = 1; i < argc; i++ )
{
cmdline += " ";
cmdline += argv[i];
}

return cmdline;
}

QString CCommandline::Argument ( int i ) // i = 1..ArgumentCount()
{
if ( ( i >= 0 ) && ( i < argc ) )
{
return QString ( argv[i] );
}

return QString();
}

/************************************************
sequencial parse functions
using the argument index: 1 <= i < argc
************************************************/

bool CCommandline::GetFlagArgument ( int& i, const QString& strShortOpt, const QString& strLongOpt )
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
return true;
}
else
{
return false;
}
}

bool CCommandline::GetStringArgument ( int& i, const QString& strShortOpt, const QString& strLongOpt, QString& strArg )
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
if ( ++i >= argc )
{
if ( onArgumentError )
{
onArgumentError ( QString ( "%1: '%2' needs a string argument." ).arg ( argv[0] ).arg ( argv[i - 1] ) );
}

return false;
}

strArg = argv[i];

return true;
}
else
{
return false;
}
}

bool CCommandline::GetNumericArgument ( int& i,
const QString& strShortOpt,
const QString& strLongOpt,
double rRangeStart,
double rRangeStop,
double& rValue )
{
if ( ( !strShortOpt.compare ( argv[i] ) ) || ( !strLongOpt.compare ( argv[i] ) ) )
{
QString errmsg = "%1: '%2' needs a numeric argument from '%3' to '%4'.";

if ( ++i >= argc )
{
if ( onArgumentError )
{
onArgumentError ( errmsg.arg ( argv[0] ).arg ( argv[i - 1] ).arg ( rRangeStart ).arg ( rRangeStop ) );
}

return false;
}

char* p;
rValue = strtod ( argv[i], &p );
if ( *p || ( rValue < rRangeStart ) || ( rValue > rRangeStop ) )
{
if ( onArgumentError )
{
onArgumentError ( errmsg.arg ( argv[0] ).arg ( argv[i - 1] ).arg ( rRangeStart ).arg ( rRangeStop ) );
}

if ( rValue < rRangeStart )
{
rValue = rRangeStart;
}
else if ( rValue > rRangeStop )
{
rValue = rRangeStop;
}

return true;
}

return true;
}
else
{
return false;
}
}

/************************************************
find and get a specific argument:
************************************************/

bool CCommandline::GetFlagArgument ( const QString& strShortOpt, const QString& strLongOpt )
{
for ( int i = 1; i < argc; i++ )
{
if ( GetFlagArgument ( i, strShortOpt, strLongOpt ) )
{
return true;
}
}

return false;
}

bool CCommandline::GetStringArgument ( const QString& strShortOpt, const QString& strLongOpt, QString& strArg )
{
for ( int i = 1; i < argc; i++ )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best case would be some kind of hash table/set to have a match in O(1). But that's just thinking out loudly

{
if ( GetStringArgument ( i, strShortOpt, strLongOpt, strArg ) )
{
return true;
}
}

return false;
}

bool CCommandline::GetNumericArgument ( const QString& strShortOpt, const QString& strLongOpt, double rRangeStart, double rRangeStop, double& rValue )
{
for ( int i = 1; i < argc; i++ )
{
if ( GetNumericArgument ( i, strShortOpt, strLongOpt, rRangeStart, rRangeStop, rValue ) )
{
return true;
}
}

return false;
}
Loading