-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed.rb
166 lines (153 loc) · 3.71 KB
/
ed.rb
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#まだ途中です...
#対応できていません
class Ed
def initialize
begin
@file = File.open(ARGV[0])
@size = 0
@currentline = 0
@maxline = 0
@buffer = []
rescue
end
if([email protected]?) then
@file.each do |line|
@buffer.push(line)
@size += line.bytesize
end
@currentline = @buffer.length
@maxline = @buffer.length
puts @size
puts @buffer.length
# puts @buffer
elsif(!ARGV[0].nil?) then
@filename = ARGV[0]
puts ARGV[0] + ": No such file or directory"
end
#必要な変数
#@address
#@command
#@parameters
#modestate
#edit_mode: 0
#cmd_mode: 1
@mode = 1
#puts @buffer
#if @view is true and @mode:1 then print *
@view = false
@mark = "*"
loop do
read
eval
print
end
end
#コマンド受付
def read
#puts @mode
if(@mode==1 && @view) then printf @mark end
@cmd = STDIN.gets.chomp
#puts @cmd.size
end
#コマンド評価
# ed_ver.1
# add q,p(n),a,d,return
def eval
@size = 0
# @file.each do |line|
# @size += line.bytesize
# end
#コマンド正規表現
#/\A(adress(,adress)?)?command(parameters)?\z
#adress -> (?:\d+|[.$,;]|\/.*\/)
#command -> (wq|[acdefgijkmnpqrsw]|\z)
#parameters -> (.*)
#後方参照をうまく利用する
#式展開も利用
#/\A((\d+|[.$,;]|\/.*\/)(,(\d+|[.$,;]|\/.*\/))?)?(wq|[acdefgijkmnpqrsw]|\z)((.*))?\z/
@maxline = @buffer.length
@result = nil
if(@cmd.match(/[0-9]/)) then
@currentline = @cmd[0,1]
if(!@buffer[@cmd.to_i-1].nil? && @mode == 1) then puts @buffer[@cmd.to_i-1] end
end
if (@cmd.match(/[a]/)) then @mode = 0 end
if(@cmd.size < 2) then
if(@cmd =~ /a/) then
loop do
@message = STDIN.gets
if(@message =~ /\./) then break end
@buffer.insert(@currentline,@message)
@currentline += 1
end
elsif(@cmd =~ /c/) then
elsif(@cmd =~ /d/) then
@buffer.delete_at(@currentline-1)
if(@currentline > 0) then @currentline -= 1 end
puts @currentline
elsif(@cmd =~ /e/) then
elsif(@cmd =~ /E/) then
elsif(@cmd =~ /f/) then
elsif(@cmd =~ /h/) then
elsif(@cmd =~ /i/) then
elsif(@cmd =~ /j/) then
elsif(@cmd =~ /l/) then
elsif(@cmd =~ /m/) then
elsif(@cmd =~ /n/) then
elsif(@cmd =~ /p/) then
if (@mode == 1) then
if(@currentline <= @maxline)
@result = @buffer[@currentline-1]
puts @currentline
end
end
elsif(@cmd =~ /P/) then
if (@mode == 1) then
if(!@view) then @view = true
elsif (@view) then @view = false end
end
elsif(@cmd =~ /q/) then
if(@mode == 1)
exit
end
elsif(@cmd =~ /Q/) then
if(@mode == 1)
exit
end
elsif(@cmd =~ /r/) then
elsif(@cmd =~ /s/) then
elsif(@cmd =~ /t/) then
elsif(@cmd =~ /u/) then
elsif(@cmd =~ /w/) then
File.open(ARGV[0],"w") do |file|
@buffer.each do |line|
file.write(line)
@size += line.bytesize
end
end
puts @size
elsif(@cmd =~ /W/) then
elsif(@cmd =~ /z/) then
elsif(@cmd =~ /\=/) then
elsif(@cmd =~ /\./) then
@mode = 1
elsif(@cmd.empty?) then
if(@currentline < @maxline)
@currentline += 1
puts @currentline
else
@result ="?"
end
else
@result = "?"
end
else
@result = "?"
end
end
#出力
def print
if([email protected]?) then puts @result end
end
end
Ed.new