From e802e46ef830b524fd845aea83969de875b144d0 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Mon, 30 Oct 2023 09:05:20 -0900 Subject: [PATCH] stringbuf.c: chipped away at style modernization This is a big one with a lot of outdated style in it. Folded lines which were >80 columns but <132 columns. Add {} braces to single-line conditional blocks. Replaced C89 comments with C99 comments. --- libcitadel/lib/json.c | 7 +- libcitadel/lib/stringbuf.c | 220 ++++++++++++++++--------------------- 2 files changed, 94 insertions(+), 133 deletions(-) diff --git a/libcitadel/lib/json.c b/libcitadel/lib/json.c index ff649f75a..d664700fa 100644 --- a/libcitadel/lib/json.c +++ b/libcitadel/lib/json.c @@ -141,8 +141,7 @@ JsonValue *NewJsonNull(const char *Key, long keylen) { Ret = (JsonValue*) malloc(sizeof(JsonValue)); memset(Ret, 0, sizeof(JsonValue)); Ret->Type = JSON_NULL; - if (Key != NULL) - { + if (Key != NULL) { Ret->Name = NewStrBufPlain(Key, keylen); } Ret->Value = NewStrBufPlain(HKEY("nulll")); @@ -195,9 +194,7 @@ void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal) { const char *Key; long keylen; - - switch (Val->Type) - { + switch (Val->Type) { case JSON_STRING: StrBufAppendBufPlain(Target, HKEY("\""), 0); StrECMAEscAppend(Target, Val->Value, NULL); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 201b2dfa1..2101465e0 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -3844,9 +3844,6 @@ long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen) } - - - /******************************************************************************* * wrapping ZLib * *******************************************************************************/ @@ -3959,6 +3956,7 @@ int CompressBuffer(StrBuf *Buf) return 0; } + /******************************************************************************* * File I/O; Callbacks to libevent * *******************************************************************************/ @@ -3976,8 +3974,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) * where a cut left is inexpensive */ - if (FB->ReadWritePointer != NULL) - { + if (FB->ReadWritePointer != NULL) { long already_read; already_read = FB->ReadWritePointer - FB->Buf->buf; @@ -3993,9 +3990,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) 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. @@ -4008,8 +4003,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) 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); @@ -4043,8 +4037,8 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) 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; @@ -4082,16 +4076,14 @@ int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB) 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; @@ -4114,10 +4106,7 @@ eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) 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) { @@ -4134,15 +4123,11 @@ eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) 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; @@ -4183,8 +4168,8 @@ eReadState StrBufCheckBuffer(IOBuffer *FB) return eReadSuccess; } -long IOBufferStrLength(IOBuffer *FB) -{ + +long IOBufferStrLength(IOBuffer *FB) { if ((FB == NULL) || (FB->Buf == NULL)) return 0; if (FB->ReadWritePointer == NULL) @@ -4246,17 +4231,15 @@ int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error) } -/** - * 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, @@ -4277,16 +4260,18 @@ int StrBufTCP_read_buffered_line(StrBuf *Line, 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; @@ -4309,9 +4294,7 @@ int StrBufTCP_read_buffered_line(StrBuf *Line, 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); @@ -4323,10 +4306,9 @@ int StrBufTCP_read_buffered_line(StrBuf *Line, 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; } @@ -4347,18 +4329,18 @@ int StrBufTCP_read_buffered_line(StrBuf *Line, 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, @@ -4379,32 +4361,24 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 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'; @@ -4417,8 +4391,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, } len = pch - pos; - if (len > 0 && (*(pch - 1) == '\r') ) - { + if (len > 0 && (*(pch - 1) == '\r') ) { retlen--; len --; pcht --; @@ -4426,24 +4399,21 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, } *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 @@ -4452,8 +4422,9 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, /* 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; @@ -4462,8 +4433,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, while ((nSuccessLess < timeout) && (pLF == NULL) && (*fd != -1)) { - if (IsNonBlock) - { + if (IsNonBlock) { tv.tv_sec = 1; tv.tv_usec = 0; @@ -4482,9 +4452,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 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); @@ -4504,8 +4472,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 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; @@ -4515,8 +4482,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, continue; } - else - { + else { nSuccessLess++; } } @@ -4524,11 +4490,11 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 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 @@ -4562,26 +4528,23 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E 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; @@ -4591,8 +4554,9 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E *Error = strerror(errno); close (*fd); *fd = -1; - if (*Error == NULL) + if (*Error == NULL) { *Error = ErrRBLF_SelectFailed; + } return -1; } if (! FD_ISSET(*fd, &rfds) != 0) { @@ -4619,19 +4583,19 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E 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, -- 2.30.2