~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/dom/characterdata.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 of the PHP license,       |
 
8
   | that is bundled with this package in the file LICENSE, and is        |
 
9
   | available through the world-wide-web at the following url:           |
 
10
   | http://www.php.net/license/3_0.txt.                                  |
 
11
   | If you did not receive a copy of the PHP license and are unable to   |
 
12
   | obtain it through the world-wide-web, please send a note to          |
 
13
   | license@php.net so we can mail you a copy immediately.               |
 
14
   +----------------------------------------------------------------------+
 
15
   | Authors: Christian Stocker <chregu@php.net>                          |
 
16
   |          Rob Richards <rrichards@php.net>                            |
 
17
   +----------------------------------------------------------------------+
 
18
*/
 
19
 
 
20
/* $Id: characterdata.c,v 1.13.2.1 2004/11/18 19:55:00 rrichards Exp $ */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include "config.h"
 
24
#endif
 
25
 
 
26
#include "php.h"
 
27
#if HAVE_LIBXML && HAVE_DOM
 
28
#include "php_dom.h"
 
29
 
 
30
 
 
31
/*
 
32
* class DOMCharacterData extends DOMNode 
 
33
*
 
34
* URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-FF21A306
 
35
* Since: 
 
36
*/
 
37
 
 
38
zend_function_entry php_dom_characterdata_class_functions[] = {
 
39
        PHP_FALIAS(substringData, dom_characterdata_substring_data, NULL)
 
40
        PHP_FALIAS(appendData, dom_characterdata_append_data, NULL)
 
41
        PHP_FALIAS(insertData, dom_characterdata_insert_data, NULL)
 
42
        PHP_FALIAS(deleteData, dom_characterdata_delete_data, NULL)
 
43
        PHP_FALIAS(replaceData, dom_characterdata_replace_data, NULL)
 
44
        {NULL, NULL, NULL}
 
45
};
 
46
 
 
47
/* {{{ data     string  
 
48
readonly=no 
 
49
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-72AB8359
 
50
Since: 
 
51
*/
 
52
int dom_characterdata_data_read(dom_object *obj, zval **retval TSRMLS_DC)
 
53
{
 
54
        xmlNodePtr nodep;
 
55
        xmlChar *content;
 
56
 
 
57
        nodep = dom_object_get_node(obj);
 
58
 
 
59
        if (nodep == NULL) {
 
60
                php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
 
61
                return FAILURE;
 
62
        }
 
63
 
 
64
        ALLOC_ZVAL(*retval);
 
65
        
 
66
        if ((content = xmlNodeGetContent(nodep)) != NULL) {
 
67
                ZVAL_STRING(*retval, content, 1);
 
68
                xmlFree(content);
 
69
        } else {
 
70
                ZVAL_EMPTY_STRING(*retval);
 
71
        }
 
72
 
 
73
        return SUCCESS;
 
74
}
 
75
 
 
76
int dom_characterdata_data_write(dom_object *obj, zval *newval TSRMLS_DC)
 
