summaryrefslogtreecommitdiffstats
path: root/dcop/client/marshall.cpp
blob: 101c75bb2fa51a4861ed9a2c3648d424d3dd2998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*****************************************************************
Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#define KDE_QT_ONLY
#include "../../tdecore/kurl.cpp"

bool mkBool( const TQString& s )
{
    if ( s.lower()  == "true" )
	return true;
    if ( s.lower()  == "yes" )
	return true;
    if ( s.lower()  == "on" )
	return true;
    if ( s.toInt() != 0 )
	return true;

    return false;
}

TQPoint mkPoint( const TQString &str )
{
    const char *s = str.latin1();
    char *end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int x = strtol(s, &end, 10);
    s = (const char *)end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int y = strtol(s, &end, 10);
    return TQPoint( x, y );
}

TQSize mkSize( const TQString &str )
{
    const char *s = str.latin1();
    char *end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int w = strtol(s, &end, 10);
    s = (const char *)end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int h = strtol(s, &end, 10);
    return TQSize( w, h );
}

TQRect mkRect( const TQString &str )
{
    const char *s = str.latin1();
    char *end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int p1 = strtol(s, &end, 10);
    s = (const char *)end;
    bool legacy = (*s == 'x');
    while(*s && !isdigit(*s) && *s != '-') s++;
    int p2 = strtol(s, &end, 10);
    s = (const char *)end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int p3 = strtol(s, &end, 10);
    s = (const char *)end;
    while(*s && !isdigit(*s) && *s != '-') s++;
    int p4 = strtol(s, &end, 10);
    if (legacy)
    {
       return TQRect( p3, p4, p1, p2 );
    }
    return TQRect( p1, p2, p3, p4 );
}

TQColor mkColor( const TQString& s )
{
    TQColor c;
    c.setNamedColor(s);
    return c;
}

const char *qStringToC(const TQCString &s)
{
   if (s.isEmpty())
      return "";
   return s.data();
}

