1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* $Id: buffer.h,v 1.2 2001/04/23 19:34:30 binnema Exp $
*/
/*
** Copyright (C) 2001 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
#ifndef _BUFFER_H_
#define _BUFFER_H_
#include <assert.h>
#include <stdlib.h>
typedef struct _Buffer Buffer;
struct _Buffer {
char* _data;
size_t _len;
};
typedef size_t BufferIterator;
/*
* buffer_new:
* create a new buffer
*
* returns: a new buffer or NULL if something went wrong
*/
Buffer* buffer_new (void);
/*
* buffer_destroy:
* destroy an existing buffer
*
* buffer: an existing buffer
*
* returns: NULL
*
* precondition: buffer != NULL
*/
Buffer* buffer_destroy (Buffer* buffer);
/*
* buffer_set_data:
* add data to buffer by copying
*
* buffer_manage_data:
* manage an existing char[]
*
* buffer: a Buffer ptr
* data : a char ptr
* len : length of the char buffer
*
* preconditions: buffer != NULL &&
* data == NULL -> len == 0
*/
void buffer_set_data (Buffer* buffer, const char* data, size_t len);
void buffer_manage_data (Buffer* buffer, char* data, size_t len);
/*
* buffer_data:
* get a ptr to the data inside the buffer
*
* buffer: a Buffer ptr
*
* returns: a ptr to the buffer data
*
* precondition: buffer != NULL
*/
const char* buffer_data (const Buffer* buffer);
/*
* buffer_length:
* get the length of the buffer
*
* buffer: a Buffer ptr
*
* precondition: buffer != NULL
*/
size_t buffer_length (const Buffer *buffer);
/*
* buffer_append, buffer_prepend:
* append or prepend data to the buffer
*
* buffer: a buffer ptr
* data : a char* buffer
* len : length of the buffer
*
* preconditions: buffer != NULL &&
* data==NULL -> len == 0
*/
void buffer_append (Buffer* buffer, const char* data, size_t len);
void buffer_prepend (Buffer* buffer, const char* data, size_t len);
/*
* buffer_insert:
* insert data in the buffer
*
* buffer: a Buffer ptr
* data : a char* buffer
* len : length of the char* buffer
* pos : pos where to insert the data
*
* preconditions: buffer != NULL &&
* pos < buffer_length (buffer) &&
* data == NULL --> len == 0
*/
void buffer_insert (Buffer* buffer, const char* data, size_t len,
BufferIterator pos);
/*
* buffer_erase:
* erase a part of the buffer
*
* buffer: a Buffer ptr
* pos : start position of the part to erase
* len : length of the part to erase
*
* preconditions: buffer != NULL &&
* pos < buffer_length (buffer) &&
* pos + len < buffer_length (buffer)
*/
void buffer_erase (Buffer* buffer, BufferIterator pos, size_t len);
/*
* buffer_find:
* find a string in the buffer
*
* buffer : a Buffer ptr
* pattern : the pattern to search
* len : length of the pattern
*
* returns: BufferIterator pointing to the found position, or to buffer_end if nothing was found
*
* preconditions: buffer != NULL &&
* pattern == NULL -> len == 0
*/
BufferIterator buffer_find (const Buffer* buffer, const char* pattern, size_t len);
/*
* buffer_clear:
* clear the buffer
*
* buffer : a buffer ptr
*
* preconditions: buffer != NULL
*/
void buffer_clear (Buffer* buffer);
/*
* buffer_begin, buffer_end:
* macros for c++-stl like iterators
*/
#define buffer_begin(b) ((BufferIterator)0)
#define buffer_end(b) ((BufferIterator)buffer_length(b))
/*
* buffer_at:
* get a char from the buffer
*
* buffer: a buffer ptr
* pos : a buffer iterator
*
* returns: a character
*
* preconditions: buffer != NULL
* && pos < buffer_end(buffer)
*/
char buffer_at (const Buffer* buffer, BufferIterator pos);
#endif /*_BUFFER_H_*/
|