1. Re: wanted, code to get path from either filename or path\f

> Al,
>
> I am not sure if this is exactly what you are looking for, but the
> following takes a path/file spec and returns drive, path, file and
> extension.
>
> Dave

I have otherwise an include that does some of those things:

(It's also available on the FTP as words.e)
-----8<----- Cut away this line and all above ----->8-----
------------------------------------------------
----  Misc. utility functions by Gargoyle   ----
----  (Anders Eurenius <c96aes at cs.umu.se>)  ----
------------------------------------------------
--      min(objseq) -> obj
--      max(objseq) -> obj
--      abbrev(str a,str b)-> id/bool
--      complete(str tmp,strseq lib)-> int n
--      megs(atom x) -> str
--      spcparse(str) -> (int*int)seq:wordlimits
--      words(str) -> int
--      word(str,int) -> str
--      delword(str,int) -> str
--      split(seq s, seq delimiterlist) -> seqseq
--      splice(seqseq s, obj delimiter)-> seq
--      strhead(str s,atom brkchar) -> seq
--      strtail(str s,atom brkchar) -> seq
--      ask(str s)->bool
--      confirm(str s)->bool
--      tackon(str,str) -> str
--      pathpart(str) -> str
--      namepart(str) -> str

include slice.e
include sort.e
include get.e
include wildcard.e
include set.e

global constant MAXINT=1073741823
global sequence WSPC
constant kMG={{}}&"kMGTPE"

-- min(objseq) -> obj
global function min(sequence s)
object x
 x=s[1]
 for i=1 to length(s) do if compare(s[i],x)=1 then x=s[i] end if end for
 return(x)
end function

-- max(objseq) -> obj
global function max(sequence s)
object x
 x=s[1]
 for i=1 to length(s) do if compare(s[i],x)=1 then x=s[i] end if end for
 return(x)
end function

-- abbrev(str a,str b)-> id/bool
global function abbrev(sequence sa, sequence sb)
atom la,lb
 la=length(sa) lb=length(sb)
 if la<lb then lb=la la=1 else la=2 end if
 for i=1 to lb do if sa[i]!=sb[i] then return(0) end if end for
 return(la)
end function

-- complete(str,strseq)-> int n
global function complete(sequence str,sequence lib)
 for i=1 to length(lib) do
  if abbrev(str,lib[i])=1 then return(i) end if
 end for
 return(0)
end function

-- megs(atom x) -> str
global function megs(atom x)
sequence s
atom z z=1
 if x<0 then s="-" x=-x else s="" end if
 while x>1024 do z=z+1 x=x/1024 end while
 if z=1
  then return(s&sprintf("%d",floor(x)))
  else return(s&sprintf("%.2f",x)&kMG[z])
 end if
end function

-- spcparse(str s) -> <int*int>seq:wordlimits
global function spcparse(sequence s)
sequence lim
atom beg,l

 lim={} beg=-1 l=length(s)
 for i=1 to l do
  if member(WSPC,s[i])!=0 and beg>-1 then lim=lim&{{beg,i-1}} beg=-1 end if
  if member(WSPC,s[i]) =0 and beg=-1 then beg=i end if
 end for
 if beg>-1 then lim=lim&{{beg,l}} end if
 return(lim)
end function

-- words(str s) -> int
global function words(sequence s)
 return(length(spcparse(s)))
end function

-- word(str s, int n) -> str
global function word(sequence s,integer n)
sequence w
 w=spcparse(s)
 if length(w)<n then return({}) end if
 return(ssub(s,w[n][1],w[n][2]))
end function

-- delword(str,int) -> str
global function delword(sequence s,integer n)
sequence w
 w=spcparse(s)
 if length(w)<n then return({}) end if
 return(sdelete(s,w[n][1],w[n][2]+1))
end function

-- split(seq s, seq delimiters) -> seqseq
global function split(sequence s,sequence x)
sequence ix,u
 ix={} u={}
 for i=1 to length(x) do
  ix=union(ix,sfind(s,x[i]))
 end for
 ix=sort(ix)
 if not member(x,s[1]) then ix=0&ix end if
 if not member(x,s[length(s)]) then ix=ix&MAXINT end if
 for i=1 to length(ix)-1 do
  u=u&{ssub(s,ix[i]+1,ix[i+1]-1)}
 end for
 return(u)
end function

-- splice(seqseq s, obj delimiterseq)-> seq
global function splice(sequence s,sequence x)
sequence u
atom l
 u={} l=length(s)
 for i=1 to l-1 do
  u=u&s[i]&x
 end for
 u=u&s[l]
 return(u)
end function

-- strhead(str s,atom brkchar) -> seq
global function strhead(sequence s,atom x)
sequence w w={}
 for i=1 to length(s) do if s[i]=x then exit else w=w&s[i] end if end for
 return(w)
end function

-- strtail(str s,atom brkchar) -> seq
global function strtail(sequence s,atom x)
--sequence w w={}
--      for i=length(s) to 1 by -1 do printf(1,"%s %s\n",{s[i],w}) if s[i]=x
then
exit else w=w&s[i] end if end for --    return(srev(w))
 s=srev(s)
 return(srev(s[1..find(x,s)-1]))
end function

-- ask(str s)->bool
global function ask(sequence s)
 puts(1,s&"[y/N]\n")
 return(lower(wait_key())='y')
end function

-- confirm(str s)->bool
global function confirm(sequence s)
 puts(1,s&"[Y/n]\n")
 return(not lower(wait_key())='n')
end function

-- tackon(str,str) -> str
function tackon(sequence s,sequence x)
integer l l=length(s)
 if l=1 then s=s&":\""&x
 elsif s[l]!='\\' then s=s&"\\"&x
 else s=s&x
 end if
 return(s)
end function

-- pathpart(str) -> str
function pathpart(sequence s)
integer l
 s=split(s,"\\") l=length(s)
 if l<2 then s=s&"\\" else s=splice(s[l-1],"\\") end if
 return(s)
end function

-- namepart(str) -> str
function namepart(sequence s)
integer l
 s=split(s,"\\") l=length(s)
 return(s[l])
end function

WSPC=" \t\r\n"

--
-----8<----- Cut away this line and all below ----->8-----

-------------------------------------------------------------------
Anders Eurenius <c96aes at cs.umu.se> ICQ UIN:1453793
Computer Science/Engineering student at the university of Umeaa

In a world without fences, who needs Gates
-------------------------------------------------------------------

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu