00001 /* 00002 * Copyright 2009-2010 10gen, Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef BUFFER_H 00018 #define BUFFER_H 00019 00020 /* Note: if any of these functions return a failure condition then the buffer 00021 * has already been freed. */ 00022 00023 /* A buffer */ 00024 typedef struct buffer* buffer_t; 00025 /* A position in the buffer */ 00026 typedef int buffer_position; 00027 00028 /* Allocate and return a new buffer. 00029 * Return NULL on allocation failure. */ 00030 buffer_t buffer_new(void); 00031 00032 /* Free the memory allocated for `buffer`. 00033 * Return non-zero on failure. */ 00034 int buffer_free(buffer_t buffer); 00035 00036 /* Save `size` bytes from the current position in `buffer` (and grow if needed). 00037 * Return offset for writing, or -1 on allocation failure. */ 00038 buffer_position buffer_save_space(buffer_t buffer, int size); 00039 00040 /* Write `size` bytes from `data` to `buffer` (and grow if needed). 00041 * Return non-zero on allocation failure. */ 00042 int buffer_write(buffer_t buffer, const char* data, int size); 00043 00044 /* Write `size` bytes from `data` to `buffer` at position `position`. 00045 * Does not change the internal position of `buffer`. 00046 * Return non-zero if buffer isn't large enough for write. */ 00047 int buffer_write_at_position(buffer_t buffer, buffer_position position, const char* data, int size); 00048 00049 /* Getters for the internals of a buffer_t. 00050 * Should try to avoid using these as much as possible 00051 * since they break the abstraction. */ 00052 buffer_position buffer_get_position(buffer_t buffer); 00053 char* buffer_get_buffer(buffer_t buffer); 00054 00055 #endif