Re: standard read_file()

new topic     » goto parent     » topic index » view thread      » older message » newer message

c.k.lester wrote:
> 
> Jeremy, the read_file() function in EusLibs and the new one in file.e are
> giving different results. I don't know which one is valid, though. The code
> for the EusLibs version is below.
> 
> The only significant difference I see is that EusLibs reads it in binary
> mode, while the standard one does not.
> 
> }}}
<eucode>-- read_file(object f)
> -- returns the contents of file /f
> -- /f can be a filename or a file number as returned by open()
> -- -1 is returned if failed
> global function euslibs_read_file(object f)
> object fn, ret, kar, len
> 	if sequence(f) then
> 		len = file_length(f)
> 		if len = 0 then
> 			return ""
> 		elsif len < 0 then
> 			return -1
> 		end if
> 		
> 		ret = repeat(0, len)
> 		fn = open(f, "rb")
> 		if fn < 0 then return -1 end if
> 		
> 		for i = 1 to len do
> 			ret[i] = getc(fn)
> 		end for
> 		close(fn)
> 	else
> 		ret = ""
> 		while 1 do
> 			kar = getc(f)
> 			if kar != -1 then
> 				ret &= kar
> 			else
> 				exit
> 			end if
> 		end while
> 	end if
> 	return ret
> end function</eucode>
{{{


I thought file.e already had a "read_file()", it's this:

global function getf(sequence filename)
object junk, file, filecontents, readbyte

 file = open(filename,"rb")
 if not equal(file,-1) then
   junk = dir(filename) -- get some info about it
   junk = junk[1][3] -- the filesize
   filecontents = repeat(0,junk)
-- i wish i could set a pointer to bulk memory here, 
-- use allocate(),
-- and later return just this pointer, 
-- which could then be deallocated,
-- saving memory and time
   junk = 0
   readbyte = getc(file)
   while not equal(readbyte,-1) do
     junk += 1
     filecontents[junk] = readbyte
     readbyte = getc(file)
   end while
   close(file)
   return filecontents
 end if
 return -1
end function


Kat

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu