/src/lib/io/filesystem/path_name/path_name_notation.e

http://github.com/tybor/Liberty · Specman e · 251 lines · 187 code · 28 blank · 36 comment · 11 complexity · 3d61b540ca9ba46d0a2699b90c3208c0 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 PATH_NAME_NOTATION
  5. inherit
  6. DIRECTORY_NOTATION
  7. PATH_JOINER
  8. redefine
  9. join_directory, join_file
  10. end
  11. feature {ANY} -- DIRECTORY_NOTATION interface
  12. to_parent_directory (some_path: STRING)
  13. do
  14. --*** PATH_NAME and DIRECTORY_NOTATION differ on corner cases, which is why this implementation is
  15. --*** not as straightforward as expected.
  16. tmp1.make_from_string(some_path)
  17. if tmp1.is_empty then
  18. -- Make it even emptier
  19. tmp1.make_empty
  20. else
  21. if tmp1.last.is_empty then
  22. tmp1.remove_last
  23. end
  24. if not tmp1.is_empty then
  25. tmp1.remove_last
  26. end
  27. end
  28. some_path.copy(tmp1.to_string)
  29. if not some_path.is_empty then
  30. to_directory_path(some_path)
  31. end
  32. end
  33. to_subdirectory_with (parent_path, entry_name: STRING)
  34. local
  35. ds: STRING
  36. do
  37. --*** PATH_NAME and DIRECTORY_NOTATION differ on corner cases, which is why this implementation is
  38. --*** not as straightforward as expected.
  39. tmp1.make_from_string(parent_path)
  40. if tmp1.is_empty then
  41. ds := tmp1.drive_specification
  42. if ds /= Void and then not ds.is_empty then
  43. tmp2.make_root
  44. tmp2.join_to(tmp1)
  45. end
  46. end
  47. tmp1.start_join(Void, 0)
  48. tmp1.join_directory(entry_name)
  49. tmp1.end_join
  50. parent_path.copy(tmp1.to_string)
  51. to_directory_path(parent_path)
  52. end
  53. to_file_path_with (parent_path, file_name: STRING)
  54. local
  55. ds: STRING
  56. do
  57. --*** PATH_NAME and DIRECTORY_NOTATION differ on corner cases, which is why this implementation is
  58. --*** not as straightforward as expected.
  59. tmp1.make_from_string(parent_path)
  60. if tmp1.is_empty then
  61. ds := tmp1.drive_specification
  62. if ds /= Void and then not ds.is_empty then
  63. tmp2.make_root
  64. tmp2.join_to(tmp1)
  65. end
  66. end
  67. tmp1.start_join(Void, 0)
  68. tmp1.join_file(file_name)
  69. tmp1.end_join
  70. parent_path.copy(tmp1.to_string)
  71. end
  72. to_subpath_with (parent_path, subpath: STRING)
  73. do
  74. tmp1.make_from_string(parent_path)
  75. tmp2.make_from_string(subpath)
  76. tmp1.join(tmp2)
  77. parent_path.copy(tmp1.to_string)
  78. end
  79. can_map_drive (source_notation: DIRECTORY_NOTATION; drive: STRING): BOOLEAN
  80. do
  81. Result := True
  82. end
  83. to_root (source_notation: DIRECTORY_NOTATION; drive: STRING)
  84. do
  85. -- *** `source_notation' is not taken into account
  86. tmp1.make_empty
  87. tmp1.start_join(drive, 1)
  88. tmp1.end_join
  89. drive.copy(tmp1.to_string)
  90. end
  91. to_default_root (directory: STRING)
  92. do
  93. tmp1.make_root
  94. directory.copy(tmp1.to_string)
  95. end
  96. to_current_directory (directory: STRING)
  97. do
  98. tmp1.make_current
  99. directory.copy(tmp1.to_string)
  100. end
  101. is_absolute_path (path: STRING): BOOLEAN
  102. do
  103. tmp1.make_from_string(path)
  104. Result := tmp1.is_absolute
  105. end
  106. is_valid_path (a_path: STRING): BOOLEAN
  107. do
  108. Result := tmp1.is_valid_path(a_path)
  109. end
  110. is_valid_directory_path (a_path: STRING): BOOLEAN
  111. do
  112. Result := tmp1.is_valid_path(a_path)
  113. if Result then
  114. tmp1.make_from_string(a_path)
  115. Result := tmp1.is_valid_directory
  116. end
  117. end
  118. is_valid_file_name (name: STRING): BOOLEAN
  119. do
  120. Result := tmp1.is_valid_file_name(name)
  121. end
  122. to_short_name_in (buffer, path: STRING)
  123. do
  124. tmp1.make_from_string(path)
  125. buffer.copy(tmp1.short_name)
  126. end
  127. feature {DIRECTORY_NOTATION}
  128. to_notation (path: STRING; destination: DIRECTORY_NOTATION): STRING
  129. local
  130. pnn: PATH_NAME_NOTATION
  131. do
  132. if pnn ?:= destination then
  133. pnn ::= destination
  134. tmp2.make_from_string(path)
  135. Result := pnn.from_path_name(tmp2)
  136. else
  137. destination_notation := destination
  138. current_path := path
  139. tmp1.make_from_string(path.twin)
  140. tmp1.join_to(Current)
  141. Result := current_path
  142. end
  143. end
  144. feature {PATH_JOINER}
  145. start_join (drive: STRING; absoluteness: INTEGER)
  146. do
  147. -- *** some information about absoluteness is lost
  148. if absoluteness = 0 then
  149. -- *** `drive' is not taken into account
  150. destination_notation.to_current_directory(current_path)
  151. elseif destination_notation.can_map_drive(Current, drive) then
  152. current_path.copy(drive)
  153. destination_notation.to_root(Current, current_path)
  154. else
  155. destination_notation.to_default_root(current_path)
  156. end
  157. end
  158. join_directory (element: STRING)
  159. do
  160. destination_notation.to_subdirectory_with(current_path, element)
  161. end
  162. join_up
  163. do
  164. -- *** Sometimes, we must actually join ".." or its
  165. -- equivalent instead
  166. destination_notation.to_parent_directory(current_path)
  167. end
  168. join_file (element: STRING)
  169. do
  170. destination_notation.to_file_path_with(current_path, element)
  171. end
  172. join_element (element: STRING)
  173. do
  174. destination_notation.to_subpath_with(current_path, element)
  175. end
  176. join_extension (an_extension: STRING)
  177. do
  178. --*** Not a very good idea
  179. current_path.extend('.')
  180. current_path.append(an_extension)
  181. end
  182. end_join
  183. do
  184. end
  185. join_error: BOOLEAN False
  186. feature {PATH_NAME_NOTATION}
  187. from_path_name (other: PATH_NAME): STRING
  188. require
  189. other /= tmp1
  190. do
  191. tmp1.make_empty
  192. other.join_to(tmp1)
  193. Result := tmp1.to_string
  194. end
  195. feature {}
  196. tmp1, tmp2: PATH_NAME
  197. deferred
  198. ensure
  199. tmp1 /= tmp2
  200. end
  201. current_path: STRING
  202. destination_notation: DIRECTORY_NOTATION
  203. end -- class PATH_NAME_NOTATION
  204. --
  205. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  206. --
  207. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  208. -- of this software and associated documentation files (the "Software"), to deal
  209. -- in the Software without restriction, including without limitation the rights
  210. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  211. -- copies of the Software, and to permit persons to whom the Software is
  212. -- furnished to do so, subject to the following conditions:
  213. --
  214. -- The above copyright notice and this permission notice shall be included in
  215. -- all copies or substantial portions of the Software.
  216. --
  217. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  218. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  219. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  220. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  221. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  222. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  223. -- THE SOFTWARE.