Skip to content

Commit

Permalink
Merge pull request #37 from ecmwf/feature/datareader-size
Browse files Browse the repository at this point in the history
Add datareader.size which allows
  • Loading branch information
TomHodson authored Nov 19, 2024
2 parents 0d6c2a4 + 48af9a8 commit aa78558
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/pyfdb/processed_fdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ int fdb_datareader_close(fdb_datareader_t* dr);
int fdb_datareader_tell(fdb_datareader_t* dr, long* pos);
int fdb_datareader_seek(fdb_datareader_t* dr, long pos);
int fdb_datareader_skip(fdb_datareader_t* dr, long count);
int fdb_datareader_size(fdb_datareader_t* dr, long* size);
int fdb_datareader_read(fdb_datareader_t* dr, void *buf, long count, long* read);
int fdb_delete_datareader(fdb_datareader_t* dr);

Expand Down
15 changes: 11 additions & 4 deletions src/pyfdb/pyfdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,19 @@ def tell(self):
lib.fdb_datareader_tell(self.__dataread, where)
return where[0]

def read(self, count=-1) -> bytes:
def size(self):
size = ffi.new("long*")
lib.fdb_datareader_size(self.__dataread, size)
return size[0]

def read(self, size=-1) -> bytes:
self.open()
if isinstance(count, int):
buf = bytearray(count)
if isinstance(size, int):
if size == -1:
size = self.size()
buf = bytearray(size)
read = ffi.new("long*")
lib.fdb_datareader_read(self.__dataread, ffi.from_buffer(buf), count, read)
lib.fdb_datareader_read(self.__dataread, ffi.from_buffer(buf), size, read)
return buf[0 : read[0]]
return bytearray()

Expand Down

0 comments on commit aa78558

Please sign in to comment.