das2C
das core C utilities (v3)
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
30 extern "C" {
31 #endif
32 
47 typedef 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 
66 DAS_API DasBuf* new_DasBuf(size_t uLen);
67 
78 DAS_API DasErrCode DasBuf_initReadWrite(DasBuf* pThis, char* sBuf, size_t uLen);
79 
88 DAS_API DasErrCode DasBuf_initReadOnly(DasBuf* pThis, const char* sBuf, size_t uLen);
89 
96 DAS_API void DasBuf_reinit(DasBuf* pThis);
97 
108 DAS_API void del_DasBuf(DasBuf* pThis);
109 
116 DAS_API DasErrCode DasBuf_puts(DasBuf* pThis, const char* sStr);
117 
124 DAS_API DasErrCode DasBuf_printf(DasBuf* pThis, const char* sFmt, ...);
125 
133 DAS_API DasErrCode DasBuf_write(DasBuf* pThis, const void* pData, size_t uLen);
134 
152  DasBuf* pThis, int nIndent1, int nIndent, int nWrap, const char* fmt, ...
153 );
154 
159 DAS_API int DasBuf_writeFrom(DasBuf* pThis, FILE* pIn, size_t uLen);
160 
169 DAS_API int DasBuf_writeFromSock(DasBuf* pThis, int nFd, size_t uLen);
170 
179 DAS_API int DasBuf_writeFromSSL(DasBuf* pThis, void* vpSsl, size_t uLen);
180 
181 
186 DAS_API size_t DasBuf_written(const DasBuf* pThis);
187 
193 DAS_API size_t DasBuf_writeSpace(const DasBuf* pThis);
194 
204 DAS_API size_t DasBuf_unread(const DasBuf* pThis);
205 
217 DAS_API size_t DasBuf_strip(DasBuf* pThis);
218 
226 DAS_API size_t DasBuf_read(DasBuf* pThis, char* pOut, size_t uOut);
227 
243 DAS_API const ubyte* DasBuf_direct(const DasBuf* pThis, size_t* pLength);
244 
251 DAS_API size_t DasBuf_peek(const DasBuf* pThis, char* pOut, size_t uOut);
252 
258 DAS_API int DasBuf_last(const DasBuf* pThis);
259 
275 DAS_API const char* DasBuf_readRec(
276  DasBuf* pThis, const char* sDelim, size_t uDelimLen, size_t* pLen
277 );
278 
279 
285 DAS_API size_t DasBuf_readOffset(const DasBuf* pThis);
286 
294 DAS_API DasErrCode DasBuf_setReadOffset(DasBuf* pThis, size_t uPos);
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif /* _das_buffer_h_ */
301 
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 DasErrCode DasBuf_initReadOnly(DasBuf *pThis, const char *sBuf, size_t uLen)
Initialize a read-only buffer than points to an external byte array.
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.
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...
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: defs.h:164
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 *sBuf, 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 init...
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 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.