2009-12-02 20:22:16 +00:00
|
|
|
/*
|
2010-03-01 17:09:21 +00:00
|
|
|
* Copyright 2009-2010 10gen, Inc.
|
2009-12-02 20:22:16 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-08-08 19:06:39 +00:00
|
|
|
#ifndef _BSON_BUFFER_H
|
|
|
|
#define _BSON_BUFFER_H
|
2009-12-02 20:22:16 +00:00
|
|
|
|
2009-12-02 22:01:49 +00:00
|
|
|
/* Note: if any of these functions return a failure condition then the buffer
|
|
|
|
* has already been freed. */
|
|
|
|
|
2009-12-02 20:22:16 +00:00
|
|
|
/* A buffer */
|
2011-08-08 19:06:39 +00:00
|
|
|
typedef struct bson_buffer* bson_buffer_t;
|
2009-12-02 20:22:16 +00:00
|
|
|
/* A position in the buffer */
|
2011-08-08 19:06:39 +00:00
|
|
|
typedef int bson_buffer_position;
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
/* Allocate and return a new buffer.
|
|
|
|
* Return NULL on allocation failure. */
|
2011-08-08 19:06:39 +00:00
|
|
|
bson_buffer_t bson_buffer_new(void);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
/* Free the memory allocated for `buffer`.
|
|
|
|
* Return non-zero on failure. */
|
2011-08-08 19:06:39 +00:00
|
|
|
int bson_buffer_free(bson_buffer_t buffer);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
/* Save `size` bytes from the current position in `buffer` (and grow if needed).
|
|
|
|
* Return offset for writing, or -1 on allocation failure. */
|
2011-08-08 19:06:39 +00:00
|
|
|
bson_buffer_position bson_buffer_save_space(bson_buffer_t buffer, int size);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
/* Write `size` bytes from `data` to `buffer` (and grow if needed).
|
|
|
|
* Return non-zero on allocation failure. */
|
2011-08-08 19:06:39 +00:00
|
|
|
int bson_buffer_write(bson_buffer_t buffer, const char* data, int size);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
/* Write `size` bytes from `data` to `buffer` at position `position`.
|
|
|
|
* Does not change the internal position of `buffer`.
|
|
|
|
* Return non-zero if buffer isn't large enough for write. */
|
2011-08-08 19:06:39 +00:00
|
|
|
int bson_buffer_write_at_position(bson_buffer_t buffer, bson_buffer_position position, const char* data, int size);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
2011-08-08 19:06:39 +00:00
|
|
|
/* Getters for the internals of a bson_buffer_t.
|
2009-12-02 20:22:16 +00:00
|
|
|
* Should try to avoid using these as much as possible
|
|
|
|
* since they break the abstraction. */
|
2011-08-08 19:06:39 +00:00
|
|
|
bson_buffer_position bson_buffer_get_position(bson_buffer_t buffer);
|
|
|
|
char* bson_buffer_get_buffer(bson_buffer_t buffer);
|
2009-12-02 20:22:16 +00:00
|
|
|
|
|
|
|
#endif
|