-// Copyright (c) 1987-2023 by the citadel.org team
+// Copyright (c) 1987-2024 by the citadel.org team
//
-// This program is open source software. Use, duplication, or disclosure
-// is subject to the terms of the GNU General Public License, version 3.
+// This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License, version 3.
#define _GNU_SOURCE
#include "sysdep.h"
}
#else
-/* void it... */
+// void it...
#define dbg_FreeStrBuf(a, b)
#define dbg_IncreaseBuf(a)
#define dbg_Init(a)
#endif
-/*
- * swaps the contents of two StrBufs
- * this is to be used to have cheap switched between a work-buffer and a target buffer
- * A First one
- * B second one
- */
+// swaps the contents of two StrBufs
+// this is to be used to have cheap switched between a work-buffer and a target buffer
+// A First one
+// B second one
static inline void iSwapBuffers(StrBuf *A, StrBuf *B) {
StrBuf C;
}
-/*
- * Cast operator to Plain String
- * @note if the buffer is altered by StrBuf operations, this pointer may become
- * invalid. So don't lean on it after altering the buffer!
- * Since this operation is considered cheap, rather call it often than risking
- * your pointer to become invalid!
- * Str the string we want to get the c-string representation for
- * @returns the Pointer to the Content. Don't mess with it!
- */
+// Cast operator to Plain String
+// note: if the buffer is altered by StrBuf operations, this pointer may become
+// invalid. So don't lean on it after altering the buffer!
+// Since this operation is considered cheap, rather call it often than risking
+// your pointer to become invalid!
+// Str the string we want to get the c-string representation for
+// returns the Pointer to the Content. Don't mess with it!
inline const char *ChrPtr(const StrBuf *Str) {
if (Str == NULL)
return "";
}
-/*
- * since we know strlen()'s result, provide it here.
- * Str the string to return the length to
- * @returns contentlength of the buffer
- */
+// since we know strlen()'s result, provide it here.
+// Str the string to return the length to
+// returns contentlength of the buffer
inline int StrLength(const StrBuf *Str) {
return (Str != NULL) ? Str->BufUsed : 0;
}
}
-/*
- * shrink long term buffers to their real size so they don't waste memory
- * Buf buffer to shrink
- * Force if not set, will just executed if the buffer is much to big; set for lifetime strings
- * @returns physical size of the buffer
- */
+// shrink long term buffers to their real size so they don't waste memory
+// Buf buffer to shrink
+// Force if not set, will just executed if the buffer is much to big; set for lifetime strings
+// returns physical size of the buffer
long StrBufShrinkToFit(StrBuf *Buf, int Force) {
if (Buf == NULL)
return -1;
}
-/*
- * Allocate a new buffer with default buffer size
- * @returns the new stringbuffer
- */
+// Allocate a new buffer with default buffer size
+// returns the new stringbuffer
StrBuf *NewStrBuf(void) {
StrBuf *NewBuf;
}
-/*
- * Copy Constructor; returns a duplicate of CopyMe
- * CopyMe Buffer to faxmilate
- * @returns the new stringbuffer
- */
+// Copy Constructor; returns a duplicate of CopyMe
+// CopyMe Buffer to faxmilate
+// returns the new stringbuffer
StrBuf *NewStrBufDup(const StrBuf *CopyMe) {
StrBuf *NewBuf;
}
-/*
- * Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards.
- * NoMe if non-NULL, we will use that buffer as value; KeepOriginal will abused as len.
- * CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal.
- * CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe
- * KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer?
- * @returns the new stringbuffer
- */
+// Copy Constructor; CreateRelpaceMe will contain CopyFlushMe afterwards.
+// NoMe if non-NULL, we will use that buffer as value; KeepOriginal will abused as len.
+// CopyFlushMe Buffer to faxmilate if KeepOriginal, or to move into CreateRelpaceMe if !KeepOriginal.
+// CreateRelpaceMe If NULL, will be created, else Flushed and filled CopyFlushMe
+// KeepOriginal should CopyFlushMe remain intact? or may we Steal its buffer?
+// returns the new stringbuffer
void NewStrBufDupAppendFlush(StrBuf **CreateRelpaceMe, StrBuf *CopyFlushMe, const char *NoMe, int KeepOriginal) {
StrBuf *NewBuf;
return;
}
- /*
- * Randomly Chosen: bigger than 64 chars is cheaper to swap the buffers instead of copying.
- * else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might
- * be a big IO-Buffer...
- */
+ // Randomly Chosen: bigger than 64 chars is cheaper to swap the buffers instead of copying.
+ // else *CreateRelpaceMe may use more memory than needed in a longer term, CopyFlushMe might
+ // be a big IO-Buffer...
if (KeepOriginal || (StrLength(CopyFlushMe) < 256)) {
if (*CreateRelpaceMe == NULL) {
*CreateRelpaceMe = NewBuf = NewStrBufPlain(NULL, CopyFlushMe->BufUsed);
}
-/*
- * create a new Buffer using an existing c-string
- * this function should also be used if you want to pre-suggest
- * the buffer size to allocate in conjunction with ptr == NULL
- * ptr the c-string to copy; may be NULL to create a blank instance
- * nChars How many chars should we copy; -1 if we should measure the length ourselves
- * @returns the new stringbuffer
- */
+// create a new Buffer using an existing c-string
+// this function should also be used if you want to pre-suggest
+// the buffer size to allocate in conjunction with ptr == NULL
+// ptr the c-string to copy; may be NULL to create a blank instance
+// nChars How many chars should we copy; -1 if we should measure the length ourselves
+// returns the new stringbuffer
StrBuf *NewStrBufPlain(const char* ptr, int nChars) {
StrBuf *NewBuf;
size_t Siz = BaseStrBufSize;
}
-/*
- * Set an existing buffer from a c-string
- * Buf buffer to load
- * ptr c-string to put into
- * nChars set to -1 if we should work 0-terminated
- * @returns the new length of the string
- */
+//
+// Set an existing buffer from a c-string
+// Buf buffer to load
+// ptr c-string to put into
+// nChars set to -1 if we should work 0-terminated
+// @returns the new length of the string
+///
int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars) {
size_t Siz;
size_t CopySize;
}
-/*
- * use strbuf as wrapper for a string constant for easy handling
- * StringConstant a string to wrap
- * SizeOfStrConstant should be sizeof(StringConstant)-1
- */
-StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
-{
+//
+// use strbuf as wrapper for a string constant for easy handling
+// StringConstant a string to wrap
+// SizeOfStrConstant should be sizeof(StringConstant)-1
+///
+StrBuf *_NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant) {
StrBuf *NewBuf;
NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
}
-/*
- * flush the content of a Buf; keep its struct
- * buf Buffer to flush
- */
+//
+// flush the content of a Buf; keep its struct
+// buf Buffer to flush
+///
int FlushStrBuf(StrBuf *buf) {
if ((buf == NULL) || (buf->buf == NULL)) {
return -1;
return 0;
}
-/*
- * wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
- * buf Buffer to wipe
- */
-int FLUSHStrBuf(StrBuf *buf)
-{
+//
+// wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
+// buf Buffer to wipe
+///
+int FLUSHStrBuf(StrBuf *buf) {
if (buf == NULL)
return -1;
if (buf->ConstBuf)
#ifdef SIZE_DEBUG
int hFreeDbglog = -1;
#endif
-/*
- * Release a Buffer
- * Its a double pointer, so it can NULL your pointer
- * so fancy SIG11 appear instead of random results
- * FreeMe Pointer Pointer to the buffer to free
- */
-void FreeStrBuf (StrBuf **FreeMe)
-{
+//
+// Release a Buffer
+// Its a double pointer, so it can NULL your pointer
+// so fancy SIG11 appear instead of random results
+// FreeMe Pointer Pointer to the buffer to free
+///
+void FreeStrBuf (StrBuf **FreeMe) {
if (*FreeMe == NULL)
return;
*FreeMe = NULL;
}
-/*
- * flatten a Buffer to the Char * we return
- * Its a double pointer, so it can NULL your pointer
- * so fancy SIG11 appear instead of random results
- * The Callee then owns the buffer and is responsible for freeing it.
- * SmashMe Pointer Pointer to the buffer to release Buf from and free
- * @returns the pointer of the buffer; Callee owns the memory thereafter.
- */
+//
+// flatten a Buffer to the Char * we return
+// Its a double pointer, so it can NULL your pointer
+// so fancy SIG11 appear instead of random results
+// The Callee then owns the buffer and is responsible for freeing it.
+// SmashMe Pointer Pointer to the buffer to release Buf from and free
+// @returns the pointer of the buffer; Callee owns the memory thereafter.
+///
char *SmashStrBuf (StrBuf **SmashMe) {
char *Ret;
}
-/*
- * Release the buffer
- * If you want put your StrBuf into a Hash, use this as Destructor.
- * VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
- */
+// Release the buffer
+// If you want put your StrBuf into a Hash, use this as Destructor.
+// VFreeMe untyped pointer to a StrBuf. be shure to do the right thing [TM]
void HFreeStrBuf (void *VFreeMe) {
StrBuf *FreeMe = (StrBuf*)VFreeMe;
if (FreeMe == NULL)
}
-/*******************************************************************************
- * Simple string transformations *
- *******************************************************************************/
+//******************************************************************************
+// Simple string transformations *
+//******************************************************************************/
-// Wrapper around atol
+// Wrapper around atol
long StrTol(const StrBuf *Buf) {
if (Buf == NULL)
return 0;
}
-// Wrapper around atoi
+// Wrapper around atoi
int StrToi(const StrBuf *Buf) {
if (Buf == NULL)
return 0;
return NCharsRemain;
}
-/**
- * Cut nChars from the start of the string
- * Buf Buffer to modify
- * nChars how many chars should be skipped?
- */
-void StrBufCutLeft(StrBuf *Buf, int nChars)
-{
+// Cut nChars from the start of the string
+// Buf Buffer to modify
+// nChars how many chars should be skipped?
+void StrBufCutLeft(StrBuf *Buf, int nChars) {
if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
if (nChars >= Buf->BufUsed) {
FlushStrBuf(Buf);
Buf->buf[Buf->BufUsed] = '\0';
}
-/**
- * Cut the trailing n Chars from the string
- * Buf Buffer to modify
- * nChars how many chars should be trunkated?
- */
-void StrBufCutRight(StrBuf *Buf, int nChars)
-{
+// Cut the trailing n Chars from the string
+// Buf Buffer to modify
+// nChars how many chars should be trunkated?
+void StrBufCutRight(StrBuf *Buf, int nChars) {
if ((Buf == NULL) || (Buf->BufUsed == 0) || (Buf->buf == NULL))
return;
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.
- */
+//*
+// 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)
{
if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
}
-/**
- * Strip leading and trailing spaces from a string; with premeasured and adjusted length.
- * Buf the string to modify
- */
+//*
+// Strip leading and trailing spaces from a string; with premeasured and adjusted length.
+// Buf the string to modify
+///
void StrBufTrim(StrBuf *Buf)
{
int delta = 0;
}
if (delta > 0) StrBufCutLeft(Buf, delta);
}
-/**
- * changes all spaces in the string (tab, linefeed...) to Blank (0x20)
- * Buf the string to modify
- */
+//*
+// changes all spaces in the string (tab, linefeed...) to Blank (0x20)
+// Buf the string to modify
+///
void StrBufSpaceToBlank(StrBuf *Buf)
{
char *pche, *pch;
}
-/**
- * uppercase the contents of a buffer
- * Buf the buffer to translate
- */
+//*
+// uppercase the contents of a buffer
+// Buf the buffer to translate
+///
void StrBufUpCase(StrBuf *Buf)
{
char *pch, *pche;
}
-/**
- * lowercase the contents of a buffer
- * Buf the buffer to translate
- */
+//*
+// lowercase the contents of a buffer
+// Buf the buffer to translate
+///
void StrBufLowerCase(StrBuf *Buf)
{
char *pch, *pche;
}
-/*******************************************************************************
- * a tokenizer that kills, maims, and destroys *
- *******************************************************************************/
+//******************************************************************************
+// a tokenizer that kills, maims, and destroys *
+//******************************************************************************/
-/**
- * Replace a token at a given place with a given length by another token with given length
- * Buf String where to work on
- * where where inside of the Buf is the search-token
- * HowLong How long is the token to be replaced
- * Repl Token to insert at 'where'
- * ReplLen Length of repl
- * @returns -1 if fail else length of resulting Buf
- */
+//*
+// Replace a token at a given place with a given length by another token with given length
+// Buf String where to work on
+// where where inside of the Buf is the search-token
+// HowLong How long is the token to be replaced
+// Repl Token to insert at 'where'
+// 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) {
if ((Buf == NULL) || (where > Buf->BufUsed) || (where + HowLong > Buf->BufUsed)) {
memmove(Buf->buf + where + ReplLen, Buf->buf + where + HowLong, Buf->BufUsed - where - HowLong);
memcpy(Buf->buf + where, Repl, ReplLen);
Buf->BufUsed += ReplLen - HowLong;
+ Buf->buf[Buf->BufUsed] = 0;
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
- */
+//*
+// 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;
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.
- */
+//*
+// 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 ReducedBy;
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.
- */
+//*
+// 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 * /
+ const char *s, *e; // source
+ int len = 0; // running total length of extracted string
+ int current_token = 0; // token currently being processed
if (dest != NULL) {
dest->buf[0] = '\0';