Module: Proj::FileApiCallbacks
- Included in:
- FileApiImpl
- Defined in:
- lib/proj/file_api.rb
Instance Method Summary collapse
-
#close_callback(context, handle, user_data) ⇒ Object
Close file.
-
#exists_callback(context, path, user_data) ⇒ Object
Return TRUE if a file exists.
- #install_callbacks(context) ⇒ Object
-
#mkdir_callback(context, path, user_data) ⇒ Object
Return TRUE if directory exists or could be created.
-
#open_callback(context, path, access_mode, user_data) ⇒ Object
Open file.
-
#read_callback(context, handle, buffer, size_bytes, user_data) ⇒ Object
Read sizeBytes into buffer from current position and return number of bytes read.
-
#rename_callback(context, original_path, new_path, user_data) ⇒ Object
Return TRUE if file could be renamed.
-
#seek_callback(context, handle, offset, whence, user_data) ⇒ Object
Seek to offset using whence=SEEK_SET/SEEK_CUR/SEEK_END.
-
#tell_callback(context, handle, user_data) ⇒ Object
Return current file position.
-
#unlink_callback(context, path, user_data) ⇒ Object
Return TRUE if file could be removed.
-
#write_callback(context, handle, buffer, size_bytes, user_data) ⇒ Object
Write sizeBytes into buffer from current position and return number of bytes written.
Instance Method Details
#close_callback(context, handle, user_data) ⇒ Object
Close file
58 59 60 |
# File 'lib/proj/file_api.rb', line 58 def close_callback(context, handle, user_data) self.close end |
#exists_callback(context, path, user_data) ⇒ Object
Return TRUE if a file exists
63 64 65 66 67 68 69 |
# 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) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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) ⇒ Object
Return TRUE if directory exists or could be created
72 73 74 75 76 77 78 |
# 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) ⇒ Object
Open file. Return NULL if error
27 28 29 30 |
# 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) ⇒ Object
Read sizeBytes into buffer from current position and return number of bytes read
33 34 35 36 37 38 |
# 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) ⇒ Object
Return TRUE if file could be renamed
90 91 92 93 94 95 96 |
# 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) ⇒ Object
Seek to offset using whence=SEEK_SET/SEEK_CUR/SEEK_END. Return TRUE in case of success
47 48 49 50 |
# 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) ⇒ Object
Return current file position
53 54 55 |
# File 'lib/proj/file_api.rb', line 53 def tell_callback(context, handle, user_data) self.tell end |
#unlink_callback(context, path, user_data) ⇒ Object
Return TRUE if file could be removed
81 82 83 84 85 86 87 |
# File 'lib/proj/file_api.rb', line 81 def unlink_callback(context, path, user_data) if self.unlink(path) 1 else 0 end end |
#write_callback(context, handle, buffer, size_bytes, user_data) ⇒ Object
Write sizeBytes into buffer from current position and return number of bytes written
41 42 43 44 |
# 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 |