/src/lib/io/filesystem/regular_file.e

http://github.com/tybor/Liberty · Specman e · 130 lines · 83 code · 15 blank · 32 comment · 3 complexity · 0a3e8a8f718d1fae45c585de01886c1f MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class REGULAR_FILE
  5. inherit
  6. FILE
  7. create {ANY}
  8. make
  9. feature {ANY}
  10. name: FIXED_STRING
  11. path: FIXED_STRING
  12. is_directory: BOOLEAN False
  13. is_regular: BOOLEAN True
  14. as_directory: DIRECTORY
  15. do
  16. check False end
  17. end
  18. as_regular: REGULAR_FILE
  19. do
  20. Result := Current
  21. end
  22. exists: BOOLEAN
  23. local
  24. ft: FILE_TOOLS
  25. do
  26. Result := ft.file_exists(path)
  27. end
  28. feature {ANY} -- Text stream access
  29. read: INPUT_STREAM
  30. -- Returns a stream connected for reading the file. If the read stream is not connected anymore,
  31. -- connects it again.
  32. -- Always returns the same object.
  33. require
  34. not is_writing
  35. do
  36. if read_memory = Void then
  37. create read_memory.connect_to(path.out)
  38. elseif not read_memory.is_connected then
  39. read_memory.connect_to(path.out)
  40. end
  41. Result := read_memory
  42. ensure
  43. is_reading
  44. end
  45. is_reading: BOOLEAN
  46. do
  47. Result := read_memory /= Void and then read_memory.is_connected
  48. end
  49. write: OUTPUT_STREAM
  50. -- Returns a stream connected for writing to the file. If the write stream is not connected anymore,
  51. -- connects it again.
  52. -- Always returns the same object.
  53. require
  54. not is_reading
  55. do
  56. if write_memory = Void then
  57. create write_memory.connect_to(path.out)
  58. elseif not write_memory.is_connected then
  59. write_memory.connect_to(path.out)
  60. end
  61. Result := write_memory
  62. ensure
  63. is_writing
  64. end
  65. append: OUTPUT_STREAM
  66. -- Returns a stream connected for appending to the file. If the write stream is already connected,
  67. -- use `write' instead.
  68. -- Always returns the same object.
  69. require
  70. not is_reading
  71. not is_writing
  72. do
  73. if write_memory = Void then
  74. create write_memory.connect_for_appending_to(path.out)
  75. else
  76. write_memory.connect_for_appending_to(path.out)
  77. end
  78. Result := write_memory
  79. ensure
  80. is_writing
  81. end
  82. is_writing: BOOLEAN
  83. do
  84. Result := write_memory /= Void and then write_memory.is_connected
  85. end
  86. feature {}
  87. make (a_file_path: ABSTRACT_STRING)
  88. do
  89. path := a_file_path.intern
  90. basic_directory.compute_short_name_of(path)
  91. name := basic_directory.last_entry.intern
  92. end
  93. read_memory: TEXT_FILE_READ
  94. write_memory: TEXT_FILE_WRITE
  95. end -- class REGULAR_FILE
  96. --
  97. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  98. --
  99. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  100. -- of this software and associated documentation files (the "Software"), to deal
  101. -- in the Software without restriction, including without limitation the rights
  102. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  103. -- copies of the Software, and to permit persons to whom the Software is
  104. -- furnished to do so, subject to the following conditions:
  105. --
  106. -- The above copyright notice and this permission notice shall be included in
  107. -- all copies or substantial portions of the Software.
  108. --
  109. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  110. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  111. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  112. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  113. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  114. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  115. -- THE SOFTWARE.