lineparser.cpp
00001 /* 00002 * Copyright (C) 2003-2004 Adam Geitgey <adam@rootnode.org> 00003 * Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se> 00004 * Copyright (c) 2005 Ryan Nickell <p0z3r@earthlink.net> 00005 * Copyright (c) 2005 Petri Damsten <damu@iki.fi> 00006 * 00007 * This file is part of SuperKaramba. 00008 * 00009 * SuperKaramba is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * SuperKaramba is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with SuperKaramba; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 ****************************************************************************/ 00023 #include "lineparser.h" 00024 #include <tqregexp.h> 00025 00026 LineParser::LineParser(const TQString& line) 00027 { 00028 set(line); 00029 } 00030 00031 LineParser::~LineParser() 00032 { 00033 } 00034 00035 void LineParser::set(const TQString& line) 00036 { 00037 TQRegExp rx("^\\s*(\\S+)"); 00038 m_line = line; 00039 00040 rx.search(m_line); 00041 m_meter = rx.cap(1).upper(); 00042 } 00043 00044 int LineParser::getInt(TQString w, int def) const 00045 { 00046 TQRegExp rx( "\\W+" + w +"=([-]?\\d+)", false ); 00047 if (rx.search(m_line) != -1) 00048 return rx.cap(1).toInt(); 00049 else 00050 return def; 00051 } 00052 00053 TQColor LineParser::getColor(TQString w, TQColor def) const 00054 { 00055 TQRegExp rx( "\\W+" + w + "=([-]?\\d+),([-]?\\d+),([-]?\\d+)", false ); 00056 if (rx.search(m_line) != -1) 00057 return TQColor(rx.cap(1).toInt(), rx.cap(2).toInt(), rx.cap(3).toInt()); 00058 else 00059 return def; 00060 } 00061 00062 TQString LineParser::getString(TQString w, TQString def) const 00063 { 00064 TQString result; 00065 TQRegExp rx( "\\W+" + w + "=\"([^\"]*)\"", false ); 00066 00067 bool found = (rx.search(m_line)==-1)?false:true; 00068 if (rx.cap(1).isEmpty()) 00069 { 00070 rx = TQRegExp(w + "=(\\S+)", false); 00071 found = (rx.search(m_line)==-1)?false:true; 00072 result = rx.cap(1); 00073 } 00074 else 00075 { 00076 result = rx.cap(1); 00077 } 00078 if(found) 00079 return result; 00080 else 00081 return def; 00082 } 00083 00084 bool LineParser::getBoolean(TQString w, bool def) const 00085 { 00086 TQString boolean = getString(w, "-").lower(); 00087 if(boolean == "-") 00088 return def; 00089 else if (boolean == "true") // true / false 00090 return true; 00091 else if (boolean == "1") // 1 / 0 00092 return true; 00093 else if (boolean == "on") // on / off 00094 return true; 00095 return false; 00096 }