TestCopy.mod 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. MODULE TestCopy;
  2. (* Copy a textfile with ISO-Modula-2 I/O-Library
  3. reading the in- and out-file specification
  4. as parameter from the command line.
  5. Example: The OpenVMS command
  6. $ run testcopy
  7. $_ x.lis y.lis
  8. copies file x.lis to file y.lis.
  9. When installed as a foreign command, the arguments
  10. can be specified in the command line:
  11. $ testcopy :== $dev:[dir]testcopy.exe
  12. $ testcopy x.lis y.lis
  13. Copyright (1993-1996) by Guenter Dotzel
  14. http://www.modulaware.com/
  15. GD/1993
  16. *)
  17. FROM ProgramArgs IMPORT IsArgPresent, ArgChan;
  18. FROM IOResult IMPORT ReadResults, ReadResult;
  19. FROM SeqFile IMPORT OpenRead, OpenWrite, OpenResults,
  20. Close, ChanId, read, write;
  21. FROM TextIO IMPORT ReadRestLine, SkipLine, ReadToken,
  22. WriteLn, WriteString;
  23. IMPORT STextIO, SWholeIO;
  24. TYPE fnam= ARRAY [0..254] OF CHAR;
  25. VAR in,out: fnam;
  26. ores: OpenResults;
  27. input, output: ChanId;
  28. line: ARRAY[0..8191] OF CHAR;
  29. lines: INTEGER;
  30. PROCEDURE usage;
  31. BEGIN
  32. STextIO.WriteString("TestCopy usage: 'testcopy IN.EXT OUT.EXT'");
  33. STextIO.WriteLn;
  34. END usage;
  35. BEGIN
  36. lines:=0;
  37. IF IsArgPresent() THEN
  38. ReadToken(ArgChan(), in);
  39. IF IsArgPresent() THEN
  40. ReadToken(ArgChan(), out);
  41. OpenRead(input, in, read, ores);
  42. IF ores=opened THEN
  43. OpenWrite(output, out, write, ores);
  44. IF ores=opened THEN
  45. ReadRestLine(input, line);
  46. WHILE ReadResult(input) <> endOfInput DO
  47. IF ReadResult(input) = allRight THEN
  48. WriteString(output, line); WriteLn(output);
  49. INC(lines);
  50. ELSIF ReadResult(input) = endOfLine THEN
  51. WriteLn(output);
  52. INC(lines);
  53. ELSE
  54. STextIO.WriteString("TestCopy error reading infile");
  55. STextIO.WriteLn;
  56. HALT;
  57. END;
  58. SkipLine(input); ReadRestLine(input, line);
  59. END;
  60. Close(input);
  61. ELSE
  62. STextIO.WriteString("TestCopy can't create outfile");
  63. STextIO.WriteLn;
  64. END;
  65. Close(output);
  66. STextIO.WriteString("TestCopy: endOfInput reached, lines copied=");
  67. SWholeIO.WriteInt(lines,0);
  68. STextIO.WriteLn;
  69. ELSE
  70. STextIO.WriteString("TestCopy can't open infile");
  71. STextIO.WriteLn;
  72. END;
  73. ELSE usage;
  74. END;
  75. ELSE usage;
  76. END;
  77. END TestCopy.