fe0ec15fa082e247f3f48cf3a04108303dc91dc5
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filexml.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "bcsignals.h"
28 #include "arraylist.h"
29 #include "cstrdup.h"
30 #include "filexml.h"
31 #include "mainerror.h"
32
33 // messes up cutads link
34 #undef eprintf
35 #define eprintf printf
36
37 static const char left_delm = '<', right_delm = '>';
38 const char *FileXML::xml_header = "<?xml version=\"1.0\"?>\n";
39 const int FileXML::xml_header_size = strlen(xml_header);
40
41 XMLBuffer::XMLBuffer(long buf_size, long max_size, int del)
42 {
43         bsz = buf_size;
44         bfr = new unsigned char[bsz];
45         inp = outp = bfr;
46         lmt = bfr + bsz;
47         isz = max_size;
48         destroy = del;
49         share_lock = new Mutex("XMLBuffer::share_lock", 1);
50 }
51
52 XMLBuffer::XMLBuffer(const char *buf, long buf_size, int del)
53 {       // reading
54         bfr = (unsigned char *)buf;
55         bsz = buf_size;
56         outp = bfr;
57         lmt = inp = bfr+bsz;
58         isz = bsz;
59         destroy = del;
60         share_lock = new Mutex("XMLBuffer::share_lock");
61 }
62
63 XMLBuffer::XMLBuffer(long buf_size, char *buf, int del)
64 {       // writing
65         bfr = (unsigned char *)buf;
66         bsz = buf_size;
67         inp = bfr;
68         lmt = outp = bfr+bsz;
69         isz = bsz;
70         destroy = del;
71         share_lock = new Mutex("XMLBuffer::share_lock");
72 }
73
74 XMLBuffer::~XMLBuffer()
75 {
76         if( destroy ) delete [] bfr;
77         delete share_lock;
78 }
79
80 int XMLBuffer::demand(long len)
81 {
82         if( len > bsz ) {
83                 if( !destroy ) return 0;
84                 long sz = inp-bfr;
85                 len += sz/2 + BCTEXTLEN;
86                 unsigned char *np = new unsigned char[len];
87                 if( sz > 0 ) memcpy(np,bfr,sz);
88                 inp = np + (inp-bfr);
89                 outp = np + (outp-bfr);
90                 lmt = np + len;  bsz = len;
91                 delete [] bfr;   bfr = np;
92         }
93         return 1;
94 }
95
96 int XMLBuffer::write(const char *bp, int len)
97 {
98         if( len <= 0 ) return 0;
99         if( !destroy && lmt-inp < len ) len = lmt-inp;
100         demand(otell()+len);
101         memmove(inp,bp,len);
102         inp += len;
103         return len;
104 }
105
106 int XMLBuffer::read(char *bp, int len)
107 {
108         long size = inp - outp;
109         if( size <= 0 && len > 0 ) return -1;
110         if( len > size ) len = size;
111         memmove(bp,outp,len);
112         outp += len;
113         return len;
114 }
115
116 void XMLBuffer::copy_from(XMLBuffer *xbuf)
117 {
118         xbuf->share_lock->lock("XMLBuffer::copy_from");
119         share_lock->lock("XMLBuffer::copy_from");
120         oseek(0);
121         write((const char*)xbuf->pos(0), xbuf->otell());
122         iseek(xbuf->itell());
123         xbuf->share_lock->unlock();
124         share_lock->unlock();
125 }
126
127
128 // Precision in base 10
129 // for float is 6 significant figures
130 // for double is 16 significant figures
131
132 XMLTag::Property::Property(const char *pp, const char *vp)
133 {
134 //printf("Property %s = %s\n",pp, vp);
135         prop = cstrdup(pp);
136         value = cstrdup(vp);
137 }
138
139 XMLTag::Property::~Property()
140 {
141         delete [] prop;
142         delete [] value;
143 }
144
145
146 XMLTag::XMLTag()
147 {
148         string = 0;
149         avail = used = 0;
150 }
151
152 XMLTag::~XMLTag()
153 {
154         properties.remove_all_objects();
155         delete [] string;
156 }
157
158 const char *XMLTag::get_property(const char *property, char *value)
159 {
160         int i = properties.size();
161         while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
162         if( i >= 0 )
163                 strcpy(value, properties[i]->value);
164         else
165                 *value = 0;
166         return value;
167 }
168
169 //getters
170 const char *XMLTag::get_property_text(int i) {
171         return i < properties.size() ? properties[i]->prop : "";
172 }
173 int XMLTag::get_property_int(int i) {
174         return i < properties.size() ? atol(properties[i]->value) : 0;
175 }
176 float XMLTag::get_property_float(int i) {
177         return i < properties.size() ? atof(properties[i]->value) : 0.;
178 }
179 const char* XMLTag::get_property(const char *prop) {
180         for( int i=0; i<properties.size(); ++i ) {
181                 if( !strcasecmp(properties[i]->prop, prop) )
182                         return properties[i]->value;
183         }
184         return 0;
185 }
186 int32_t XMLTag::get_property(const char *prop, int32_t dflt) {
187         const char *cp = get_property(prop);
188         return !cp ? dflt : atol(cp);
189 }
190 int64_t XMLTag::get_property(const char *prop, int64_t dflt) {
191         const char *cp = get_property(prop);
192         return !cp ? dflt : strtoll(cp,0,0);
193 }
194 float XMLTag::get_property(const char *prop, float dflt) {
195         const char *cp = get_property(prop);
196         return !cp ? dflt : atof(cp);
197 }
198 double XMLTag::get_property(const char *prop, double dflt) {
199         const char *cp = get_property(prop);
200         return !cp ? dflt : atof(cp);
201 }
202
203 //setters
204 int XMLTag::set_title(const char *text) {
205         strcpy(title, text);
206         return 0;
207 }
208 int XMLTag::set_property(const char *text, const char *value) {
209         properties.append(new XMLTag::Property(text, value));
210         return 0;
211 }
212 int XMLTag::set_property(const char *text, int32_t value)
213 {
214         char text_value[BCSTRLEN];
215         sprintf(text_value, "%d", value);
216         set_property(text, text_value);
217         return 0;
218 }
219 int XMLTag::set_property(const char *text, int64_t value)
220 {
221         char text_value[BCSTRLEN];
222         sprintf(text_value, "%jd", value);
223         set_property(text, text_value);
224         return 0;
225 }
226 int XMLTag::set_property(const char *text, float value)
227 {
228         char text_value[BCSTRLEN];
229         if (value - (float)((int64_t)value) == 0)
230                 sprintf(text_value, "%jd", (int64_t)value);
231         else
232                 sprintf(text_value, "%.6e", value);
233         set_property(text, text_value);
234         return 0;
235 }
236 int XMLTag::set_property(const char *text, double value)
237 {
238         char text_value[BCSTRLEN];
239         if (value - (double)((int64_t)value) == 0)
240                 sprintf(text_value, "%jd", (int64_t)value);
241         else
242                 sprintf(text_value, "%.16e", value);
243         set_property(text, text_value);
244         return 0;
245 }
246
247
248 int XMLTag::reset_tag()
249 {
250         used = 0;
251         properties.remove_all_objects();
252         return 0;
253 }
254
255 int XMLTag::write_tag(FileXML *xml)
256 {
257         XMLBuffer *buf = xml->buffer;
258 // title header
259         buf->next(left_delm);
260         buf->write(title, strlen(title));
261
262 // properties
263         for( int i=0; i<properties.size(); ++i ) {
264                 const char *prop = properties[i]->prop;
265                 const char *value = properties[i]->value;
266                 int plen = strlen(prop), vlen = strlen(value);
267                 bool need_quotes = !vlen || strchr(value,' ') || strchr(value,'\n');
268                 buf->next(' ');
269                 xml->append_text(prop, plen);
270                 buf->next('=');
271                 if( need_quotes ) buf->next('\"');
272                 xml->append_text(value, vlen);
273                 if( need_quotes ) buf->next('\"');
274         }
275
276         buf->next(right_delm);
277         return 0;
278 }
279
280 #define ERETURN(s) do { \
281   printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
282   return 1; } while(0)
283 #define EOB_RETURN(s) ERETURN(", end of buffer");
284
285 int XMLTag::read_tag(FileXML *xml)
286 {
287         XMLBuffer *buf = xml->buffer;
288         int len, term;
289         long prop_start, prop_end;
290         long value_start, value_end;
291         long ttl;
292         int ch = buf->next();
293         title[0] = 0;
294 // skip ws
295         while( ch>=0 && ws(ch) ) ch = buf->next();
296         if( ch < 0 ) EOB_RETURN();
297
298 // read title
299         ttl = buf->itell() - 1;
300         for( int i=0; i<MAX_TITLE && ch>=0; ++i, ch=buf->next() ) {
301                 if( ch == right_delm || ch == '=' || ws(ch) ) break;
302         }
303         if( ch < 0 ) EOB_RETURN();
304         len = buf->itell()-1 - ttl;
305         if( len >= MAX_TITLE ) ERETURN(", title too long");
306 // if title
307         if( ch != '=' ) {
308                 memmove(title, buf->pos(ttl), len);
309                 title[len] = 0;
310         }
311 // no title but first property.
312         else {
313                 title[0] = 0;
314                 buf->iseek(ttl);
315                 ch = buf->next();
316         }
317 // read properties
318         while( ch >= 0 && ch != right_delm ) {
319 // find tag start, skip header leadin
320                 while( ch >= 0 && (ch==left_delm || ws(ch)) )
321                         ch = buf->next();
322 // find end of property name
323                 prop_start = buf->itell()-1;
324                 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
325                         ch = buf->next();
326                 if( ch < 0 ) EOB_RETURN();
327                 prop_end = buf->itell()-1;
328 // skip ws = ws
329                 while( ch >= 0 && ws(ch) )
330                         ch = buf->next();
331                 if( ch == '=' ) ch = buf->next();
332                 while( ch >= 0 && ws(ch) )
333                         ch = buf->next();
334                 if( ch < 0 ) EOB_RETURN();
335 // set terminating char
336                 if( ch == '\"' ) {
337                         term = ch;
338                         ch = buf->next();
339                 }
340                 else
341                         term = ' ';
342                 value_start = buf->itell()-1;
343                 while( ch >= 0 ) {
344 // old edl bug work-around, allow nl in quoted string
345                         if( ch==term || ch==right_delm ) break;
346                         if( ch=='\n' && term!='\"' ) break;
347                         ch = buf->next();
348                 }
349                 if( ch < 0 ) EOB_RETURN();
350                 value_end = buf->itell()-1;
351 // add property
352                 int plen = prop_end-prop_start;
353                 if( !plen ) continue;
354                 int vlen = value_end-value_start;
355                 char prop[plen+1], value[vlen+1];
356                 const char *coded_prop = (const char *)buf->pos(prop_start);
357                 const char *coded_value = (const char *)buf->pos(value_start);
358 // props should not have coded text
359                 memcpy(prop, coded_prop, plen);
360                 prop[plen] = 0;
361                 xml->decode(value, coded_value, vlen);
362                 if( prop_end > prop_start ) {
363                         Property *property = new Property(prop, value);
364                         properties.append(property);
365                 }
366 // skip the terminating char
367                 if( ch != right_delm ) ch = buf->next();
368         }
369         if( !properties.size() && !title[0] ) ERETURN(", emtpy");
370         return 0;
371 }
372
373
374
375 FileXML::FileXML(int coded)
376 {
377         output = 0;
378         output_length = 0;
379         buffer = new XMLBuffer();
380         set_coding(coded);
381         shared = 0;
382 }
383
384 FileXML::~FileXML()
385 {
386         if( !shared ) delete buffer;
387         else buffer->share_lock->unlock();
388         delete [] output;
389 }
390
391
392 int FileXML::terminate_string()
393 {
394         append_data("", 1);
395         return 0;
396 }
397
398 int FileXML::rewind()
399 {
400         terminate_string();
401         buffer->iseek(0);
402         return 0;
403 }
404
405
406 int FileXML::append_newline()
407 {
408         append_data("\n", 1);
409         return 0;
410 }
411
412 int FileXML::append_tag()
413 {
414         tag.write_tag(this);
415         append_text(tag.string, tag.used);
416         tag.reset_tag();
417         return 0;
418 }
419
420 int FileXML::append_text(const char *text)
421 {
422         if( text != 0 )
423                 append_text(text, strlen(text));
424         return 0;
425 }
426
427 int FileXML::append_data(const char *text)
428 {
429         if( text != 0 )
430                 append_data(text, strlen(text));
431         return 0;
432 }
433
434 int FileXML::append_data(const char *text, long len)
435 {
436         if( text != 0 && len > 0 )
437                 buffer->write(text, len);
438         return 0;
439 }
440
441 int FileXML::append_text(const char *text, long len)
442 {
443         if( text != 0 && len > 0 ) {
444                 int size = coded_length(text, len);
445                 char coded_text[size+1];
446                 encode(coded_text, text, len);
447                 buffer->write(coded_text, size);
448         }
449         return 0;
450 }
451
452
453 char* FileXML::get_data()
454 {
455         char *data = (char *)buffer->cstr();
456         long ofs = buffer->itell();
457         return data + ofs;
458 }
459 char* FileXML::string()
460 {
461         return (char *)buffer->cstr();
462 }
463
464 long FileXML::length()
465 {
466         return buffer->otell();
467 }
468
469 char* FileXML::read_text()
470 {
471         int ch = buffer->next();
472 // filter out first char is new line
473         if( ch == '\n' ) ch = buffer->next();
474         long ipos = buffer->itell();
475         if( ch >= 0 ) --ipos;
476         long pos = ipos;
477 // scan for delimiter
478         while( ch >= 0 ) {
479                 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
480                 if( ch < 0 ) break;
481                 pos = buffer->itell()-1;
482                 if( (ch = buffer->next()) != '/' ) continue;
483                 char *cp = tag.title;
484                 while( (ch=buffer->next()) >= 0 && ch == *cp ) ++cp;
485                 if( ch < 0 ) break;
486                 if( *cp ) continue;
487                 while( ch == ' ' ) ch = buffer->next();
488                 if( ch == right_delm ) break;
489         }
490         if( ch < 0 )
491                 pos = buffer->itell();
492         buffer->iseek(pos);
493         long len = pos - ipos;
494         if( len >= output_length ) {
495                 delete [] output;
496                 output_length = len+1;
497                 output = new char[output_length];
498         }
499         if( len > 0 )
500                 decode(output,(const char *)buffer->pos(ipos), len);
501         output[len] = 0;
502         return output;
503 }
504
505 int FileXML::read_tag()
506 {
507         int ch = buffer->next();
508 // scan to next tag
509         while( ch >= 0 && ch != left_delm ) ch = buffer->next();
510         if( ch < 0 ) return 1;
511         tag.reset_tag();
512         return tag.read_tag(this);
513 }
514
515 int FileXML::skip_tag()
516 {
517         char tag_title[sizeof(tag.title)];
518         strcpy(tag_title, tag.title);
519         int n = 1;
520         while( !read_tag() ) {
521                 if( tag.title[0] == tag_title[0] ) {
522                         if( !strcasecmp(&tag_title[1], &tag.title[1]) ) ++n;
523                 }
524                 else if( tag.title[0] != '/' ) continue;
525                 else if( strcasecmp(&tag_title[0], &tag.title[1]) ) continue;
526                 else if( --n <= 0 ) return 0;
527         }
528         return 1;
529 }
530
531 int FileXML::read_data_until(const char *tag_end, XMLBuffer *xbuf, int skip)
532 {
533         long ipos = buffer->itell();
534         int pos = -1;
535         for( int ch=buffer->next(); ch>=0; ch=buffer->next() ) {
536                 if( pos < 0 ) { // looking for next tag
537                         if( ch == left_delm ) {
538                                 ipos = buffer->itell()-1;
539                                 pos = 0;
540                         }
541                         else
542                                 xbuf->next(ch);
543                         continue;
544                 }
545                 // check for end of match
546                 if( !tag_end[pos] && ch == right_delm ) break;
547                 // if mismatched, copy prefix to out
548                 if( tag_end[pos] != ch ) {
549                         xbuf->next(left_delm);
550                         for( int i=0; i<pos; ++i )
551                                 xbuf->next(tag_end[i]);
552                         pos = -1;
553                         xbuf->next(ch);
554                         continue;
555                 }
556                 ++pos;
557         }
558 // if end tag is reached, pos is left on the < of the end tag
559         if( !skip && pos >= 0 && !tag_end[pos] )
560                 buffer->iseek(ipos);
561         return xbuf->otell();
562 }
563
564 int FileXML::read_text_until(const char *tag_end, XMLBuffer *xbuf, int skip)
565 {
566         int len = read_data_until(tag_end, xbuf, skip);
567         char *cp = xbuf->cstr();
568         decode(cp, cp, len);
569         return 0;
570 }
571
572 int FileXML::write_to_file(const char *filename)
573 {
574         FILE *out = fopen(filename, "wb");
575         if( !out ) {
576                 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
577                 return 1;
578         }
579         int ret = write_to_file(out, filename);
580         fclose(out);
581         return ret;
582 }
583
584 int FileXML::write_to_file(FILE *file, const char *filename)
585 {
586         strcpy(this->filename, filename);
587         const char *str = string();
588         long len = strlen(str);
589 // Position may have been rewound after storing
590         if( !fwrite(xml_header, xml_header_size, 1, file) ||
591             ( len > 0 && !fwrite(str, len, 1, file) ) ) {
592                 eprintf("\"%s\": %m\n", filename);
593                 return 1;
594         }
595         return 0;
596 }
597
598 int FileXML::read_from_file(const char *filename, int ignore_error)
599 {
600
601         strcpy(this->filename, filename);
602         FILE *in = fopen(filename, "rb");
603         if( !in ) {
604                 if(!ignore_error)
605                         eprintf("\"%s\" %m\n", filename);
606                 return 1;
607         }
608         fseek(in, 0, SEEK_END);
609         long length = ftell(in);
610         fseek(in, 0, SEEK_SET);
611         char *fbfr = new char[length+1];
612         delete buffer;
613         (void)fread(fbfr, length, 1, in);
614         fbfr[length] = 0;
615         buffer = new XMLBuffer(fbfr, length, 1);
616         fclose(in);
617         return 0;
618 }
619
620 int FileXML::read_from_string(char *string)
621 {
622         strcpy(this->filename, "");
623         long length = strlen(string);
624         char *sbfr = new char[length+1];
625         strcpy(sbfr, string);
626         delete buffer;
627         buffer = new XMLBuffer(sbfr, length, 1);
628         return 0;
629 }
630
631 void FileXML::set_coding(int coding)
632 {
633         coded = coding;
634         if( coded ) {
635                 decode = XMLBuffer::decode_data;
636                 encode = XMLBuffer::encode_data;
637                 coded_length = XMLBuffer::encoded_length;
638         }
639         else {
640                 decode = XMLBuffer::copy_data;
641                 encode = XMLBuffer::copy_data;
642                 coded_length = XMLBuffer::copy_length;
643         }
644 }
645
646 int FileXML::get_coding()
647 {
648         return coded;
649 }
650
651 int FileXML::set_shared_input(XMLBuffer *xbuf)
652 {
653         strcpy(this->filename, "");
654         delete buffer;
655         buffer = xbuf;
656         xbuf->share_lock->lock("FileXML::set_shared_input");
657         xbuf->iseek(0);
658         shared = 1;
659         set_coding(coded);
660         return 0;
661 }
662
663 int FileXML::set_shared_output(XMLBuffer *xbuf)
664 {
665         strcpy(this->filename, "");
666         delete buffer;
667         buffer = xbuf;
668         xbuf->share_lock->lock("FileXML::set_shared_output");
669         xbuf->oseek(0);
670         shared = 1;
671         set_coding(coded);
672         return 0;
673 }
674
675
676 // ================================ XML tag
677
678 int XMLTag::title_is(const char *tp)
679 {
680         return !strcasecmp(title, tp) ? 1 : 0;
681 }
682
683 char* XMLTag::get_title()
684 {
685         return title;
686 }
687
688 int XMLTag::get_title(char *value)
689 {
690         if( title[0] != 0 ) strcpy(value, title);
691         return 0;
692 }
693
694 int XMLTag::test_property(char *property, char *value)
695 {
696         for( int i=0; i<properties.size(); ++i ) {
697                 if( !strcasecmp(properties[i]->prop, property) &&
698                     !strcasecmp(properties[i]->value, value) )
699                         return 1;
700         }
701         return 0;
702 }
703
704 static inline int xml_cmp(const char *np, const char *sp)
705 {
706    const char *tp = ++np;
707    while( *np ) { if( *np != *sp ) return 0;  ++np; ++sp; }
708    return np - tp;
709 }
710
711 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
712 {
713    char *ret = bp;
714    if( n < 0 ) n = strlen(sp);
715    const char *ep = sp + n;
716    while( sp < ep ) {
717       if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
718       switch( (n=*sp++) ) {
719       case 'a': // &amp;
720          if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&';  sp += n;  continue; }
721          break;
722       case 'g': // &gt;
723          if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>';  sp += n;  continue; }
724          break;
725       case 'l': // &lt;
726          if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<';  sp += n;  continue; }
727          break;
728       case 'q': // &quot;
729          if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"';  sp += n;  continue; }
730          break;
731       case '#': { // &#<num>;
732          char *cp = 0;  int v = strtoul(sp,&cp,10);
733          if( *cp == ';' ) { *bp++ = (char)v;  sp = cp+1;  continue; }
734          n = cp - sp; }
735          break;
736       default:
737          *bp++ = '&';
738          *bp++ = (char)n;
739          continue;
740       }
741       sp -= 2;  n += 2;
742       while( --n >= 0 ) *bp++ = *sp++;
743    }
744    *bp = 0;
745    return ret;
746 }
747
748
749 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
750 {
751         char *ret = bp;
752         if( n < 0 ) n = strlen(sp);
753         const char *cp, *ep = sp + n;
754         while( sp < ep ) {
755                 int ch = *sp++;
756                 switch( ch ) {
757                 case '<':  cp = "&lt;";    break;
758                 case '>':  cp = "&gt;";    break;
759                 case '&':  cp = "&amp;";   break;
760                 case '"':  cp = "&quot;";  break;
761                 default:  *bp++ = ch;      continue;
762                 }
763                 while( *cp != 0 ) *bp++ = *cp++;
764         }
765         *bp = 0;
766         return ret;
767 }
768
769 long XMLBuffer::encoded_length(const char *sp, int n)
770 {
771         long len = 0;
772         if( n < 0 ) n = strlen(sp);
773         const char *ep = sp + n;
774         while( sp < ep ) {
775                 int ch = *sp++;
776                 switch( ch ) {
777                 case '<':  len += 4;  break;
778                 case '>':  len += 4;  break;
779                 case '&':  len += 5;  break;
780                 case '"':  len += 6;  break;
781                 default:   ++len;     break;
782                 }
783         }
784         return len;
785 }
786
787 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
788 {
789         int len = n < 0 ? strlen(sp) : n;
790         if( bp != sp )
791                 memmove(bp,sp,len);
792         bp[len] = 0;
793         return bp;
794 }
795
796 long XMLBuffer::copy_length(const char *sp, int n)
797 {
798         int len = n < 0 ? strlen(sp) : n;
799         return len;
800 }
801