TQCString demarshal( TQDataStream &stream, const TQString &type )
{
    TQCString result;

    if ( type == "int" || type == "TQ_INT32" )
    {
        int i;
        stream >> i;
        result.setNum( i );
    } else if ( type == "uint" || type == "TQ_UINT32" || type == "unsigned int" )
    {
        uint i;
        stream >> i;
        result.setNum( i );
    } else if ( type == "long" || type == "long int" )
    {
        long l;
        stream >> l;
        result.setNum( l );
    } else if ( type == "unsigned long" || type == "unsigned long int" )
    {
        unsigned long l;
        stream >> l;
        result.setNum( l );
    } else if ( type == "float" )
    {
        float f;
        stream >> f;
        result.setNum( f, 'f' );
    } else if ( type == "double" )
    {
        double d;
        stream >> d;
        result.setNum( d, 'f' );
    } else if ( type == "TQ_INT64" ) {
        TQ_INT64 i;
        stream >> i;
        result.sprintf( "%lld", i );
    } else if ( type == "TQ_UINT64" ) {
        TQ_UINT64 i;
        stream >> i;
        result.sprintf( "%llu", i );
    } else if ( type == "bool" )
    {
        bool b;
        stream >> b;
        result = b ? "true" : "false";
    } else if ( type == TQSTRING_OBJECT_NAME_STRING )
    {
        TQString s;
        stream >> s;
        result = s.local8Bit();
    } else if ( type == TQCSTRING_OBJECT_NAME_STRING )
    {
        stream >> result;
    } else if ( type == "QCStringList" )
    {
        return demarshal( stream, TQVALUELIST_OBJECT_NAME_STRING "<" TQCSTRING_OBJECT_NAME_STRING ">" );
    } else if ( type == TQSTRINGLIST_OBJECT_NAME_STRING )
    {
        return demarshal( stream, TQVALUELIST_OBJECT_NAME_STRING "<" TQSTRING_OBJECT_NAME_STRING ">" );
    } else if ( type == TQCOLOR_OBJECT_NAME_STRING )
    {
        TQColor c;
        stream >> c;
        result = TQString(c.name()).local8Bit();
    } else if ( type == TQSIZE_OBJECT_NAME_STRING )
    {
        TQSize s;
        stream >> s;
        result.sprintf( "%dx%d", s.width(), s.height() );
    } else if ( type == TQPIXMAP_OBJECT_NAME_STRING || type == TQIMAGE_OBJECT_NAME_STRING )
    {
        TQImage i;
        stream >> i;
        TQByteArray ba;
        TQBuffer buf( ba );
        buf.open( IO_WriteOnly );
        i.save( &buf, "XPM" );
        result = buf.buffer();
    } else if ( type == TQPOINT_OBJECT_NAME_STRING )
    {
        TQPoint p;
        stream >> p;
        result.sprintf( "+%d+%d", p.x(), p.y() );
    } else if ( type == TQRECT_OBJECT_NAME_STRING )
    {
        TQRect r;
        stream >> r;
        result.sprintf( "%dx%d+%d+%d", r.width(), r.height(), r.x(), r.y() );
    } else if ( type == TQVARIANT_OBJECT_NAME_STRING )
    {
        TQ_INT32 type;
        stream >> type;
        return demarshal( stream, TQVariant::typeToName( (TQVariant::Type)type ) );
    } else if ( type == "DCOPRef" )
    {
        DCOPRef r;
        stream >> r;
        result.sprintf( "DCOPRef(%s,%s)", qStringToC(r.app()), qStringToC(r.object()) );
    } else if ( type == "KURL" )
    {
        KURL r;
        stream >> r;
        result = r.url().local8Bit();
    } else if ( type.left( 12 ) == TQVALUELIST_OBJECT_NAME_STRING "<" )
    {
        if ( (uint)type.find( '>', 12 ) != type.length() - 1 )
            return result;

        TQString nestedType = type.mid( 12, type.length() - 13 );

        if ( nestedType.isEmpty() )
            return result;

        TQ_UINT32 count;
        stream >> count;

        TQ_UINT32 i = 0;
        for (; i < count; ++i )
        {
            TQCString arg = demarshal( stream, nestedType );
            result += arg;

            if ( i < count - 1 )
                result += '\n';
        }
    } else if ( type.left( 6 ) == TQMAP_OBJECT_NAME_STRING "<" )
    {
        int commaPos = type.find( ',', 6 );

        if ( commaPos == -1 )
            return result;

        if ( (uint)type.find( '>', commaPos ) != type.length() - 1 )
            return result;

        TQString keyType = type.mid( 6, commaPos - 6 );
        TQString valueType = type.mid( commaPos + 1, type.length() - commaPos - 2 );

        TQ_UINT32 count;
        stream >> count;

        TQ_UINT32 i = 0;
        for (; i < count; ++i )
        {
            TQCString key = demarshal( stream, keyType );

            if ( key.isEmpty() )
                continue;

            TQCString value = demarshal( stream, valueType );

            if ( value.isEmpty() )
                continue;

            result += key + "->" + value;

            if ( i < count - 1 )
                result += '\n';
        }
    }
    else
    {
       result.sprintf( "<%s>", type.latin1());
    }

    return result;

}

