Globally replace text in several files: Difference between revisions

→‎{{header|Fortran}}: File system interface minimalism.
m (added whitespace before the TOC.)
(→‎{{header|Fortran}}: File system interface minimalism.)
Line 462:
This is in the style of F77 and solves the usual problem of "how long is a piece of string" by choosing a size that is surely long enough. Thus, CHARACTER*6666 ALINE. Fortran 2003 allows the alteration of the length of a character variable, but, unless there is a facility whereby in something like <code>READ(F,11) ALINE</code> the size of ALINE is adjusted to suit the record being read, this doesn't help much. The Q format code allows discovery of the length of a record as it is being read, and so only the required portion, ALINE(1:L), of an input record is placed and trailing spaces out to 6666 are not supplied nor need they be scanned. Thus an input record that has trailing spaces will have them preserved - unless the text replacement changes spaces...
 
The search is done by using the supplied INDEX function which alas rarely has an option to specify the starting point via an additional (optional?) parameter. So this must be done via <code>INDEX(ALINE(L1:L),THIS)</code> and then one must carefully consider offsets and the like while counting on fingers and becoming confused. On the other hand, ''it'' handles annoyances such as ALINE(L1:L) being shorter than THIS. The expression ALINE(L1:L) does ''not'' create a new string variable by copying the specified text, it works (or should work!) via offsets into ALINE. Similarly, there is no attempt to concatenate an output string to write in one go as that too would involve copying text about. Though WRITE statements involve no small overhead in themselves.. Although if LEN(THIS) = LEN(THAT) as is the case in the example task an alter-in-place could be used and ALINE(1:L) be written out in one go, the more general approach is used of writing text up to the start of a match, writing out the replacement THAT, and scanning beyond the match for the next text until the tail end.
 
The file to be altered cannot be changed "in-place", as by writing back an altered record even if the text replacement does not involve a change in length because such a facility is not available for text files that are read and written sequentially only. More accomplished file systems may well offer varying-length records with update possible even of longer or shorter new versions but standard Fortran does not demand such facilities. So, the altered content has to be written to a temporary file (or perhaps could be held in a capacious memory) which is then read back to overwrite the original file. It would be safer to rename the original file and write to a new version, but Fortran typically does not have access to any file renaming facilities and the task calls for an overwrite anyway. So, overwrite it is, which is actually a file delete followed by a write.
 
Once equipped with a subroutine that applies a specified change to a named disc file, there is no difficulty in invoking it for a horde of disc files. A more civilised routine might make reports about the files assaulted and the number of changes, and also be prepared to report various oddities such as a file being available but not for WRITE. It is for this reason that the source file is opened with READWRITE even though it at that stage is only going to be read from.<lang Fortran> SUBROUTINE FILEHACK(FNAME,THIS,THAT) !Attacks a file!
1,220

edits