Action on close

,

I’m writing a library to support reading and writing BioVision Hierarchical files.

This widely used format for 3D animation consists of a number of skeleton definitions, then the number of frames of data, then the frames of data themselves. Other than this count, the data could be written in append mode, which is a pretty reasonable thing to want to do with an animation format. In particular, one might want to ‘drip feed’ the frames in real time.

Since the frame count isn’t actually used by readers that can wait for more data, I’d like to provide the option to only go back and write the frame count when the file is closed. This demands a hook into close. But as a library, I’d like all this to be transparent to the user. If feels unProloglike to force this on the user.

Is there some way to hook file close? Hopefully without redefining the system predicate (which seems aggressive) or installing a compile time hook (which also seems extreme, and fragile)?

There needs to be an open_bvh_file anyway, to get the skeleton, but I’d rather not wrap the stream just so it can’t be closed with close/1

trying to avoid that - it forces remembering to use the special close on the user.

The most obvious seems predicates open_bio_vision(File, Mode, Stream) and close_bio_vision(Stream). Actually hooking close/1 can be done using the new wrap_predicate/4 feature but that seems an overkill you should not want.

There is another ugly solution: create a specialized stream. Typically you do that in C. It can also be done on Prolog using open_prolog_stream/4. Seems an overkill to me though.

1 Like