third_party/inchi/INCHI_BASE/src/ichi_io.c
int inchi_ios_print( INCHI_IOSTREAM * ios, const char* lpszFormat, ... )
{
    int ret = 0, ret2 = 0;
    va_list argList;

    if (!ios)
    {
        return -1;
    }

    if (ios->type == INCHI_IOS_TYPE_STRING)
    {
        /* output to string buffer */
        int max_len;
        my_va_start( argList, lpszFormat );
        max_len = GetMaxPrintfLength( lpszFormat, argList );
        va_end( argList );
        if (max_len >= 0)
        {
            if (ios->s.nAllocatedLength - ios->s.nUsedLength <= max_len)
            {
                /* enlarge output string */
                int  nAddLength = inchi_max( INCHI_ADD_STR_LEN, max_len );
                char *new_str =
                    (char *) inchi_calloc( ios->s.nAllocatedLength + nAddLength, sizeof( char ) );
                if (new_str)
                {
                    if (ios->s.pStr)
                    {
                        if (ios->s.nUsedLength > 0)
                        {
                            memcpy( new_str, ios->s.pStr, sizeof( new_str[0] )* ios->s.nUsedLength );
                        }
                        inchi_free( ios->s.pStr );
                    }
                    ios->s.pStr = new_str;
                    ios->s.nAllocatedLength += nAddLength;
                }
                else
                {
                    return -1; /* failed */
                }
            }
            /* output */
            my_va_start( argList, lpszFormat );
            ret = vsprintf( ios->s.pStr + ios->s.nUsedLength, lpszFormat, argList );
            va_end( argList );
            if (ret >= 0)
            {
                ios->s.nUsedLength += ret;
            }
#ifdef TARGET_LIB_FOR_WINCHI
#if 0
            if (FWPRINT)
            {
                my_va_start( argList, lpszFormat );
                FWPRINT( lpszFormat, argList );
                va_end( argList );
            }
#endif
#endif
            return ret;
        }
        return -1;
    }

    else if (ios->type == INCHI_IOS_TYPE_FILE)
    {
        /* output to file */
        if (ios->f)
        {
            my_va_start( argList, lpszFormat );
            ret = vfprintf( ios->f, lpszFormat, argList );
            va_end( argList );
        }
        else
        {
            my_va_start( argList, lpszFormat );
            ret2 = vfprintf( stdout, lpszFormat, argList );
            va_end( argList );
        }
#ifdef TARGET_LIB_FOR_WINCHI
        if (FWPRINT)
        {
            my_va_start( argList, lpszFormat );
            FWPRINT( lpszFormat, argList );
            va_end( argList );
        }
#endif
        return ret ? ret : ret2;
    }

    /* no output */
    return 0;
}
/****************************************************************************
 General procedure for printing to INCHI_IOSTREAM
****************************************************************************/
General procedure for printing to INCHI_IOSTREAM
General procedure for printing to INCHI_IOSTREAM
int inchi_ios_print( INCHI_IOSTREAM * ios, const char* lpszFormat, ... )
{
    int ret = 0, ret2 = 0;
    va_list argList;
    if (!ios)
    {
        return -1;
    }
    if (ios->type == INCHI_IOS_TYPE_STRING)
    {
        int max_len;
        my_va_start( argList, lpszFormat );
        max_len = GetMaxPrintfLength( lpszFormat, argList );
        va_end( argList );
        if (max_len >= 0)
        {
            if (ios->s.nAllocatedLength - ios->s.nUsedLength <= max_len)
            {
                int  nAddLength = inchi_max( INCHI_ADD_STR_LEN, max_len );
                char *new_str =
                    (char *) inchi_calloc( ios->s.nAllocatedLength + nAddLength, sizeof( char ) );
                if (new_str)
                {
                    if (ios->s.pStr)
                    {
                        if (ios->s.nUsedLength > 0)
                        {
                            memcpy( new_str, ios->s.pStr, sizeof( new_str[0] )* ios->s.nUsedLength );
                        }
                        inchi_free( ios->s.pStr );
                    }
                    ios->s.pStr = new_str;
                    ios->s.nAllocatedLength += nAddLength;
                }
                else
                {
                    return -1; 
                }
            }
            my_va_start( argList, lpszFormat );
            ret = vsprintf( ios->s.pStr + ios->s.nUsedLength, lpszFormat, argList );
            va_end( argList );
            if (ret >= 0)
            {
                ios->s.nUsedLength += ret;
            }
#ifdef TARGET_LIB_FOR_WINCHI
#if 0
            if (FWPRINT)
            {
                my_va_start( argList, lpszFormat );
                FWPRINT( lpszFormat, argList );
                va_end( argList );
            }
#endif
#endif
            return ret;
        }
        return -1;
    }
    else if (ios->type == INCHI_IOS_TYPE_FILE)
    {
        if (ios->f)
        {
            my_va_start( argList, lpszFormat );
            ret = vfprintf( ios->f, lpszFormat, argList );
            va_end( argList );
        }
        else
        {
            my_va_start( argList, lpszFormat );
            ret2 = vfprintf( stdout, lpszFormat, argList );
            va_end( argList );
        }
#ifdef TARGET_LIB_FOR_WINCHI
        if (FWPRINT)
        {
            my_va_start( argList, lpszFormat );
            FWPRINT( lpszFormat, argList );
            va_end( argList );
        }
#endif
        return ret ? ret : ret2;
    }
    return 0;
}
int inchi_ios_print ( INCHI_IOSTREAM * ios , const char * lpszFormat , ... ) { int ret = 0 , ret2 = 0 ; va_list argList ; if ( ! ios ) { return -1 ; } if ( ios -> type == INCHI_IOS_TYPE_STRING ) { int max_len ; my_va_start ( argList , lpszFormat ) ; max_len = GetMaxPrintfLength ( lpszFormat , argList ) ; va_end ( argList ) ; if ( max_len >= 0 ) { if ( ios -> s . nAllocatedLength - ios -> s . nUsedLength <= max_len ) { int nAddLength = inchi_max ( INCHI_ADD_STR_LEN , max_len ) ; char * new_str = ( char * ) inchi_calloc ( ios -> s . nAllocatedLength + nAddLength , sizeof ( char ) ) ; if ( new_str ) { if ( ios -> s . pStr ) { if ( ios -> s . nUsedLength > 0 ) { memcpy ( new_str , ios -> s . pStr , sizeof ( new_str [ 0 ] ) * ios -> s . nUsedLength ) ; } inchi_free ( ios -> s . pStr ) ; } ios -> s . pStr = new_str ; ios -> s . nAllocatedLength += nAddLength ; } else { return -1 ; } } my_va_start ( argList , lpszFormat ) ; ret = vsprintf ( ios -> s . pStr + ios -> s . nUsedLength , lpszFormat , argList ) ; va_end ( argList ) ; if ( ret >= 0 ) { ios -> s . nUsedLength += ret ; } #ifdef TARGET_LIB_FOR_WINCHI #if 0 if ( FWPRINT ) { my_va_start ( argList , lpszFormat ) ; FWPRINT ( lpszFormat , argList ) ; va_end ( argList ) ; } #endif #endif return ret ; } return -1 ; } else if ( ios -> type == INCHI_IOS_TYPE_FILE ) { if ( ios -> f ) { my_va_start ( argList , lpszFormat ) ; ret = vfprintf ( ios -> f , lpszFormat , argList ) ; va_end ( argList ) ; } else { my_va_start ( argList , lpszFormat ) ; ret2 = vfprintf ( stdout , lpszFormat , argList ) ; va_end ( argList ) ; } #ifdef TARGET_LIB_FOR_WINCHI if ( FWPRINT ) { my_va_start ( argList , lpszFormat ) ; FWPRINT ( lpszFormat , argList ) ; va_end ( argList ) ; } #endif return ret ? ret : ret2 ; } return 0 ; }
General procedure for printing to INCHI_IOSTREAM
General procedure for printing to INCHI_IOSTREAM
/* output to string buffer */ /* enlarge output string */ /* failed */ /* output */ /* output to file */ /* no output */