コード例 #1
0
ファイル: asprintf.c プロジェクト: DastanIqbal/FreeSWITCH
static __inline__ void
simpleVasprintf(char **      const retvalP,
                const char * const fmt,
                va_list            varargs) {
/*----------------------------------------------------------------------------
   This is a poor man's implementation of vasprintf(), of GNU fame.
-----------------------------------------------------------------------------*/
    char * result;
    size_t bufferSize;
    bool outOfMemory;

    for (result = NULL, bufferSize = 4096, outOfMemory = false;
         !result && !outOfMemory;
        ) {

        result = malloc(bufferSize);
        if (!result)
            outOfMemory = true;
        else {
            size_t bytesNeeded;
            newVsnprintf(result, bufferSize, fmt, varargs, &bytesNeeded);
            if (bytesNeeded > bufferSize) {
                free(result);
                result = NULL;
                bufferSize = bytesNeeded;
            }
        }
    }
    *retvalP = result;
}
コード例 #2
0
ファイル: casprintf.c プロジェクト: BlackPearl01/quick-box
static __inline__ int
simpleVasprintf(char **      const resultP,
                const char * const fmt,
                va_list            varargs) {
/*----------------------------------------------------------------------------
   This is a poor man's implementation of vasprintf(), of GNU fame.
-----------------------------------------------------------------------------*/
    int retval;
    char * buffer;
    size_t bufferSize;
    bool outOfMemory;

    for (buffer = NULL, bufferSize = 4096, outOfMemory = false;
         !buffer && !outOfMemory;
        ) {

        buffer = malloc(bufferSize);
        if (!buffer)
            outOfMemory = true;
        else {
            size_t bytesNeeded;
            newVsnprintf(buffer, bufferSize, fmt, varargs, &bytesNeeded);
            if (bytesNeeded > bufferSize) {
                free(buffer);
                buffer = NULL;
                bufferSize = bytesNeeded;
            }
        }
    }
    if (outOfMemory)
        retval = -1;
    else {
        retval = strlen(buffer);
        *resultP = buffer;
    }
    return retval;
}