j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

Base64 encoding in I/O statements #330

Open ivan-pi opened 8 months ago

ivan-pi commented 8 months ago

Popular visualization formats like the VTK XML File Format use Base64 encoding of numerical data as text. This slightly increases the amount of storage, but proves to be more reliable when distributing data via channels that predominantly support text (e.g. the internet). Another example use case would be embedding binary data (e.g. images, other binary assets) in HTML reports generated by a Fortran program.

The idea was suggested here:

As the user "rwmsu" has stated in that thread:

This would be a big help for writing things like the VTK XML format files where you want to mix base64 encoded binary with standard text. I presume it would have to be an option on the read and/or write statement. something like

Write(iunit, *) ‘’
Write(iunit, *, ENCODE=“base64”) some_real_array

etc.

Having that capability in a compiler would have saved me the time and the almost 1000 lines of code it took to develop my own base64 encoding/decoding package.

Right now, available libraries for Base64 encoding include:

klausler commented 8 months ago

When the need for a feature can be addressed in portable open-source libraries that are available today (or in a few years), experience shows that that's a much better solution than trying to change the language. A "standard" feature is likely to be an incomplete solution -- you'll want some means to deal with endianness, and the standard language doesn't even provide that today for unformatted I/O despite near-universal CONVERT= extensions. It won't appear in a standard until 2028 at the earliest, and it won' be widely available until the 2030's. Since the current process does not generate any conformance tests, portability across implementations won't be assured, and since the process doesn't involve prototyping, any ambiguity or contradictions in the feature's description will lurk in the document until implementors try to make sense of it after publication.

If you have a library implementation today, and it's portable and universally available, you already have the best solution. Second-best would be a pilot extension to an open-source Fortran compiler and I/O runtime library.

certik commented 8 months ago

since the process doesn't involve prototyping

The Fortran standard process must involve prototyping.

Second-best would be a pilot extension to an open-source Fortran compiler and I/O runtime library.

Yes, that has to be a prerequisite for any proposal.

certik commented 8 months ago

Another idea: if Fortran had a way to hook in user-defined readers/writers and still use the built-in Fortran syntax to open and read from files (see https://github.com/j3-fortran/fortran_proposals/issues/331), then one could define a "Base64Writer" as a user library, and still use the write syntax.

ivan-pi commented 8 months ago

The hook you propose seems related to Section 2.3.9 of the coroutine proposal: https://j3-fortran.org/doc/year/19/19-169.pdf (I haven't dived into the details yet, just noticed it has a similar purpose)