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