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 == "QPixmap" || type == "QImage" ) 
00164     {
00165         QImage i;
00166         stream >> i;
00167         QByteArray ba;
00168         QBuffer buf( ba );
00169         buf.open( IO_WriteOnly );
00170         i.save( &buf, "XPM" );
00171         result = ba;
00172     } else if ( type == "QPoint" )
00173     {
00174         QPoint p;
00175         stream >> p;
00176         result.sprintf( "+%d+%d", p.x(), p.y() );
00177     } else if ( type == "QRect" )
00178     {
00179         QRect r;
00180         stream >> r;
00181         result.sprintf( "%dx%d+%d+%d", r.width(), r.height(), r.x(), r.y() );
00182     } else if ( type == "QVariant" )
00183     {
00184         Q_INT32 type;
00185         stream >> type;
00186         return demarshal( stream, QVariant::typeToName( (QVariant::Type)type ) );
00187     } else if ( type == "DCOPRef" )
00188     {
00189         DCOPRef r;
00190         stream >> r;
00191         result.sprintf( "DCOPRef(%s,%s)", qStringToC(r.app()), qStringToC(r.object()) );
00192     } else if ( type == "KURL" )
00193     {
00194         KURL r;
00195         stream >> r;
00196         result = r.url().local8Bit();
00197     } else if ( type.left( 11 ) == "QValueList<" )
00198     {
00199         if ( (uint)type.find( '>', 11 ) != type.length() - 1 )
00200             return result;
00201 
00202         QString nestedType = type.mid( 11, type.length() - 12 );
00203 
00204         if ( nestedType.isEmpty() )
00205             return result;
00206 
00207         Q_UINT32 count;
00208         stream >> count;
00209 
00210         Q_UINT32 i = 0;
00211         for (; i < count; ++i )
00212         {
00213             QCString arg = demarshal( stream, nestedType );
00214             if ( arg.isEmpty() )
00215                 continue;
00216 
00217             result += arg;
00218 
00219             if ( i < count - 1 )
00220                 result += '\n';
00221         }
00222     } else if ( type.left( 5 ) == "QMap<" )
00223     {
00224         int commaPos = type.find( ',', 5 );
00225 
00226         if ( commaPos == -1 )
00227             return result;
00228 
00229         if ( (uint)type.find( '>', commaPos ) != type.length() - 1 )
00230             return result;
00231 
00232         QString keyType = type.mid( 5, commaPos - 5 );
00233         QString valueType = type.mid( commaPos + 1, type.length() - commaPos - 2 );
00234 
00235         Q_UINT32 count;
00236         stream >> count;
00237 
00238         Q_UINT32 i = 0;
00239         for (; i < count; ++i )
00240         {
00241             QCString key = demarshal( stream, keyType );
00242 
00243             if ( key.isEmpty() )
00244                 continue;
00245 
00246             QCString value = demarshal( stream, valueType );
00247 
00248             if ( value.isEmpty() )
00249                 continue;
00250 
00251             result += key + "->" + value;
00252 
00253             if ( i < count - 1 )
00254                 result += '\n';
00255         }
00256     }
00257     else
00258     {
00259        result.sprintf( "<%s>", type.latin1());
00260     }
00261 
00262     return result;
00263 
00264 }
00265 
00266 void marshall( QDataStream &arg, QCStringList args, uint &i, QString type )
00267 {
00268     if (type == "QStringList")
00269        type = "QValueList<QString>";
00270     if (type == "QCStringList")
00271        type = "QValueList<QCString>";
00272     if( i >= args.count() )
00273     {
00274     qWarning("Not enough arguments.");
00275     exit(1);
00276     }
00277     QString s = QString::fromLocal8Bit( args[ i ] );
00278 
00279     if ( type == "int" )
00280     arg << s.toInt();
00281     else if ( type == "uint" )
00282     arg << s.toUInt();
00283     else if ( type == "unsigned" )
00284     arg << s.toUInt();
00285     else if ( type == "unsigned int" )
00286     arg << s.toUInt();
00287     else if ( type == "Q_UINT32" )
00288     arg << s.toUInt();
00289     else if ( type == "Q_UINT64" ) {
00290     QVariant qv = QVariant( s );
00291     arg << qv.toULongLong();
00292     }
00293     else if ( type == "long" )
00294     arg << s.toLong();
00295     else if ( type == "long int" )
00296     arg << s.toLong();
00297     else if ( type == "unsigned long" )
00298     arg << s.toULong();
00299     else if ( type == "unsigned long int" )
00300     arg << s.toULong();
00301     else if ( type == "float" )
00302     arg << s.toFloat();
00303     else if ( type == "double" )
00304     arg << s.toDouble();
00305     else if ( type == "bool" )
00306     arg << mkBool( s );
00307     else if ( type == "QString" )
00308     arg << s;
00309     else if ( type == "QCString" )
00310     arg << QCString( args[ i ] );
00311     else if ( type == "QColor" )
00312     arg << mkColor( s );
00313     else if ( type == "QPoint" )
00314     arg << mkPoint( s );
00315     else if ( type == "QSize" )
00316     arg << mkSize( s );
00317     else if ( type == "QRect" )
00318     arg << mkRect( s );
00319     else if ( type == "KURL" )
00320     arg << KURL( s );
00321     else if ( type == "QVariant" ) {
00322     if ( s == "true" || s == "false" )
00323         arg << QVariant( mkBool( s ), 42 );
00324     else if ( s.left( 4 ) == "int(" )
00325         arg << QVariant( s.mid(4, s.length()-5).toInt() );
00326     else if ( s.left( 7 ) == "QPoint(" )
00327         arg << QVariant( mkPoint( s.mid(7, s.length()-8) ) );
00328     else if ( s.left( 6 ) == "QSize(" )
00329         arg << QVariant( mkSize( s.mid(6, s.length()-7) ) );
00330     else if ( s.left( 6 ) == "QRect(" )
00331         arg << QVariant( mkRect( s.mid(6, s.length()-7) ) );
00332     else if ( s.left( 7 ) == "QColor(" )
00333         arg << QVariant( mkColor( s.mid(7, s.length()-8) ) );
00334     else
00335         arg << QVariant( s );
00336     } else if ( type.startsWith("QValueList<") ||
00337             type == "KURL::List" ) {
00338     if ( type == "KURL::List" )
00339             type = "KURL";
00340         else
00341         type = type.mid(11, type.length() - 12);
00342     QStringList list;
00343     QString delim = s;
00344     if (delim == "[")
00345        delim = "]";
00346     if (delim == "(")
00347        delim = ")";
00348     i++;
00349     QByteArray dummy_data;
00350     QDataStream dummy_arg(dummy_data, IO_WriteOnly);
00351 
00352     uint j = i;
00353     uint count = 0;
00354     // Parse list to get the count
00355     while (true) {
00356         if( j > args.count() )
00357         {
00358         qWarning("List end-delimiter '%s' not found.", delim.latin1());
00359         exit(1);
00360         }
00361         if( QString::fromLocal8Bit( args[ j ] ) == delim )
00362         break;
00363         marshall( dummy_arg, args, j, type );
00364         count++;
00365     }
00366     arg << (Q_UINT32) count;
00367     // Parse the list for real
00368     while (true) {
00369         if( i > args.count() )
00370         {
00371         qWarning("List end-delimiter '%s' not found.", delim.latin1());
00372         exit(1);
00373         }
00374         if( QString::fromLocal8Bit( args[ i ] ) == delim )
00375         break;
00376         marshall( arg, args, i, type );
00377     }
00378     } else {
00379     qWarning( "cannot handle datatype '%s'", type.latin1() );
00380     exit(1);
00381     }
00382     i++;
00383 }
00384 
00385 // vim: set noet ts=8 sts=4 sw=4:
00386 
KDE Logo
This file is part of the documentation for dcop Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Feb 18 15:09:47 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003