third_party/inchi/INCHI_BASE/src/mol_fmt3.c
int MolfileV3000ReadField( void* data,
                           int data_type,
                           char** line_ptr )
{
    int  nread = 0;
    char field[MOL_FMT_V3000_MAXFIELDLEN];
    const int max_field_len = sizeof( field );
    long ldata = 0L;
    double ddata = 0.0;
    char *p_end;

    memset( field, 0, max_field_len );

    nread = read_upto_delim( line_ptr, field, max_field_len, " \t\n\v\f\r" );

    switch (data_type)
    {
        case MOL_FMT_STRING_DATA:
        {
            if (nread)
            {
                mystrncpy( (char *) data, field, nread + 1 );
            }
            else
            {
                ( (char *) data )[0] = '\0';
            }
        }
        break;

        case MOL_FMT_CHAR_INT_DATA:
        case MOL_FMT_SHORT_INT_DATA:
        case MOL_FMT_LONG_INT_DATA:
        case MOL_FMT_INT_DATA:
        {
            /* assume that field ends at first non-digit */
            ldata = strtol( field, &p_end, 10 );

            if (p_end == field)
            {
                nread = 0;
            }

            if (data_type == MOL_FMT_LONG_INT_DATA)
            {
                if (LONG_MIN < ldata && ldata < LONG_MAX)
                {
                    *(long*) data = (long) ldata;
                }
                else
                {
                    *(long*) data = 0L;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_INT_DATA)
            {
                if (INT_MIN <= ldata  && ldata <= INT_MAX)
                {
                    *(int*) data = (int) ldata;
                }
                else
                {
                    *(int*) data = (int) 0;
                    nread = -1;
                }
            }

            else if (data_type == MOL_FMT_CHAR_INT_DATA)
            {
                if (SCHAR_MIN <= ldata  && ldata <= SCHAR_MAX)
                {
                    *(S_CHAR*) data = (S_CHAR) ldata;
                }
                else
                {
                    *(S_CHAR*) data = (S_CHAR) 0;
                    nread = -1;
                }
            }

            else if (data_type == MOL_FMT_SHORT_INT_DATA)
            {
                if (SHRT_MIN <= ldata && ldata <= SHRT_MAX)
                {
                    *(S_SHORT*) data = (S_SHORT) ldata;
                }
                else
                {
                    *(S_SHORT*) data = (S_SHORT) 0;
                    nread = -1;
                }
            }
            else
            {
                nread = -1;
            }
        }
        break;    /* INT's */


        case MOL_FMT_DOUBLE_DATA:
        case MOL_FMT_FLOAT_DATA:
        {
            /* assume that field ends at first non-digit */
            ddata = strtod( field, &p_end );

            if (p_end == field)
            {
                nread = 0;
            }

            if (data_type == MOL_FMT_DOUBLE_DATA)
            {
                if (ddata != HUGE_VAL && /*ldata*/ ddata != -HUGE_VAL)
                {
                    *(double*) data = ddata;
                }
                else
                {
                    *(double*) data = 0.0;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_FLOAT_DATA)
            {
                if (fabs( ddata ) <= (double) FLT_MIN)
                {
                    *(float*) data = 0.0;
                }
                else if (fabs( ddata ) >= (double) FLT_MAX)
                {
                    *(float*) data = 0.0;
                    nread = -1;
                }
            }
            else
            {
                *(float*) data = (float) ddata;
            }
        }
        break; /* REAL's */

        default:
            nread = -1;
    }

    return nread;
}
/****************************************************************************
    Read V3000 field.

    It is MolfileReadField updated for V3000.
    It considers right whitespace as stop sign,
    no predefined len is used.

    Returns -1 on error otherwise number of bytes read.

    NB: ASSUMES THAT STRING HAS BEEN NORMALIZED with normalize_string()

    TODO: treat strings with spaces in double quotes
****************************************************************************/
Read V3000 field.
It is MolfileReadField updated for V3000.
It considers right whitespace as stop sign,
no predefined len is used.

1 on error otherwise number of bytes read.

ASSUMES THAT STRING HAS BEEN NORMALIZED with normalize_string()

treat strings with spaces in double quotes
Read V3000 field . It is MolfileReadField updated for V3000 . It considers right whitespace as stop sign no predefined len is used . 1 on error otherwise number of bytes read . ASSUMES THAT STRING HAS BEEN NORMALIZED with normalize_string () treat strings with spaces in double quotes
int MolfileV3000ReadField( void* data,
                           int data_type,
                           char** line_ptr )
{
    int  nread = 0;
    char field[MOL_FMT_V3000_MAXFIELDLEN];
    const int max_field_len = sizeof( field );
    long ldata = 0L;
    double ddata = 0.0;
    char *p_end;
    memset( field, 0, max_field_len );
    nread = read_upto_delim( line_ptr, field, max_field_len, " \t\n\v\f\r" );
    switch (data_type)
    {
        case MOL_FMT_STRING_DATA:
        {
            if (nread)
            {
                mystrncpy( (char *) data, field, nread + 1 );
            }
            else
            {
                ( (char *) data )[0] = '\0';
            }
        }
        break;
        case MOL_FMT_CHAR_INT_DATA:
        case MOL_FMT_SHORT_INT_DATA:
        case MOL_FMT_LONG_INT_DATA:
        case MOL_FMT_INT_DATA:
        {
            ldata = strtol( field, &p_end, 10 );
            if (p_end == field)
            {
                nread = 0;
            }
            if (data_type == MOL_FMT_LONG_INT_DATA)
            {
                if (LONG_MIN < ldata && ldata < LONG_MAX)
                {
                    *(long*) data = (long) ldata;
                }
                else
                {
                    *(long*) data = 0L;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_INT_DATA)
            {
                if (INT_MIN <= ldata  && ldata <= INT_MAX)
                {
                    *(int*) data = (int) ldata;
                }
                else
                {
                    *(int*) data = (int) 0;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_CHAR_INT_DATA)
            {
                if (SCHAR_MIN <= ldata  && ldata <= SCHAR_MAX)
                {
                    *(S_CHAR*) data = (S_CHAR) ldata;
                }
                else
                {
                    *(S_CHAR*) data = (S_CHAR) 0;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_SHORT_INT_DATA)
            {
                if (SHRT_MIN <= ldata && ldata <= SHRT_MAX)
                {
                    *(S_SHORT*) data = (S_SHORT) ldata;
                }
                else
                {
                    *(S_SHORT*) data = (S_SHORT) 0;
                    nread = -1;
                }
            }
            else
            {
                nread = -1;
            }
        }
        break;    
        case MOL_FMT_DOUBLE_DATA:
        case MOL_FMT_FLOAT_DATA:
        {
            ddata = strtod( field, &p_end );
            if (p_end == field)
            {
                nread = 0;
            }
            if (data_type == MOL_FMT_DOUBLE_DATA)
            {
                if (ddata != HUGE_VAL &&  ddata != -HUGE_VAL)
                {
                    *(double*) data = ddata;
                }
                else
                {
                    *(double*) data = 0.0;
                    nread = -1;
                }
            }
            else if (data_type == MOL_FMT_FLOAT_DATA)
            {
                if (fabs( ddata ) <= (double) FLT_MIN)
                {
                    *(float*) data = 0.0;
                }
                else if (fabs( ddata ) >= (double) FLT_MAX)
                {
                    *(float*) data = 0.0;
                    nread = -1;
                }
            }
            else
            {
                *(float*) data = (float) ddata;
            }
        }
        break; 
        default:
            nread = -1;
    }
    return nread;
}
int MolfileV3000ReadField ( void * data , int data_type , char * * line_ptr ) { int nread = 0 ; char field [ MOL_FMT_V3000_MAXFIELDLEN ] ; const int max_field_len = sizeof ( field ) ; long ldata = 0L ; double ddata = 0.0 ; char * p_end ; memset ( field , 0 , max_field_len ) ; nread = read_upto_delim ( line_ptr , field , max_field_len , " \t \n \v \f \r " ) ; switch ( data_type ) { case MOL_FMT_STRING_DATA : { if ( nread ) { mystrncpy ( ( char * ) data , field , nread + 1 ) ; } else { ( ( char * ) data ) [ 0 ] = ' \0 ' ; } } break ; case MOL_FMT_CHAR_INT_DATA : case MOL_FMT_SHORT_INT_DATA : case MOL_FMT_LONG_INT_DATA : case MOL_FMT_INT_DATA : { ldata = strtol ( field , & p_end , 10 ) ; if ( p_end == field ) { nread = 0 ; } if ( data_type == MOL_FMT_LONG_INT_DATA ) { if ( LONG_MIN < ldata && ldata < LONG_MAX ) { * ( long * ) data = ( long ) ldata ; } else { * ( long * ) data = 0L ; nread = -1 ; } } else if ( data_type == MOL_FMT_INT_DATA ) { if ( INT_MIN <= ldata && ldata <= INT_MAX ) { * ( int * ) data = ( int ) ldata ; } else { * ( int * ) data = ( int ) 0 ; nread = -1 ; } } else if ( data_type == MOL_FMT_CHAR_INT_DATA ) { if ( SCHAR_MIN <= ldata && ldata <= SCHAR_MAX ) { * ( S_CHAR * ) data = ( S_CHAR ) ldata ; } else { * ( S_CHAR * ) data = ( S_CHAR ) 0 ; nread = -1 ; } } else if ( data_type == MOL_FMT_SHORT_INT_DATA ) { if ( SHRT_MIN <= ldata && ldata <= SHRT_MAX ) { * ( S_SHORT * ) data = ( S_SHORT ) ldata ; } else { * ( S_SHORT * ) data = ( S_SHORT ) 0 ; nread = -1 ; } } else { nread = -1 ; } } break ; case MOL_FMT_DOUBLE_DATA : case MOL_FMT_FLOAT_DATA : { ddata = strtod ( field , & p_end ) ; if ( p_end == field ) { nread = 0 ; } if ( data_type == MOL_FMT_DOUBLE_DATA ) { if ( ddata != HUGE_VAL && ddata != - HUGE_VAL ) { * ( double * ) data = ddata ; } else { * ( double * ) data = 0.0 ; nread = -1 ; } } else if ( data_type == MOL_FMT_FLOAT_DATA ) { if ( fabs ( ddata ) <= ( double ) FLT_MIN ) { * ( float * ) data = 0.0 ; } else if ( fabs ( ddata ) >= ( double ) FLT_MAX ) { * ( float * ) data = 0.0 ; nread = -1 ; } } else { * ( float * ) data = ( float ) ddata ; } } break ; default : nread = -1 ; } return nread ; }
Read V3000 field.
Read V3000 field .
/* assume that field ends at first non-digit */ /* INT's */ /* assume that field ends at first non-digit */ /*ldata*/ /* REAL's */