stringbuf.c: random idle style cleanup.
authorArt Cancro <ajc@citadel.org>
Sun, 5 May 2024 01:38:43 +0000 (18:38 -0700)
committerArt Cancro <ajc@citadel.org>
Sun, 5 May 2024 01:38:43 +0000 (18:38 -0700)
libcitadel/README.txt
libcitadel/lib/stringbuf.c

index 81336d9abe9b795e13652bb044f46daa8eaac62f..584e5363219998ea55c759ea6ac85a6bfadabde2 100644 (file)
@@ -1,12 +1,10 @@
 This is libcitadel, a library which contains code that is used in multiple
 components of the Citadel system -- the server, the text mode client, and
 WebCit.  It is not intended to be a general-purpose library for widespread
-use, although there are parts of it that may be useful for that purpose,
-such as the MIME parser and the vCard data type.
+use, although there are parts of it that may be useful for that purpose.
 
-Copyright (c) 1987-2023 by the citadel.org development team.
-This program is distributed under the terms of the GNU General Public
-License, version 3.
+Copyright (c) 1987-2024 by the citadel.org development team.
+This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
 
 To build and install libcitadel:
 
index 92da0a931496ff3617b17e1bd3c26fda5a581169..7f136deb05155c3f509e498964802ba943134315 100644 (file)
@@ -36,11 +36,11 @@ int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen, const Bytef * source,
 #endif
 int BaseStrBufSize = 64;
 int EnableSplice = 0;
-int ZLibCompressionRatio = -1; /* defaults to 6 */
+int ZLibCompressionRatio = -1; // defaults to 6
 #ifdef HAVE_ZLIB
-#define DEF_MEM_LEVEL 8 /*< memlevel??? */
-#define OS_CODE 0x03   /*< unix */
-const int gz_magic[2] = { 0x1f, 0x8b };        /* gzip magic header */
+#define DEF_MEM_LEVEL 8 // memlevel???
+#define OS_CODE 0x03   // unix
+const int gz_magic[2] = { 0x1f, 0x8b };        // gzip magic header
 #endif
 
 const char *StrBufNOTNULL = ((char*) NULL) - 1;
@@ -926,14 +926,12 @@ void StrBufCutRight(StrBuf *Buf, int nChars) {
        Buf->buf[Buf->BufUsed] = '\0';
 }
 
-//*
+
 //  Cut the string after n Chars
 //  Buf Buffer to modify
 //  AfternChars after how many chars should we trunkate the string?
 //  At if non-null and points inside of our string, cut it there.
