|
On 12/13/06, James Rich <james@xxxxxxxxxxx> wrote:
I've written some code which has some bugs. I think I need to scrap the whole thing and start over with a fresh approach. I need to replace tags in a string with another string. So I'm looking for ideas on the best ways to accomplish this. Here's an example of what the original string might look like: This string is <tag 1>an example string</tag 1> that I made up. My code needs to replace <tag 1> with some string (passed to it is beginstring) and replace </tag 1> with some string (passed as endstring). I need to catch errors like opening tags that don't have closing tags and opening tags that occur before a previous opening tag's matching end tag. I'm almost certain this is something that lots of RPG programmers have done before. My code sucks. How do you do it?
Hi James, I would start with a control struct that encapsulates a tag in a string: d refTag ds qualified d bx 10i 0 d Lx 10i 0 d tagType 1a constants that enumerate the tagType: d conTagType_Begin c '1' d conTagType_End c '2' d conTagType_eof c '3' d conTagType_bof c '4' a proc that constructs a tag: d tag_Construct pr likeds(refTag) d extproc(''tag_Construct') d InTagType 1a const a procedure to scan for the next tag: d tag_ScanNext pr likeds(refTag) d extproc('tag_ScanNext') d InString 32000 a varying options(*varsize) const d InCurTag const likeds(refTag) d options(*omit) this proc starts the scan from the current tag and returns the refTag struct that represents the next tag. here is that basic loop that steps from tag to tag in the string /free tag = tags_Construct( conTagType_bof ) ; dow conTrue ; curTag = tag ; tag = tag_ScanNext( sText: curTag ) ; select ; when tag.TagType = conTagType_eof ; leave ; when curTag.TagType = conTagType_begin and tag.TagType <> conTagType_end ; // this is an error. begin tag not followed by end tag when curTag.TagType = conTagType_bof and tag.TagType = conTagType_end ; // error. end tag not preceeded by begin tag when tag.TagType = conTagType_begin and curTag.TagType = conTagType_bof ; iter ; // got the begin tag. now loop back and get the end tag. when curTag.TagType = conTagType_begin and tag.TagType = conTagType_end RenameTag( sText: curTag: tag ) ; endsl ; enddo ; to rename the key in both the begin and end tag: d tag_RenameKey pr d extproc('tag_RenameKey') d InString 32000 a varying options(*varsize) d InBeginTag likeds(refTag) d InEndTag likeds(refTag) d InNewKey 80a const varying this proc can rename the keys in the tags because it gets the string the tags are contained in and the refTag structs of the begin and end tag that identifiy the location of the tags in the string. good luck! hope it helps. -Steve
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.