fhg
Volume 6, Number 2 -- February 1, 2006

A FUNction to Align Text

Published: February 1, 2006

Hey, Ted:

I read with interest your article on the IIF function. I have a function called alignText that I use in just about every program. I use it primarily to center headings, but have found it useful for right adjust as well. I have had this for a long time, so it is in traditional form, not freeform. Notice that there are three optional parameters. The alignment defaults to centering, and then two additional parameters are used to change the display attribute if I want to.

-- Gordon Larkins, ASK Senior Technical Consultant


It was nice of Gordie to share his routine with the rest of us. Centering text is a common task in green-screen and report programming. Everybody needs one of these routines in the old toolbox.

Here's the procedure prototype for alignText. There are several ways to handle the storage of prototypes, but I think the easiest is to place the prototype in a member of a source physical file called PROTOTYPES. All callers will need to use a /COPY or /INCLUDE directive to bring the prototype in at compilation.

D alignText       pr           132                          
D  inText                      132    value                 
D  inLen                        10i 0 value                 
D  inAlign                       1    value options(*nopass)
D  inBegAtt                      1    value options(*nopass)
D  inEndAtt                      1    value options(*nopass)

Here's the source code for an ALIGNTEXT module.

H nomain                                                    
                                                            
D/copy prototypes,alignText                                 
P alignText       b                   export                
D alignText       pi           132                          
D  inText                      132    value                 
D  inLen                        10i 0 value                 
D  inAlign                       1    value options(*nopass)
D  inBegAtt                      1    value options(*nopass)
D  inEndAtt                      1    value options(*nopass)
                                                            
D outText         s            132                          
D cx              s             10i 0                       
                                                            
C                   Do                                      
                                                            
C                   If        inText = *Blank               
C                             or inLen < %Len(%Trim(inText))
C                   Eval      outText = inText                   
C                   Leave                                        
C                   Endif                                        
                                                                 
C                   Select                                       
                                                                 
C                   When      %Parms > 4                         
C                   Eval      outText = inBegAtt + %Trim(inText) 
C                                       + inEndAtt               
C                   When      %Parms > 3                         
C                   Eval      outText = inBegAtt + %Trim(inText) 
                                                                 
C                   Other                                        
C                   Eval      outText = %Trim(inText)            
C                   Endsl                                        
                                                                 
 * Right Align Text
C                   If        %Parms > 2 and inAlign = 'L'           
C                   Eval      cx = inLen - %Len(%Trim(outText)) + 1  
C                   Eval      %Subst(outText : cx) = outText         
C                   Eval      %Subst(outText : 1 : cx - 1) = *Blank  
C                   Leave                                            
C                   Endif                                            
                                                                     
 * Center Text                                                       
                                                                     
C                   Eval      cx = (inLen - %Len(%Trim(outText))) / 2
C     ' '           Cat(p)    outText : cx  outText                  
                                                                     
C                   Enddo                                            
                                                                     
C                   Return    outText                                
                                                                     
P align Text       e

I recommend that the module be converted to a service program. Anyone who already has a service program for the manipulation of character strings might want to add this routine to that service program.

Gordie also sent a couple of code fragments showing how to call alignText. The first one does not use my IIF routine.

D hdrOne          s             45a 
D parmInqMnt      s              1a 

D/copy prototypes,alignText         

 /free                      
    *inlr = *on;            
    hdrOne = 'Customer Master'; 

    if parmInqMnt = 'M';        
        hdrOne = %Trim(hdrOne) + ' Maintenance'; 
    else;                       
        hdrOne = %Trim(hdrOne) + ' Inquiry'; 
    endif;               
                                 
    hdrOne = alignText(hdrOne : %Len(hdrOne));

The second fragment uses IIF, which shortens the code considerably. This is a good demonstration of how the creation of well-designed, reusable subprocedures can reduce the programming effort and thereby increase productivity.

D hdrOne          s             45a   
D parmInqMnt      s              1a   
                                      
D/copy prototypes,alignText           
D/copy prototypes,iif                 
                                      
 /free                                
    *inlr = *on;                      
    hdrOne = 'Customer Master';       

    hdrOne = alignText('Customer Master '  
           + iif(parmInqMnt = 'M' : 'Maintenance' : 'Inquiry')
           : %Len(hdrOne)); 

These program fragments center the text. For right-adjust, pass an L into the third parameter.

hdrOne = alignText(hdrOne : %Len(hdrOne):'L');

To learn more about the P-codes that can be passed into the fourth and fifth parameters, see the discussion of P-field values in the online DDS reference manual. The following command causes the hdrOne variable to blink on monochrome terminals and to be displayed in red on color terminals.

hdrOne = alignText(hdrOne : %Len(hdrOne):'C': x'28': x'20');

--Ted



Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Computer Keyes:  Rapidly convert *SCS printer files into black and white or full color PDF documents
Cosyn Software:  Audit Trail/400 for Sarbanes Oxley
COMMON:  Join us at the Spring 2006 conference, March 26-30, in Minneapolis, Minnesota

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement