Dynamically Loading Sections of the Text Into Memory

As an alternative to reading in an entire text file into the memory file at once, text in an ASCII file can be loaded into memory only when needed by the application. This is done by putting a suffix after the keyword in the text file to indicate how to handle the keyworded section. Follow these steps for using dynamically loaded keyword sections:

Step 1: To set up the text file for automatic dynamic loading of text, append a comma and the letter 'A' (for automatic keyword loading) to the end of the keyword for those sections that you want dynamically read in. If you do not put ',A' at the end of the keyword, this section is read into memory when mf_rd() is called.

For example, the text file below has three keyworded sections, two of which will be dynamically loaded when needed by the application and one that is always in memory.

*FM INFO

This has no suffix at the end of the keyword so the whole section is read into memory.

*FM_FLDINFO,A

This has the automatic keyword suffix (,A).  This text will be dynamically loaded when needed by the application.

*FM_MEMOINFO,A

This has the automatic keyword suffix (,A).  This text will be dynamically loaded when needed by the application.

Another line.

Step 2: Define the memory file with mf_def(). The number of columns should be the maximum number of characters in any line in the text file. The number of rows must be equal to or greater than: the number of keywords, plus the number of lines of text that are always in memory, plus the largest number of lines of text that can be read in dynamically during the program.

For the example above, the memory file must be defined to have 80 columns and at least 8 rows (3 keyword lines, 2 lines of text in memory all the time, and a maximum of 3 that can be read in at any time during the program).

Note: The function mf_size() returns the minimum number of rows and columns needed to hold a memory file. This function does take automatically-loaded keyword sections into account. It may be easier to use this function to determine the number of rows and columns needed, rather than doing the calculations yourself.

Step 3: Read in the memory file with mf_rd(). You must read in the memory file before any part of the memory file text is needed, whether it is dynamically loaded or put in memory.

When mf_rd() encounters a keyword without a suffix, the keyword line and each line of text in that section is read into the memory file. When mf_rd() encounters a keyword with the suffix ',A', the keyword line is read into memory and an index into the text file is associated with that keyword.

At this point, you can use the memory file as you normally would. When a certain section of the memory file is called for, the system looks at the keyword in the memory file and determines where the text is. If it is not already in the memory file, it uses the index associated with the keyword to read in the appropriate lines into the memory. These lines are then cleared when the next keyword section is read in by the application.

Caution: The text file cannot change during the application, because indices into the text file are used to determine what lines in the file to read into memory. You should ensure that the text file does not change by avoiding mf_wr() in your application and making the file safe from external changes from other sources by making it a read-only file. The file stays open until the memory file is read or another mf_rd() is called for that memory file.

More:

Speeding Dynamic Loading with an Index File

Changing the File Extension of the Index File

Manually Loading and Unloading Keyworded Sections

Changing the Delimiter Character


Home Contents Previous Next