home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / netrexx.zip / NetRexx / linecomment.nrx < prev    next >
Text File  |  1998-02-14  |  2KB  |  60 lines

  1. /* linecomment.nrx -- convert appropriate block comments to line comments */
  2.  
  3. /* This is a sample file input and output program, showing how to open,
  4.    check, and process text files, and handle exceptions.
  5.    Note the use of the Reader and Writer classes, which convert your
  6.    local computer's 'code page' (character encoding) to Unicode during
  7.    reading and back again during writing. */
  8.  
  9. parse arg fin fout .   -- get the arguments: input and output files
  10. if fout='' then do
  11.   say '# Please specify both input and output files'
  12.   exit 1
  13. end
  14.  
  15. /* Open and check the files */
  16. do
  17.   infile=File(fin)
  18.   instream=FileInputStream(infile)
  19.   inhandle=BufferedReader(InputStreamReader(instream))
  20.   outfile=File(fout)
  21.   if outfile.getAbsolutePath=infile.getAbsolutePath then do
  22.     say '# Input file cannot be used as the output file'
  23.     exit 1
  24.   end
  25.   outstream=FileOutputStream(outfile)
  26.   outhandle=OutputStreamWriter(outstream)
  27.   say 'Processing' infile'...'
  28. catch e=IOException
  29.   say '# error opening file' e.getMessage
  30. end
  31.  
  32. linesep=System.getProperty('line.separator') -- be platform-neutral
  33.  
  34. /* The main processing loop */
  35. loop linenum=1 by 1
  36.   line=Rexx inhandle.readLine           -- get next line [as Rexx string]
  37.   if line=null then leave linenum       -- normal end of file
  38.  
  39.   parse line pre '/*' mid '*/' post     -- process the line
  40.   if pre\='' then
  41.    if mid\='' then
  42.    if post=='' then
  43.     line=pre'--'mid
  44.  
  45.   if linenum>1 then outhandle.write(linesep, 0, linesep.length)
  46.   outhandle.write(line, 0, line.length)
  47. catch e=IOException
  48.   say '# error reading or writing file' e.getMessage
  49. catch Exception
  50.   say '# processing ended'
  51. finally do                              -- close files
  52.     if inhandle\=null  then inhandle.close
  53.     if outhandle\=null then outhandle.close
  54.   catch IOException
  55.     -- ignore errors during close
  56.   end
  57. end linenum
  58.  
  59. say linenum-1 'lines written'
  60.