home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Professional
/
OS2PRO194.ISO
/
os2
/
print
/
adjust
/
adjust78.cmd
Wrap
OS/2 REXX Batch file
|
1994-02-03
|
12KB
|
471 lines
/* ADJUST78.CMD 1.21 */
/* 31-10-92 */
/* Olaf Koch, Compuserve 100010,2732 */
/* Feel free to distribute */
/*───────────────────────────────────────────────────────────────────────────*/
/* Functionality
- restricts any line in an ASCII file to a maximum of 78 characters
(useful for laser printer output); optional
- translates German special character set Ä,Ö,Ü,ä,ö,ü,ß into ASCII
characters below 129 (table ID 1); optional
- reverse translation into German special character set Ä,Ö,Ü,ä,ö,ü,ß
(table ID 2); optional - ATTENTION: this might translate more than you
want
- add your own character translation set in procedure ReadCharTable
with a unique table ID
- the paragraph formatting cannot be preserved in all cases; if a line is
longer than 78 character the remaining part of the line is merged
with the next one if the next line contains more than 1 character and does
not begin with a blank (e.g. to mark space between two paragraphs or
to initiate special treatment by CIS formatting software)
- only FAT names are allowed
- this code is not very optimized yet (as you might see) */
/*───────────────────────────────────────────────────────────────────────────*/
/* Used Procedures
(Main)
ProcessFile
CommandLine
ReadCharTable
Replace
Syntax
FileNameCheck */
/*───────────────────────────────────────────────────────────────────────────*/
/* Changelist
1.21 - 31-10-92
- filenames are checked for FAT name conventions
- bug corrected: ADJUST78 didn't translate characters if they were
part of a merged line (the combination of the remaining part of
a line which is longer than 78 characters and the next one which
gets appended)
- GCP saves an empty line with a leading blank (due to CIS formatting
software); I don't merge lines with leading blanks anymore to
preserve the intended format of a message
- if two lines are merged, ADJUST78 checks for two following blanks
(and converts them into only one blank)
1.20 - 30-10-92
- first released version */
/*───────────────────────────────────────────────────────────────────────────*/
/* (Main) procedure */
/* no echo please */
'@ECHO OFF'
'CLS'
version='ADJUST78 1.21'
debug=0
/* load REXXUTIL functions */
CALL rxfuncadd sysloadfuncs, rexxutil, sysloadfuncs
CALL sysloadfuncs
/* write commandline parameters in variable 'parameter' */
PARSE ARG parameter
/* split parameter into i variables parameter.i */
CALL commandline parameter
/* no matching files found */
IF files.0 = 0 THEN
CALL Syntax 0
/* check tableID parameter */
tableID=parameter.2
IF tableID <> 1 & tableID <> 2 THEN
tableID=0
/* check -no78 parameter */
no78=parameter.3
IF no78 <> '-no78' THEN
/* line adjusting */
no78=0
ELSE
/* no line adjusting */
DO
no78=1
/* it doesn't make sense to do neither adjusting nor translating */
IF tableID=0 THEN
CALL Syntax 0
END
/* read character translation set into a stem variable */
IF tableID <> 0 THEN
DO
CALL ReadCharTable tableID
/* list character translation set */
SAY
SAY version
SAY
SAY "Character translation set" tableID
SAY
/* loop to dispatch characters from stem */
DO i = 1 TO charnumber
SAY oldchar.i '->' newchar.i
END
END
/* loop to process each file found in the file name mask */
DO x=1 TO files.0
inputfile=files.x
CALL ProcessFile
END
RETURN /* Main */
/*───────────────────────────────────────────────────────────────────────────*/
/* Procedure to process one file at a time */
ProcessFile:
/* get time information for both time format output and seconds arithemetics */
starttime=TIME()
starttimesec=TIME("S")
/* get unique name for the temporary file */
tempfile=SysTempFileName('adjust78.???')
/* provide processing information */
SAY
SAY version
SAY
SAY "Started at" starttime'.'
SAY TRANSLATE(inputfile) "being processed."
IF tableID = 0 THEN
SAY "No character translation set being used."
ELSE
SAY "Character translation set" tableID "being used."
IF no78 = 1 THEN
SAY "No line adjustment."
ELSE
SAY "Lines being cut down to max. 78 characters."
/* set name of backup file */
/* valid FAT name? */
rc=FileNameCheck(inputfile)
IF rc=1 THEN
DO
/* no FAT name */
SAY 'ERROR:' inputfile 'is not a valid FAT filename.'
RETURN
END
IF debug=1 THEN
DO
say drivedir
say filename
say lengthfilename
say lengthfirstpart
say lengthlastpart
say firstdot
say lastdot
say inputfile
say backupfile
pull
return
END
/* read all lines in a stem variable, translate character set */
DO i=1 BY 1 WHILE LINES(inputfile) <> 0
PARSE VALUE LINEIN(inputfile) WITH lines.i
IF tableID <> 0 THEN
DO k=1 TO charnumber
lines.i=Replace(lines.i, oldchar.k, newchar.k)
END
END
/* close input file */
RC=LINEOUT(inputfile)
linecount=i-1
newlinecount=linecount
/* loop: process lines */
newlinestemcount=1
i=1
DO WHILE i <= linecount
/* read line*/
inputline=lines.i
IF no78 = 0 THEN
/* line adjustment */
DO
/* split line */
DO WHILE LENGTH(inputline) > 78
/* get last blank in 78 first characters */
lastblank=LASTPOS(" ",inputline,78)
IF lastblank=0 THEN
lastblank=78
/* get actual chunk of line */
inputlineout=SUBSTR(inputline,1,lastblank)
/* cut variable 'inputline' */
inputline=SUBSTR(inputline,lastblank+1)
/* write chunk into stem 'newlines'*/
newlines.newlinestemcount=inputlineout
newlinestemcount=newlinestemcount+1
IF debug=1 THEN
DO
say i
say lines.i
say length(lines.i)
pull
END
/* merge two lines or not? */
/* jump 1 in whilecounter */
i=i+1
IF LENGTH(lines.i) = 0 | SUBSTR(lines.i,1,1) = ' ' THEN
DO
i=i-1
/* don't add next line to current line if
a) next line is empty OR
b) next line begins with a blank */
END
ELSE
DO
/* cut trailing blanks in old line */
inputline=STRIP(inputline,"T")
/* cut leading blanks in new line */
lines.i=STRIP(lines.i,"L")
/* add next line */
inputline=inputline' 'lines.i
END
END
END
/* (remaining) line is shorter than 79 characters or no78=1 specified */
newlines.newlinestemcount=inputline
newlinestemcount=newlinestemcount+1
i=i+1
END
/* endloop: no more lines */
/* write output file */
DO y=1 TO (newlinestemcount-1)
RC=LINEOUT(tempfile,newlines.y)
END
/* close temporary output file */
RC=LINEOUT(tempfile)
/* make backup */
'copy' inputfile backupfile '>nul'
/* replace input file */
'copy' tempfile inputfile '>nul'
/* delete temporary file */
'del' tempfile '>nul'
/* read all lines in a stem variable */
DO i=1 BY 1 WHILE LINES(inputfile) <> 0
PARSE VALUE LINEIN(inputfile) with lines.i
END
/* close input file */
RC=LINEOUT(inputfile)
newlinecount=i-1
IF (newlinecount-linecount) <> 0 THEN
DO
IF (newlinecount-linecount) = 1 THEN
SAY "1 line has been added."
ELSE
SAY (newlinecount-linecount) "lines have been added."
END
ELSE
SAY "No lines have been added."
endtime=TIME()
endtimesec=TIME("S")
SAY "Ended at" endtime'.'
SAY "Processing lasted" (endtimesec-starttimesec)' second(s).'
RETURN
/*───────────────────────────────────────────────────────────────────────────*/
/* write commandline parameters into parameter.i */
CommandLine:
string=ARG(1)
DO i=1 TO WORDS(string)
parameter.i=SUBWORD(string,i,1)
END
/* get all files specified in the file mask, only filename */
RC=SysFileTree(parameter.1,files,"FO")
IF parameter.1='-h' THEN
CALL Syntax 1
RETURN
/*───────────────────────────────────────────────────────────────────────────*/
/* make stem variable with character translation sets */
ReadCharTable:
tableID=ARG(1)
SELECT
WHEN tableID=0 THEN
NOP
WHEN tableID=1 THEN
DO
charnumber=7
oldchar.1="Ä"
oldchar.2="Ö"
oldchar.3="Ü"
oldchar.4="ä"
oldchar.5="ö"
oldchar.6="ü"
oldchar.7="ß"
newchar.1="Ae"
newchar.2="Oe"
newchar.3="Ue"
newchar.4="ae"
newchar.5="oe"
newchar.6="ue"
newchar.7="ss"
END
WHEN tableID=2 THEN
DO
charnumber=7
oldchar.1="Ae"
oldchar.2="Oe"
oldchar.3="Ue"
oldchar.4="ae"
oldchar.5="oe"
oldchar.6="ue"
oldchar.7="ss"
newchar.1="Ä"
newchar.2="Ö"
newchar.3="Ü"
newchar.4="ä"
newchar.5="ö"
newchar.6="ü"
newchar.7="ß"
END
OTHERWISE
DO
SAY "Error: Unknown character set"
EXIT
END
END /* of SELECT */
RETURN
/*───────────────────────────────────────────────────────────────────────────*/
/* replace characters */
Replace:
string=ARG(1)
oldstring=ARG(2)
newstring=ARG(3)
oldstringlen=LENGTH(oldstring)
newstringlen=LENGTH(newstring)
difflen=newstringlen-oldstringlen
/* it is necessary to insert blanks? */
IF difflen > 0 THEN
fillstring=COPIES(" ",difflen)
start=1
DO WHILE POS(oldstring,string,start) <> 0
/* where is the old character? */
foundpos=POS(oldstring,string,start)
/* it is necessary to insert blanks? */
IF difflen > 0 THEN
string=INSERT(fillstring,string,foundpos)
IF difflen < 0 THEN
string=DELSTR(string,foundpos+1,1)
/* overlay the new character */
string=OVERLAY(newstring,string,foundPos)
/* search the rest of the line */
start=foundPos
END
RETURN(string)
/*───────────────────────────────────────────────────────────────────────────*/
/* display Syntax help */
Syntax:
/* show character sets (1) or not (0) */
showchar=ARG(1)
SAY
SAY version
SAY
SAY 'ADJUST78 input.ext [tableID [-no78]]'
SAY 'ADJUST78 -h'
SAY
SAY 'input.ext - File(s) to be processed.'
SAY ' Original file is saved as input.bak.'
SAY ' Wildcard use allowed. Only FAT filenames allowed.'
SAY 'tableID - Specifies the character translation set:'
SAY ' 0 - no translation at all'
DO i=1 TO 2
CALL ReadCharTable i
SAY ' 'i' - translation set' i
IF showchar=1 THEN
DO
DO j = 1 TO charnumber
SAY ' 'oldchar.j '->' newchar.j
END
SAY
SAY 'Press ENTER to continue...'
PULL
END
END
SAY '-no78 - Perform no line splitting.'
SAY ' Use only in conjunction with tableID > 0.'
SAY '-h - Show extended help with character sets.'
SAY
EXIT
/*───────────────────────────────────────────────────────────────────────────*/
/* valid FAT name? */
FileNameCheck:
filename=ARG(1)
/* cut name down to filename without drive and directory */
lastbackslash=LASTPOS('\',filename)
drivedir=SUBSTR(filename,1,lastbackslash-1)
filename=SUBSTR(filename,lastbackslash+1,LENGTH(filename)-lastbackslash)
lengthfilename=LENGTH(filename)
firstdot=POS('.',filename)
lastdot=LASTPOS('.',filename)
IF firstdot=0 THEN
DO
lengthfirstpart=LENGTH(SUBSTR(filename,1))
lengthlastpart=0
END
ELSE
DO
lengthfirstpart=LENGTH(SUBSTR(filename,1,firstdot-1))
lengthlastpart=LENGTH(SUBSTR(filename,lastdot+1,lengthfilename-lastdot))
END
IF lengthfilename <= 12 & ,,
lengthfirstpart <= 8 & ,,
lastdot = firstdot & ,,
lengthlastpart <= 3 THEN
IF firstdot=0 THEN
backupfile=drivedir'\'SUBSTR(filename,1)'.BAK'
ELSE
backupfile=drivedir'\'SUBSTR(filename,1,firstdot-1)'.BAK'
ELSE
RETURN(1)
RETURN(0)