From 4e037cf415c5f48647ddc37350c862084b07f03b Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Thu, 2 Nov 2023 17:42:28 -0900 Subject: [PATCH] War on old style continues. I really ought to be writing some real code tonight. But I get into a file and I see old style and I want to make it clean and shiny and it's easier than the kind of heavy brain work I should be doing. When faced with a heavy think, I dawdle for a long time before I really get started. I've done this since I was a child. --- libcitadel/lib/stringbuf.c | 302 +++++++++++++++---------------------- 1 file changed, 118 insertions(+), 184 deletions(-) diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 2101465e0..b382025b9 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -124,21 +124,23 @@ void dbg_FreeStrBuf(StrBuf *FreeMe, char *FromWhere) { char buf[SIZ * 3]; long n; n = snprintf(buf, SIZ * 3, "%c+|%ld|%ld|%ld|%s|%s|\n", - FromWhere, - (*FreeMe)->nIncreases, - (*FreeMe)->BufUsed, - (*FreeMe)->BufSize, - (*FreeMe)->bt, - (*FreeMe)->bt_lastinc); + FromWhere, + (*FreeMe)->nIncreases, + (*FreeMe)->BufUsed, + (*FreeMe)->BufSize, + (*FreeMe)->bt, + (*FreeMe)->bt_lastinc + ); n = write(hFreeDbglog, buf, n); } else { char buf[128]; long n; n = snprintf(buf, 128, "%c_|0|%ld%ld|\n", - FromWhere, - (*FreeMe)->BufUsed, - (*FreeMe)->BufSize); + FromWhere, + (*FreeMe)->BufUsed, + (*FreeMe)->BufSize + ); n = write(hFreeDbglog, buf, n); } } @@ -543,12 +545,13 @@ StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant) * flush the content of a Buf; keep its struct * buf Buffer to flush */ -int FlushStrBuf(StrBuf *buf) -{ - if ((buf == NULL) || (buf->buf == NULL)) +int FlushStrBuf(StrBuf *buf) { + if ((buf == NULL) || (buf->buf == NULL)) { return -1; - if (buf->ConstBuf) - return -1; + } + if (buf->ConstBuf) { + return -1; + } buf->buf[0] ='\0'; buf->BufUsed = 0; return 0; @@ -726,9 +729,9 @@ long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char Po // AppendBuf Buffer to copy at the end of our buffer // Offset Should we start copying from an offset? void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset) { - if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) || - (Buf == NULL) || (Buf->buf == NULL)) + if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) || (Buf == NULL) || (Buf->buf == NULL)) { return; + } if (Buf->BufSize - Offset < AppendBuf->BufUsed + Buf->BufUsed + 1) IncreaseBuf(Buf, (Buf->BufUsed > 0), AppendBuf->BufUsed + Buf->BufUsed); @@ -763,9 +766,7 @@ void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, u IncreaseBuf(Buf, (Buf->BufUsed > 0), BufSizeRequired); } - memcpy(Buf->buf + Buf->BufUsed, - AppendBuf + Offset, - aps); + memcpy(Buf->buf + Buf->BufUsed, AppendBuf + Offset, aps); Buf->BufUsed += aps; Buf->buf[Buf->BufUsed] = '\0'; } @@ -902,18 +903,18 @@ int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t n return 0; } if (Offset + nChars < Source->BufUsed) { - if ((nChars >= dest->BufSize) && - (IncreaseBuf(dest, 0, nChars + 1) == -1)) + if ((nChars >= dest->BufSize) && (IncreaseBuf(dest, 0, nChars + 1) == -1)) { return 0; + } memcpy(dest->buf, Source->buf + Offset, nChars); dest->BufUsed = nChars; dest->buf[dest->BufUsed] = '\0'; return nChars; } NCharsRemain = Source->BufUsed - Offset; - if ((NCharsRemain >= dest->BufSize) && - (IncreaseBuf(dest, 0, NCharsRemain + 1) == -1)) + if ((NCharsRemain >= dest->BufSize) && (IncreaseBuf(dest, 0, NCharsRemain + 1) == -1)) { return 0; + } memcpy(dest->buf, Source->buf + Offset, NCharsRemain); dest->BufUsed = NCharsRemain; dest->buf[dest->BufUsed] = '\0'; @@ -984,9 +985,7 @@ void StrBufTrim(StrBuf *Buf) int delta = 0; if ((Buf == NULL) || (Buf->BufUsed == 0)) return; - while ((Buf->BufUsed > 0) && - isspace(Buf->buf[Buf->BufUsed - 1])) - { + while ((Buf->BufUsed > 0) && isspace(Buf->buf[Buf->BufUsed - 1])) { Buf->BufUsed --; } Buf->buf[Buf->BufUsed] = '\0'; @@ -1090,28 +1089,21 @@ void StrBufLowerCase(StrBuf *Buf) * ReplLen Length of repl * @returns -1 if fail else length of resulting Buf */ -int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, - const char *Repl, long ReplLen) -{ +int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, const char *Repl, long ReplLen) { - if ((Buf == NULL) || - (where > Buf->BufUsed) || - (where + HowLong > Buf->BufUsed)) + if ((Buf == NULL) || (where > Buf->BufUsed) || (where + HowLong > Buf->BufUsed)) { return -1; + } - if (where + ReplLen - HowLong > Buf->BufSize) - if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0) + if (where + ReplLen - HowLong > Buf->BufSize) { + if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0) { return -1; + } + } - memmove(Buf->buf + where + ReplLen, - Buf->buf + where + HowLong, - Buf->BufUsed - where - HowLong); - - memcpy(Buf->buf + where, - Repl, ReplLen); - + memmove(Buf->buf + where + ReplLen, Buf->buf + where + HowLong, Buf->BufUsed - where - HowLong); + memcpy(Buf->buf + where, Repl, ReplLen); Buf->BufUsed += ReplLen - HowLong; - return Buf->BufUsed; } @@ -1121,8 +1113,7 @@ int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, * tok Tokenizer char to count * @returns numbers of tokenizer chars found */ -int StrBufNum_tokens(const StrBuf *source, char tok) -{ +int StrBufNum_tokens(const StrBuf *source, char tok) { char *pch, *pche; long NTokens; if ((source == NULL) || (source->BufUsed == 0)) @@ -1157,9 +1148,7 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator) /* Find desired eter */ end = Source->buf + Source->BufUsed; d = Source->buf; - while ((d <= end) && - (count < parmnum)) - { + while ((d <= end) && (count < parmnum)) { /* End of string, bail! */ if (!*d) { d = NULL; @@ -1175,9 +1164,7 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator) /* Find next eter */ s = d; - while ((s <= end) && - (*s && *s != separator)) - { + while ((s <= end) && (*s && *s != separator)) { s++; } if (*s == separator) @@ -1269,8 +1256,7 @@ int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char se break; } } - if ( (current_token == parmnum) && - (*s != separator)) { + if ( (current_token == parmnum) && (*s != separator)) { dest->buf[len] = *s; ++len; } @@ -1379,24 +1365,17 @@ unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, cha * pStart the token iterator pointer to inspect * @returns whether the revolving pointer is inside of the search range */ -int StrBufHaveNextToken(const StrBuf *Source, const char **pStart) -{ - if ((Source == NULL) || - (*pStart == StrBufNOTNULL) || - (Source->BufUsed == 0)) - { +int StrBufHaveNextToken(const StrBuf *Source, const char **pStart) { + if ((Source == NULL) || (*pStart == StrBufNOTNULL) || (Source->BufUsed == 0)) { return 0; } - if (*pStart == NULL) - { + if (*pStart == NULL) { return 1; } - else if (*pStart > Source->buf + Source->BufUsed) - { + else if (*pStart > Source->buf + Source->BufUsed) { return 0; } - else if (*pStart <= Source->buf) - { + else if (*pStart <= Source->buf) { return 0; } @@ -1411,58 +1390,47 @@ int StrBufHaveNextToken(const StrBuf *Source, const char **pStart) * separator tokenizer * @returns -1 if not found, else length of token. */ -int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator) -{ +int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator) { const char *s; /* source */ const char *EndBuffer; /* end stop of source buffer */ int current_token = 0; /* token currently being processed */ int len = 0; /* running total length of extracted string */ - if ((Source == NULL) || - (Source->BufUsed == 0) ) - { + if ((Source == NULL) || (Source->BufUsed == 0)) { *pStart = StrBufNOTNULL; - if (dest != NULL) + if (dest != NULL) { FlushStrBuf(dest); + } return -1; } EndBuffer = Source->buf + Source->BufUsed; - if (dest != NULL) - { + if (dest != NULL) { dest->buf[0] = '\0'; dest->BufUsed = 0; } - else - { + else { *pStart = EndBuffer + 1; return -1; } - if (*pStart == NULL) - { + if (*pStart == NULL) { *pStart = Source->buf; /* we're starting to examine this buffer. */ } - else if ((*pStart < Source->buf) || - (*pStart > EndBuffer ) ) - { + else if ((*pStart < Source->buf) || (*pStart > EndBuffer ) ) { return -1; /* no more tokens to find. */ } s = *pStart; /* start to find the next token */ - while ((s <= EndBuffer) && - (current_token == 0) ) - { - if (*s == separator) - { + while ((s <= EndBuffer) && (current_token == 0) ) { + if (*s == separator) { /* we found the next token */ ++current_token; } - if (len >= dest->BufSize) - { + if (len >= dest->BufSize) { /* our Dest-buffer isn't big enough, increase it. */ dest->BufUsed = len; @@ -1510,14 +1478,12 @@ int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pSt * nTokens number of tokens to fastforward over * @returns -1 if not found, else length of token. */ -int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens) -{ +int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens) { const char *s, *EndBuffer; //* source * / int len = 0; //* running total length of extracted string * / int current_token = 0; //* token currently being processed * / - if ((Source == NULL) || - (Source->BufUsed ==0)) { + if ((Source == NULL) || (Source->BufUsed ==0)) { return(-1); } if (nTokens == 0) @@ -1533,12 +1499,8 @@ int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator return (-1); } - s = *pStart; - //cit_backtrace(); - //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); - while ((s < EndBuffer) && !IsEmptyStr(s)) { if (*s == separator) { ++current_token; @@ -1554,15 +1516,13 @@ int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator return(len); } -/** - * a string tokenizer to fetch an integer - * Source StringBuffer to read from - * pStart Cursor on the tokenstring - * separator tokenizer character - * @returns 0 if not found, else integer representation of the token - */ -int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator) -{ + +// a string tokenizer to fetch an integer +// Source StringBuffer to read from +// pStart Cursor on the tokenstring +// separator tokenizer character +// returns 0 if not found, else integer representation of the token +int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator) { StrBuf tmp; char buf[64]; @@ -1577,15 +1537,13 @@ int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separa return 0; } -/** - * a string tokenizer to fetch a long integer - * Source StringBuffer to read from - * pStart Cursor on the tokenstring - * separator tokenizer character - * @returns 0 if not found, else long integer representation of the token - */ -long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator) -{ + +// a string tokenizer to fetch a long integer +// Source StringBuffer to read from +// pStart Cursor on the tokenstring +// separator tokenizer character +// returns 0 if not found, else long integer representation of the token +long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator) { StrBuf tmp; char buf[64]; @@ -1601,15 +1559,12 @@ long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char sepa } -/** - * a string tokenizer to fetch an unsigned long - * Source StringBuffer to read from - * pStart Cursor on the tokenstring - * separator tokenizer character - * @returns 0 if not found, else unsigned long representation of the token - */ -unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator) -{ +// a string tokenizer to fetch an unsigned long +// Source StringBuffer to read from +// pStart Cursor on the tokenstring +// separator tokenizer character +// returns 0 if not found, else unsigned long representation of the token +unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator) { StrBuf tmp; char buf[64]; char *pnum; @@ -1630,27 +1585,22 @@ unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char * } - - - /******************************************************************************* * Escape Appending * *******************************************************************************/ -/** - * Escape a string for feeding out as a URL while appending it to a Buffer - * OutBuf the output buffer - * In Buffer to encode - * PlainIn way in from plain old c strings - */ -void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) -{ +// Escape a string for feeding out as a URL while appending it to a Buffer +// OutBuf the output buffer +// In Buffer to encode +// PlainIn way in from plain old c strings +void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) { const char *pch, *pche; char *pt, *pte; int len; - if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) ) + if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) ) { return; + } if (PlainIn != NULL) { len = strlen(PlainIn); pch = PlainIn; @@ -1697,12 +1647,10 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) } -/* - * Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer - * OutBuf the output buffer - * In Buffer to encode - * PlainIn way in from plain old c strings - */ +// Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer +// OutBuf the output buffer +// In Buffer to encode +// PlainIn way in from plain old c strings void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) { const char *pch, *pche; char *pt, *pte; @@ -1755,14 +1703,13 @@ void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) *pt = '\0'; } -/* - * append a string with characters having a special meaning in xml encoded to the buffer - * OutBuf the output buffer - * In Buffer to encode - * PlainIn way in from plain old c strings - * PlainInLen way in from plain old c strings; maybe you've got binary data or know the length? - * OverrideLowChars should chars < 0x20 be replaced by _ or escaped as xml entity? - */ + +// append a string with characters having a special meaning in xml encoded to the buffer +// OutBuf the output buffer +// In Buffer to encode +// PlainIn way in from plain old c strings +// PlainInLen way in from plain old c strings; maybe you've got binary data or know the length? +// OverrideLowChars should chars < 0x20 be replaced by _ or escaped as xml entity? void StrBufXMLEscAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn, @@ -2323,26 +2270,23 @@ long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn) return Target->BufUsed; } -/** - * Append a string, escaping characters which have meaning in HTML + json. - * - * Target target buffer - * Source source buffer; set to NULL if you just have a C-String - * PlainIn Plain-C string to append; set to NULL if unused - * nbsp If nonzero, spaces are converted to non-breaking spaces. - * nolinebreaks if set to 1, linebreaks are removed from the string. - * if set to 2, linebreaks are replaced by <br/> - */ -long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks) -{ +// Append a string, escaping characters which have meaning in HTML + json. +// +// Target target buffer +// Source source buffer; set to NULL if you just have a C-String +// PlainIn Plain-C string to append; set to NULL if unused +// nbsp If nonzero, spaces are converted to non-breaking spaces. +// nolinebreaks if set to 1, linebreaks are removed from the string. +// if set to 2, linebreaks are replaced by <br/> +long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks) { const char *aptr, *eiptr; char *bptr, *eptr; long len; int IsUtf8Sequence = 0; - if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) ) + if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL)) { return -1; - + } if (PlainIn != NULL) { aptr = PlainIn; len = strlen(PlainIn); @@ -2354,16 +2298,17 @@ long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *Plai len = Source->BufUsed; } - if (len == 0) + if (len == 0) { return -1; + } bptr = Target->buf + Target->BufUsed; - eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */ + eptr = Target->buf + Target->BufSize - 11; // our biggest unit to put in... - while (aptr < eiptr){ + while (aptr < eiptr) { if(bptr >= eptr) { IncreaseBuf(Target, 1, -1); - eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */ + eptr = Target->buf + Target->BufSize - 11; // our biggest unit to put in... bptr = Target->buf + Target->BufUsed; } switch (*aptr) { @@ -2435,14 +2380,13 @@ long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *Plai isxdigit(*(aptr + 3)) && isxdigit(*(aptr + 4)) && isxdigit(*(aptr + 5))) - { /* oh, a unicode escaper. let it pass through. */ + { /* oh, a unicode escaper. let it pass through. */ memcpy(bptr, aptr, 6); aptr += 5; bptr +=6; Target->BufUsed += 6; } - else - { + else { *bptr = '\\'; bptr ++; *bptr = '\\'; @@ -2471,7 +2415,7 @@ long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *Plai bptr ++; Target->BufUsed += 2; break; - case 32: + case 32: if (nbsp == 1) { memcpy(bptr, HKEY(" ")); bptr += 6; @@ -2511,11 +2455,10 @@ void StrBufAsciify(StrBuf *Buf, const char repl) { } -/* - * unhide special chars hidden to the HTML escaper - * target buffer to put the unescaped string in - * source buffer to unescape - */ + +// unhide special chars hidden to the HTML escaper +// target buffer to put the unescaped string in +// source buffer to unescape void StrBufEUid_unescapize(StrBuf *target, const StrBuf *source) { int a, b, len; char hex[3]; @@ -2561,8 +2504,7 @@ void StrBufEUid_escapize(StrBuf *target, const StrBuf *source) { if (target != NULL) FlushStrBuf(target); - if ((source == NULL) || (target == NULL) || (target->buf == NULL)) - { + if ((source == NULL) || (target == NULL) || (target->buf == NULL)) { return; } @@ -2641,18 +2583,10 @@ typedef struct __z_enc_stream { vStreamT *StrBufNewStreamContext(eStreamType type, const char **Err) { - //base64_decodestate *state;; *Err = NULL; switch (type) { - //case eBase64Decode: - //case eBase64Encode: - //state = (base64_decodestate*) malloc(sizeof(base64_decodestate)); - //base64_init_decodestate(state); - //return (vStreamT*) state; - //break; - case eZLibDecode: { z_enc_stream *stream; -- 2.30.2