author | insilmaril |
Mon, 08 May 2006 13:25:48 +0000 | |
changeset 319 | b4b4ff36d160 |
parent 0 | 7a96bd401351 |
child 2 | 608f976aa7bb |
child 44 | 2513e153d481 |
permissions | -rw-r--r-- |
insilmaril@0 | 1 |
#include "options.h" |
insilmaril@0 | 2 |
#include <iostream> |
insilmaril@0 | 3 |
|
insilmaril@0 | 4 |
#include <qapplication.h> |
insilmaril@0 | 5 |
|
insilmaril@0 | 6 |
using namespace std; |
insilmaril@0 | 7 |
|
insilmaril@0 | 8 |
|
insilmaril@0 | 9 |
Option::Option() |
insilmaril@0 | 10 |
{ |
insilmaril@0 | 11 |
name=""; |
insilmaril@0 | 12 |
sName=""; |
insilmaril@0 | 13 |
lName=""; |
insilmaril@0 | 14 |
type=SwitchOption; |
insilmaril@0 | 15 |
sarg=""; |
insilmaril@0 | 16 |
active=false; |
insilmaril@0 | 17 |
} |
insilmaril@0 | 18 |
|
insilmaril@0 | 19 |
void Option::set(const QString &n, const OptionType &t, const QString &s, const QString &l) |
insilmaril@0 | 20 |
{ |
insilmaril@0 | 21 |
sName="-"+s; |
insilmaril@0 | 22 |
lName="--"+l; |
insilmaril@0 | 23 |
type=t; |
insilmaril@0 | 24 |
name=n; |
insilmaril@0 | 25 |
} |
insilmaril@0 | 26 |
|
insilmaril@0 | 27 |
QString Option::getName () { return name; } |
insilmaril@0 | 28 |
QString Option::getShort () { return sName; } |
insilmaril@0 | 29 |
QString Option::getLong() { return lName; } |
insilmaril@0 | 30 |
OptionType Option::getType() { return type; } |
insilmaril@0 | 31 |
void Option::setArg(const QString& s) { sarg=s; } |
insilmaril@0 | 32 |
QString Option::getArg() { return sarg; } |
insilmaril@0 | 33 |
void Option::setActive() { active=true; } |
insilmaril@0 | 34 |
bool Option::isActive() { return active; } |
insilmaril@0 | 35 |
|
insilmaril@0 | 36 |
/////////////////////////////////////////////////////////////// |
insilmaril@0 | 37 |
Options::Options() {} |
insilmaril@0 | 38 |
|
insilmaril@0 | 39 |
int Options::parse() |
insilmaril@0 | 40 |
{ |
insilmaril@0 | 41 |
QStringList arglist; |
insilmaril@0 | 42 |
int i=0; |
insilmaril@0 | 43 |
while (i<qApp->argc()) |
insilmaril@0 | 44 |
{ |
insilmaril@0 | 45 |
arglist.append (qApp->argv()[i]); |
insilmaril@0 | 46 |
i++; |
insilmaril@0 | 47 |
} |
insilmaril@0 | 48 |
|
insilmaril@0 | 49 |
// Get program name |
insilmaril@0 | 50 |
progname=arglist.first(); |
insilmaril@0 | 51 |
arglist.pop_front(); |
insilmaril@0 | 52 |
|
insilmaril@0 | 53 |
// Work through rest of options |
insilmaril@0 | 54 |
bool isFile; |
insilmaril@0 | 55 |
OptionList::iterator itopt; |
insilmaril@0 | 56 |
QStringList::iterator itarg; |
insilmaril@0 | 57 |
itarg=arglist.begin(); |
insilmaril@0 | 58 |
while (itarg!=arglist.end()) |
insilmaril@0 | 59 |
{ |
insilmaril@0 | 60 |
isFile=true; |
insilmaril@0 | 61 |
if ((*itarg).left(1)=="-") |
insilmaril@0 | 62 |
{ |
insilmaril@0 | 63 |
// Compare given option to all defined options |
insilmaril@0 | 64 |
itopt=optlist.begin(); |
insilmaril@0 | 65 |
while (itopt!=optlist.end()) |
insilmaril@0 | 66 |
{ |
insilmaril@0 | 67 |
if ((*itarg)==(*itopt).getShort() || |
insilmaril@0 | 68 |
(*itarg)==(*itopt).getLong()) |
insilmaril@0 | 69 |
{ |
insilmaril@0 | 70 |
(*itopt).setActive(); |
insilmaril@0 | 71 |
isFile=false; |
insilmaril@0 | 72 |
if ((*itopt).getType()==StringOption) |
insilmaril@0 | 73 |
{ |
insilmaril@0 | 74 |
itarg++; |
insilmaril@0 | 75 |
if (itarg==arglist.end()) |
insilmaril@0 | 76 |
{ |
insilmaril@0 | 77 |
cout << "Error: argument to option missing\n"; |
insilmaril@0 | 78 |
return 1; |
insilmaril@0 | 79 |
} |
insilmaril@0 | 80 |
(*itopt).setArg (*itarg); |
insilmaril@0 | 81 |
isFile=false; |
insilmaril@0 | 82 |
} |
insilmaril@0 | 83 |
break; |
insilmaril@0 | 84 |
} |
insilmaril@0 | 85 |
itopt++; |
insilmaril@0 | 86 |
} |
insilmaril@0 | 87 |
if (isFile) |
insilmaril@0 | 88 |
{ |
insilmaril@0 | 89 |
cout << "Error: Unknown argument "<<*itarg<<endl; |
insilmaril@0 | 90 |
return 1; |
insilmaril@0 | 91 |
} |
insilmaril@0 | 92 |
} else |
insilmaril@0 | 93 |
filelist.append (*itarg); |
insilmaril@0 | 94 |
itarg++; |
insilmaril@0 | 95 |
} |
insilmaril@0 | 96 |
return 0; |
insilmaril@0 | 97 |
} |
insilmaril@0 | 98 |
|
insilmaril@0 | 99 |
void Options::add (const QString &n, const OptionType &t=SwitchOption, const QString &s="", const QString &l="") |
insilmaril@0 | 100 |
{ |
insilmaril@0 | 101 |
Option o; |
insilmaril@0 | 102 |
o.set (n,t,s,l); |
insilmaril@0 | 103 |
optlist.append (o); |
insilmaril@0 | 104 |
} |
insilmaril@0 | 105 |
|
insilmaril@0 | 106 |
void Options::setHelpText (const QString &s) |
insilmaril@0 | 107 |
{ |
insilmaril@0 | 108 |
helptext=s; |
insilmaril@0 | 109 |
} |
insilmaril@0 | 110 |
|
insilmaril@0 | 111 |
QString Options::getHelpText () |
insilmaril@0 | 112 |
{ |
insilmaril@0 | 113 |
return helptext; |
insilmaril@0 | 114 |
} |
insilmaril@0 | 115 |
|
insilmaril@0 | 116 |
QString Options::getProgramName() |
insilmaril@0 | 117 |
{ |
insilmaril@0 | 118 |
return progname; |
insilmaril@0 | 119 |
} |
insilmaril@0 | 120 |
|
insilmaril@0 | 121 |
QStringList Options::getFileList () |
insilmaril@0 | 122 |
{ |
insilmaril@0 | 123 |
return filelist; |
insilmaril@0 | 124 |
} |
insilmaril@0 | 125 |
|
insilmaril@0 | 126 |
bool Options::isOn(const QString &s) |
insilmaril@0 | 127 |
{ |
insilmaril@0 | 128 |
OptionList::iterator it; |
insilmaril@0 | 129 |
for ( it = optlist.begin(); it != optlist.end(); ++it ) |
insilmaril@0 | 130 |
if ((*it).getName()==s && (*it).isActive() ) |
insilmaril@0 | 131 |
return true; |
insilmaril@0 | 132 |
return false; |
insilmaril@0 | 133 |
} |
insilmaril@0 | 134 |
|
insilmaril@0 | 135 |
QString Options::getArg(const QString &s) |
insilmaril@0 | 136 |
{ |
insilmaril@0 | 137 |
OptionList::iterator it; |
insilmaril@0 | 138 |
for ( it = optlist.begin(); it != optlist.end(); ++it ) |
insilmaril@0 | 139 |
{ |
insilmaril@0 | 140 |
if ((*it).getName()==s) |
insilmaril@0 | 141 |
return (*it).getArg(); |
insilmaril@0 | 142 |
} |
insilmaril@0 | 143 |
return ""; |
insilmaril@0 | 144 |
} |