Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Internal stream I/O #857

Open
ivan-pi opened this issue Aug 6, 2024 · 0 comments
Open

Internal stream I/O #857

ivan-pi opened this issue Aug 6, 2024 · 0 comments
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@ivan-pi
Copy link
Member

ivan-pi commented Aug 6, 2024

Motivation

In some cases it is necessary to read data from a character string in "stream" mode. Fortran does not support stream access on internal files.

In C this can be done using fread, where the file handle is associated to a character buffer; on POSIX conforming systems, this can be done using fmemopen. In C++ the std::basic_stringstream can be used for this purpose.

An interface in Fortran could look something like this:

! Read data from stream into buffer
!
! buffer ... object where the read data will be stored
! size ... number of bytes to be read
! stream ... the character string to be treated as a stream
! ierr ... error flag
subroutine stream_read(buffer,size,stream,ierr)
   integer, parameter :: ascii = selected_char_kind ("ascii")
   type(*), intent(inout) :: buffer
   integer, intent(in) :: size
   character(len=:,kind=ascii), pointer, intent(inout) :: stream
   integer, intent(out), optional :: ierr
end subroutine

It may be possible to use a deferred-rank object (i.e. dimension(..)) to automatically convey the dimensions of the buffer. But I suppose the element size needs to be communicated separately. This would be similar to how MPI procedures work: https://docs.open-mpi.org/en/v5.0.x/man-openmpi/man3/MPI_Recv.3.html#fortran-2008-syntax

Prior Art

stdlib already contains a similar function to_num_from_stream (spec found here).

Additional Information

Related threads:

@ivan-pi ivan-pi added the idea Proposition of an idea and opening an issue to discuss it label Aug 6, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

No branches or pull requests

1 participant