# The xBase package, initiated June 05, 2010

package.checkclib('xbase');  # has C dynamic link library been found ?

xbase.initstring := 'xBase v1.4.1 as of June 17, 2014\n';

xbase.wipe := proc(f, record :: number) is
   local flag;
   if not xbase.isopen(f) then
      error('Error in `xbase.wipe`: file handle is not valid.')
   elif float(record) or isnonposint(record) then
      error('Error in `xbase.wipe`: record number must be a posint.')
   fi;
   if record > xbase.records(f) then
      error('Error in `xbase.wipe`: record ' & record & ' does not exist.')
   fi;
   xbase.mark(f, record);
   flag := true;
   for i to xbase.fields(f) do  # 2.2.1 correction
      if xbase.purge(f, record, i) = false then
         if flag then  # unmark it if the first field has not yet been overwritten
            xbase.mark(f, record, false)
         fi;
         error('Error in `xbase.wipe`: record ' & i & ' could not be deleted.')
      fi;
      flag := false
   od
end;


# backward compatibility

xbase.isVoid := xbase.isvoid;

xbase.field := proc(f, n :: number, option) is  # 2.2.0 RC 5
   local r;
   if not xbase.isopen(f) then
      error('Error in `xbase.field`: file handle is not valid.')
   elif float(n) or isnonposint(n) or n > xbase.fields(f) then
      error('Error in `xbase.field`: field ' & n & ' does not exist.')
   fi;
   r := xbase.readdbf(f, fields = n);
   return is option = 'set' then toset(r) else r si
end;

