}
-
-
-
/*******************************************************************************
* wrapping ZLib *
*******************************************************************************/
return 0;
}
+
/*******************************************************************************
* File I/O; Callbacks to libevent *
*******************************************************************************/
* where a cut left is inexpensive
*/
- if (FB->ReadWritePointer != NULL)
- {
+ if (FB->ReadWritePointer != NULL) {
long already_read;
already_read = FB->ReadWritePointer - FB->Buf->buf;
FB->ReadWritePointer = FB->Buf->buf;
bufremain = FB->Buf->BufSize;
}
- else if ((unread < 64) ||
- (bufremain < already_read))
- {
+ else if ((unread < 64) || (bufremain < already_read)) {
/*
* if its just a tiny bit remaining, or we run out of space...
* lets tidy up.
FB->ReadWritePointer = FB->Buf->buf;
bufremain = FB->Buf->BufSize - unread - 1;
}
- else if (bufremain < (FB->Buf->BufSize / 10))
- {
+ else if (bufremain < (FB->Buf->BufSize / 10)) {
/* get a bigger buffer */
IncreaseBuf(FB->Buf, 0, FB->Buf->BufUsed + 1);
return n;
}
-int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB)
-{
+
+int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB) {
long WriteRemain;
int n;
return n;
}
-/**
- * extract a "next line" from Buf; Ptr to persist across several iterations
- * LineBuf your line will be copied here.
- * FB BLOB with lines of text...
- * Ptr moved arround to keep the next-line across several iterations
- * has to be &NULL on start; will be &NotNULL on end of buffer
- * @returns size of copied buffer
- */
-eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
-{
+
+// extract a "next line" from Buf; Ptr to persist across several iterations
+// LineBuf your line will be copied here.
+// FB BLOB with lines of text...
+// Ptr moved arround to keep the next-line across several iterations
+// has to be &NULL on start; will be &NotNULL on end of buffer
+// returns size of copied buffer
+eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) {
const char *aptr, *ptr, *eptr;
char *optr, *xptr;
eptr = FB->Buf->buf + FB->Buf->BufUsed;
xptr = LineBuf->buf + LineBuf->BufSize - 1;
- while ((ptr <= eptr) &&
- (*ptr != '\n') &&
- (*ptr != '\r') )
- {
+ while ((ptr <= eptr) && (*ptr != '\n') && (*ptr != '\r') ) {
*optr = *ptr;
optr++; ptr++;
if (optr == xptr) {
if ((*(ptr - 1) != '\r') && (*(ptr - 1) != '\n')) {
LineBuf->BufUsed = optr - LineBuf->buf;
*optr = '\0';
- if ((FB->ReadWritePointer != NULL) &&
- (FB->ReadWritePointer != FB->Buf->buf))
- {
- /* Ok, the client application read all the data
- it was interested in so far. Since there is more to read,
- we now shrink the buffer, and move the rest over.
- */
- StrBufCutLeft(FB->Buf,
- FB->ReadWritePointer - FB->Buf->buf);
+ if ((FB->ReadWritePointer != NULL) && (FB->ReadWritePointer != FB->Buf->buf)) {
+ // Ok, the client application read all the data
+ // it was interested in so far. Since there is more to read,
+ // we now shrink the buffer, and move the rest over.
+ StrBufCutLeft(FB->Buf, FB->ReadWritePointer - FB->Buf->buf);
FB->ReadWritePointer = FB->Buf->buf;
}
return eMustReadMore;
return eReadSuccess;
}
-long IOBufferStrLength(IOBuffer *FB)
-{
+
+long IOBufferStrLength(IOBuffer *FB) {
if ((FB == NULL) || (FB->Buf == NULL))
return 0;
if (FB->ReadWritePointer == NULL)
}
-/**
- * Read a line from socket
- * flushes and closes the FD on error
- * Line the line to read from the fd / I/O Buffer
- * buf the buffer to get the input to
- * fd pointer to the filedescriptor to read
- * timeout number of successless selects until we bail out
- * selectresolution how long to wait on each select
- * Error strerror() on error
- * @returns numbers of chars read
- */
+// Read a line from socket
+// flushes and closes the FD on error
+// Line the line to read from the fd / I/O Buffer
+// buf the buffer to get the input to
+// fd pointer to the filedescriptor to read
+// timeout number of successless selects until we bail out
+// selectresolution how long to wait on each select
+// Error strerror() on error
+// returns numbers of chars read
int StrBufTCP_read_buffered_line(StrBuf *Line,
StrBuf *buf,
int *fd,
if (pch != NULL) {
rlen = 0;
len = pch - buf->buf;
- if (len > 0 && (*(pch - 1) == '\r') )
+ if (len > 0 && (*(pch - 1) == '\r') ) {
rlen ++;
+ }
StrBufSub(Line, buf, 0, len - rlen);
StrBufCutLeft(buf, len + 1);
return len - rlen;
}
}
- if (buf->BufSize - buf->BufUsed < 10)
+ if (buf->BufSize - buf->BufUsed < 10) {
IncreaseBuf(buf, 1, -1);
+ }
fdflags = fcntl(*fd, F_GETFL);
IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
nSuccessLess ++;
continue;
}
- rlen = read(*fd,
- &buf->buf[buf->BufUsed],
- buf->BufSize - buf->BufUsed - 1);
+ rlen = read(*fd, &buf->buf[buf->BufUsed], buf->BufSize - buf->BufUsed - 1);
if (rlen < 1) {
*Error = strerror(errno);
close(*fd);
buf->BufUsed += rlen;
buf->buf[buf->BufUsed] = '\0';
pch = strchr(buf->buf, '\n');
- if ((pch == NULL) &&
- (buf->BufUsed + 10 > buf->BufSize) &&
- (IncreaseBuf(buf, 1, -1) == -1))
+ if ((pch == NULL) && (buf->BufUsed + 10 > buf->BufSize) && (IncreaseBuf(buf, 1, -1) == -1)) {
return -1;
+ }
continue;
}
static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
-/**
- * Read a line from socket
- * flushes and closes the FD on error
- * Line where to append our Line read from the fd / I/O Buffer;
- * IOBuf the buffer to get the input to; lifetime pair to FD
- * Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
- * fd pointer to the filedescriptor to read
- * timeout number of successless selects until we bail out
- * selectresolution how long to wait on each select
- * Error strerror() on error
- * @returns numbers of chars read or -1 in case of error. "\n" will become 0
- */
+
+
+// Read a line from socket
+// flushes and closes the FD on error
+// Line where to append our Line read from the fd / I/O Buffer;
+// IOBuf the buffer to get the input to; lifetime pair to FD
+// Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
+// fd pointer to the filedescriptor to read
+// timeout number of successless selects until we bail out
+// selectresolution how long to wait on each select
+// Error strerror() on error
+// returns numbers of chars read or -1 in case of error. "\n" will become 0
int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
StrBuf *IOBuf,
const char **Pos,
struct timeval tv;
retlen = 0;
- if ((Line == NULL) ||
- (Pos == NULL) ||
- (IOBuf == NULL) ||
- (*fd == -1))
- {
- if (Pos != NULL)
+ if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL) || (*fd == -1)) {
+ if (Pos != NULL) {
*Pos = NULL;
+ }
*Error = ErrRBLF_PreConditionFailed;
return -1;
}
pos = *Pos;
- if ((IOBuf->BufUsed > 0) &&
- (pos != NULL) &&
- (pos < IOBuf->buf + IOBuf->BufUsed))
- {
+ if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed)) {
char *pcht;
pche = IOBuf->buf + IOBuf->BufUsed;
pch = pos;
pcht = Line->buf;
- while ((pch < pche) && (*pch != '\n'))
- {
- if (Line->BufUsed + 10 > Line->BufSize)
- {
+ while ((pch < pche) && (*pch != '\n')) {
+ if (Line->BufUsed + 10 > Line->BufSize) {
long apos;
apos = pcht - Line->buf;
*pcht = '\0';
}
len = pch - pos;
- if (len > 0 && (*(pch - 1) == '\r') )
- {
+ if (len > 0 && (*(pch - 1) == '\r') ) {
retlen--;
len --;
pcht --;
}
*pcht = '\0';
- if ((pch >= pche) || (*pch == '\0'))
- {
+ if ((pch >= pche) || (*pch == '\0')) {
FlushStrBuf(IOBuf);
*Pos = NULL;
pch = NULL;
pos = 0;
}
- if ((pch != NULL) &&
- (pch <= pche))
- {
+ if ((pch != NULL) && (pch <= pche)) {
if (pch + 1 >= pche) {
*Pos = NULL;
FlushStrBuf(IOBuf);
}
- else
+ else {
*Pos = pch + 1;
-
+ }
return retlen;
}
else
/* If we come here, Pos is Unset since we read everything into Line, and now go for more. */
- if (IOBuf->BufSize - IOBuf->BufUsed < 10)
+ if (IOBuf->BufSize - IOBuf->BufUsed < 10) {
IncreaseBuf(IOBuf, 1, -1);
+ }
fdflags = fcntl(*fd, F_GETFL);
IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
while ((nSuccessLess < timeout) &&
(pLF == NULL) &&
(*fd != -1)) {
- if (IsNonBlock)
- {
+ if (IsNonBlock) {
tv.tv_sec = 1;
tv.tv_usec = 0;
continue;
}
}
- rlen = read(*fd,
- &IOBuf->buf[IOBuf->BufUsed],
- IOBuf->BufSize - IOBuf->BufUsed - 1);
+ rlen = read(*fd, &IOBuf->buf[IOBuf->BufUsed], IOBuf->BufSize - IOBuf->BufUsed - 1);
if (rlen < 1) {
*Error = strerror(errno);
close(*fd);
if ((pLF >= pche) || (*pLF == '\0'))
pLF = NULL;
- if (IOBuf->BufUsed + 10 > IOBuf->BufSize)
- {
+ if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
long apos = 0;
if (pLF != NULL) apos = pLF - IOBuf->buf;
continue;
}
- else
- {
+ else {
nSuccessLess++;
}
}
if (pLF != NULL) {
pos = IOBuf->buf;
len = pLF - pos;
- if (len > 0 && (*(pLF - 1) == '\r') )
+ if (len > 0 && (*(pLF - 1) == '\r') ) {
len --;
+ }
StrBufAppendBufPlain(Line, ChrPtr(IOBuf), len, 0);
- if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed)
- {
+ if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed) {
FlushStrBuf(IOBuf);
}
else
struct timeval tv;
fd_set rfds;
- if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1))
- {
+ if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1)) {
*Error = ErrRBLF_BLOBPreConditionFailed;
return -1;
}
- if (!append)
+ if (!append) {
FlushStrBuf(Buf);
- if (Buf->BufUsed + nBytes >= Buf->BufSize)
+ }
+ if (Buf->BufUsed + nBytes >= Buf->BufSize) {
IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
-
+ }
ptr = Buf->buf + Buf->BufUsed;
fdflags = fcntl(*fd, F_GETFL);
IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
nSuccessLess = 0;
- while ((nRead < nBytes) &&
- (*fd != -1))
- {
- if (IsNonBlock)
- {
+ while ((nRead < nBytes) && (*fd != -1)) {
+ if (IsNonBlock) {
tv.tv_sec = 1;
tv.tv_usec = 0;
*Error = strerror(errno);
close (*fd);
*fd = -1;
- if (*Error == NULL)
+ if (*Error == NULL) {
*Error = ErrRBLF_SelectFailed;
+ }
return -1;
}
if (! FD_ISSET(*fd, &rfds) != 0) {
const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
const char *ErrRBB_too_many_selects = "StrBufReadBLOBBuffered: to many selects; aborting.";
-/**
- * Input binary data from socket
- * flushes and closes the FD on error
- * Blob put binary thing here
- * IOBuf the buffer to get the input to
- * Pos offset inside of IOBuf
- * fd pointer to the filedescriptor to read
- * append Append to an existing string or replace?
- * nBytes the maximal number of bytes to read
- * check whether we should search for '000\n' terminators in case of timeouts
- * Error strerror() on error
- * @returns numbers of chars read
- */
+
+
+// Input binary data from socket
+// flushes and closes the FD on error
+// Blob put binary thing here
+// IOBuf the buffer to get the input to
+// Pos offset inside of IOBuf
+// fd pointer to the filedescriptor to read
+// append Append to an existing string or replace?
+// nBytes the maximal number of bytes to read
+// check whether we should search for '000\n' terminators in case of timeouts
+// Error strerror() on error
+// returns numbers of chars read
int StrBufReadBLOBBuffered(StrBuf *Blob,
StrBuf *IOBuf,
const char **Pos,