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