77
{
 
78
        zval value_copy;
 
79
        xmlNode *nodep;
 
80
 
 
81
        nodep = dom_object_get_node(obj);
 
82
 
 
83
        if (nodep == NULL) {
 
84
                php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
 
85
                return FAILURE;
 
86
        }
 
87
 
 
88
        if (newval->type != IS_STRING) {
 
89
                if(newval->refcount > 1) {
 
90
                        value_copy = *newval;
 
91
                        zval_copy_ctor(&value_copy);
 
92
                        newval = &value_copy;
 
93
                }
 
94
                convert_to_string(newval);
 
95
        }
 
96
 
 
97
        xmlNodeSetContentLen(nodep, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
 
98
 
 
99
        if (newval == &value_copy) {
 
100
                zval_dtor(newval);
 
101
        }
 
102
 
 
103
        return SUCCESS;
 
104
}
 
105
 
 
106
/* }}} */
 
107
 
 
108
/* {{{ length   long    
 
109
readonly=yes 
 
110
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7D61178C
 
111
Since: 
 
112
*/
 
113
int dom_characterdata_length_read(dom_object *obj, zval **retval TSRMLS_DC)
 
114
{
 
115
        xmlNodePtr nodep;
 
116
        xmlChar *content;
 
117
        long length = 0;
 
118
 
 
119
        nodep = dom_object_get_node(obj);
 
120
 
 
121
        if (nodep == NULL) {
 
122
                php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
 
123
                return FAILURE;
 
124
        }
 
125
 
 
126
        ALLOC_ZVAL(*retval);
 
127
        
 
128
        content = xmlNodeGetContent(nodep);
 
129
 
 
130
        if (content) {
 
131
                length = xmlUTF8Strlen(content);
 
132
                xmlFree(content);
 
133
        }
 
134
 
 
135
        ZVAL_LONG(*retval, length);
 
136
 
 
137
        return SUCCESS;
 
138
}
 
139
 
 
140
/* }}} */
 
141
 
 
142
 
 
143
/* {{{ proto string dom_characterdata_substring_data(int offset, int count);
 
144
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6531BCCF
 
145
Since: 
 
146
*/
 
147
PHP_FUNCTION(dom_characterdata_substring_data)
 
148
{
 
149
        zval       *id;
 
150
        xmlChar    *cur;
 
151
        xmlChar    *substring;
 
152
        xmlNodePtr  node;
 
153
        long        offset, count;
 
154
        int         length;
 
155
        dom_object      *intern;
 
156
 
 
157
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
 
158
                return;
 
159
        }
 
160
 
 
161
        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
 
162
 
 
163
        cur = xmlNodeGetContent(node);
 
164
        if (cur == NULL) {
 
165
                RETURN_FALSE;
 
166
        }
 
167
 
 
168
        length = xmlUTF8Strlen(cur);
 
169
 
 
170
        if (offset < 0 || count < 0 || offset > length) {
 
171
                xmlFree(cur);
 
172
                php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
 
173
                RETURN_FALSE;
 
174
        }
 
175
 
 
176
        if ((offset + count) > length) {
 
177
                count = length - offset;
 
178
        }
 
179
 
 
180
        substring = xmlUTF8Strsub(cur, offset, count);
 
181
        xmlFree(cur);
 
182
 
 
183
        if (substring) {
 
184
                RETVAL_STRING(substring, 1);
 
185
                xmlFree(substring);
 
186
        } else {
 
187
                RETVAL_EMPTY_STRING();
 
188
        }
 
189
}
 
190
/* }}} end dom_characterdata_substring_data */
 
191
 
 
192
 
 
193
/* {{{ proto void dom_characterdata_append_data(string arg);
 
194
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-32791A2F
 
195
Since: 
 
196
*/
 
197
PHP_FUNCTION(dom_characterdata_append_data)
 
198
{
 
199
        zval *id;
 
200
        xmlNode *nodep;
 
201
        dom_object *intern;
 
202
        char *arg;
 
203
        int arg_len;
 
204
 
 
205
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_characterdata_class_entry, &arg, &arg_len) == FAILURE) {
 
206
                return;
 
207
        }
 
208
 
 
209
        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
 
210
 
 
211
        xmlTextConcat(nodep, arg, arg_len);
 
212
 
 
213
        RETURN_TRUE;
 
214
}
 
215
/* }}} end dom_characterdata_append_data */
 
216
 
 
217
 
 
218
/* {{{ proto void dom_characterdata_insert_data(int offset, string arg);
 
219
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3EDB695F
 
220
Since: 
 
221
*/
 
222
PHP_FUNCTION(dom_characterdata_insert_data)
 
223
{
 
224
        zval *id;
 
225
        xmlChar         *cur, *first, *second;
 
226
        xmlNodePtr  node;
 
227
        char            *arg;
 
228
        long        offset;
 
229
        int         length, arg_len;
 
230
        dom_object      *intern;
 
231
 
 
232
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols", &id, dom_characterdata_class_entry, &offset, &arg, &arg_len) == FAILURE) {
 
233
                return;
 
234
        }
 
235
 
 
236
        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
 
237
 
 
238
        cur = xmlNodeGetContent(node);
 
239
        if (cur == NULL) {
 
240
                RETURN_FALSE;
 
241
        }
 
242
 
 
243
        length = xmlUTF8Strlen(cur);
 
244
 
 
245
        if (offset < 0 || offset > length) {
 
246
                xmlFree(cur);
 
247
                php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
 
248
                RETURN_FALSE;
 
249
        }
 
250
 
 
251
        first = xmlUTF8Strndup(cur, offset);
 
252
        second = xmlUTF8Strsub(cur, offset, length - offset);
 
253
        xmlFree(cur);
 
