Problem solve Get help with specific problems with your technologies, process and projects.

Alternative approach to remove invalid characters

Here's a version of "Remove invalid characters" that offers a number of advantages over the original.

1. It will work on any (*) database file, with any number of fields. The program will be invoked by a command, which will pass the qualified file name in the standard format of: file name in first 10 bytes, library name in second 10 bytes. This can easily be changed to accept separate parameters of file name and library name, if required.

2. The program will update only character fields, replacing each hex byte below x'40' (space) with a space. Obviously, we don't want to update packed or binary fields in this way! I also don't think it's advisable to update invalid bytes of a zoned field with zeroes.

3. A message of the form "n record(s) out of m updated in file x" will be sent to the user.

* Well, almost. The program-described file will not handle null fields. For these, we must use embedded SQL or the C record-oriented I/O functions.

RPG program, FIXHEX:

H Option(*SRCSTMT : *NODEBUGIO)                         
H Dftactgrp(*NO) Actgrp('FIX')                          
FFile      uf   f32766        disk    usropn            
FQadspffd  if   e             disk    usropn            
D Record          ds                                    
D  Inrec                         1    Dim(32766)        
D QualFile        s             20                      
D FileV           s             21    Varying           
D CmdStr          s            150                      
D CmdLen          s             15  5 Inz(%Size(CmdStr))
D IndDS           ds                  Based(@Ind)       
D  Ind                            n   Dim(%Elem(Inrec)) 
D I               s              5  0                   
D BadHex          s               n                     
D RcdUpd          s             10u 0                   
D RcdTot          s             10u 0                   
D                sds                                    
D User                  254    263                      
C     *Entry        Plist                               
C                   Parm                    QualFile    
C     Pcmd          Plist                                                 
C                   Parm                    CmdStr                        
C                   Parm                    CmdLen                        
C                   Eval      FileV = %Trimr(%Subst(QualFile : 11 : 10)) +
C                                     '/' +                               
C                                     %Trimr(%Subst(QualFile : 1 : 10))   
 * Generate DSPFFD outfile for received file.                             
C                   Eval      CmdStr = 'DSPFFD ' + FileV +                
C                                      ' *OUTFILE OUTFILE(QTEMP/QADSPFFD)'
C                   Call      'QCMDEXC'     Pcmd                          
C                   Eval      CmdStr = 'OVRDBF QADSPFFD QTEMP/QADSPFFD'   
C                   Call      'QCMDEXC'     Pcmd                          
C                   Open      Qadspffd                                    
 * Allocate and populate list of character field bytes in received file.  
C                   Read      Qadspffd                                    
C                   Alloc     Whrlen        @Ind                          
C                   Dow       Not %Eof(Qadspffd)                          
C                   If        Whfldt = 'A'                                
C                   Eval      %Subst(IndDS : Whibo : Whfldb) = *ON        
C                   Else                                                  
C                   Eval      %Subst(IndDS : Whibo : Whfldb) = *OFF             
C                   Endif                                                       
C                   Read      Qadspffd                                          
C                   Enddo                                                       
 * Override program-described file to received file.                            
C                   Eval      CmdStr = 'OVRDBF FILE ' + FileV                   
C                   Call      'QCMDEXC'     Pcmd                                
C                   Open      File                                              
 * Loop through received file, replacing each character byte below ' ' with ' '.
C                   Read      File          Record                              
C                   Dow       Not %Eof(File)                                    
C                   Eval      BadHex = *OFF                                     
C                   Eval      RcdTot = RcdTot + 1                               
 * Check each character byte in record.                                         
C                   For       I = 1 to Whrlen                                   
C                   If        Inrec(I) < ' ' and Ind(I)                         
C                   Eval      Inrec(I) = ' '                                    
C                   Eval      BadHex = *ON                                      
C                   Endif                                                       
C                   Endfor                                                 
 * Update record if any bad hex bytes found.                               
C                   If        BadHex                                       
C                   Update    File          Record                         
C                   Eval      RcdUpd = RcdUpd + 1                          
C                   Endif                                                  
C                   Read      File          Record                         
C                   Enddo                                                  
 * Tell user how many records were updated.                                
C                   Eval      CmdStr = 'SNDMSG TOUSR(' + %Trimr(User) +    
C                                      ') MSG(''FIXHEX: ' + %Char(RcdUpd) +
C                                      ' record(s) out of ' +              
C                                      %Char(RcdTot) +                     
C                                      ' updated in file ' + FileV + '.'')'
C                   Call      'QCMDEXC'     Pcmd                           
C                   Eval      *INLR = *ON 

Command, FIXHEX:
(A validation program could also be included, if desired.)

             CMD        PROMPT('Fix Invalid Hex Data')

             PARM       KWD(FILE) TYPE(QUALNAME) MIN(1) PROMPT('File')

             QUAL       TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL) +
                          (*CURLIB)) PROMPT('Library') 

Dig Deeper on iSeries CL programming