Writing varying(2) and varying(4) character data to the IFS stream file in RPGLE

Writing varying(2) and varying(4) character data to the IFS stream file in RPGLE.
Writing varying(2) and varying(4) character data to the IFS stream file in RPGLE, varying, varying(2),varying(4), writing varying fields to the ifs stream file using rpgle, varying fields in as400, varyig fields in ibmi, as400, ibmi, iseries, systemi, as400andsqltricks, as400 tutorials, ibmi tutorials
Writing varying(2) and varying(4) character data to the IFS stream file in RPGLE

Varying(2) and varying(4) are default values which are pre-assumed by the system, if we do not provide 2 and 4 it is considered by the system itself depending upon the length of the character data.

Differentiate between varying(2) and varying (4) character data in RPGLE

Varying keyword used while defining character variable to denote the variable is a varying length variable not a fixed format. Let's take an example to understand.

Varying(2) character fields

Difsdata          s             20a   inz('TEST')

Here, we defined standalone character field ifsdata of fixed length 20. This field will always have the length of 20 irrespective of the data it contains.

Difsdata          s             20a   inz('TEST') varying

or

Difsdata          s             20a   inz('TEST') varying(2)

Here, we defined standalone character field ifsdata of varying length 20. This field will have the length as per the data contained in it. Therefore, this field has actual length 4. We can either defined varying or varying(2) both are acceptable. If we only defined Varying the compiler assumed it to varying(2) if the variable length is from 1 to 65535.

The two bytes appended to this varying(2) length field at the start that contains the length of this field as per the length of the data contained within it.

Varying(4) character fields

Difsdata          s         123520a   inz('TEST')

Here, we defined standalone character field ifsdata of fixed length 123520. This field will always have the length of 123520a irrespective of the data it contains.

Difsdata          s         123520a   inz('TEST') varying

or

Difsdata          s         123520a   inz('TEST') varying(4)

Here, we defined standalone character field ifsdata of varying length 123520. This field will have the length as per the data contained in it. Therefore, this field has actual length 4. We can either defined varying or varying(4) both are acceptable. If we only defined Varying the compiler assumed it to varying(4) if the variable length is from greater than 65535.

The four bytes appended to this varying(4) length field at the start that contains the length of this field as per the length of the data contained within it.

Write char Varying(2) data to IFS in RPGLE

Here we are referring to the same program mentioned in Handling Errors while working with IFS stream file using RPGLE. We will only change the definition of the field ifsdata and write() procedure call. I am not writing all the Fixed and Fully free code here that you can refer from the link mentioned above. I am highlighting the change which i am doing to the code which is mentioned and explained in above link.

 D errorifs        PR              *   ExtProc('__errno')                                           
                                                                                                    
 D strerror        PR              *   ExtProc('strerror')                                          
 D error_num                     10I 0 value                                                        
                                                                                                    
                                                                                                    
 D close           PR            10i 0 extproc('close')                     *                       
 D fileds                        10i 0 value                                *file descriptor        
                                                                                                    
 D write           PR            10i 0 extproc('write')                     * 32 bit, no. of byt    
 D fileds                        10i 0 value                                *file descriptor        
 D buffer                          *   value                                * pointer to byte       
 D noofbytes                     10U 0 value                                * 32 bits               
                                                                                                    
 D open            PR            10I 0 extproc('open')                                              
 D ifspath                         *   value options(*string)               *ifs path               
 D oflag                         10I 0 value                                *string of 32 bits      
 D mode                          10U 0 value options(*nopass)               * 9 bits                
 D codepage                      10U 0 value options(*nopass)               *                       
                                                                                                    
  * <-----oflag---->                                                                                
 D O_readonly      C                   1                                                            
 D O_writeonly     C                   2                                                            
 D O_readwrite     C                   4                                                            
 D O_createfileifnotexist...                                                                        
 D                 C                   8                                                            
 D O_exclusivecreate...                                                                             
 D                 C                   16                                                           
 D O_truncateto0bytes...                                                                            
 D                 C                   64                                                           
 D O_appendtofile  C                   256                                                          
 D O_converttextbycodepage...                                                                       
 D                 C                   8388608                                                      
 D O_openintextmode...                                                                              
 D                 C                   16777216                                                     
  *                                                                                                 
  * <-----mode---->                                                                                 
  * owner,group,other (RWX)                                                                         
  *                                         owner authority                                         
 D M_readowner     C                   256                                                          
 D M_writeowner    C                   128                                                          
 D M_executeowner  C                   64                                                           
  *                                         group authority                                         
 D M_readgroup     C                   32                                                           
 D M_writegroup    C                   16                                                           
 D M_executegroup  C                   8                                                            
  *                                         other people                                            
 D M_readother     C                   4                                                            
 D M_writeother    C                   2                                                            
 D M_executeother  C                   1                                                            
  *                                                                                                 
 Difspath          s            512a                                                                
 Doflag            s             10I 0                                                              
 Dmode             s             10U 0                                                              
 Dcodepage         s             10U 0                                                              
 Dfiledescriptor   s             10i 0                                                              
 Difsdata          s          65535a   inz varying(2)                                               
 Dreturn_write     s             10i 0 inz                                                          
 Dreturn_close     s             10i 0 inz                                                          
                                                                                                    
                                                                                                    
 Derror_ptr        S               *                                                                
 Derror_num        S             10I 0 based(error_ptr)                                             
                                                                                                    
 Derrormsg_ptr     S               *                                                                
 Derror_msg        S            500a   based(errormsg_ptr)                                          
                                                                                                    
  /free                                                                                             
   ifspath = '/home/easyclass/helloworld';                                                          
   oflag = O_writeonly +                                                                            
           O_createfileifnotexist;                                                                  
   mode =                                                                                           
          M_readowner +                                                                             
          M_writeowner +                                                                            
          M_executeowner;                                                                           
                                                                                                    
   filedescriptor = open(%trim(ifspath):                                                            
                         oflag:                                                                     
                         mode);                                                                     
   if filedescriptor < 0;                                                                           
     error_ptr = errorIFS();                                                                        
     errormsg_ptr = strerror(error_num);                                                            
     return;                                                                                        
   endif;                                                                                           
                                                                                                    
   ifsdata = 'HELLO WORLD';                
   // explanation of below highlighted
   // we are passing address of ifsdata variable and adding 2 bytes to the address i.e. 
// start reading by shifting right to the 2 bytes from the start
// of the address since first 2 bytes contained length of the variable ifsdata.
return_write = write(filedescriptor:%addr(ifsdata)+2:%size(ifsdata)); if return_write < %size(ifsdata); error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); return; endif; return_close = close(filedescriptor); if return_close = -1; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); return; endif; *inlr = *on; return; /end-free

Write char Varying(4) data to IFS in RPGLE

Here we are referring to the same program mentioned in Handling Errors while working with IFS stream file using RPGLE. We will only change the definition of the field ifsdata and write() procedure call. I am not writing all the Fixed and Fully free code here that you can refer from the link mentioned above. I am highlighting the change which i am doing to the code which is mentioned and explained in above link.

  * varying(2) --> 1 -65535                                                                         
  * varying(4) --> >65535                                                                           
                                                                                                    
 D errorifs        PR              *   ExtProc('__errno')                                           
                                                                                                    
 D strerror        PR              *   ExtProc('strerror')                                          
 D error_num                     10I 0 value                                                        
                                                                                                    
                                                                                                    
 D close           PR            10i 0 extproc('close')                     *                       
 D fileds                        10i 0 value                                *file descriptor        
                                                                                                    
 D write           PR            10i 0 extproc('write')                     * 32 bit, no. of byt    
 D fileds                        10i 0 value                                *file descriptor        
 D buffer                          *   value                                * pointer to byte       
 D noofbytes                     10U 0 value                                * 32 bits               
                                                                                                    
 D open            PR            10I 0 extproc('open')                                              
 D ifspath                         *   value options(*string)               *ifs path               
 D oflag                         10I 0 value                                *string of 32 bits      
 D mode                          10U 0 value options(*nopass)               * 9 bits                
 D codepage                      10U 0 value options(*nopass)               *                       
                                                                                                    
  * <-----oflag---->                                                                                
 D O_readonly      C                   1                                                            
 D O_writeonly     C                   2                                                            
 D O_readwrite     C                   4                                                            
 D O_createfileifnotexist...                                                                        
 D                 C                   8                                                            
 D O_exclusivecreate...                                                                             
 D                 C                   16                                                           
 D O_truncateto0bytes...                                                                            
 D                 C                   64                                                           
 D O_appendtofile  C                   256                                                          
 D O_converttextbycodepage...                                                                       
 D                 C                   8388608                                                      
 D O_openintextmode...                                                                              
 D                 C                   16777216                                                     
  *                                                                                                 
  * <-----mode---->                                                                                 
  * owner,group,other (RWX)                                                                         
  *                                         owner authority                                         
 D M_readowner     C                   256                                                          
 D M_writeowner    C                   128                                                          
 D M_executeowner  C                   64                                                           
  *                                         group authority                                         
 D M_readgroup     C                   32                                                           
 D M_writegroup    C                   16                                                           
 D M_executegroup  C                   8                                                            
  *                                         other people                                            
 D M_readother     C                   4                                                            
 D M_writeother    C                   2                                                            
 D M_executeother  C                   1                                                            
  *                                                                                                 
 Difspath          s            512a                                                                
 Doflag            s             10I 0                                                              
 Dmode             s             10U 0                                                              
 Dcodepage         s             10U 0                                                              
 Dfiledescriptor   s             10i 0                                                              
 Difsdata          s          65536a   inz varying(4)                                               
 Dreturn_write     s             10i 0 inz                                                          
 Dreturn_close     s             10i 0 inz                                                          
                                                                                                    
                                                                                                    
 Derror_ptr        S               *                                                                
 Derror_num        S             10I 0 based(error_ptr)                                             
                                                                                                    
 Derrormsg_ptr     S               *                                                                
 Derror_msg        S            500a   based(errormsg_ptr)                                          
                                                                                                    
  /free                                                                                             
   ifspath = '/home/easyclass/helloworld';                                                          
   oflag = O_writeonly +                                                                            
           O_createfileifnotexist;                                                                  
   mode =                                                                                           
          M_readowner +                                                                             
          M_writeowner +                                                                            
          M_executeowner;                                                                           
                                                                                                    
   filedescriptor = open(%trim(ifspath):                                                            
                         oflag:                                                                     
                         mode);                                                                     
   if filedescriptor < 0;                                                                           
     error_ptr = errorIFS();                                                                        
     errormsg_ptr = strerror(error_num);                                                            
     return;                                                                                        
   endif;                                                                                           
                                                                                                    
   ifsdata = 'HELLO WORLD';
   // explanation of below highlighted
   // we are passing address of ifsdata variable and adding 4 bytes to the address i.e. 
// start reading by shifting right to the 4 bytes from the start
// of the address since first 4 bytes contained length of the variable ifsdata.
return_write = write(filedescriptor:%addr(ifsdata)+4:%size(ifsdata)); if return_write < %size(ifsdata); error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); return; endif; return_close = close(filedescriptor); if return_close = -1; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); return; endif; *inlr = *on; return; /end-free

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain