From: Wilfried Göesgens Date: Thu, 10 Sep 2009 20:24:35 +0000 (+0000) Subject: * add StrBufSmash() so functions demanding a non-const char* can better cooperate. X-Git-Tag: v7.86~860 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=cf4f08dd3a634b7617a61c450d02e3cbde200a52;p=citadel.git * add StrBufSmash() so functions demanding a non-const char* can better cooperate. --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 92f76f07a..32fa9759d 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -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 */ diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index a7beafe5c..6afbb1662 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -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. diff --git a/libcitadel/tests/stringbuf_test.c b/libcitadel/tests/stringbuf_test.c index 231dee0c9..6aa7e74a7 100644 --- a/libcitadel/tests/stringbuf_test.c +++ b/libcitadel/tests/stringbuf_test.c @@ -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); /*