#38 Enhancements to FILESPEC

None
open
nobody
5
2012-08-22
2005-02-07
mdlueck
No

Below is LDSFileSpec from my Object CUR. It handles
some things more gracefully than IBM's code. Support
for UNC exists. Also it returns '' vs 0 so it is safe
to cross platform ask for drive || path and not get
'0/blablabla...' on *nix platforms nor have to check OS
and do something different.

::method LDSFileSpec
expose LDSSystemInfo LDSLogging
/ DEV / if LDSSystemInfo~tracemode\='' then interpret
'trace 'LDSSystemInfo~tracemode
RUNmode=arg(1)
FINDfilename=arg(2)

/ DEV /
LDSLogging~Verbose('LDSRexxUtil~LDSFileSpec,
RUNmode='RUNmode' FINDfilename='FINDfilename)

/ Check Params /
if strip(RUNmode)='' | strip(FINDfilename)='' then do
LDSLogging~Logger('LDSRexxUtil~LDSFileSpec ERROR!
-> Not Enough Paramaters')
return 0
end

RUNmode=translate(strip(RUNmode))
FINDfilename=strip(FINDfilename)

/ See what slash we are using /
if pos('\', FINDfilename)\=0 & pos('/',
FINDfilename)\=0 then do
LDSLogging~Logger('LDSRexxUtil~LDSFileSpec ERROR!
-> FINDfilename contains both slashes, can not process
FINDfilename')
return 0
end

if pos('/', FINDfilename)=0 then do
slash='\'
doubleslash='\'
end
else do
slash='/'
doubleslash='//'
end

select
when RUNmode='DRIVE' then do
FINDstring=left(FINDfilename, 2)
select
when datatype(left(FINDstring, 1), 'M') &
right(FINDstring, 1)=':' then do
/ DOS support, no extra code needed /
nop
end / DOS /
when FINDstring=doubleslash then do
/ UNC mode /
NEXTpos=pos(slash, right(FINDfilename,
length(FINDfilename)-2))
if NEXTpos=0 then do
FINDstring=''
end
else do
FINDstring=left(FINDfilename, NEXTpos+1)
end
end / UNC /
otherwise do
FINDstring=''
end
end / select /
end

  when RUNmode='PATH' then do
     select
        when pos(slash, FINDfilename)\=0 &

pos(doubleslash, FINDfilename)=0 then do
/ DOS mode /
FIRSTpos=pos(slash, FINDfilename)
LASTpos=lastpos(slash, FINDfilename)
if FIRSTpos=LASTpos then do
FINDstring=slash
end
else do
FINDstring=substr(FINDfilename,
FIRSTpos, LASTpos-FIRSTpos+1)
end
end / DOS /
when left(FINDfilename, 2)=doubleslash then do
/ UNC mode /
parse value FINDfilename with . =3
FINDfilename
FIRSTpos=pos(slash, FINDfilename)
LASTpos=lastpos(slash, FINDfilename)
if FIRSTpos=LASTpos then do
FINDstring=slash
end
else do
FINDstring=substr(FINDfilename,
FIRSTpos, LASTpos-FIRSTpos+1)
end
end / UNC /
otherwise do
FINDstring=''
end
end / select /
end

  when RUNmode='NAME' then do
     if pos(slash, FINDfilename)=0 then do
        FINDstring=''
     end
     else do
        FINDstring=right(FINDfilename,

length(FINDfilename)-lastpos(slash, FINDfilename))
end
end

  otherwise do
     LDSLogging~Logger('LDSRexxUtil~LDSFileSpec

ERROR! -> RUNmode=' || RUNmode || ' is not a supported
RUNmode')
FINDstring=''
end
end / select /

return FINDstring

Discussion

  • Jon Wolfers
    Jon Wolfers
    2008-08-10

    Logged In: YES
    user_id=667060
    Originator: NO

    Hi Michael,

    I was looking at this and wondered whether a class might not be the way to go with this, such as you could do

    a = .filespec~new('c:\My Documents\rexx is great.doc')
    say a~drive
    say a~path
    say a~name

    or even

    say .filespec~new('c:\My Documents\rexx is great.doc')~name

    I quickly knocked up a class based on your code & wondered what you thought and whether you would be happy for me to take this further. I think it could benefit from suitable MakeArray and String Methods

    As I don't seem to be able to attach a file to this tracker I include the code inline as you did.

    Jon

    / ========================================================================= /
    ::class filespec public
    / ========================================================================= /
    ::attribute Drive get
    ::attribute Drive set private
    ::attribute Path get
    ::attribute Path set private
    ::attribute Name get
    ::attribute Name set private
    ::attribute error
    / ------------------------------------------------------------------------- /
    ::method init
    / ------------------------------------------------------------------------- /
    expose name path drive

    use strict arg filespec

    self~error = .false

    -- filespec = filespec~strip -- not sure this might not cause an error

    / See what slash we are using /
    if filespec~pos('\') \=0 & filespec~pos('/') \= 0
    then do
    self~error = .true
    return self~error
    end

    if filespec~pos('/') = 0
    then slash='\'
    else slash='/'
    doubleSlash = slash~copies(2)

    / set the drive /
    drive = filespec~left(2)
    select
    when drive~left(1)~datatype('M') & drive~right(1)=':'
    then nop / DOS support, no extra code needed /
    when drive = doubleslash
    then do / UNC mode /
    NEXTpos = filespec~right(filespec~length - 2)~pos(slash)
    if NEXTpos = 0
    then drive = ''
    else drive = filespec~left(NEXTpos + 1)
    end / UNC /
    otherwise drive = ''
    end / select /

    / set the name /
    if filespec~pos(slash) = 0
    then name = ''
    else name = filespec~right(filespec~length -filespec~lastpos(slash))

    / set the path /
    select
    when filespec~pos(slash) \= 0 & filespec~pos(doubleslash) = 0
    then do / DOS mode /
    FIRSTpos = filespec~pos(slash)
    LASTpos = filespec~lastpos(slash)
    if FIRSTpos = LASTpos
    then path = slash
    else path = filespec~substr(FIRSTpos, LASTpos-FIRSTpos+1)
    end / DOS /
    when filespec~left(2) = doubleslash
    then do / UNC mode /
    parse value filespec with . =3 filespec
    FIRSTpos = filespec~pos(slash)
    LASTpos = filespec~lastpos(slash)
    if FIRSTpos = LASTpos
    then path = slash
    else path = filespec~substr(FIRSTpos, LASTpos-FIRSTpos+1)
    end / UNC /
    otherwise
    path = ''
    end / select /

     
  • Rick McGuire
    Rick McGuire
    2008-08-10

    Logged In: YES
    user_id=1125291
    Originator: NO

    I sort of have a longer range plan of having a File class object that implements a lot of the function currently in rexxutils in a class, including things like filespec capabilities. This will be loosely based on the Java File class, and portions of this will need to be implemented via native code. I've sort of been waiting for the new APIs to get fully implemented before taking a crack at this.

     
  • Rick McGuire
    Rick McGuire
    2008-08-10

    Logged In: YES
    user_id=1125291
    Originator: NO

    Actually, this might make a good incubator project. It could be implemented initially mostly in ooRexx, using the rexxutils code for anything where native access is required.

     
  • Jon Wolfers
    Jon Wolfers
    2008-08-10

    Logged In: YES
    user_id=667060
    Originator: NO

    Hi Rick,

    Sounds very good - I'll leave well alone.
    I take it you did see the recent thread on comp.lang.rexx about filenames starting with blanks etc!

    Jon

     
  • Rick McGuire
    Rick McGuire
    2008-08-10

    Logged In: YES
    user_id=1125291
    Originator: NO

    Yes, I saw that discussion. The appropriate solution is the File class, which eliminates the need for parsing the information entirely.

    Rick

     
  • Jon Wolfers
    Jon Wolfers
    2008-08-11

    Logged In: YES
    user_id=667060
    Originator: NO

    Hi Rick,

    A couple of our comments crossed in the email, leaving me confused . When you said:

    Actually, this might make a good incubator project. It could be
    implemented initially mostly in ooRexx, using the rexxutils code for
    anything where native access is required.

    were you encouraging me to go ahead with what I was proposing?
    It seems more likely to me that you meant the file class that you were speaking of.

    thanks, Jon

     
  • Rick McGuire
    Rick McGuire
    2008-08-11

    Logged In: YES
    user_id=1125291
    Originator: NO

    I was talking about the file class I was proposing...a good first start at that sort of class can be done by just using the the rexxutils functions, since a lot of the code would be written in ooRexx anyway, we can get a good first start on defining the methods and playing around with the capability. Methods such as the ones you were proposing would be a natural fit on that class.

     


Anonymous


Cancel   Add attachments