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