Declare a function within a .P program (V 8.03)

You can, at the top of your program below your variables and parameters:

DEFINE FUNCTION fDepunct RETURNS CHAR (pStr AS CHAR):

Def var vcRet as char no-undo.

If pStr = "" then return "".

vcRet = replace(pStr, ">", "&GT").
vcRet = replace(vcRet, "<", "&LT").
... etc ...

RETURN vcRet.

END FUNCTION.

Then in your mainline code ....

msgOut = msgOut + fDepunct(field1) + " blah blah blah " + fDepunct(field2) ... etc.

in general stringVariable = fDepunct(stringExpression) will return the replaced string.

If pStr doesn't contain the target characters it will be returned unchanged.

Good Luck!

Chris Heins
Sr. Mgr. Applications Software
PNY Technologies, Inc.
[Description: Description: cid:image001.jpg@01CA473E.14A061C0]
p: 973.560.5370
m: 908.256.3662
e: cheins@...<mailto:cheins@...>
w: www.pny.com<http://www.pny.com/>

_________________________________________________________________________________________________________
NOT INTENDED AS A SUBSTITUTE FOR A WRITING
NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO, SHALL CONSTITUTE A BINDING CONTRACT, OR ANY CONTRACTUAL OBLIGATION BY PNY, OR ANY INTENT TO ENTER INTO ANY BINDING OBLIGATIONS, NOTWITHSTANDING ANY ENACTMENT OF THE UNIFORM ELECTRONIC TRANSACTIONS ACT, THE FEDERAL E-SIGN ACT, OR ANY OTHER STATE OR FEDERAL LAW OF SIMILAR SUBSTANCE OR EFFECT. THIS EMAIL MESSAGE, ITS CONTENTS AND ATTACHMENTS ARE NOT INTENDED TO REPRESENT AN OFFER OR ACCEPTANCE OF AN OFFER TO ENTER INTO A CONTRACT. NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO SHALL ALTER THIS DISCLAIMER.
This e-mail message from PNY Technologies, Inc. (http://www.pny.com) is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.


[Non-text portions of this message have been removed]

I have a .P program that runs when a certain BAM triggers.  It builds the e-mail message dynamically.


The email is HTML based so there's lots of HTML Tags like: <br>, <b></b>, <table></table>, etc...

Since any text that contains a "<" or ">" could break the HTML, I need to convert them to "&lt;" and "&gt;".  There are also some extended characters I replace, like: TM (trademark sym), (C) (copyright sym) , (R) (Registered sym), â€œ and ” (fancy quotes), etc ...


Currently, I have to check each fetch from the DB for each of the possibly offending characters, and do a substitution.  For example:


tempString = substr(ShipDtl.LineDesc,1,25).

tempString  = REPLACE(tempString , ">", "&gt;").

tempString  = REPLACE(tempString , "<", "&lt;").

tempString  = REPLACE(tempString , CHR(49833), "(C)").

tempString  = REPLACE(tempString , CHR(49838), "(R)").

tempString  = REPLACE(tempString , CHR(14844060),CHR(34)).

tempString  = REPLACE(tempString , CHR(14844061),CHR(34)).

tempString  = REPLACE(tempString , CHR(14845090), "(TM)").

NewEmailBody = NewEmailBody + tempString.


And then I have to do all of this again on reading a different field.


Can I declare a function with a .P program?  Something like:

/**** Start of Original .P code ****/

....

tempString = foo(substr(ShipDtl.LineDesc,1,25)).

...

/***** End of original .P code *****/

 

DECLARE foo(s AS CHARACTER) AS CHARACTER){

  s = REPLACE(s, ">", "&gt;").

  s = REPLACE(s, "<", "&lt;").

  ...

  s = REPLACE(s, CHR(14845090), "(TM)").

  RETURN (s).

  }


That possible?  And if so, what is the syntax?


Calvin

 

P.S. I cant just do the check on NewEmailBody once at the end, because it will contain valid "<" and ">" for the HTML tags.


*** SOLVED ****

Guess I should have looked a little harder before posting.  It's explained in the Progress 4GL Reference.

You just need to declare the function, like:
FUNCTION foo RETURNS CHARACTER (s AS CHARACTER):
  s = REPLACE(s, ">", "&gt;").
  s = REPLACE(s, "<", "&lt;").
  s = REPLACE(s, CHR(49833), "&copy;").
  s = REPLACE(s, CHR(49838), "&reg;").
  s = REPLACE(s, CHR(14844060),CHR(34)).
  s = REPLACE(s, CHR(14844061),CHR(34)).
  s = REPLACE(s, CHR(14845090), "&trade;").
  s = REPLACE(s, CHR(14844588), "&euro;").
  RETURN s.
END.