diff options
author | geronimo <geronimo013@gmx.de> | 2012-07-13 04:26:40 +0200 |
---|---|---|
committer | geronimo <geronimo013@gmx.de> | 2012-07-13 04:26:40 +0200 |
commit | 2d48ae784ea6828e8626c32c848f64232d8f35c0 (patch) | |
tree | fab114b03e91125783a778b835dd1913b039cebe /libs/util/src | |
download | cmp-2d48ae784ea6828e8626c32c848f64232d8f35c0.tar.gz cmp-2d48ae784ea6828e8626c32c848f64232d8f35c0.tar.bz2 |
initial import
Diffstat (limited to 'libs/util/src')
-rw-r--r-- | libs/util/src/AbstractListAssembler.cc | 57 | ||||
-rw-r--r-- | libs/util/src/Codec.cc | 248 | ||||
-rw-r--r-- | libs/util/src/JSonWriter.cc | 178 | ||||
-rw-r--r-- | libs/util/src/MD5Calculator.cc | 57 | ||||
-rw-r--r-- | libs/util/src/ManagedMap.cc | 73 | ||||
-rw-r--r-- | libs/util/src/ManagedVector.cc | 51 | ||||
-rw-r--r-- | libs/util/src/NamedValue.cc | 67 | ||||
-rw-r--r-- | libs/util/src/StringBuilder.cc | 178 | ||||
-rw-r--r-- | libs/util/src/util.cc | 61 |
9 files changed, 970 insertions, 0 deletions
diff --git a/libs/util/src/AbstractListAssembler.cc b/libs/util/src/AbstractListAssembler.cc new file mode 100644 index 0000000..e83f965 --- /dev/null +++ b/libs/util/src/AbstractListAssembler.cc @@ -0,0 +1,57 @@ +/** + * ======================== legal notice ====================== + * + * File: AbstractListAssembler.cc + * Created: 6. Juli 2012, 09:38 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <AbstractListAssembler.h> +#include <ManagedVector.h> +#include <StringBuilder.h> +#include <Logging.h> + +cAbstractListAssembler::cAbstractListAssembler() +{ +} + +cAbstractListAssembler::~cAbstractListAssembler() +{ +} + +bool cAbstractListAssembler::AssembleList(cStringBuilder& sb, cManagedVector &ElemVector, std::map<int, size_t> &Categories, size_t start, uint delta) +{ + isyslog("::AssembleList() with start=%lu, delta=%i", start, delta); + int n = 0; + size_t end = start + delta; + + if (end >= ElemVector.size()) { + end = ElemVector.size(); + if (delta >= ElemVector.size()) start = 0; + else start = ElemVector.size() - delta; + } + OpenList(sb, Categories, ElemVector.size(), start, delta); + isyslog("now really assemble list with start=%lu, delta=%i", start, delta); + for (size_t i=start; i < end; ++i) { + AddElement(sb, ElemVector[i], ++n % 2); + } + CloseList(sb, ElemVector.size(), start, delta); + + return true; +}
\ No newline at end of file diff --git a/libs/util/src/Codec.cc b/libs/util/src/Codec.cc new file mode 100644 index 0000000..bf6acf3 --- /dev/null +++ b/libs/util/src/Codec.cc @@ -0,0 +1,248 @@ +/** + * ======================== legal notice ====================== + * + * File: Codec.cc + * Created: 21. Mai 2012, 14:00 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include "Codec.h" +#include "util.h" +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include <openssl/md5.h> + +// --- Codec ------------------------------------------------------------------ +static cHexEncoder * hexEncoder = NULL; +static cHexDecoder * hexDecoder = NULL; + +cCodec::cCodec(const char *Table, char SpecialChar) + : translateTable(strdup(Table)) + , keyChar(SpecialChar) +{ +} + +cCodec::~cCodec() +{ + FREE(translateTable); +} + +size_t cCodec::DecodeSequence(unsigned char *d, unsigned char *s) +{ + *d = *s; + + return 1; +} + +// --- Encoder ---------------------------------------------------------------- + +cEncoder::cEncoder(char SpecialChar, const char *TrTable) + : cCodec(TrTable, SpecialChar) +{ +} + +cEncoder::~cEncoder() +{ +} + +char *cEncoder::Encode(const char *Source, size_t SourceLength) +{ + if (!Source) return NULL; + if (!SourceLength) SourceLength = strlen(Source); + + const unsigned char *s = (const unsigned char *)Source; + const unsigned char *last = (const unsigned char *)(Source + SourceLength); + char *rv = (char *)malloc(SourceLength * 3 + 1); + char *d = rv; + unsigned char c; + + for (; s < last; ++s) { + c = *s; + if (c < 0x80 && (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')) + *d++ = c; + else if (c == ' ') + *d++ = '+'; + else { + *d++ = KeyChar(); + *d++ = TranslateTable()[c >> 4]; + *d++ = TranslateTable()[c & 0x0F]; + } + } + *d = 0; + + return rv; +} + +// --- URLEncoder ------------------------------------------------------------- + +cURLEncoder::cURLEncoder() + : cEncoder('%') +{ +} + +cURLEncoder::~cURLEncoder() +{ +} + +// --- Decoder ---------------------------------------------------------------- + +cDecoder::cDecoder(char SpecialChar, const char *TrTable) + : cCodec(TrTable, SpecialChar) +{ +} + +cDecoder::~cDecoder() +{ +} + +char *cDecoder::Decode(const char *Source, size_t SourceLength) +{ + if (!Source) return NULL; + if (!SourceLength) SourceLength = strlen(Source); + + const unsigned char *s = (const unsigned char *)Source; + const unsigned char *last = (const unsigned char *)(Source + SourceLength); + char *rv = (char *)malloc(SourceLength + 1); + unsigned char *d = (unsigned char *) rv; + size_t seqLen; + + for (; s < last; ++s) { + if (*s == KeyChar()) { + seqLen = DecodeSequence(d++, (unsigned char *) ++s); + s += seqLen; + } + else if (*s == '+') { + *d++ = ' '; + } + else { + *d++ = *s; + } + } + *d = 0; + + return rv; +} + +size_t cDecoder::DecodeSequence(unsigned char *d, unsigned char *s) +{ + // just transform hex sequence back to character + unsigned char c = s[0] >= 'A' ? ((s[0] & 0xDF) - 'A') + 10 : (s[0] - '0'); + + c *= 16; + c += s[1] >= 'A' ? ((s[1] & 0xDF) - 'A') + 10 : (s[1] - '0'); + *d = c; + + return 1; +} + +// --- URLDecoder ------------------------------------------------------------- + +cURLDecoder::cURLDecoder() + : cDecoder('%') +{ +} + +cURLDecoder::~cURLDecoder() +{ +} + +// --- HexEncoder ------------------------------------------------------------- + +cHexEncoder::cHexEncoder() + : cEncoder('#', "0123456789abcdef") +{ +} + +cHexEncoder::~cHexEncoder() +{ +} + +char* cHexEncoder::Encode(const unsigned char* Source, size_t SourceLength) +{ + if (!SourceLength) SourceLength = strlen((const char *)Source); + const unsigned char *s = (const unsigned char *)Source; + char *rv = (char *)malloc(SourceLength * 3 + 1); + char *d = rv; + unsigned char c; + + for (int i=0; i < 16; ++i, ++s) { + c = *s; + *d++ = TranslateTable()[c >> 4]; + *d++ = TranslateTable()[c & 0x0F]; + } + *d = 0; + + return rv; +} + +cHexEncoder * getHexEncoder(void) { + if (!hexEncoder) hexEncoder = new cHexEncoder(); + return hexEncoder; +} + +// --- HexDecoder ------------------------------------------------------------- + +cHexDecoder::cHexDecoder() +{ +} + +cHexDecoder::~cHexDecoder() +{ +} + +char* cHexDecoder::Decode(const char* Source, size_t SourceLength) +{ + if (!Source) return NULL; + if (!SourceLength) SourceLength = strlen(Source); + + const unsigned char *s = (const unsigned char *)Source; + const unsigned char *last = (const unsigned char *)(Source + SourceLength); + char *rv = (char *)malloc(SourceLength + 1); + unsigned char *d = (unsigned char *) rv; + unsigned char c0, c1; + + for (; s < last; s += 2) { + c0 = toupper(s[0]); + c1 = c0 >= 'A' ? ((c0 & 0xDF) - 'A') + 10 : (c0 - '0'); + c1 *= 16; + c0 = toupper(s[1]); + c1 = c0 >= 'A' ? ((c0 & 0xDF) - 'A') + 10 : (c0 - '0'); + *d++ = c1; + } + *d = 0; + + return rv; +} + +cHexDecoder * getHexDecoder(void) { + if (!hexDecoder) hexDecoder = new cHexDecoder(); + return hexDecoder; +} + +void codecCleanUp(void) { + if (hexEncoder) { + delete hexEncoder; + hexEncoder = NULL; + } + if (hexDecoder) { + delete hexDecoder; + hexDecoder = NULL; + } +} diff --git a/libs/util/src/JSonWriter.cc b/libs/util/src/JSonWriter.cc new file mode 100644 index 0000000..cde994c --- /dev/null +++ b/libs/util/src/JSonWriter.cc @@ -0,0 +1,178 @@ +/** + * ======================== legal notice ====================== + * + * File: JSonWriter.cc + * Created: 6. Juli 2012, 12:47 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <JSonWriter.h> +#include <StringBuilder.h> +#include <Logging.h> +#include <stdio.h> + +cJSonWriter::cJSonWriter(cStringBuilder &StringBuilder) + : lastState(JS_Unknown) + , sb(StringBuilder) +{ +} + +cJSonWriter::~cJSonWriter() +{ +} + +cJSonWriter::JSonState cJSonWriter::State() +{ + if (state.empty()) return JS_Unknown; + return state.top(); +} + +void cJSonWriter::PushState(JSonState State) +{ + state.push(State); +} + +cJSonWriter::JSonState cJSonWriter::PopState(void) +{ + lastState = State(); + state.pop(); + + return lastState; +} + +cJSonWriter &cJSonWriter::Object(void) { + if (lastState == JS_Object) sb.Append(","); + sb.Append("{"); + PushState(JS_Object); + lastState = JS_Unknown; + return *this; +} + +cJSonWriter &cJSonWriter::EndObject(void) { + if (State() != JS_Object) { + esyslog("JSonWriter::EndObject(): invalid status %d - should be %d", State(), JS_Object); + } + else { + PopState(); + sb.Append("}"); + } + return *this; +} + +cJSonWriter &cJSonWriter::Array(void) { + if (State() != JS_Key) { + esyslog("JSonWriter::Array(): invalid status %d - should be %d", State(), JS_Key); + } + else { + sb.Append("["); + PushState(JS_Array); + lastState = JS_Unknown; + } + return *this; +} + +cJSonWriter &cJSonWriter::EndArray(void) { + if (State() != JS_Array) { + esyslog("JSonWriter::EndArray(): invalid status %d - should be %d", State(), JS_Array); + } + else { + sb.Append("]"); + PopState(); + if (State() == JS_Key) PopState(); // array is a value, so pop the key + } + return *this; +} + +cJSonWriter &cJSonWriter::Key(const char *Name) { + if (State() != JS_Object) { + esyslog("JSonWriter::Key(): invalid status %d - should be %d", State(), JS_Object); + } + else { + if (lastState == JS_Key) sb.Append(", "); + sb.Append("\"").Append(Name).Append("\": "); + PushState(JS_Key); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(const char *Text) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append("\"").Append(Text).Append("\""); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(int v) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append(v); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(long v) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append(v); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(size_t v) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append(v); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(double v) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append(v); + } + return *this; +} + +cJSonWriter &cJSonWriter::Value(bool v) { + if (State() != JS_Key) { + esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key); + } + else { + PopState(); + sb.Append(v, "true", "false"); + } + return *this; +} diff --git a/libs/util/src/MD5Calculator.cc b/libs/util/src/MD5Calculator.cc new file mode 100644 index 0000000..7f2aea4 --- /dev/null +++ b/libs/util/src/MD5Calculator.cc @@ -0,0 +1,57 @@ +/** + * ======================== legal notice ====================== + * + * File: MD5Calculator.cc + * Created: 3. Juli 2012, 13:15 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <MD5Calculator.h> +#include <Codec.h> +#include <string.h> + +cMD5Calculator::cMD5Calculator() +{ + MD5_Init(&context); +} + +cMD5Calculator::~cMD5Calculator() +{ +} + +void cMD5Calculator::AddContent(const char* Buf, size_t bufSize) +{ + if (!bufSize) bufSize = strlen(Buf); + MD5_Update(&context, Buf, bufSize); +} + +char * cMD5Calculator::Hash(void) +{ + unsigned char md[20]; + + MD5_Final(md, &context); + + return getHexEncoder()->Encode(md, 16); +} + +void cMD5Calculator::Reset() +{ + memset(&context, 0, sizeof(context)); + MD5_Init(&context); +}
\ No newline at end of file diff --git a/libs/util/src/ManagedMap.cc b/libs/util/src/ManagedMap.cc new file mode 100644 index 0000000..e0dd528 --- /dev/null +++ b/libs/util/src/ManagedMap.cc @@ -0,0 +1,73 @@ +/** + * ======================== legal notice ====================== + * + * File: ManagedMap.cc + * Created: 7. Juli 2012, 08:48 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <ManagedMap.h> +#include <Logging.h> +#include <stdlib.h> + +cManagedMap::cManagedMap(void (*cbFreeElem)(void *)) + : freeCallback(cbFreeElem) +{ +} + +cManagedMap::~cManagedMap() +{ + clear(); +} + +void cManagedMap::clear() +{ + iterator it = internalMap.begin(); + + while (it != internalMap.end()) { + if (freeCallback) { + isyslog("gonna free %s", it->first.c_str()); + freeCallback(it->second); + } + ++it; + } + internalMap.clear(); +} + +void *cManagedMap::get(const char* key) +{ + const_iterator it = internalMap.find(key); + + if (it == internalMap.end()) return NULL; + return it->second; +} + +const void *cManagedMap::get(const char* key) const +{ + const_iterator it = internalMap.find(key); + + if (it == internalMap.end()) return NULL; + return it->second; +} + +void cManagedMap::put(const char* key, void* value) +{ + isyslog("cManagedMap::put[%s] => 0x%0X", key, value); + internalMap[key] = value; +}
\ No newline at end of file diff --git a/libs/util/src/ManagedVector.cc b/libs/util/src/ManagedVector.cc new file mode 100644 index 0000000..7a67bfb --- /dev/null +++ b/libs/util/src/ManagedVector.cc @@ -0,0 +1,51 @@ +/** + * ======================== legal notice ====================== + * + * File: ManagedVector.cc + * Created: 7. Juli 2012, 08:30 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <ManagedVector.h> +#include <vector> +#include <algorithm> + +cManagedVector::cManagedVector(void (*cbFreeElem)(void *)) + : freeCallback(cbFreeElem) +{ +} + +cManagedVector::~cManagedVector() +{ + clear(); +} + +void cManagedVector::clear() +{ + for (size_t i=0; i < internalVector.size(); ++i) { + freeCallback(internalVector[i]); + } + internalVector.clear(); +} + +void cManagedVector::sort(bool (*fnSort)(void *a, void *b)) +{ + if (fnSort) std::sort(internalVector.begin(), internalVector.end(), fnSort); + else std::sort(internalVector.begin(), internalVector.end()); +}
\ No newline at end of file diff --git a/libs/util/src/NamedValue.cc b/libs/util/src/NamedValue.cc new file mode 100644 index 0000000..f807cb6 --- /dev/null +++ b/libs/util/src/NamedValue.cc @@ -0,0 +1,67 @@ +/** + * ======================== legal notice ====================== + * + * File: NamedValue.cc + * Created: 3. Juli 2012, 17:43 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <NamedValue.h> +#include <util.h> +#include <stdlib.h> +#include <stdio.h> + + +cNamedValue::cNamedValue(char *Name, char *Value, bool takePointers) + : name(takePointers ? Name : strdup(Name)) + , value(takePointers ? Value : Value ? strdup(Value) : NULL) +{ +} + +cNamedValue::cNamedValue(const char *Name, const char *Value) + : name(strdup(Name)) + , value(Value ? strdup(Value) : NULL) +{ +} + +cNamedValue::cNamedValue(const cNamedValue &other) + : name(strdup(other.name)) + , value(other.value ? strdup(other.value) : NULL) +{ + printf(">>> OUPS - just cloned a named value with name<%s> and value<%s>\n", name, value); +} + +cNamedValue::~cNamedValue() +{ + FREE(name); + FREE(value); +} + +cNamedValue &cNamedValue::operator=(const cNamedValue &other) +{ + printf(">>> OUPS - I'm inside of operator = !!!\n"); + if (&other == this) return *this; + FREE(name); + FREE(value); + name = strdup(other.Name()); + if (other.value) value = strdup(other.Value()); + else value = NULL; + + return *this; +}
\ No newline at end of file diff --git a/libs/util/src/StringBuilder.cc b/libs/util/src/StringBuilder.cc new file mode 100644 index 0000000..8e90151 --- /dev/null +++ b/libs/util/src/StringBuilder.cc @@ -0,0 +1,178 @@ +/** + * ======================== legal notice ====================== + * + * File: StringBuilder.cc + * Created: 6. Juli 2012, 10:54 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <StringBuilder.h> +#include <Logging.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +static void freeStringCallback(void *elem) +{ + free(elem); +} + +cStringBuilder::cStringBuilder(const char *First) + : chunkSize(127) + , readOffset(0) + , writeOffset(0) + , pool(freeStringCallback) +{ + init(); + if (First) Append(First); +} + +cStringBuilder::cStringBuilder(int ChunkSize) + : chunkSize(ChunkSize) + , readOffset(0) + , writeOffset(0) + , pool(freeStringCallback) +{ + init(); +} + +cStringBuilder::~cStringBuilder() +{ +} + +void cStringBuilder::init(void) +{ + char *first = (char *) malloc(chunkSize); + + pool.push_back(first); +} + +void cStringBuilder::Write(const char *Text) +{ + if (!Text) { + esyslog("ERROR: text to add is a NULL-ponter!"); + return; + } + uint chunkFree = chunkSize - writeOffset; + char *curChunk = (char *) pool[pool.size() - 1]; + const char *p = Text; + + while (strlen(p) >= chunkFree) { + strncpy(curChunk + writeOffset, p, chunkFree); + p += chunkFree; + + curChunk = (char *) malloc(chunkSize); + pool.push_back(curChunk); + writeOffset = 0; + chunkFree = chunkSize; + } + if (strlen(p)) { + strcpy(curChunk + writeOffset, p); + writeOffset += strlen(p); + } +} + +size_t cStringBuilder::Size() +{ + return (pool.size() - 1) * chunkSize + writeOffset; +} + +size_t cStringBuilder::Size() const +{ + return (pool.size() - 1) * chunkSize + writeOffset; +} + +size_t cStringBuilder::Copy(char* Buf, size_t BufSize) +{ + uint chunkNo = readOffset / chunkSize; + uint chunkOff = readOffset % chunkSize; + uint bytes2Write = chunkSize; + char *curChunk = (char *) pool[chunkNo]; + size_t bytesWritten = 0; + + if (chunkNo == (pool.size() - 1)) bytes2Write = writeOffset; + while ((BufSize - bytesWritten) > (bytes2Write - chunkOff)) { + strncpy(Buf + bytesWritten, curChunk + chunkOff, bytes2Write - chunkOff); + bytesWritten += bytes2Write - chunkOff; + + chunkOff = 0; + if (++chunkNo < (pool.size() - 1)) bytes2Write = chunkSize; + else bytes2Write = writeOffset; + if (chunkNo == pool.size()) break; + curChunk = (char *) pool[chunkNo]; + } + readOffset += bytesWritten; + + return bytesWritten; +} + +cStringBuilder &cStringBuilder::Append(const char* Text) +{ + if (!Text) Write("(null)"); + Write(Text); + + return *this; +} + +cStringBuilder &cStringBuilder::Append(bool v, const char *TrueValue, const char *FalseValue) +{ + if (v) Write(TrueValue); + else Write(FalseValue); + + return *this; +} + +cStringBuilder &cStringBuilder::Append(double v) +{ + char *elem = NULL; + + asprintf(&elem, "%lg", v); + if (elem) { + Write(elem); + free(elem); + } + return *this; +} + +cStringBuilder &cStringBuilder::Append(int v) +{ + char buf[12]; + + if (snprintf(buf, sizeof(buf), "%d", v)) Write(buf); + + return *this; +} + +cStringBuilder &cStringBuilder::Append(long v) +{ + char buf[24]; + + if (snprintf(buf, sizeof(buf), "%ld", v)) Write(buf); + + return *this; +} + +cStringBuilder &cStringBuilder::Append(size_t v) +{ + char buf[24]; + + if (snprintf(buf, sizeof(buf), "%lu", v)) Write(buf); + + return *this; +} diff --git a/libs/util/src/util.cc b/libs/util/src/util.cc new file mode 100644 index 0000000..8caac91 --- /dev/null +++ b/libs/util/src/util.cc @@ -0,0 +1,61 @@ +/** + * ======================== legal notice ====================== + * + * File: util.cc + * Created: 4. Juli 2012, 05:56 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libutil - base classes used by other libraries + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <util.h> +#include <string.h> +#include <ctype.h> + +const char * skipWhitespace(const char *Buffer) +{ + while (*Buffer && isspace(*Buffer)) ++Buffer; + + return Buffer; +} + +const char *getWord(char *buf, int bufSize, const char *src) +{ + const char *s = src; + char *d = buf; + + if (!strncmp(src, "\n\n", 2) || !strncmp(src, "\r\n\r\n", 4)) + return src; + + while (*s && isspace(*s)) ++s; + while (*s && ((d - buf) < bufSize) && !isspace(*s)) *d++ = *s++; + *d = 0; + + return *s ? s : NULL; +} + +const char *restOfLine(char *buf, int bufSize, const char *src) +{ + const char *s = src; + char *d = buf; + + while (*s && isspace(*s)) ++s; + while (*s && ((d - buf) < bufSize) && *s != '\n' && *s != '\r') *d++ = *s++; + *d = 0; + + return *s ? s : NULL; +} |