| 1 |
/* |
| 2 |
kwintv, Video4Linux based TV application |
| 3 |
|
| 4 |
Copyright (C) 1998,1999 Moritz Wenk ([email protected]) |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef myDEBUG |
| 22 |
#define pDEBUG |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/utsname.h> |
| 29 |
#include "parsecl.h" |
| 30 |
|
| 31 |
parsecl::parsecl(int _argc, char** _argv, const struct parse_option* o, |
| 32 |
const char* help) |
| 33 |
{ |
| 34 |
argc = _argc; |
| 35 |
argv = _argv; |
| 36 |
opt = o; |
| 37 |
|
| 38 |
const char* f = find('h'); |
| 39 |
if (f) |
| 40 |
{ |
| 41 |
printf("\nUsage: %s %s\n\n", argv[0], help); |
| 42 |
int i = 0; |
| 43 |
while (opt[i].oshort) |
| 44 |
{ |
| 45 |
printf(" %c%c %s%s %s %s\n", |
| 46 |
(opt[i].oshort != ' ') ? '-' : ' ', |
| 47 |
(opt[i].oshort != ' ') ? opt[i].oshort : ' ', |
| 48 |
opt[i].olong ? "--" : "", |
| 49 |
opt[i].olong ? opt[i].olong : "", |
| 50 |
opt[i].options ? opt[i].options : "", |
| 51 |
opt[i].help ? opt[i].help : ""); |
| 52 |
i++; |
| 53 |
} |
| 54 |
printf("\n"); |
| 55 |
exit(1); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
parsecl::~parsecl() |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
int parsecl::getInt(int oid, int odefault, int min, int max) const |
| 64 |
{ |
| 65 |
const char* p = find(oid); |
| 66 |
if (p) |
| 67 |
{ |
| 68 |
int i = atoi(p); |
| 69 |
if (i >= min && i <= max) |
| 70 |
return i; |
| 71 |
} |
| 72 |
|
| 73 |
return odefault; |
| 74 |
} |
| 75 |
|
| 76 |
const char * parsecl::getString(int oid, const char* odefault, const char* options[]) const |
| 77 |
{ |
| 78 |
const char* p = find(oid); |
| 79 |
if (p) |
| 80 |
return p; |
| 81 |
|
| 82 |
return odefault; |
| 83 |
} |
| 84 |
|
| 85 |
bool parsecl::getSwitch(int oid, bool odefault) const |
| 86 |
{ |
| 87 |
if (find(oid)) |
| 88 |
return true; |
| 89 |
|
| 90 |
return odefault; |
| 91 |
} |
| 92 |
|
| 93 |
const char* parsecl::find(int oid) const |
| 94 |
{ |
| 95 |
int f = 0; |
| 96 |
while (opt[f].oshort && opt[f].oshort != oid) |
| 97 |
f++; |
| 98 |
if (opt[f].oshort) |
| 99 |
{ |
| 100 |
int i = 0; |
| 101 |
while (i < argc) |
| 102 |
{ |
| 103 |
if (argv[i][0] == '-') |
| 104 |
{ |
| 105 |
int x = 2; |
| 106 |
if (argv[i][1] != '-') |
| 107 |
{ |
| 108 |
if (argv[i][1] == opt[f].oshort && argv[i][2] == 0) |
| 109 |
break; // find |
| 110 |
} |
| 111 |
else if (argv[i][2] == 0) |
| 112 |
{ |
| 113 |
i = argc; |
| 114 |
break; // end of args |
| 115 |
} |
| 116 |
else |
| 117 |
x++; |
| 118 |
|
| 119 |
if (strcmp(&argv[i][x], opt[f].olong) == 0) |
| 120 |
break; // find |
| 121 |
} |
| 122 |
i++; |
| 123 |
} |
| 124 |
|
| 125 |
if (opt[f].options) |
| 126 |
{ |
| 127 |
if (i < argc) |
| 128 |
{ |
| 129 |
const char* p = strchr(argv[i], '='); |
| 130 |
if (p) |
| 131 |
p++; |
| 132 |
else if (i + 1 < argc) |
| 133 |
p = argv[i++]; |
| 134 |
return p; |
| 135 |
} |
| 136 |
} |
| 137 |
return argv[i]; |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |