fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.09k stars 166 forks source link

Proposal: File handling procedures (goto, touch, grep) #851

Open nedtaylor opened 3 months ago

nedtaylor commented 3 months ago

Motivation

I have had a look through the stdlib_io module and cannot find the following procedures implemented. But I could be wrong.

Should stdlib contain some more io features present in languages such as bash? I quite often find the need to use the following and have, so far, resorted to using my own implementations:

I often find the need for procedures to quickly navigate and check for strong occurrences within files to be able to best take advantage external files.

Prior Art

Additional Information

No response

jalvesz commented 3 months ago

My first impression is that touch and grep are linux-user-oriented, Windows users might not be familiar with their syntax. Such functionality should be OS agnostic (as best as possible).

Regarding touch I think that Fortran's native open already does the job, no?

Regarding grep you might want to look at https://fortran-lang.discourse.group/t/fortran-regex-library/4917/3 and https://fortran-lang.discourse.group/t/new-release-of-forgex-fortran-regular-expression/8325. It would be a nice addition to stdlib to have some kind of combination of those repos here!

For goto, it seems like a nice idea, I would just suggest using a different name to avoid confusions with go to.

nedtaylor commented 3 months ago

@jalvesz, thanks for the great feedback.:)

Yeah, these are very linux/unix-based ideas. Does that mean they shouldn't be in fpm, or just change the naming convention and their syntax?

Regarding open, does it work for directories as well as files (then again, I'm not even sure if bash touch makes new directories).

Regarding grep, from my cursory glance at those libraries, I believe they act on strings, not files (I could be wrong, please correct me if so). You are definitely right though, more regular expression handling capabilities would be great!

My own personal implementation of goto is called jump.

jalvesz commented 3 months ago

I guess fpm needed to include all these toolings internally to handle file manipulation. Then the question is whether fpm should depend on stdlib such that certain file manipulation procedures are taken from stdlib instead ... ?

You are right, open does not work for directories. The alternative might be to encapsulate something like call execute_command_line ('mkdir -p out/' // dirname ). execute_command_line requires Fortran2008, and mkdir is recognized by Windows and Unix.

Regarding grep, from my cursory glance at those libraries, I believe they act on strings, not files (I could be wrong, please correct me if so). You are definitely right though, more regular expression handling capabilities would be great!

Yes, in which case you could load the file into a single large string and from there apply all sorts of extractions

character(:), allocatable :: file_str_handle
open( newunit = u , file=name, access='stream', action="read", iostat=err  )

inquire(unit=u, size=file_sze)
allocate(character(file_sze) :: file_str_handle)
read(u) file_str_handle
close(u)
...
!> Do stuff on "file_str_handle"

This is limited to files that can be fully loaded in the RAM. Larger files might need something like this https://fortran-lang.discourse.group/t/memory-mapped-files-in-fortran/7178/20 to be available ... or loading by chunks reading line-by-line.