dcop Library API Documentation

marshall.cpp

00001 /*****************************************************************
00002 Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00017 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020 
00021 ******************************************************************/
00022 
00023 #define KDE_QT_ONLY
00024 #include "../../kdecore/kurl.cpp"
00025 
00026 bool mkBool( const QString& s )
00027 {
00028     if ( s.lower()  == "true" )
00029     return true;
00030     if ( s.lower()  == "yes" )
00031     return true;
00032     if ( s.lower()  == "on" )
00033     return true;
00034     if ( s.toInt() != 0 )
00035     return true;
00036 
00037     return false;
00038 }
00039 
00040 QPoint mkPoint( const QString &str )
00041 {
00042     const char *s = str.latin1();
00043     char *end;
00044     while(*s && !isdigit(*s)) s++;
00045     int x = strtol(s, &end, 10);
00046     s = (const char *)end;
00047     while(*s && !isdigit(*s)) s++;
00048     int y = strtol(s, &end, 10);
00049     return QPoint( x, y );
00050 }
00051 
00052 QSize mkSize( const QString &str )
00053 {
00054     const char *s = str.latin1();
00055     char *end;
00056     while(*s && !isdigit(*s)) s++;
00057     int w = strtol(s, &end, 10);
00058     s = (const char *)end;
00059     while(*s && !isdigit(*s)) s++;
00060     int h = strtol(s, &end, 10);
00061     return QSize( w, h );
00062 }
00063 
00064 QRect mkRect( const QString &str )
00065 {
00066     const char *s = str.latin1();
00067     char *end;
00068     while(*s && !isdigit(*s)) s++;
00069     int p1 = strtol(s, &end, 10);
00070     s = (const char *)end;
00071     bool legacy = (*s == 'x');
00072     while(*s && !isdigit(*s)) s++;
00073     int p2 = strtol(s, &end, 10);
00074     s = (const char *)end;
00075     while(*s && !isdigit(*s)) s++;
00076     int p3 = strtol(s, &end, 10);
00077     s = (const char *)end;
00078     while(*s && !isdigit(*s)) s++;
00079     int p4 = strtol(s, &end, 10);
00080     if (legacy)
00081     {
00082        return QRect( p3, p4, p1, p2 );
00083     }
00084     return QRect( p1, p2, p3, p4 );
00085 }
00086 
00087 QColor mkColor( const QString& s )
00088 {
00089     QColor c;
00090     c.setNamedColor(s);
00091     return c;
00092 }
00093 
00094 const char *qStringToC(const QCString &s)
00095 {
00096    if (s.isEmpty())
00097       return "";
00098    return s.data();
00099 }
00100 
00101 QCString demarshal( QDataStream &stream, const QString &type )
00102 {
00103     QCString result;
00104 
00105     if ( type == "int" )
00106     {
00107         int i;
00108         stream >> i;
00109         result.setNum( i );
00110     } else if ( type == "uint" || type == "Q_UINT32" )
00111     {
00112         uint i;
00113         stream >> i;
00114         result.setNum( i );
00115     } else if ( type == "long" )
00116     {
00117         long l;
00118         stream >> l;
00119         result.setNum( l );
00120     } else if ( type == "float" )
00121     {
00122         float f;
00123         stream >> f;
00124         result.setNum( f, 'f' );
00125     } else if ( type == "double" )
00126     {
00127         double d;
00128         stream >> d;
00129         result.setNum( d, 'f' );
00130     } else if ( type == "Q_UINT64" ) {
00131         Q_UINT64 i;
00132         stream >> i;
00133         result.sprintf( "%llu", i );
00134     } else if ( type == "bool" )
00135     {
00136         bool b;
00137         stream >> b;
00138         result = b ? "true" : "false";
00139     } else if ( type == "QString" )
00140     {
00141         QString s;
00142         stream >> s;
00143         result = s.local8Bit();
00144     } else if ( type == "QCString" )
00145     {
00146         stream >> result;
00147     } else if ( type == "QCStringList" )
00148     {
00149         return demarshal( stream, "QValueList<QCString>" );
00150     } else if ( type == "QStringList" )
00151     {
00152         return demarshal( stream, "QValueList<QString>" );
00153     } else if ( type == "QColor" )
00154     {
00155         QColor c;
00156         stream >> c;
00157         result = c.name().local8Bit();
00158     } else if ( type == "QSize" )
00159     {
00160         QSize s;
00161         stream >> s;
00162         result.sprintf( "%dx%d", s.width(), s.height() );
00163     } else if ( type == "QPoint" )
00164     {
00165         QPoint p;
00166         stream >> p;
00167         result.sprintf( "+%d+%d", p.x(), p.y() );
00168     } else if ( type == "QRect" )
00169     {
00170         QRect r;
00171         stream >> r;
00172         result.sprintf( "%dx%d+%d+%d", r.width(), r.height(), r.x(), r.y() );
00173     } else if ( type == "QVariant" )
00174     {
00175         Q_INT32 type;
00176         stream >> type;
00177         return demarshal( stream, QVariant::typeToName( (QVariant::Type)type ) );
00178     } else if ( type == "DCOPRef" )
00179     {
00180         DCOPRef r;
00181         stream >> r;
00182         result.sprintf( "DCOPRef(%s,%s)", qStringToC(r.app()), qStringToC(r.object()) );
00183     } else if ( type == "KURL" )
00184     {
00185         KURL r;
00186         stream >> r;
00187         result = r.url().local8Bit();
00188     } else if ( type.left( 11 ) == "QValueList<" )
00189     {
00190         if ( (uint)type.find( '>', 11 ) != type.length() - 1 )
00191             return result;
00192 
00193         QString nestedType = type.mid( 11, type.length() - 12 );
00194 
00195         if ( nestedType.isEmpty() )
00196             return result;
00197 
00198         Q_UINT32 count;
00199         stream >> count;
00200 
00201         Q_UINT32 i = 0;
00202         for (; i < count; ++i )
00203         {
00204             QCString arg = demarshal( stream, nestedType );
00205             if ( arg.isEmpty() )
00206                 continue;
00207 
00208             result += arg;
00209 
00210             if ( i < count - 1 )
00211                 result += '\n';
00212         }
00213     } else if ( type.left( 5 ) == "QMap<" )
00214     {
00215         int commaPos = type.find( ',', 5 );
00216 
00217         if ( commaPos == -1 )
00218             return result;
00219 
00220         if ( (uint)type.find( '>', commaPos ) != type.length() - 1 )
00221             return result;
00222 
00223         QString keyType = type.mid( 5, commaPos - 5 );
00224         QString valueType = type.mid( commaPos + 1, type.length() - commaPos - 2 );
00225 
00226         Q_UINT32 count;
00227         stream >> count;
00228 
00229         Q_UINT32 i = 0;
00230         for (; i < count; ++i )
00231         {
00232             QCString key = demarshal( stream, keyType );
00233 
00234             if ( key.isEmpty() )
00235                 continue;
00236 
00237             QCString value = demarshal( stream, valueType );
00238 
00239             if ( value.isEmpty() )
00240                 continue;
00241 
00242             result += key + "->" + value;
00243 
00244             if ( i < count - 1 )
00245                 result += '\n';
00246         }
00247     }
00248     else
00249     {
00250        result.sprintf( "<%s>", type.latin1());
00251     }
00252 
00253     return result;
00254 
00255 }
00256 
00257 void marshall( QDataStream &arg, QCStringList args, uint &i, QString type )
00258 {
00259     if (type == "QStringList")
00260        type = "QValueList<QString>";
00261     if (type == "QCStringList")
00262        type = "QValueList<QCString>";
00263     if( i >= args.count() )
00264     {
00265     qWarning("Not enough arguments.");
00266     exit(1);
00267     }
00268     QString s = QString::fromLocal8Bit( args[ i ] );
00269 
00270     if ( type == "int" )
00271     arg << s.toInt();
00272     else if ( type == "uint" )
00273     arg << s.toUInt();
00274     else if ( type == "unsigned" )
00275     arg << s.toUInt();
00276     else if ( type == "unsigned int" )
00277     arg << s.toUInt();
00278     else if ( type == "Q_UINT32" )
00279     arg << s.toUInt();
00280     else if ( type == "Q_UINT64" ) {
00281     QVariant qv = QVariant( s );
00282     arg << qv.toULongLong();
00283     }
00284     else if ( type == "long" )
00285     arg << s.toLong();
00286     else if ( type == "long int" )
00287     arg << s.toLong();
00288     else if ( type == "unsigned long" )
00289     arg << s.toULong();
00290     else if ( type == "unsigned long int" )
00291     arg << s.toULong();
00292     else if ( type == "float" )
00293     arg << s.toFloat();
00294     else if ( type == "double" )
00295     arg << s.toDouble();
00296     else if ( type == "bool" )
00297     arg << mkBool( s );
00298     else if ( type == "QString" )
00299     arg << s;
00300     else if ( type == "QCString" )
00301     arg << QCString( args[ i ] );
00302     else if ( type == "QColor" )
00303     arg << mkColor( s );
00304     else if ( type == "QPoint" )
00305     arg << mkPoint( s );
00306     else if ( type == "QSize" )
00307     arg << mkSize( s );
00308     else if ( type == "QRect" )
00309     arg << mkRect( s );
00310     else if ( type == "KURL" )
00311     arg << KURL( s );
00312     else if ( type == "QVariant" ) {
00313     if ( s == "true" || s == "false" )
00314         arg << QVariant( mkBool( s ), 42 );
00315     else if ( s.left( 4 ) == "int(" )
00316         arg << QVariant( s.mid(4, s.length()-5).toInt() );
00317     else if ( s.left( 7 ) == "QPoint(" )
00318         arg << QVariant( mkPoint( s.mid(7, s.length()-8) ) );
00319     else if ( s.left( 6 ) == "QSize(" )
00320         arg << QVariant( mkSize( s.mid(6, s.length()-7) ) );
00321     else if ( s.left( 6 ) == "QRect(" )
00322         arg << QVariant( mkRect( s.mid(6, s.length()-7) ) );
00323     else if ( s.left( 7 ) == "QColor(" )
00324         arg << QVariant( mkColor( s.mid(7, s.length()-8) ) );
00325     else
00326         arg << QVariant( s );
00327     } else if ( type.startsWith("QValueList<") ||
00328             type == "KURL::List" ) {
00329     if ( type == "KURL::List" )
00330             type = "KURL";
00331         else
00332         type = type.mid(11, type.length() - 12);
00333     QStringList list;
00334     QString delim = s;
00335     if (delim == "[")
00336        delim = "]";
00337     if (delim == "(")
00338        delim = ")";
00339     i++;
00340     QByteArray dummy_data;
00341     QDataStream dummy_arg(dummy_data, IO_WriteOnly);
00342 
00343     uint j = i;
00344     uint count = 0;
00345     // Parse list to get the count
00346     while (true) {
00347         if( j > args.count() )
00348         {
00349         qWarning("List end-delimiter '%s' not found.", delim.latin1());
00350         exit(1);
00351         }
00352         if( QString::fromLocal8Bit( args[ j ] ) == delim )
00353         break;
00354         marshall( dummy_arg, args, j, type );
00355         count++;
00356     }
00357     arg << (Q_UINT32) count;
00358     // Parse the list for real
00359     while (true) {
00360         if( i > args.count() )
00361         {
00362         qWarning("List end-delimiter '%s' not found.", delim.latin1());
00363         exit(1);
00364         }
00365         if( QString::fromLocal8Bit( args[ i ] ) == delim )
00366         break;
00367         marshall( arg, args, i, type );
00368     }
00369     } else {
00370     qWarning( "cannot handle datatype '%s'", type.latin1() );
00371     exit(1);
00372     }
00373     i++;
00374 }
00375 
00376 // vim: set noet ts=8 sts=4 sw=4:
00377 
KDE Logo
This file is part of the documentation for dcop Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:33:23 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003