PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/io/basic/text_file_write.e

http://github.com/tybor/Liberty
Specman e | 210 lines | 145 code | 24 blank | 41 comment | 8 complexity | ae6d771a91e0947a83dac0b322994cc0 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class TEXT_FILE_WRITE
  5   --
  6   -- Basic output facilities to write a named file on the disk.
  7   --
  8   -- Note: most features are common with STD_OUTPUT so you can test your
  9   --       program first on the screen and then, changing of instance
 10   --       (STD_OUTPUT/TEXT_FILE_WRITE), doing the same on a file.
 11   --
 12
 13inherit
 14   FILE_STREAM
 15      redefine out_in_tagged_out_memory
 16      end
 17   TERMINAL_OUTPUT_STREAM
 18      redefine out_in_tagged_out_memory
 19      end
 20
 21create {ANY}
 22   make, connect_to, connect_for_appending_to
 23
 24feature {ANY}
 25   flushed_character_count: INTEGER_64
 26         -- Note that '%N' is counted as one character even if two
 27         -- bytes are written in the file.
 28
 29   character_count: INTEGER_64
 30         -- See also `flushed_character_count'
 31         -- Note that '%N' is counted as one character even if two
 32         -- bytes are written in the file.
 33      require
 34         is_connected
 35      do
 36         Result := flushed_character_count + buffer_position
 37      end
 38
 39   connect_to (new_path: ABSTRACT_STRING)
 40         -- Truncate file to zero length or create text file for writing.
 41         -- The stream is positioned at the beginning of the file.
 42      local
 43         p: POINTER
 44      do
 45         p := new_path.to_external
 46         output_stream := text_file_write_open(p)
 47         if output_stream.is_not_null then
 48            set_path(new_path)
 49            if capacity = 0 then
 50               buffer := buffer.calloc(4096)
 51               capacity := 4096
 52            end
 53         end
 54      ensure then
 55         is_connected implies character_count = 0
 56      end
 57
 58   connect_for_appending_to (new_path: ABSTRACT_STRING)
 59         -- Open for writing. The file is created if it does not exist.
 60         -- The stream is positioned at the end of the file.
 61      require
 62         not is_connected
 63         not new_path.is_empty
 64      local
 65         p: POINTER
 66      do
 67         p := new_path.to_external
 68         output_stream := text_file_write_append(p)
 69         if output_stream.is_not_null then
 70            set_path(new_path)
 71            if capacity = 0 then
 72               buffer := buffer.calloc(4096)
 73               capacity := 4096
 74            end
 75         end
 76      ensure
 77         is_connected implies character_count = 0
 78      end
 79
 80   disconnect
 81      do
 82         if buffer_position > 0 then
 83            write_buffer
 84         end
 85         io_fclose(output_stream)
 86         path := Void
 87         filter := Void
 88         flushed_character_count := 0
 89      end
 90
 91   out_in_tagged_out_memory
 92      do
 93         tagged_out_memory.append(once "{TEXT_FILE_WRITE ")
 94         tagged_out_memory.append(path)
 95         tagged_out_memory.extend('}')
 96      end
 97
 98feature {FILTER_OUTPUT_STREAM}
 99   filtered_put_character (c: CHARACTER)
100      do
101         if buffer_position >= 4096 then
102            write_buffer
103         end
104         buffer.put(c, buffer_position)
105         buffer_position := buffer_position + 1
106      end
107
108   filtered_flush
109      do
110         if buffer_position > 0 then
111            write_buffer
112         end
113         io_flush(output_stream)
114      ensure
115         flushed_character_count = character_count
116      end
117
118feature {FILTER}
119   filtered_descriptor: INTEGER
120      do
121         Result := sequencer_descriptor(output_stream)
122      end
123
124   filtered_has_descriptor: BOOLEAN True
125
126   filtered_stream_pointer: POINTER
127      do
128         Result := output_stream
129      end
130
131   filtered_has_stream_pointer: BOOLEAN True
132
133feature {}
134   buffer: NATIVE_ARRAY[CHARACTER]
135
136   buffer_position: INTEGER
137
138   capacity: INTEGER
139
140   output_stream: POINTER
141
142   make
143         -- The new created object is not connected. (See also `connect_to' and
144         -- `connect_for_appending_to'.)
145      do
146         buffer := buffer.calloc(4096)
147         capacity := 4096
148      ensure
149         not is_connected
150      end
151
152   write_buffer
153      local
154         unused_result: INTEGER
155      do
156         if buffer_position > 0 then
157            unused_result := io_fwrite(buffer, buffer_position, output_stream)
158            flushed_character_count := flushed_character_count + buffer_position
159            buffer_position := 0
160         end
161      end
162
163   text_file_write_open (path_pointer: POINTER): POINTER
164      external "plug_in"
165      alias "{
166         location: "${sys}/plugins"
167         module_name: "io"
168         feature_name: "text_file_write_open"
169         }"
170      end
171
172   text_file_write_append (path_pointer: POINTER): POINTER
173      external "plug_in"
174      alias "{
175         location: "${sys}/plugins"
176         module_name: "io"
177         feature_name: "text_file_write_append"
178         }"
179      end
180
181   io_fclose (stream: POINTER)
182      external "plug_in"
183      alias "{
184         location: "${sys}/plugins"
185         module_name: "io"
186         feature_name: "io_fclose"
187         }"
188      end
189
190end -- class TEXT_FILE_WRITE
191--
192-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
193--
194-- Permission is hereby granted, free of charge, to any person obtaining a copy
195-- of this software and associated documentation files (the "Software"), to deal
196-- in the Software without restriction, including without limitation the rights
197-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
198-- copies of the Software, and to permit persons to whom the Software is
199-- furnished to do so, subject to the following conditions:
200--
201-- The above copyright notice and this permission notice shall be included in
202-- all copies or substantial portions of the Software.
203--
204-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
205-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
206-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
207-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
208-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
209-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
210-- THE SOFTWARE.