summaryrefslogtreecommitdiff
path: root/ext/qtdom/qtdom_qt.cpp
blob: 6cc5050e49b9b1479c6e0614ae69f9179e7b027b (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
// 
// $Id$
//
// Jan Borsodi <jb@ez.no>
// Created on: <09-Nov-2000 11:18:46 root>
//


extern "C"
{
//#include "php.h"
//#include "php_ini.h"
//#include "php_qtdom.h"
#include "qtdom_qt.h"
}


//#if HAVE_QTDOM

#include <qdom.h>
#include <qstring.h>
#include <qglobal.h>

struct qdom_node *qdom_to_node( QDomNode *node );

/*!
  Initialises certain global variables, they are:
  g_qdom_message_log : A global variable for handling error logs and message handler function.
*/

extern "C" void qdom_init()
{
    g_qdom_message_log = new qdom_message;
    g_qdom_message_log->Log = 0;
    g_qdom_message_log->OldHandler = 0;
}

/*!
  Frees global variables initialised in the init function.
*/

extern "C" void qdom_shutdown()
{
    if ( g_qdom_message_log->Log )
        delete []g_qdom_message_log->Log;
    delete g_qdom_message_log;
    g_qdom_message_log = 0;
}

/*!
  Copies the version number for Qt into the \c ver variable,
  the variable must be allocated by user and must have enough characters (20 should suffice).
*/

extern "C" void qdom_do_version( char **ver )
{
    strcpy( *ver, QT_VERSION_STR );
}

/*!
  Moves the DOM node to the next sibling if any and returns the node.
*/

extern "C" struct qdom_node *qdom_do_next_node( struct qdom_node *node )
{
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    if ( !q_node )
        return 0;

    if ( q_node->isNull() )
        return 0;
    *q_node = q_node->nextSibling();
    if ( q_node->isNull() )
        return 0;

    node->Type = q_node->nodeType();
    const char *name = q_node->nodeName().latin1();
    node->Name = new char[q_node->nodeName().length()+1];
    strcpy( node->Name, name );
    const char *content = q_node->nodeValue().latin1();
    node->Content = new char[q_node->nodeValue().length()+1];
    strcpy( node->Content, content );

    return node;
}

/*!
  Finds the first child of the current node and returns it if any.
*/

extern "C" struct qdom_node *qdom_do_first_child( struct qdom_node *node )
{
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    if ( !q_node )
        return 0;

    if ( q_node->isNull() )
        return 0;
    *q_node = q_node->firstChild();
    if ( q_node->isNull() )
        return 0;

    node->Type = q_node->nodeType();
    const char *name = q_node->nodeName().latin1();
    node->Name = new char[q_node->nodeName().length()+1];
    strcpy( node->Name, name );
    const char *content = q_node->nodeValue().latin1();
    node->Content = new char[q_node->nodeValue().length()+1];
    strcpy( node->Content, content );

    return node;
}

/*!
  Returns the number of the children for the current node.
*/

extern "C" int qdom_do_node_children_count( struct qdom_node *node )
{
    if ( !node )
        return 0;
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    if ( !q_node )
        return 0;
    return q_node->childNodes().count();
}

/*!
  Returns the number of attributes for the current node.
*/

extern "C" int qdom_do_node_attribute_count( struct qdom_node *node )
{
    if ( !node )
        return 0;
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    if ( !q_node )
        return 0;
    return q_node->attributes().length();
}

/*!
  Returns the attribute map for the current node.
*/

extern "C" struct qdom_attribute *qdom_do_node_attributes( struct qdom_node *node )
{
    struct qdom_attribute *attr = new struct qdom_attribute;
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    QDomNamedNodeMap *map = new QDomNamedNodeMap( q_node->attributes() );
    attr->Q_Node = map;
    attr->Count = map->length();
    return attr;
}

/*!
  Returns the node at a given index taken from the attribute list if any.
*/

extern "C" struct qdom_node *qdom_do_attribute_at( struct qdom_attribute *attr, int index )
{
    if ( !attr )
        return 0;
    QDomNamedNodeMap *map = (QDomNamedNodeMap *)attr->Q_Node;
    if ( index < 0 || index >= map->length() )
        return 0;
    QDomNode node = map->item( index );
    return qdom_to_node( &node );
}

/*!
  Frees an attribute map.
*/

extern "C" void qdom_do_attributes_free( struct qdom_attribute *attr )
{
    if ( !attr )
        return;
    QDomNamedNodeMap *map = (QDomNamedNodeMap *)attr->Q_Node;
    delete map;
    delete attr;
}

/*!
  Makes a copy of a node.
*/

extern "C" struct qdom_node *qdom_do_copy_node( struct qdom_node *node )
{
    if ( !node )
        return 0;
    struct qdom_node *tmp = new struct qdom_node;
    if ( node->Name )
    {
        tmp->Name = new char[strlen(node->Name)+1];
        strcpy( tmp->Name, node->Name );
    }
    else
        tmp->Name = 0;
    if ( node->Content )
    {
        tmp->Content = new char[strlen(node->Content)+1];
        strcpy( tmp->Content, node->Content );
    }
    else
        tmp->Content = 0;
    if ( node->Q_Node )
    {
        QDomNode *q_node = (QDomNode *)node->Q_Node;
        tmp->Q_Node = new QDomNode( *q_node );
    }
    else
        tmp->Q_Node = 0;
    tmp->Type = node->Type;
    return tmp;
}

/*!
  Frees a node.
*/

extern "C" void qdom_do_node_free( struct qdom_node *node )
{
    if ( !node )
        return;
    delete []node->Name;
    delete []node->Content;
    QDomNode *q_node = (QDomNode *)node->Q_Node;
    delete q_node;
    delete node;
}

/*!
  Wraps a qdom_node struct around a QDomNode object which can be used by the C code.
*/

struct qdom_node *qdom_to_node( QDomNode *node )
{
    if ( !node )
        return 0;
    qdom_node *q_node = new struct qdom_node;
    q_node->Type = node->nodeType();
    const char *name = node->nodeName().latin1();
    q_node->Name = new char[node->nodeName().length()+1];
    strcpy( q_node->Name, name );
    const char *content = node->nodeValue().latin1();
    q_node->Content = new char[node->nodeValue().length()+1];
    strcpy( q_node->Content, content );
    q_node->Q_Node = new QDomNode( *node );
    return q_node;
}

/*!
  Copies the doctype name taken from the the qdom_doc object to the
  \c name variable, the variable is initialised by the function.
*/

extern "C" void qdom_do_doc_type( struct qdom_doc *doc, char **name )
{
    if ( !doc )
    {
        *name = 0;
        return;
    }
    QDomDocument *document = (QDomDocument *)doc->Document;
    QString str = document->doctype().name();
    const char *q_name = str.latin1();
    if ( q_name )
    {
        *name = new char[strlen(q_name)+1];
        strcpy( *name, q_name );
    }
    else
    {
        *name = 0;
    }
}

/*!
  Initialises a qdom_doc struct with the string taken from \c arg.
*/

extern "C" struct qdom_doc *qdom_do_init( const char *arg )
{
    struct qdom_doc *doc = new struct qdom_doc;
    QDomDocument *document = new QDomDocument();
    document->setContent( QString( arg ) );
    QDomNode *node = new QDomNode;
    *node = document->documentElement();
    doc->Document = document;
    doc->CurrentNode = node;
    doc->Children = qdom_to_node( node );
    return doc;
}

/*!
  Frees a qdom_doc struct.
*/

extern "C" void qdom_do_free( struct qdom_doc *doc )
{
    QDomNode *node = (QDomNode *)doc->CurrentNode;
    QDomDocument *document = (QDomDocument *)doc->Document;
    delete document;
    delete node;
    delete doc->Children;
    delete doc;
}

/*!
  The custom message output used for catching Qt error messages when parsing with QDOM.
*/

void qdom_messageOutput( QtMsgType , const char *msg )
{
    if ( !g_qdom_message_log )
        return;
    int msg_len = strlen( msg );
    int log_len = 0;
    if ( g_qdom_message_log->Log )
        log_len = strlen( g_qdom_message_log->Log );
    int total_len = log_len+msg_len+2;
    char *log = new char[total_len];
    if ( g_qdom_message_log->Log )
        strncpy( log, g_qdom_message_log->Log, log_len );
    strncpy( log+log_len, msg, msg_len );
    log[log_len+msg_len] = '\n';
    log[total_len - 1] = '\0';
    if ( g_qdom_message_log->Log )
        delete []g_qdom_message_log->Log;
    g_qdom_message_log->Log = log;
}

/*!
  Installs the custom message handler and clears the log entries.
*/

extern "C" void qdom_do_install_message_handler()
{
    if ( !g_qdom_message_log )
        g_qdom_message_log = new qdom_message;
    msg_handler *old_handler = new msg_handler;
    g_qdom_message_log->OldHandler = (void *)old_handler;
    if ( g_qdom_message_log->Log )
        delete []g_qdom_message_log->Log;
    g_qdom_message_log->Log = 0;
    *old_handler = qInstallMsgHandler( qdom_messageOutput );
}

/*!
  Frees the custom message handler.
*/

extern "C" void qdom_do_free_message_handler()
{
    msg_handler *old_handler = (msg_handler *)g_qdom_message_log->OldHandler;
    qInstallMsgHandler( *old_handler );
}

/*!
  Returns the string containg the error log, or 0 if no log is available.
*/

extern "C" char *qdom_error_log()
{
    if ( !g_qdom_message_log )
        return 0;
    return g_qdom_message_log->Log;
}

//#endif // HAVE_QTDOM