libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 libdas2, the Core Das2 C Library.
4  *
5  * Libdas2 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  * Libdas2 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 libdas2; 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 
49 typedef struct das_buffer{
50  char* sBuf;
51  size_t uLen;
52  char* pWrite;
53  const char* pReadBeg;
54  const char* pReadEnd;
55  size_t uWrap;
56 } DasBuf;
57 
68 DasBuf* new_DasBuf(size_t uLen);
69 
80 DasErrCode DasBuf_initReadWrite(DasBuf* pThis, char* sBuf, size_t uLen);
81 
90 DasErrCode DasBuf_initReadOnly(DasBuf* pThis, const char* sBuf, size_t uLen);
91 
98 void DasBuf_reinit(DasBuf* pThis);
99 
110 void del_DasBuf(DasBuf* pThis);
111 
118 DasErrCode DasBuf_puts(DasBuf* pThis, const char* sStr);
119 
126 DasErrCode DasBuf_printf(DasBuf* pThis, const char* sFmt, ...);
127 
135 DasErrCode DasBuf_write(DasBuf* pThis, const void* pData, size_t uLen);
136 
154  DasBuf* pThis, int nIndent1, int nIndent, int nWrap, const char* fmt, ...
155 );
156 
161 int DasBuf_writeFrom(DasBuf* pThis, FILE* pIn, size_t uLen);
162 
171 int DasBuf_writeFromSock(DasBuf* pThis, int nFd, size_t uLen);
172 
181 int DasBuf_writeFromSSL(DasBuf* pThis, void* vpSsl, size_t uLen);
182 
183 
188 size_t DasBuf_written(const DasBuf* pThis);
189 
195 size_t DasBuf_writeSpace(const DasBuf* pThis);
196 
206 size_t DasBuf_unread(const DasBuf* pThis);
207 
219 size_t DasBuf_strip(DasBuf* pThis);
220 
228 size_t DasBuf_read(DasBuf* pThis, char* pOut, size_t uOut);
229 
245 const char* DasBuf_readRec(
246  DasBuf* pThis, const char* sDelim, size_t uDelimLen, size_t* pLen
247 );
248 
249 
255 size_t DasBuf_readOffset(const DasBuf* pThis);
256 
264 DasErrCode DasBuf_setReadOffset(DasBuf* pThis, size_t uPos);
265 
266 #ifdef __cplusplus
267 }
268 #endif
269 
270 #endif /* _das_buffer_h_ */
271 
size_t DasBuf_written(const DasBuf *pThis)
Get the size of the data in the buffer.
size_t DasBuf_strip(DasBuf *pThis)
Adjust read points so that the data starts and ends on non-space values.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: util.h:117
DasErrCode DasBuf_initReadWrite(DasBuf *pThis, char *sBuf, size_t uLen)
Initialize a read-write buffer that points to an external byte array.
size_t DasBuf_writeSpace(const DasBuf *pThis)
Get the remaining write space in the buffer.
void DasBuf_reinit(DasBuf *pThis)
Re-initialize a buffer including read and write points This version can be a little quicker than init...
DasErrCode DasBuf_puts(DasBuf *pThis, const char *sStr)
Add a string to the buffer.
DasErrCode DasBuf_write(DasBuf *pThis, const void *pData, size_t uLen)
Add generic data to the buffer.
Little buffer class to handle accumulating string data.
Definition: buffer.h:49
int DasBuf_writeFromSock(DasBuf *pThis, int nFd, size_t uLen)
Add generic data to the buffer from a socket.
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.
size_t DasBuf_readOffset(const DasBuf *pThis)
Get the offset of the read position.
DasErrCode DasBuf_setReadOffset(DasBuf *pThis, size_t uPos)
Set the offset of the read position.
int DasBuf_writeFrom(DasBuf *pThis, FILE *pIn, size_t uLen)
Add generic data to the buffer from a file.
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...
DasErrCode DasBuf_paragraph(DasBuf *pThis, int nIndent1, int nIndent, int nWrap, const char *fmt,...)
Write wrapped utf-8 text to the buffer.
DasBuf * new_DasBuf(size_t uLen)
Create a new Read-Write buffer on the heap Allocates a new char buffer of the indicated size...
DasErrCode DasBuf_initReadOnly(DasBuf *pThis, const char *sBuf, size_t uLen)
Initialize a read-only buffer than points to an external byte array.
size_t DasBuf_unread(const DasBuf *pThis)
Get the number of bytes remaining from the read begin point to the read end point.
int DasBuf_writeFromSSL(DasBuf *pThis, void *vpSsl, size_t uLen)
Add generic data to the buffer from an OpenSSL object.
DasErrCode DasBuf_printf(DasBuf *pThis, const char *sFmt,...)
Write formatted strings to the buffer.
void del_DasBuf(DasBuf *pThis)
Free a buffer object along with it&#39;s backing store.