Difference between revisions of "Prefix String"

From MidrangeWiki
Jump to: navigation, search
Line 4: Line 4:
 
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to extract the left-most non-blank '''''n''''' characters from a supplied string.
 
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to extract the left-most non-blank '''''n''''' characters from a supplied string.
  
== Service Procedure ==
+
== Service Prototype ==
 +
Place the following in a separate copybook for inclusion in both the caller and the service program source members.
 
<pre>
 
<pre>
 
**free
 
**free
Line 17: Line 18:
 
   PadChar              char(1)  const    options(*nopass);
 
   PadChar              char(1)  const    options(*nopass);
 
end-pr;
 
end-pr;
 +
</pre>
 +
 +
== Service Procedure ==
 +
Place the following in a service program source member.
 +
<pre>
 +
**free
  
 
//==============================================================================
 
//==============================================================================

Revision as of 16:20, 14 December 2018

Summary

The following are the RPG/LE fully free-form definitions and instructions needed for using the Prefix String service procedure. This service procedure simply allows the caller to extract the left-most non-blank n characters from a supplied string.

Service Prototype

Place the following in a separate copybook for inclusion in both the caller and the service program source members.

**free

//==============================================================================
// After removing blanks on the left, return requested number of bytes of
// prefix characters -- optional padding on the right.
//==============================================================================
dcl-pr GenUtl_PrefixString        varchar(256) rtnparm;
  OrigString            varchar(65535) const;
  ByteCount             packed(3:0) const;
  PadChar               char(1)   const     options(*nopass);
end-pr;

Service Procedure

Place the following in a service program source member.

**free

//==============================================================================
// After removing blanks on the left, return requested number of bytes of
// prefix characters -- optional padding on the right.
//==============================================================================
dcl-proc GenUtl_PrefixString      export;
  dcl-pi *n             varchar(256) rtnparm;
    OrigString          varchar(65535) const;
    ByteCount           packed(3:0) const;
    PadChar             char(1)   const     options(*nopass);
  end-pi;

  dcl-s StrLeng         like(ByteCount);
  dcl-s NewString       varchar(256) inz('');
  dcl-s PadString       char(256) inz(*blanks);

  NewString = %triml(OrigString);      // trim blanks before determining prefix
  StrLeng = ByteCount;                      // start with bytes requested
  if %parms < %parmnum(PadChar)             // if no padding and
  or StrLeng > %len(NewString);             // request greater than string supplied
    StrLeng = %len(NewString);              // take supplied string length
  endif;

  if StrLeng > *zero;                  // if something to return
    if StrLeng > %len(PadString);           // if more than what can return
      StrLeng = %len(PadString);            // set to max return length
    endif;
    if %parms < %parmnum(PadChar);          // if no padding
      NewString = %subst(OrigString: 1: StrLeng);
    else;                                   // else, with left padding
      NewString = %subst(OrigString + %xlate(' ': PadChar: PadString)
                        : 1: StrLeng );
    endif;
  else;                                // else
    return '';                         // nothing to return
  endif;

  return NewString;                    // return the suffix string
end-proc;