json_ada_d429d7af/json/src/json-streams.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
--  SPDX-License-Identifier: Apache-2.0
--
--  Copyright (c) 2016 onox <denkpadje@gmail.com>
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.

with Ada.Characters.Latin_1;
with Ada.IO_Exceptions;
with Ada.Streams.Stream_IO;
with Ada.Unchecked_Conversion;

package body JSON.Streams is

   use type AS.Stream_Element_Offset;

   procedure Read_Character (Object : in out Stream; Item : out Character) is
      function Convert is new Ada.Unchecked_Conversion
        (Source => AS.Stream_Element, Target => Character);
   begin
      if Object.Index not in Object.Bytes'Range then
         raise Ada.IO_Exceptions.End_Error;
      end if;

      Item := Convert (Object.Bytes (Object.Index));
      Object.Index := Object.Index + 1;
   end Read_Character;

   function Has_Buffered_Character (Object : Stream) return Boolean
     is (Object.Next_Character /= Ada.Characters.Latin_1.NUL);

   function Read_Character (Object : in out Stream) return Character is
      Index : AS.Stream_Element_Offset;
   begin
      return Object.Read_Character (Index);
   end Read_Character;

   function Read_Character
     (Object : in out Stream;
      Index  : out AS.Stream_Element_Offset) return Character
   is
      C : Character;
   begin
      Index := Object.Index;
      if Object.Next_Character = Ada.Characters.Latin_1.NUL then
         Object.Read_Character (C);
      else
         C := Object.Next_Character;
         Object.Next_Character := Ada.Characters.Latin_1.NUL;
      end if;
      return C;
   end Read_Character;

   procedure Write_Character (Object : in out Stream; Next : Character) is
   begin
      Object.Next_Character := Next;
   end Write_Character;

   function Is_Equal_String
     (Object : Stream;
      Offset, Length : AS.Stream_Element_Offset;
      Value : String) return Boolean
   is
      subtype Constrained_String is String (1 .. Integer (Length));

      function Convert is new Ada.Unchecked_Conversion
        (Source => AS.Stream_Element_Array, Target => Constrained_String);
   begin
      return Value = Convert (Object.Bytes (Offset .. Offset + Length - 1));
   end Is_Equal_String;

   function Get_String
     (Object : Stream;
      Offset, Length : AS.Stream_Element_Offset) return String
   is
      subtype Constrained_String is String (1 .. Integer (Length));

      function Convert is new Ada.Unchecked_Conversion
        (Source => AS.Stream_Element_Array, Target => Constrained_String);
   begin
      return Convert (Object.Bytes (Offset .. Offset + Length - 1));
   end Get_String;

   function Create_Stream
     (Bytes : not null Stream_Element_Array_Access) return Stream is
   begin
      return (Bytes          => Bytes,
              Next_Character => Ada.Characters.Latin_1.NUL,
              Index          => Bytes'First);
   end Create_Stream;

   -----------------------------------------------------------------------------

   function From_File
     (File_Name : String) return Stream_Element_Array_Access
   is
      package IO renames AS.Stream_IO;

      File : IO.File_Type;
   begin
      IO.Open (File, IO.In_File, File_Name);

      begin
         declare
            subtype Byte_Array is AS.Stream_Element_Array
              (1 .. AS.Stream_Element_Offset (IO.Size (File)));
            Content : constant not null Stream_Element_Array_Access := new Byte_Array;
         begin
            Byte_Array'Read (IO.Stream (File), Content.all);
            IO.Close (File);
            return Content;
         end;
      exception
         when others =>
            IO.Close (File);
            raise;
      end;
   end From_File;

   function From_Text
     (Text : String) return Stream_Element_Array_Access
   is
      subtype Constrained_SEA is AS.Stream_Element_Array (1 .. Text'Length);

      function Convert is new Ada.Unchecked_Conversion
        (Source => String, Target => Constrained_SEA);

      Content : constant Stream_Element_Array_Access := new Constrained_SEA'(Convert (Text));
   begin
      return Content;
   end From_Text;

end JSON.Streams;