summaryrefslogtreecommitdiff
path: root/libs/util
diff options
context:
space:
mode:
authorgeronimo <geronimo013@gmx.de>2012-08-04 09:10:44 (GMT)
committergeronimo <geronimo013@gmx.de>2012-08-04 09:10:44 (GMT)
commite8175c7b23047fefd52f85afe05e2864b2a9bba8 (patch)
tree8dab4e287545805d173a6c8e297c1798a1a01599 /libs/util
parentc7d67f443bdfe842c2e356d4edbe345338e3e08d (diff)
downloadcmp-e8175c7b23047fefd52f85afe05e2864b2a9bba8.tar.gz
cmp-e8175c7b23047fefd52f85afe05e2864b2a9bba8.tar.bz2
created new icons to enable visual feedback on mediatype selection, changed json-parser and enabled virtual mediatype usage
Diffstat (limited to 'libs/util')
-rw-r--r--libs/util/include/StringBuilder.h7
-rw-r--r--libs/util/src/JSonWriter.cc5
-rw-r--r--libs/util/src/StringBuilder.cc50
3 files changed, 57 insertions, 5 deletions
diff --git a/libs/util/include/StringBuilder.h b/libs/util/include/StringBuilder.h
index db98934..977c4e7 100644
--- a/libs/util/include/StringBuilder.h
+++ b/libs/util/include/StringBuilder.h
@@ -34,7 +34,7 @@ public:
cStringBuilder(int chunkSize = 127);
virtual ~cStringBuilder();
- cStringBuilder &Append(const char *Text);
+ cStringBuilder &Append(const char *Text, const char char2Replace[] = NULL, const char *replacements[] = NULL);
cStringBuilder &Append(char c);
cStringBuilder &Append(bool v, const char *TrueValue = "X", const char *FalseValue="-");
cStringBuilder &Append(double v);
@@ -48,9 +48,12 @@ public:
size_t Copy(char *Buf, size_t BufSize);
char *toString(void);
+protected:
+ void Write(const char *Text);
+ void WriteAndReplace(const char *Text, const char char2Replace[], const char *replacements[]);
+
private:
void init(void);
- void Write(const char *p);
int chunkSize;
size_t readOffset; ///< read offest is the offset over all chunks (thus goes from 0 to size)
///< Copy can be called subsequently, if Buf is smaller than StringBuilders size
diff --git a/libs/util/src/JSonWriter.cc b/libs/util/src/JSonWriter.cc
index ec9b1dc..8fcb140 100644
--- a/libs/util/src/JSonWriter.cc
+++ b/libs/util/src/JSonWriter.cc
@@ -28,6 +28,9 @@
#include <stdio.h>
#define DEBUG 1
+static const char char2Replace[] = { '"', '\0' };
+static const char *replacements[] = { "\\\"", NULL };
+
cJSonWriter::cJSonWriter(cStringBuilder &StringBuilder)
: lastState(JS_Unknown)
, sb(StringBuilder)
@@ -130,7 +133,7 @@ cJSonWriter &cJSonWriter::Value(const char *Text) {
}
else {
PopState();
- sb.Append("\"").Append(Text).Append("\"");
+ sb.Append("\"").Append(Text, char2Replace, replacements).Append("\"");
}
return *this;
}
diff --git a/libs/util/src/StringBuilder.cc b/libs/util/src/StringBuilder.cc
index 28b048e..7da1aac 100644
--- a/libs/util/src/StringBuilder.cc
+++ b/libs/util/src/StringBuilder.cc
@@ -91,6 +91,51 @@ void cStringBuilder::Write(const char *Text)
}
}
+void cStringBuilder::WriteAndReplace(const char *Text, const char char2Replace[], const char *replacements[])
+{
+ if (!Text) {
+ esyslog("ERROR: text to add is a NULL-pointer!");
+ return;
+ }
+ int done;
+ char *curChunk = (char *) pool[pool.size() - 1];
+ char *chunkLast = curChunk + chunkSize;
+ char *pd;
+ const char *ps, *srcLast = Text + strlen(Text), *pChk;
+
+ for (ps = Text, pd = curChunk + writeOffset; ps < srcLast; ++ps) {
+ done = 0;
+ for (pChk = char2Replace; pChk && *pChk; ++pChk) {
+ if (*ps == *pChk) {
+ const char *rps = replacements[pChk - char2Replace];
+ done = 1;
+
+ while (rps && *rps) {
+ if (writeOffset == chunkSize) {
+ curChunk = (char *) malloc(chunkSize);
+ pool.push_back(curChunk);
+ writeOffset = 0;
+ pd = curChunk;
+ chunkLast = curChunk + chunkSize;
+ }
+ *pd++ = *rps++;
+ ++writeOffset;
+ }
+ }
+ }
+ if (done) continue;
+ if (writeOffset == chunkSize) {
+ curChunk = (char *) malloc(chunkSize);
+ pool.push_back(curChunk);
+ writeOffset = 0;
+ pd = curChunk;
+ chunkLast = curChunk + chunkSize;
+ }
+ *pd++ = *ps;
+ ++writeOffset;
+ }
+}
+
size_t cStringBuilder::Size()
{
return (pool.size() - 1) * chunkSize + writeOffset;
@@ -125,10 +170,11 @@ size_t cStringBuilder::Copy(char* Buf, size_t BufSize)
return bytesWritten;
}
-cStringBuilder &cStringBuilder::Append(const char* Text)
+cStringBuilder &cStringBuilder::Append(const char* Text, const char char2Replace[], const char *replacements[])
{
if (!Text) Write("(null)");
- Write(Text);
+ if (char2Replace && replacements) WriteAndReplace(Text, char2Replace, replacements);
+ else Write(Text);
return *this;
}