/src/lib/io/basic/binary_file_write.e

http://github.com/tybor/Liberty · Specman e · 298 lines · 220 code · 32 blank · 46 comment · 23 complexity · 3ea3bec395642eb3bbcdc544c35a5b75 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class BINARY_FILE_WRITE
  5. -- This class allow to write a file on the disk as a binary file
  6. -- (ie. file containing bytes). If you need to write text in a file,
  7. -- then consider using TEXT_FILE_WRITE.
  8. inherit
  9. DISPOSABLE
  10. BINARY_OUTPUT_STREAM
  11. redefine
  12. dispose
  13. end
  14. TERMINAL_OUTPUT_STREAM
  15. redefine
  16. dispose
  17. end
  18. FILE_STREAM
  19. redefine
  20. dispose
  21. end
  22. create {ANY}
  23. make, connect_to, connect_for_appending_to
  24. feature {ANY}
  25. connect_to (new_path: ABSTRACT_STRING)
  26. -- Truncate file to zero length or create binary file for writing.
  27. -- The stream is positioned at the beginning of the file.
  28. local
  29. p: POINTER
  30. do
  31. p := new_path.to_external
  32. output_stream := binary_file_write_open(p)
  33. if output_stream.is_not_null then
  34. set_path(new_path)
  35. if capacity = 0 then
  36. buffer := buffer.calloc(4096)
  37. capacity := 4096
  38. end
  39. if the_terminal_settings /= Void then
  40. the_terminal_settings.make(output_stream, Current)
  41. end
  42. end
  43. end
  44. connect_for_appending_to (new_path: STRING)
  45. -- Truncate file to zero length or create binary file for writing.
  46. -- The stream is positioned at the beginning of the file.
  47. require
  48. not is_connected
  49. not new_path.is_empty
  50. local
  51. p: POINTER
  52. do
  53. p := new_path.to_external
  54. output_stream := binary_file_write_append(p)
  55. if output_stream.is_not_null then
  56. path := new_path
  57. if capacity = 0 then
  58. buffer := buffer.calloc(4096)
  59. capacity := 4096
  60. end
  61. if the_terminal_settings /= Void then
  62. the_terminal_settings.make(output_stream, Current)
  63. end
  64. end
  65. end
  66. disconnect
  67. do
  68. if buffer_position > 0 then
  69. write_buffer
  70. end
  71. fclose(output_stream)
  72. path := Void
  73. end
  74. put_byte (byte: INTEGER)
  75. do
  76. if buffer_position >= 4096 then
  77. write_buffer
  78. end
  79. buffer.put(byte.to_character, buffer_position)
  80. buffer_position := buffer_position + 1
  81. end
  82. put_integer_16_native_endian (i: INTEGER_16)
  83. -- Write in the same order as the machine running this code.
  84. -- The result is machine dependant.
  85. do
  86. if buffer_position >= 4095 or else buffer_position.is_odd then
  87. write_buffer
  88. end
  89. put_16_ne(buffer, i, buffer_position)
  90. buffer_position := buffer_position + 2
  91. end
  92. put_integer_16_big_endian (i: INTEGER_16)
  93. -- Write `i' in big endian mode.
  94. -- The result is machine independent.
  95. do
  96. if buffer_position >= 4095 or else buffer_position.is_odd then
  97. write_buffer
  98. end
  99. put_16_be(buffer, i, buffer_position)
  100. buffer_position := buffer_position + 2
  101. end
  102. put_integer_16_little_endian (i: INTEGER_16)
  103. -- Write `i' in little endian mode.
  104. -- The result is machine independent.
  105. do
  106. if buffer_position >= 4095 or else buffer_position.is_odd then
  107. write_buffer
  108. end
  109. put_16_le(buffer, i, buffer_position)
  110. buffer_position := buffer_position + 2
  111. end
  112. put_integer_32_native_endian (i: INTEGER_32)
  113. -- Write in the same order as the machine running this code.
  114. -- The result is machine dependant.
  115. do
  116. if buffer_position >= 4095 or else buffer_position.is_odd then
  117. write_buffer
  118. end
  119. put_32_ne(buffer, i, buffer_position)
  120. buffer_position := buffer_position + 4
  121. end
  122. put_integer_32_big_endian (i: INTEGER_32)
  123. -- Write `i' in big endian mode.
  124. -- The result is machine independent.
  125. do
  126. if buffer_position >= 4095 or else buffer_position.is_odd then
  127. write_buffer
  128. end
  129. put_32_be(buffer, i, buffer_position)
  130. buffer_position := buffer_position + 4
  131. end
  132. put_integer_32_little_endian (i: INTEGER_32)
  133. -- Write `i' in little endian mode.
  134. -- The result is machine independent.
  135. do
  136. if buffer_position >= 4095 or else buffer_position.is_odd then
  137. write_buffer
  138. end
  139. put_32_le(buffer, i, buffer_position)
  140. buffer_position := buffer_position + 4
  141. end
  142. terminal_settings: TERMINAL_SETTINGS
  143. do
  144. if the_terminal_settings = Void then
  145. create the_terminal_settings.make(filtered_stream_pointer, Current)
  146. end
  147. Result := the_terminal_settings
  148. ensure
  149. valid: Result /= Void
  150. associated: Result.associated_stream = Current
  151. end
  152. feature {FILTER_OUTPUT_STREAM}
  153. filtered_put_character (c: CHARACTER)
  154. do
  155. put_byte(c.code)
  156. end
  157. filtered_flush
  158. -- forces a write of unwritten character (write my have been
  159. -- delayed, flush writes buffered characters)
  160. do
  161. if buffer_position > 0 then
  162. write_buffer
  163. end
  164. io_flush(output_stream)
  165. end
  166. feature {FILTER}
  167. filtered_descriptor: INTEGER 0
  168. filtered_has_descriptor: BOOLEAN False
  169. filtered_stream_pointer: POINTER
  170. do
  171. Result := output_stream
  172. end
  173. filtered_has_stream_pointer: BOOLEAN True
  174. feature {}
  175. buffer: NATIVE_ARRAY[CHARACTER]
  176. buffer_position: INTEGER
  177. capacity: INTEGER
  178. output_stream: POINTER
  179. the_terminal_settings: TERMINAL_SETTINGS
  180. make
  181. -- The new created object is not connected. (See also `connect_to' and
  182. -- `connect_for_appending_to'.)
  183. do
  184. buffer := buffer.calloc(4096)
  185. capacity := 4096
  186. ensure
  187. not is_connected
  188. end
  189. dispose
  190. require
  191. disconnect_file_after_use: not is_connected
  192. do
  193. end
  194. write_buffer
  195. local
  196. unused_result: INTEGER
  197. do
  198. if buffer_position > 0 then
  199. unused_result := io_fwrite(buffer, buffer_position, output_stream)
  200. buffer_position := 0
  201. end
  202. end
  203. feature {} -- built-ins
  204. put_16_ne (a_buf: NATIVE_ARRAY[CHARACTER]; i: INTEGER_16; ch_pos: INTEGER)
  205. external "built_in"
  206. end
  207. put_16_le (a_buf: NATIVE_ARRAY[CHARACTER]; i: INTEGER_16; ch_pos: INTEGER)
  208. external "built_in"
  209. end
  210. put_16_be (a_buf: NATIVE_ARRAY[CHARACTER]; i: INTEGER_16; ch_pos: INTEGER)
  211. external "built_in"
  212. end
  213. put_32_ne (a_buf: NATIVE_ARRAY[CHARACTER]; i: INTEGER_32; ch_pos: INTEGER)
  214. external "built_in"
  215. end
  216. put_32_le (a_buf: NATIVE_ARRAY[CHARACTER]; i: INTEGER_32; ch_pos: INTEGER)
  217. external "built_in"
  218. end
  219. put_32_be (a_buf: NATIVE_ARRAY[CHARACTER]; an_i: INTEGER_32; ch_pos: INTEGER)
  220. external "built_in"
  221. end
  222. binary_file_write_open (a_path_pointer: POINTER): POINTER
  223. external "plug_in"
  224. alias "{
  225. location: "${sys}/plugins"
  226. module_name: "io"
  227. feature_name: "binary_file_write_open"
  228. }"
  229. end
  230. binary_file_write_append (a_path_pointer: POINTER): POINTER
  231. external "plug_in"
  232. alias "{
  233. location: "${sys}/plugins"
  234. module_name: "io"
  235. feature_name: "binary_file_write_append"
  236. }"
  237. end
  238. fclose (a_stream_pointer: POINTER)
  239. external "plug_in"
  240. alias "{
  241. location: "${sys}/plugins"
  242. module_name: "io"
  243. feature_name: "io_fclose"
  244. }"
  245. end
  246. end -- class BINARY_FILE_WRITE
  247. --
  248. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  249. --
  250. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  251. -- of this software and associated documentation files (the "Software"), to deal
  252. -- in the Software without restriction, including without limitation the rights
  253. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  254. -- copies of the Software, and to permit persons to whom the Software is
  255. -- furnished to do so, subject to the following conditions:
  256. --
  257. -- The above copyright notice and this permission notice shall be included in
  258. -- all copies or substantial portions of the Software.
  259. --
  260. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  261. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  262. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  263. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  264. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  265. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  266. -- THE SOFTWARE.