.\" Man page generated from reStructuredText. . . .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 .. .TH "BSON_RESERVE_BUFFER" "3" "Apr 02, 2024" "1.26.2" "libbson" .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .EX uint8_t * bson_reserve_buffer (bson_t *bson, uint32_t size); .EE .UNINDENT .UNINDENT .SH PARAMETERS .INDENT 0.0 .IP \(bu 2 \fBbson\fP: An initialized \fI\%bson_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 \fI\%bson_init()\fP or \fI\%bson_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 \fI\%bson_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 \fI\%bson_t\fP pointer and reads a file into it directly: .INDENT 0.0 .INDENT 3.5 .sp .EX #include #include bool read_into (bson_t *bson, FILE *fp) { uint8_t *buffer; long size; if (fseek (fp, 0L, SEEK_END) < 0) { perror (\(dqCouldn\(aqt get file size\(dq); return 1; } size = ftell (fp); if (size == EOF) { perror (\(dqCouldn\(aqt get file size\(dq); return 1; } if (size > INT32_MAX) { fprintf (stderr, \(dqFile too large\en\(dq); return 1; } /* reserve buffer space \- bson is temporarily invalid */ buffer = bson_reserve_buffer (bson, (uint32_t) size); if (!buffer) { fprintf (stderr, \(dqCouldn\(aqt reserve %ld bytes\(dq, size); return false; } /* read file directly into the buffer */ rewind (fp); if (fread ((void *) buffer, 1, (size_t) size, fp) < (size_t) size) { perror (\(dqCouldn\(aqt read file\(dq); return false; } return true; } int main () { FILE *fp; char *json; /* stack\-allocated, initialized bson_t */ bson_t bson = BSON_INITIALIZER; if (!(fp = fopen (\(dqdocument.bson\(dq, \(dqrb\(dq))) { perror (\(dqCouldn\(aqt read file\(dq); return 1; } read_into (&bson, fp); fclose (fp); json = bson_as_canonical_extended_json (&bson, NULL); printf (\(dq%s\en\(dq, json); bson_free (json); bson_destroy (&bson); return 0; } .EE .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .