gnoga_2.1.2_5f127c56/deps/zanyblue/src/text/zanyblue-text-catalogs.adb

   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
--  -*- coding: utf-8 -*-
--
--  ZanyBlue, an Ada library and framework for finite element analysis.
--
--  Copyright (c) 2012, 2017, Michael Rohan <mrohan@zanyblue.com>
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions
--  are met:
--
--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--
--    * Neither the name of ZanyBlue nor the names of its contributors may
--      be used to endorse or promote products derived from this software
--      without specific prior written permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
--  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
--  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
--  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
--  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
--  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

with Ada.Containers.Indefinite_Vectors;
with ZanyBlue.OS;
with ZanyBlue.Text.Message_Maps;
with ZanyBlue.Text.Indexed_Strings;
with UXStrings.Conversions;

package body ZanyBlue.Text.Catalogs is

   use Ada.Containers;
   use ZanyBlue.OS;
   use ZanyBlue.Text.Message_Maps;
   use ZanyBlue.Text.Indexed_Strings;

   type Catalog_Value is record
      Facilities     : Indexed_Strings_Type;
      Keys           : Indexed_Strings_Type;
      Locales        : Indexed_Strings_Type;
      Messages       : Message_Map_Type;
      Logical_Size   : Natural               := 0;
      Pseudo_Map     : Pseudo_Map_Access;
      Mark_Messages  : Boolean               := True;
      Mark_Arguments : Boolean               := True;
      Single_Pool    : Boolean               := False;
      Raise_Errors   : Boolean               := True;
      Source_Locales : Boolean               := True;
      Printer        : Printer_Access        := Standard_Printer;
      Filter         : Message_Filter_Access := null;
   end record;

   function Map_To_Triple
     (Catalog  : Catalog_Type;
      Facility : String;
      Key      : String;
      Locale   : Locale_Type;
      Create   : Boolean)
      return Message_Triple;

   ---------
   -- Add --
   ---------

   procedure Add
     (Catalog  : Catalog_Type;
      Facility : String;
      Key      : String;
      Message  : String;
      Locale   : Locale_Type)
   is
   begin
      Add (Catalog, Facility, Key, Message, Locale, Locale);
   end Add;

   ---------
   -- Add --
   ---------

   procedure Add
     (Catalog       : Catalog_Type;
      Facility      : String;
      Key           : String;
      Message       : String;
      Locale        : Locale_Type;
      Source_Locale : Locale_Type)
   is

      Triple : constant Message_Triple :=
        Map_To_Triple (Catalog, Facility, Key, Locale, True);

   begin
      Add_Locale (Catalog, Source_Locale);
      Catalog.C.Logical_Size := Catalog.C.Logical_Size + Message.Length;
      Catalog.C.Messages.Add
        (Triple, Message, Get_Locale_Index (Catalog, Source_Locale));
   end Add;

   ---------
   -- Add --
   ---------

   procedure Add
     (Catalog  : Catalog_Type;
      Facility : String;
      Key      : String;
      Pool     : Static_Message_Pool_Type;
      First    : Positive;
      Last     : Natural;
      Locale   : Locale_Type)
   is
   begin
      Add (Catalog, Facility, Key, Pool, First, Last, Locale, Locale);
   end Add;

   ---------
   -- Add --
   ---------

   procedure Add
     (Catalog       : Catalog_Type;
      Facility      : String;
      Key           : String;
      Pool          : Static_Message_Pool_Type;
      First         : Positive;
      Last          : Natural;
      Locale        : Locale_Type;
      Source_Locale : Locale_Type)
   is

      New_Message : Message_Definition;
      Triple      : Message_Triple;

   begin
      Add_Locale (Catalog, Source_Locale);
      --  Validate the message is actually within the argument pool
      if First < Pool.First or else Last > Pool.Last then
         raise Invalid_Static_Message_Error;
      end if;
      if Catalog.C.Single_Pool then
         --  Application requested all message be stored in a single
         --  pool: simply add static message to the dynamic pool area.
         --  This is normally used by "zbmcompile" when generating code.
         Add
           (Catalog, Facility, Key, Pool.Slice (First, Last), Locale,
            Source_Locale);
         return;
      end if;
      Triple := Map_To_Triple (Catalog, Facility, Key, Locale, True);
      New_Message.Pool         := Pool;
      New_Message.First        := First;
      New_Message.Last         := Last;
      New_Message.Locale_Index := Get_Locale_Index (Catalog, Source_Locale);
      Catalog.C.Messages.Add (Triple, New_Message);
   end Add;

   ------------------
   -- Add_Facility --
   ------------------

   procedure Add_Facility
     (Catalog  : Catalog_Type;
      Facility : String)
   is
      Index : Facility_Index_Type;             --  Throwaway value
      pragma Warnings (Off, Index);
   begin
      Add_Facility (Catalog, Facility, Index);
   end Add_Facility;

   ------------------
   -- Add_Facility --
   ------------------

   procedure Add_Facility
     (Catalog  :     Catalog_Type;
      Facility :     String;
      Index    : out Facility_Index_Type)
   is
   begin
      Catalog.C.Facilities.Add (Facility, Index);
   end Add_Facility;

   -------------
   -- Add_Key --
   -------------

   procedure Add_Key
     (Catalog :     Catalog_Type;
      Key     :     String;
      Index   : out Key_Index_Type)
   is
   begin
      Catalog.C.Keys.Add (Key, Index);
   end Add_Key;

   -------------------
   -- Add_Key_Value --
   -------------------

   overriding procedure Add_Key_Value
     (Handler       : in out Catalog_Handler_Type;
      Facility      :        String;
      Key           :        String;
      Value         :        String;
      Locale        :        Locale_Type;
      Source_Locale :        Locale_Type;
      File_Name     :        String;
      Line          :        Natural)
   is
      pragma Unreferenced (File_Name);
      pragma Unreferenced (Line);
   begin
      Add (Handler.Catalog, Facility, Key, Value, Locale, Source_Locale);
   end Add_Key_Value;

   ----------------
   -- Add_Locale --
   ----------------

   procedure Add_Locale
     (Catalog : Catalog_Type;
      Locale  : Locale_Type)
   is
      Index : Locale_Index_Type;             --  Throwaway value
      pragma Warnings (Off, Index);
   begin
      Add_Locale (Catalog, Locale, Index);
   end Add_Locale;

   ----------------
   -- Add_Locale --
   ----------------

   procedure Add_Locale
     (Catalog :     Catalog_Type;
      Locale  :     Locale_Type;
      Index   : out Locale_Index_Type)
   is
   begin
      Catalog.C.Locales.Add (Locale_Name (Locale), Index);
   end Add_Locale;

   ------------
   -- Create --
   ------------

   function Create return Catalog_Type is
      Result : constant Catalog_Type := (C => new Catalog_Value);
   begin
      return Result;
   end Create;

   ------------------------
   -- Disable_Exceptions --
   ------------------------

   procedure Disable_Exceptions (Catalog : Catalog_Type) is
   begin
      Catalog.C.Raise_Errors := False;
   end Disable_Exceptions;

   ----------------------------
   -- Disable_Source_Locales --
   ----------------------------

   procedure Disable_Source_Locales (Catalog : Catalog_Type) is
   begin
      Catalog.C.Source_Locales := False;
   end Disable_Source_Locales;

   ----------
   -- Dump --
   ----------

   procedure Dump
     (Catalog   : Catalog_Type;
      File_Name : String := "") is separate;

   -------------------
   -- Duplicate_Key --
   -------------------

   overriding procedure Duplicate_Key
     (Handler       : in out Catalog_Handler_Type;
      Facility      :        String;
      Key           :        String;
      Locale        :        Locale_Type;
      File_Name     :        String;
      Current_Line  :        Natural;
      Previous_Line :        Natural)
   is
      pragma Unreferenced (Handler);
      pragma Unreferenced (Facility);
      pragma Unreferenced (Locale);
   begin
      raise Duplicate_Key_Error
        with To_UTF_8 (File_Name & ":" & Key) & ":" &
        Natural'Image (Current_Line) & ":" & Natural'Image (Previous_Line);
   end Duplicate_Key;

   -----------------------
   -- Enable_Exceptions --
   -----------------------

   procedure Enable_Exceptions (Catalog : Catalog_Type) is
   begin
      Catalog.C.Raise_Errors := True;
   end Enable_Exceptions;

   --------------------------------
   -- Enable_Pseudo_Translations --
   --------------------------------

   procedure Enable_Pseudo_Translations
     (Catalog        : Catalog_Type;
      Mapping        : Pseudo_Map_Vector;
      Mark_Messages  : Boolean := True;
      Mark_Arguments : Boolean := True)
   is
   begin
      Catalog.C.Pseudo_Map := new Pseudo_Map_Type;
      Catalog.C.Pseudo_Map.Add_Mapping (Mapping);
      Catalog.C.Mark_Messages  := Mark_Messages;
      Catalog.C.Mark_Arguments := Mark_Arguments;
   end Enable_Pseudo_Translations;

   ---------------------------
   -- Enable_Source_Locales --
   ---------------------------

   procedure Enable_Source_Locales (Catalog : Catalog_Type) is
   begin
      Catalog.C.Source_Locales := True;
   end Enable_Source_Locales;

   ------------------------
   -- Exceptions_Enabled --
   ------------------------

   function Exceptions_Enabled
     (Catalog : Catalog_Type)
      return Boolean
   is
   begin
      return Catalog.C.Raise_Errors;
   end Exceptions_Enabled;

   -----------------
   -- Get_Catalog --
   -----------------

   function Get_Catalog
     (Handler : Catalog_Handler_Type)
      return Catalog_Type
   is
   begin
      return Handler.Catalog;
   end Get_Catalog;

   ------------------
   -- Get_Facility --
   ------------------

   function Get_Facility
     (Catalog : Catalog_Type;
      Index   : Facility_Index_Type)
      return String
   is
   begin
      return Catalog.C.Facilities.Get (Positive (Index));
   exception
      when No_Such_Item =>
         raise No_Such_Facility_Error with Facility_Index_Type'Image (Index);
   end Get_Facility;

   ------------------------
   -- Get_Facility_Index --
   ------------------------

   function Get_Facility_Index
     (Catalog : Catalog_Type;
      Name    : String)
      return Facility_Index_Type
   is
      Index : Positive;
   begin
      Index :=
        Catalog.C.Facilities.Get (Name, No_Such_Facility_Error'Identity);
      return Facility_Index_Type (Index);
   end Get_Facility_Index;

   -------------
   -- Get_Key --
   -------------

   function Get_Key
     (Catalog : Catalog_Type;
      Index   : Key_Index_Type)
      return String
   is
   begin
      return Catalog.C.Keys.Get (Positive (Index));
   exception
      when No_Such_Item =>
         raise No_Such_Key_Error with Key_Index_Type'Image (Index);
   end Get_Key;

   -------------------
   -- Get_Key_Index --
   -------------------

   function Get_Key_Index
     (Catalog : Catalog_Type;
      Name    : String)
      return Key_Index_Type
   is
      Index : Positive;
   begin
      Index := Catalog.C.Keys.Get (Name, No_Such_Key_Error'Identity);
      return Key_Index_Type (Index);
   end Get_Key_Index;

   ----------------
   -- Get_Locale --
   ----------------

   function Get_Locale
     (Catalog : Catalog_Type;
      Index   : Locale_Index_Type)
      return Locale_Type
   is
   begin
      return Make_Locale (Catalog.C.Locales.Get (Positive (Index)));
   exception
      when No_Such_Item =>
         raise No_Such_Locale_Error with Locale_Index_Type'Image (Index);
   end Get_Locale;

   ----------------------
   -- Get_Locale_Index --
   ----------------------

   function Get_Locale_Index
     (Catalog : Catalog_Type;
      Name    : String)
      return Locale_Index_Type
   is
      Index : Positive;
   begin
      Index := Catalog.C.Locales.Get (Name, No_Such_Locale_Error'Identity);
      return Locale_Index_Type (Index);
   end Get_Locale_Index;

   ----------------------
   -- Get_Locale_Index --
   ----------------------

   function Get_Locale_Index
     (Catalog : Catalog_Type;
      Locale  : Locale_Type)
      return Locale_Index_Type
   is
   begin
      return Get_Locale_Index (Catalog, Locale_Name (Locale));
   end Get_Locale_Index;

   ---------------------
   -- Get_Locale_Name --
   ---------------------

   function Get_Locale_Name
     (Catalog : Catalog_Type;
      Index   : Locale_Index_Type)
      return String
   is
   begin
      return Locale_Name (Get_Locale (Catalog, Index));
   end Get_Locale_Name;

   ------------------------
   -- Get_Mark_Arguments --
   ------------------------

   function Get_Mark_Arguments
     (Catalog : Catalog_Type)
      return Boolean
   is
   begin
      return Catalog.C.Mark_Arguments;
   end Get_Mark_Arguments;

   -----------------------
   -- Get_Mark_Messages --
   -----------------------

   function Get_Mark_Messages
     (Catalog : Catalog_Type)
      return Boolean
   is
   begin
      return Catalog.C.Mark_Messages;
   end Get_Mark_Messages;

   --------------
   -- Get_Pool --
   --------------

   function Get_Pool
     (Catalog : Catalog_Type)
      return String
   is
   begin
      if not Catalog.C.Single_Pool then
         raise Multiple_Pools_Error;
      end if;
      return Catalog.C.Messages.Get_Pool;
   end Get_Pool;

   --------------------
   -- Get_Pseudo_Map --
   --------------------

   function Get_Pseudo_Map
     (Catalog : Catalog_Type)
      return Pseudo_Map_Access
   is
   begin
      return Catalog.C.Pseudo_Map;
   end Get_Pseudo_Map;

   --------------
   -- Get_Text --
   --------------

   function Get_Text
     (Catalog        : Catalog_Type;
      Facility       : String;
      Key            : String;
      Locale         : Locale_Type;
      Message_Locale : access Locale_Type := null)
      return String
   is

      Message        : Message_Definition;
      Language       : Language_Type;
      Script         : Script_Type;
      Territory      : Territory_Type;
      Base_Territory : Territory_Type;
      Triple         : Message_Triple;

   begin
      Get_Locale_Codes (Locale, Language, Script, Territory);
      Base_Territory := Territory;
      Locale_Loop :
      for I in 1 .. Maximum_Locale_Parents loop
         Map_Locale_Triple :
         begin
            Triple :=
              Map_To_Triple
                (Catalog, Facility, Key,
                 Make_Locale (Language, Script, Territory), False);
            Catalog.C.Messages.Get (Triple, Message);
            if Message_Locale /= null then
               Message_Locale.all :=
                 Get_Locale (Catalog, Message.Locale_Index);
            end if;
            return Catalog.C.Messages.Text (Message);
         exception
            when No_Such_Locale_Error | Constraint_Error =>
               exit Locale_Loop when Language = Empty_Language;
               Parent_Codes
                 (Language, Script, Territory,
                  Base_Territory => Base_Territory);
         end Map_Locale_Triple;
      end loop Locale_Loop;
      raise No_Such_Message_Error with To_UTF_8 (Facility & "/" & Key);
   end Get_Text;

   --------------
   -- Get_Text --
   --------------

   function Get_Text
     (Catalog        : Catalog_Type;
      Facility_Index : Facility_Index_Type;
      Key_Index      : Key_Index_Type;
      Locale_Index   : Locale_Index_Type)
      return String
   is

      Message : Message_Definition;
      Triple  : constant Message_Triple :=
        (Facility_Index => Facility_Index, Key_Index => Key_Index,
         Locale_Index   => Locale_Index);

   begin
      Catalog.C.Messages.Get (Triple, Message);
      return Catalog.C.Messages.Text (Message);
   exception
      when Constraint_Error =>
         raise No_Such_Message_Error
           with Facility_Index_Type'Image (Facility_Index) & "/" &
           Key_Index_Type'Image (Key_Index);
   end Get_Text;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (Catalog         : Catalog_Type;
      Messages        : ZBMCompile_List;
      Pool            : Static_Message_Pool_Type;
      Facilities      : Constant_String_List;
      Keys            : Constant_String_List;
      Locales         : Constant_String_List;
      Package_Name    : String  := "";
      Pool_Length     : Natural := 0;
      Expected_Length : Natural := 0)
   is
      F, K, L, EL : Positive;
   begin
      if Pool_Length /= Expected_Length then
         raise Pool_Size_Mismatch_Error
           with To_UTF_8 (Package_Name) & Natural'Image (Pool_Length) & " /=" &
           Natural'Image (Expected_Length);
      end if;
      Reserve (Catalog, Messages => Messages'Length);
      for I in Messages'Range loop
         F  := Positive (Messages (I).Facility_Index);
         K  := Positive (Messages (I).Key_Index);
         L  := Positive (Messages (I).Locale_Index);
         EL := Positive (Messages (I).Source_Locale_Index);
         Add
           (Catalog, Facilities (F).all, Keys (K).all, Pool,
            Messages (I).First, Messages (I).Last,
            Make_Locale (Locales (L).all), Make_Locale (Locales (EL).all));
      end loop;
   end Initialize;

   -----------------------
   -- Invalid_Character --
   -----------------------

   overriding procedure Invalid_Character
     (Handler      : in out Catalog_Handler_Type;
      Facility     :        String;
      File_Name    :        String;
      Current_Line :        Natural;
      Ch           :        Character)
   is
      pragma Unreferenced (Handler);
      pragma Unreferenced (Facility);
   begin
      raise Unicode_Escape_Error
        with To_UTF_8 (File_Name) & ":" & Natural'Image (Current_Line) & ":" &
        "Invalid character: " & Ch;
   end Invalid_Character;

   ------------------------
   -- Invalid_Definition --
   ------------------------

   overriding procedure Invalid_Definition
     (Handler         : in out Catalog_Handler_Type;
      Facility        :        String;
      Locale          :        Locale_Type;
      File_Name       :        String;
      Current_Line    :        Natural;
      Additional_Info :        String)
   is
      pragma Unreferenced (Handler);
      pragma Unreferenced (Facility);
      pragma Unreferenced (Locale);
      function Image is new UXStrings.Conversions.Integer_Image (Natural);
   begin
      raise Unicode_Escape_Error
        with To_UTF_8
          (File_Name & ":" & Image (Current_Line) & ":" & Additional_Info);
   end Invalid_Definition;

   -----------------
   -- Is_Filtered --
   -----------------

   function Is_Filtered
     (Catalog  : Catalog_Type;
      Facility : String;
      Key      : String)
      return Boolean
   is
   begin
      return
        Catalog.C.Filter /= null
        and then Catalog.C.Filter.Is_Filtered (Facility, Key);
   end Is_Filtered;

   --------------
   -- Is_Valid --
   --------------

   function Is_Valid
     (Catalog : Catalog_Type)
      return Boolean
   is
   begin
      return Catalog.C /= null;
   end Is_Valid;

   -------------
   -- Iterate --
   -------------

   procedure Iterate
     (Catalog : Catalog_Type;
      Handler : not null access procedure
        (Facility      : Facility_Index_Type;
         Key           : Key_Index_Type;
         Locale        : Locale_Index_Type;
         Source_Locale : Locale_Index_Type;
         First         : Positive;
         Last          : Natural;
         Count         : Natural))
   is
   begin
      Catalog.C.Messages.Iterate (Handler);
   end Iterate;

   -------------
   -- Iterate --
   -------------

   procedure Iterate
     (Catalog : Catalog_Type;
      Handler : not null access procedure
        (Facility      : Facility_Index_Type;
         Key           : Key_Index_Type;
         Locale        : Locale_Index_Type;
         Source_Locale : Locale_Index_Type;
         Message       : String;
         Count         : Natural))
   is
   begin
      Catalog.C.Messages.Iterate (Handler);
   end Iterate;

   -------------------
   -- Load_Facility --
   -------------------

   procedure Load_Facility
     (Facility           :        String;
      Source_Name        :        String;
      N_Locales          :    out Natural;
      N_Messages         :    out Natural;
      Handler            : in out Catalog_Handler_Type'Class;
      Directory          :        String      := ".";
      Extension          :        String      := Default_Extension;
      Base_Locale_Only   :        Boolean     := False;
      Locale_Prefix      :        String      := "";
      Source_Root_Locale :        Locale_Type := Root_Locale) is separate;

   -------------------
   -- Load_Facility --
   -------------------

   procedure Load_Facility
     (Catalog            :     Catalog_Type;
      Facility           :     String;
      Source_Name        :     String;
      N_Locales          : out Natural;
      N_Messages         : out Natural;
      Directory          :     String      := ".";
      Extension          :     String      := Default_Extension;
      Base_Locale_Only   :     Boolean     := False;
      Locale_Prefix      :     String      := "";
      Source_Root_Locale :     Locale_Type := Root_Locale)
   is

      Handler : Catalog_Handler_Type;

   begin
      Handler.Catalog := Catalog;
      Load_Facility
        (Facility, Source_Name, N_Locales, N_Messages, Handler,
         Directory          => Directory, Extension => Extension,
         Base_Locale_Only => Base_Locale_Only, Locale_Prefix => Locale_Prefix,
         Source_Root_Locale => Source_Root_Locale);
   end Load_Facility;

   -------------------
   -- Load_Facility --
   -------------------

   procedure Load_Facility
     (Catalog            :     Catalog_Type;
      Facility           :     String;
      N_Locales          : out Natural;
      N_Messages         : out Natural;
      Directory          :     String      := ".";
      Extension          :     String      := Default_Extension;
      Source_Root_Locale :     Locale_Type := Root_Locale)
   is
   begin
      Load_Facility
        (Catalog, Facility, Facility, N_Locales, N_Messages,
         Directory          => Directory, Extension => Extension,
         Source_Root_Locale => Source_Root_Locale);
   end Load_Facility;

   ---------------
   -- Load_File --
   ---------------

   function Load_File
     (Catalog       : Catalog_Type;
      File_Name     : String;
      Facility      : String;
      Locale        : Locale_Type;
      Source_Locale : Locale_Type := Root_Locale)
      return Natural
   is

      Handler : Catalog_Handler_Type;

   begin
      Add_Locale (Catalog, Source_Locale);
      Handler.Set_Catalog (Catalog);
      Parse (Handler, File_Name, Facility, Locale, Source_Locale);
      return Handler.Get_N_Messages;
   end Load_File;

   ---------------
   -- Load_File --
   ---------------

   procedure Load_File
     (File_Name     :        String;
      Facility      :        String;
      Locale        :        Locale_Type;
      Handler       : in out Catalog_Handler_Type'Class;
      Source_Locale :        Locale_Type := Root_Locale)
   is
   begin
      Parse (Handler, File_Name, Facility, Locale, Source_Locale);
   end Load_File;

   -----------------------
   -- Logical_Pool_Size --
   -----------------------

   function Logical_Pool_Size
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Logical_Size;
   end Logical_Pool_Size;

   -------------------
   -- Map_To_Triple --
   -------------------

   function Map_To_Triple
     (Catalog  : Catalog_Type;
      Facility : String;
      Key      : String;
      Locale   : Locale_Type;
      Create   : Boolean)
      return Message_Triple
   is
      Result : Message_Triple;
   begin
      if Create then
         Add_Facility (Catalog, Facility, Result.Facility_Index);
         Add_Key (Catalog, Key, Result.Key_Index);
         Add_Locale (Catalog, Locale, Result.Locale_Index);
      else
         Result.Facility_Index := Get_Facility_Index (Catalog, Facility);
         Result.Key_Index      := Get_Key_Index (Catalog, Key);
         Result.Locale_Index   := Get_Locale_Index (Catalog, Locale);
      end if;
      return Result;
   end Map_To_Triple;

   --------------------------
   -- Number_Of_Facilities --
   --------------------------

   function Number_Of_Facilities
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Facilities.Length;
   end Number_Of_Facilities;

   --------------------
   -- Number_Of_Keys --
   --------------------

   function Number_Of_Keys
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Keys.Length;
   end Number_Of_Keys;

   -----------------------
   -- Number_Of_Locales --
   -----------------------

   function Number_Of_Locales
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Locales.Length;
   end Number_Of_Locales;

   ------------------------
   -- Number_Of_Messages --
   ------------------------

   function Number_Of_Messages
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Messages.Length;
   end Number_Of_Messages;

   ---------------
   -- Pool_Size --
   ---------------

   function Pool_Size
     (Catalog : Catalog_Type)
      return Natural
   is
   begin
      return Catalog.C.Messages.Pool_Size;
   end Pool_Size;

   -----------
   -- Print --
   -----------

--     procedure Print
--       (Catalog     : Catalog_Type;
--        Destination : Ada.Text_IO.File_Type;
--        Facility    : String;
--        Key         : String;
--        Locale      : Locale_Type;
--        Arguments   : ZanyBlue.Text.Arguments.Argument_List;
--        Message     : String;
--        With_NL     : Boolean)
--     is
--     begin
--        Catalog.C.Printer.Print
--          (Destination, Facility, Key, Locale, Arguments, Message, With_NL);
--     end Print;

   -----------
   -- Print --
   -----------

   procedure Print
     (Catalog     : Catalog_Type;
      Destination : UXStrings.Text_IO.File_Type;
      Facility    : String;
      Key         : String;
      Locale      : Locale_Type;
      Arguments   : ZanyBlue.Text.Arguments.Argument_List;
      Message     : String;
      With_NL     : Boolean)
   is
   begin
      Catalog.C.Printer.Print
        (Destination, Facility, Key, Locale, Arguments, Message, With_NL);
   end Print;

   -------------------
   -- Query_Message --
   -------------------

   procedure Query_Message
     (Catalog        :     Catalog_Type;
      Facility_Index :     Facility_Index_Type;
      Key_Index      :     Key_Index_Type;
      Locale_Index   :     Locale_Index_Type;
      First          : out Positive;
      Last           : out Natural)
   is

      Triple : constant Message_Triple :=
        (Facility_Index => Facility_Index, Key_Index => Key_Index,
         Locale_Index   => Locale_Index);
      Message : Message_Definition;

   begin
      if not Catalog.C.Single_Pool then
         raise Multiple_Pools_Error;
      end if;
      Catalog.C.Messages.Get (Triple, Message);
      First := Message.First;
      Last  := Message.Last;
   exception
      when Constraint_Error =>
         raise No_Such_Message_Error
           with Facility_Index_Type'Image (Facility_Index) & "/" &
           Key_Index_Type'Image (Key_Index);
   end Query_Message;

   -------------
   -- Reserve --
   -------------

   procedure Reserve
     (Catalog   : Catalog_Type;
      Pool_Size : Natural := 0;
      Messages  : Natural := 0)
   is
      pragma Unreferenced (Pool_Size);
   begin
      --  There doesn't appear to be an API to reserve space for an
      --  Unbounded String?  Skipping the Pool_Size adjustment.
      if Messages > 0 then
         Catalog.C.Messages.Adjust_Size (Messages);
      end if;
   end Reserve;

   -----------------
   -- Set_Catalog --
   -----------------

   procedure Set_Catalog
     (Handler : in out Catalog_Handler_Type;
      Catalog :        Catalog_Type)
   is
   begin
      Handler.Catalog := Catalog;
   end Set_Catalog;

   ----------------
   -- Set_Filter --
   ----------------

   procedure Set_Filter
     (Catalog : Catalog_Type;
      Filter  : Message_Filter_Access)
   is
   begin
      Catalog.C.Filter := Filter;
   end Set_Filter;

   -----------------
   -- Set_Printer --
   -----------------

   procedure Set_Printer
     (Catalog : Catalog_Type;
      Printer : Printer_Access)
   is
   begin
      Catalog.C.Printer := Printer;
   end Set_Printer;

   ----------------------------
   -- Source_Locales_Enabled --
   ----------------------------

   function Source_Locales_Enabled
     (Catalog : Catalog_Type)
      return Boolean
   is
   begin
      return Catalog.C.Source_Locales;
   end Source_Locales_Enabled;

   ---------------------
   -- Use_Single_Pool --
   ---------------------

   procedure Use_Single_Pool (Catalog : Catalog_Type) is
   begin
      Catalog.C.Single_Pool := True;
   end Use_Single_Pool;

end ZanyBlue.Text.Catalogs;