Find a character pattern within a variable.
Last month, in "A CL Command to Scan for Characters," we built the command Scan Characters (SCNCHR). The SCNCHR command returns the position of the first character within a CL variable that is found in a list of search-for values. This month, we'll look at how to provide a Scan String (SCNSTR) command.
SCNSTR will scan a CL character variable to determine if a given pattern of characters, such as the pattern 'bag' is contained within the variable. If the pattern is found, the command will return the position of the first byte of the pattern within the CL variable; otherwise, a value of 0 will be returned. For instance, scanning for the pattern 'bag' within a variable set to the value of 'a cabbage' would return a value of 6, indicating that the pattern 'bag' is found starting at the sixth byte of the CL variable. This is in contrast to last month's SCNCHR command, where a scan of characters 'bag' within a CL variable would return a value of 1, indicating that the first character (the 'a') of 'a cabbage' is found within the list of searched-for values ('b', 'a', and 'g'). If you're familiar with the RPG %scan built-in, you'll notice that the SCNSTR command performs a very similar function.
Some of you may already be familiar with the system API that will be used to implement the SCNSTR command. It's the Scan for String Pattern (QCLSCAN) API, which is one of the few APIs that were available all the way back in the initial release of the AS/400 operating system OS/400. Its age however does not, in general, detract from its usefulness, and providing a command interface to QCLSCAN is likely to simplify the development efforts of your CL-based programming staff. You may note that I used the qualifier "in general" when referring to the age of the API. There are in fact some API restrictions—for instance, a maximum CL variable length of 999 characters to scan, which could prove to be a limitation when working with larger CL variables. With that in mind, next month we'll extend the capabilities of the SCNSTR command processing program (CPP) beyond some of the limitations found in the underlying API (though the SCNSTR command will still be limited to a maximum character variable length of 5000 bytes due to a limitation of the PARM command used when defining character-based command parameters).
The SCNSTR Command
As we did last month, we'll start by providing a command definition for the SCNSTR command. Note that this version of the command defines the parameter to scan (keyword Base) with a maximum length of 999 bytes to match the QCLSCAN API limitation. As mentioned previously, a future version of SCNSTR will support a maximum length of 5000 bytes.
Cmd Prompt('Scan for String')
Parm Kwd(ScanFor) Type(*Char) Len(999) +
Min(1) Vary(*Yes *Int4) +
Prompt('Char pattern to search for')
Parm Kwd(Base) Type(*Char) Len(999) +
Min(1) Vary(*Yes *Int4) +
Prompt('String to be scanned')
Parm Kwd(Pos) Type(*UInt4) +
Min(1) RtnVal(*Yes) +
Prompt('Position of matching string')
Parm Kwd(StrPos) Type(*UInt4) +
Dft(1) Rel(*GT 0) +
Prompt('Starting pos. within Base')
Assuming that the previous command source is stored in member SCNSTR of source file QCMDSRC, you can create SCNSTR with the following command:
CrtCmd Cmd(ScnStr) Pgm(ScnStrCPP) Allow(*BPgm *IPgm)
This command indicates that the CPP of SCNSTR is program SCNSTRCPP and that the command can be run within a program. As with the previous CHKCHR and SCNCHR commands, you will not be able to run the SCNSTR command interactively from a command line due to the Pos parameter being a return value.
Below is the source for our CPP ScnStrCPP.
The SCNSTRCPP Command Processing Program
Pgm Parm(&ScanFor_In &Base_In &Pos &StrPos)
Dcl Var(&ScanFor_In) Type(*Char) Len(1003)
Dcl Var(&ScanForLen) Type(*Int) Len(4) +
Stg(*Defined) DefVar(&ScanFor_In)
Dcl Var(&ScanFor) Type(*Char) Len(999) +
Stg(*Defined) DefVar(&ScanFor_In 5)
Dcl Var(&Base_In) Type(*Char) Len(1003)
Dcl Var(&BaseLen) Type(*Int) Len(4) +
Stg(*Defined) DefVar(&Base_In)
Dcl Var(&Base) Type(*Char) Len(999) +
Stg(*Defined) DefVar(&Base_In 5)
Dcl Var(&Pos) Type(*UInt)
Dcl Var(&StrPos) Type(*UInt)
Dcl Var(&AdjBaseLen) Type(*Dec) Len(3 0)
Dcl Var(&AdjScnFLen) Type(*Dec) Len(3 0)
Dcl Var(&AdjStrPos) Type(*Dec) Len(3 0)
Dcl Var(&AdjPos) Type(*Dec) Len(3 0)
Dcl Var(&MsgDta) Type(*Char) Len(8)
Dcl Var(&MsgDta1) Type(*UInt) +
Stg(*Defined) DefVar(&MsgDta 1)
Dcl Var(&MsgDta2) Type(*UInt) +
Stg(*Defined) DefVar(&MsgDta 5)
ChgVar Var(&AdjScnFLen) Value(&ScanForLen)
ChgVar Var(&AdjBaseLen) Value(&BaseLen)
ChgVar Var(&AdjStrPos) Value(&StrPos)
Call Pgm(QCLSCAN) Parm(&Base &AdjBaseLen &AdjStrPos +
&ScanFor &AdjScnFLen ' ' ' ' ' ' &AdjPos)
Select
When Cond(&AdjPos = -1) Then(Do)
ChgVar Var(&MsgDta1) Value(&ScanForLen)
ChgVar Var(&MsgDta2) Value(&BaseLen)
SndPgmMsg MsgID(ESC0004) MsgF(OurMsgs) +
MsgDta(&MsgDta) MsgType(*Escape)
EndDo
When Cond(&AdjPos = -5) Then(Do)
ChgVar Var(&MsgDta1) Value(&StrPos)
ChgVar Var(&MsgDta2) Value(&BaseLen)
SndPgmMsg MsgID(ESC0003) MsgF(OurMsgs) +
MsgDta(&MsgDta) MsgType(*Escape)
EndDo
Otherwise +
Cmd(ChgVar Var(&Pos) Value(&AdjPos))
EndSelect
EndPgm
Assuming that the preceding source is stored in member SCNSTRCPP of source file QCLSRC, you can use the following command to create the SCNSTRCPP program:
CrtBndCL Pgm(ScnStrCPP)
In an error situation, the SCNSTRCPP program will send one of two escape messages. The first escape message, ESC0003—STRPOS value of &1 is longer than the length of BASE value (&2) —was created in last month's article, "A CL Command to Scan for Characters," in support of the SCNCHR command. This message is re-used by SCNSTRCPP. The second message, ESC0004—SCANFOR length of &1 is longer than BASE length of &2—is new and needs to be created. To add this message to the message file OURMSGS (a message file we've used in past articles), you can use the following command.
AddMsgD MsgID(ESC0004) MsgF(OurMsgs) +
Msg('SCANFOR length of &1 is longer than BASE length of &2') +
Fmt((*UBin 4) (*UBin 4))
How SCNSTRCPP Works
As with the previous articles, if you are not interested in how the SCNSTR command works, you can skip to the topic "Testing the SCNSTR command."
Before looking at the flow of SCNSTRCPP, we'll first get familiar with the QCLSCAN API. QCLSCAN defines nine parameters. The first eight parameters are inputs that you provide to the API. The ninth parameter is an output from the API that identifies either where the SCANFOR pattern was located within the BASE searched string or the type of error that was encountered by the API.
The first parameter is a variable-length character field that contains the string to be scanned. This string can have a length from 1 through 999 bytes.
The second parameter defines the length of the character string found in the first parameter. Unlike most system APIs, where length information is provided as 4-byte integer values, QCLSCAN defines this parameter as packed decimal with a length of three digits and zero decimal positions. The use of this definition is due to the API predating current system API standards.
The third parameter defines the position, within the character string found in the first parameter, where the scan is to start. This value, like the second parameter, is defined a packed decimal with a length of 3,0. The value of this parameter must be greater than 0 and not greater than the value of the second parameter (the length of the character string to scan).
The fourth parameter is a variable-length character field that contains the string, or pattern, that is to be scanned for. This string can have a length from 1 through 999 bytes.
The fifth parameter defines the length of the character string being scanned for. This value is defined as packed decimal with a length of 3,0. The value must be greater than 0 and not greater than the effective length of the string to scan. Note the qualifier "effective length" in the previous sentence. If the second parameter indicates that the string to scan is 100 bytes but the third parameter specifies that the scan is to start at the 97th byte, then the effective length of the string is only four bytes (character positions 97 through 100). Specifying a scanned-for length of five bytes when calling the API would be an error situation.
The sixth parameter indicates whether the API should convert any lowercase characters found in the first parameter to uppercase prior to scanning for the string found in the fourth parameter. The parameter is defined as a 1-byte character field where a value of '1' indicates that the string found in parameter 1 should be uppercased. Any other input value indicates that no uppercasing is to be performed. This uppercasing capability of the API is not used by the program SCNSTRCPP.
The seventh parameter indicates whether the API should trim trailing blanks from the end of the character string found in the fourth parameter. The parameter is defined as a 1-byte character field where a value of '1' indicates that trailing blanks are to be trimmed. Any other input value indicates that no trimming is to be performed. This trimming capability of the API is not used by the program SCNSTRCPP, in part due to the command analyzer already providing the trimmed length of the fourth parameter in variable &ScanForLen.
The eighth parameter indicates whether the API should treat any character in certain positions of the fourth parameter as matches. The parameter is defined as a 1-byte character field where any non-blank value represents a wildcard position. That is, a value such as '*' for the eight parameter, along with a fourth parameter scan-for value of 'b*g', indicates that the character 'b', followed by any character, and then followed by the character 'g' should be treated as a match when scanning the first parameter.
The ninth parameter is the sole return value from the API. The parameter is defined as a packed decimal field with a length of 3,0. If the returned value is 0, then no match was found in the first parameter (from the specified starting position, parameter three, for the scan-for value specified by the fourth parameter). If the returned value is greater than 0, then a match was found in the first parameter, with the returned value representing where the first character of the scanned-for value can be found within the first parameter. Note that this positive value is relative to the full length of the first parameter and not based on the starting relative location (the value of the third parameter). If the returned value is negative, then the API detected an error situation.
When the API detects an error, one of five possible negative values can be returned: negative one through negative five. Because the SCNSTRCPP program is not using all of the capabilities of QCLSCAN (no uppercasing, no checking for trailing blanks, and no wildcard scan character), only two of these five errors might be returned to our CPP. The two errors that could be returned are negative one (the scan-for pattern is longer than the string to scan) and negative five (the starting position is not valid).
With that review of the API out of the way, let's now look at the flow of the SCNSTRCPP program, which isn't much!
- As the API expects the numeric inputs for parameters 2, 3, and 5 (&AdjBaseLen, &AdjStrPos, and &AdjScnFLen, respectively) to be three-digit packed decimal values, SCNSTRCPP uses CHGVAR commands to convert the integer parameter values passed to the program (&BaseLen, &StrPos, and &ScanForLen) to packed decimal.
- he QCLSCAN API is called.
- The returned position value (&AdjPos) is examined by a Select group to send an escape message if an error occurred or return the position within the scanned string where the scanned-for pattern was found (with a value of zero indicating that the scanned-for string was not found).
Rather than sending escape messages when the API encountered an error, SCNSTRCPP could have returned the API-returned position value and left the checking for negative values (errors) to the user of the SCNSTR command. This would have allowed us to simply code the CHGVAR command found in the OTHERWISE command of the SELECT group and not have a SELECT group at all (greatly reducing the amount of executable code we wrote). Most users of CL commands, however, expect to monitor for errors by using the MONMSG command, not by examining command return values. So in keeping with normal command behavior, SCNSTRCPP uses escape messages to inform the users of error situations.
Testing the SCNSTR Command
In keeping with our last few articles, here is a test program for the SCNSTR command.
Pgm Parm(&Base_In &ScanFor_In &StrPos_In)
Dcl Var(&Base_In) Type(*Char) Len(32)
Dcl Var(&ScanFor_In) Type(*Char) Len(32)
Dcl Var(&StrPos_In) Type(*Dec)
Dcl Var(&Pos) Type(*UInt)
Dcl Var(&Pos_Char) Type(*Char) Len(5)
ScnStr ScanFor(&ScanFor_In) Base(&Base_In) +
Pos(&Pos) StrPos(&StrPos_In)
MonMsg MsgID(ESC0003) Exec(Do)
SndPgmMsg Msg('Invalid starting position') +
ToPgmQ(*Ext)
Return
EndDo
MonMsg MsgID(ESC0004) Exec(Do)
SndPgmMsg Msg('SCANFOR longer than BASE') +
ToPgmQ(*Ext)
Return
EndDo
If Cond(&Pos *NE 0) Then(Do)
ChgVar Var(&Pos_Char) Value(&Pos)
SndPgmMsg Msg('String found at ' *Cat +
&Pos_Char) +
ToPgmQ(*Ext)
EndDo
Else Cmd(SndPgmMsg Msg('String not found') +
ToPgmQ(*Ext))
EndPgm
Assuming that the preceding source is stored in member USESCNSTR of source file QCLSRC, then you can use the following command to create the USESCNSTR program:
CrtBndCL Pgm(UseScnStr)
From the command line, we can now test a few scenarios.
Entering the command Call UseScnStr ('a cabbage' 'bag' 1) will result in the message 'String found at 00006'.
Entering the command Call UseScnStr ('a cabbage' 'bag' 3) will also result in the message 'String found at 00006' as the returned position is relative to the start of the first parameter and not the starting position specified by the third parameter.
Entering the command Call UseScnStr ('a cabbage' 'tag' 1) will result in the message 'String not found'.
Entering the command Call UseScnStr ('a cabbage' '' 1), where the second parameter is two contiguous apostrophes, will result in the message 'String found at 00002' as the command analyzer will treat two consecutive apostrophes as representing a single blank and a blank is found in the second position of the string 'a cabbage'.
Entering the command Call UseScnStr ('a cabbage' 'bag' 15) will result in the message 'Invalid starting position' as the first parameter is only nine bytes in length, so starting at position 15 is invalid.
Entering the command Call UseScnStr ('a cabbage' 'bag' 8) will result in the message 'SCANFOR longer than BASE' as the second parameter ('bag') is three bytes in length, but only two bytes of the first parameter, the eighth and ninth bytes, are to be scanned due to the starting position of eight and a total string length of nine for the first parameter.
Entering the command Call UseScnStr ('a cabbage' 'bag' 7) will result in the message 'String not found' as 'bag' is only found starting at position six, which is prior to the specified starting position of seven.
Entering the command Call UseScnStr ('a cabbage' 'age' 7) will result in the message 'String found at 00007'.
Next month, we'll look at some enhancements that can be made to our SCNSTR command, but for now SCNSTR rather closely mimics the behavior of the RPG built-in %scan.
More CL Questions?
Wondering how to accomplish a function in CL? Send your CL-related questions to me at
LATEST COMMENTS
MC Press Online