Module: Recls::Ximpl::Util

Defined in:
lib/recls/ximpl/util.rb

Class Method Summary collapse

Class Method Details

.append_trailing_slash(p, slash = nil) ⇒ Object

appends trailing slash to a path if not already present

dependencies: none



86
87
88
89
90
91
92
93
94
95
# File 'lib/recls/ximpl/util.rb', line 86

def self.append_trailing_slash(p, slash = nil)

	return p if not p or p.empty?

	return p if self.is_path_name_separator(p[-1])

	slash = '/' if not slash

	"#{p}#{slash}"
end

.canonicalise_parts(parts, basename = nil) ⇒ Object

Returns a tuple consisting of:

f1. The canonicalised array of parts f2. A boolean indicating whether to ‘consume’ the basename

dependencies: OS.is_root_dir_, OS.get_number_of_dots_dir_,



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/recls/ximpl/util.rb', line 252

def self.canonicalise_parts(parts, basename = nil)

	newParts = []

	trailing_slash = parts.empty? ? nil : self.get_trailing_slash(parts[-1])

	lastSingleDots = nil

	path_is_rooted = nil

	index = -1
	parts.each do |part|

		index += 1

		next if not part
		next if part.empty?

		if path_is_rooted.nil?
			path_is_rooted = self.is_path_name_separator(part[0])
		end

		if ?. == part[0]
			if self.is_path_name_separator(part[1])
				# single dots, so ...

				# ... remember the last instance, and ...
				lastSingleDots = part

				# ... skip to leave this out of the result
				next
			elsif ?. == part[1]
				if self.is_path_name_separator(part[2])
					# double dots, so ...
					# ... skip this and pop prior from the new list iff:
					#
					# 1. there is a prior elements in the new list (size > 1); AND
					# 2. the last element in the new list is not the root directory; AND
					# 3. the last element in the list is not a dots directory
					if not newParts.empty? # 1.
						priorPart = newParts[-1]
						if 1 == newParts.size and OS.is_root_dir_(priorPart)
							# 2.
							next
						else
							dirtype = OS.get_number_of_dots_dir_(priorPart)
							if 0 == dirtype # 3.
								if newParts.pop
									next
								end
							end
						end
					end
				else
					# it's a ..X part
				end
			else
				# it's a .X part
			end
		else
			# it's a non-dots part
		end

		newParts << part
	end

	consume_basename = false

	if basename
		if ?. == basename[0]
			if 1 == basename.size
				# single dots
				if newParts.empty?
					lastSingleDots = false
				else
					consume_basename = true
				end
			elsif ?. == basename[1] and 2 == basename.size
				# double dots, so ...
				#
				# ... pop unless we already have some outstanding double dots
				if newParts.empty?
					newParts << '..'
					consume_basename = true
				elsif 1 == newParts.size && 1 == newParts[0].size && Util.is_path_name_separator(newParts[0][0])
					consume_basename = true
				else
					if 2 != OS.get_number_of_dots_dir_(newParts[-1])
						newParts.pop
						consume_basename = true
					end
				end
			end
		end
	end

	# push lastSingleDots (which may contain a trailing slash) if
	# exists and newParts is empty
	newParts << lastSingleDots if lastSingleDots and newParts.empty?

	if not newParts.empty?
		if 2 == OS.get_number_of_dots_dir_(newParts[-1])
			# the last element is the double-dots directory, but
			# need to determine whether to ensure/remote a
			# trailing slash
			if basename and not basename.empty?
				if not consume_basename
					# leave as is
				else
					# 
					newParts[-1] = '..'
				end
			end
		end
	else
		# handle case where all (double)-dots have eliminated
		# all regular directories
		if not basename or basename.empty? or consume_basename
			newParts << '.'
		end
	end

	[ newParts.join(''), consume_basename ]
end

.get_trailing_slash(p, args = {}) ⇒ Object

returns the trailing slash, or nil if none present

dependencies: none



74
75
76
77
78
79
80
# File 'lib/recls/ximpl/util.rb', line 74

def self.get_trailing_slash(p, args = {})

	return nil if p.nil?
	return nil if p.empty?

	return self.is_path_name_separator(p[-1]) ? p[-1] : nil
end

.get_windows_root(p) ⇒ Object

From path p, returns a tuple containing either:

[ nil, p ] if p does not contain a Windows root, or

[ wroot, remainder ] if p does contain a Windows root, or

[ nil, nil ] if p is nil

dependencies: none



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/recls/ximpl/util.rb', line 119

