* add StrBufSmash() so functions demanding a non-const char* can better cooperate.
authorWilfried Göesgens <willi@citadel.org>
Thu, 10 Sep 2009 20:24:35 +0000 (20:24 +0000)
committerWilfried Göesgens <willi@citadel.org>
Thu, 10 Sep 2009 20:24:35 +0000 (20:24 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_test.c

index 92f76f07a4b1fe1ef55aede58c5dee6ba9d7680a..32fa9759d74e5016bac11abc8e48d75a8ecec58e 100644 (file)
@@ -234,6 +234,7 @@ int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars);
 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant);
 #define NewConstStrBuf(a) _NewConstStrBuf(a, sizeof(a))
 void FreeStrBuf (StrBuf **FreeMe);
+char *SmashStrBuf (StrBuf **SmashMe);
 void HFreeStrBuf (void *VFreeMe);
 int FlushStrBuf(StrBuf *buf);
 int FLUSHStrBuf(StrBuf *buf); /* expensive but doesn't leave content behind for others to find in case of errors */
index a7beafe5cced7d0c8311d2b919d67e9880d9ea07..6afbb16626a68362244f3fa28908c7e17b2aa8c1 100644 (file)
@@ -411,6 +411,55 @@ void FreeStrBuf (StrBuf **FreeMe)
        *FreeMe = NULL;
 }
 
+/**
+ * \brief 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 Calle then owns the buffer and is responsible for freeing it.
+ * \param SmashMe Pointer Pointer to the buffer to release Buf from and free
+ * \return the pointer of the buffer; Callee owns the memory thereafter.
+ */
+char *SmashStrBuf (StrBuf **SmashMe)
+{
+       char *Ret;
+
+       if (*SmashMe == NULL)
+               return NULL;
+#ifdef SIZE_DEBUG
+       if (hFreeDbglog == -1){
+               pid_t pid = getpid();
+               char path [SIZ];
+               snprintf(path, SIZ, "/tmp/libcitadel_strbuf_realloc.log.%d", pid);
+               hFreeDbglog = open(path, O_APPEND|O_CREAT|O_WRONLY);
+       }
+       if ((*SmashMe)->nIncreases > 0)
+       {
+               char buf[SIZ * 3];
+               long n;
+               n = snprintf(buf, SIZ * 3, "S+|%ld|%ld|%ld|%s|%s|\n",
+                            (*SmashMe)->nIncreases,
+                            (*SmashMe)->BufUsed,
+                            (*SmashMe)->BufSize,
+                            (*SmashMe)->bt,
+                            (*SmashMe)->bt_lastinc);
+               n = write(hFreeDbglog, buf, n);
+       }
+       else
+       {
+               char buf[128];
+               long n;
+               n = snprintf(buf, 128, "S_|0|%ld%ld|\n",
+                            (*SmashMe)->BufUsed,
+                            (*SmashMe)->BufSize);
+               n = write(hFreeDbglog, buf, n);
+       }
+#endif
+       Ret = (*SmashMe)->buf;
+       free(*SmashMe);
+       *SmashMe = NULL;
+       return Ret;
+}
+
 /**
  * \brief Release the buffer
  * If you want put your StrBuf into a Hash, use this as Destructor.
index 231dee0c97627bed519391961fd36d5f5c181b3f..6aa7e74a73a96e9353e9468097e3d393592b9eda 100644 (file)
@@ -208,6 +208,14 @@ static void TestNextTokenizer_One(void)
        FreeStrBuf(&Buf);
 }
 
+static void TestNextTokenizer_Sequence(void)
+{
+       StrBuf *Buf;
+        char *teststring = "40:24524,24662,24673,27869:27935,28393,28426,31247:31258,31731,31749,31761,31778,31782,31801:31803,31813,31904,31915,33708,33935,34619,34672,34720:34723,34766,34835,37594,38854,39235,39942,40030,40142,40520,40815,40907,41201,41578,41781,41954,42292,43110,43565,43801,43998,44180,44241,44295,44401,44561,44635,44798,44861,44946,45022,45137:45148,45166,45179,45707,47114,47141:47157,47194,47314,47349,47386,47489,47496,47534:47543,54460,54601,54637:54652";
+        Buf = NewStrBufPlain(teststring, -1);
+       NextTokenizerIterateBuf(Buf, 8);
+       FreeStrBuf(&Buf);
+}
 
 
 /*
@@ -275,6 +283,7 @@ static void AddStrBufSimlpeTests(void)
        pTest = CU_add_test(pGroup, "testNextTokenizer_Empty", TestNextTokenizer_Empty);
        pTest = CU_add_test(pGroup, "testNextTokenizer_TwoEmpty", TestNextTokenizer_TwoEmpty);
        pTest = CU_add_test(pGroup, "testNextTokenizer_One", TestNextTokenizer_One);
+       pTest = CU_add_test(pGroup, "testNextTokenizer_Sequence", TestNextTokenizer_Sequence);
 
 
 /*