Re: program directory
Martin and Ralf,
Here's my solution to the program directory problem. This is based on some
very old and klunky code of mine, which I have updated and repaired with
some ideas from you and Hawke'. This should work for every situation you
have addressed. If you have any problems, let me know.
------------------------------------------------------------------------
------------------------------------
-- sequence manipulation routines --
------------------------------------
function find_last(object value, sequence s)
-- return the LAST occurrence of value in sequence s
for i = length(s) to 1 by -1 do
if equal(s[i], value) then
return i
end if
end for
return 0
end function
function find_all(object value, sequence s)
-- return a list of all indexes where value is found in s
-- [Hawke''s routine -- thanks! :) ]
sequence list list = {}
for i = 1 to length(s) do
if equal(s[i], value) then
list = append(list, i)
end if
end for
return list
end function
function parse(sequence s, object x)
-- parse s based on delimiter x into a sequence of values
-- NOTE: we append x to s so we have at least one delimiter, at the end
sequence list
integer len, prev, curr
list = find_all(x, append(s, x)) -- list of delimiter positions
len = length(list)
prev = 0
for i = 1 to len do
curr = list[i]
list[i] = s[prev+1..curr-1]
prev = curr
end for
return list -- now, a list of the delimited values
end function
------------------------
-- program_location() --
------------------------
include file.e
function program_location()
-- returns a string value containing the program's location
-- returns -1 if an error occurs
sequence cmd, program, s, list
integer loc
cmd = command_line()
program = cmd[2]
loc = find_last('\\', program)
if loc then
-- the program location is included in the name
return program[1..loc]
elsif find(':', program) = 2 then
-- the program location is included in the name
return program[1..2]
else
-- verify that we have a file extension on our program name
if not find('.', program) then
cmd = cmd[1]
if match("EX.EXE", cmd) then
program &= ".ex"
elsif match("EXW.EXE", cmd) then
program &= ".exw"
else
-- ?!? This should never happen, but...
return -1
end if
end if
if sequence(dir(program)) then
-- it's in the current directory
return ""
else
-- it's somewhere in our PATH
list = parse(getenv("PATH"), ';')
for i = 1 to length(list) do
s = list[i]
if length(s) then
if sequence(dir(s & '\\' & program)) then
return s & '\\'
end if
end if
end for
end if
end if
-- the program's location was not found!
-- (this should never happen, but just in case...)
return -1
end function
------------------------------------------------------------------------
Be seeing you,
Gabriel Boehme
------
Men do not differ much about what things they will call evils; they differ
enormously about what evils they will call excusable.
G.K. Chesterton
------
|
Not Categorized, Please Help
|
|