Difference between revisions of "Snippets"

From MidrangeWiki
Jump to: navigation, search
(Snippets View: +XML Developer capability prereq)
(sleep api)
Line 519: Line 519:
  
 
[[#top]]
 
[[#top]]
 +
 +
 +
=== RPG WAIT sim DLYJOB  ===
 +
ref: [[http://archive.midrange.com/rpg400-l/200501/msg00399.html]]
 +
 +
    HBNDDIR('QC2LE') DFTACTGRP(*NO) ACTGRP(*NEW)
 +
      * this uses the C sleep API
 +
      * this is a sample program named 'PSLEEP' to demo the call to the sleep API 
 +
      * to test,  call  PSLEEP '0000000005'
 +
      *
 +
    D PSLEEP          PR
 +
    D SECSA                        10A
 +
    D PSLEEP          PI
 +
    D SECSA                        10A
 +
      *
 +
    D WAIT            PR            1A
 +
    D SECS                          10U 0
 +
 
 +
    D CURTIME        S              T
 +
    D SECS            S            10U 0
 +
    D ANS            S            20A
 +
 
 +
    D SECSDS          DS            10
 +
    D SECSN                        10S 0
 +
 
 +
      /FREE
 +
            *INLR = *ON;
 +
   
 +
            SECSDS = SECSA;
 +
            SECS  = SECSN;
 +
     
 +
            CURTIME = %TIME();
 +
            DSPLY CURTIME;
 +
   
 +
            WAIT(SECS);
 +
   
 +
            CURTIME = %TIME();
 +
            DSPLY CURTIME ' ' ANS;
 +
      /END-FREE
 +
 
 +
      * // Procedure:  Sleep                                              //
 +
      * // Purpose:  Wait for a number of  seconds                        //
 +
      * // Parameters:                                                    //
 +
      * //    I: WSECS  -- 10 int                                          //
 +
      *
 +
      * // Returns:                                                        //
 +
      * //    0..1    -- 0=NOT OK, 1=OK                                    //
 +
      * //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
 +
    P WAIT            B
 +
    P
 +
    D WAIT            PI            1A
 +
    D WSECS                        10U 0
 +
 
 +
    DSLEEP            PR            1A  EXTPROC(X'A293858597')
 +
    D SECS                          10U 0 VALUE
 +
 
 +
      /FREE
 +
          RETURN  SLEEP(WSECS);
 +
      /END-FREE
 +
    P WAIT            E     
 +
[[#top]]
 +
  
 
==External links==
 
==External links==

Revision as of 08:11, 30 April 2009


SNIPPETS

A snippet is a sort of code template or skeleton that allows you to quickly set up a commonly used code fragment like a subprocedure definition (in ILE RPG).

This differs from code templates because the code template is invoked by the code completion facility Ctrl+Space.

Code completion with templates requires you to start keying the 'trigger' (in RPG, usually the operation code.)

With snippets, you can have Lpex include anything at all, with no trigger needed aside from the double click on the selected snippet.


Snippets View

original post [1]

To open a snippets view:

  • Enable XML Developer capability:
    • Window → Preferences → General → Capabilities, and then check XML Developer.
  • Window → Show view → Other → Basic → Snippets

#top

Create a Snippets Category

original post [2]

  • You only need to create a Category once, conceptually its like CRTSRCPF.
  • Create as many Categories as you wish.
  • -Right-click in the Snippets view and select Customize...
  • -Click the New button and create a New Category
  • -Click the New button and create a New Item
  • -Click on the snippet and then click the New button that is adjacent to the Variables list
  • -Enter your snippet and use the Insert Variable Placeholder button to add the variable

alternatively use the copy, right click process below, its easier.

Use snippets for RPGLE, CLLE, DSPF and PRTF source members.


#top

Snippets RPG

original post [3]

You can use the snippets to insert stuff into your program.

To create your own RPG snippets,

  • you can open the view in RSE or iSeries Editing perspective,
  • select your source,
  • then Right Click / Paste in the snippets view, into your Previously created Category.

It will then create a new template.

To use the snippet;

  • Open a source member for editing.
  • Place the cursor where you want the code.
  • Go to the Snippets view, to the Category where the snippet is.
  • Double click on the snippet and it appears in the source


original post [4]

Another method is to create a Snippet.

  1. Open the Snippets view.
  2. Create a new 'Drawer' for your snippets.
  3. Copy some source.
  4. Right-click in your snippet drawer, paste.
  5. This will bring up the snippet creation wizard.

Long Procedure Name

original post [5]

How do I get the PR to stay at the same column position, irrespective of what name I use for the procedure ?

Use:

D ${Procedure}...
                          PR  

Otherwise the procedure name has to always be 12 characters in length. With the elipsis (...) it can be up to 60 odd characters in length.


#top

Examples

Subprocedure skeleton:

Lpex snippet rpg subprocedure.JPG


Subprocedure skeleton

     * ${proc_comment}
    p ${proc_name}        b
    d ${proc_name}        pi            10i 0
    d  ${parm_name}                      1a
    
    d rc                            10i 0 inz
     
    c/free
       return rc;
     /end-free
    p                 e

EXEC SQL

ref [6]

    C/EXEC SQL
    C+ SELECT * FROM mylib/myfile
    C/END-EXEC
free
 exec sql select * from mylib/myfile;


Open Cursor and Fetch Mainline

ref [7]

    H ActGrp(*CALLER)
    H DftActGrp(*NO)
    D OpenCursor      PR              n
    D FetchCursor     PR              n
    D CloseCursor     PR              n
    D MyLib           s             10a
    D MyFile          s             10a
     /free
      *inlr=*on;
      if not OpenCursor();
        // perform error routine to alert the troops
        // ...
      Else;
        Dow FetchCursor();
          // putting the fetchcursor on the do loop allows the user of
          // iter, and thus iter will not perform an infinite loop
          // normal processing here...
        EndDo;
        CloseCursor();
      EndIf;
      return;
     /end-free

Open Cursor Procedure

ref [8]

    P OpenCursor      B
    D OpenCursor      PI                  like(ReturnVar)
    D ReturnVar       s               n
      // The immediately following /EXEC SQL is SQL's version of RPG's H Spec
      // It is never executed.  Just used at compile time.
    C/EXEC SQL
    C+ Set Option
    C+     Naming    = *Sys,
    C+     Commit    = *None,
    C+     UsrPrf    = *User,
    C+     DynUsrPrf = *User,
    C+     Datfmt    = *iso,
    C+     CloSqlCsr = *EndMod
    C/END-EXEC
    C/EXEC SQL
    C+ Declare C1 cursor for
    C+  Select System_Table_Schema as library,
    C+         System_Table_Name   as file
    C+  from qsys2/systables
    C/END-EXEC
    C/EXEC SQL
    C+ Open C1
    C/END-EXEC
     /free
      Select;
        When SqlStt='00000';
          return *on;
        Other;
          return *off;
      EndSl;
     /end-free
    P OpenCursor      E

Fetch Cursor Procedure

ref [9]

    P FetchCursor     B
    D FetchCursor     PI                  like(ReturnVar)
    D ReturnVar       s               n
    C/EXEC SQL
    C+ Fetch C1 into :MyLib, :MyFile
    C/END-EXEC
     /free
      Select;
        When sqlstt='00000';
          // row was received, normal
          ReturnVar=*on;
        When sqlstt='02000';
          // same as %eof, sooner or later this is normal
          ReturnVar=*off;
        Other;
          // alert the troops!
          ReturnVar=*off;
      EndSl;
      return ReturnVar;
     /end-free
    P FetchCursor     E

Close Cursor Procedure

ref [10]

    P CloseCursor     B
    D CloseCursor     PI                  like(ReturnVar)
    D ReturnVar       s               n
    C/EXEC SQL
    C+ Close C1
    C/END-EXEC
     /free
      Select;
        When sqlstt='00000';
          // cursor was closed, normal
          ReturnVar=*on;
        Other;
          // alert the troops!
          ReturnVar=*off;
      EndSl;
      return ReturnVar;
     /end-free
    P CloseCursor     E 


#top

Exporting and importing snippets

ref [[11]]

Snippets can be transferred to another PC by the process of exporting and importing.

You export on the PC you want to copy the snippets from, and import on the PC you want to copy them to.

Exporting

  • In the Snippets view right click and select Customize
  • Select your custom snippet category
  • Select Export (top of the category list, left side)
  • See the 'Save as...' dialogue box, remember the file name you chose!
  • Copy/FTP/email that XML file to the target PC

Importing

  • Get a snippets XML file some place where it's available to WDSC
  • In the Snippets view right click and select Customize
  • Select your custom snippet category
  • Select Import (top of the category list, left side)
  • See the 'Open...' dialogue box.
  • Select the XML file to import

Snippets XML structure

<?xml version="1.0" encoding="UTF-16"?>
<snippets>
    <category filters="*" id="category_1185380127750" initial_state="0" label="RPG" largeicon="" smallicon="">
        <description/>
        <item category="category_1185380127750" class="" editorclass="" id="item_1207666311828" label="subprocedure" largeicon="" smallicon="">
            <description><![CDATA[Subprocedure skeleton]]></description>
            <content><![CDATA[
      * ${proc_comment}
     p ${proc_name}        b
     d ${proc_name}        pi            10i 0
     d  ${parm_name}                      1a
     
     d rc                            10i 0 inz

     c/free
        return rc;
      /end-free
     p                 e]]></content>
            <variable default="Description of procedure" id="name_3" name="proc_comment">
                <description><![CDATA[Description]]></description>
            </variable>
            <variable default="new_parm" id="name_2" name="parm_name">
                <description><![CDATA[Parameter name]]></description>
            </variable>
            <variable default="new_proc" id="name_1" name="proc_name">
                <description><![CDATA[Procedure name]]></description>
            </variable>
        </item>
    </category>
</snippets>


#top

DayOfWeek snippet

ref [[12]]


     * // Procedure:  DayOfWeek                                           //
     * // Purpose:  Determine the day of week for a particular date       //
     * // Parameters:                                                     //
     * //    I: dt   -- date                                              //
     *
     * // Returns:                                                        //
     * //    0..6    -- 0=Sunday, 1=Monday, 2=Tuesday, etc.               //
     * // Notes:                                                          //
     * //    January 5, 1800 is a Sunday.  This procedure only works for  //
     * //    dates later than 1800-01-05.                                 //
     * //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
    P DayOfWeek       b
    P
    D DayOfWeek       pi            10i 0
    D
    D   dt                            d   value datfmt(*iso)
     /free
        return %rem (%diff (dt: d'1800-01-05': *days): 7);
     /end-free
    P DayOfWeek       e
                                                                                          

#top

FirstFriday snippet

    H DFTACTGRP(*NO ) OPTIMIZE(*NONE) OPTION(*NODEBUGIO)                  
                                                                          
                          
    D WDATE           S             10A                                   
                                                                          
                             
    D FirstFriday     PR             1A                                   
    D  Wdate                        10A                                   
                                                                          
               
     /FREE                                                                
        *INLR = *on;                                         
        WDATE = '2008-12-05';                                                   
        *IN01 = FirstFriday(WDATE);                                             
     /END-FREE  
                                                               
     * // Procedure:  FirstFriday                                         //
     * // Purpose:  Determine if the date is the First Friday             //
     * // Parameters:                                                     //
     * //    I: WDATE  -- 10 character ISO delim date                     //
     *
     * // Returns:                                                        //
     * //    0..1    -- 0=NO, 1=YES                                       //
     * // Notes:                                                          //
     * //    January 5, 1800 is a Sunday.  This procedure only works for  //
     * //    dates later than 1800-01-05.                                 //
     * //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
    P FirstFriday     B                                                         
    P                                                                           
    D FirstFriday     PI             1A                                         
    D  WDATE                        10A                                         
                                                                                
     /free                                                                      
                                                                                
       if %rem(%abs(%diff (%DATE(WDATE) : d'1800-01-05': *days)): 7) = 5        
         and %subdt(%DATE(WDATE)  :*D) <=7;                                     
           // it is the first Friday of the month                               
           RETURN '1';                                                          
      endif;                               
          RETURN '0';                      
                                           
    /end-free                              
   P FirstFriday     E                     


#top

System Date snippet

ref [[13]]

Time also works Return  %INT(%CHAR(%TIME():*ISO0)) set PI to 6S 0.

     * // Procedure:  SysDate                                             //
     * // Purpose:  Gets the system date YYYYMMDD format 8S 0              //
     * // Parameters:                                                     //
     * // Returns:                                                        //
     * //    int    -- date in YYYYMMDD fmt                               //
    * //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
    P SysDate         B                         
    P                                           
    D SysDate         PI             8S 0       
    D                                           
     /free                                      
        Return     %INT(%CHAR(%DATE():*ISO0));  
     /end-free                                  
    P SysDate         E                         

#top

CLLE Subroutine

ref [[14]]

Sample code V5R4

START:      PGM
   SAVLIB     LIB( PVCPROD)  DEV(TAP01) ENDOPT(*LEAVE)   /* Daily library backup */
   MONMSG     MSGID(PVC0001)  EXEC(DO)
      CALLSUBR BADLIBSAVE
   ENDDO
RETURN
SUBR BADLIBSAVE
 /* test */
ENDSUBR
ENDPGM

#top

RPG SYSTEM NAME

ref: [[15]]


D RtvSysName      PR             8A                                       
D   MyName        S             10A                                       
C                   eval      *inlr = *on                                 
C                   EVAL      MyName =   RtvSysName()                     
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
*  Retrieve System Name                         
P RtvSysName      B                   Export                              
D RtvSysName      PI             8A                                       
D QWCRNETA        PR                  ExtPgm('QWCRNETA')                  
D   RcvVar                   32766A   OPTIONS(*VARSIZE)                   
D   RcvVarLen                   10I 0 const                                   
D   NbrNetAtr                   10I 0 const                                   
D   AttrNames                   10A   const                                   
D   ErrorCode                  256A                                           
D* Error code structure                                                       
D EC              DS           256                                            
D* Receiver variable for QWCRNETA                      
D DS              ds                                                          
D  Filler                       32A                                         
D  RtnSystem                     8A                                         
 * Call API to get system name                                                
C                   callp     QWCRNETA(DS: %size(DS): 1: 'SYSNAME': EC)       
C                   return    RtnSystem                  
P                 E                                                           

#top


MODS w/POINTERS

ref: [[16]]

DEdtCstRtn        PR                  ExtPgm('@EDTCSTRTN')
D SubParm                             Like(MyStruct)
D SubParm1                            Like(Struct1)

Dmystruct       e ds                  ExtName(EDTCSTPF)
D                                     occurs(500)
DStruct1        e ds                  ExtName(EDTCSTHDR)

I fill the structure and then call the second program

    %occur(myStruct) = 1;
    Callp EdtCstRtn(MyStruct:Struct1);

Here is the code from the Called program:

DCstRtnParm       PR                  ExtPgm('@EDTCSTRTN')
DPlistPR                              Like(PlistParm)
DPlist1PR                             Like(Plist1)
DCstRtnParm       PI
DPlistPI                              Like(PlistParm)
DPlist1PI                             Like(Plist1)

DPlistParm      e ds                  ExtName(EDTCSTPF)
D                                     occurs(500)
D                                     based(p_PlistParm)
DPlist1         e ds                  ExtName(EDTCSTHDR)
D                                     based(p_Plist1)

 /free
     p_PlistParm = %addr(PlistPI);
     p_Plist1    = %addr(Plist1PI);

... do NOT EVAL one to the other... ... simply access PlistParm as a normal MODS.

#top


QUSRSPLA CL with STG(*DEFINED) DEFVAR

ref: [[17]]

[| See IBM defn of the SPLA0100 format for whatever attribute you need.]

/*     GET A SPOOL FILE ATTRIBUTE                                   */     
            PGM        PARM(&SPLF &FULLJOB  &RTNWID   )                   
                                                                          
            DCL        &SPLF *CHAR LEN(10)                                
            DCL        &FULLJOB *CHAR LEN(26)                             
                                                                          
            DCL        &SPLNBR *CHAR LEN(5)                               
            DCL        &SPLNBRD *DEC LEN(9 0)                             
            DCL        &SPLNBRB *CHAR LEN(4)                              
                                                                          
            DCL        &DSLEND *DEC LEN(9 0)                              
            DCL        &DSLENB *CHAR LEN(4)                               
                                                                          
            DCL        &INTJOB *CHAR LEN(16)                              
            DCL        &INTSPLF *CHAR LEN(16)                             
            DCL        &RTNWID *DEC (15 5)                                
                                                                          
            DCL        &DS      *CHAR LEN(1000)                           
            DCL        &RTNWIDTH *DEC (15 5) STG(*DEFINED) DEFVAR(&DS 889)
                                                                     
     /* GET LAST SPOOL FILE */                                       
           CHGVAR     &SPLNBRD -1                                    
           CHGVAR     %BIN(&SPLNBRB 1 4) &SPLNBRD                    
     /* USE 1000 BYTE DS        */                                   
           CHGVAR     &DSLEND 1000                                   
           CHGVAR     %BIN(&DSLENB 1 4) &DSLEND                      
                                                                     
     /* CALL API TO GET SPLFA */                                     
     /* OFFSET 888 CONTAINS THE WIDTH AS PACKED DECIMAL 15,5 */      
           CALL       QUSRSPLA PARM(&DS &DSLENB 'SPLA0100' +         
                       &FULLJOB &INTJOB &INTSPLF &SPLF &SPLNBRB)     
           MONMSG  CPF0000                                           
                                                                     
RTNWID:     CHGVAR &RTNWID    &RTNWIDTH                               
                                                                     
           RETURN                                                    
           ENDPGM                                                    

#top


RPG WAIT sim DLYJOB

ref: [[18]]

    HBNDDIR('QC2LE') DFTACTGRP(*NO) ACTGRP(*NEW)
     * this uses the C sleep API 
     * this is a sample program named 'PSLEEP' to demo the call to the sleep API  
     * to test,  call  PSLEEP '0000000005'
     *
    D PSLEEP          PR
    D SECSA                         10A
    D PSLEEP          PI
    D SECSA                         10A
     *
    D WAIT            PR             1A
    D SECS                          10U 0
  
    D CURTIME         S               T
    D SECS            S             10U 0
    D ANS             S             20A
  
    D SECSDS          DS            10
    D SECSN                         10S 0
 
     /FREE
           *INLR = *ON;
    
           SECSDS = SECSA;
           SECS   = SECSN;
     
           CURTIME = %TIME();
           DSPLY CURTIME;
    
            WAIT(SECS);
    
           CURTIME = %TIME();
           DSPLY CURTIME ' ' ANS;
      /END-FREE
  
     * // Procedure:  Sleep                                               //
     * // Purpose:  Wait for a number of  seconds                         //
     * // Parameters:                                                     //
     * //    I: WSECS  -- 10 int                                          //
     *
     * // Returns:                                                        //
     * //    0..1    -- 0=NOT OK, 1=OK                                    //
     * //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
    P WAIT            B
    P
    D WAIT            PI             1A
    D WSECS                         10U 0
 
    DSLEEP            PR             1A   EXTPROC(X'A293858597')
    D SECS                          10U 0 VALUE
 
     /FREE
          RETURN  SLEEP(WSECS);
     /END-FREE
    P WAIT            E      

#top


External links

The official WDSC web page [19]

The WDSC Developer blog [20]

WDSC tags on del.ici.ous [21]

Categories