Utility to assist with encode and decode operations.
More...
Utility to assist with encode and decode operations.
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "util.h"
Go to the source code of this file.
|
struct | DasBuf |
| Little buffer class to handle accumulating string data. More...
|
|
|
DasErrCode | DasBuf_initReadOnly (DasBuf *pThis, const char *sBuf, size_t uLen) |
| Initialize a read-only buffer than points to an external byte array. More...
|
|
DasErrCode | DasBuf_paragraph (DasBuf *pThis, int nIndent1, int nIndent, int nWrap, const char *fmt,...) |
| Write wrapped utf-8 text to the buffer. More...
|
|
int | DasBuf_writeFrom (DasBuf *pThis, FILE *pIn, size_t uLen) |
| Add generic data to the buffer from a file. More...
|
|
int | DasBuf_writeFromSock (DasBuf *pThis, int nFd, size_t uLen) |
| Add generic data to the buffer from a socket. More...
|
|
int | DasBuf_writeFromSSL (DasBuf *pThis, void *vpSsl, size_t uLen) |
| Add generic data to the buffer from an OpenSSL object. More...
|
|
size_t | DasBuf_writeSpace (const DasBuf *pThis) |
| Get the remaining write space in the buffer. More...
|
|
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 line. More...
|
|
size_t | DasBuf_readOffset (const DasBuf *pThis) |
| Get the offset of the read position. More...
|
|
DasErrCode | DasBuf_setReadOffset (DasBuf *pThis, size_t uPos) |
| Set the offset of the read position. More...
|
|
DasErrCode DasBuf_initReadOnly |
( |
DasBuf * |
pThis, |
|
|
const char * |
sBuf, |
|
|
size_t |
uLen |
|
) |
| |
Initialize a read-only buffer than points to an external byte array.
This function re-sets the read point for the buffer.
- Parameters
-
pThis | the buffer initialize |
sBuf | an pre-allocated character buffer to receive new data |
uLen | the length of the pre-allocated buffer |
DasErrCode DasBuf_paragraph |
( |
DasBuf * |
pThis, |
|
|
int |
nIndent1, |
|
|
int |
nIndent, |
|
|
int |
nWrap, |
|
|
const char * |
fmt, |
|
|
|
... |
|
) |
| |
Write wrapped utf-8 text to the buffer.
With the exception of explicit newline characters, this function uses white space only to separate words. Words are not split thus new-lines start at word boundaries.
The mentality of the function is to produce horizontal "paragraphs" of text that are space indented.
- Parameters
-
pThis | the buffer to receive the text |
nIndent1 | the start column for the first line of text |
nIndent | the start column for subsequent lines |
nWrap | the wrap column, using 80 is recommended |
fmt | A printf style format string, may contain utf-8 characters. |
- Returns
- 0 if the operation succeeded, a positive error code otherwise.
int DasBuf_writeFrom |
( |
DasBuf * |
pThis, |
|
|
FILE * |
pIn, |
|
|
size_t |
uLen |
|
) |
| |
Add generic data to the buffer from a file.
- Returns
- Then number of bytes actually read, or a negative error code if there was a problem reading from the file.
int DasBuf_writeFromSock |
( |
DasBuf * |
pThis, |
|
|
int |
nFd, |
|
|
size_t |
uLen |
|
) |
| |
Add generic data to the buffer from a socket.
- Parameters
-
pThis | The buffer |
nFd | The file descriptor associated with a readable socket |
uLen | The amount of data to read |
- Returns
- Then number of bytes actually read, or a negative error code if there was a problem reading from the socket.
int DasBuf_writeFromSSL |
( |
DasBuf * |
pThis, |
|
|
void * |
vpSsl, |
|
|
size_t |
uLen |
|
) |
| |
Add generic data to the buffer from an OpenSSL object.
- Parameters
-
pThis | The buffer |
vpSsl | A void pointer to an SSL structure |
uLen | The amount of data to read |
- Returns
- Then number of bytes actually read, or a negative error code if there was a problem reading from the socket.
size_t DasBuf_writeSpace |
( |
const DasBuf * |
pThis | ) |
|
Get the remaining write space in the buffer.
- Parameters
-
- Returns
- The number of bytes that may be still be written to the buffer.
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 line.
The main use is for reading lines of character data, though any delimited byte stream can be read with this function.
- Parameters
-
pThis | The DasBuf to read |
sDelim | the line delimiter, typically this is just a single character string, but any string may be considered the line deliminter |
uDelimLen | the length of the record deliminator in bytes |
pLen | A pointer to a location to receive the line length, excluding the delimiter. The saved value will be zero for empty lines. |
- Returns
- A pointer to the start of the current line, or NULL if no further lines are present in the buffer.
size_t DasBuf_readOffset |
( |
const DasBuf * |
pThis | ) |
|
Get the offset of the read position.
- Parameters
-
pThis | - The buffer to query |
- Returns
- The difference between the read point and the base of the buffer
Set the offset of the read position.
- Parameters
-
pThis | - The buffer in question |
uPos | - The new read offset from be beginning of the buffer |
- Returns
- 0 on success an positive error code if uPos makes no sense for the buffers current state