das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/* Copyright (C) 2015-2017 Chris Piker <chris-piker@uiowa.edu>
2 *
3 * This file is part of das2C, the Core Das2 C Library.
4 *
5 * das2C is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 2.1 as published
7 * by the Free Software Foundation.
8 *
9 * das2C is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
16 */
17
20#ifndef _das_buffer_h_
21#define _das_buffer_h_
22
23#include <stdlib.h>
24#include <stdbool.h>
25#include <stdio.h>
26
27#include "util.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
47typedef struct das_buffer{
48 char* sBuf;
49 size_t uLen;
50 char* pWrite;
51 const char* pReadBeg;
52 const char* pReadEnd;
53 size_t uWrap;
54} DasBuf;
55
66DAS_API DasBuf* new_DasBuf(size_t uLen);
67
80DAS_API DasErrCode DasBuf_initReadWrite(DasBuf* pThis, char* sExternal, size_t uLen);
81
94DAS_API DasErrCode DasBuf_initReadOnly(DasBuf* pThis, const char* sExternal, size_t uLen);
95
102DAS_API void DasBuf_reinit(DasBuf* pThis);
103
114DAS_API void del_DasBuf(DasBuf* pThis);
115
122DAS_API DasErrCode DasBuf_puts(DasBuf* pThis, const char* sStr);
123
130DAS_API DasErrCode DasBuf_printf(DasBuf* pThis, const char* sFmt, ...);
131
139DAS_API DasErrCode DasBuf_write(DasBuf* pThis, const void* pData, size_t uLen);
140
158 DasBuf* pThis, int nIndent1, int nIndent, int nWrap, const char* fmt, ...
159);
160
165DAS_API int DasBuf_writeFrom(DasBuf* pThis, FILE* pIn, size_t uLen);
166
175DAS_API int DasBuf_writeFromSock(DasBuf* pThis, int nFd, size_t uLen);
176
185DAS_API int DasBuf_writeFromSSL(DasBuf* pThis, void* vpSsl, size_t uLen);
186
187
192DAS_API size_t DasBuf_written(const DasBuf* pThis);
193
199DAS_API size_t DasBuf_writeSpace(const DasBuf* pThis);
200
210DAS_API size_t DasBuf_unread(const DasBuf* pThis);
211
223DAS_API size_t DasBuf_strip(DasBuf* pThis);
224
232DAS_API size_t DasBuf_read(DasBuf* pThis, char* pOut, size_t uOut);
233
249DAS_API const ubyte* DasBuf_direct(const DasBuf* pThis, size_t* pLength);
250
257DAS_API size_t DasBuf_peek(const DasBuf* pThis, char* pOut, size_t uOut);
258
264DAS_API int DasBuf_last(const DasBuf* pThis);
265
281DAS_API const char* DasBuf_readRec(
282 DasBuf* pThis, const char* sDelim, size_t uDelimLen, size_t* pLen
283);
284
285
291DAS_API size_t DasBuf_readOffset(const DasBuf* pThis);
292
300DAS_API DasErrCode DasBuf_setReadOffset(DasBuf* pThis, size_t uPos);
301
302#ifdef __cplusplus
303}
304#endif
305
306#endif /* _das_buffer_h_ */
307
DAS_API const char * DasBuf_readRec(DasBuf *pThis, const char *sDelim, size_t uDelimLen, size_t *pLen)
Return a pointer to the start of the current line and advance the read point to the start of the next...
DAS_API size_t DasBuf_writeSpace(const DasBuf *pThis)
Get the remaining write space in the buffer.
DAS_API DasErrCode DasBuf_setReadOffset(DasBuf *pThis, size_t uPos)
Set the offset of the read position.
DAS_API size_t DasBuf_readOffset(const DasBuf *pThis)
Get the offset of the read position.
DAS_API DasErrCode DasBuf_paragraph(DasBuf *pThis, int nIndent1, int nIndent, int nWrap, const char *fmt,...)
Write wrapped utf-8 text to the buffer.
DAS_API int DasBuf_writeFromSSL(DasBuf *pThis, void *vpSsl, size_t uLen)
Add generic data to the buffer from an OpenSSL object.
DAS_API int DasBuf_writeFrom(DasBuf *pThis, FILE *pIn, size_t uLen)
Add generic data to the buffer from a file.
DAS_API int DasBuf_writeFromSock(DasBuf *pThis, int nFd, size_t uLen)
Add generic data to the buffer from a socket.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition defs.h:184
Buffer class to handle accumulating byte streams.
Definition buffer.h:47
DAS_API size_t DasBuf_unread(const DasBuf *pThis)
Get the number of bytes remaining from the read begin point to the read end point.
DAS_API size_t DasBuf_written(const DasBuf *pThis)
Get the size of the data in the buffer.
DAS_API DasErrCode DasBuf_printf(DasBuf *pThis, const char *sFmt,...)
Write formatted strings to the buffer.
DAS_API void del_DasBuf(DasBuf *pThis)
Free a buffer object along with it's backing store.
DAS_API DasErrCode DasBuf_initReadWrite(DasBuf *pThis, char *sExternal, size_t uLen)
Initialize a read-write buffer that points to an external byte array.
DAS_API const ubyte * DasBuf_direct(const DasBuf *pThis, size_t *pLength)
Get a constant point to un-read bytes in the buffer and the number un-read.
DAS_API size_t DasBuf_strip(DasBuf *pThis)
Adjust read points so that the data starts and ends on non-space values.
DAS_API DasErrCode DasBuf_puts(DasBuf *pThis, const char *sStr)
Add a string to the buffer.
DAS_API void DasBuf_reinit(DasBuf *pThis)
Re-initialize a buffer including read and write points This version can be a little quicker than DasB...
DAS_API DasBuf * new_DasBuf(size_t uLen)
Create a new Read-Write buffer on the heap Allocates a new char buffer of the indicated size,...
DAS_API size_t DasBuf_peek(const DasBuf *pThis, char *pOut, size_t uOut)
Peak at bytes from a buffer Copies bytes out of a buffer but does not increment the read point.
DAS_API DasErrCode DasBuf_write(DasBuf *pThis, const void *pData, size_t uLen)
Add generic data to the buffer.
DAS_API DasErrCode DasBuf_initReadOnly(DasBuf *pThis, const char *sExternal, size_t uLen)
Initialize a read-only buffer that points to an external byte array.
DAS_API int DasBuf_last(const DasBuf *pThis)
Peak the last byte in the buffer.
DAS_API size_t DasBuf_read(DasBuf *pThis, char *pOut, size_t uOut)
Read bytes from a buffer Copies bytes out of a buffer and increments the read point.