/usr/share/ada/adainclude/gtkada/glib-xml.adb is in libgtkada2.24.1-dev 2.24.1-14.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 | -----------------------------------------------------------------------
-- GtkAda - Ada95 binding for the Gimp Toolkit --
-- --
-- Copyright (C) 1999-2000 E. Briot, J. Brobecker and A. Charlet --
-- Copyright (C) 2000-2010, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public --
-- License as published by the Free Software Foundation; either --
-- version 2 of the License, or (at your option) any later version. --
-- --
-- This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. --
-- --
-- You should have received a copy of the GNU General Public --
-- License along with this library; if not, write to the --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330, --
-- Boston, MA 02111-1307, USA. --
-- --
-----------------------------------------------------------------------
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Glib.Convert; use Glib.Convert;
with Glib.Error; use Glib.Error;
with Glib.Unicode; use Glib.Unicode;
with Glib.Messages; use Glib.Messages;
package body Glib.XML is
function File_Length (FD : Integer) return Long_Integer;
pragma Import (C, File_Length, "__gnat_file_length");
-- Get length of file from file descriptor FD
function Open_Read
(Name : String;
Fmode : Integer := 0) return Integer;
pragma Import (C, Open_Read, "__gnat_open_read");
-- Open file Name and return a file descriptor
function Create_File
(Name : String;
Fmode : Integer := 0) return Integer;
pragma Import (C, Create_File, "__gnat_open_create");
-- Creates new file with given name for writing, returning file descriptor
-- for subsequent use in Write calls. File descriptor returned is
-- negative if file cannot be successfully created.
function Read
(FD : Integer;
A : System.Address;
N : Integer) return Integer;
pragma Import (C, read, "read");
-- Read N bytes to address A from file referenced by FD. Returned value is
-- count of bytes actually read, which can be less than N at EOF.
procedure Write
(FD : Integer;
S : String;
N : Integer);
pragma Import (C, write, "write");
-- Write N bytes from address A to file referenced by FD. The returned
-- value is the number of bytes written, which can be less than N if a
-- disk full condition was detected.
procedure Close (FD : Integer);
pragma Import (C, Close, "close");
-- Close file referenced by FD
procedure Skip_Blanks (Buf : String; Index : in out Natural);
-- Skip blanks, LF and CR, starting at Index. Index is updated to the
-- new position (first non blank or EOF)
function Get_Node (Buf : String; Index : access Natural) return Node_Ptr;
-- The main parse routine. Starting at Index.all, Index.all is updated
-- on return. Return the node starting at Buf (Index.all) which will
-- also contain all the children and subchildren.
procedure Get_Buf
(Buf : String;
Index : in out Natural;
Terminator : Character;
S : out String_Ptr);
-- On return, S will contain the String starting at Buf (Index) and
-- terminating before the first 'Terminator' character. Index will also
-- point to the next non blank character.
-- The special XML '&' characters are translated appropriately in S.
-- S is set to null if Terminator wasn't found in Buf.
procedure Extract_Attrib
(Tag : in out String_Ptr;
Attributes : out String_Ptr;
Empty_Node : out Boolean);
-- Extract the attributes as a string, if the tag contains blanks ' '
-- On return, Tag is unchanged and Attributes contains the string
-- If the last character in Tag is '/' then the node is empty and
-- Empty_Node is set to True.
procedure Get_Next_Word
(Buf : String;
Index : in out Natural;
Word : out String_Ptr);
-- extract the next textual word from Buf and return it.
-- return null if no word left.
-- The special XML '&' characters are translated appropriately in S.
function Translate (S : String) return String;
-- Translate S by replacing the XML '&' special characters by the
-- actual ASCII character.
-- This function currently handles:
-- - "
-- - >
-- - <
-- - &
-- - '
-----------------
-- Skip_Blanks --
-----------------
procedure Skip_Blanks (Buf : String; Index : in out Natural) is
begin
while Index < Buf'Last and then
(Buf (Index) = ' ' or else Buf (Index) = ASCII.LF
or else Buf (Index) = ASCII.HT
or else Buf (Index) = ASCII.CR)
loop
Index := Index + 1;
end loop;
end Skip_Blanks;
-------------
-- Get_Buf --
-------------
procedure Get_Buf
(Buf : String;
Index : in out Natural;
Terminator : Character;
S : out String_Ptr)
is
Start : constant Natural := Index;
begin
while Index <= Buf'Last
and then Buf (Index) /= Terminator
loop
Index := Index + 1;
end loop;
if Index > Buf'Last then
S := null;
else
S := new String'(Translate (Buf (Start .. Index - 1)));
Index := Index + 1;
if Index < Buf'Last then
Skip_Blanks (Buf, Index);
end if;
end if;
end Get_Buf;
------------------------
-- Extract_Attributes --
------------------------
procedure Extract_Attrib
(Tag : in out String_Ptr;
Attributes : out String_Ptr;
Empty_Node : out Boolean)
is
Index : Natural := Tag'First;
Index_Last_Of_Tag : Natural;
S : String_Ptr;
begin
-- First decide if the node is empty
if Tag (Tag'Last) = '/' then
Empty_Node := True;
else
Empty_Node := False;
end if;
while Index <= Tag'Last and then
not
(Tag (Index) = ' ' or else Tag (Index) = ASCII.LF
or else Tag (Index) = ASCII.HT
or else Tag (Index) = ASCII.CR)
loop
Index := Index + 1;
end loop;
Index_Last_Of_Tag := Index - 1;
Skip_Blanks (Tag.all, Index);
if Index <= Tag'Last then
if Empty_Node then
Attributes := new String'(Tag (Index .. Tag'Last - 1));
else
Attributes := new String'(Tag (Index .. Tag'Last));
end if;
S := new String'(Tag (Tag'First .. Index_Last_Of_Tag));
Free (Tag);
Tag := S;
end if;
end Extract_Attrib;
-------------------
-- Get_Next_Word --
-------------------
procedure Get_Next_Word
(Buf : String;
Index : in out Natural;
Word : out String_Ptr)
is
Terminator : Character;
begin
Skip_Blanks (Buf, Index);
if Buf (Index) = ''' or Buf (Index) = '"' then
-- If the word starts with a quotation mark, then read until
-- the closing mark
Terminator := Buf (Index);
Index := Index + 1;
Get_Buf (Buf, Index, Terminator, Word);
else
-- For a normal word, scan up to either a blank, or a '='
declare
Start_Index : constant Natural := Index;
begin
while Index <= Buf'Last
and then Buf (Index) /= ' '
and then Buf (Index) /= '='
loop
Index := Index + 1;
end loop;
Word := new String'(Translate (Buf (Start_Index .. Index - 1)));
end;
end if;
if Index < Buf'Last then
Skip_Blanks (Buf, Index);
end if;
end Get_Next_Word;
---------------
-- Translate --
---------------
function Translate (S : String) return String is
Str : String (1 .. S'Length);
Start, J : Positive;
Index : Positive := S'First;
In_String : Boolean := False;
begin
if S'Length = 0 then
return S;
else
J := Str'First;
loop
if In_String or else S (Index) /= '&' then
Str (J) := S (Index);
else
Index := Index + 1;
Start := Index;
while S (Index) /= ';' loop
Index := Index + 1;
pragma Assert (Index <= S'Last);
end loop;
if S (Start .. Index - 1) = "quot" then
Str (J) := '"';
elsif S (Start .. Index - 1) = "gt" then
Str (J) := '>';
elsif S (Start .. Index - 1) = "lt" then
Str (J) := '<';
elsif S (Start .. Index - 1) = "amp" then
Str (J) := '&';
elsif S (Start .. Index - 1) = "apos" then
Str (J) := ''';
end if;
end if;
exit when Index = S'Last;
if S (Index) = '"' then
In_String := not In_String;
end if;
Index := Index + 1;
J := J + 1;
end loop;
return Str (1 .. J);
end if;
end Translate;
-------------------
-- Get_Attribute --
-------------------
function Get_Attribute
(N : Node_Ptr;
Attribute_Name : UTF8_String;
Default : UTF8_String := "") return UTF8_String
is
Index : Natural;
Key, Value : String_Ptr;
begin
if N = null or else N.Attributes = null then
return Default;
end if;
Index := N.Attributes'First;
while Index < N.Attributes'Last loop
Get_Next_Word (N.Attributes.all, Index, Key);
Get_Buf (N.Attributes.all, Index, '=', Value);
Free (Value);
Get_Next_Word (N.Attributes.all, Index, Value);
if Attribute_Name = Key.all then
exit;
else
Free (Key);
Free (Value);
end if;
end loop;
Free (Key);
if Value = null then
return Default;
else
declare
V : constant String := Value.all;
begin
Free (Value);
return V;
end;
end if;
end Get_Attribute;
-------------------
-- Set_Attribute --
-------------------
procedure Set_Attribute
(N : Node_Ptr; Attribute_Name, Attribute_Value : UTF8_String)
is
Index, Tmp : Natural;
Key, Value : String_Ptr;
Atts : String_Ptr;
Str : constant String :=
Attribute_Name & "=""" & Protect (Attribute_Value) & """ ";
begin
if N.Attributes /= null then
Index := N.Attributes'First;
-- First remove any definition of the attribute in the current list
while Index < N.Attributes'Last loop
Tmp := Index;
Get_Next_Word (N.Attributes.all, Index, Key);
Get_Buf (N.Attributes.all, Index, '=', Value);
Free (Value);
Get_Next_Word (N.Attributes.all, Index, Value);
Free (Value);
if Attribute_Name = Key.all then
Atts := new String'
(Str
& N.Attributes (N.Attributes'First .. Tmp - 1)
& N.Attributes (Index .. N.Attributes'Last));
Free (N.Attributes);
N.Attributes := Atts;
Free (Key);
return;
end if;
Free (Key);
end loop;
Atts := new String'(Str & N.Attributes.all);
Free (N.Attributes);
N.Attributes := Atts;
else
N.Attributes := new String'(Str);
end if;
end Set_Attribute;
---------------
-- Add_Child --
---------------
procedure Add_Child
(N : Node_Ptr; Child : Node_Ptr; Append : Boolean := False)
is
Tmp : Node_Ptr;
begin
if Append then
if N.Child = null then
N.Child := Child;
else
Tmp := N.Child;
while Tmp.Next /= null loop
Tmp := Tmp.Next;
end loop;
Tmp.Next := Child;
end if;
else
Child.Next := N.Child;
N.Child := Child;
end if;
Child.Parent := N;
end Add_Child;
--------------
-- Get_Node --
--------------
function Get_Node (Buf : String; Index : access Natural) return Node_Ptr is
N : constant Node_Ptr := new Node;
Q : Node_Ptr;
S : String_Ptr;
Empty_Node : Boolean;
Last_Child : Node_Ptr;
begin
pragma Assert (Buf (Index.all) = '<');
Index.all := Index.all + 1;
Get_Buf (Buf, Index.all, '>', N.Tag);
-- Check to see whether it is a comment, !DOCTYPE, or the like:
if N.Tag (N.Tag'First) = '!' then
return Get_Node (Buf, Index);
else
-- Here we have to deal with the attributes of the form
-- <tag attrib='xyyzy'>
Extract_Attrib (N.Tag, N.Attributes, Empty_Node);
-- it is possible to have a child-less node that has the form
-- <tag /> or <tag attrib='xyyzy'/>
if Empty_Node then
N.Value := new String'("");
else
if Buf (Index.all) = '<' then
if Buf (Index.all + 1) = '/' then
-- No value contained on this node
N.Value := new String'("");
Index.all := Index.all + 1;
else
-- Parse the children
Add_Child (N, Get_Node (Buf, Index));
Last_Child := N.Child;
pragma Assert (Buf (Index.all) = '<');
while Buf (Index.all + 1) /= '/' loop
Q := Last_Child;
Q.Next := Get_Node (Buf, Index);
Q.Next.Parent := N;
Last_Child := Q.Next;
pragma Assert (Buf (Index.all) = '<');
end loop;
Index.all := Index.all + 1;
end if;
else
-- Get the value of this node
Get_Buf (Buf, Index.all, '<', N.Value);
end if;
pragma Assert (Buf (Index.all) = '/');
Index.all := Index.all + 1;
Get_Buf (Buf, Index.all, '>', S);
pragma Assert (N.Tag.all = S.all);
Free (S);
end if;
return N;
end if;
exception
when others =>
return null;
end Get_Node;
-------------
-- Protect --
-------------
function Protect (S : String) return String is
Length : Natural := 0;
Valid_Utf8 : Boolean;
Invalid_Pos : Natural;
Pos : Natural;
procedure Update_Length (Idx : Natural);
-- Update the final length of the result string
procedure Translate
(Idx : Natural; Res : in out String; Res_Idx : in out Natural);
-- Protect an xml-reserved character into its entities equivalence
-------------------
-- Update_Length --
-------------------
procedure Update_Length (Idx : Natural) is
begin
case S (Idx) is
when '<' => Length := Length + 4;
when '>' => Length := Length + 4;
when '&' => Length := Length + 5;
when ''' => Length := Length + 6;
when '"' => Length := Length + 6;
when others =>
-- Single case for ascii/utf8: ascii control characters will
-- also match.
if Unichar_Type (UTF8_Get_Char (S (Idx .. S'Last))) =
Unicode_Control
then
declare
Str : constant String :=
Glib.Gunichar'Image
(UTF8_Get_Char (S (Idx .. S'Last)));
begin
-- Add 2: -1 for the starting space, and +3 for leading
-- &# and trailing ;
Length := Length + Str'Length + 2;
end;
elsif Valid_Utf8 then
Length := Length + UTF8_Next_Char (S, Idx) - Idx;
else
Length := Length + 1;
end if;
end case;
end Update_Length;
---------------
-- Translate --
---------------
procedure Translate
(Idx : Natural; Res : in out String; Res_Idx : in out Natural) is
begin
case S (Idx) is
when '<' =>
Res (Res_Idx .. Res_Idx + 3) := "<";
Res_Idx := Res_Idx + 4;
when '>' =>
Res (Res_Idx .. Res_Idx + 3) := ">";
Res_Idx := Res_Idx + 4;
when '&' =>
Res (Res_Idx .. Res_Idx + 4) := "&";
Res_Idx := Res_Idx + 5;
when ''' =>
Res (Res_Idx .. Res_Idx + 5) := "'";
Res_Idx := Res_Idx + 6;
when '"' =>
Res (Res_Idx .. Res_Idx + 5) := """;
Res_Idx := Res_Idx + 6;
when others =>
declare
Char : constant Glib.Gunichar :=
UTF8_Get_Char (S (Idx .. S'Last));
Next : Natural;
begin
if Unichar_Type (Char) = Unicode_Control then
declare
Str : constant String := Glib.Gunichar'Image (Char);
begin
Res (Res_Idx .. Res_Idx + Str'Length + 1) :=
"&#" & Str (Str'First + 1 .. Str'Last) & ";";
Res_Idx := Res_Idx + Str'Length + 2;
end;
elsif Valid_Utf8 then
Next := UTF8_Next_Char (S, Idx);
Res (Res_Idx .. Res_Idx + Next - Idx - 1) :=
S (Idx .. Next - 1);
Res_Idx := Res_Idx + Next - Idx;
else
Res (Res_Idx) := S (Idx);
Res_Idx := Res_Idx + 1;
end if;
end;
end case;
end Translate;
begin
UTF8_Validate (S, Valid_Utf8, Invalid_Pos);
if Valid_Utf8 then
Pos := S'First;
while Pos <= S'Last loop
Update_Length (Pos);
Pos := UTF8_Next_Char (S, Pos);
end loop;
else
for J in S'Range loop
Update_Length (J);
end loop;
end if;
declare
Result : String (1 .. Length);
Index : Integer := 1;
begin
if Valid_Utf8 then
Pos := S'First;
while Pos <= S'Last loop
Translate (Pos, Result, Index);
Pos := UTF8_Next_Char (S, Pos);
end loop;
else
for J in S'Range loop
Translate (J, Result, Index);
end loop;
end if;
return Result;
end;
end Protect;
-----------
-- Print --
-----------
procedure Print (N : Node_Ptr; File_Name : String := "") is
Success : Boolean;
pragma Unreferenced (Success);
begin
Print (N, File_Name, Success);
end Print;
-----------
-- Print --
-----------
procedure Print
(N : Node_Ptr;
File_Name : String;
Success : out Boolean)
is
File : Integer := 1;
procedure Do_Indent (Indent : Natural);
-- Print a string made of Indent blank characters.
procedure Print_String (S : String);
-- Print S to File, after replacing the special '<', '>',
-- '"', '&' and ''' characters.
procedure Print_Node (N : Node_Ptr; Indent : Natural);
-- Write a node and its children to File
procedure Put (S : String);
-- Write S to File
procedure Put_Line (S : String);
-- Write S & LF to File
---------
-- Put --
---------
procedure Put (S : String) is
begin
Write (File, S, S'Length);
end Put;
--------------
-- Put_Line --
--------------
procedure Put_Line (S : String) is
begin
Put (S & ASCII.LF);
end Put_Line;
---------------
-- Do_Indent --
---------------
procedure Do_Indent (Indent : Natural) is
begin
Put ((1 .. Indent => ' '));
end Do_Indent;
------------------
-- Print_String --
------------------
procedure Print_String (S : String) is
begin
for J in S'Range loop
case S (J) is
when '<' => Put ("<");
when '>' => Put (">");
when '&' => Put ("&");
when ''' => Put ("'");
when '"' => Put (""");
when ASCII.NUL .. Character'Val (9)
| Character'Val (11) .. Character'Val (31) =>
declare
Img : constant String :=
Integer'Image (Character'Pos (S (J)));
begin
Put ("&#" & Img (Img'First + 1 .. Img'Last) & ";");
end;
when others => Put ((1 => S (J)));
end case;
end loop;
end Print_String;
----------------
-- Print_Node --
----------------
procedure Print_Node (N : Node_Ptr; Indent : Natural) is
P : Node_Ptr;
begin
Do_Indent (Indent);
Put ("<" & N.Tag.all);
if N.Attributes /= null then
Put (" " & N.Attributes.all);
end if;
if N.Child /= null then
Put_Line (">");
P := N.Child;
while P /= null loop
Print_Node (P, Indent + 2);
P := P.Next;
end loop;
Do_Indent (Indent);
Put_Line ("</" & N.Tag.all & ">");
elsif N.Value = null
or else N.Value.all = ""
then
-- The following handles the difference between what you got
-- when you parsed <tag/> vs. <tag />.
if N.Tag (N.Tag'Last) = '/' then
Put_Line (">");
else
Put_Line (" />");
end if;
else
Put (">");
Print_String (N.Value.all);
Put_Line ("</" & N.Tag.all & ">");
end if;
end Print_Node;
begin
if File_Name /= "" then
File := Create_File (File_Name & ASCII.NUL);
if File < 0 then
Success := False;
return;
end if;
end if;
Put_Line ("<?xml version=""1.0""?>");
Print_Node (N, 0);
if File_Name /= "" then
Close (File);
end if;
Success := True;
end Print;
-----------
-- Parse --
-----------
function Parse (File : String) return Node_Ptr is
function Read_File (The_File : String) return String_Ptr;
-- Return the contents of an entire file.
-- If the file cannot be found, return null.
-- The caller is responsible for freeing the returned memory.
---------------
-- Read_File --
---------------
function Read_File (The_File : String) return String_Ptr is
FD : Integer;
Buffer : String_Ptr;
Length : Integer;
pragma Warnings (Off, Length);
begin
FD := Open_Read (The_File & ASCII.NUL);
if FD < 0 then
return null;
end if;
Length := Integer (File_Length (FD));
Buffer := new String (1 .. Length);
Length := Read (FD, Buffer.all'Address, Length);
Close (FD);
return Buffer;
end Read_File;
Buf : String_Ptr;
Result : Node_Ptr;
begin
Buf := Read_File (File);
if Buf = null then
return null;
end if;
Result := Parse_Buffer (Buf.all);
Free (Buf);
return Result;
end Parse;
------------------
-- Parse_Buffer --
------------------
function Parse_Buffer (Buffer : UTF8_String) return Node_Ptr is
Index : aliased Natural := 2;
XML_Version : String_Ptr;
Encoding : Integer;
Encoding_Last : Integer;
Result : Node_Ptr;
begin
Get_Buf (Buffer, Index, '>', XML_Version);
if XML_Version = null then
return null;
else
-- Check the encoding specified for that file
Encoding := Ada.Strings.Fixed.Index (XML_Version.all, "encoding");
if Encoding /= 0 then
while Encoding <= XML_Version'Last
and then XML_Version (Encoding) /= '"'
loop
Encoding := Encoding + 1;
end loop;
Encoding := Encoding + 1;
Encoding_Last := Encoding + 1;
while Encoding_Last <= XML_Version'Last
and then XML_Version (Encoding_Last) /= '"'
loop
Encoding_Last := Encoding_Last + 1;
end loop;
if Encoding_Last <= XML_Version'Last then
declare
Error : aliased GError;
Utf8_Buffer : constant String := Glib.Convert.Convert
(Buffer,
To_Codeset => "UTF-8",
From_Codeset =>
XML_Version (Encoding .. Encoding_Last - 1),
Error => Error'Unchecked_Access);
begin
if Utf8_Buffer /= "" then
Result := Get_Node (Utf8_Buffer, Index'Unchecked_Access);
else
Glib.Messages.Log
("Glib", Log_Level_Warning, Get_Message (Error));
Error_Free (Error);
end if;
end;
else
Result := Get_Node (Buffer, Index'Unchecked_Access);
end if;
else
Result := Get_Node (Buffer, Index'Unchecked_Access);
end if;
Free (XML_Version);
return Result;
end if;
end Parse_Buffer;
--------------
-- Find_Tag --
--------------
function Find_Tag (N : Node_Ptr; Tag : UTF8_String) return Node_Ptr is
P : Node_Ptr := N;
begin
while P /= null loop
if P.Tag.all = Tag then
return P;
end if;
P := P.Next;
end loop;
return null;
end Find_Tag;
-----------------------------
-- Find_Tag_With_Attribute --
-----------------------------
function Find_Tag_With_Attribute
(N : Node_Ptr;
Tag : UTF8_String;
Key : UTF8_String;
Value : UTF8_String := "") return Node_Ptr
is
P : Node_Ptr := N;
begin
while P /= null loop
if P.Tag.all = Tag then
declare
The_Value : constant String := Get_Attribute (P, Key);
begin
if The_Value /= "" then
if Value = "" or The_Value = Value then
-- if Value is not given when calling the
-- the function only the Key need to match
return P;
end if;
end if;
end;
end if;
P := P.Next;
end loop;
return null;
end Find_Tag_With_Attribute;
---------------
-- Get_Field --
---------------
function Get_Field (N : Node_Ptr; Field : UTF8_String) return String_Ptr is
P : constant Node_Ptr := Find_Tag (N.Child, Field);
begin
if P /= null then
return P.Value;
else
return null;
end if;
end Get_Field;
----------
-- Free --
----------
procedure Free
(N : in out Node_Ptr; Free_Data : Free_Specific_Data := null)
is
procedure Free_Node (N : in out Node_Ptr);
-- Free the memory for a node, but doesn't remove it from its parent
procedure Unchecked_Free is new Unchecked_Deallocation (Node, Node_Ptr);
---------------
-- Free_Node --
---------------
procedure Free_Node (N : in out Node_Ptr) is
Child : Node_Ptr := N.Child;
Next : Node_Ptr;
begin
Free (N.Tag);
Free (N.Attributes);
Free (N.Value);
if Free_Data /= null then
Free_Data (N.Specific_Data);
end if;
-- Free all the children
while Child /= null loop
Next := Child.Next;
Free_Node (Child);
Child := Next;
end loop;
Unchecked_Free (N);
end Free_Node;
Child : Node_Ptr;
Previous : Node_Ptr;
begin
if N = null then
return;
end if;
if N.Parent /= null then
Child := N.Parent.Child;
-- Remove the node from its parent
while Child /= null and then Child /= N loop
Previous := Child;
Child := Child.Next;
end loop;
if Child = N then
if Previous = null then
N.Parent.Child := N.Next;
else
Previous.Next := N.Next;
end if;
end if;
end if;
-- Free the memory occupied by the node
Free_Node (N);
end Free;
---------------
-- Deep_Copy --
---------------
function Deep_Copy (N : Node_Ptr) return Node_Ptr is
function Deep_Copy_Internal
(N : Node_Ptr; Parent : Node_Ptr := null) return Node_Ptr;
-- Internal version of Deep_Copy. Returns a deep copy of N, whose
-- parent should be Parent.
function Deep_Copy_Internal
(N : Node_Ptr; Parent : Node_Ptr := null) return Node_Ptr
is
Attr : String_Ptr;
Value : String_Ptr;
New_N : Node_Ptr;
Child : Node_Ptr;
N_Child : Node_Ptr;
begin
if N = null then
return null;
else
if N.Attributes /= null then
Attr := new String'(N.Attributes.all);
end if;
if N.Value /= null then
Value := new String'(N.Value.all);
end if;
-- Do not clone Next: For the initial node, we should not clone
-- the next nodes, only its children. And for children this is
-- done by Deep_Copy_Internal on the parent
New_N := new Node'
(Tag => new String'(N.Tag.all),
Attributes => Attr,
Value => Value,
Parent => Parent,
Child => null,
Next => null,
Specific_Data => N.Specific_Data);
-- Clone each child
Child := N.Child;
while Child /= null loop
if N_Child = null then
New_N.Child := Deep_Copy_Internal (Child, Parent => New_N);
N_Child := New_N.Child;
else
N_Child.Next := Deep_Copy_Internal (Child, Parent => New_N);
N_Child := N_Child.Next;
end if;
Child := Child.Next;
end loop;
return New_N;
end if;
end Deep_Copy_Internal;
begin
return Deep_Copy_Internal (N);
end Deep_Copy;
--------------
-- Is_Equal --
--------------
function Is_Equal (Node1, Node2 : Node_Ptr) return Boolean is
begin
if Node1 = null then
if Node2 /= null then
return False;
else
return True;
end if;
elsif Node2 = null then
return False;
end if;
if Node1.Tag = null then
if Node2.Tag /= null then
return False;
end if;
elsif Node2.Tag = null then
return False;
elsif Node1.Tag.all /= Node2.Tag.all then
return False;
end if;
if Node1.Attributes = null then
if Node2.Attributes /= null then
return False;
end if;
elsif Node2.Attributes = null then
return False;
elsif Node1.Attributes.all /= Node2.Attributes.all then
return False;
end if;
if Node1.Value = null then
if Node2.Value /= null then
return False;
end if;
elsif Node2.Value = null then
return False;
elsif Node1.Value.all /= Node2.Value.all then
return False;
end if;
if Node1.Child = null then
if Node2.Child /= null then
return False;
end if;
elsif Node2.Child = null then
return False;
elsif not Is_Equal (Node1.Child, Node2.Child) then
return False;
end if;
if Node1.Next = null then
if Node2.Next /= null then
return False;
end if;
elsif Node2.Next = null then
return False;
elsif not Is_Equal (Node1.Next, Node2.Next) then
return False;
end if;
return True;
end Is_Equal;
--------------------
-- Children_Count --
--------------------
function Children_Count (N : Node_Ptr) return Natural is
Tmp : Node_Ptr;
Count : Natural := 0;
begin
if N /= null then
Tmp := N.Child;
while Tmp /= null loop
Count := Count + 1;
Tmp := Tmp.Next;
end loop;
end if;
return Count;
end Children_Count;
end Glib.XML;
|