Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

parser.cc

Go to the documentation of this file.
00001 
00002 #include "parser.h"
00003 
00004 /* OPIE */
00005 #include <opie2/odebug.h>
00006 using namespace Opie::Core;
00007 
00008 /* QT */
00009 #include <qstringlist.h>
00010 
00011 
00012 using namespace OpieTooth;
00013 
00014 namespace {
00015 
00016 
00017     // "Test Foo Bar" (0x3456)
00018     // @param ret Test Foo Bar
00019     // @eturn 13398
00020     // tactic find " (
00021 int convert( const QString& line, QString& ret ) {
00022 //    owarn << "called" << oendl;
00023     ret = QString::null;
00024     int i = 0;
00025     int pos = line.findRev("\" (");
00026     if ( pos > 0 ) { // it shouldn't be at pos 0
00027         ret = line.left(pos ).stripWhiteSpace();
00028         //      owarn << "ret: " << ret.latin1() << oendl;
00029         ret = ret.replace(QRegExp("[\"]"), "");
00030         //owarn << "ret: " << ret.latin1() << oendl;
00031         QString dummy = line.mid(pos + 5 );
00032         //owarn << "dummy: " << dummy.latin1() << oendl;
00033         dummy = dummy.replace(QRegExp("[)]"), "");
00034         //owarn << "dummy: " << dummy.latin1() << oendl;
00035 //        dummy = dummy.remove( dummy.length() -2, 1 ); // remove the )
00036         bool ok;
00037         i = dummy.toInt(&ok, 16 );
00038         //if (ok ) {
00039         //     owarn << "converted " << i << oendl;
00040         //}else owarn << "failed" << oendl;
00041         //owarn << "exiting" << oendl;
00042         return i;
00043     }
00044     //owarn << "output " << i << oendl;
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         //owarn << "line:" << (*it).latin1() << oendl;
00067         if ( (*it).startsWith("Browsing") ) continue;
00068 
00069         if ( (*it).stripWhiteSpace().isEmpty() ) { // line is empty because a new Service begins
00070             owarn << "could add" << oendl;
00071             // now see if complete and add
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) ) ) ;//continue;
00082         if (parseRecHandle( (*it) ) ) ;//continue;
00083         if (parseClassId(   (*it) ) ) ;//continue;
00084         if (parseProtocol(  (*it) ) ) ;//continue;
00085         if (parseProfile(   (*it) ) ) ;//continue;
00086     }
00087     // missed the last one
00088     if (m_complete) {
00089 //        owarn << "adding" << oendl;
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("  " )  ){ // ok now are the informations in place
00132         owarn << "line with class id" << oendl;
00133         owarn << str.latin1() << oendl;
00134 
00135         // "Obex Object Push" (0x1105)
00136         //  find backwards the " and the from 0 to pos and the mid pos+1
00137         // then stripWhiteSpace add name replace '"' with ""
00138         // and then convert 0x1105 toInt()
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("  ") ) { // "L2CAP" (0x0100)
00159         owarn << "double protocol filter" << oendl;
00160 
00161         if (!m_protocolAdded ) { // the protocol does neither supply a channel nor port so add it now
00162             Services::ProtocolDescriptor desc(  m_protName,  m_protId );
00163             m_item.insertProtocolDescriptor( desc );
00164         }
00165         m_protocolAdded = false;
00166         { // the find function
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     //  now find
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 }

Generated on Sat Nov 5 16:17:43 2005 for OPIE by  doxygen 1.4.2