-///
-void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
-{
+void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At) {
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
        if (At != NULL){
                AfternChars = At - Buf->buf;
@@ -946,12 +944,9 @@ void StrBufCutAt(StrBuf *Buf, int AfternChars, const char *At)
 }
 
 
-//*
 //  Strip leading and trailing spaces from a string; with premeasured and adjusted length.
 //  Buf the string to modify
-///
-void StrBufTrim(StrBuf *Buf)
-{
+void StrBufTrim(StrBuf *Buf) {
        int delta = 0;
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
 
@@ -967,12 +962,11 @@ void StrBufTrim(StrBuf *Buf)
        }
        if (delta > 0) StrBufCutLeft(Buf, delta);
 }
-//*
+
+
 //  changes all spaces in the string  (tab, linefeed...) to Blank (0x20)
 //  Buf the string to modify
-///
-void StrBufSpaceToBlank(StrBuf *Buf)
-{
+void StrBufSpaceToBlank(StrBuf *Buf) {
        char *pche, *pch;
 
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
@@ -987,8 +981,7 @@ void StrBufSpaceToBlank(StrBuf *Buf)
        }
 }
 
-void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
-{
+void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary) {
        const char *pLeft;
        const char *pRight;
 
@@ -1008,12 +1001,9 @@ void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
 }
 
 
-//*
 //  uppercase the contents of a buffer
 //  Buf the buffer to translate
-///
-void StrBufUpCase(StrBuf *Buf) 
-{
+void StrBufUpCase(StrBuf *Buf) {
        char *pch, *pche;
 
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
@@ -1027,12 +1017,9 @@ void StrBufUpCase(StrBuf *Buf)
 }
 
 
-//*
 //  lowercase the contents of a buffer
 //  Buf the buffer to translate
-///
-void StrBufLowerCase(StrBuf *Buf) 
-{
+void StrBufLowerCase(StrBuf *Buf) {
        char *pch, *pche;
 
        if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
@@ -1078,12 +1065,10 @@ int StrBufReplaceToken(StrBuf *Buf, long where, long HowLong, const char *Repl,
        return Buf->BufUsed;
 }
 
-//*
 //  Counts the numbmer of tokens in a buffer
 //  source String to count tokens in
 //  tok    Tokenizer char to count
 // @returns numbers of tokenizer chars found
-///
 int StrBufNum_tokens(const StrBuf *source, char tok) {
        char *pch, *pche;
        long NTokens;
@@ -1103,24 +1088,22 @@ int StrBufNum_tokens(const StrBuf *source, char tok) {
        return NTokens;
 }
 
-//*
+
 //  a string tokenizer
 //  Source StringBuffer to read into
 //  parmnum n'th Parameter to remove
 //  separator tokenizer character
 // @returns -1 if not found, else length of token.
-///
-int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
-{
+int StrBufRemove_token(StrBuf *Source, int parmnum, char separator) {
        int ReducedBy;
-       char *d, *s, *end;              /* dest, source */
+       char *d, *s, *end;              // dest, source
        int count = 0;
 
-       /* Find desired eter */
+       // Find desired eter
        end = Source->buf + Source->BufUsed;
        d = Source->buf;
        while ((d <= end) && (count < parmnum)) {
-               /* End of string, bail! */
+               // End of string, bail!
                if (!*d) {
                        d = NULL;
                        break;
@@ -1131,9 +1114,9 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
                d++;
        }
        if ((d == NULL) || (d >= end))
-               return 0;               /* @Parameter not found */
+               return 0;               // Parameter not found
 
-       /* Find next eter */
+       // Find next eter
        s = d;
        while ((s <= end) && (*s && *s != separator)) {
                s++;
@@ -1142,7 +1125,7 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
                s++;
        ReducedBy = d - s;
 
-       /* Hack and slash */
+       // Hack and slash
        if (s >= end) {
                return 0;
        }
@@ -1159,17 +1142,16 @@ int StrBufRemove_token(StrBuf *Source, int parmnum, char separator)
                *--d = '\0';
                Source->BufUsed += ReducedBy;
        }
-       /*
-       while (*s) {
-               *d++ = *s++;
-       }
-       *d = 0;
-       */
+
+       //while (*s) {
+               //*d++ = *s++;
+       //}
+       //*d = 0;
+
        return ReducedBy;
 }
 
-int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator)
-{
+int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen, int parmnum, char separator) {
        const StrBuf Temp = {
                (char*)Source,
                SourceLen,
@@ -1186,16 +1168,13 @@ int StrBufExtract_tokenFromStr(StrBuf *dest, const char *Source, long SourceLen,
        return StrBufExtract_token(dest, &Temp, parmnum, separator);
 }
 
-//*
-//  a string tokenizer
-//  dest Destination StringBuffer
-//  Source StringBuffer to read into
-//  parmnum n'th Parameter to extract
-//  separator tokenizer character
-// @returns -1 if not found, else length of token.
-///
-int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
-{
+// a string tokenizer
+// dest Destination StringBuffer
+// Source StringBuffer to read into
+// parmnum n'th Parameter to extract
+// separator tokenizer character
+// returns -1 if not found, else length of token.
+int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator) {
        const char *s, *e;              // source
        int len = 0;                    // running total length of extracted string
        int current_token = 0;          // token currently being processed
@@ -1249,18 +1228,12 @@ int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char se
 }
 
 
-
-
-
-/**
- *  a string tokenizer to fetch an integer
- *  Source String containing tokens
- *  parmnum n'th Parameter to extract
- *  separator tokenizer character
- * @returns 0 if not found, else integer representation of the token
- */
-int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
-{
+//  a string tokenizer to fetch an integer
+//  Source String containing tokens
+//  parmnum n'th Parameter to extract
+//  separator tokenizer character
+// @returns 0 if not found, else integer representation of the token
+int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator) {
        StrBuf tmp;
        char buf[64];
        
@@ -1275,15 +1248,13 @@ int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
                return 0;
 }
 
-/**
- *  a string tokenizer to fetch a long integer
- *  Source String containing tokens
- *  parmnum n'th Parameter to extract
- *  separator tokenizer character
- * @returns 0 if not found, else long integer representation of the token
- */
-long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
-{
+
+// a string tokenizer to fetch a long integer
+// Source String containing tokens
+// parmnum n'th Parameter to extract
+// separator tokenizer character
+// returns 0 if not found, else long integer representation of the token
+long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator) {
        StrBuf tmp;
        char buf[64];
        
@@ -1299,15 +1270,12 @@ long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
 }
 
 
-/**
- *  a string tokenizer to fetch an unsigned long
- *  Source String containing tokens
- *  parmnum n'th Parameter to extract
- *  separator tokenizer character
- * @returns 0 if not found, else unsigned long representation of the token
- */
-unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
-{
+// a string tokenizer to fetch an unsigned long
+// Source String containing tokens
+// parmnum n'th Parameter to extract
+// separator tokenizer character
+// returns 0 if not found, else unsigned long representation of the token
+unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator) {
        StrBuf tmp;
        char buf[64];
        char *pnum;
@@ -1328,14 +1296,11 @@ unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, cha
 }
 
 
-
-/**
- *  a string tokenizer; Bounds checker
- *  function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
- *  Source our tokenbuffer
- *  pStart the token iterator pointer to inspect
- * @returns whether the revolving pointer is inside of the search range
- */
+// a string tokenizer; Bounds checker
+// function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
+// Source our tokenbuffer
+// 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)) {
                return 0;
@@ -1783,8 +1748,7 @@ void StrBufXMLEscAppend(StrBuf *OutBuf,
  *  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?
  */
-void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
-{
+void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen) {
        const unsigned char *pch, *pche;
        char *pt, *pte;
        int len;
@@ -1884,8 +1848,7 @@ void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn) {
  *  nolinebreaks       if set to 1, linebreaks are removed from the string.
  *                      if set to 2, linebreaks are replaced by &ltbr/&gt
  */
-long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks)
-{
+long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks) {
        const char *aptr, *eiptr;
        char *bptr, *eptr;
        long len;
@@ -1994,8 +1957,7 @@ long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int
  *  Source     source buffer; set to NULL if you just have a C-String
  *  PlainIn       Plain-C string to append; set to NULL if unused
  */
-void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
-{
+void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn) {
        const char *aptr, *eiptr;
        char *tptr, *eptr;
        long len;
@@ -2060,8 +2022,7 @@ void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
  *  Source     source buffer; set to NULL if you just have a C-String
  *  PlainIn       Plain-C string to append; set to NULL if unused
  */
-void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
-{
+void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn) {
        const char *aptr, *eiptr;
        char *tptr, *eptr;
        long len;
@@ -2130,8 +2091,7 @@ void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
  *  PlainIn       Plain-C string to append; set to NULL if unused
  * @returns size of result or -1
  */
-long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
-{
+long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn) {
        const char *aptr, *eiptr;
        char *bptr, *eptr;
        long len;
@@ -2823,8 +2783,7 @@ int StrBufDecodeHex(StrBuf *Buf) {
  *  Mute char to put over invalid chars
  *  Buf Buffor to transform
  */
-int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
-{
+int StrBufSanitizeAscii(StrBuf *Buf, const char Mute) {
        unsigned char *pch;
 
        if (Buf == NULL) return -1;
@@ -2843,8 +2802,7 @@ int StrBufSanitizeAscii(StrBuf *Buf, const char Mute)
  *  Buf Buffer to translate
  *  StripBlanks Reduce several blanks to one?
  */
-long StrBufUnescape(StrBuf *Buf, int StripBlanks)
-{
+long StrBufUnescape(StrBuf *Buf, int StripBlanks) {
        int a, b;
        char hex[3];
        long len;
@@ -2896,8 +2854,7 @@ long StrBufUnescape(StrBuf *Buf, int StripBlanks)
  *     source          Source string to be encoded.
  * @returns     encoded length; -1 if non success.
  */
-int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
-{
+int StrBufRFC2047encode(StrBuf **target, const StrBuf *source) {
        const char headerStr[] = "=?UTF-8?Q?";
        int need_to_encode = 0;
        int i = 0;
@@ -3229,8 +3186,7 @@ void StrBufReplaceChars(StrBuf *buf, char search, char replace) {
  *  removes all \\r s from the string, or replaces them with \n if its not a combination of both.
  *  buf Buffer to modify
  */
-void StrBufToUnixLF(StrBuf *buf)
-{
+void StrBufToUnixLF(StrBuf *buf) {
        char *pche, *pchS, *pchT;
        if (buf == NULL)
                return;
@@ -3268,8 +3224,7 @@ void StrBufToUnixLF(StrBuf *buf)
  *  fromcode   Source encoding
  *  pic           anonimized pointer to iconv struct
  */
-void  ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
-{
+void  ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic) {
 #ifdef HAVE_ICONV
        iconv_t ic = (iconv_t)(-1) ;
        ic = iconv_open(tocode, fromcode);
@@ -3293,8 +3248,7 @@ void  ctdl_iconv_open(const char *tocode, const char *fromcode, void *pic)
  *  bptr where to start searching
  * @returns found position, NULL if none.
  */
-static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr)
-{
+static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr) {
        const char * end;
        /* Find the next ?Q? */
        if (Buf->BufUsed - (bptr - Buf->buf)  < 6)
@@ -3326,8 +3280,7 @@ static inline const char *FindNextEnd (const StrBuf *Buf, const char *bptr)
  *  TmpBuf To share a workbuffer over several iterations. prepare to have it filled with useless stuff afterwards.
  *  pic Pointer to the iconv-session Object
  */
-void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic)
-{
+void StrBufConvert(StrBuf *ConvertBuf, StrBuf *TmpBuf, void *pic) {
 #ifdef HAVE_ICONV
        long trycount = 0;
        size_t siz;
@@ -3477,8 +3430,7 @@ inline static void DecodeSegment(StrBuf *Target,
  *  FoundCharset overrides DefaultCharset if non-empty; If we find a charset inside of the string, 
  *        put it here for later use where no string might be known.
  */
-void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset)
-{
+void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset) {
        StrBuf *ConvertBuf;
        StrBuf *ConvertBuf2;
        ConvertBuf = NewStrBufPlain(NULL, StrLength(DecodeMe));
@@ -3656,8 +3608,7 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
  *  Char the character to examine
  * @returns width of utf8 chars in bytes; if the sequence is broken 0 is returned; 1 if its simply ASCII.
  */
-static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
-{
+static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE) {
        int n = 0;
         unsigned char test = (1<<7);
 
@@ -3680,8 +3631,7 @@ static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *Char
  *  Char character to inspect
  * @returns yes or no
  */
-static inline int Ctdl_IsUtf8SequenceStart(const char Char)
-{
+static inline int Ctdl_IsUtf8SequenceStart(const char Char) {
 /** 11??.???? indicates an UTF8 Sequence. */
        return ((Char & 0xC0) == 0xC0);
 }
@@ -3691,8 +3641,7 @@ static inline int Ctdl_IsUtf8SequenceStart(const char Char)
  *  Buf string to measure
  * @returns the number of glyphs in Buf
  */
-long StrBuf_Utf8StrLen(StrBuf *Buf)
-{
+long StrBuf_Utf8StrLen(StrBuf *Buf) {
        int n = 0;
        int m = 0;
        char *aptr, *eptr;
@@ -3721,8 +3670,7 @@ long StrBuf_Utf8StrLen(StrBuf *Buf)
  *  maxlen how long may the string become?
  * @returns current length of the string
  */
-long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
-{
+long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen) {
        char *aptr, *eptr;
        int n = 0, m = 0;
 
@@ -3825,8 +3773,7 @@ int ZEXPORT compress_gzip(Bytef * dest,
  * Attention! If you feed this a Const String, you must maintain the uncompressed buffer yourself!
  *  Buf buffer whose content is to be gzipped
  */
-int CompressBuffer(StrBuf *Buf)
-{
+int CompressBuffer(StrBuf *Buf) {
 #ifdef HAVE_ZLIB
        char *compressed_data = NULL;
        size_t compressed_len, bufsize;
@@ -3866,8 +3813,7 @@ int CompressBuffer(StrBuf *Buf)
  *           File I/O; Callbacks to libevent                                   *
  *******************************************************************************/
 
-long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
-{
+long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB) {
        long bufremain = 0;
        int n;
        
@@ -4060,8 +4006,7 @@ eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) {
  *  check whether the chunk-buffer has more data waiting or not.
  *  FB Chunk-Buffer to inspect
  */
-eReadState StrBufCheckBuffer(IOBuffer *FB)
-{
+eReadState StrBufCheckBuffer(IOBuffer *FB) {
        if (FB == NULL)
                return eReadFail;
        if (FB->Buf->BufUsed == 0)
@@ -4097,8 +4042,7 @@ long IOBufferStrLength(IOBuffer *FB) {
  *  Error strerror() on error 
  * @returns numbers of chars read
  */
-int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
-{
+int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error) {
        int len, rlen, slen;
 
        if ((buf == NULL) || (buf->buf == NULL)) {
@@ -4422,8 +4366,7 @@ static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong argumen
  *  Error strerror() on error 
  * @returns numbers of chars read
  */
-int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
-{
+int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error) {
        int fdflags;
        int rlen;
        int nSuccessLess;