/src/lib/io/filesystem/directory_notation.e

http://github.com/tybor/Liberty · Specman e · 265 lines · 162 code · 23 blank · 80 comment · 7 complexity · 8fceaad266564ab99b685b236a2e0402 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 DIRECTORY_NOTATION
  5. feature {ANY}
  6. is_current_directory (path: STRING): BOOLEAN
  7. deferred
  8. end
  9. is_parent_directory (path: STRING): BOOLEAN
  10. deferred
  11. end
  12. to_parent_directory (some_path: STRING)
  13. -- Tries to compute in `some_path' (which may be either a
  14. -- file path or a directory path) the parent directory of
  15. -- `some_path'. When `some_path' is a path with no parent
  16. -- directory, `some_path' `is_empty' after this call. This
  17. -- operation does not perform any disk access.
  18. require
  19. is_valid_path(some_path)
  20. deferred
  21. end
  22. to_subdirectory_with (parent_path, entry_name: STRING)
  23. -- Try to compute in `parent_path' the new subdirectory path
  24. -- obtained when trying to concatenate smartly `parent_path'
  25. -- with some `entry_name'. When this fails, `parent_path'
  26. -- `is_empty' after this call. This operation does not
  27. -- perform any disk access.
  28. require
  29. is_valid_path(parent_path)
  30. is_valid_path(entry_name)
  31. deferred
  32. ensure
  33. entry_name.is_equal(old entry_name.twin)
  34. end
  35. to_file_path_with (parent_path, file_name: STRING)
  36. -- Try to compute in `parent_path' the new file path obtained
  37. -- when trying to concatenate smartly `parent_path' with
  38. -- some `file_name'. When this fails, `parent_path'
  39. -- `is_empty' after this call. This operation does not
  40. -- perform any disk access.
  41. require
  42. is_valid_path(parent_path)
  43. is_valid_file_name(file_name)
  44. deferred
  45. ensure
  46. file_name.is_equal(old file_name.twin)
  47. end
  48. to_subpath_with (parent_path, subpath: STRING)
  49. -- Try to compute in `parent_path' the new file path obtained
  50. -- when trying to concatenate smartly `parent_path' with
  51. -- some `subpath'. When this fails, `parent_path'
  52. -- `is_empty' after this call. This operation does not
  53. -- perform any disk access.
  54. require
  55. is_valid_directory_path(parent_path)
  56. is_valid_path(subpath)
  57. not is_absolute_path(subpath)
  58. deferred
  59. ensure
  60. parent_path.is_empty or else is_valid_path(parent_path)
  61. parent_path.is_empty or else is_valid_directory_path(parent_path) = old is_valid_directory_path(subpath)
  62. parent_path.is_empty or else is_absolute_path(parent_path) = old is_absolute_path(parent_path)
  63. end
  64. frozen to_absolute_path_in (possible_parent, path: STRING)
  65. -- If `path' is not absolute, make it so by appending it to
  66. -- `possible_parent'. Else, overwrite `possible_parent' with
  67. -- path.
  68. require
  69. is_valid_directory_path(possible_parent)
  70. is_absolute_path(possible_parent)
  71. is_valid_path(path)
  72. do
  73. if not is_absolute_path(path) then
  74. to_subpath_with(possible_parent, path)
  75. else
  76. possible_parent.copy(path)
  77. end
  78. ensure
  79. is_absolute_path(possible_parent)
  80. end
  81. to_short_name_in (buffer, path: STRING)
  82. require
  83. is_valid_path(path)
  84. buffer /= Void
  85. deferred
  86. end
  87. feature {ANY}
  88. frozen from_notation (source_notation: DIRECTORY_NOTATION; path: STRING)
  89. -- Convert `path' from `source_notation' to `Current'
  90. -- notation. If this fails, then `path' `is_empty' after this
  91. -- call.
  92. require
  93. source_notation.is_valid_path(path)
  94. local
  95. tmp: STRING
  96. do
  97. tmp := source_notation.to_notation(path, Current)
  98. if path /= tmp then
  99. path.copy(tmp)
  100. end
  101. ensure
  102. path.is_empty or else is_valid_path(path)
  103. end
  104. can_sanitize (name: STRING): BOOLEAN
  105. do
  106. -- Default is to have no sanitizing
  107. end
  108. to_valid_file_name (name: STRING)
  109. -- Sanitize `name' (by removing forbidden characters or
  110. -- encoding them)
  111. require
  112. name /= Void
  113. not is_valid_file_name(name)
  114. can_sanitize(name)
  115. do
  116. check
  117. False
  118. end
  119. ensure
  120. is_valid_file_name(name)
  121. end
  122. to_directory_path (path: STRING)
  123. -- Make sure that the given path is a canonical directory
  124. -- path as would be returned by `to_subdirectory_with'
  125. require
  126. path /= Void
  127. is_valid_path(path)
  128. deferred
  129. ensure
  130. is_valid_directory_path(path)
  131. end
  132. can_map_drive (source_notation: DIRECTORY_NOTATION; drive: STRING): BOOLEAN
  133. deferred
  134. end
  135. to_root (source_notation: DIRECTORY_NOTATION; drive: STRING)
  136. -- Convert `drive' from a drive letter/device name in
  137. -- `source_notation' to an absolute path in `Current'
  138. -- notation.
  139. require
  140. can_map_drive(source_notation, drive)
  141. deferred
  142. ensure
  143. is_valid_path(drive)
  144. is_absolute_path(drive)
  145. end
  146. to_default_root (directory: STRING)
  147. deferred
  148. ensure
  149. is_valid_path(directory)
  150. is_absolute_path(directory)
  151. end
  152. to_current_directory (directory: STRING)
  153. -- Put the relative directory representing the current
  154. -- working directory into directory. Not to be confused with
  155. -- the absolute path of the current working directory at a
  156. -- given time. This operation does not perform any disk
  157. -- access.
  158. require
  159. directory /= Void
  160. deferred
  161. ensure
  162. is_valid_path(directory)
  163. not is_absolute_path(directory)
  164. end
  165. feature {ANY}
  166. is_case_sensitive: BOOLEAN
  167. deferred
  168. end
  169. is_valid_path (path: STRING): BOOLEAN
  170. -- Does `path' represent a syntactically valid file or
  171. -- directory path? The result does not imply that there
  172. -- actually a file or directory with that name. This
  173. -- operation does not perform any disk access.
  174. deferred
  175. ensure
  176. path.is_equal(old path.twin)
  177. Result implies not path.is_empty
  178. end
  179. is_valid_directory_path (path: STRING): BOOLEAN
  180. -- Does `path' represent a syntactically valid directory
  181. -- path? For many Systems, there may be no syntactical
  182. -- difference between file paths and directory paths, in
  183. -- that case there is no difference between
  184. -- `is_valid_directory_path' and `is_valid_path'.
  185. deferred
  186. ensure
  187. path.is_equal(old path.twin)
  188. Result implies is_valid_path(path)
  189. end
  190. is_valid_file_name (name: STRING): BOOLEAN
  191. -- Does `path' only contain valid characters for a file? The
  192. -- result does not imply that there is actually a file or
  193. -- directory with that name. Not the same as `is_valid_path':
  194. -- path separators (/ for unix, \ for windows, ...) are
  195. -- allowed in paths, but not in file names. This operation
  196. -- does not perform any disk access.
  197. deferred
  198. ensure
  199. name.is_equal(old name.twin)
  200. Result implies not name.is_empty
  201. end
  202. is_absolute_path (path: STRING): BOOLEAN
  203. -- Is `path' absolute, i.e. is its meaning independent of
  204. -- current drive and working directory ? This operation does
  205. -- not perform any disk access.
  206. require
  207. is_valid_path(path)
  208. deferred
  209. ensure
  210. path.is_equal(old path.twin)
  211. end
  212. feature {DIRECTORY_NOTATION}
  213. to_notation (path: STRING; destination_notation: DIRECTORY_NOTATION): STRING
  214. require
  215. is_valid_path(path)
  216. destination_notation /= Void
  217. deferred
  218. ensure
  219. path.is_equal(old path.twin)
  220. Result.is_empty or else destination_notation.is_valid_path(Result)
  221. end
  222. end -- class DIRECTORY_NOTATION
  223. --
  224. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  225. --
  226. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  227. -- of this software and associated documentation files (the "Software"), to deal
  228. -- in the Software without restriction, including without limitation the rights
  229. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  230. -- copies of the Software, and to permit persons to whom the Software is
  231. -- furnished to do so, subject to the following conditions:
  232. --
  233. -- The above copyright notice and this permission notice shall be included in
  234. -- all copies or substantial portions of the Software.
  235. --
  236. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  237. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  238. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  239. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  240. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  241. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  242. -- THE SOFTWARE.