home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / ADJ78.ZIP / ADJUST78.CMD
OS/2 REXX Batch file  |  1992-10-31  |  12KB  |  471 lines

  1. /* ADJUST78.CMD 1.21 */
  2. /* 31-10-92 */
  3. /* Olaf Koch, Compuserve 100010,2732 */
  4. /* Feel free to distribute */
  5.  
  6. /*───────────────────────────────────────────────────────────────────────────*/
  7.  
  8. /* Functionality
  9.  
  10.    - restricts any line in an ASCII file to a maximum of 78 characters
  11.      (useful for laser printer output); optional
  12.    - translates German special character set Ä,Ö,Ü,ä,ö,ü,ß into ASCII
  13.      characters below 129 (table ID 1); optional
  14.    - reverse translation into German special character set Ä,Ö,Ü,ä,ö,ü,ß
  15.      (table ID 2); optional - ATTENTION: this might translate more than you
  16.      want
  17.    - add your own character translation set in procedure ReadCharTable
  18.      with a unique table ID
  19.    - the paragraph formatting cannot be preserved in all cases; if a line is
  20.      longer than 78 character the remaining part of the line is merged
  21.      with the next one if the next line contains more than 1 character and does
  22.      not begin with a blank (e.g. to mark space between two paragraphs or
  23.      to initiate special treatment by CIS formatting software)
  24.    - only FAT names are allowed
  25.    - this code is not very optimized yet (as you might see) */
  26.  
  27.  
  28. /*───────────────────────────────────────────────────────────────────────────*/
  29.  
  30. /* Used Procedures
  31.  
  32.    (Main)
  33.    ProcessFile
  34.    CommandLine
  35.    ReadCharTable
  36.    Replace
  37.    Syntax
  38.    FileNameCheck */
  39.  
  40. /*───────────────────────────────────────────────────────────────────────────*/
  41.  
  42. /* Changelist
  43.  
  44.    1.21 - 31-10-92
  45.         - filenames are checked for FAT name conventions
  46.         - bug corrected: ADJUST78 didn't translate characters if they were
  47.           part of a merged line (the combination of the remaining part of
  48.           a line which is longer than 78 characters and the next one which
  49.           gets appended)
  50.         - GCP saves an empty line with a leading blank (due to CIS formatting
  51.           software); I don't merge lines with leading blanks anymore to
  52.           preserve the intended format of a message
  53.         - if two lines are merged, ADJUST78 checks for two following blanks
  54.           (and converts them into only one blank)
  55.    1.20 - 30-10-92
  56.         - first released version */
  57.  
  58. /*───────────────────────────────────────────────────────────────────────────*/
  59.  
  60. /* (Main) procedure */
  61.  
  62. /* no echo please */
  63. '@ECHO OFF'
  64. 'CLS'
  65.  
  66. version='ADJUST78 1.21'
  67. debug=0
  68.  
  69. /* load REXXUTIL functions */
  70. CALL rxfuncadd sysloadfuncs, rexxutil, sysloadfuncs
  71. CALL sysloadfuncs
  72.  
  73. /* write commandline parameters in variable 'parameter' */
  74. PARSE ARG parameter
  75. /* split parameter into i variables parameter.i */
  76. CALL commandline parameter
  77.  
  78. /* no matching files found */
  79. IF files.0 = 0 THEN
  80.   CALL Syntax 0
  81.  
  82. /* check tableID parameter */
  83. tableID=parameter.2
  84. IF tableID <> 1 & tableID <> 2 THEN
  85.   tableID=0
  86.  
  87. /* check -no78 parameter */
  88. no78=parameter.3
  89. IF no78 <> '-no78' THEN
  90.   /* line adjusting */
  91.   no78=0
  92. ELSE
  93.   /* no line adjusting */
  94.   DO
  95.     no78=1
  96.     /* it doesn't make sense to do neither adjusting nor translating */
  97.     IF tableID=0 THEN
  98.       CALL Syntax 0
  99.   END
  100.  
  101. /* read character translation set into a stem variable */
  102. IF tableID <> 0 THEN
  103.   DO
  104.     CALL ReadCharTable tableID
  105.     /* list character translation set */
  106.     SAY
  107.     SAY version
  108.     SAY
  109.     SAY "Character translation set" tableID
  110.     SAY
  111.     /* loop to dispatch characters from stem */
  112.     DO i = 1 TO charnumber
  113.       SAY oldchar.i '->' newchar.i
  114.     END
  115.   END
  116.  
  117. /* loop to process each file found in the file name mask */
  118. DO x=1 TO files.0
  119.   inputfile=files.x
  120.   CALL ProcessFile
  121. END
  122.  
  123. RETURN /* Main */
  124.  
  125. /*───────────────────────────────────────────────────────────────────────────*/
  126.  
  127. /* Procedure to process one file at a time */
  128. ProcessFile:
  129.  
  130. /* get time information for both time format output and seconds arithemetics */
  131. starttime=TIME()
  132. starttimesec=TIME("S")
  133.  
  134. /* get unique name for the temporary file */
  135. tempfile=SysTempFileName('adjust78.???')
  136.  
  137. /* provide processing information */
  138. SAY
  139. SAY version
  140. SAY
  141. SAY "Started at" starttime'.'
  142. SAY TRANSLATE(inputfile) "being processed."
  143. IF tableID = 0 THEN
  144.   SAY "No character translation set being used."
  145. ELSE
  146.   SAY "Character translation set" tableID "being used."
  147. IF no78 = 1 THEN
  148.   SAY "No line adjustment."
  149. ELSE
  150.   SAY "Lines being cut down to max. 78 characters."
  151.  
  152. /* set name of backup file */
  153. /* valid FAT name? */
  154. rc=FileNameCheck(inputfile)
  155. IF rc=1 THEN
  156.   DO
  157.     /* no FAT name */
  158.     SAY 'ERROR:' inputfile 'is not a valid FAT filename.'
  159.     RETURN
  160.   END
  161.  
  162. IF debug=1 THEN
  163.   DO
  164.     say drivedir
  165.     say filename
  166.  
  167.     say lengthfilename
  168.     say lengthfirstpart
  169.     say lengthlastpart
  170.     say firstdot
  171.     say lastdot
  172.  
  173.     say inputfile
  174.     say backupfile
  175.     pull
  176.     return
  177.   END
  178.  
  179. /* read all lines in a stem variable, translate character set */
  180. DO i=1 BY 1 WHILE LINES(inputfile) <> 0
  181.   PARSE VALUE LINEIN(inputfile) WITH lines.i
  182.   IF tableID <> 0 THEN
  183.     DO k=1 TO charnumber
  184.       lines.i=Replace(lines.i, oldchar.k, newchar.k)
  185.     END
  186. END
  187. /* close input file */
  188. RC=LINEOUT(inputfile)
  189. linecount=i-1
  190. newlinecount=linecount
  191.  
  192. /* loop: process lines */
  193. newlinestemcount=1
  194. i=1
  195. DO WHILE i <= linecount
  196.  
  197.   /* read line*/
  198.   inputline=lines.i
  199.  
  200.   IF no78 = 0 THEN
  201.     /* line adjustment */
  202.     DO
  203.       /* split line */
  204.       DO WHILE LENGTH(inputline) > 78
  205.  
  206.         /* get last blank in 78 first characters */
  207.         lastblank=LASTPOS(" ",inputline,78)
  208.         IF lastblank=0 THEN
  209.           lastblank=78
  210.         /* get actual chunk of line */
  211.         inputlineout=SUBSTR(inputline,1,lastblank)
  212.         /* cut variable 'inputline' */
  213.         inputline=SUBSTR(inputline,lastblank+1)
  214.  
  215.         /* write chunk into stem 'newlines'*/
  216.         newlines.newlinestemcount=inputlineout
  217.         newlinestemcount=newlinestemcount+1
  218.  
  219. IF debug=1 THEN
  220.   DO
  221.     say i
  222.     say lines.i
  223.     say length(lines.i)
  224.     pull
  225.   END
  226.  
  227.         /* merge two lines or not? */
  228.         /* jump 1 in whilecounter */
  229.         i=i+1
  230.         IF LENGTH(lines.i) = 0 | SUBSTR(lines.i,1,1) = ' ' THEN
  231.           DO
  232.             i=i-1
  233.             /* don't add next line to current line if
  234.                a) next line is empty OR
  235.                b) next line begins with a blank */
  236.           END
  237.         ELSE
  238.           DO
  239.             /* cut trailing blanks in old line */
  240.             inputline=STRIP(inputline,"T")
  241.             /* cut leading blanks in new line */
  242.             lines.i=STRIP(lines.i,"L")
  243.             /* add next line */
  244.             inputline=inputline' 'lines.i
  245.           END
  246.       END
  247.     END
  248.  
  249.   /* (remaining) line is shorter than 79 characters or no78=1 specified */
  250.   newlines.newlinestemcount=inputline
  251.   newlinestemcount=newlinestemcount+1
  252.   i=i+1
  253. END
  254. /* endloop: no more lines */
  255.  
  256. /* write output file */
  257. DO y=1 TO (newlinestemcount-1)
  258.   RC=LINEOUT(tempfile,newlines.y)
  259. END
  260.  
  261. /* close temporary output file */
  262. RC=LINEOUT(tempfile)
  263.  
  264. /* make backup */
  265. 'copy' inputfile backupfile '>nul'
  266. /* replace input file */
  267. 'copy' tempfile inputfile '>nul'
  268. /* delete temporary file */
  269. 'del' tempfile '>nul'
  270.  
  271. /* read all lines in a stem variable */
  272. DO i=1 BY 1 WHILE LINES(inputfile) <> 0
  273.   PARSE VALUE LINEIN(inputfile) with lines.i
  274. END
  275. /* close input file */
  276. RC=LINEOUT(inputfile)
  277. newlinecount=i-1
  278.  
  279. IF (newlinecount-linecount) <> 0 THEN
  280.   DO
  281.     IF (newlinecount-linecount) = 1 THEN
  282.       SAY "1 line has been added."
  283.     ELSE
  284.       SAY (newlinecount-linecount) "lines have been added."
  285.   END
  286. ELSE
  287.   SAY "No lines have been added."
  288. endtime=TIME()
  289. endtimesec=TIME("S")
  290. SAY "Ended at" endtime'.'
  291. SAY "Processing lasted" (endtimesec-starttimesec)' second(s).'
  292. RETURN
  293.  
  294. /*───────────────────────────────────────────────────────────────────────────*/
  295.  
  296. /* write commandline parameters into parameter.i */
  297. CommandLine:
  298.  
  299. string=ARG(1)
  300. DO i=1 TO WORDS(string)
  301.   parameter.i=SUBWORD(string,i,1)
  302. END
  303. /* get all files specified in the file mask, only filename */
  304. RC=SysFileTree(parameter.1,files,"FO")
  305. IF parameter.1='-h' THEN
  306.   CALL Syntax 1
  307. RETURN
  308.  
  309. /*───────────────────────────────────────────────────────────────────────────*/
  310.  
  311. /* make stem variable with character translation sets */
  312. ReadCharTable:
  313.  
  314. tableID=ARG(1)
  315.  
  316. SELECT
  317.   WHEN tableID=0 THEN
  318.     NOP
  319.   WHEN tableID=1 THEN
  320.     DO
  321.       charnumber=7
  322.       oldchar.1="Ä"
  323.       oldchar.2="Ö"
  324.       oldchar.3="Ü"
  325.       oldchar.4="ä"
  326.       oldchar.5="ö"
  327.       oldchar.6="ü"
  328.       oldchar.7="ß"
  329.       newchar.1="Ae"
  330.       newchar.2="Oe"
  331.       newchar.3="Ue"
  332.       newchar.4="ae"
  333.       newchar.5="oe"
  334.       newchar.6="ue"
  335.       newchar.7="ss"
  336.     END
  337.   WHEN tableID=2 THEN
  338.     DO
  339.       charnumber=7
  340.       oldchar.1="Ae"
  341.       oldchar.2="Oe"
  342.       oldchar.3="Ue"
  343.       oldchar.4="ae"
  344.       oldchar.5="oe"
  345.       oldchar.6="ue"
  346.       oldchar.7="ss"
  347.       newchar.1="Ä"
  348.       newchar.2="Ö"
  349.       newchar.3="Ü"
  350.       newchar.4="ä"
  351.       newchar.5="ö"
  352.       newchar.6="ü"
  353.       newchar.7="ß"
  354.     END
  355.   OTHERWISE
  356.     DO
  357.       SAY "Error: Unknown character set"
  358.       EXIT
  359.     END
  360. END /* of SELECT */
  361. RETURN
  362.  
  363. /*───────────────────────────────────────────────────────────────────────────*/
  364.  
  365. /* replace characters */
  366. Replace:
  367.  
  368. string=ARG(1)
  369. oldstring=ARG(2)
  370. newstring=ARG(3)
  371.  
  372. oldstringlen=LENGTH(oldstring)
  373. newstringlen=LENGTH(newstring)
  374. difflen=newstringlen-oldstringlen
  375.  
  376. /* it is necessary to insert blanks? */
  377. IF difflen > 0 THEN
  378.   fillstring=COPIES(" ",difflen)
  379.  
  380. start=1
  381. DO WHILE POS(oldstring,string,start) <> 0
  382.   /* where is the old character? */
  383.   foundpos=POS(oldstring,string,start)
  384.   /* it is necessary to insert blanks? */
  385.   IF difflen > 0 THEN
  386.     string=INSERT(fillstring,string,foundpos)
  387.   IF difflen < 0 THEN
  388.     string=DELSTR(string,foundpos+1,1)
  389.   /* overlay the new character */
  390.   string=OVERLAY(newstring,string,foundPos)
  391.   /* search the rest of the line */
  392.   start=foundPos
  393. END
  394. RETURN(string)
  395.  
  396. /*───────────────────────────────────────────────────────────────────────────*/
  397.  
  398. /* display Syntax help */
  399. Syntax:
  400.  
  401. /* show character sets (1) or not (0) */
  402. showchar=ARG(1)
  403.  
  404. SAY
  405. SAY version
  406. SAY
  407. SAY 'ADJUST78 input.ext [tableID [-no78]]'
  408. SAY 'ADJUST78 -h'
  409. SAY
  410. SAY 'input.ext - File(s) to be processed.'
  411. SAY '            Original file is saved as input.bak.'
  412. SAY '            Wildcard use allowed. Only FAT filenames allowed.'
  413. SAY 'tableID   - Specifies the character translation set:'
  414. SAY '              0 - no translation at all'
  415. DO i=1 TO 2
  416.   CALL ReadCharTable i
  417.   SAY '              'i' - translation set' i
  418.   IF showchar=1 THEN
  419.     DO
  420.       DO j = 1 TO charnumber
  421.         SAY '                    'oldchar.j '->' newchar.j
  422.       END
  423.       SAY
  424.       SAY 'Press ENTER to continue...'
  425.       PULL
  426.     END
  427. END
  428. SAY '-no78     - Perform no line splitting.'
  429. SAY '            Use only in conjunction with tableID > 0.'
  430. SAY '-h        - Show extended help with character sets.'
  431. SAY
  432. EXIT
  433.  
  434. /*───────────────────────────────────────────────────────────────────────────*/
  435.  
  436. /* valid FAT name? */
  437. FileNameCheck:
  438.  
  439. filename=ARG(1)
  440.  
  441. /* cut name down to filename without drive and directory */
  442. lastbackslash=LASTPOS('\',filename)
  443. drivedir=SUBSTR(filename,1,lastbackslash-1)
  444. filename=SUBSTR(filename,lastbackslash+1,LENGTH(filename)-lastbackslash)
  445.  
  446. lengthfilename=LENGTH(filename)
  447. firstdot=POS('.',filename)
  448. lastdot=LASTPOS('.',filename)
  449. IF firstdot=0 THEN
  450.   DO
  451.     lengthfirstpart=LENGTH(SUBSTR(filename,1))
  452.     lengthlastpart=0
  453.   END
  454. ELSE
  455.   DO
  456.     lengthfirstpart=LENGTH(SUBSTR(filename,1,firstdot-1))
  457.     lengthlastpart=LENGTH(SUBSTR(filename,lastdot+1,lengthfilename-lastdot))
  458.   END
  459.  
  460. IF lengthfilename <= 12 & ,,
  461.    lengthfirstpart <= 8 & ,,
  462.    lastdot = firstdot   & ,,
  463.    lengthlastpart <= 3  THEN
  464.   IF firstdot=0 THEN
  465.     backupfile=drivedir'\'SUBSTR(filename,1)'.BAK'
  466.   ELSE
  467.     backupfile=drivedir'\'SUBSTR(filename,1,firstdot-1)'.BAK'
  468. ELSE
  469.   RETURN(1)
  470. RETURN(0)
  471.