String strVPrintf(const char *fmt, va_list argp) { Buffer b = bufNew(); bufVPrintf(b, fmt, argp); return bufLiberate(b); }
static CCommand ccCompleteCommand() { String s = bufLiberate(ccBuf); ccBuf = NULL; return s; }
local String arRdItemArch(Archive ar) { String name = arRdItemArch0(ar); ULong idx = 0L; Buffer buf; String ptr; if (name[0] != '\0') return name; if (name[1] == '/') { /* Do we ever get more than one of these? */ arReadNameTable(ar); /* Just go round again... */ return arRdItemArch(ar); } /* Indirect name: character offset into the name table. */ (void)sscanf(&name[1], "%8lu ", &idx); /* Debugging */ arDEBUG(dbOut, ">>> Reading offset #%lu (out of %lu)\n", idx, (ULong)strLength(ar->names)); /* Start a new character buffer */ buf = bufNew(); /* Jump to the correct place in the buffer */ ptr = ar->names + idx; /* Scan characters up until a / or newline */ while ((*ptr != '\n') && (*ptr != '/') && *ptr) bufPutc(buf, *ptr++); /* Convert the buffer into a text string */ name = bufLiberate(buf); /* Debugging */ arDEBUG(dbOut, ">>> [%s]\n", name); /* Return the name from the directory table */ return name; }
String strPrintf(const char *fmt, ...) { va_list argp; Buffer buf = bufNew(); va_start(argp, fmt); bufVPrintf(buf, fmt, argp); va_end(argp); return bufLiberate(buf); }
/* * Given the list fnames [z,...,b,a] and punct "->", * allocate the string "'a'->'b'->...->'z'". */ local String inclActiveFileChain(StringList fnames, String punct) { StringList sl0, sl; Buffer buf; String s; int i; buf = bufNew(); sl0 = listReverse(String)(fnames); for (sl = sl0, i = 0; sl; sl = cdr(sl), i++) { if (i > 0) bufPuts(buf, punct); bufPrintf(buf, "'%s'", car(sl)); } s = bufLiberate(buf); listFree(String)(sl0); return s; }
String strReplace(String txt, String orig, String repl) { Buffer buf = bufNew(); String s; int replLen; bufNeed(buf, strlen(orig)); replLen = strlen(repl); while (true) { char *nxt = strstr(txt, orig); if (nxt == NULL) break; bufAddn(buf, txt, nxt-txt); bufAddn(buf, repl, replLen); txt = nxt; txt += strlen(orig); } bufAddn(buf, txt, strlen(txt)); bufAdd1(buf, '\0'); s = bufLiberate(buf); return s; }