void dyStringVaPrintf(struct dyString *ds, char *format, va_list args) /* VarArgs Printf to end of dyString. */ { /* attempt to format the string in the current space. If there * is not enough room, increase the buffer size and try again */ int avail, sz; while (TRUE) { va_list argscp; va_copy(argscp, args); avail = ds->bufSize - ds->stringSize; if (avail <= 0) { /* Don't pass zero sized buffers to vsnprintf, because who knows * if the library function will handle it. */ dyStringExpandBuf(ds, ds->bufSize+ds->bufSize); avail = ds->bufSize - ds->stringSize; } sz = vsnprintf(ds->string + ds->stringSize, avail, format, argscp); va_end(argscp); /* note that some version return -1 if too small */ if ((sz < 0) || (sz >= avail)) dyStringExpandBuf(ds, ds->bufSize+ds->bufSize); else { ds->stringSize += sz; break; } } }
char dyStringAppendC(struct dyString *ds, char c) /* Append char to end of string. */ { char *s; if (ds->stringSize >= ds->bufSize) dyStringExpandBuf(ds, ds->bufSize+256); s = ds->string + ds->stringSize++; *s++ = c; *s = 0; return c; }
void dyStringResize(struct dyString *ds, int newSize) /* resize a string, if the string expands, blanks are appended */ { int oldSize = ds->stringSize; if (newSize > oldSize) { /* grow */ if (newSize > ds->bufSize) dyStringExpandBuf(ds, newSize + ds->stringSize); memset(ds->string+newSize, ' ', newSize); } ds->string[newSize] = '\0'; ds->stringSize = newSize; }
void dyStringAppendMultiC(struct dyString *ds, char c, int n) /* Append N copies of char to end of string. */ { int oldSize = ds->stringSize; int newSize = oldSize + n; int newAllocSize = newSize + oldSize; char *buf; if (newSize > ds->bufSize) dyStringExpandBuf(ds,newAllocSize); buf = ds->string; memset(buf+oldSize, c, n); ds->stringSize = newSize; buf[newSize] = 0; }
void dyStringAppendN(struct dyString *ds, char *string, int stringSize) /* Append string of given size to end of string. */ { int oldSize = ds->stringSize; int newSize = oldSize + stringSize; char *buf; if (newSize > ds->bufSize) { int newAllocSize = newSize + oldSize; dyStringExpandBuf(ds,newAllocSize); } buf = ds->string; memcpy(buf+oldSize, string, stringSize); ds->stringSize = newSize; buf[newSize] = 0; }
void dyStringBumpBufSize(struct dyString *ds, int size) /* Force dyString buffer to be at least given size. */ { if (ds->bufSize < size) dyStringExpandBuf(ds, size); }