Re: cross-platform library for file system operations

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

Ok, I've added the minor changes to add linux support for the functions
del_file() and del_dir(). It took only a few minutes. (My secret: /usr/bin/man).

I'll add linux support for the other functions as soon as I know what they are.

jbrown

--CUT HERE--
-- cross-platform file system routines for Euphoria 2.3
-- version 0.001  (just started)
-- by Juergen Luethje, 19.05.02
-- Linux support by jbrown 5/19/02
-- I tested them, but:     *** use at your own risk! ***

-- Global functions:
--    success = del_file(filename)
--    success = del_dir(dirname)  / dirname can be with or without trailing
backslash

--====================================================================--
-- Initialization

-- from misc.e
constant DOS32 = 1,
		 WIN32 = 2,
		 LINUX = 3

include machine.e
include dll.e

constant true = 1
constant false = 0

-- after dos.e
type string (sequence s)
   for loop = 1 to length(s) do
	  if sequence(s[loop]) then
		 return false
	  end if
   end for
   return true
end type

function dosver()
   sequence reg_list

   reg_list = repeat(0,10)
   reg_list[REG_AX] = #3000
   reg_list = dos_interrupt(#21,reg_list)
   return remainder(reg_list[REG_AX],256) + floor(reg_list[REG_AX]/256)/100
end function

integer short_names
if platform() = DOS32 then
   short_names = dosver() < 7 or atom(getenv("windir"))
end if


atom kernel32, DeleteFile_, RemoveDirectory_
if platform() = WIN32 then
   -- get handles to all dll routines that we need
   kernel32 = open_dll("kernel32.dll")
DeleteFile_      = define_c_func(kernel32, "DeleteFileA", {C_POINTER},
   C_LONG)
RemoveDirectory_ = define_c_func(kernel32, "RemoveDirectoryA", {C_POINTER},
   C_LONG)
end if

atom dll, unlink_, rmdir_
if platform() = LINUX then
	dll = open_dll("")
	unlink_ = define_c_func(dll, "unlink", {C_POINTER}, C_INT)
	rmdir_ = define_c_func(dll, "rmdir", {C_POINTER}, C_INT)
end if

--====================================================================--
-- platform specific del_file() functions

function del_file_DOS32 (string filename)
-- from dos.e

   atom low_buff
   sequence reg_list

   low_buff = allocate_low(length(filename) + 1)
   if not low_buff then
	  return false       -- no success
   end if
   poke(low_buff, filename & 0)
   reg_list = repeat(0,10)
   if short_names then
	  reg_list[REG_AX] = #4100
   else
	  reg_list[REG_AX] = #7141
   end if
   reg_list[REG_DS] = floor(low_buff / 16)
   reg_list[REG_DX] = remainder(low_buff, 16)
   reg_list[REG_SI] = #0000
   reg_list[REG_FLAGS] = or_bits(reg_list[REG_FLAGS], 1)
   reg_list = dos_interrupt(#21, reg_list)
   free_low(low_buff)
   if and_bits(reg_list[REG_FLAGS], 1) != 0 then
	  return false       -- no success
   else
	  return true        -- success
   end if
end function


function del_file_WIN32 (string filename)
   atom lpfilename
   integer flag

   lpfilename = allocate_string(filename)
   flag = c_func(DeleteFile_, {lpfilename})
   free(lpfilename)
   return flag      -- success (true/false)
end function


function del_file_LINUX (string filename)
	atom ret, fstr
	fstr = allocate_string(filename)
	ret = c_func(unlink_, {fstr})
	free(fstr)
	return (ret = 0) --0 return on sucess, -1 on error
end function


------------------------------------------------------------------------
-- general del_file() function

global function del_file (string filename)
-- returns success (true/false)

   if platform() = DOS32 then
	  return del_file_DOS32(filename)
   elsif platform() = WIN32 then
	  return del_file_WIN32(filename)
   else
	  return del_file_LINUX(filename)
   end if
end function


--====================================================================--
-- platform specific del_dir() functions

function del_dir_DOS32 (string dirname)
-- modified after dos.e

   atom low_buff
   sequence reg_list

   -- remove trailing backslash [JuLu]
   if dirname[length(dirname)] = '\\' then
	  dirname = dirname[1..length(dirname)-1]
   end if

   low_buff = allocate_low(length(dirname) + 1)
   if not low_buff then
	  return false       -- no success
   end if
   poke(low_buff, dirname & 0)
   reg_list = repeat(0,10)
   if short_names then
	  reg_list[REG_AX] = #3A00
   else
	  reg_list[REG_AX] = #713A
   end if
   reg_list[REG_DS] = floor(low_buff / 16)
   reg_list[REG_DX] = remainder(low_buff, 16)
   reg_list[REG_FLAGS] = or_bits(reg_list[REG_FLAGS], 1)
   reg_list = dos_interrupt(#21, reg_list)
   free_low(low_buff)
   if and_bits(reg_list[REG_FLAGS], 1) != 0 then
	  return false       -- no success
   else
	  return true        -- success
   end if
end function


function del_dir_WIN32 (string dirname)
   atom lpdirname
   integer flag

   lpdirname = allocate_string(dirname)
   flag = c_func(RemoveDirectory_, {lpdirname})
   free(lpdirname)
   return flag      -- success (true/false)
end function


function del_dir_LINUX (string dirname)
	atom ret, dstr
	dstr = allocate_string(dirname)
	ret = c_func(rmdir_, {dstr})
	return (ret = 0) --0 return on sucess, -1 on error
end function


------------------------------------------------------------------------
-- general del_dir() function

global function del_dir (string dirname)
-- returns success (true/false)

   if platform() = DOS32 then
	  return del_dir_DOS32(dirname)
   elsif platform() = WIN32 then
	  return del_dir_WIN32(dirname)
   else
	  return del_dir_LINUX(dirname)
   end if
end function


--====================================================================--
--CUT HERE--



-- 
http://fastmail.fm - You've just been FastMailed!

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

Search



Quick Links

User menu

Not signed in.

Misc Menu