fixed qgetoptz

This commit is contained in:
Federico Ponchio 2014-05-07 09:04:34 +00:00
parent 7b18a903ec
commit cf2495aaac
5 changed files with 65 additions and 64 deletions

View File

@ -8,7 +8,7 @@
#include <QMutex>
#include <QSemaphore>
namespace mt{
namespace mt {
typedef QThread thread;
typedef QMutex mutex;
typedef QMutexLocker mutexlocker;

View File

@ -12,38 +12,43 @@ class condition;
class mutex
{
MT_PREVENT_COPY(mutex)
MT_PREVENT_COPY(mutex)
public:
public:
typedef mutex this_type;
typedef void base_type;
typedef mutex this_type;
typedef void base_type;
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
bool tryLock(void)
{
int a = pthread_mutex_trylock(&(this->m));
return a == 0;
}
private:
private:
friend class condition;
friend class condition;
pthread_mutex_t m;
pthread_mutex_t m;
};
}

View File

@ -6,6 +6,7 @@
#include "base.h"
#include <pthread.h>
#include <signal.h>
namespace mt
{

View File

@ -213,6 +213,28 @@ void GetOpt::parse() {
}
}
bool GetOpt::assignOption(Option &o, QString arg, QString &error) {
QVariant::Type type;
if(o.value) type = o.value->type();
if(o.string_value) type = QVariant::String;
if(o.double_value) type = QVariant::Double;
if(o.int_value) type = QVariant::Int;
if(o.boolean_value) type = QVariant::Bool;
QVariant v(arg);
if(!v.canConvert(type) || !v.convert(type)) {
error = "Error while parsing option " + o.name + ": cannot convert " +
arg + " to: " + v.typeName();
return false;
}
if(o.value) *(o.value) = v;
if(o.string_value) *(o.string_value) = v.toString();
if(o.double_value) *(o.double_value) = v.toDouble();
if(o.int_value) *(o.int_value) = v.toInt();
if(o.boolean_value) *(o.boolean_value) = v.toBool();
return true;
}
bool GetOpt::parse(QString &error) {
for(int i = 0; i < args.size(); i++) {
QString arg = args[i];
@ -245,7 +267,7 @@ bool GetOpt::parse(QString &error) {
error = "Missing argument after option '" + arg + "'";
return false;
}
if(!parseOption(o, arg))
if(!assignOption(o, arg, error))
return false;
}
@ -273,20 +295,24 @@ bool GetOpt::parse(QString &error) {
error = "Missing argument after option '" + arg + "'";
return false;
}
if(!assignOption(o, arg, error))
return false;
/*
QVariant v(arg);
if(!v.canConvert(o.value->type()) || !v.convert(o.value->type())) {
error = "Error while parsing option " + o.name + ": cannot convert " +
arg + " to: " + o.value->typeName();
return false;
}
*(o.value) = v;
*(o.value) = v; */
}
//argument
} else {
arguments.push_back(arg);
}
}
//test arguments
//regular arguments
for(int i = 0; i < options.size(); i++) {
Option &o = options[i];
if(o.type != Option::ARGUMENT) continue;
@ -294,25 +320,16 @@ bool GetOpt::parse(QString &error) {
error = "Too few arguments, could not parse argument '" + o.name + "'";
return false;
}
if(!parseOption(o, arguments.front()))
if(!assignOption(o, arguments.front(), error))
return false;
arguments.pop_front();
}
//test arguments
//optional arguments
for(int i = 0; i < options.size(); i++) {
Option &o = options[i];
if(o.type != Option::OPTIONAL) continue;
if(arguments.isEmpty()) break;
if(!parseOption(o, arguments.front()))
return false;
arguments.pop_front();
}
//test arguments
for(int i = 0; i < options.size(); i++) {
Option &o = options[i];
if(o.type != Option::ARGUMENT) continue;
if(arguments.isEmpty()) break;
if(!parseOption(o, arguments.front()))
if(!assignOption(o, arguments.front(), error))
return false;
arguments.pop_front();
}
@ -364,26 +381,3 @@ QString GetOpt::formatDesc(QString desc, int len) {
}
return output;
}
bool GetOpt::parseOption(GetOpt::Option &o, const QString &arg) {
QVariant::Type type;
if(o.value) type = o.value->type();
if(o.string_value) type = QVariant::String;
if(o.double_value) type = QVariant::Double;
if(o.int_value) type = QVariant::Int;
if(o.boolean_value) type = QVariant::Bool;
QVariant v(arg);
if(!v.canConvert(type) || !v.convert(type)) {
cerr << "Error while parsing option " << qPrintable(o.name) << ": cannot convert " <<
qPrintable(arg) << " to: " << qPrintable(v.typeName()) << endl;
return false;
}
if(o.value)
*(o.value) = v;
if(o.string_value) *(o.string_value) = v.toString();
if(o.double_value) *(o.double_value) = v.toDouble();
if(o.int_value) *(o.int_value) = v.toInt();
if(o.boolean_value) *(o.boolean_value) = v.toBool();
return true;
}

View File

@ -113,7 +113,7 @@ class GetOpt {
//return argv[0]
QString &applicationName();
protected:
protected:
//parses and return true on success
bool parse(QString &error);
//return options or switch
@ -122,8 +122,9 @@ class GetOpt {
bool findArg(const QString &name, Option &option);
//split desc into n pieces of the right length TODO: check for newlines also
QString formatDesc(QString desc, int len);
//manage conversion from string to option value
bool assignOption(Option &option, QString arg, QString &error);
bool parseOption(Option &option, const QString &arg);
};
#endif