iso_2.0.0_abd8a59c/generator/src/common.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
with Ada.Strings.Fixed;          use Ada.Strings.Fixed;
with Ada.Wide_Wide_Text_IO;      use Ada.Wide_Wide_Text_IO;
with Ada.Strings.Wide_Wide_Fixed;use Ada.Strings.Wide_Wide_Fixed;
package body Common is

   function Sanatize (S : Unbounded_Wide_Wide_String) return Unbounded_Wide_Wide_String
   is
      Check  : constant Wide_Wide_String := To_Wide_Wide_String(S);
      Result : Unbounded_Wide_Wide_String := Null_Unbounded_Wide_Wide_String;
   begin
      for Ch of Check loop
         if Ch = '"' then
            Append (Result, """""");
         else
            Append (Result, Ch);
         end if;
      end loop;
      return Result;
   end Sanatize;

   function Get_Next_Field (S : Wide_Wide_String; Next : out Natural)
      return Unbounded_Wide_Wide_String
   is
      Has_Comma : constant Boolean := S (S'First) = '"';
      --  Set it to the start, or start+1 if it has a quote.
      First : constant Natural := (if S'First = S'Last then raise Invalid_File
                              else (if Has_Comma then
                                          S'First + 1 else
                                          S'First));
      --  Set it to the next comma, or the quote if it has a quote.
      Next_Sep : constant Natural := Index (S (First .. S'Last),
                                          (if Has_Comma then
                                                """" else
                                                ","));
      Sep_Found : constant Boolean := Next_Sep > 0;
      --  Actual end of string we're returning.
      Last : constant Natural := (if Sep_Found then
                                    Next_Sep - 1 --  Right before , or "
                              else
                                    S'Last);
   begin

      --  Next is set to the comma/quote +1 (or end)
      Next := (if Sep_Found then
                  (if Has_Comma then -- we need to look for ", or " as eol
                  (if Next_Sep /= S'Last and then
                        (Next_Sep + 2) > S'Last
                  then
                        S'Last --  " is eol
                  else --  Next_Sep check
                        Next_Sep + 2) --  It's probably ",
                  else --  Has_Comma
                  Next_Sep + 1) --  It has no comma
               else --  Sep_Found
                  S'Last); --  No seperator found
      --  Return the result
      return To_Unbounded_Wide_Wide_String (S (First .. Last));
   end Get_Next_Field;

   function Get_Node_String (N : Node) return Unbounded_Wide_Wide_String
   is
      Result : Unbounded_Wide_Wide_String := Null_Unbounded_Wide_Wide_String;
      List   : constant Node_List := Child_Nodes (N);
   begin
      for I in 0 .. Length (List) - 1 loop
         declare
            Current_Node : constant Node := Item (List, I);
         begin
            Append (Result, TWS (Node_Value (Current_Node)));
         end;
      end loop;
      return Result;
   end Get_Node_String;
   function Get_Node_String (N : Node) return Unbounded_String is
   begin
      return To_Unbounded_String (TS (Get_Node_String (N)));
   end Get_Node_String;
end Common;