superkaramba
lineparser.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "lineparser.h"
00024
00025 #include <QRegExp>
00026
00027 LineParser::LineParser(const QString& line)
00028 {
00029 set(line);
00030 }
00031
00032 LineParser::~LineParser()
00033 {}
00034
00035 void LineParser::set(const QString& line)
00036 {
00037 QRegExp rx("^\\s*(\\S+)");
00038 m_line = line;
00039
00040 rx.indexIn(m_line);
00041 m_meter = rx.cap(1).toUpper();
00042 }
00043
00044 int LineParser::getInt(const QString &w, int def) const
00045 {
00046 QRegExp rx("\\W+" + w + "=([-]?\\d+)", Qt::CaseInsensitive);
00047 if (rx.indexIn(m_line) != -1)
00048 return rx.cap(1).toInt();
00049 else
00050 return def;
00051 }
00052
00053 QColor LineParser::getColor(const QString &w, const QColor &def) const
00054 {
00055 QRegExp rx("\\W+" + w + "=([-]?\\d+),([-]?\\d+),([-]?\\d+)(,([-]?\\d+))?", Qt::CaseInsensitive);
00056 if (rx.indexIn(m_line) != -1) {
00057 int alpha = 255;
00058 QString alphaString = rx.cap(4).remove(0, 1);
00059 if (!alphaString.isEmpty()) {
00060 alpha = alphaString.toInt();
00061 }
00062 return QColor(rx.cap(1).toInt(), rx.cap(2).toInt(), rx.cap(3).toInt(), alpha);
00063 } else {
00064 return def;
00065 }
00066 }
00067
00068 QString LineParser::getString(const QString &w, const QString &def) const
00069 {
00070 QString result;
00071 QRegExp rx("\\W+" + w + "=\"([^\"]*)\"", Qt::CaseInsensitive);
00072
00073 bool found = (rx.indexIn(m_line) == -1) ? false : true;
00074 if (rx.cap(1).isEmpty()) {
00075 rx = QRegExp(w + "=(\\S+)", Qt::CaseInsensitive);
00076 found = (rx.indexIn(m_line) == -1) ? false : true;
00077 result = rx.cap(1);
00078 } else {
00079 result = rx.cap(1);
00080 }
00081
00082 result.replace("%quote", "\"");
00083
00084 if (found)
00085 return result;
00086 else
00087 return def;
00088 }
00089
00090 bool LineParser::getBoolean(const QString &w, bool def) const
00091 {
00092 QString boolean = getString(w, "-").toLower();
00093 if (boolean == "-")
00094 return def;
00095 else if (boolean == "true")
00096 return true;
00097 else if (boolean == "1")
00098 return true;
00099 else if (boolean == "on")
00100 return true;
00101 return false;
00102 }