libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Functions
buffer.h File Reference

Utility to assist with encode and decode operations. More...

Detailed Description

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.

Data Structures

struct  DasBuf
 Little buffer class to handle accumulating string data. More...
 

Functions

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...
 

Function Documentation

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
pThisthe buffer initialize
sBufan pre-allocated character buffer to receive new data
uLenthe 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
pThisthe buffer to receive the text
nIndent1the start column for the first line of text
nIndentthe start column for subsequent lines
nWrapthe wrap column, using 80 is recommended
fmtA 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
pThisThe buffer
nFdThe file descriptor associated with a readable socket
uLenThe 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
pThisThe buffer
vpSslA void pointer to an SSL structure
uLenThe 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
pThisThe buffer
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
pThisThe DasBuf to read
sDelimthe line delimiter, typically this is just a single character string, but any string may be considered the line deliminter
uDelimLenthe length of the record deliminator in bytes
pLenA 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
DasErrCode DasBuf_setReadOffset ( DasBuf pThis,
size_t  uPos 
)

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