| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
version = 0; |
| 24 |
|
| 25 |
// ---------------------------------------------------------------------- |
| 26 |
// List |
| 27 |
|
| 28 |
List = $new(null); |
| 29 |
|
| 30 |
List.new = function() { |
| 31 |
var o = $new(null); |
| 32 |
$objsetproto(o,@List); |
| 33 |
o.l = 0; |
| 34 |
o.h = null; |
| 35 |
o.q = null; |
| 36 |
return o; |
| 37 |
}; |
| 38 |
|
| 39 |
@List = $new(null); |
| 40 |
|
| 41 |
List.prototype = @List; |
| 42 |
|
| 43 |
@List.get_length = function() { |
| 44 |
return this.l; |
| 45 |
}; |
| 46 |
|
| 47 |
@List.add = function(x) { |
| 48 |
var a = $array(x,null); |
| 49 |
if( this.q != null ) |
| 50 |
this.q[1] = a; |
| 51 |
else |
| 52 |
this.h = a; |
| 53 |
this.q = a; |
| 54 |
this.l = this.l + 1; |
| 55 |
}; |
| 56 |
|
| 57 |
@List.push = function(x) { |
| 58 |
var a = $array(x,this.h); |
| 59 |
this.h = a; |
| 60 |
if( this.q == null ) |
| 61 |
this.q = a; |
| 62 |
this.l = this.l + 1; |
| 63 |
}; |
| 64 |
|
| 65 |
@List.remove = function(x) { |
| 66 |
var cur = this.h; |
| 67 |
var prev = null; |
| 68 |
while( cur != null ) { |
| 69 |
if( cur[0] == x ) { |
| 70 |
if( prev == null ) |
| 71 |
this.h = cur[1]; |
| 72 |
else |
| 73 |
prev[1] = cur[1]; |
| 74 |
if( this.q == cur ) |
| 75 |
this.q = prev; |
| 76 |
this.l = this.l - 1; |
| 77 |
return true; |
| 78 |
} |
| 79 |
prev = cur; |
| 80 |
cur = cur[1]; |
| 81 |
} |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
@List.at = function(p) { |
| 86 |
var cur = this.h; |
| 87 |
while( cur != null ) { |
| 88 |
if( $not(p) ) |
| 89 |
return cur[0]; |
| 90 |
p = p - 1; |
| 91 |
cur = cur[1]; |
| 92 |
} |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
@List.pop = function() { |
| 97 |
if( this.h == null ) |
| 98 |
return null; |
| 99 |
if( this.q == this.h ) |
| 100 |
this.q = null; |
| 101 |
var x = this.h[0]; |
| 102 |
this.h = this.h[1]; |
| 103 |
this.l = this.l - 1; |
| 104 |
return x; |
| 105 |
} |
| 106 |
|
| 107 |
@List.first = function() { |
| 108 |
if( this.h == null ) |
| 109 |
return null; |
| 110 |
return this.h[0]; |
| 111 |
} |
| 112 |
|
| 113 |
@List.last = function() { |
| 114 |
if( this.q == null ) |
| 115 |
return null; |
| 116 |
return this.q[0]; |
| 117 |
} |
| 118 |
|
| 119 |
@List.iter = function(f) { |
| 120 |
var p = this.h; |
| 121 |
while( p != null ) { |
| 122 |
f(p[0]); |
| 123 |
p = p[1]; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
@List.find = function(f) { |
| 128 |
var p = this.h; |
| 129 |
while( p != null ) { |
| 130 |
var x = f(p[0]); |
| 131 |
if( x != null ) |
| 132 |
return x; |
| 133 |
p = p[1]; |
| 134 |
} |
| 135 |
return null; |
| 136 |
} |
| 137 |
|
| 138 |
@List.exists = function(f) { |
| 139 |
var p = this.h; |
| 140 |
while( p != null ) { |
| 141 |
if( f(p[0]) ) |
| 142 |
return true; |
| 143 |
p = p[1]; |
| 144 |
} |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
@List.join = function(sep) { |
| 149 |
if( $not(this.l) ) |
| 150 |
return String.new(""); |
| 151 |
var b = StringBuf.new(); |
| 152 |
var p = this.h; |
| 153 |
while( p != null ) { |
| 154 |
b.add(p[0]); |
| 155 |
p = p[1]; |
| 156 |
if( p != null ) |
| 157 |
b.add(sep); |
| 158 |
} |
| 159 |
return String.new(b.__string()); |
| 160 |
} |
| 161 |
|
| 162 |
@List.toArray = function() { |
| 163 |
var a = $amake(this.l); |
| 164 |
var p = this.h; |
| 165 |
var n = 0; |
| 166 |
while( p != null ) { |
| 167 |
a[n] = p[0]; |
| 168 |
n = n + 1; |
| 169 |
p = p[1]; |
| 170 |
} |
| 171 |
return Array.new1(a); |
| 172 |
} |
| 173 |
|
| 174 |
@List.toString = function() { |
| 175 |
return String.new(this.__string()); |
| 176 |
} |
| 177 |
|
| 178 |
@List.__string = function() { |
| 179 |
var b = StringBuf.new(); |
| 180 |
var p = this.h; |
| 181 |
b.add("["); |
| 182 |
while( p != null ) { |
| 183 |
b.add(p[0]); |
| 184 |
p = p[1]; |
| 185 |
if( p != null ) |
| 186 |
b.add(","); |
| 187 |
} |
| 188 |
b.add("]"); |
| 189 |
return b.__string(); |
| 190 |
} |
| 191 |
|
| 192 |
@List.map = function(f) { |
| 193 |
var l = List.new(); |
| 194 |
if( this.h != null ) { |
| 195 |
var cur = this.h; |
| 196 |
var last = null; |
| 197 |
l.h = null; |
| 198 |
while( cur != null ) { |
| 199 |
if( last == null ) { |
| 200 |
last = $array(f(cur[0]),null); |
| 201 |
l.h = last; |
| 202 |
} else { |
| 203 |
var tmp = $array(f(cur[0]),null); |
| 204 |
last[1] = tmp; |
| 205 |
last = tmp; |
| 206 |
} |
| 207 |
cur = cur[1]; |
| 208 |
} |
| 209 |
l.q = last; |
| 210 |
l.l = this.l; |
| 211 |
} |
| 212 |
return l; |
| 213 |
} |
| 214 |
|
| 215 |
@List.filter = function(f) { |
| 216 |
var l = List.new(); |
| 217 |
if( this.h != null ) { |
| 218 |
var cur = this.h; |
| 219 |
var last = null; |
| 220 |
var count = 0; |
| 221 |
l.h = null; |
| 222 |
while( cur != null ) { |
| 223 |
if( f(cur[0]) ) { |
| 224 |
if( last == null ) { |
| 225 |
last = $array(cur[0],null); |
| 226 |
l.h = last; |
| 227 |
} else { |
| 228 |
var tmp = $array(cur[0],null); |
| 229 |
last[1] = tmp; |
| 230 |
last = tmp; |
| 231 |
} |
| 232 |
count += 1; |
| 233 |
} |
| 234 |
cur = cur[1]; |
| 235 |
} |
| 236 |
l.q = last; |
| 237 |
l.l = count; |
| 238 |
} |
| 239 |
return l; |
| 240 |
} |
| 241 |
|
| 242 |
@List.__serialize = function() { |
| 243 |
return $array("List",version); |
| 244 |
} |
| 245 |
|
| 246 |
$exports.List = List; |
| 247 |
|
| 248 |
// ---------------------------------------------------------------------- |
| 249 |
// Array |
| 250 |
|
| 251 |
Array = $new(null); |
| 252 |
|
| 253 |
Array.new = function() { |
| 254 |
var o = $new(null); |
| 255 |
$objsetproto(o,@Array); |
| 256 |
o.@a = $array(); |
| 257 |
return o; |
| 258 |
} |
| 259 |
|
| 260 |
Array.new1 = function(a) { |
| 261 |
var o = $new(null); |
| 262 |
$objsetproto(o,@Array); |
| 263 |
o.@a = a; |
| 264 |
return o; |
| 265 |
} |
| 266 |
|
| 267 |
@Array = $new(null); |
| 268 |
|
| 269 |
Array.prototype = @Array; |
| 270 |
|
| 271 |
@Array.get_length = function() { |
| 272 |
return $asize(this.@a); |
| 273 |
} |
| 274 |
|
| 275 |
@Array.concat = function(a) { |
| 276 |
var l = $asize(this.@a); |
| 277 |
var l2 = $asize(a.@a); |
| 278 |
var a = $amake(l+l2); |
| 279 |
$ablit(a,0,this.@a,0,l); |
| 280 |
$ablit(a,l,a.@a,0,l2); |
| 281 |
this.@a = a; |
| 282 |
} |
| 283 |
|
| 284 |
@Array.sub = function(s,l) { |
| 285 |
var a = $asub(this.@a,s,l); |
| 286 |
if( a == null ) |
| 287 |
return null; |
| 288 |
return Array.new1(a); |
| 289 |
} |
| 290 |
|
| 291 |
@Array.sort = function(f) { |
| 292 |
var a = this.@a; |
| 293 |
var i = 0; |
| 294 |
var l = $asize(a); |
| 295 |
while( i < l ) { |
| 296 |
var swap = false; |
| 297 |
var j = 0; |
| 298 |
var max = l - i - 1; |
| 299 |
while( j < max ) { |
| 300 |
if( f(a[j],a[j+1]) > 0 ) { |
| 301 |
var tmp = a[j+1]; |
| 302 |
a[j+1] = a[j]; |
| 303 |
a[j] = tmp; |
| 304 |
swap = true; |
| 305 |
} |
| 306 |
j = j + 1; |
| 307 |
} |
| 308 |
if( $not(swap) ) |
| 309 |
break; |
| 310 |
i = i + 1; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
@Array.iter = function(f) { |
| 315 |
var i = 0; |
| 316 |
var a = this.@a; |
| 317 |
var l = $asize(a); |
| 318 |
while( i < l ) { |
| 319 |
f(a[i]); |
| 320 |
i = i + 1; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
@Array.map = function(f) { |
| 325 |
var i = 0; |
| 326 |
var a = this.@a; |
| 327 |
var l = $asize(a); |
| 328 |
var a2 = $amake(l); |
| 329 |
while( i < l ) { |
| 330 |
a2[i] = f(a[i]); |
| 331 |
i = i + 1; |
| 332 |
} |
| 333 |
return Array.new1(a2); |
| 334 |
} |
| 335 |
|
| 336 |
@Array.join = function(sep) { |
| 337 |
var i = 0; |
| 338 |
var a = this.@a; |
| 339 |
var l = $asize(a); |
| 340 |
var b = StringBuf.new(); |
| 341 |
while( i < l ) { |
| 342 |
b.add(a[i]); |
| 343 |
i = i + 1; |
| 344 |
if( i != l ) |
| 345 |
b.add(sep); |
| 346 |
} |
| 347 |
return b.toString(); |
| 348 |
} |
| 349 |
|
| 350 |
@Array.toList = function() { |
| 351 |
var i = 0; |
| 352 |
var a = this.@a; |
| 353 |
var l = $asize(a); |
| 354 |
var li = List.new(); |
| 355 |
while( i < l ) { |
| 356 |
li.add(a[i]); |
| 357 |
i = i + 1; |
| 358 |
} |
| 359 |
return li; |
| 360 |
} |
| 361 |
|
| 362 |
@Array.toString = function() { |
| 363 |
return String.new(this.__string()); |
| 364 |
} |
| 365 |
|
| 366 |
@Array.__string = function() { |
| 367 |
return $string(this.@a); |
| 368 |
} |
| 369 |
|
| 370 |
@Array.set = function(p,v) { |
| 371 |
var a = this.@a; |
| 372 |
if( $asize(a) > p ) |
| 373 |
return (a[p] = v); |
| 374 |
var a2 = $amake(p+1); |
| 375 |
$ablit(a2,0,a,0,$asize(a)); |
| 376 |
this.@a = a2; |
| 377 |
return (a2[p] = v); |
| 378 |
} |
| 379 |
|
| 380 |
@Array.__serialize = function() { |
| 381 |
return $array("Array",version); |
| 382 |
} |
| 383 |
|
| 384 |
$exports.Array = Array; |
| 385 |
|
| 386 |
// ---------------------------------------------------------------------- |
| 387 |
// Hash |
| 388 |
|
| 389 |
Hash = $new(null); |
| 390 |
|
| 391 |
Hash.new = function() { |
| 392 |
var o = $new(null); |
| 393 |
$objsetproto(o,@Hash); |
| 394 |
o.@h = $hnew(3); |
| 395 |
return o; |
| 396 |
} |
| 397 |
|
| 398 |
@Hash = $new(null); |
| 399 |
|
| 400 |
Hash.prototype = @Hash; |
| 401 |
|
| 402 |
@Hash.get = function(k) { |
| 403 |
return $hget(this.@h,k.@s,null); |
| 404 |
} |
| 405 |
|
| 406 |
@Hash.set = function(k,v) { |
| 407 |
$hset(this.@h,k.@s,v,null); |
| 408 |
} |
| 409 |
|
| 410 |
@Hash.remove = function(k) { |
| 411 |
return $hremove(this.@h,k.@s,null); |
| 412 |
} |
| 413 |
|
| 414 |
@Hash.exists = function(k) { |
| 415 |
return $hmem(this.@h,k.@s,null); |
| 416 |
} |
| 417 |
|
| 418 |
@Hash.iter = function(f) { |
| 419 |
$hiter(this.@h,function(k,v) { f(String.new(k),v) }); |
| 420 |
} |
| 421 |
|
| 422 |
@Hash.toString = function() { |
| 423 |
return String.new(this.__string()); |
| 424 |
} |
| 425 |
|
| 426 |
@Hash.__string = function() { |
| 427 |
var s = StringBuf.new(); |
| 428 |
var i = 0; |
| 429 |
var first = true; |
| 430 |
s.add("#hash["); |
| 431 |
$hiter(this.@h,function(k,v) { |
| 432 |
if( first ) |
| 433 |
first = false; |
| 434 |
else |
| 435 |
s.add(", "); |
| 436 |
s.add(k); |
| 437 |
s.add(" => "); |
| 438 |
s.add(v); |
| 439 |
}); |
| 440 |
s.add("]"); |
| 441 |
return s.__string(); |
| 442 |
} |
| 443 |
|
| 444 |
@Hash.__serialize = function() { |
| 445 |
return $array("Hash",version); |
| 446 |
} |
| 447 |
|
| 448 |
$exports.Hash = Hash; |
| 449 |
|
| 450 |
// ---------------------------------------------------------------------- |
| 451 |
// String |
| 452 |
|
| 453 |
string_split = $loader.loadprim("std@string_split",2); |
| 454 |
|
| 455 |
String = $new(null); |
| 456 |
|
| 457 |
String.new = function(s) { |
| 458 |
if( s == null ) |
| 459 |
return null; |
| 460 |
|
| 461 |
if( $typeof(s) != $tstring ) |
| 462 |
s = $string(s); |
| 463 |
|
| 464 |
var o = $new(null); |
| 465 |
$objsetproto(o,@String); |
| 466 |
o.@s = s; |
| 467 |
return o; |
| 468 |
} |
| 469 |
|
| 470 |
@String = $new(null); |
| 471 |
|
| 472 |
String.prototype = @String; |
| 473 |
|
| 474 |
@String.get_length = function() { |
| 475 |
return $ssize(this.@s); |
| 476 |
} |
| 477 |
|
| 478 |
@String.charAt = function(p) { |
| 479 |
return String.new( $ssub(this.@s,p,1) ); |
| 480 |
} |
| 481 |
|
| 482 |
@String.at = function(p) { |
| 483 |
return $sget(this.@s,p); |
| 484 |
} |
| 485 |
|
| 486 |
@String.sub = function(s,l) { |
| 487 |
if( l == null ) |
| 488 |
l = $ssize(this.@s) - s; |
| 489 |
return String.new( $ssub(this.@s,s,l) ); |
| 490 |
} |
| 491 |
|
| 492 |
@String.split = function(s) { |
| 493 |
var a = string_split(this.@s,s.@s); |
| 494 |
var l = List.new(); |
| 495 |
while( a != null ) { |
| 496 |
l.add( String.new(a[0]) ); |
| 497 |
a = a[1]; |
| 498 |
} |
| 499 |
return l; |
| 500 |
} |
| 501 |
|
| 502 |
@String.toString = function() { |
| 503 |
return this; |
| 504 |
} |
| 505 |
|
| 506 |
@String.__string = function() { |
| 507 |
return this.@s; |
| 508 |
} |
| 509 |
|
| 510 |
@String.__compare = function(o) { |
| 511 |
return $compare(this.@s,o.@s); |
| 512 |
} |
| 513 |
|
| 514 |
@String.__add = function(s) { |
| 515 |
return String.new(this.@s+$string(s)); |
| 516 |
} |
| 517 |
|
| 518 |
@String.__radd = function(s) { |
| 519 |
return String.new($string(s)+this.@s); |
| 520 |
} |
| 521 |
|
| 522 |
@String.__serialize = function() { |
| 523 |
return $array("String",version); |
| 524 |
} |
| 525 |
|
| 526 |
$exports.String = String; |
| 527 |
|
| 528 |
// ---------------------------------------------------------------------- |
| 529 |
// Int32 |
| 530 |
|
| 531 |
Int32 = $new(null); |
| 532 |
|
| 533 |
Int32.of_int = $loader.loadprim("std@int32_new",1); |
| 534 |
Int32.to_int = $loader.loadprim("std@int32_to_int",1); |
| 535 |
Int32.add = $loader.loadprim("std@int32_add",2); |
| 536 |
Int32.sub = $loader.loadprim("std@int32_sub",2); |
| 537 |
Int32.mul = $loader.loadprim("std@int32_mul",2); |
| 538 |
Int32.div = $loader.loadprim("std@int32_div",2); |
| 539 |
Int32.mod = $loader.loadprim("std@int32_mod",2); |
| 540 |
Int32.shl = $loader.loadprim("std@int32_shl",2); |
| 541 |
Int32.shr = $loader.loadprim("std@int32_shr",2); |
| 542 |
Int32.ushr = $loader.loadprim("std@int32_ushr",2); |
| 543 |
Int32.and = $loader.loadprim("std@int32_and",2); |
| 544 |
Int32.xor = $loader.loadprim("std@int32_xor",2); |
| 545 |
Int32.or = $loader.loadprim("std@int32_or",2); |
| 546 |
Int32.neg = $loader.loadprim("std@int32_neg",1); |
| 547 |
Int32.complement = $loader.loadprim("std@int32_complement",1); |
| 548 |
Int32.compare = $loader.loadprim("std@int32_compare",2); |
| 549 |
|
| 550 |
$exports.Int32 = Int32; |
| 551 |
|
| 552 |
// ---------------------------------------------------------------------- |
| 553 |
// StringBuf |
| 554 |
|
| 555 |
buffer_new = $loader.loadprim("std@buffer_new",0); |
| 556 |
buffer_add = $loader.loadprim("std@buffer_add",2); |
| 557 |
buffer_add_sub = $loader.loadprim("std@buffer_add_sub",4); |
| 558 |
buffer_add_char = $loader.loadprim("std@buffer_add_char",2); |
| 559 |
buffer_string = $loader.loadprim("std@buffer_string",1); |
| 560 |
|
| 561 |
StringBuf = $new(null); |
| 562 |
|
| 563 |
StringBuf.new = function() { |
| 564 |
var b = $new(null); |
| 565 |
$objsetproto(b,@StringBuf); |
| 566 |
b.@b = buffer_new(); |
| 567 |
return b; |
| 568 |
} |
| 569 |
|
| 570 |
@StringBuf = $new(null); |
| 571 |
|
| 572 |
StringBuf.prototype = @StringBuf; |
| 573 |
|
| 574 |
@StringBuf.add = function(s) { |
| 575 |
buffer_add(this.@b,s); |
| 576 |
} |
| 577 |
|
| 578 |
@StringBuf.addSub = function(s,p,l) { |
| 579 |
buffer_add_sub(this.@b,s,p,l); |
| 580 |
} |
| 581 |
|
| 582 |
@StringBuf.addChar = function(c) { |
| 583 |
buffer_add_char(this.@b,c); |
| 584 |
} |
| 585 |
|
| 586 |
@StringBuf.__string = function() { |
| 587 |
return buffer_string(this.@b); |
| 588 |
} |
| 589 |
|
| 590 |
@StringBuf.toString = function() { |
| 591 |
return String.new(this.__string()); |
| 592 |
} |
| 593 |
|
| 594 |
$exports.StringBuf = StringBuf; |
| 595 |
|
| 596 |
// ---------------------------------------------------------------------- |
| 597 |
// Xml |
| 598 |
|
| 599 |
parse_xml = $loader.loadprim("std@parse_xml",2); |
| 600 |
|
| 601 |
fxml = function(name,att) { |
| 602 |
var o = $new(null); |
| 603 |
$objsetproto(o,@Xml); |
| 604 |
var f = $objfields(att); |
| 605 |
|
| 606 |
var i = 0; |
| 607 |
var l = $asize(f); |
| 608 |
while( i < l ) { |
| 609 |
$objset(att,f[i], String.new($objget(att,f[i])) ); |
| 610 |
i = i + 1; |
| 611 |
} |
| 612 |
o.att = att; |
| 613 |
o.node = String.new(name); |
| 614 |
o.children = List.new(); |
| 615 |
o.parent = this.cur; |
| 616 |
this.cur.children.add(o); |
| 617 |
this.cur = o; |
| 618 |
} |
| 619 |
|
| 620 |
fpcdata = function(text) { |
| 621 |
var o = $new(null); |
| 622 |
$objsetproto(o,@Xml); |
| 623 |
o.text = String.new(text); |
| 624 |
o.parent = this.cur; |
| 625 |
o.cdata = false; |
| 626 |
this.cur.children.add(o); |
| 627 |
} |
| 628 |
|
| 629 |
fcdata = function(text) { |
| 630 |
var o = $new(); |
| 631 |
$objsetproto(o,@Xml); |
| 632 |
o.text = String.new(text); |
| 633 |
o.parent = this.cur; |
| 634 |
o.cdata = true; |
| 635 |
this.cur.children.add(o); |
| 636 |
} |
| 637 |
|
| 638 |
fcomment = function(comment) { |
| 639 |
} |
| 640 |
|
| 641 |
fdoctype = function(dtype) { |
| 642 |
} |
| 643 |
|
| 644 |
fdone = function() { |
| 645 |
this.cur = this.cur.parent; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
Xml = $new(null); |
| 650 |
|
| 651 |
Xml.new = function(s) { |
| 652 |
var doc = $new(null); |
| 653 |
$objsetproto(doc,@Xml); |
| 654 |
doc.children = List.new(); |
| 655 |
parse_xml(s.@s,{ |
| 656 |
cur => doc, |
| 657 |
xml => fxml, |
| 658 |
cdata => fcdata, |
| 659 |
pcdata => fpcdata, |
| 660 |
comment => fcomment, |
| 661 |
doctype => fdoctype, |
| 662 |
done => fdone |
| 663 |
}); |
| 664 |
return doc; |
| 665 |
} |
| 666 |
|
| 667 |
@Xml = $new(null); |
| 668 |
|
| 669 |
Xml.prototype = @Xml; |
| 670 |
|
| 671 |
@Xml.get = function(name) { |
| 672 |
if( this.att == null ) |
| 673 |
$throw(String.new("Xml.get")); |
| 674 |
return $objget(this.att,$hash(name.@s)); |
| 675 |
} |
| 676 |
|
| 677 |
@Xml.set = function(name,val) { |
| 678 |
if( this.att == null ) |
| 679 |
$throw(String.new("Xml.set")); |
| 680 |
return $objset(this.att,$hash(name.@s),val); |
| 681 |
} |
| 682 |
|
| 683 |
@Xml.nodes = function() { |
| 684 |
if( this.children == null ) |
| 685 |
$throw(String.new("Xml.nodes")); |
| 686 |
var s = this.children.h; |
| 687 |
var l = List.new(); |
| 688 |
while( s != null ) { |
| 689 |
var x = s[0]; |
| 690 |
if( x.node != null ) |
| 691 |
l.add(x); |
| 692 |
s = s[1]; |
| 693 |
} |
| 694 |
return l; |
| 695 |
} |
| 696 |
|
| 697 |
@Xml.firstChild = function() { |
| 698 |
if( this.children == null ) |
| 699 |
$throw(String.new("Xml.firstChild")); |
| 700 |
return this.children.first() |
| 701 |
} |
| 702 |
|
| 703 |
@Xml.firstNode = function() { |
| 704 |
if( this.children == null ) |
| 705 |
$throw(String.new("Xml.firstNode")); |
| 706 |
var s = this.children.h; |
| 707 |
while( s != null ) { |
| 708 |
if( s[0].node != null ) |
| 709 |
return s[0]; |
| 710 |
s = s[1]; |
| 711 |
} |
| 712 |
return null; |
| 713 |
} |
| 714 |
|
| 715 |
@Xml.toString = function() { |
| 716 |
return String.new(this.__string()); |
| 717 |
} |
| 718 |
|
| 719 |
xml_string_rec = function(x,b) { |
| 720 |
if( x.text != null ) { |
| 721 |
if( x.cdata ) |
| 722 |
buffer_add(b,"<![CDATA["); |
| 723 |
buffer_add(b,x.text); |
| 724 |
if( x.cdata ) |
| 725 |
buffer_add(b,"]]>"); |
| 726 |
return; |
| 727 |
} |
| 728 |
var s = x.children; |
| 729 |
if( s == null ) |
| 730 |
return; |
| 731 |
s = s.h; |
| 732 |
if( x.node != null ) { |
| 733 |
buffer_add(b,"<"); |
| 734 |
buffer_add(b,x.node); |
| 735 |
var a = x.att; |
| 736 |
var f = $objfields(a); |
| 737 |
var n = $asize(f); |
| 738 |
var i = 0; |
| 739 |
while( i < n ) { |
| 740 |
buffer_add(b," "); |
| 741 |
buffer_add(b,$field(f[i])); |
| 742 |
buffer_add(b,"=\""); |
| 743 |
buffer_add(b,$objget(a,f[i])); |
| 744 |
buffer_add(b,"\""); |
| 745 |
i = i + 1; |
| 746 |
} |
| 747 |
if( s == null ) { |
| 748 |
buffer_add(b,"/>"); |
| 749 |
return; |
| 750 |
} |
| 751 |
buffer_add(b,">"); |
| 752 |
while( s != null ) { |
| 753 |
xml_string_rec(s[0],b); |
| 754 |
s = s[1]; |
| 755 |
} |
| 756 |
buffer_add(b,"</"); |
| 757 |
buffer_add(b,x.node); |
| 758 |
buffer_add(b,">"); |
| 759 |
return; |
| 760 |
} |
| 761 |
while( s != null ) { |
| 762 |
xml_string_rec(s[0],b); |
| 763 |
s = s[1]; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
@Xml.__string = function() { |
| 768 |
var b = buffer_new(); |
| 769 |
xml_string_rec(this,b); |
| 770 |
return buffer_string(b); |
| 771 |
} |
| 772 |
|
| 773 |
@Xml.__serialize = function() { |
| 774 |
return $array("Xml",version); |
| 775 |
} |
| 776 |
|
| 777 |
$exports.Xml = Xml; |
| 778 |
|
| 779 |
$exports.__unserialize = function(v) { |
| 780 |
if( $typeof(v) != $tarray ) |
| 781 |
$throw("Invalid serialized data"); |
| 782 |
if( v[1] != version ) |
| 783 |
$throw("Invalid version for class "+v[0]); |
| 784 |
return switch( v[0] ) { |
| 785 |
"Xml" => @Xml |
| 786 |
"List" => @List |
| 787 |
"Array" => @Array |
| 788 |
"String" => @String |
| 789 |
"Hash" => @Hash |
| 790 |
default => $throw("Unknown class "+v[0]) |
| 791 |
} |
| 792 |
} |