Debian Codegraph @

gcc/ada/libgnat/a-stbufi.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- ADA.STRINGS.TEXT_BUFFERS.FILES --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2020-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 package body Ada.Strings.Text_Buffers.Files is
33
34 procedure Put_UTF_8_Implementation
35 (Buffer : in out Root_Buffer_Type'Class;
36 Item : UTF_Encoding.UTF_8_String) is
37 Result : Integer;
38 begin
39 Result := OS.Write (File_Buffer (Buffer).FD,
40 Item (Item'First)'Address,
41 Item'Length);
42 if Result /= Item'Length then
43 raise Program_Error with OS.Errno_Message;
44 end if;
45 end Put_UTF_8_Implementation;
46
47 function Create_From_FD
48 (FD : System.OS_Lib.File_Descriptor;
49 Close_Upon_Finalization : Boolean := True) return File_Buffer
50 is
51 use OS;
52 begin
53 if FD = Invalid_FD then
54 raise Program_Error with OS.Errno_Message;
55 end if;
56 return Result : File_Buffer do
57 Result.FD := FD;
58 Result.Close_Upon_Finalization := Close_Upon_Finalization;
59 end return;
60 end Create_From_FD;
61
62 function Create_File (Name : String) return File_Buffer is
63 begin
64 return Create_From_FD (OS.Create_File (Name, Fmode => OS.Binary));
65 end Create_File;
66
67 procedure Finalize (Ref : in out Self_Ref) is
68 Success : Boolean;
69 use OS;
70 begin
71 if Ref.Self.FD /= OS.Invalid_FD
72 and then Ref.Self.Close_Upon_Finalization
73 then
74 Close (Ref.Self.FD, Success);
75 if not Success then
76 raise Program_Error with OS.Errno_Message;
77 end if;
78 end if;
79 Ref.Self.FD := OS.Invalid_FD;
80 end Finalize;
81
82 end Ada.Strings.Text_Buffers.Files;