+int SB_CompareStr (const StrBuf* S1, const char* S2)
+/* Do a lexical compare of S1 and S2. See strcmp for result codes. */
+{
+ int Result;
+ unsigned S2Len = strlen (S2);
+ if (S1->Len < S2Len) {
+ Result = memcmp (S1->Buf, S2, S1->Len);
+ if (Result == 0) {
+ /* S1 considered lesser because it's shorter */
+ Result = -1;
+ }
+ } else if (S1->Len > S2Len) {
+ Result = memcmp (S1->Buf, S2, S2Len);
+ if (Result == 0) {
+ /* S2 considered lesser because it's shorter */
+ Result = 1;
+ }
+ } else {
+ Result = memcmp (S1->Buf, S2, S1->Len);
+ }
+ return Result;
+}
+
+
+
+void SB_VPrintf (StrBuf* S, const char* Format, va_list ap)
+/* printf function with S as target. The function is safe, which means that
+ * the current contents of S are discarded, and are allocated again with
+ * a matching size for the output. The function will call FAIL when problems
+ * are detected (anything that let xsnprintf return -1).
+ */
+{
+ va_list tmp;
+ int SizeNeeded;
+
+ /* Since we must determine the space needed anyway, we will try with
+ * the currently allocated memory. If the call succeeds, we've saved
+ * an allocation. If not, we have to reallocate and try again.
+ */
+ va_copy (tmp, ap);
+ SizeNeeded = xvsnprintf (S->Buf, S->Allocated, Format, tmp);
+ va_end (tmp);
+
+ /* Check the result, the xvsnprintf function should not fail */
+ CHECK (SizeNeeded >= 0);
+
+ /* Check if we must reallocate */
+ if ((unsigned) SizeNeeded >= S->Allocated) {
+ /* Must retry. Use CheapRealloc to avoid copying */
+ SB_CheapRealloc (S, SizeNeeded + 1); /* Account for '\0' */
+ (void) xvsnprintf (S->Buf, S->Allocated, Format, ap);
+ }
+
+ /* Update string buffer variables */
+ S->Len = SizeNeeded;
+ S->Index = 0;
+}
+
+
+
+void SB_Printf (StrBuf* S, const char* Format, ...)
+/* vprintf function with S as target. The function is safe, which means that
+ * the current contents of S are discarded, and are allocated again with
+ * a matching size for the output. The function will call FAIL when problems
+ * are detected (anything that let xsnprintf return -1).
+ */
+{
+ va_list ap;
+ va_start (ap, Format);
+ SB_VPrintf (S, Format, ap);
+ va_end (ap);
+}
+
+
+