1. Modified PSplit.e drive/dir/name/ext spliter, still 1 prob, help.

Someone sent PSplit.e to the mailing list after i made a request.
I've corrected some bugs, and listed what I couldn't easily fix.
If someone can fix the commented bug, please send the code to this list, thanks.


-- PSplit.e modified to correctly process root dir and
-- filenames without an extension. Most mod's are --'ed
-- I didn't write the original PSplit.
-- Test code at end uses command line.

-- Still doesn't find the correct drive/dir/fn/ext for
-- "D:filename.ext" or "D:" , Need interrupt or function
-- to get current dir/path of 'D:' --BUG--

with trace
trace(0)

include file.e

global constant -- elements of returned sequence
  PSplit_DRIVE = 1,
  PSplit_DIRECTORY = 2,
  PSplit_FILENAME = 3,
  PSplit_EXTENSION = 4

global function PSplit(sequence Path)
-- Split a full path name to Drive, Directory, FileName and Extension

  atom Drive
  sequence Directory, FileName, Extension, ReturnValue

  atom Pos, Len
  sequence CurDir

  CurDir = current_dir()
  Pos = find(':',Path)
  if Pos > 0 then
    Drive = Path[Pos-1]
    Path = Path[Pos+1..length(Path)]
  else
    Drive = CurDir[1]
  end if

  Len = length(Path)
  Directory = ""
  for ix = Len to 1 by -1 do
    if compare(Path[ix],'\\') = 0 then
      if ix != 1 then  -- if dir is not root
        Directory = Path[1..ix-1]
        Path = Path[ix+1..length(Path)]
        exit
      else
        Directory = Path[1..ix] -- dir is root. dir = \
        Path = Path[ix+1..length(Path)]
      end if
    end if
    if compare(Path[ix], ':') = 0 then
      Directory = CurDir[3..length(CurDir)] -- needed?
      exit
    end if
  end for
  if compare(Directory, "") = 0 then
    Directory = CurDir[3..length(CurDir)]
  end if
  if compare(Directory, "\\") = 0 then -- fix dir = \
    Directory = "" -- ok to append "\" now
  end if

  Len = length(Path)
  FileName = ""
  Extension = ""

 if match(".",Path) != 0 then -- does extension exist?
   for ix = Len to 1 by -1 do
     if compare(Path[ix], '.') = 0 then
       FileName = Path[1..ix-1]
       Extension = Path[ix+1..length(Path)]
       exit
     end if
   end for
 else
   FileName = Path[1..Len] -- no ext, so ignore '.'
 end if

  ReturnValue = {Drive, Directory, FileName, Extension}
  return ReturnValue
end function


-- program to test global function PSplit(x) below

object s
sequence cmd_line
         cmd_line = command_line()

if length(cmd_line) < 3 then cmd_line = cmd_line & {""} end if
s = PSplit(cmd_line[3])
puts(1,"\n    Drive: " & s[PSplit_DRIVE])
puts(1,"\nDirectory: " & s[PSplit_DIRECTORY])
puts(1,"\n Filename: " & s[PSplit_FILENAME])
puts(1,"\nExtension: " & s[PSplit_EXTENSION] & "\n")

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu