module Proj::FileApiCallbacks

Public Instance Methods

close_callback(context, handle, user_data) click to toggle source

Close file

# File lib/proj/file_api.rb, line 58
def close_callback(context, handle, user_data)
  self.close
end
exists_callback(context, path, user_data) click to toggle source

Return TRUE if a file exists

# File lib/proj/file_api.rb, line 63
def exists_callback(context, path, user_data)
  if self.exists(path)
    1
  else
    0
  end
end
install_callbacks(context) click to toggle source
# File lib/proj/file_api.rb, line 3
def install_callbacks(context)
  proj_file_api = Api::PROJ_FILE_API.new
  proj_file_api[:version] = 1

  # Store procs to instance variables so they don't get garbage collected
  @open_cbk = proj_file_api[:open_cbk] = self.method(:open_callback)
  @read_cbk = proj_file_api[:read_cbk] = self.method(:read_callback)
  @write_cbk = proj_file_api[:write_cbk] = self.method(:write_callback)
  @seek_cbk = proj_file_api[:seek_cbk] = self.method(:seek_callback)
  @tell_cbk = proj_file_api[:tell_cbk] = self.method(:tell_callback)
  @close_cbk = proj_file_api[:close_cbk] = self.method(:close_callback)
  @exists_cbk = proj_file_api[:exists_cbk] = self.method(:exists_callback)
  @mkdir_cbk = proj_file_api[:mkdir_cbk] = self.method(:mkdir_callback)
  @unlink_cbk = proj_file_api[:unlink_cbk] = self.method(:unlink_callback)
  @rename_cbk = proj_file_api[:rename_cbk] = self.method(:rename_callback)

  result = Api.proj_context_set_fileapi(context, proj_file_api, nil)

  if result != 1
    Error.check_object(self)
  end
end
mkdir_callback(context, path, user_data) click to toggle source

Return TRUE if directory exists or could be created

# File lib/proj/file_api.rb, line 72
def mkdir_callback(context, path, user_data)
  if self.mdkir(path)
    1
  else
    0
  end
end
open_callback(context, path, access_mode, user_data) click to toggle source

Open file. Return NULL if error

# File lib/proj/file_api.rb, line 27
def open_callback(context, path, access_mode, user_data)
  result = self.open(path, access_mode)
  result ?  FFI::MemoryPointer.new(:size_t) : nil
end
read_callback(context, handle, buffer, size_bytes, user_data) click to toggle source

Read sizeBytes into buffer from current position and return number of bytes read

# File lib/proj/file_api.rb, line 33
def read_callback(context, handle, buffer, size_bytes, user_data)
  data = self.read(size_bytes)
  read_bytes = [size_bytes, data.size].min
  buffer.write_bytes(data, 0, read_bytes)
  read_bytes
end
rename_callback(context, original_path, new_path, user_data) click to toggle source

Return TRUE if file could be renamed

# File lib/proj/file_api.rb, line 90
def rename_callback(context, original_path, new_path, user_data)
  if self.rename(original_path, new_path)
    1
  else
    0
  end
end
seek_callback(context, handle, offset, whence, user_data) click to toggle source

Seek to offset using whence=SEEK_SET/SEEK_CUR/SEEK_END. Return TRUE in case of success

# File lib/proj/file_api.rb, line 47
def seek_callback(context, handle, offset, whence, user_data)
  self.seek(offset, whence)
  return 1 # True
end
tell_callback(context, handle, user_data) click to toggle source

Return current file position

# File lib/proj/file_api.rb, line 53
def tell_callback(context, handle, user_data)
  self.tell
end
write_callback(context, handle, buffer, size_bytes, user_data) click to toggle source

Write sizeBytes into buffer from current position and return number of bytes written

# File lib/proj/file_api.rb, line 41
def write_callback(context, handle, buffer, size_bytes, user_data)
  data = buffer.get_bytes(0, size_bytes)
  self.write(data)
end