254
 
 
255
        xmlNodeSetContent(node, first);
 
256
        xmlNodeAddContent(node, arg);
 
257
        xmlNodeAddContent(node, second);
 
258
        
 
259
        xmlFree(first);
 
260
        xmlFree(second);
 
261
 
 
262
        RETURN_TRUE;
 
263
}
 
264
/* }}} end dom_characterdata_insert_data */
 
265
 
 
266
 
 
267
/* {{{ proto void dom_characterdata_delete_data(int offset, int count);
 
268
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7C603781
 
269
Since: 
 
270
*/
 
271
PHP_FUNCTION(dom_characterdata_delete_data)
 
272
{
 
273
        zval *id;
 
274
        xmlChar    *cur, *substring, *second;
 
275
        xmlNodePtr  node;
 
276
        long        offset, count;
 
277
        int         length;
 
278
        dom_object      *intern;
 
279
 
 
280
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
 
281
                return;
 
282
        }
 
283
 
 
284
        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
 
285
 
 
286
        cur = xmlNodeGetContent(node);
 
287
        if (cur == NULL) {
 
288
                RETURN_FALSE;
 
289
        }
 
290
 
 
291
        length = xmlUTF8Strlen(cur);
 
292
 
 
293
        if (offset < 0 || count < 0 || offset > length) {
 
294
                xmlFree(cur);
 
295
                php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
 
296
                RETURN_FALSE;
 
297
        }
 
298
 
 
299
        if (offset > 0) {
 
300
                substring = xmlUTF8Strsub(cur, 0, offset);
 
301
        } else {
 
302
                substring = NULL;
 
303
        }
 
304
 
 
305
        if ((offset + count) > length) {
 
306
                count = length - offset;
 
307
        }
 
308
 
 
309
        second = xmlUTF8Strsub(cur, offset + count, length - offset);
 
310
        substring = xmlStrcat(substring, second);
 
311
 
 
312
        xmlNodeSetContent(node, substring);
 
313
 
 
314
        xmlFree(cur);
 
315
        xmlFree(second);
 
316
        xmlFree(substring);
 
317
 
 
318
        RETURN_TRUE;
 
319
}
 
320
/* }}} end dom_characterdata_delete_data */
 
321
 
 
322
 
 
323
/* {{{ proto void dom_characterdata_replace_data(int offset, int count, string arg);
 
324
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-E5CBA7FB
 
325
Since: 
 
326
*/
 
327
PHP_FUNCTION(dom_characterdata_replace_data)
 
328
{
 
329
        zval *id;
 
330
        xmlChar         *cur, *substring, *second = NULL;
 
331
        xmlNodePtr  node;
 
332
        char            *arg;
 
333
        long        offset, count;
 
334
        int         length, arg_len;
 
335
        dom_object      *intern;
 
336
 
 
337
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olls", &id, dom_characterdata_class_entry, &offset, &count, &arg, &arg_len) == FAILURE) {
 
338
                return;
 
339
        }
 
340
 
 
341
        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
 
342
 
 
343
        cur = xmlNodeGetContent(node);
 
344
        if (cur == NULL) {
 
345
                RETURN_FALSE;
 
346
        }
 
347
 
 
348
        length = xmlUTF8Strlen(cur);
 
349
 
 
350
        if (offset < 0 || count < 0 || offset > length) {
 
351
                xmlFree(cur);
 
352
                php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
 
353
                RETURN_FALSE;
 
354
        }
 
355
 
 
356
        if (offset > 0) {
 
357
                substring = xmlUTF8Strsub(cur, 0, offset);
 
358
        } else {
 
359
                substring = NULL;
 
360
        }
 
361
 
 
362
        if ((offset + count) > length) {
 
363
                count = length - offset;
 
364
        }
 
365
 
 
366
        if (offset < length) {
 
367
                second = xmlUTF8Strsub(cur, offset + count, length - offset);
 
368
        }
 
369
 
 
370
        substring = xmlStrcat(substring, arg);
 
371
        substring = xmlStrcat(substring, second);
 
372
 
 
373
        xmlNodeSetContent(node, substring);
 
374
 
 
375
        xmlFree(cur);
 
376
        if (second) {
 
377
                xmlFree(second);
 
378
        }
 
379
        xmlFree(substring);
 
380
 
 
381
        RETURN_TRUE;
 
382
}
 
383
/* }}} end dom_characterdata_replace_data */
 
384
#endif