From e02d9fb502f878f8e8d816f1c222b21863b57a43 Mon Sep 17 00:00:00 2001 From: Chris Bradley Date: Mon, 19 Feb 2024 16:50:00 +0000 Subject: [PATCH] GribHandle starting from an offset --- src/metkit/codes/GribHandle.cc | 23 +++++++++++++++++++++++ src/metkit/codes/GribHandle.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/src/metkit/codes/GribHandle.cc b/src/metkit/codes/GribHandle.cc index 65434aa5..f766f011 100644 --- a/src/metkit/codes/GribHandle.cc +++ b/src/metkit/codes/GribHandle.cc @@ -82,6 +82,29 @@ GribHandle::GribHandle(eckit::DataHandle& handle): handle_ = h; } +GribHandle::GribHandle(eckit::DataHandle& handle, eckit::Offset offset): + handle_(nullptr), + owned_(true) { + + codes_handle* h = nullptr; + int err = 0; + + FILE* f = handle.openf(); + ASSERT(f); + + fseek(f, offset, SEEK_SET); + h = codes_handle_new_from_file(0, f, PRODUCT_GRIB, &err); + + CODES_CALL(err); + ASSERT(h); + handle_ = h; + + // XXX: Part of a workaround to sync handle and f on linux. + // XXX: This needs to be investigated further. + handle.seek(ftello(f)); + fclose(f); // XXX: The other constructor does not close the file, why do we? +} + GribHandle::~GribHandle() noexcept(false) { if (handle_ && owned_) { CODES_CALL(codes_handle_delete(handle_)); diff --git a/src/metkit/codes/GribHandle.h b/src/metkit/codes/GribHandle.h index b900bff6..b7e06f1e 100644 --- a/src/metkit/codes/GribHandle.h +++ b/src/metkit/codes/GribHandle.h @@ -53,6 +53,9 @@ class GribHandle : private eckit::NonCopyable { /// constructor creating a grib_handle from a DataHandle explicit GribHandle(eckit::DataHandle&); + // Constructor creating a grib_handle from a DataHandle starting from a given offset + explicit GribHandle(eckit::DataHandle&, eckit::Offset); + /// destructor will delete the grib_handle if we own it ~GribHandle() noexcept(false);