Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
nedtaylor opened this issue Jul 31, 2024 · 3 comments
Open

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

nedtaylor opened this issue Jul 31, 2024 · 3 comments
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@nedtaylor
Copy link

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:

  • touch (bash form of creating a blank file or directory if it doesn't already exist)
  • grep (get regular expression from the file)
  • goto (go to line number)

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

  • touch - implemented in bash
  • grep - implemented in bash
  • goto - not sure if many languages have one like this, but the closest I can think of would be sed -n '10p' filename.

Additional Information

No response

@nedtaylor nedtaylor added the idea Proposition of an idea and opening an issue to discuss it label Jul 31, 2024
@nedtaylor nedtaylor changed the title File handling procedures (goto, touch, grep) Proposal: File handling procedures (goto, touch, grep) Jul 31, 2024
@jalvesz
Copy link
Contributor

jalvesz commented Aug 1, 2024

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
Copy link
Author

@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
Copy link
Contributor

jalvesz commented Aug 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

No branches or pull requests

2 participants