I like to ask about
Is it possible to write something like this with ADA
```Ada
package my_rw_file is new file
(name => "whatever"
,mode => read_write
,implementation => standard -- or portable or fast
);
package as_string is new. xxx(from => my_rw_file);
-- parse (as_string);
package data is new parse (as_string, format => markdown); -- or whatever
```
Is it possible to write something like this with ADA
package my_rw_file is new file
(name => "whatever"
,mode => read_write
,implementation => standard -- or portable or fast
);
package as_string is new. xxx(from => my_rw_file);
-- parse (as_string);
package data is new parse (as_string, format => markdown); -- or whatever
On 2022-11-21 09:30, Jesper Quorning wrote:
Is it possible to write something like this with ADA
"Ada" is a woman's name, not an acronymn.
package my_rw_file is new file
  (name => "whatever"
   ,mode => read_write
   ,implementation => standard -- or portable or fast
  );
package as_string is new. xxx(from => my_rw_file);
-- parse (as_string);
package data is new parse (as_string, format => markdown); -- or whatever
If I presume that the '.' in the declaration of As_String is a typo,
what you have here is a sequence of related generic pkg instantiations,
so you can write this in Ada if you have the corresponding generic pkgs.
I have no idea what the result would provide.
If you want to read the arbitrary contents of a file into a String,
that's easily done:
with Ada.Directories;
package String_A_File is
  use type Ada.Directories.File_Size;
  function File_As_String (Name : in String) return String with
     Pre => Ada.Directories.Exists (Name) and then
             Ada.Directories.Size (Name) <=
             Ada.Directories.File_Size (Integer'Last),
     Post => File_As_String'Result'First = 1 and
             File_As_String'Result'Last =
             Integer (Ada.Directories.Size (Name) );
end String_A_File;
with Ada.Sequential_IO;
package body String_A_File is
  function File_As_String (Name : in String) return String is
     subtype FAS is String (1 .. Integer (Ada.Directories.Size (Name) ) );
     package FAS_IO is new Ada.Sequential_IO (Element_Type => FAS);
     File  : FAS_IO.File_Type;
     Result : FAS;
  begin -- File_As_String
     FAS_IO.Open (File => File, Mode => FAS_IO.In_File, Name => Name);
     FAS_IO.Read (File => File, Item => Result;
     FAS_IO.Close (File => File);
     return Result;
  end File_As_String;
end String_A_File;
This presumes that Result will fit on the stack. If that's likely to be
a problem, then you will need to use Unbounded_String and read the file Character by Character.
Is it possible to write something like this with ADA
```Ada
package my_rw_file is new file
(name => "whatever"
,mode => read_write
,implementation => standard -- or portable or fast
);
package as_string is new. xxx(from => my_rw_file);
-- parse (as_string);
package data is new parse (as_string, format => markdown); -- or whatever
For the OP's benefit (Jeffrey of course knows this): an alternative to Unbounded_String is to allocate the Result string on the heap, and return an access to the heap string. With that method, you can still read the entire string with one call of FAS_IO.Read instead of Character by Character.
On 2022-11-21 16:52, Niklas Holsti wrote:
For the OP's benefit (Jeffrey of course knows this): an alternative to
Unbounded_String is to allocate the Result string on the heap, and
return an access to the heap string. With that method, you can still
read the entire string with one call of FAS_IO.Read instead of
Character by Character.
I know it, and I deliberately reject it. Having access types in a pkg
spec is poor design. Delegating the associated memory management and all
its opportunities for error to the pkg client is very poor design.
The original post was a bit of a mess, and I am not quite new to Ada. But I lack some skills regarding software construction and software engineering.
What I want goes something like this:
- Zero copy access to a (read-only) file.
- Get lines of file one by one as a stream of lines.
for Line of File.As_Line_Stream loop
Parse (Line);
end loop;
- Zero copy access to a (read-only) file.
On 2023-01-02 00:36, Jesper Quorning wrote:
- Zero copy access to a (read-only) file.
So far as I know, any access to a file involves copying information from
the external medium into memory, so this may be impossible.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 399 |
Nodes: | 16 (2 / 14) |
Uptime: | 65:03:02 |
Calls: | 8,355 |
Calls today: | 15 |
Files: | 13,159 |
Messages: | 5,893,946 |
Posted today: | 1 |