00001
00002 #include "parser.h"
00003
00004
00005 #include <opie2/odebug.h>
00006 using namespace Opie::Core;
00007
00008
00009 #include <qstringlist.h>
00010
00011
00012 using namespace OpieTooth;
00013
00014 namespace {
00015
00016
00017
00018
00019
00020
00021 int convert( const QString& line, QString& ret ) {
00022
00023 ret = QString::null;
00024 int i = 0;
00025 int pos = line.findRev("\" (");
00026 if ( pos > 0 ) {
00027 ret = line.left(pos ).stripWhiteSpace();
00028
00029 ret = ret.replace(QRegExp("[\"]"), "");
00030
00031 QString dummy = line.mid(pos + 5 );
00032
00033 dummy = dummy.replace(QRegExp("[)]"), "");
00034
00035
00036 bool ok;
00037 i = dummy.toInt(&ok, 16 );
00038
00039
00040
00041
00042 return i;
00043 }
00044
00045 return i;
00046 }
00047
00048 };
00049
00050
00051 Parser::Parser(const QString& output ) {
00052 parse( output );
00053 }
00054 void Parser::setText(const QString& output) {
00055 parse( output );
00056 }
00057 Services::ValueList Parser::services() const {
00058 return m_list;
00059 }
00060 void Parser::parse( const QString& string) {
00061 m_list.clear();
00062 m_complete = true;
00063 QStringList list = QStringList::split('\n', string,TRUE );
00064 QStringList::Iterator it;
00065 for (it = list.begin(); it != list.end(); ++it ) {
00066
00067 if ( (*it).startsWith("Browsing") ) continue;
00068
00069 if ( (*it).stripWhiteSpace().isEmpty() ) {
00070 owarn << "could add" << oendl;
00071
00072 if (m_complete ) {
00073 if (!m_item.serviceName().isEmpty() )
00074 m_list.append( m_item );
00075 Services serv;
00076 m_item = serv;
00077 m_complete = true;
00078 continue;
00079 }
00080 }
00081 if (parseName( (*it) ) ) ;
00082 if (parseRecHandle( (*it) ) ) ;
00083 if (parseClassId( (*it) ) ) ;
00084 if (parseProtocol( (*it) ) ) ;
00085 if (parseProfile( (*it) ) ) ;
00086 }
00087
00088 if (m_complete) {
00089
00090 if (!m_item.serviceName().isEmpty() )
00091 m_list.append(m_item );
00092 }
00093 QValueList<Services>::Iterator it2;
00094
00095 if (m_list.isEmpty() )
00096 owarn << "m_list is empty" << oendl;
00097 for (it2 = m_list.begin(); it2 != m_list.end(); ++it2 ) {
00098 owarn << "name " << (*it2).serviceName().latin1() << oendl;
00099 }
00100 }
00101 bool Parser::parseName( const QString& str) {
00102 if (str.startsWith("Service Name:") ) {
00103 m_item.setServiceName( str.mid(13).stripWhiteSpace() );
00104 owarn << m_item.serviceName() << oendl;
00105 return true;
00106 }
00107 return false;
00108 }
00109 bool Parser::parseRecHandle( const QString& str) {
00110 if (str.startsWith("Service RecHandle:" ) ) {
00111 QString out = str.mid(18 ).stripWhiteSpace();
00112 owarn << "out " << out.latin1() << oendl;
00113 int value = out.mid(2).toInt(&m_ok, 16 );
00114 if (m_ok && (value != -1) )
00115 m_complete = true;
00116 else
00117 m_complete = false;
00118 owarn << "rec handle " << value << oendl;
00119 m_item.setRecHandle( value );
00120 return true;
00121
00122 }
00123 return false;
00124 }
00125 bool Parser::parseClassId( const QString& str) {
00126 if (str.startsWith("Service Class ID List:") ) {
00127 owarn << "found class id" << oendl;
00128 owarn << "line: " << str.latin1() << oendl;
00129 m_classOver = true;
00130 return true;
00131 }else if ( m_classOver && str.startsWith(" " ) ){
00132 owarn << "line with class id" << oendl;
00133 owarn << str.latin1() << oendl;
00134
00135
00136
00137
00138
00139 QString classes;
00140 int ids;
00141 ids = convert( str, classes );
00142 owarn << "ids " << ids << oendl;
00143 m_item.insertClassId( ids, classes );
00144
00145 return true;
00146 }else{
00147 owarn << "Else " << m_classOver << oendl;
00148 m_classOver = false;
00149 }
00150 return false;
00151 }
00152 bool Parser::parseProtocol( const QString& str) {
00153 if (str.startsWith("Protocol Descriptor List:") ) {
00154 m_protocolOver = true;
00155 m_protocolAdded = false;
00156 return true;
00157
00158 }else if (m_protocolOver && str.startsWith(" ") ) {
00159 owarn << "double protocol filter" << oendl;
00160
00161 if (!m_protocolAdded ) {
00162 Services::ProtocolDescriptor desc( m_protName, m_protId );
00163 m_item.insertProtocolDescriptor( desc );
00164 }
00165 m_protocolAdded = false;
00166 {
00167 m_protId = convert(str, m_protName );
00168 }
00169 return true;
00170 }else if (m_protocolOver && str.startsWith(" ") ) {
00171 owarn << "tripple protocol filter" << oendl;
00172 m_protocolAdded = true;
00173 QString dummy = str.stripWhiteSpace();
00174 int pos = dummy.findRev(':');
00175 if ( pos > -1 ) {
00176 int port = dummy.mid(pos+1 ).stripWhiteSpace().toInt();
00177 Services::ProtocolDescriptor desc( m_protName, m_protId, port );
00178 m_item.insertProtocolDescriptor( desc );
00179 }
00180 return true;
00181 }else if (m_protocolOver ) {
00182 m_protocolOver = false;
00183 }
00184 return false;
00185 }
00186 bool Parser::parseProfile( const QString& str) {
00187 if (str.startsWith("Profile Descriptor List:") ) {
00188 m_profOver = true;
00189 }else if ( m_profOver && str.startsWith(" ") ) {
00190 m_profId = convert( str, m_profName );
00191 }else if ( m_profOver && str.startsWith(" ") ) {
00192
00193 int pos = str.findRev(':');
00194 if ( pos > 0 ) {
00195 int dummy = str.mid(pos+1 ).stripWhiteSpace().toInt();
00196 owarn << "dummyInt: " << dummy << oendl;
00197 Services::ProfileDescriptor desc( m_profName, m_profId, dummy );
00198 m_item.insertProfileDescriptor(desc);
00199 }
00200 }else
00201 m_profOver = false;
00202
00203
00204 return false;
00205 }