/src/lib/io/basic/text_file_read_write.e

http://github.com/tybor/Liberty · Specman e · 239 lines · 178 code · 27 blank · 34 comment · 10 complexity · 29cb82c3cc455abba5726a82ed1c7376 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_READ_WRITE
  5. -- This class allows to read and write a named file on the disk.
  6. -- Note that opening a file in READ and WRITE mode is not a very
  7. -- common case and may lead to performance decrease compared to
  8. -- TEXT_FILE_READ and TEXT_FILE_WRITE performance. Such a file
  9. -- is both an INPUT_STREAM and an OUTPUT_STREAM.
  10. inherit
  11. FILE_STREAM
  12. redefine out_in_tagged_out_memory
  13. end
  14. TERMINAL_INPUT_OUTPUT_STREAM
  15. redefine filtered_read_line_in, out_in_tagged_out_memory
  16. end
  17. insert
  18. STRING_HANDLER
  19. redefine out_in_tagged_out_memory
  20. end
  21. create {ANY}
  22. make, connect_to, connect_for_appending_to
  23. feature {ANY}
  24. connect_to (new_path: ABSTRACT_STRING)
  25. -- Open for reading and writing. The stream is positioned at the
  26. -- beginning of the file.
  27. local
  28. s: POINTER; tfw: TEXT_FILE_WRITE; exists: BOOLEAN
  29. do
  30. if (create {FILE_TOOLS}).is_readable(new_path) then
  31. exists := True
  32. else
  33. create tfw.connect_to(new_path)
  34. if tfw.is_connected then
  35. tfw.disconnect
  36. exists := True
  37. end
  38. end
  39. if exists then
  40. check
  41. (create {FILE_TOOLS}).is_readable(new_path)
  42. end
  43. s := text_file_read_write_open(new_path.to_external)
  44. if s.is_not_null then
  45. stream := s
  46. set_path(new_path)
  47. end
  48. end
  49. end
  50. connect_for_appending_to (new_path: ABSTRACT_STRING)
  51. -- Open for reading and writing. The file is created if it does not
  52. -- exist. The stream is positioned at the end of the file.
  53. local
  54. s: POINTER; tfw: TEXT_FILE_WRITE; exists: BOOLEAN
  55. do
  56. if (create {FILE_TOOLS}).is_readable(new_path) then
  57. exists := True
  58. else
  59. create tfw.connect_to(new_path)
  60. if tfw.is_connected then
  61. tfw.disconnect
  62. exists := True
  63. end
  64. end
  65. if exists then
  66. check
  67. (create {FILE_TOOLS}).is_readable(new_path)
  68. end
  69. s := text_file_read_write_append(new_path.to_external)
  70. if s.is_not_null then
  71. stream := s
  72. set_path(new_path)
  73. end
  74. end
  75. end
  76. disconnect
  77. do
  78. io_fclose(stream)
  79. path := Void
  80. end
  81. can_unread_character: BOOLEAN
  82. do
  83. Result := not unread_character_flag
  84. end
  85. end_of_input: BOOLEAN
  86. do
  87. io_flush(stream)
  88. Result := io_feof(stream)
  89. end
  90. out_in_tagged_out_memory
  91. do
  92. tagged_out_memory.append(once "{TEXT_FILE_READ_WRITE ")
  93. tagged_out_memory.append(path)
  94. tagged_out_memory.extend('}')
  95. end
  96. feature {FILTER_INPUT_STREAM}
  97. filtered_last_character: CHARACTER
  98. filtered_read_character
  99. local
  100. c: INTEGER
  101. do
  102. io_flush(stream)
  103. c := io_getc(stream)
  104. if c >= 0 then
  105. filtered_last_character := c.to_character
  106. end
  107. unread_character_flag := False
  108. end
  109. filtered_unread_character
  110. local
  111. p: POINTER; c: CHARACTER
  112. do
  113. p := stream
  114. c := last_character
  115. io_ungetc(c, p)
  116. unread_character_flag := True
  117. end
  118. filtered_read_line_in (str: STRING)
  119. do
  120. from
  121. read_character
  122. until
  123. end_of_input or else last_character = '%N'
  124. loop
  125. str.extend(last_character)
  126. read_character
  127. end
  128. end
  129. feature {FILTER_OUTPUT_STREAM}
  130. filtered_put_character (c: CHARACTER)
  131. do
  132. io_flush(stream)
  133. io_putc(c, stream)
  134. end
  135. filtered_flush
  136. do
  137. io_flush(stream)
  138. end
  139. feature {FILTER}
  140. filtered_descriptor: INTEGER
  141. do
  142. Result := sequencer_descriptor(stream)
  143. end
  144. filtered_has_descriptor: BOOLEAN True
  145. filtered_stream_pointer: POINTER
  146. do
  147. Result := stream
  148. end
  149. filtered_has_stream_pointer: BOOLEAN True
  150. feature {}
  151. unread_character_flag: BOOLEAN
  152. stream: POINTER
  153. make
  154. -- The new created object is not connected. (See also `connect_to' and
  155. -- `connect_for_appending_to'.)
  156. do
  157. ensure
  158. not is_connected
  159. end
  160. text_file_read_write_open (a_path_pointer: POINTER): POINTER
  161. external "plug_in"
  162. alias "{
  163. location: "${sys}/plugins"
  164. module_name: "io"
  165. feature_name: "text_file_read_write_open"
  166. }"
  167. end
  168. text_file_read_write_append (a_path_pointer: POINTER): POINTER
  169. external "plug_in"
  170. alias "{
  171. location: "${sys}/plugins"
  172. module_name: "io"
  173. feature_name: "text_file_read_write_append"
  174. }"
  175. end
  176. io_fclose (a_stream_pointer: POINTER)
  177. external "plug_in"
  178. alias "{
  179. location: "${sys}/plugins"
  180. module_name: "io"
  181. feature_name: "io_fclose"
  182. }"
  183. end
  184. io_feof (a_stream_pointer: POINTER): BOOLEAN
  185. external "plug_in"
  186. alias "{
  187. location: "${sys}/plugins"
  188. module_name: "io"
  189. feature_name: "io_feof"
  190. }"
  191. end
  192. end -- class TEXT_FILE_READ_WRITE
  193. --
  194. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  195. --
  196. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  197. -- of this software and associated documentation files (the "Software"), to deal
  198. -- in the Software without restriction, including without limitation the rights
  199. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  200. -- copies of the Software, and to permit persons to whom the Software is
  201. -- furnished to do so, subject to the following conditions:
  202. --
  203. -- The above copyright notice and this permission notice shall be included in
  204. -- all copies or substantial portions of the Software.
  205. --
  206. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  207. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  208. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  209. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  210. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  211. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  212. -- THE SOFTWARE.