/src/lib/io/low_level/output_stream_tools.e

http://github.com/tybor/Liberty · Specman e · 395 lines · 304 code · 37 blank · 54 comment · 0 complexity · 95e34d2923bdd030d7f5a15d71d45f51 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class OUTPUT_STREAM_TOOLS
  5. --
  6. -- OUTPUT_STREAM implementation. Only OUTPUT_STREAM can inherit from this class.
  7. --
  8. insert
  9. STRING_HANDLER
  10. feature {ANY}
  11. put_character (c: CHARACTER)
  12. require
  13. is_connected
  14. not is_filtered and then can_put_character(c)
  15. deferred
  16. end
  17. flush
  18. -- Flushes the pipe. If `is_filtered', calls the filter's
  19. -- `flush' instead.
  20. require
  21. is_connected
  22. deferred
  23. end
  24. can_put_character (c: CHARACTER): BOOLEAN
  25. deferred
  26. end
  27. is_filtered: BOOLEAN
  28. deferred
  29. end
  30. is_connected: BOOLEAN
  31. deferred
  32. end
  33. feature {ABSTRACT_STRING}
  34. put_natively_stored_string (s: NATIVELY_STORED_STRING)
  35. require
  36. s /= Void
  37. do
  38. put_abstract_string(s)
  39. end
  40. put_abstract_string (s: ABSTRACT_STRING)
  41. require
  42. s /= Void
  43. local
  44. i, count: INTEGER
  45. do
  46. from
  47. i := 1
  48. count := s.count
  49. until
  50. i > count
  51. loop
  52. put_character(s.item(i))
  53. i := i + 1
  54. end
  55. end
  56. feature {ANY}
  57. put_string (s: ABSTRACT_STRING)
  58. -- Output `s' to current output device.
  59. require
  60. is_connected
  61. not is_filtered
  62. s /= Void
  63. do
  64. s.print_on(as_output_stream)
  65. end
  66. put_unicode_string (unicode_string: UNICODE_STRING)
  67. -- Output the UTF-8 encoding of the `unicode_string'.
  68. require
  69. is_connected
  70. not is_filtered
  71. unicode_string /= Void
  72. do
  73. tmp_string.clear_count
  74. unicode_string.utf8_encode_in(tmp_string)
  75. put_string(tmp_string)
  76. end
  77. put_line (s: ABSTRACT_STRING)
  78. -- Output the string followed by a '%N'.
  79. do
  80. put_string(s)
  81. put_new_line
  82. end
  83. feature {ANY} -- To write a number:
  84. frozen put_integer (i: INTEGER_64)
  85. -- Output `i' to current output device.
  86. require
  87. is_connected
  88. not is_filtered
  89. do
  90. tmp_string.clear_count
  91. i.append_in(tmp_string)
  92. put_string(tmp_string)
  93. end
  94. frozen put_integer_format (i: INTEGER_64; s: INTEGER)
  95. -- Output `i' to current output device using at most `s' character.
  96. require
  97. is_connected
  98. not is_filtered
  99. do
  100. tmp_string.clear_count
  101. i.append_in_format(tmp_string, s)
  102. put_string(tmp_string)
  103. end
  104. frozen put_natural_8 (n: NATURAL_8)
  105. -- Output `n' to current output device.
  106. require
  107. is_connected
  108. not is_filtered
  109. do
  110. tmp_string.clear_count
  111. n.append_in(tmp_string)
  112. put_string(tmp_string)
  113. end
  114. frozen put_natural_8_format (n: NATURAL_8; s: INTEGER)
  115. -- Output `n' to current output device using at most `s' character.
  116. require
  117. is_connected
  118. not is_filtered
  119. do
  120. tmp_string.clear_count
  121. n.append_in_format(tmp_string, s)
  122. put_string(tmp_string)
  123. end
  124. frozen put_natural_16 (n: NATURAL_16)
  125. -- Output `n' to current output device.
  126. require
  127. is_connected
  128. not is_filtered
  129. do
  130. tmp_string.clear_count
  131. n.append_in(tmp_string)
  132. put_string(tmp_string)
  133. end
  134. frozen put_natural_16_format (n: NATURAL_16; s: INTEGER)
  135. -- Output `n' to current output device using at most `s' character.
  136. require
  137. is_connected
  138. not is_filtered
  139. do
  140. tmp_string.clear_count
  141. n.append_in_format(tmp_string, s)
  142. put_string(tmp_string)
  143. end
  144. frozen put_natural_32 (n: NATURAL_32)
  145. -- Output `n' to current output device.
  146. require
  147. is_connected
  148. not is_filtered
  149. do
  150. tmp_string.clear_count
  151. n.append_in(tmp_string)
  152. put_string(tmp_string)
  153. end
  154. frozen put_natural_32_format (n: NATURAL_32; s: INTEGER)
  155. -- Output `n' to current output device using at most `s' character.
  156. require
  157. is_connected
  158. not is_filtered
  159. do
  160. tmp_string.clear_count
  161. n.append_in_format(tmp_string, s)
  162. put_string(tmp_string)
  163. end
  164. frozen put_natural_64 (n: NATURAL_64)
  165. -- Output `n' to current output device.
  166. require
  167. is_connected
  168. not is_filtered
  169. do
  170. tmp_string.clear_count
  171. n.append_in(tmp_string)
  172. put_string(tmp_string)
  173. end
  174. frozen put_natural_64_format (n: NATURAL_64; s: INTEGER)
  175. -- Output `n' to current output device using at most `s' character.
  176. require
  177. is_connected
  178. not is_filtered
  179. do
  180. tmp_string.clear_count
  181. n.append_in_format(tmp_string, s)
  182. put_string(tmp_string)
  183. end
  184. put_real (r: REAL)
  185. -- Output `r' to current output device.
  186. require
  187. is_connected
  188. not is_filtered
  189. do
  190. tmp_string.clear_count
  191. r.append_in(tmp_string)
  192. put_string(tmp_string)
  193. end
  194. put_real_format (r: REAL; f: INTEGER)
  195. -- Output `r' with only `f' digit for the fractional part.
  196. -- Examples:
  197. -- put_real(3.519,2) print "3.51".
  198. require
  199. is_connected
  200. not is_filtered
  201. f >= 0
  202. do
  203. tmp_string.clear_count
  204. r.append_in_format(tmp_string, f)
  205. put_string(tmp_string)
  206. end
  207. put_real_scientific (r: REAL; f: INTEGER)
  208. -- Output `r' using the scientific notation with only `f' digit for the fractional part.
  209. -- Examples:
  210. -- put_real_scientific(3.519,2) print "3.16e+00".
  211. require
  212. is_connected
  213. not is_filtered
  214. f >= 0
  215. do
  216. tmp_string.clear_count
  217. r.append_in_scientific(tmp_string, f)
  218. put_string(tmp_string)
  219. end
  220. put_number (number: NUMBER)
  221. -- Output the `number'.
  222. require
  223. is_connected
  224. not is_filtered
  225. number /= Void
  226. do
  227. tmp_string.clear_count
  228. number.append_in(tmp_string)
  229. put_string(tmp_string)
  230. end
  231. feature {ANY} -- Other features:
  232. put_boolean (b: BOOLEAN)
  233. -- Output `b' to current output device according
  234. -- to the Eiffel format.
  235. require
  236. is_connected
  237. not is_filtered
  238. do
  239. tmp_string.clear_count
  240. b.append_in(tmp_string)
  241. put_string(tmp_string)
  242. end
  243. put_pointer (p: POINTER)
  244. -- Output a viewable version of `p'.
  245. require
  246. is_connected
  247. not is_filtered
  248. do
  249. tmp_string.clear_count
  250. p.append_in(tmp_string)
  251. put_string(tmp_string)
  252. end
  253. put_new_line
  254. -- Output a newline character.
  255. require
  256. is_connected
  257. not is_filtered
  258. do
  259. put_character('%N')
  260. end
  261. put_spaces (nb: INTEGER)
  262. -- Output `nb' spaces character.
  263. require
  264. is_connected
  265. not is_filtered
  266. nb >= 0
  267. local
  268. count: INTEGER
  269. do
  270. from
  271. until
  272. count >= nb
  273. loop
  274. put_character(' ')
  275. count := count + 1
  276. end
  277. end
  278. append_file (file_name: STRING)
  279. require
  280. is_connected
  281. not is_filtered
  282. ;(create {FILE_TOOLS}).is_readable(file_name)
  283. local
  284. c: CHARACTER
  285. do
  286. tmp_file_read.connect_to(file_name)
  287. from
  288. tmp_file_read.read_character
  289. until
  290. tmp_file_read.end_of_input
  291. loop
  292. c := tmp_file_read.last_character
  293. put_character(c)
  294. tmp_file_read.read_character
  295. end
  296. tmp_file_read.disconnect
  297. end
  298. feature {}
  299. as_output_stream: OUTPUT_STREAM
  300. deferred
  301. ensure
  302. yes_indeed_it_is_the_same_object: Result.to_pointer = to_pointer
  303. end
  304. feature {}
  305. tmp_file_read: TEXT_FILE_READ
  306. once
  307. create Result.make
  308. end
  309. tmp_string: STRING
  310. once
  311. create Result.make(512)
  312. end
  313. feature {}
  314. io_putc (byte: CHARACTER; stream: POINTER)
  315. external "plug_in"
  316. alias "{
  317. location: "${sys}/plugins"
  318. module_name: "io"
  319. feature_name: "io_putc"
  320. }"
  321. end
  322. io_fwrite (buf: NATIVE_ARRAY[CHARACTER]; size: INTEGER; stream: POINTER): INTEGER
  323. external "plug_in"
  324. alias "{
  325. location: "${sys}/plugins"
  326. module_name: "io"
  327. feature_name: "io_fwrite"
  328. }"
  329. end
  330. io_flush (stream: POINTER)
  331. external "plug_in"
  332. alias "{
  333. location: "${sys}/plugins"
  334. module_name: "io"
  335. feature_name: "io_flush"
  336. }"
  337. end
  338. end -- class OUTPUT_STREAM_TOOLS
  339. --
  340. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  341. --
  342. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  343. -- of this software and associated documentation files (the "Software"), to deal
  344. -- in the Software without restriction, including without limitation the rights
  345. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  346. -- copies of the Software, and to permit persons to whom the Software is
  347. -- furnished to do so, subject to the following conditions:
  348. --
  349. -- The above copyright notice and this permission notice shall be included in
  350. -- all copies or substantial portions of the Software.
  351. --
  352. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  353. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  354. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  355. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  356. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  357. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  358. -- THE SOFTWARE.