/* * Set the minimum allocated space and/or the maximum string length. * If length < current dsPtr->len truncates string, else sets minimum allocated space. * RETURNS: currently allocated space. */ int Jsi_DSSetLength(Jsi_DString *dsPtr, int length) { if (DSNotInit(dsPtr)) InitStr(dsPtr); if (length < 0) return dsPtr->spaceAvl; if (length >= JSI_MAX_ALLOC_BUF) { LogError("max alloc exceeded %d", length); length = JSI_MAX_ALLOC_BUF-1; } if (length >= dsPtr->spaceAvl) { int isStatic = (dsPtr->staticSpace == dsPtr->str); dsPtr->spaceAvl = length; if (isStatic == 0 || length >= dsPtr->staticSize) { char *newString = (char *) Jsi_Realloc((isStatic?NULL:dsPtr->str), (unsigned) (dsPtr->spaceAvl+1)); if (!newString) { LogError("malloc failed %d", dsPtr->spaceAvl+1); return -1; } dsPtr->str = newString; if (isStatic && dsPtr->len>0) memcpy(dsPtr->str, dsPtr->staticSpace, (size_t) (dsPtr->len+1)); } } if (length < dsPtr->len) { dsPtr->str[length] = 0; dsPtr->len = length; } return dsPtr->spaceAvl; }
/* * Returns the strdup of the string value and resets the DString in the same way as Jsi_DSFree. * This just avoids the user having to do an extra malloc/free when the DString was already malloced. * It is the responsibility of the caller to free the returned value. * RETURNS: previous string value malloced. */ char* Jsi_DSFreeDup(Jsi_DString *dsPtr) { char *cp; if (DSNotInit(dsPtr)) InitStr(dsPtr); if (dsPtr->staticSpace == dsPtr->str) { cp = Jsi_Strdup(dsPtr->str); Jsi_DSSetLength(dsPtr, 0); return cp; } cp = dsPtr->str; dsPtr->str = dsPtr->staticSpace; dsPtr->staticSpace[0] = 0; dsPtr->spaceAvl = dsPtr->staticSize; dsPtr->len = 0; dsPtr->str = NULL; return cp; }
/* * Calls Jsi_DSAppendLen for each string value argument, passing in -1 for the length. * Each string is assumed to be null terminated and the final argument must be a NULL. * RETURNS: The string starting at the first appended character. */ char * Jsi_DSAppend(Jsi_DString *dsPtr, const char *str, ...) { va_list argList; char *elem; if (DSNotInit(dsPtr)) InitStr(dsPtr); int len = dsPtr->len; if (!str) return dsPtr->str; Jsi_DSAppendLen(dsPtr, str, -1); va_start(argList, str); while ((elem = va_arg(argList, char *)) != NULL) { Jsi_DSAppendLen(dsPtr, elem, -1); } va_end(argList); return dsPtr->str+len; }
/* * Format output and append to the end of dsPtr. * RETURNS: The string starting at the first appended character. */ char * Jsi_DSPrintf(Jsi_DString *dsPtr, const char *fmt, ...) { va_list argList; #ifndef JSI_PRINTF_BUFSIZ #define JSI_PRINTF_BUFSIZ BUFSIZ #endif char buf[JSI_PRINTF_BUFSIZ], *bPtr = buf; int n, bsiz = sizeof(buf), needAppend = 1; if (DSNotInit(dsPtr)) InitStr(dsPtr); int len = dsPtr->len; char *send = dsPtr->str + len; uint avail = (dsPtr->spaceAvl - len); if (avail >= sizeof(buf)) { /* Format directly into string. */ bPtr = dsPtr->str+len; bsiz = avail; needAppend = 0; } va_start(argList, fmt); n = vsnprintf(bPtr, bsiz, fmt, argList); if (n<0 || (n+len)>JSI_MAX_ALLOC_BUF) { LogError("vsnprintf error: rc=%d, len=%d", n, len); va_end(argList); return send; } if (n >= bsiz) { int m = len+n+1; if (Jsi_DSSetLength(dsPtr, m) < m) { va_start(argList, fmt); return send; } m = vsnprintf(dsPtr->str+len, len+1, fmt, argList); if (m != n) { LogError("len mismatch: %d != %d", m, n); va_end(argList); return send; } } else if (needAppend) { Jsi_DSAppendLen(dsPtr, buf, n); } va_end(argList); return send; }
void PipeWrite(int fd[2],int len) { int nWirted; char text[MAXSIZE]; if(len < MAXSIZE) //safety check { InitStr(text,WRITE_NUM); } else { printf("PipeWrite Error : len is too long \n"); exit(1); } printf("Write Process : Wanna input %d characters \n",len); close(fd[0]); nWirted = write(fd[1],text,len); printf("Write Process : Wrote in %d characters ...\n",nWirted); }
/* * Append length bytes to the string. If length is less than 0, * the value of strlen is used. If required, the DString is realloced to * be large enough to contain bytes, plus an extra null byte that is added to the end. * RETURNS: The string starting at the first appended character. */ char *Jsi_DSAppendLen(Jsi_DString *dsPtr, const char *string, int length) { if (DSNotInit(dsPtr)) InitStr(dsPtr); int len = dsPtr->len; if (string) { if (length < 0) length = strlen(string); int nsiz = length + len+1; if (nsiz >= dsPtr->spaceAvl) { if (nsiz < dsPtr->spaceAvl*2) nsiz = dsPtr->spaceAvl*2; if (Jsi_DSSetLength(dsPtr, nsiz) < nsiz) return Jsi_DSValue(dsPtr); } char * dst = dsPtr->str + dsPtr->len; memcpy(dst, string, length); dst[length] = 0; dsPtr->len += length; } return dsPtr->str+len; }
Status ClearString(HString *str) { if (!str) return ERROR; free(str->ch); return InitStr(str); }
/* Initialize string. */ void Jsi_DSInit(Jsi_DString *dsPtr) { dsPtr->staticSize = 0; dsPtr->str = NULL; InitStr(dsPtr); }
/* RETURNS: string value. */ char* Jsi_DSValue(Jsi_DString *dsPtr) { if (DSNotInit(dsPtr)) InitStr(dsPtr); return dsPtr->str; }