The QDFRTVFD API provides pretty much everything you could want to know about a display file.
When developing an application program, I tend to avoid hard-coding program values such as the limit value of a FOR operation. For instance, rather than hard-coding a FOR X TO 10, I might replace the 10 with an appropriate built-in, such as %size(Y), %len(Y), %elem(Y), etc. If Y has to double in size in the future, I much prefer changing the definition of Y and allowing a recompile of the program to take care of the details of loop control rather than having to search through the code and perhaps changing one or more occurrences of 10 to 20.
While recently presenting to a user group, a question came up concerning one part of a program where I had chosen to hard-code a value—in this case, the size of a subfile page that I was loading one page at a time. This hard-coding was similar to the approach I took in the article "Monocasing Character Data" a few months ago where, as shown below, I initialized the variable SizeOfSFLPage to the size of a subfile page (12) and then used this variable for loop control.
dSizeOfSFLPage s 5u 0 inz(12)
…
GetNxtMsgs(SizeOfSFLPage);
…
for myX = 1 to (NbrMsgsToGet + 1);
…
endfor;
…
The question raised was whether or not this hard-coded initialization of SizeOfSFLPage to 12 can be avoided. As this would be a very short article indeed if the answer was "no," you can safely assume that there is an API to provide a program, at run time, the page size for a subfile. The API is Retrieve Display File Description (QDFRTVFD) and is documented here.
Reviewing the API documentation, you will quickly see that there is a whole lot more information available to you than just the SFLPAG value. The API can, in fact, appear to be quite daunting in terms of all the structures, substructures, and field definitions. Fortunately, the API is not that difficult to use as long as you utilize the provided documentation and know the type of information you are looking for.
Here is a sample program that displays the SFLPAG value for subfile control record format MSG_CTL of display file VINING/FNDMSGSDF, which happens to be the display file and subfile used in the previous "Monocasing Character Data" article.
h dftactgrp(*no)
dSFLPagSiz pr 10i 0
d QualDspF 20 const
d RcdFmtName 10 const
dRcdFmt s 10 inz('MSG_CTL')
dPagSiz s 10i 0
dMsg s 50
dWait s 1
dQualDspF ds
d DspFName 10 inz('FNDMSGSDF')
d DspFLib 10 inz('VINING')
/free
PagSiz = SFLPagSiz(QualDspF :RcdFmt);
select;
when PagSiz > 0;
Msg = 'The subfile page size for ' + %trimr(RcdFmt) +
' is ' + %char(PagSiz);
when PagSiz = -1;
Msg = 'Record format ' + %trimr(RcdFmt) + ' not found';
when PagSiz = -2;
Msg = 'Record format ' + %trimr(RcdFmt) +
' is not SFLCTL';
other;
Msg = 'Unexpected return value ' + %char(PagSiz) +
' encountered';
endsl;
dsply Msg ' ' Wait;
*inlr = *on;
return;
/end-free
pSFLPagSiz b
dSFLPagSiz pi 10i 0
d QualDspF 20 const
d RcdFmtName 10 const
dRtvDspFD pr extpgm('QDFRTVFD')
d RcvVar 1 options(*varsize)
d LenRcvVar 10i 0 const
d Format 8 const
d QualDspF 20 const
d ErrCde likeds(QUSEC)
dX s 10i 0
dRtnValue s 10i 0
dBasePtr s *
dBaseInf ds likeds(QDFFBASE)
d based(BasePtr)
dFileInfPtr s *
dFileInf ds likeds(QDFFINFO)
d based(FileInfPtr)
dRcdFmtTblPtr s *
dRcdFmtTblInf ds likeds(QDFARFTE)
d based(RcdFmtTblPtr)
dRcdFmtPtr s *
dRcdFmtInf ds likeds(QDFFRINF)
d based(RcdFmtPtr)
dSFLCtlPtr s *
dSFLCtlRcd ds likeds(QDFFSFCR)
d based(SFLCtlPtr)
dSFLCtlEPtr s *
dSFLCtlEnt ds likeds(QDFFSFHR)
d based(SFLCtlEPtr)
dInlRcvVar ds qualified
d BytRtn 10i 0
d BytAvl 10i 0
dSFLCtlFmt c x'20'
/copy qsysinc/qrpglesrc,qdfrtvfd
/copy qsysinc/qrpglesrc,qusec
/free
QUSBPrv = 0;
RtvDspFD(InlRcvVar :%size(InlRcvVar) :'DSPF0100' :QualDspF :QUSEC);
BasePtr = %alloc(InlRcvVar.BytAvl);
RtvDspFD(BaseInf :InlRcvVar.BytAvl :'DSPF0100' :QualDspF :QUSEC);
FileInfPtr = BasePtr + BaseInf.QDFFINOF;
RcdFmtTblPtr = FileInfPtr + FileInf.QDFFDFLO;
for X = 1 to BaseInf.QDFFFRCS;
if RcdFmtTblInf.QDFARFNM = RcdFmtName;
leave;
else;
RcdFmtTblPtr += %size(RcdFmtTblInf);
endif;
endfor;
if X <= BaseInf.QDFFFRCS;
RcdFmtPtr = FileInfPtr + RcdFmtTblInf.QDFARFOF;
if %bitand(%subst(RcdFmtInf.QDFBITS09 :1 :1) :SFLCtlFmt) =
SFLCtlFmt;
SFLCtlPtr = RcdFmtPtr + RcdFmtInf.QDFFRAOF;
SFLCtlEPtr = SFLCtlPtr + %size(SFLCtlRcd);
RtnValue = SFLCtlEnt.QDFFSFPG;
else;
RtnValue = -2;
endif;
else;
RtnValue = -1;
endif;
dealloc BasePtr;
return Rtnvalue;
/end-free
pSFLPagSiz e
The program starts by calling the procedure SFLPagSiz, passing two parameters: the qualified name of a display file and the name of a subfile control record format. The SFLPagSiz procedure defines a return value where a positive value represents the size of the subfile page and a negative value an error condition. The two defined errors are -1 for a record format name that is not found in the display file and -2 for a record format that is not a subfile control record. More severe errors, such as the display file itself not being found, would result in an escape message being sent as explained in the following paragraph.
The SFLPagSiz procedure first sets the API error code structure Bytes provided field to 0, indicating that errors encountered by an API are to be returned as escape messages. The procedure then calls the QDFRTVFD API twice. As display files can contain many record formats, with each record format defining many possible fields and conditions, it's difficult to guess how much information about any given display file is available. The first call to the API is simply to determine how large the display file description is. For this reason, a receiver variable (InlRcvVar) of minimal size is used for the first call. This receiver variable is 8 bytes in size, just large enough to access the size of the *DSPF (variable QDFFSIZE, which I refer to as InlRcvVar.BytAvl in the program), which is provided in bytes 5 through 8 as seen in the QSYSINC definition of the QDFFBASE structure below.
DQDFFBASE DS
D* Base File Section (QDFFBASE)
D* Base file structure. This is
D* the first structure and is
D* located at offset zero of the
D* returned data.
D QDFFRETN 1 4B 0
D* Length of the returned data.
D QDFFSIZE 5 8B 0
D* Size of the display file
D* description.
Having initially called QDFRTVFD to obtain the size of the display file information, the program then allocates the necessary amount of storage using the %alloc built-in and receives back a return value pointer to the allocated storage. This pointer, BasePtr, is defined as the basing pointer for the structure BaseInf, with BaseInf being defined like QDFFBASE as shown below. The program now calls QDFRTVFD the second time, this time passing a receiver variable InlRcvVar.BytAvl bytes in size, which is large enough to receive back all information related to the display file.
dBasePtr s *
dBaseInf ds likeds(QDFFBASE)
d based(BasePtr)
I will point out that the sample program is making a simplifying assumption with this second call to the QDFRTVFD API—namely, that no one is recreating the display file between the first and second call to QDFRTVFD. If the display file is recreated with more record formats and/or more fields than existed for the first call to the API, then the second call may still not receive all of the display file information. If this is a concern, then the program can replace the second call to QDFRTVFD with a do loop, which is exited when the returned BaseInf.QDFFSIZE value is less than or equal to the returned BaseInf.QDFFRETN value. Within the do loop, the program could use %realloc to reallocate the storage associated with BasePtr to the most recent BaseInf.QDFFSIZE value and call QDFRTVFD again with this reallocated storage as the receiver variable.
Having received all of the display file information that is available, it's now time to find just what the SFLPAG size value is. Most system APIs return fewer than three structures within the API receiver variable, and how to navigate across the returned structures is generally pretty obvious. Navigating most of the structures returned by the QDFRTVFD API is, on the other hand, anything but obvious. But as mentioned previously, the API documentation can certainly make your life easier in this regard, especially if you know what you want.
In our case, we're looking for the SFLPAG value of a subfile control record. So if we go to the API documentation found here and do a find (Ctrl+F) on the string SFLPAG, we will be brought to a page looking like the following.
Figure 1: This is the Subfile Control Entry documentation. (Click image to enlarge.)
As seen from the preceding screen shot, a subfile page value can be found at offset 2 of the subfile control entry structure QDFFSFHR where there can be one or more occurrences (the ARRAY(*) reference) of the structure, depending on how many subfile record formats are defined within the display file. Looking at the QSYSINC/QRPGLESRC include member QDFRTVFD, which is copied into the SFLPagSize procedure using /copy, we find the structure QDFFSFHR and the field QDFFSFPG defined. Note that the documented API structure and field names won't always be the same as the QSYSINC names; for instance, WDFFSFPG has been renamed in QSYSINC to QDFFSFPG, but searching on the structure (or format) name will generally get you to the right location in order to determine the appropriate QSYSINC names. Within the sample program, the structure SFLCtlEnt is defined as LIKEDS(QDFFSFHR) and BASED(SFLCtlEPtr) as shown below.
dSFLCtlEPtr s *
dSFLCtlEnt ds likeds(QDFFSFHR)
d based(SFLCtlEPtr)
The API documentation shows that the QDFFSFHR structure is defined at variable WDFFSFPM of structure QDFFSFCR, so, assuming for the moment that we can get to QDFFSFCR, access to QDFFSFHR can be accomplished by adding the offset of WDFFSFPM to the starting address of QDFFSFCR. Looking at the documentation for QDFFSFCR, we find that it can be accessed by adding the displacement WDFFRAOF found in structure QDFFRINF to the starting address of QDFFRINF; looking at the documentation for QDFFRINF, we find that it can be accessed by adding the displacement WDFARFOF found in structure QDFARFTE to the starting address of QDFARFTE; looking at the documentation for QDFARFTE, we find that it can be accessed by adding the displacement WDFFDFLO found in structure QDFFINFO to the starting address of QDFFINFO; looking at the documentation for QDFFINFO, we find that it can be accessed by adding displacement WDFFINOF in structure QDFFBASE to the starting address of QDFFBASE; and looking at the documentation for QDFFBASE, we find that this is the first structure returned in the receiver variable of the QDFRTVFD API, which has a starting address value represented by the pointer variable BasePtr.
In the same manner as QDFFSFHR, all of the API structures referenced in the previous paragraph are defined within the sample program using LIKEDS and BASED. This use of pointers and based variables is not required in order to use the QDFRTVFD API, but is certainly easier (for me anyway) than using an approach such as %subst. With the previous paragraph as a "roadmap" to aid in our navigating of the various QDFRTVFD structures, the following bolded code demonstrates the use of the roadmap (where we reverse the previously listed order as we're starting from QDFFBASE rather than QDFFSFHR).
FileInfPtr = BasePtr + BaseInf.QDFFINOF;
RcdFmtTblPtr = FileInfPtr + FileInf.QDFFDFLO;
for X = 1 to BaseInf.QDFFFRCS;
if RcdFmtTblInf.QDFARFNM = RcdFmtName;
leave;
else;
RcdFmtTblPtr += %size(RcdFmtTblInf);
endif;
endfor;
if X <= BaseInf.QDFFFRCS;
RcdFmtPtr = FileInfPtr + RcdFmtTblInf.QDFARFOF;
if %bitand(%subst(RcdFmtInf.QDFBITS09 :1 :1) :SFLCtlFmt) =
SFLCtlFmt;
SFLCtlPtr = RcdFmtPtr + RcdFmtInf.QDFFRAOF;
SFLCtlEPtr = SFLCtlPtr + %size(SFLCtlRcd);
RtnValue = SFLCtlEnt.QDFFSFPG;
You might notice that there are a few stops along the road. The "for X = 1 to BaseInf.QDFFFRCS" loop is needed as there can be multiple record formats defined within a display file. This loop allows us to find the record format of interest, where QDFFFRCS is the number of record formats defined in the display file and QDFARFNM is the name of a record format found in the display file. If this FOR loop is exited with X being greater than QDFFFRCS, then the searched-for record format name (RcdFmtName) is not defined within the display file and the variable RtnValue is set to -1, representing an error condition.
The second stop along the road is to verify that the record format found in the display file is in fact a subfile control record. This is accomplished by the check "if %bitand(%subst(RcdFmtInf.QDFBITS09 :1 :1) :SFLCtlFmt) = SFLCtlFmt." Per the documentation for variable WDFFRFLG of structure QDFFRINF, the third bit of WDFFRFLG is "on" when the record format contains the SFLCTL keyword. You might notice that WDFFRFLG is a case where the QSYSINC name, QDFBITS09, is quite different from the documentation name of WDFFRFLG. Resolving this naming difference, however, is straightforward.
At this point, the procedure sets the RtnValue variable to the SFLPAG value 12 (which is located at SFLCtlEnt.QDFFSFPG), deallocates the previously allocated storage for the API receiver variable (which is why SFLCtlEnt.QDFFSFPG was copied to RtnValue as SFLCtlEnt is Based on the previously allocated—and now deallocated—BasePtr addressed storage), and returns the RtnValue value to the calling procedure. The caller then displays either the SFLPag size or some appropriate error text and ends. So, rather than hard-coding a subfile page size as I did in the earlier article with…
dSizeOfSFLPage s 5u 0 inz(12)
…I could have dynamically accessed the SFLPAG value using the SFLPagSiz procedure, avoiding program maintenance with respect to the subfile page size.
At this point, I do want to point out that I am not recommending that developers stop hard-coding SFLPAG values within their applications and start using a procedure such as SFLPagSiz. For the vast majority of the application software I write, I will choose to continue to hard-code such values, and most likely you should do likewise. But when developing tools to aid you in the running of your system, keep in mind that an API such as QDFRTVFD is available and can, at runtime, be used quite effectively in determining display file characteristics. The SFLPagSiz procedure provides you with one example of how you can navigate the wealth of information available to you and your applications.
As usual, if you have any API questions, send them to me at
as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1
LATEST COMMENTS
MC Press Online