The stream-operations structure

The stream-operations structure contains pointers to the C functions that implement the generic stream I/O functions for the particular stream. A valid stream-operations structure must exist for the DataBlade® API to locate at run time your type-specific implementations of these generic stream I/O functions. Therefore, it must be initialized before the call to mi_stream_init().

The following code fragment shows the declaration of the stream-operations structure, mi_st_ops. For the most current definition, see the mistream.h header file.
#define OPS_NAME_LENGTH 40

struct mi_stream_operations {
   /* the pointers to the functions */
   mi_integer (*close)(MI_STREAM *strm_desc);
   mi_integer (*read)(MI_STREAM *strm_desc, void *buf, 
      mi_integer nbytes);
   mi_integer (*write)(MI_STREAM *strm_desc, void *buf,
      mi_integer nbytes);
   mi_integer (*seek)(MI_STREAM *strm_desc, 
      mi_int8 *offset, mi_integer whence);
   mi_int8 *  (*tell)(MI_STREAM *strm_desc);
   mi_integer (*setpos)(MI_STREAM *strm_desc, 
      mi_int8 *pos);
   mi_integer (*getpos)(MI_STREAM *strm_desc, 
      mi_int8 *pos);
   mi_integer (*length)(MI_STREAM *strm_desc, 
      mi_int8 *length);
   /* names of the functions above */
   char close_name [OPS_NAME_LENGTH];
   char read_name  [OPS_NAME_LENGTH];
   char write_name [OPS_NAME_LENGTH];
   char seek_name  [OPS_NAME_LENGTH];
   char tell_name  [OPS_NAME_LENGTH];
   char setpos_name[OPS_NAME_LENGTH];
   char getpos_name[OPS_NAME_LENGTH];
   char length_name[OPS_NAME_LENGTH];
   /* the function handles for the functions above */
   void *close_fhandle;
   void *read_fhandle;
   void *write_fhandle;
   void *seek_fhandle;
   void *tell_fhandle;
   void *setpos_fhandle;
   void *getpos_fhandle;
   void *length_fhandle;
} mi_st_ops;
As preceding code fragment shows, the stream-operations structure consists of the following parts:
  • The function pointers to the generic stream I/O functions
  • The names of the generic stream I/O functions
  • The function handles of the generic stream I/O functions

You must initialize the pointers and names and set the handles to NULL.

The following code fragment shows a sample stream-operations structure that provides function pointers for the type-specific implementations of the mi_stream_close(), mi_stream_read(), and mi_stream_write() functions for a stream on a user-defined type named newstream.
Figure 1: A sample stream-operations structure
static struct mi_st_ops stream_ops_newstream =
{
   stream_close_newstream,
   stream_read_newstream,
   stream_write_newstream,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   "stream_close_newstream",
   "stream_read_newstream",
   "stream_write_newstream",
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL   
};

The preceding code fragment statically initializes the stream-operations structure. If you initialize this structure dynamically, do so in the stream-open function.