home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / textcvt.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  132 lines

  1. ############################################################################
  2. #
  3. #    File:     textcvt.icn
  4. #
  5. #    Subject:  Program to convert text file formats
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     November 21, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #
  18. #  Program to convert text file(s) among various platforms' formats.
  19. #
  20. #  The supported text file types are UNIX, MS-DOS, and Macintosh.  A
  21. #  universal input text reading algorithm is used, so only the output
  22. #  file format must be specified.
  23. #
  24. #  The files are either converted in-place by converting to a temporary
  25. #  file and copying the result back to the original, or are copied to a
  26. #  separate new file, depending on the command line options.  If the
  27. #  conversion is interrupted, the temporary file might still remain as
  28. #  <original name>.temp (or, for MS-DOS, <original name root>.tmp.
  29. #
  30. ############################################################################
  31. #
  32. #  Links:  io, options
  33. #
  34. ############################################################################
  35.  
  36. link io
  37. link options
  38.  
  39. procedure Usage(s)
  40.   write(&errout,\s)
  41.   stop("Usage: textcvt [-options] -<output format> textfile..._
  42.        \n  options:_
  43.        \n    f <file name>  output file name if different from input_
  44.        \n    o <dir name>   output filename prefix (e.g. directory)_
  45.        \n    c              copy first file to second file_
  46.        \n  <output format>:_
  47.        \n    u: UNIX_
  48.        \n    d: MS-DOS_
  49.        \n    m: Macintosh")
  50. end
  51.  
  52. procedure Options(arg)
  53.   local opt
  54.   opt := options(arg,"udmo:f:c",Usage)
  55.   OutEnder :=
  56.       if \opt["u"] then "\x0a"
  57.       else if \opt["d"] then "\x0d\x0a"
  58.       else if \opt["m"] then "\x0d"
  59.       else Usage()
  60.   OutDir := opt["o"]
  61.   if OutFile := \opt["f"] then {
  62.     if *arg > 1 then Usage("Only one input file allowed with -f")
  63.   }
  64.   else if \opt["c"] then {
  65.     if *arg ~= 2 then Usage("Exactly two files required for -c")
  66.     OutFile := pull(arg)
  67.   }
  68.   return opt
  69. end
  70.  
  71.  
  72. global OutEnder,OutDir,OutFile
  73.  
  74. procedure main(arg)
  75.   local oldName,old,newName,tmp,notInPlace,tmpName
  76.   Options(arg)
  77.   notInPlace := \(OutFile | OutDir)
  78.   every oldName := !arg do {
  79.     old := open(oldName,"ru") | {
  80.       write(&errout,"Can't open ",oldName)
  81.       next
  82.     }
  83.     if \notInPlace then {
  84.       tmpName := (\OutDir | "") || (\OutFile | tail(oldName)[2])
  85.       tmp := open(tmpName,"wu") | {
  86.         write(&errout,"Can't open output file ",tmpName)
  87.     close(old)
  88.     next
  89.       }
  90.       writes(&errout,"Converting ",oldName," -> ",tmpName," -- ")
  91.     }
  92.     else {
  93.       tmpName := if match("MS_DOS",&host) then suffix(oldName)[1] || ".tmp"
  94.           else oldName || ".temp"
  95.       tmp := open(tmpName,"wu") | {
  96.         write(&errout,"Can't open work file ",tmpName)
  97.     close(old)
  98.     next
  99.       }
  100.       writes(&errout,"Converting ",oldName," -- ")
  101.     }
  102.     flush(&errout)
  103.     ConvertText(old,tmp)
  104.     close(tmp)
  105.     close(old)
  106.     if \notInPlace then {
  107.       write(&errout,"done.")
  108.     }
  109.     else {
  110.       (fcopy(tmpName,oldName) & write(&errout,"done.")) |
  111.           write(&errout,"done.")
  112.       remove(tmpName)
  113.     }
  114.   }
  115. end
  116.  
  117. procedure ConvertText(old,new)
  118.   local buf,c,trail
  119.   while buf := reads(old,2000) do {
  120.     if buf[-1] == "\x0d" then buf ||:= reads(old)
  121.     buf ? {
  122.       while writes(new,tab(upto('\x0a\x0d')),OutEnder) do {
  123.         c := move(1)
  124.     if c == "\x0d" then ="\x0a"
  125.       }
  126.       writes(new,trail := tab(0))
  127.     }
  128.   }
  129.   if *\trail > 0 then writes(new,OutEnder)
  130.   return
  131. end
  132.