Module: Recls::Ximpl::Util

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

Overview

:nodoc: all

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



90
91
92
93
94
95
96
97
98
99
# File 'lib/recls/ximpl/util.rb', line 90

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_,



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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/recls/ximpl/util.rb', line 273

def self.canonicalise_parts(parts, basename = nil)

  newParts = []

  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



78
79
80
81
82
83
84
# File 'lib/recls/ximpl/util.rb', line 78

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



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
155
156
157
158
159
160
161
162
163
# File 'lib/recls/ximpl/util.rb', line 123

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



68
69
70
71
72
73
# File 'lib/recls/ximpl/util.rb', line 68

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/recls/ximpl/util.rb', line 53

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/recls/ximpl/util.rb', line 167

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



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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/recls/ximpl/util.rb', line 207

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



105
106
107
108
109
110
111
112
# File 'lib/recls/ximpl/util.rb', line 105

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