marshall.cpp Source File (dcop Library)

| # dcop Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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.sprintf( "%d", i );
    00110     } else if ( type == "uint" )
    00111     {
    00112         uint i;
    00113         stream >> i;
    00114         result.sprintf( "%d", i );
    00115     } else if ( type == "long" )
    00116     {
    00117         long l;
    00118         stream >> l;
    00119         result.sprintf( "%ld", l );
    00120     } else if ( type == "float" )
    00121     {
    00122         float f;
    00123         stream >> f;
    00124         result.sprintf( "%f", (double)f );
    00125     } else if ( type == "double" )
    00126     {
    00127         double d;
    00128         stream >> d;
    00129         result.sprintf( "%f", d );
    00130     } else if ( type == "bool" )
    00131     {
    00132         bool b;
    00133         stream >> b;
    00134         result = b ? "true" : "false";
    00135     } else if ( type == "QString" )
    00136     {
    00137         QString s;
    00138         stream >> s;
    00139         result = s.local8Bit();
    00140     } else if ( type == "QCString" )
    00141     {
    00142         stream >> result;
    00143     } else if ( type == "QCStringList" )
    00144     {
    00145         return demarshal( stream, "QValueList<QCString>" );
    00146     } else if ( type == "QStringList" )
    00147     {
    00148         return demarshal( stream, "QValueList<QString>" );
    00149     } else if ( type == "QColor" )
    00150     {
    00151         QColor c;
    00152         stream >> c;
    00153         result = c.name().local8Bit();
    00154     } else if ( type == "QSize" )
    00155     {
    00156         QSize s;
    00157         stream >> s;
    00158         result.sprintf( "%dx%d", s.width(), s.height() );
    00159     } else if ( type == "QPoint" )
    00160     {
    00161         QPoint p;
    00162         stream >> p;
    00163         result.sprintf( "+%d+%d", p.x(), p.y() );
    00164     } else if ( type == "QRect" )
    00165     {
    00166         QRect r;
    00167         stream >> r;
    00168         result.sprintf( "%dx%d+%d+%d", r.width(), r.height(), r.x(), r.y() );
    00169     } else if ( type == "QVariant" )
    00170     {
    00171         Q_INT32 type;
    00172         stream >> type;
    00173         return demarshal( stream, QVariant::typeToName( (QVariant::Type)type ) );
    00174     } else if ( type == "DCOPRef" )
    00175     {
    00176         DCOPRef r;
    00177         stream >> r;
    00178         result.sprintf( "DCOPRef(%s,%s)", qStringToC(r.app()), qStringToC(r.object()) );
    00179     } else if ( type == "KURL" )
    00180     {
    00181         KURL r;
    00182         stream >> r;
    00183         result = r.url().local8Bit();
    00184     } else if ( type.left( 11 ) == "QValueList<" )
    00185     {
    00186         if ( (uint)type.find( '>', 11 ) != type.length() - 1 )
    00187             return result;
    00188
    00189         QString nestedType = type.mid( 11, type.length() - 12 );
    00190
    00191         if ( nestedType.isEmpty() )
    00192             return result;
    00193
    00194         Q_UINT32 count;
    00195         stream >> count;
    00196
    00197         Q_UINT32 i = 0;
    00198         for (; i < count; ++i )
    00199         {
    00200             QCString arg = demarshal( stream, nestedType );
    00201             if ( arg.isEmpty() )
    00202                 continue;
    00203
    00204             result += arg;
    00205
    00206             if ( i < count - 1 )
    00207                 result += '\n';
    00208         }
    00209     } else if ( type.left( 5 ) == "QMap<" )
    00210     {
    00211         int commaPos = type.find( ',', 5 );
    00212
    00213         if ( commaPos == -1 )
    00214             return result;
    00215
    00216         if ( (uint)type.find( '>', commaPos ) != type.length() - 1 )
    00217             return result;
    00218
    00219         QString keyType = type.mid( 5, commaPos - 5 );
    00220         QString valueType = type.mid( commaPos + 1, type.length() - commaPos - 2 );
    00221
    00222         Q_UINT32 count;
    00223         stream >> count;
    00224
    00225         Q_UINT32 i = 0;
    00226         for (; i < count; ++i )
    00227         {
    00228             QCString key = demarshal( stream, keyType );
    00229
    00230             if ( key.isEmpty() )
    00231                 continue;
    00232
    00233             QCString value = demarshal( stream, valueType );
    00234
    00235             if ( value.isEmpty() )
    00236                 continue;
    00237
    00238             result += key + "->" + value;
    00239
    00240             if ( i < count - 1 )
    00241                 result += '\n';
    00242         }
    00243     }
    00244     else
    00245     {
    00246        result.sprintf( "<%s>", type.latin1());
    00247     }
    00248
    00249     return result;
    00250
    00251 }
    00252
    00253 void marshall( QDataStream &arg, QCStringList args, uint &i, QString type )
    00254 {
    00255     if (type == "QStringList")
    00256        type = "QValueList<QString>";
    00257     if (type == "QCStringList")
    00258        type = "QValueList<QCString>";
    00259     if( i >= args.count() )
    00260     {
    00261         qWarning("Not enough arguments.");
    00262         exit(1);
    00263     }
    00264     QString s = QString::fromLocal8Bit( args[ i ] );
    00265
    00266     if ( type == "int" )
    00267         arg << s.toInt();
    00268     else if ( type == "uint" )
    00269         arg << s.toUInt();
    00270     else if ( type == "unsigned" )
    00271         arg << s.toUInt();
    00272     else if ( type == "unsigned int" )
    00273         arg << s.toUInt();
    00274     else if ( type == "long" )
    00275         arg << s.toLong();
    00276     else if ( type == "long int" )
    00277         arg << s.toLong();
    00278     else if ( type == "unsigned long" )
    00279         arg << s.toULong();
    00280     else if ( type == "unsigned long int" )
    00281         arg << s.toULong();
    00282     else if ( type == "float" )
    00283         arg << s.toFloat();
    00284     else if ( type == "double" )
    00285         arg << s.toDouble();
    00286     else if ( type == "bool" )
    00287         arg << mkBool( s );
    00288     else if ( type == "QString" )
    00289         arg << s;
    00290     else if ( type == "QCString" )
    00291         arg << QCString( args[ i ] );
    00292     else if ( type == "QColor" )
    00293         arg << mkColor( s );
    00294     else if ( type == "QPoint" )
    00295         arg << mkPoint( s );
    00296     else if ( type == "QSize" )
    00297         arg << mkSize( s );
    00298     else if ( type == "QRect" )
    00299         arg << mkRect( s );
    00300     else if ( type == "KURL" )
    00301         arg << KURL( s );
    00302     else if ( type == "QVariant" ) {
    00303         if ( s == "true" || s == "false" )
    00304             arg << QVariant( mkBool( s ), 42 );
    00305         else if ( s.left( 4 ) == "int(" )
    00306             arg << QVariant( s.mid(4, s.length()-5).toInt() );
    00307         else if ( s.left( 7 ) == "QPoint(" )
    00308             arg << QVariant( mkPoint( s.mid(7, s.length()-8) ) );
    00309         else if ( s.left( 6 ) == "QSize(" )
    00310             arg << QVariant( mkSize( s.mid(6, s.length()-7) ) );
    00311         else if ( s.left( 6 ) == "QRect(" )
    00312             arg << QVariant( mkRect( s.mid(6, s.length()-7) ) );
    00313         else if ( s.left( 7 ) == "QColor(" )
    00314             arg << QVariant( mkColor( s.mid(7, s.length()-8) ) );
    00315         else
    00316             arg << QVariant( s );
    00317     } else if ( type.startsWith("QValueList<")) {
    00318         type = type.mid(11, type.length() - 12);
    00319         QStringList list;
    00320         QString delim = s;
    00321         if (delim == "[")
    00322            delim = "]";
    00323         if (delim == "(")
    00324            delim = ")";
    00325         i++;
    00326         QByteArray dummy_data;
    00327         QDataStream dummy_arg(dummy_data, IO_WriteOnly);
    00328
    00329         uint j = i;
    00330         uint count = 0;
    00331         // Parse list to get the count
    00332         while (true) {
    00333             if( j > args.count() )
    00334             {
    00335                 qWarning("List end-delimiter '%s' not found.", delim.latin1());
    00336                 exit(1);
    00337             }
    00338             if( QString::fromLocal8Bit( args[ j ] ) == delim )
    00339                 break;
    00340             marshall( dummy_arg, args, j, type );
    00341             count++;
    00342         }
    00343         arg << (Q_UINT32) count;
    00344         // Parse the list for real
    00345         while (true) {
    00346             if( i > args.count() )
    00347             {
    00348                 qWarning("List end-delimiter '%s' not found.", delim.latin1());
    00349                 exit(1);
    00350             }
    00351             if( QString::fromLocal8Bit( args[ i ] ) == delim )
    00352                 break;
    00353             marshall( arg, args, i, type );
    00354         }
    00355     } else {
    00356         qWarning( "cannot handle datatype '%s'", type.latin1() );
    00357         exit(1);
    00358     }
    00359     i++;
    00360 }
    00361
    00362 // vim: set noet ts=8 sts=4 sw=4:
    00363

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:42:42 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001