misc/vim: inform the user when no results are found
[nit.git] / lib / filter_stream.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 class FilterReader
15 super Reader
16 # Filter readed elements
17 var stream: nullable Reader = null
18
19 redef fun eof: Bool
20 do
21 assert stream != null
22 return stream.eof
23 end
24 end
25
26 class FilterWriter
27 super Writer
28 # Filter outputed elements
29 var stream: nullable Writer = null
30
31 # Can the stream be used to write
32 redef fun is_writable: Bool
33 do
34 assert stream != null
35 return stream.is_writable
36 end
37 end
38
39 class StreamCat
40 super FilterReader
41 private var streams: Iterator[Reader]
42
43 redef fun eof: Bool
44 do
45 if stream == null then
46 return true
47 else if stream.eof then
48 stream.close
49 stream = null
50 return eof
51 else
52 return false
53 end
54 end
55
56 redef fun stream: nullable Reader
57 do
58 var res = super
59 if res == null and _streams.is_ok then
60 res = _streams.item
61 stream = res
62 assert stream != null
63 _streams.next
64 end
65 return res
66 end
67
68 redef fun read_char: Int
69 do
70 assert not eof
71 return stream.read_char
72 end
73
74 redef fun close
75 do
76 while stream != null do
77 stream.close
78 stream = null
79 end
80 end
81
82 init with_streams(streams: Array[Reader])
83 do
84 _streams = streams.iterator
85 end
86
87 init(streams: Reader ...) is old_style_init do
88 _streams = streams.iterator
89 end
90 end
91
92 class StreamDemux
93 super FilterWriter
94 private var streams: Array[Writer]
95
96 redef fun is_writable: Bool
97 do
98 if stream.is_writable then
99 return true
100 else
101 for i in _streams
102 do
103 if i.is_writable then
104 return true
105 end
106 end
107 return false
108 end
109 end
110
111 redef fun write(s: Text)
112 do
113 for i in _streams
114 do
115 stream = i
116 if stream.is_writable then
117 stream.write(s)
118 end
119 end
120 end
121
122 redef fun close
123 do
124 for i in _streams
125 do
126 stream = i
127 stream.close
128 end
129 end
130
131 init with_streams(streams: Array[Writer])
132 do
133 _streams = streams
134 end
135
136 init(streams: Writer ...) is old_style_init do
137 _streams = streams
138 end
139 end