def self.get_windows_root(p)

	return [ nil, nil ] if not p

	if Recls::Ximpl::OS::OS_IS_WINDOWS

		# Windows local drive (e.g. 'H:')
		#
		# NOTE: this works for both rooted and unrooted paths
		if p =~ /^([a-zA-Z]:)/
			return [ $1, $' ]
		end

		# UNC network drive
		#
		# NOTE: there are several permutations ...
		if p =~ /^(\\\\[^\\\/:*?<>|]+\\[^\\\/:*?<>|]+)([\\\/].*)$/
			# \\server\share{\{... rest of path}}
			return [ $1, $2 ]
		end
		if p =~ /^(\\\\[^\\\/:*?<>|]+\\[^\\\/:*?<>|]+)$/
			# \\server\share
			return [ $1, nil ]
		end
		if p =~ /^(\\\\[^\\\/:*?<>|]+\\)$/
			# \\server\
			return [ $1, nil ]
		end
		if p =~ /^(\\\\[^\\\/:*?<>|]+)$/
			# \\server
			return [ $1, nil ]
		end
	end

	return [ nil, p ]
end

.has_trailing_slash(p) ⇒ Object

Indicates whether a trailing slash is on the given path

dependencies: none



64
65
66
67
68
69
# File 'lib/recls/ximpl/util.rb', line 64

def self.has_trailing_slash(p)

	return p if p.nil? or p.empty?

	return self.is_path_name_separator(p[-1])
end

.is_path_name_separator(c) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/recls/ximpl/util.rb', line 50

def self.is_path_name_separator(c)

	return true if ?/ == c

	if Recls::Ximpl::OS::OS_IS_WINDOWS
		return true if ?\\ == c
	end

	return false
end

.path_parts(path) ⇒ Object

obtains the parts from a path, including any Windows root and the file basename



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/recls/ximpl/util.rb', line 158

def self.path_parts(path)

	return nil if path.nil?
	return [] if path.empty?

	parts = []

	wr, rem =  self.get_windows_root(path)

	parts << wr if wr

	until rem.nil? || rem.empty?
		if rem =~ /^([^\\\/]*[\\\/])/
			parts << $1
			rem = $'
		else
			parts << rem
			rem = ''
		end
	end

	parts
end

.split_path(p) ⇒ Object

Returns a tuple consisting of the following elements (or nil, for any element that is not) present

f1. Windows root, or nil f2. directory, or nil f3. basename, or nil f4. basename-minus-extension, or nil f5. extension, or nil f6. array of directory path parts, which may be empty (not nil) f7. array of all path parts, which may be empty (not nil)

dependencies: Util.path_parts, Util.get_windows_root



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/recls/ximpl/util.rb', line 195

def self.split_path(p)

	f1_windows_root, remainder = self.get_windows_root p
	f1_windows_root = nil if not f1_windows_root or f1_windows_root.empty?
	remainder = nil if not remainder or remainder.empty?

	if not remainder or remainder.empty?
		f2_directory = nil
		f3_basename = nil
		f4_nameonly = nil
		f5_extension = nil
	else
		if remainder =~ /^(.*[\\\/])([^\\\/]*)$/
			f2_directory = $1
			f3_basename = $2
		else
			f2_directory = nil
			f3_basename = remainder
			f4_nameonly = nil
			f5_extension = nil
		end

		f2_directory = nil if not f2_directory or f2_directory.empty?
		f3_basename = nil if not f3_basename or f3_basename.empty?

		if f3_basename
			# special case: treat '.' and '..' as file-name only
			if '.' == f3_basename or '..' == f3_basename
				f4_nameonly = f3_basename
				f5_extension = nil
			elsif f3_basename =~ /^(.*)(\.[^.]*)$/
				f4_nameonly = $1
				f5_extension = $2
			else
				f4_nameonly = f3_basename
				f5_extension = nil
			end
		else
			f4_nameonly = nil
			f5_extension = nil
		end
	end

	f4_nameonly = nil if not f4_nameonly or f4_nameonly.empty?
	f5_extension = nil if not f5_extension or f5_extension.empty?
	f6_directory_parts = self.path_parts(f2_directory)
	f7_path_parts = self.path_parts(p)

	return [ f1_windows_root, f2_directory, f3_basename, f4_nameonly, f5_extension, f6_directory_parts, f7_path_parts ]
end

.trim_trailing_slash(p) ⇒ Object

trims trailing slash from a path, unless it is the root

dependencies: none



101
102
103
104
105
106
107
108
# File 'lib/recls/ximpl/util.rb', line 101

def self.trim_trailing_slash(p)

	return p if not p or p.empty?

	p = p[0 ... -1] if self.is_path_name_separator(p[-1])

	return p
end