--/makedoc title Documentation for version.e v0.2. --/topic Information --/info -- version.e enables code to decide what is the current interpreter version, -- without outside assistance or knowledge. -- Version 0.1 was released Feb 15, 2007. -- Version 0.2 has more comments and auxilliary functions to recognise and set the -- default string returned instead of the actual interpreter version. Should -- also succeed under more adverse conditions. -- Version 0.3 released Feb 20,2007 tries harder to guess the installation folder. -- -- DISCLAIMER - TERMS OF USE -- -- The software is provided as-is. The author can not be held responsible for anything -- resulting from the use of the current software. -- Permission is granted to freely distribute the code. -- Modifying the code and releasing it is allowed under the provision that --/li the original author is adequately represented as such. --/li each alteration is clearly marked as such. --/topic Authors --/info -- CChris ~ sequence s -- At your discretion, you may use the file as is, or -- uncomment the include statement and comment the constants and function below. --include get.e sequence s constant GET_EOF=-1,CHUNK=100 function get_bytes(integer fn, integer n) -- Copied from RDS' official get.e standard include file. -- so as to avoid the hassles of including files etc integer c, first, last sequence s if n = 0 then return {} end if c = getc(fn) if c = GET_EOF then return {} end if s = repeat(c, n) last = 1 while last < n do -- for speed, read a chunk without checking for EOF first = last+1 last = last+CHUNK if last > n then last = n end if for i = first to last do s[i] = getc(fn) end for -- check for EOF after each chunk if s[last] = GET_EOF then -- trim the EOF's and return while s[last] = GET_EOF do last -= 1 end while return s[1..last] end if end while return s end function integer slash,separator sequence default if platform()>WIN32 then slash='/' separator=':' default="/$HOME/usr/euphoria/" else slash='\\' separator=';' default="C:\\EUPHORIA\\" end if sequence notAVersion notAVersion="0.0.0" --/topic Version Info --/func setNotAVersion(object s) --/desc Possibly sets the string returned when the version is not available. --/ret (SEQUENCE) The current string used to signal failure to determine interpreter version. -- If /i s is an atom, the current value will be returned and kept unchanged. global function setNotAVersinon(object s) sequence oldVal oldVal=notAVersion if sequence(s) then notAVersion=s end if return notAVersion end procedure --/topic Version Info --/func isAVersion(sequence s) --/desc Tells whether a string signals reckon to establish corrent interpreter version. --/ret (INTEGER) 1 if the string is a version string, else 0. -- Use this function to interpret the value returned by /getEuVersion(). global function isAVersion(sequence s) return compare(s,notAVersion)!=0 end function --/topic Version Info --/func getEuVersion() --/desc Returns a string, the version of the Euphoria interpreter used to run this file. --/ret (SEQUENCE) A version strong, or /notAVersion if unavailable or irrelevant. -- Bound or translated programs return /notAVersion. Programs executed from spurce -- might return "2.3" , "3.0.1", etc. If the version isn't reckoned, the function -- returns a default string (initially "0.0.0"). Use /isAVersion() to check -- whether this string is returned, and /setNotAVersion() to get or zset it. global function getEuVersion() integer p,q sequence s1,s2 object x s=command_line() if not compare(s[1],s[2]) then -- the program is not running as source code, hence this is not needed return notAVersion end if s=s[1] -- figure the Euphoria installation folder from the environment. -- If this fails, ... x=getenv("EUDIR") if sequence(x) then s1=x&slash p=length(s1) else -- try looking in the include standard folder x=getenv("EUINC") if sequence(x) then s1=x p=length(s1) else s1=s p=find(slash,s1) if p=0 then -- executable was found, but no explicit path x=getenv("PATH") if sequence(x) then s1=x else s1="" end if while 1 do p=find(separator,s1) if p=0 then -- no path, or path sequence exhausted s1={'.',slash} -- try current dir exit else s2=s1[1..p-1] q=open(s2&slash&s,"r") -- faster than a dir() if q!=-1 then close(q) s1=s2 exit else s1=s1[p+1..length(s1)] end if end if end while end if p=length(s1) while p>0 and s1[p]!=slash do p-=1 end while p-=1 end if while p>0 and s1[p]!=slash do p-=1 end while end if s1=s1[1..p]&"readme.htm" if s1[1]='\"' then -- quoted path s1&='\"' end if p=open(s1,"rb") if p=-1 then -- the best guess didn't work, try platform-dependent defaults p=open(default,"rb") if p=-1 then return notAVersion end if end if q=machine_func(19,{p,1600}) -- seek() if q=0 then return notAVersion end if s1=get_bytes(p,200) -- get a chunk from the file -- hopefully its header didn't change much over the years close(p) p=match("version ",s1) if p>0 then -- return version string s1=s1[p+8..length(s1)] p=match("
",s1) return s1[1..p-1] else return notAVersion end if end function