ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Utility for wrapping text

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Utility for wrapping text

    Does anybody know if there is a utility where you can pass:
    1) A bunch a text
    2) How long each line of text should be

    And it would give you back what each line should say?

    The issue is that I have to write paragraphs where some of the verbiage is different but some is the same. My current method is to have three arrays. Depending on the situation, it will do a for-loop to print each line.

    Example:
    Code:
    [COLOR=#000000][SIZE=2][SIZE=2][FONT=courier new]
    /free                                                               
                                                                        
        Select;                                                         
           When (myParm = 1);                                           
              myText = 'Short sentence.';                               
           When (myParm = 2);                                           
              myText = 'Medium length sentence.';                       
           When (myParm = 3);                                           
              myText = 'This sentence is longer than the others.';      
        EndSL;                                                          
                                                                        
        myText = %TRIM(myText)                                          
               + '  The remaining verbiage would be the same.';          
                                                                        
                                                                        
        //           1         2         3         4         5         6
        //  123456789012345678901234567890123456789012345678901234567890
    
        //  Short sentence.  The remaining verbiage would be the same.  
                                                                    
        //  Medium length sentence.  The remaining verbiage would be the
        //  same.                                                       
                                                                    
        //  This sentence is longer than the others.  The remaining     
        //  verbiage would be the same.                                 
    
    [/FONT][/SIZE][/SIZE][/COLOR]
    In the above example, I would pass the text and indicate that the maximum length is 60
    http://www.linkedin.com/in/chippermiller

  • #2
    Re: Utility for wrapping text

    Hi Chipper:

    I'm not sure if this will fill your requirement:

    Note: The code accompanying this article is available for download here. One common integration problem encountered by those building client/server or Web-based interfaces to a legacy green-screen application is mapping a free form text field into a fixed width field. This tip presents a code segment I wrote to solve this problem. For example, say


    Best of Luck
    GLS
    The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

    Comment


    • #3
      Re: Utility for wrapping text

      That's what I need -- thanks! It shortens the number of spaces between sentences from two to only one, but I can tweak the code to handle that.
      http://www.linkedin.com/in/chippermiller

      Comment


      • #4
        Re: Utility for wrapping text

        That's a great article, thanks for sharing..

        Code:
              //
              // Author: Michael Sansoterra,  ITJungle.com
              //
              // WrapText Service Program
              //
              //
              // Program Compilation Notes:
              //
              // 1. Create Module (CRTRPGMOD)
              // 2. CRTSRVPGM SRVPGM(xxxx/WRAPTEXT)
              //              EXPORT(*ALL)
              //              TEXT('Wrap Text Utility')
              //              USRPRF(*OWNER)
              //
              //
              //
              // SQL Registration - Table Function Definition:
              // ===============================================
              // Create Function xxxx/WrapText
              // (UnformattedText VarChar(8192),
              //  LineLen         Dec(5,0),
              //  LineBreak       VarChar(10))
              // Returns Table
              // (LineNo   Dec(5,0),
              //  LineText VarChar(128))
              // Language RPGLE
              // Not Fenced
              // Deterministic
              // No SQL
              // External Name 'xxxx/WRAPTEXT(WRAPTEXT_TF)'
              // Disallow Parallel
              // Parameter Style DB2SQL
              // Cardinality 10
              //
              //
              //
              //
             HNoMain Option(*SrcStmt)
        
        
              //  UDTF call parameter constants
        
        
             D UDTF_FirstCall  S             10I 0 Inz(-2)
             D UDTF_Open       S             10I 0 Inz(-1)
             D UDTF_Fetch      S             10I 0 Inz(0)
             D UDTF_Close      S             10I 0 Inz(1)
             D UDTF_LastCall   S             10I 0 Inz(2)
        
        
              //  SQL States
        
        
             D SQLSTATEOK      C                   '00000'
             D ENDOFTABLE      C                   '02000'
             D UDTF_ERROR      C                   'US001'
        
        
              //====================================================================
              // Prototype for replace utility
              //====================================================================
             DReplace          PR          8192    Varying
             D parmSearchStr               8192    Varying Const Options(*VarSize)
             D FindStr                     8192    Varying Const Options(*VarSize)
             D ReplaceStr                  8192    Varying Const Options(*VarSize)
              //====================================================================
              // Prototype for wrapping unformatted data to a specified length
              //====================================================================
             D WrapText        PR         12288
             D  UnfText                    8192    Varying Const Options(*VarSize)
             D  LineLen                       5  0 Const
             D  LineBreak                    10    Varying Const Options(*NoPass)
              //====================================================================
              // Prototype for wrapping unformatted data to a specified length
              //====================================================================
             D WrapText_TF     PR
              // Function Input(s)
             D  UnfText                    8192    Varying Const Options(*VarSize)
             D  LineLen                       5  0 Const
             D  LineBreak                    10    Varying Const
              // Function Output(s)
             D  LineNo                        5  0
             D  LineText                    128    Varying
              // Function Input Null Indicators
             D  UnfText_NI                    5I 0
             D  LineLen_NI                    5I 0
             D  LineBreak_NI                  5I 0
              // Function Output Null Indicators
             D  LineNo_NI                     5I 0
             D  LineText_NI                   5I 0
              // DB2SQL Style Parameters
             D SQLState                       5
             D FunctionName                 517
             D SpecificName                 128
             D SQLMsgText                    70    Varying
              // UDTF Call Type
             D TFCallType                    10I 0
        
        
              //===============================================================
              // Subprocedure: Wrap Text to Specified Length
              //===============================================================
             P WrapText        B                   Export
        
        
             D WrapText        PI         12288
             D  UnfText                    8192    Varying Const Options(*VarSize)
             D  LineLen                       5  0 Const
             D  LineBreak                    10    Varying Const Options(*NoPass)
              // Work Fields
             D WrkText         s            + 1    Like(UnfText)
             D LineText        s          12288
             D WordText        s                   Like(UnfText)
             D FmtText         s          12288
             D WordLen         s              5  0
              // Word/Line counters
             D Line            s              5  0
             D Word            s              5  0
        
        
              /Free
                 WrkText=%TrimL(UnfText)+' ';
                 If %Parms>=3;
                     If %Len(LineBreak)>0;
                         WrkText=Replace(WrkText:LineBreak:' '+LineBreak+' ');
                     EndIf;
                 EndIf;
        
        
                 If LineLen<=*Zero
                 Or LineLen>%Size(UnfText);
                     Return 'INVALID LEN*';
                 EndIf;
        
        
                 If UnfText=*Blank;
                     Return '';
                 EndIf;
        
        
                 Dow %Len(WrkText)>*Zero;
        
        
                     // Find Boundary of word
                     WordLen=%Scan(' ':WrkText)-1;
        
        
                     If WordLen>*Zero;
        
        
                         // Test if Word length is greater than the wrap length
                         If WordLen>LineLen ;
                             WordText=%Subst(WrkText:1:LineLen);
                             WrkText=%Subst(WrkText:LineLen+1);
                         Else;
                             WordText=%Subst(WrkText:1:WordLen);
                             WrkText=%TrimL(%Subst(WrkText:WordLen+1));
                         EndIf;
        
        
                         // Test if break was requested
                         If %Parms=3;
                             If WordText=LineBreak;
                                 WordText=' ';
                                 ExSr BuildLine;
                             EndIf;
                         EndIf;
        
        
                         // If Length of Current Line + Length of the current word
                         // > than formatted line length, make a new line
                         If %Len(%TrimR(LineText)) +
                            %Len(WordText)+1>LineLen
                         And %Len(%TrimR(LineText))>0;
                            ExSr BuildLine;
                         EndIf;
        
        
                         //  Append Word to current Line
                         //  NOTE: Word will be blank if a line break specified
                         If WordText<>*Blanks;
                             Word=Word+1;
                             If Word=1;
                                 LineText=WordText;
                             Else;
                                 LineText=%TrimR(LineText)+' '+WordText;
                             EndIf;
                         EndIf;
        
        
                     EndIf;
                 EndDo;
        
        
                 // Build Remaining Line
                 If LineText<>*blanks;
                     ExSr BuildLine;
                 EndIf;
        
        
                 Return FmtText;
        
        
                 // Build Single Line according to the requested format width
                 BegSr BuildLine;
                     Word=*Zero;
                     If Line=*Zero;
                         FmtText=%Subst(LineText:1:LineLen);
                     Else;
                         If LineLen*Line>%Size(FmtText);
                             LeaveSr;
                         Else;
                             FmtText=%Subst(FmtText:1:LineLen*Line) +
                                     %Subst(LineText:1:LineLen);
                         EndIf;
                     EndIf;
                     Line=Line+1;
        
        
                     LineText=*Blank;
                 EndSr;
              /End-Free
             P WrapText        E
        
        
        
        
              //===============================================================
              // Subprocedure: Wrap Text & return as individual lines to DB2 TF
              //===============================================================
             P WrapText_TF     B                   Export
             D WrapText_TF     PI
              // Function Input(s)
             D  UnfText                    8192    Varying Const Options(*VarSize)
             D  LineLen                       5  0 Const
             D  LineBreak                    10    Varying Const
              // Function Output(s)
             D  LineNo                        5  0
             D  LineText                    128    Varying
              // Function Input Null Indicators
             D  UnfText_NI                    5I 0
             D  LineLen_NI                    5I 0
             D  LineBreak_NI                  5I 0
              // Function Output Null Indicators
             D  LineNo_NI                     5I 0
             D  LineText_NI                   5I 0
              // DB2SQL Style Parameters
             D SQLState                       5
             D FunctionName                 517
             D SpecificName                 128
             D SQLMsgText                    70    Varying
              // UDTF Call Type
             D TFCallType                    10I 0
        
        
             DLineLenTmp       S              5  0
             DText             S          12288    Varying Static
             DNoLines          S             10I 0 Static
             DCurLine          S             10I 0 Static
              /Free
                 SQLState=SQLStateOK;
        
        
                 Select;
                 // Function Open -> Prepare data for retrieval
                 When TFCallType=UDTF_Open;
                     If LineLen<=0
                     Or LineLen>=%Size(UnfText)-2;
                         SQLState=ENDOFTABLE;
                         Return;
                     EndIf;
                     NoLines=*Zero;
                     CurLine=*Zero;
                     Text=WrapText(UnfText:LineLen:LineBreak);
        
        
                     NoLines=%Len(%TrimR(Text))/LineLen;
                     If %Rem(%Len(%TrimR(Text)):LineLen)>0;
                         NoLines+=1;
                     EndIf;
        
        
                 // Function Fetch -> Return data from multi-occur data
                 // structure
                 When TFCallType=UDTF_Fetch;
                     If CurLine=NoLines;
                         SQLState=ENDOFTABLE;
                     Else;
                         LineNo=CurLine+1;
                         If CurLine*LineLen+LineLen>%Size(Text)-2;
                             LineLenTmp=%Size(Text)-2-(CurLine*LineLen);
                         Else;
                             LineLenTmp=LineLen;
                         EndIf;
                         LineText=%TrimR(
                         %Subst(Text:CurLine*LineLen+1:LineLenTmp));
        
        
                         CurLine+=1;
                     EndIf;
                 When TFCallType=UDTF_Close;
                 EndSl;
              /End-Free
             P WrapText_TF     E
        
        
              //================================================================
              // Find and Replace a string
              //
              // Parameters:
              //    Search   IN - String to search
              //    Find     IN - String to find
              //    Replace  IN - String to replace
              //
              // Returns:
              //    Result        String with replaced data
              //
              //
              // Notes:
              //
              //
              //================================================================
             PReplace          B                   Export
             DReplace          PI          8192    Varying
             D parmSearchStr               8192    Varying Const Options(*VarSize)
             D FindStr                     8192    Varying Const Options(*VarSize)
             D ReplaceStr                  8192    Varying Const Options(*VarSize)
        
        
             D SearchStr       S           8192    Varying Static
        
        
             D Pos             S              5i 0
             D SL              S              5i 0
             D FL              S              5i 0
        
        
              /Free
                 SearchStr=parmSearchStr;
        
        
                 FL=%Len(FindStr);
                 Pos=*zero;
        
        
                 Dow pos + fl<=%Len(SearchStr);
                     SL=pos+1;
                     Pos=%Scan(FindStr:SearchStr:sl);
        
        
                     // Leave if search string isn't found
                     If Pos=*Zero;
                         Leave;
                     EndIf;
        
        
                     // Build new string with replaced text in the middle
                     If Pos + fl > %Len(SearchStr);
                         SearchStr=%Subst(SearchStr:1:Pos-1) +
                                   ReplaceStr;
                     Else;
                          SearchStr=%Subst(SearchStr:1:Pos-1) +
                                    ReplaceStr +
                                    %Subst(SearchStr:Pos + fl);
                     EndIf;
        
        
                     // Set starting position for search of next occurance
        
        
                     Pos=Pos+%Len(ReplaceStr)-1;
                 EndDo;
        
        
                 Return SearchStr;
              /End-Free
             P Replace         E
        
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: Utility for wrapping text

          Great! I wrote my own a few years ago... called mine wraptxt

          Comment


          • #6
            Re: Utility for wrapping text

            i prefer ThisCrapIsTooLongChopItDownToSize()
            I'm not anti-social, I just don't like people -Tommy Holden

            Comment


            • #7
              Re: Utility for wrapping text

              I don't know how useful this would be, but here is an example that Scott Klement posted 6 or 7 years ago. Pete

              Code:
              FQSYSPRT   O    F   80        PRINTER                               
                                                                                  
              D dbfield         s           1500A                                 
              D line            s             80A                                 
                                                                                  
              D max             s             10I 0                               
              D linemax         s             10I 0                               
              D start           s             10I 0                               
              D end             s             10I 0                               
              D len             s             10I 0                               
                                                                                  
               /free                                                              
                 dbfield = 'Four score and seven years ago our fathers brought '  
                         + 'forth on this continent, a new nation, conceived in ' 
                         + 'Liberty, and dedicated to the proposition that all '  
                         + 'men are created equal. Now we are engaged in a great '
                         + 'civil war, testing whether that nation, or any nation '  
                         + 'so conceived and so dedicated, can long endure. We are ' 
                         + 'met on a great battlefield of that war. We have come to '
                         + 'dedicate a portion of that field, as a final resting '   
                         + 'place for those who here gave their lives for that that '
                         + 'nation might live. It is altogether fitting and proper ' 
                         + 'that we should do this. But, in a larger sense, we can ' 
                         + 'not dedicate -- we can not consecrate -- we can not '    
                         + 'hallow -- this ground. The brave men, living and dead, ' 
                         + 'who struggled here, have consecrated it, far above our ' 
                         + 'poor power to add or detract. The world will little '    
                         + 'note, nor long remember what we say here, but it can '   
                         + 'never forget what they did here. It is for us the '      
                         + 'living, rather, to be dedicated here to the unfinished ' 
                         + 'work which they who fought here have thus far so nobly ' 
                         + 'advanced. It is rather for us to be here dedicated to '  
                         + 'the great task remaining before us -- that from these '  
                         + 'honored dead we take increased devotion to that cause '  
                         + 'for which they gave the last full measure of devotion '  
                         + '-- that we here highly resolve that these dead shall '   
                         + 'not have died in vain -- that this nation, under God, '  
                         + 'shall have a new birth of freedom -- and that government 
                         + 'of the people, by the people, for the people, shall '    
                         + 'not perish from the earth.';                             
                                                                                     
                                                                                     
                 max = %len(%trimr(dbfield));                                        
                 start = 1;                                                          
                                                                                     
                 dow ( start <= max );                                               
                                                                                     
                      linemax = start + %size(line);                                 
                                                                                     
                      if (linemax > max);                                            
                         linemax = max;                                              
                      endif;                                                         
                                                                                     
                      end = linemax;                                                 
                                                                                     
                      dow (end < max and %subst(dbfield:end:1) < > ' ');                  
                        end = end - 1;                                               
                        if (end <= start);                                           
                           end = linemax;                                            
                           leave;                                                    
                        endif;                                                       
                      enddo;                                                         
                                                                                     
                      len = (end - start) + 1;                                       
                      line = %subst(dbfield:start:len);                              
                      except printme;                                                
                      start = start + len;                                           
                 enddo;                                                              
                                                                                     
                 *inlr = *on;                                                        
                                                                                     
               /end-free                                                             
                                                                                     
              OQSYSPRT   E            PrintMe                                        
              O                       Line
              Last edited by Pete; January 26, 2012, 03:08 PM. Reason: Corrected typo

              Comment


              • #8
                Re: Utility for wrapping text

                Originally posted by Pete View Post
                I don't know how useful this would be, but here is an example that Scott Klement posted 6 or 7 years ago. Pete

                < snip >

                I like that one! A lot less code -- will have to play with it to test it, but I like what I see so far! Thanks!!!
                http://www.linkedin.com/in/chippermiller

                Comment


                • #9
                  Re: Utility for wrapping text

                  At first glance Scott's code just breaks into 80 char segments regardless of complete words...
                  Michael's code breaks at the end of a word

                  GLS
                  The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

                  Comment


                  • #10
                    Re: Utility for wrapping text

                    Ok... nobody laugh, here is my code. It accepts up to 1320 characters and a line break and wraps the text.

                    Code:
                    H NOMAIN                                                                                       
                    H BNDDIR('HHPLIB/HHPBIND')                                                                     
                    H**************************************************************************                    
                    H* CREATE COMMAND:  CRTRPGMOD (option 15) BNDDIR(ASTHHOBJ/ASTBIND)                             
                    H*                                                                                             
                    H*  GREG WILBURN             WRAPTXT             SEP 24, 2010                                  
                    H*                                                                                             
                    H* This module is designed to word wrap a text string based on length                          
                    H*  and carriage return character.  It accepts up to 10x132 char string.                       
                    H*                                                                                             
                    H* Input:  Up to 1320 char string with wrap length & carriage return chars (optional)          
                    H* Output: 1320 char string with carriage return removed                                       
                    H*                                                                                             
                    H* Usage:  Move returned value into a 10x132 array in calling pgm.  Each element               
                    H*         of the array represents a line wrapped to the length specified.                     
                    H*                                                                                             
                    H**************************************************************************                    
                    D/COPY HHPLIB/QCPYSRC,WRAP_TXT                                             Prototype           
                    P**************************************************************************                    
                    P WrapTxt         B                   Export                                                   
                    D WrapTxt         PI          2647A   Varying                                                  
                    D  intext                     1320A   Const                                Input Text String   
                    D  incr                          4A   Const                                Input Carriage Retrn
                    D  inln                          3S 0 Const                                Input Length        
                    D*                                                                                             
                    D w1320           s           1320a   Inz                                  Text String Wrkfld  
                    D*                                                                                             
                    D strpos          s                   Like(inln) Inz                                           
                    D pos             s                   Like(inln) Inz                                           
                    D endpos          s                   Like(inln) Inz                                           
                    D ln              s                   like(inln) Inz                       Length of wrap      
                    D cr              s                   like(incr)                           Carriage Return     
                    D crl             s              1S 0 Inz                                  CR length           
                    D x               s              2S 0 Inz(1)                                                   
                    D*                                                                                             
                    D txtx            s            132a   Inz dim(10)                          Text Work Array     
                    D outtext         s           1320a   Inz                                  Wrapped Output Text 
                    D*                                                                                             
                    C**************************************************************************                    
                    C*                                                                                             
                    C                   eval      w1320 = intext                                                   
                    C                   eval      ln = inln                                                        
                    C                   eval      cr = incr                                                        
                    C*                                                                                             
                    C                   eval      crl = %scan(' ':incr)                                            
                    C                   if        crl = 0                                                          
                    C                   eval      crl = %len(incr)                                                 
                    C                   endif                                                                     
                    C*                                                                                            
                    C                   dou       %subst(w1320:1:ln) = *blanks or                                 
                    C                             x > %elem(txtx)                                                 
                    C                   eval      pos = %scan(cr:%subst(w1320:1:ln))           Scan for CR        
                    C                   if        cr <> *blanks and                            Non-blank          
                    C                             pos > 0                                      CR Found           
                    C                   eval      endpos = pos - 1                                                
                    C                   if        endpos > 0                                                      
                    C                   eval      txtx(x) = %triml(%subst(w1320:1:endpos))                        
                    C                   add       1             x                                                 
                    C                   endif                                                                     
                    C                   eval      strpos = pos + crl                           Omit CR            
                    C                   eval      w1320 = %triml(%subst(w1320:strpos))                            
                    C                   iter                                                                      
                    C*                                                                                            
                    C                   else                                                   CR Not Found       
                    C*                                                                                            
                    C                   eval      endpos = ln                                  Find a Line Break  
                    C                   if        %subst(w1320:endpos:1) = ' '                 Last char is blank 
                    C                   else                                                                      
                    C                   dou       %subst(w1320:endpos:1) = ' ' or              Look for blank     
                    C                             endpos = 1                                                      
                    C                   eval      endpos = endpos - 1                                             
                    C                   enddo                                                    
                    C                   endif                                                    
                    C*                                                                           
                    C                   if        endpos > 0                                     
                    C                   eval      txtx(x) = %triml(%subst(w1320:1:endpos))       
                    C                   add       1             x                                
                    C                   endif                                                    
                    C                   eval      strpos = endpos + 1                            
                    C                   eval      w1320 = %triml(%subst(w1320:strpos))           
                    C                   iter                                                     
                    C*                                                                           
                    C                   endif                                                    
                    C                   enddo                                                    
                    C*                                                                           
                    C                   movea     txtx          outtext                          
                    C                   eval      *inlr = *on                                    
                    C*                                                                           
                    C                   Return    outtext                                        
                    C*                                                                           
                    P WrapTxt         E

                    Comment


                    • #11
                      Re: Utility for wrapping text

                      See Michael's qualifications here:

                      Comment


                      • #12
                        Re: Utility for wrapping text

                        I'm pretty sure mine does what he's asking for. It's a bit more complicated because Toys R Us thought it was wise to "invent" their own carriage return character and put it into the actual text, instead of of just wrapping before sending it to me.

                        You can pass this 1320 characters of data, the length you want it wrapped at, and it returns the data wrapped in 132 character rows.
                        Last edited by gwilburn; January 26, 2012, 03:27 PM.

                        Comment


                        • #13
                          Re: Utility for wrapping text

                          Creating a procedure to bend text like Neo


                          Create a source file, for this examples I created QPRCSRC in my library JAMIE.
                          This procedure allows you to pass a hunk of data (up to 64680 Characters) and the number
                          of characters you want the “hunk” broken into. The data will be returned in a datastructure
                          array and be available to the calling program. Some of this (if not all) will bore most of you,
                          but I hope that it helps at least one person. This needs some work….

                          · quotes and apostrophe need to be addressed
                          · if there is too much text passed KAPOW!
                          · Check that passed in Max record length is not greater than 132.
                          · Etc…


                          Code:
                          crtsrcpf jamie/qprcsrc rcdlen(112)
                          Place the source code for WRAPTXT and WRAPTXT_CP into the source file.
                          Click image for larger version

Name:	image1.gif
Views:	1
Size:	5.6 KB
ID:	126489

                          The code for WRAPTXT – Don’t forget I get paid by the line of code
                          Code:
                               h nomain expropts(*resdecpos)                                                                  
                                *----------------------------------------------------------------                             
                                * PROGRAM - WRAPTXT                                                                           
                                * PURPOSE - Subprocedure to wrap text                                                         
                                * WRITTEN - 99/99/99                                                                          
                                * AUTHOR  -                                                                                   
                                * Parameters                                                                                  
                                *       Instring              Char 64680 input                                                
                                *       length                dec  15  5 input                                                
                                *                                                                                             
                                *                                                                                             
                                *   The WRAPTEXT_CP copy member should be used by programs                                    
                                *   that call this subprocedure to obtain the procedure                                       
                                *   prototype fields.                                                                         
                                *----------------------------------------------------------------                             
                                                                                                                             
                                /copy qprcsrc,WRAPTXT_CP                                                                      
                                                                                                                             
                               d backSome        s             10i 0 inz(1)                                                   
                               d count           s             10i 0 inz                                                      
                               d count2          s             10i 0 inz                                                      
                               d ProcedureError  s               n   inz                                                      
                               d length          s              4  0 inz                                                      
                               d MX              s             10i 0 inz                                                      
                               d skip            s             10i 0                                                          
                               d start           s              4  0 inz(1)                                                   
                               d Total2Read      s             10i 0 inz                                                      
                               d TotalLines      s             10i 0 inz                                                      
                               d Workmax         s             10i 0 inz                                                      
                               d WorkString      s          64680    inz varying                                              
                                *                                                                                             
                                * Begin Procedure                                                                             
                                *                                                                                             
                               P wraptxt         B                   export                                                   
                                *                                                                                             
                                * Procedure Interface                                                                         
                                *                                                                                             
                               d wraptxt         pi              n                                                            
                               d instring                   64680    const                                                    
                               d inMaxlength                   15  5 const                                                    
                                                                                                                             
                                                                                                                              
                                /free                                                                                         
                                                                                                                             
                                      reset ProcedureError;                                                                   
                                                                                                                             
                                      if inmaxlength > *zeros;                                                                
                                                                                                                             
                                       workmax = inmaxlength;                                                                 
                                                                                                                              
                                       TotalLines = %len(%trim(instring))/workmax;                                            
                                                                                                                              
                                       // if last line not full we still need to read it                                      
                                       // we may still have an error when we start to split                                   
                                       // the lines by word...                                                                
                                       if %rem(%len(%trim(instring)):workmax) > *zeros;                                       
                                        TotalLines+=1;                                                                        
                                       endif;                                                                                 
                                       reset MX;                                                                              
                                       clear MAX132;                                                                          
                                       reset Total2Read;                                                                      
                                                                                                                              
                                       for count = 1 to TotalLines;                                                           
                                        reset BackSome;                                                                       
                                        Total2Read += workmax;                                                                
                                        WorkString = %subst(InString:Start:workmax);                                          
                                                                                                                              
                                        if %subst(WorkString:workmax:1) <> *blanks;                                           
                                                                                                                             
                                         for count2 = workmax downto 1  By  1;                                                
                                                                                                                             
                                          if %subst(WorkString:count2:1) = *blanks;                                           
                                           leave;                                                                             
                                          endif;                                                                              
                                                                                                                              
                                         endfor;                                                                              
                                                                                                                              
                                         MX+=1;                                                                               
                                         max132.string(MX) = %subst(InString:start:(Count2-1));                               
                                         BackSome += (workmax - %len(%trim(max132.string(MX))));                              
                                                                                                                             
                                         skip = 2;                                                                            
                                        else;                                                                                 
                                         MX+=1;                                                                               
                                         max132.string(MX) = WorkString;                                                      
                                         skip = 1;                                                                            
                                        endif;                                                                                
                                                                                                                             
                                        Start += (workmax - BackSome)+skip;                                                   
                                       endfor;                                                                                
                                                                                                                             
                                      else;                                                                                   
                                       ProcedureError = *on;                                                                  
                                      endif;                                                                                  
                                                                                                                             
                                      return ProcedureError;                                                                  
                                                                                                                              
                                /end-free                                                                                     
                                                                                                                              
                               p wraptxt         E

                          Here is the code for the copy book…Just stick with me on this. The copy book is another way to
                          make using a procedure in another program much easier.
                          Code:
                                *-----------------------------------------------------------                                  
                                * wraptxt - wrap text   132 x 490 = 64680                                                     
                                *                                                                                             
                                *-----------------------------------------------------------                                  
                               d wraptxt         pr              n                                                            
                               d instring                   64680    const                                                    
                               d inMaxlength                   15  5 const                                                    
                                                                                                                             
                                                                                                                              
                               d max132          ds                  qualified  inz                                           
                               d  WholeTomato               64680                                                             
                               d   String                     132    dim(490) overlay(WholeTomato:*next)
                          Okay lets compile this thing..
                          Code:
                          CRTRPGMOD MODULE(JAMIE/WRAPTXT)         
                          SRCFILE(JAMIE/QPRCSRC) DBGVIEW(*SOURCE)
                          Now let’s test this: I need a binding directory first..
                          Code:
                          CRTBNDDIR BNDDIR(JAMIE/GLOBAL) TEXT('Jamie''s Global Binding directory')
                          Now add our new procedure to the binding directory
                          Code:
                          wrkbnddir global
                          Take option 9 to add


                          Click image for larger version

Name:	image2.gif
Views:	1
Size:	4.2 KB
ID:	126490

                          Then enter and press


                          Click image for larger version

Name:	image3.gif
Views:	1
Size:	4.2 KB
ID:	126491

                          Now we are ready to test this mess.

                          Notice the header(bnddir) and the copy book WRAPTXT_CP
                          Code:
                               
                              H dftactgrp( *no ) bnddir('GLOBAL') OPTION(*NODEBUGIO) 
                                                                                                                       
                                /copy qprcsrc,wraptxt_CP                                                                     
                                                                                                                             
                               d BigField        s           5000                                                            
                               d MyError         s               n                                                           
                                * ================================================                                           
                                * M A I N   L I N E                                                                          
                                * ================================================                                           
                                *                                                                                            
                                /free                                                                                        
                                                                                                                              
                                   bigfield =                                                                                
                                   'Photo: Steve Granitz: Every parent has' +                                                
                                   ' his or her tricks -- things that must be' +                                             
                                   ' done to keep the house running. Brad Pitt is no different.' +                           
                                   ' In a recent interview with CBS'+                                                         
                                   ' This Morning, the Oscar-nominated actor (Moneyball)' +                                  
                                   ' revealed that he sometimes has to'+                                                     
                                   ' rely on a certain carbonated beverage to get' +                                         
                                   ' his kids moving in the morning.';                                                       
                                                                                                                              
                                                                                                                             
                                     MyError = wraptxt(BigField:40);                                                         
                                                                                                                              
                                     *inlr = *on;                                                                            
                                                                                                                              
                                /end-free

                          Create the above program with a simple from PDM or command line
                          Code:
                          CRTBNDRPG PGM(JAMIE/WRAPTEST) SRCFILE(JAMIE/SOURCE) DBGVIEW(*SOURCE)
                          Start Debug, call WRAPTEST and enjoy.

                          Click image for larger version

Name:	image4.gif
Views:	1
Size:	4.6 KB
ID:	126492
                          All my answers were extracted from the "Big Dummy's Guide to the As400"
                          and I take no responsibility for any of them.

                          www.code400.com

                          Comment


                          • #14
                            Re: Utility for wrapping text

                            this fix will allow data to be returned to calling program...
                            Attached Files
                            All my answers were extracted from the "Big Dummy's Guide to the As400"
                            and I take no responsibility for any of them.

                            www.code400.com

                            Comment

                            Working...
                            X