Consultor Eletrônico



Kbase P142684: 4GL/ABL: How to remove a set of undesirable characters from a text or data (.d) file?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   3/12/2009
Status: Verified

GOAL:

4GL/ABL: How to remove a set of undesirable characters from a text or data (.d) file?

GOAL:

How to strip one or more control characters from a text or data (.d) file?

GOAL:

How to strip the CTRL-Z (EOF) characters from a text or data (.d) file?

FACT(s) (Environment):

All Supported Operating Systems
Progress 9.x
OpenEdge 10.x
OpenEdge Category: Language (4GL/ABL)

FIX:

The following procedure removes or strips out a set of undesirable unprintable control characters from data (Customer.d) file. The procedure takes as input any text or data file which may have one or more instances of some undesirable characters as identified by the cUndesirableCharacterList variable and outputs a file with the same contents as the original minus these undesirable characters. See Solution P142767, "4GL/ABL: How to get a listing of all characters in a text or data file?" for specific help on how to generate the list of these undesirable characters.

DEFINE VARIABLE cSourceFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTargetFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE mSourceMemptr AS MEMPTR NO-UNDO.
DEFINE VARIABLE mTargetMemptr AS MEMPTR NO-UNDO.
DEFINE VARIABLE iSourceCounter AS INTEGER NO-UNDO.
DEFINE VARIABLE iTargetCounter AS INTEGER NO-UNDO.
DEFINE VARIABLE iMemptrSize AS INTEGER NO-UNDO.
DEFINE VARIABLE iCurrentByte AS INTEGER NO-UNDO.
DEFINE VARIABLE cUndesirableCharacterList AS CHARACTER NO-UNDO.
ASSIGN
cSourceFileName = "C:\OpenEdge\WRK101B\Customer.d"
cTargetFileName = "C:\OpenEdge\WRK101B\CustomerFixed.d"
cUndesirableCharacterList = "CHR(4),CHR(19),CHR(26)".
RUN LoadFileToMemptr(INPUT cSourceFileName, OUTPUT mSourceMemptr).
ASSIGN
iMemptrSize = GET-SIZE(mSourceMemptr).
SET-SIZE(mTargetMemptr) = iMemptrSize.
DO iSourceCounter = 1 TO iMemptrSize:
iCurrentByte = GET-BYTE( mSourceMemptr , iSourceCounter).
IF LOOKUP(CHR(iCurrentByte),cUndesirableCharacterList, ',') > 0 THEN NEXT.
ELSE
iTargetCounter = iTargetCounter + 1.
PUT-BYTE ( mTargetMemptr , iTargetCounter ) = iCurrentByte.
END.
RUN SaveMemptrToFile(INPUT mTargetMemptr, INPUT cTargetFileName).
PROCEDURE LoadFileToMemptr:
DEFINE INPUT PARAMETER ipcFileName AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER opmMessage AS MEMPTR NO-UNDO.
FILE-INFO:FILE-NAME = ipcFileName.
SET-SIZE(opmMessage) = FILE-INFO:FILE-SIZE.
INPUT FROM VALUE(ipcFileName) BINARY NO-MAP NO-CONVERT.
IMPORT UNFORMATTED opmMessage.
INPUT CLOSE.
END PROCEDURE.
PROCEDURE SaveMemptrToFile:
DEFINE INPUT PARAMETER ipmMessage AS MEMPTR NO-UNDO.
DEFINE INPUT PARAMETER ipcFileName AS CHARACTER NO-UNDO.
OUTPUT TO VALUE(ipcFileName) BINARY NO-MAP NO-CONVERT.
EXPORT ipmMessage.
OUTPUT CLOSE.
END PROCEDURE.