void marshall( TQDataStream &arg, QCStringList args, uint &i, TQString type )
{
    if( i >= args.count() )
    {
	tqWarning("Not enough arguments (expected %u, got %lu).",  i,  args.count());
	exit(1);
    }
    TQString s = TQString::fromLocal8Bit( args[ i ] );

    if (type == TQSTRINGLIST_OBJECT_NAME_STRING) {
       type = TQVALUELIST_OBJECT_NAME_STRING "<" TQSTRING_OBJECT_NAME_STRING ">";
    }
    if (type == "QCStringList") {
       type = TQVALUELIST_OBJECT_NAME_STRING "<" TQSTRING_OBJECT_NAME_STRING ">";
    }

    if ( type == "int" )
	arg << s.toInt();
    else if ( type == "uint" )
	arg << s.toUInt();
    else if ( type == "unsigned" )
	arg << s.toUInt();
    else if ( type == "unsigned int" )
	arg << s.toUInt();
    else if ( type == "TQ_INT32" )
	arg << s.toInt();
    else if ( type == "TQ_INT64" ) {
	TQVariant qv = TQVariant( s );
	arg << qv.toLongLong();
    }
    else if ( type == "TQ_UINT32" )
	arg << s.toUInt();
    else if ( type == "TQ_UINT64" ) {
	TQVariant qv = TQVariant( s );
	arg << qv.toULongLong();
    }
    else if ( type == "long" )
	arg << s.toLong();
    else if ( type == "long int" )
	arg << s.toLong();
    else if ( type == "unsigned long" )
	arg << s.toULong();
    else if ( type == "unsigned long int" )
	arg << s.toULong();
    else if ( type == "float" )
	arg << s.toFloat();
    else if ( type == "double" )
	arg << s.toDouble();
    else if ( type == "bool" )
	arg << mkBool( s );
    else if ( type == TQSTRING_OBJECT_NAME_STRING )
	arg << s;
    else if ( type == TQCSTRING_OBJECT_NAME_STRING )
	arg << TQCString( args[ i ] );
    else if ( type == TQCOLOR_OBJECT_NAME_STRING )
	arg << mkColor( s );
    else if ( type == TQPOINT_OBJECT_NAME_STRING )
	arg << mkPoint( s );
    else if ( type == TQSIZE_OBJECT_NAME_STRING )
	arg << mkSize( s );
    else if ( type == TQRECT_OBJECT_NAME_STRING )
	arg << mkRect( s );
    else if ( type == "KURL" )
	arg << KURL( s );
    else if ( type == TQVARIANT_OBJECT_NAME_STRING ) {
	int tqPointKeywordLength = strlen(TQPOINT_OBJECT_NAME_STRING);
	int tqSizeKeywordLength = strlen(TQSIZE_OBJECT_NAME_STRING);
	int tqRectKeywordLength = strlen(TQRECT_OBJECT_NAME_STRING);
	int tqColorKeywordLength = strlen(TQCOLOR_OBJECT_NAME_STRING);
	if ( s == "true" || s == "false" ) {
	    arg << TQVariant( mkBool( s ) );
	}
	else if ( s.left( 4 ) == "int(" ) {
	    arg << TQVariant( s.mid(4, s.length()-5).toInt() );
	}
	else if ( s.left( (tqPointKeywordLength+1) ) == TQPOINT_OBJECT_NAME_STRING "(" ) {
	    arg << TQVariant( mkPoint( s.mid((tqPointKeywordLength+1), s.length()-(tqPointKeywordLength+2)) ) );
	}
	else if ( s.left( (tqSizeKeywordLength+1) ) == TQSIZE_OBJECT_NAME_STRING "(" ) {
	    arg << TQVariant( mkSize( s.mid((tqSizeKeywordLength+1), s.length()-(tqSizeKeywordLength+2)) ) );
	}
	else if ( s.left( (tqRectKeywordLength+1) ) == TQRECT_OBJECT_NAME_STRING "(" ) {
	    arg << TQVariant( mkRect( s.mid((tqRectKeywordLength+1), s.length()-(tqRectKeywordLength+2)) ) );
	}
	else if ( s.left( (tqColorKeywordLength+1) ) == TQCOLOR_OBJECT_NAME_STRING "(" ) {
	    arg << TQVariant( mkColor( s.mid((tqColorKeywordLength+1), s.length()-(tqColorKeywordLength+2)) ) );
	}
	else {
	    arg << TQVariant( s );
	}
    } else if ( type.startsWith(TQVALUELIST_OBJECT_NAME_STRING "<") || type == "KURL::List" ) {
	if ( type == "KURL::List" ) {
            type = "KURL";
        }
        else {
	    int tqValueListKeywordLength = strlen(TQVALUELIST_OBJECT_NAME_STRING);
	    type = type.mid((tqValueListKeywordLength+1), type.length() - (tqValueListKeywordLength+2));
	}
	TQStringList list;
	TQString delim = s;
	if (delim == "[")
	   delim = "]";
	if (delim == "(")
	   delim = ")";
	i++;
	TQByteArray dummy_data;
	TQDataStream dummy_arg(dummy_data, IO_WriteOnly);

	uint j = i;
	uint count = 0;
	// Parse list to get the count
	while (true) {
	    if( j > args.count() )
	    {
		tqWarning("List end-delimiter '%s' not found.", delim.latin1());
		exit(1);
	    }
	    if( TQString::fromLocal8Bit( args[ j ] ) == delim )
		break;
	    marshall( dummy_arg, args, j, type );
	    count++;
	}
	arg << (TQ_UINT32) count;
	// Parse the list for real
	while (true) {
	    if( i > args.count() )
	    {
		tqWarning("List end-delimiter '%s' not found.", delim.latin1());
		exit(1);
	    }
	    if( TQString::fromLocal8Bit( args[ i ] ) == delim )
		break;
	    marshall( arg, args, i, type );
	}
    } else {
	tqWarning( "cannot handle datatype '%s'", type.latin1() );
	exit(1);
    }
    i++;
}