diff options
| author | fusion32 <marcopuzziello@gmail.com> | 2025-10-15 04:13:13 -0300 |
|---|---|---|
| committer | fusion32 <marcopuzziello@gmail.com> | 2025-10-15 04:13:13 -0300 |
| commit | 7a2358c859dfe5251b709e63f35bc3ed5658e3e2 (patch) | |
| tree | 6d5047bf5a9ee27ed5db5276e0caf69277a0ee16 /src/querymanager.hh | |
| parent | a4b5623497d10febf17231b47d9a1b4cd3c6767d (diff) | |
| download | querymanager-7a2358c859dfe5251b709e63f35bc3ed5658e3e2.tar.gz querymanager-7a2358c859dfe5251b709e63f35bc3ed5658e3e2.zip | |
postgres interval helpers + a few more queries
Diffstat (limited to 'src/querymanager.hh')
| -rw-r--r-- | src/querymanager.hh | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/querymanager.hh b/src/querymanager.hh index 7a5c21b..baef19c 100644 --- a/src/querymanager.hh +++ b/src/querymanager.hh @@ -156,6 +156,8 @@ int RoundSecondsToDays(int Seconds); bool StringEmpty(const char *String); bool StringEq(const char *A, const char *B); bool StringEqCI(const char *A, const char *B); +bool StringStartsWith(const char *String, const char *Prefix); +bool StringStartsWithCI(const char *String, const char *Prefix); bool StringCopyN(char *Dest, int DestCapacity, const char *Src, int SrcLength); bool StringCopy(char *Dest, int DestCapacity, const char *Src); void StringCopyEllipsis(char *Dest, int DestCapacity, const char *Src); @@ -638,6 +640,114 @@ public: const T *end(void) const { return m_Data + m_Length; } }; +// String Buffer +//============================================================================== +template<int N = KB(2)> +struct StringBuffer{ +private: + STATIC_ASSERT(N > 0); + int m_Position; + char m_Buffer[N]; + +public: + StringBuffer(void) { Reset(); } + StringBuffer(const StringBuffer &Other) = delete; + void operator=(const StringBuffer &Other) = delete; + + bool Overflowed(void){ return m_Position >= N; } + bool Empty(void){ return m_Position == 0; } + + void Reset(void) { + m_Position = 0; + m_Buffer[0] = 0; + } + + void Assign(const char *String){ + int StringLength = (int)strlen(String); + int CopyLength = StringLength; + + if(CopyLength >= N){ + CopyLength = (N - 1); + } + + if(CopyLength > 0){ + memcpy(m_Buffer, String, CopyLength); + } + + m_Buffer[CopyLength] = 0; + m_Position = StringLength; + } + + void Format(const char *Format, ...) ATTR_PRINTF(2, 3) { + va_list ap; + va_start(ap, Format); + int Written = vsnprintf(m_Buffer, N, Format, ap); + va_end(ap); + + if(Written >= 0){ + m_Position = Written; + }else{ + m_Position = 0; + m_Buffer[0] = 0; + } + } + + void Append(const char *String){ + if(m_Position >= (N - 1)){ + return; + } + + int StringLength = (int)strlen(String); + int CopyLength = StringLength; + int Remainder = (N - m_Position); + + if(CopyLength >= Remainder){ + CopyLength = (Remainder - 1); + } + + if(CopyLength > 0){ + memcpy(m_Buffer + m_Position, String, CopyLength); + } + + m_Buffer[m_Position + CopyLength] = 0; + m_Position += StringLength; + } + + void FormatAppend(const char *Format, ...) ATTR_PRINTF(2, 3) { + if(m_Position >= (N - 1)){ + return; + } + + va_list ap; + va_start(ap, Format); + int Written = vsnprintf((m_Buffer + m_Position), (N - m_Position), Format, ap); + va_end(ap); + + if(Written >= 0){ + m_Position += Written; + }else{ + m_Buffer[m_Position] = 0; + } + } + + int Length(void){ + if(Overflowed()){ + return (N - 1); + }else{ + return m_Position; + } + } + + const char *CString(void){ + if(Overflowed()){ + m_Buffer[N - 1] = 0; + }else{ + m_Buffer[m_Position] = 0; + } + return m_Buffer; + } +}; + // sha256.cc //============================================================================== void SHA256(const uint8 *Input, int InputBytes, uint8 *Digest); |
