third_party/inchi/INCHI_BASE/src/ichiread.c
int AddAuxSegmentCoord( int         nRet,
                        XYZ_COORD   *pXYZ,
                        int         nLenXYZ,
                        INChI       *pInpInChI[INCHI_NUM][TAUT_NUM],
                        int         nNumComponents[INCHI_NUM][TAUT_NUM] )
{
    int iINChI, j, k, n, m, numAt[TAUT_NUM], num_at, nNumMissingNumbers = 0, ret = 0;
    INChI *pInChI = NULL;
    INChI *pAltInChI = NULL;
    XYZ_COORD *pxyz;

    /* Propagate numberings (original:canonical atom mapping)                                                                           */
    /* NB: we already saved isotopic numbering, if any, into the first nNumberOfAtoms elements of INChI::nPossibleLocationsOfIsotopicH, */
    /* and the non-isotopic numbering into the second half of nNumberOfAtoms elements of that INChI::nPossibleLocationsOfIsotopicH      */

    for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
    {
        for (j = TAUT_YES; TAUT_NON <= j; j--)   /* for FixedH and MobileH ... */
        {
            for (k = 0; k < nNumComponents[iINChI][j]; k++)  /* for each component ... */
            {
                int   jj = ALT_TAUT( j );
                pInChI = pInpInChI[iINChI][j] + k;
                pAltInChI = ( k < nNumComponents[iINChI][jj] ) ? pInpInChI[iINChI][jj] + k : NULL;
                numAt[j] = ( !pInChI->bDeleted ) ? pInChI->nNumberOfAtoms : 0;
                numAt[jj] = ( pAltInChI && !pAltInChI->bDeleted ) ? pAltInChI->nNumberOfAtoms : 0;
                switch (j)
                {
                    case TAUT_YES:
                        if (!numAt[j])
                        {
                            break; /* component does not exist */
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH)
                        {
                            nNumMissingNumbers++;
                            break;
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH[0])
                        {
                            if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
                            {
                                /* copy from non-isotopic (2nd half of the at. numbers array) to the isotopic (1st half) */
                                ret = CopyAtomNumbers( pInChI, 1, pInChI, 0 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                            {
                                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                                pInChI->nPossibleLocationsOfIsotopicH = NULL;
                                nNumMissingNumbers++;
                            }
                        }
                        break;

                    case TAUT_NON:
                        if (!numAt[j])
                        {
                            break; /* component does not exist */
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH)
                        {
                            /* trying to get numbers from Mobile-H component */
                            if (!numAt[jj] || !( pAltInChI->nPossibleLocationsOfIsotopicH ))
                            {
                                nNumMissingNumbers++;
                                break;
                            }
                            if (pAltInChI->nPossibleLocationsOfIsotopicH[0])
                            {
                                ret = CopyAtomNumbers( pInChI, 1, pAltInChI, 1 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                                if (pAltInChI->nPossibleLocationsOfIsotopicH[numAt[jj]])
                                {
                                    ret = CopyAtomNumbers( pInChI, 1, pAltInChI, 0 );
                                    if (ret < 0)
                                    {
                                        goto exit_function;
                                    }
                                }
                                else
                                {
                                    /* pAltInChI->nPossibleLocationsOfIsotopicH should have */
                                    /* been deallocated on previous TAUT_YES pass           */
                                    ret = RI_ERR_PROGR;
                                    goto exit_function;
                                }
                        }
                        else if (!pInChI->nPossibleLocationsOfIsotopicH[0])
                        {
                            if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
                            {
                                /* copy from non-isotopic to isotopic */
                                ret = CopyAtomNumbers( pInChI, 1, pInChI, 0 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                            {
                                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                                pInChI->nPossibleLocationsOfIsotopicH = NULL;
                                nNumMissingNumbers++;
                            }
                        }
                        break;
                }
            }
        }
    }

    /* Add coordinates */
    for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
    {
        for (j = 0; j < TAUT_NUM; j++)
        {
            for (k = 0; k < nNumComponents[iINChI][j]; k++)
            {
                pInChI = pInpInChI[iINChI][j] + k;
                num_at = ( !pInChI->bDeleted ) ? pInChI->nNumberOfAtoms : 0;
                if (!num_at)
                {
                    if (pInChI->nPossibleLocationsOfIsotopicH)
                    {
                        inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                        pInChI->nPossibleLocationsOfIsotopicH = NULL;
                    }
                    continue;
                }
                if (!pInChI->nPossibleLocationsOfIsotopicH)
                {
                    continue;
                }
                if (iINChI == INCHI_BAS && num_at == 1 &&
                     pInChI->szHillFormula && !strcmp( pInChI->szHillFormula, "H" ) &&
                     (int) pInChI->nPossibleLocationsOfIsotopicH[0] - 1 >= nLenXYZ)
                {
                    ; /* a single atom H disconnected from a metal atom has no coordinates */
                }
                else
                {
                    /* add atom coordinates */
                    pxyz = (XYZ_COORD *) inchi_calloc( num_at, sizeof( pxyz[0] ) );
                    if (!pxyz)
                    {
                        ret = RI_ERR_ALLOC;
                        goto exit_function;
                    }
                    for (n = 0; n < num_at; n++)
                    {
                        m = (int) pInChI->nPossibleLocationsOfIsotopicH[n] - 1;
                        if (m < 0 || m >= nLenXYZ)
                        {
                            inchi_free( pxyz );
                            ret = RI_ERR_SYNTAX;
                            goto exit_function;
                        }
                        pxyz[n] = pXYZ[m];
                    }
                    pInChI->IsotopicTGroup = (INChI_IsotopicTGroup *) pxyz;
                }
                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                pInChI->nPossibleLocationsOfIsotopicH = NULL;
            }
        }
    }
    ret = nRet; /* normal exit */

exit_function:

    return ret;
}
/****************************************************************************
Parse and save atom coordinates from AuxInfo
****************************************************************************/
Parse and save atom coordinates from AuxInfo
Parse and save atom coordinates from AuxInfo
int AddAuxSegmentCoord( int         nRet,
                        XYZ_COORD   *pXYZ,
                        int         nLenXYZ,
                        INChI       *pInpInChI[INCHI_NUM][TAUT_NUM],
                        int         nNumComponents[INCHI_NUM][TAUT_NUM] )
{
    int iINChI, j, k, n, m, numAt[TAUT_NUM], num_at, nNumMissingNumbers = 0, ret = 0;
    INChI *pInChI = NULL;
    INChI *pAltInChI = NULL;
    XYZ_COORD *pxyz;
    for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
    {
        for (j = TAUT_YES; TAUT_NON <= j; j--)   
        {
            for (k = 0; k < nNumComponents[iINChI][j]; k++)  
            {
                int   jj = ALT_TAUT( j );
                pInChI = pInpInChI[iINChI][j] + k;
                pAltInChI = ( k < nNumComponents[iINChI][jj] ) ? pInpInChI[iINChI][jj] + k : NULL;
                numAt[j] = ( !pInChI->bDeleted ) ? pInChI->nNumberOfAtoms : 0;
                numAt[jj] = ( pAltInChI && !pAltInChI->bDeleted ) ? pAltInChI->nNumberOfAtoms : 0;
                switch (j)
                {
                    case TAUT_YES:
                        if (!numAt[j])
                        {
                            break; 
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH)
                        {
                            nNumMissingNumbers++;
                            break;
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH[0])
                        {
                            if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
                            {
                                ret = CopyAtomNumbers( pInChI, 1, pInChI, 0 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                            {
                                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                                pInChI->nPossibleLocationsOfIsotopicH = NULL;
                                nNumMissingNumbers++;
                            }
                        }
                        break;
                    case TAUT_NON:
                        if (!numAt[j])
                        {
                            break; 
                        }
                        if (!pInChI->nPossibleLocationsOfIsotopicH)
                        {
                            if (!numAt[jj] || !( pAltInChI->nPossibleLocationsOfIsotopicH ))
                            {
                                nNumMissingNumbers++;
                                break;
                            }
                            if (pAltInChI->nPossibleLocationsOfIsotopicH[0])
                            {
                                ret = CopyAtomNumbers( pInChI, 1, pAltInChI, 1 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                                if (pAltInChI->nPossibleLocationsOfIsotopicH[numAt[jj]])
                                {
                                    ret = CopyAtomNumbers( pInChI, 1, pAltInChI, 0 );
                                    if (ret < 0)
                                    {
                                        goto exit_function;
                                    }
                                }
                                else
                                {
                                    ret = RI_ERR_PROGR;
                                    goto exit_function;
                                }
                        }
                        else if (!pInChI->nPossibleLocationsOfIsotopicH[0])
                        {
                            if (pInChI->nPossibleLocationsOfIsotopicH[numAt[j]])
                            {
                                ret = CopyAtomNumbers( pInChI, 1, pInChI, 0 );
                                if (ret < 0)
                                {
                                    goto exit_function;
                                }
                            }
                            else
                            {
                                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                                pInChI->nPossibleLocationsOfIsotopicH = NULL;
                                nNumMissingNumbers++;
                            }
                        }
                        break;
                }
            }
        }
    }
    for (iINChI = 0; iINChI < INCHI_NUM; iINChI++)
    {
        for (j = 0; j < TAUT_NUM; j++)
        {
            for (k = 0; k < nNumComponents[iINChI][j]; k++)
            {
                pInChI = pInpInChI[iINChI][j] + k;
                num_at = ( !pInChI->bDeleted ) ? pInChI->nNumberOfAtoms : 0;
                if (!num_at)
                {
                    if (pInChI->nPossibleLocationsOfIsotopicH)
                    {
                        inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                        pInChI->nPossibleLocationsOfIsotopicH = NULL;
                    }
                    continue;
                }
                if (!pInChI->nPossibleLocationsOfIsotopicH)
                {
                    continue;
                }
                if (iINChI == INCHI_BAS && num_at == 1 &&
                     pInChI->szHillFormula && !strcmp( pInChI->szHillFormula, "H" ) &&
                     (int) pInChI->nPossibleLocationsOfIsotopicH[0] - 1 >= nLenXYZ)
                {
                    ; 
                }
                else
                {
                    pxyz = (XYZ_COORD *) inchi_calloc( num_at, sizeof( pxyz[0] ) );
                    if (!pxyz)
                    {
                        ret = RI_ERR_ALLOC;
                        goto exit_function;
                    }
                    for (n = 0; n < num_at; n++)
                    {
                        m = (int) pInChI->nPossibleLocationsOfIsotopicH[n] - 1;
                        if (m < 0 || m >= nLenXYZ)
                        {
                            inchi_free( pxyz );
                            ret = RI_ERR_SYNTAX;
                            goto exit_function;
                        }
                        pxyz[n] = pXYZ[m];
                    }
                    pInChI->IsotopicTGroup = (INChI_IsotopicTGroup *) pxyz;
                }
                inchi_free( pInChI->nPossibleLocationsOfIsotopicH );
                pInChI->nPossibleLocationsOfIsotopicH = NULL;
            }
        }
    }
    ret = nRet; 
exit_function:
    return ret;
}
int AddAuxSegmentCoord ( int nRet , XYZ_COORD * pXYZ , int nLenXYZ , INChI * pInpInChI [ INCHI_NUM ] [ TAUT_NUM ] , int nNumComponents [ INCHI_NUM ] [ TAUT_NUM ] ) { int iINChI , j , k , n , m , numAt [ TAUT_NUM ] , num_at , nNumMissingNumbers = 0 , ret = 0 ; INChI * pInChI = NULL ; INChI * pAltInChI = NULL ; XYZ_COORD * pxyz ; for ( iINChI = 0 ; iINChI < INCHI_NUM ; iINChI ++ ) { for ( j = TAUT_YES ; TAUT_NON <= j ; j -- ) { for ( k = 0 ; k < nNumComponents [ iINChI ] [ j ] ; k ++ ) { int jj = ALT_TAUT ( j ) ; pInChI = pInpInChI [ iINChI ] [ j ] + k ; pAltInChI = ( k < nNumComponents [ iINChI ] [ jj ] ) ? pInpInChI [ iINChI ] [ jj ] + k : NULL ; numAt [ j ] = ( ! pInChI -> bDeleted ) ? pInChI -> nNumberOfAtoms : 0 ; numAt [ jj ] = ( pAltInChI && ! pAltInChI -> bDeleted ) ? pAltInChI -> nNumberOfAtoms : 0 ; switch ( j ) { case TAUT_YES : if ( ! numAt [ j ] ) { break ; } if ( ! pInChI -> nPossibleLocationsOfIsotopicH ) { nNumMissingNumbers ++ ; break ; } if ( ! pInChI -> nPossibleLocationsOfIsotopicH [ 0 ] ) { if ( pInChI -> nPossibleLocationsOfIsotopicH [ numAt [ j ] ] ) { ret = CopyAtomNumbers ( pInChI , 1 , pInChI , 0 ) ; if ( ret < 0 ) { goto exit_function ; } } else { inchi_free ( pInChI -> nPossibleLocationsOfIsotopicH ) ; pInChI -> nPossibleLocationsOfIsotopicH = NULL ; nNumMissingNumbers ++ ; } } break ; case TAUT_NON : if ( ! numAt [ j ] ) { break ; } if ( ! pInChI -> nPossibleLocationsOfIsotopicH ) { if ( ! numAt [ jj ] || ! ( pAltInChI -> nPossibleLocationsOfIsotopicH ) ) { nNumMissingNumbers ++ ; break ; } if ( pAltInChI -> nPossibleLocationsOfIsotopicH [ 0 ] ) { ret = CopyAtomNumbers ( pInChI , 1 , pAltInChI , 1 ) ; if ( ret < 0 ) { goto exit_function ; } } else if ( pAltInChI -> nPossibleLocationsOfIsotopicH [ numAt [ jj ] ] ) { ret = CopyAtomNumbers ( pInChI , 1 , pAltInChI , 0 ) ; if ( ret < 0 ) { goto exit_function ; } } else { ret = RI_ERR_PROGR ; goto exit_function ; } } else if ( ! pInChI -> nPossibleLocationsOfIsotopicH [ 0 ] ) { if ( pInChI -> nPossibleLocationsOfIsotopicH [ numAt [ j ] ] ) { ret = CopyAtomNumbers ( pInChI , 1 , pInChI , 0 ) ; if ( ret < 0 ) { goto exit_function ; } } else { inchi_free ( pInChI -> nPossibleLocationsOfIsotopicH ) ; pInChI -> nPossibleLocationsOfIsotopicH = NULL ; nNumMissingNumbers ++ ; } } break ; } } } } for ( iINChI = 0 ; iINChI < INCHI_NUM ; iINChI ++ ) { for ( j = 0 ; j < TAUT_NUM ; j ++ ) { for ( k = 0 ; k < nNumComponents [ iINChI ] [ j ] ; k ++ ) { pInChI = pInpInChI [ iINChI ] [ j ] + k ; num_at = ( ! pInChI -> bDeleted ) ? pInChI -> nNumberOfAtoms : 0 ; if ( ! num_at ) { if ( pInChI -> nPossibleLocationsOfIsotopicH ) { inchi_free ( pInChI -> nPossibleLocationsOfIsotopicH ) ; pInChI -> nPossibleLocationsOfIsotopicH = NULL ; } continue ; } if ( ! pInChI -> nPossibleLocationsOfIsotopicH ) { continue ; } if ( iINChI == INCHI_BAS && num_at == 1 && pInChI -> szHillFormula && ! strcmp ( pInChI -> szHillFormula , " " ) && ( int ) pInChI -> nPossibleLocationsOfIsotopicH [ 0 ] - 1 >= nLenXYZ ) { ; } else { pxyz = ( XYZ_COORD * ) inchi_calloc ( num_at , sizeof ( pxyz [ 0 ] ) ) ; if ( ! pxyz ) { ret = RI_ERR_ALLOC ; goto exit_function ; } for ( n = 0 ; n < num_at ; n ++ ) { m = ( int ) pInChI -> nPossibleLocationsOfIsotopicH [ n ] - 1 ; if ( m < 0 || m >= nLenXYZ ) { inchi_free ( pxyz ) ; ret = RI_ERR_SYNTAX ; goto exit_function ; } pxyz [ n ] = pXYZ [ m ] ; } pInChI -> IsotopicTGroup = ( INChI_IsotopicTGroup * ) pxyz ; } inchi_free ( pInChI -> nPossibleLocationsOfIsotopicH ) ; pInChI -> nPossibleLocationsOfIsotopicH = NULL ; } } } ret = nRet ; exit_function : return ret ; }
Parse and save atom coordinates from AuxInfo
Parse and save atom coordinates from AuxInfo
/* Propagate numberings (original:canonical atom mapping)                                                                           */ /* NB: we already saved isotopic numbering, if any, into the first nNumberOfAtoms elements of INChI::nPossibleLocationsOfIsotopicH, */ /* and the non-isotopic numbering into the second half of nNumberOfAtoms elements of that INChI::nPossibleLocationsOfIsotopicH      */ /* for FixedH and MobileH ... */ /* for each component ... */ /* component does not exist */ /* copy from non-isotopic (2nd half of the at. numbers array) to the isotopic (1st half) */ /* component does not exist */ /* trying to get numbers from Mobile-H component */ /* pAltInChI->nPossibleLocationsOfIsotopicH should have */ /* been deallocated on previous TAUT_YES pass           */ /* copy from non-isotopic to isotopic */ /* Add coordinates */ /* a single atom H disconnected from a metal atom has no coordinates */ /* add atom coordinates */ /* normal exit */