.\" Man page generated from reStructuredText. . .TH "BSON_RESERVE_BUFFER" "3" "Feb 23, 2019" "1.14.0" "Libbson" .SH NAME bson_reserve_buffer \- bson_reserve_buffer() . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C uint8_t * bson_reserve_buffer (bson_t *bson, uint32_t size); .ft P .fi .UNINDENT .UNINDENT .SH PARAMETERS .INDENT 0.0 .IP \(bu 2 \fBbson\fP: An initialized \fBbson_t\fP\&. .IP \(bu 2 \fBsize\fP: The length in bytes of the new buffer. .UNINDENT .SH DESCRIPTION .sp Grow the internal buffer of \fBbson\fP to \fBsize\fP and set the document length to \fBsize\fP\&. Useful for eliminating copies when reading BSON bytes from a stream. .sp First, initialize \fBbson\fP with \fBbson_init\fP or \fBbson_new\fP, then call this function. After it returns, the length of \fBbson\fP is set to \fBsize\fP but its contents are uninitialized memory: you must fill the contents with a BSON document of the correct length before any other operations. .sp The document must be freed with \fBbson_destroy\fP\&. .SH RETURNS .sp A pointer to the internal buffer, which is at least \fBsize\fP bytes, or NULL if the space could not be allocated. .SH EXAMPLE .sp Use \fBbson_reserve_buffer\fP to write a function that takes a \fBbson_t\fP pointer and reads a file into it directly: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include #include bool read_into (bson_t *bson, FILE *fp) { uint8_t *buffer; long size; if (fseek (fp, 0L, SEEK_END) < 0) { perror ("Couldn\(aqt get file size"); return 1; } size = ftell (fp); if (size == EOF) { perror ("Couldn\(aqt get file size"); return 1; } if (size > INT32_MAX) { fprintf (stderr, "File too large\en"); return 1; } /* reserve buffer space \- bson is temporarily invalid */ buffer = bson_reserve_buffer (bson, (uint32_t) size); if (!buffer) { fprintf (stderr, "Couldn\(aqt reserve %ld bytes", size); return false; } /* read file directly into the buffer */ rewind (fp); if (fread ((void *) buffer, 1, (size_t) size, fp) < (size_t) size) { perror ("Couldn\(aqt read file"); return false; } return true; } int main () { FILE *fp; char *json; /* stack\-allocated, initialized bson_t */ bson_t bson = BSON_INITIALIZER; if (!(fp = fopen ("document.bson", "rb"))) { perror ("Couldn\(aqt read file"); return 1; } read_into (&bson, fp); fclose (fp); json = bson_as_canonical_extended_json (&bson, NULL); printf ("%s\en", json); bson_free (json); bson_destroy (&bson); return 0; } .ft P .fi .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .