third_party/inchi/INCHI_BASE/src/ichiread.c
int AddLinkedBond( AT_NUMB at1,
                   AT_NUMB at2,
                   AT_NUMB num_at,
                   LINKED_BONDS *pLB )
{
    int nReqLen = inchi_max( 2 * num_at + 2, pLB->len + 2 );
    AT_NUMB prev;
#if ( FIX_GAF_2019_2==1 )
    {
        if (at2>num_at || at1>num_at || at1<0 || at2<0 || num_at>MAX_ATOMS)
        {
            return RI_ERR_SYNTAX;
        }
    }
#endif
    if (pLB->len_alloc <= nReqLen)
    {
        /*int nNewLen = nReqLen + (nReqLen + LINKED_BOND_ADD - 1)%LINKED_BOND_ADD + LINKED_BOND_ADD;*/
        int nNewLen = nReqLen - nReqLen%LINKED_BOND_ADD + 2 * LINKED_BOND_ADD;
        ONE_LINKED_BOND *pBond = (ONE_LINKED_BOND *) inchi_calloc( nNewLen, sizeof( pBond[0] ) );
        if (!pBond)
        {
            return RI_ERR_ALLOC; /* allocation error */
        }
        if (pLB->pBond && pLB->len)
        {
            memcpy( pBond, pLB->pBond, pLB->len * sizeof( pBond[0] ) );
        }
        if (pLB->pBond)
            inchi_free( pLB->pBond );
        pLB->pBond = pBond;
        pLB->len_alloc = nNewLen;
    }
    if (!pLB->len)
    {
        pLB->len = num_at + 1;
        memset( pLB->pBond, 0, ( num_at + 1 ) * sizeof( pLB->pBond[0] ) );
    }

    prev = pLB->pBond[at1].prev; /* position of the last neighbor of at1 in the pLB->pBond */
    if (!prev)
    {
        pLB->pBond[at1].neigh = at2;
        pLB->pBond[at1].prev = at1;
    }
    else
    {
        pLB->pBond[pLB->len].neigh = at2;
        pLB->pBond[pLB->len].prev = prev;
        pLB->pBond[at1].prev = pLB->len++;
    }

    prev = pLB->pBond[at2].prev; /* position of the last neighbor of at2 in the pLB->pBond */
    if (!prev)
    {
        pLB->pBond[at2].neigh = at1;
        pLB->pBond[at2].prev = at2;
    }
    else
    {
        pLB->pBond[pLB->len].neigh = at1;
        pLB->pBond[pLB->len].prev = prev;
        pLB->pBond[at2].prev = pLB->len++;
    }

    return 0;
}
/****************************************************************************
Add one more bond to the linked lists for both neighbors
****************************************************************************/
Add one more bond to the linked lists for both neighbors
Add one more bond to the linked lists for both neighbors
int AddLinkedBond( AT_NUMB at1,
                   AT_NUMB at2,
                   AT_NUMB num_at,
                   LINKED_BONDS *pLB )
{
    int nReqLen = inchi_max( 2 * num_at + 2, pLB->len + 2 );
    AT_NUMB prev;
#if ( FIX_GAF_2019_2==1 )
    {
        if (at2>num_at || at1>num_at || at1<0 || at2<0 || num_at>MAX_ATOMS)
        {
            return RI_ERR_SYNTAX;
        }
    }
#endif
    if (pLB->len_alloc <= nReqLen)
    {
        int nNewLen = nReqLen - nReqLen%LINKED_BOND_ADD + 2 * LINKED_BOND_ADD;
        ONE_LINKED_BOND *pBond = (ONE_LINKED_BOND *) inchi_calloc( nNewLen, sizeof( pBond[0] ) );
        if (!pBond)
        {
            return RI_ERR_ALLOC; 
        }
        if (pLB->pBond && pLB->len)
        {
            memcpy( pBond, pLB->pBond, pLB->len * sizeof( pBond[0] ) );
        }
        if (pLB->pBond)
            inchi_free( pLB->pBond );
        pLB->pBond = pBond;
        pLB->len_alloc = nNewLen;
    }
    if (!pLB->len)
    {
        pLB->len = num_at + 1;
        memset( pLB->pBond, 0, ( num_at + 1 ) * sizeof( pLB->pBond[0] ) );
    }
    prev = pLB->pBond[at1].prev; 
    if (!prev)
    {
        pLB->pBond[at1].neigh = at2;
        pLB->pBond[at1].prev = at1;
    }
    else
    {
        pLB->pBond[pLB->len].neigh = at2;
        pLB->pBond[pLB->len].prev = prev;
        pLB->pBond[at1].prev = pLB->len++;
    }
    prev = pLB->pBond[at2].prev; 
    if (!prev)
    {
        pLB->pBond[at2].neigh = at1;
        pLB->pBond[at2].prev = at2;
    }
    else
    {
        pLB->pBond[pLB->len].neigh = at1;
        pLB->pBond[pLB->len].prev = prev;
        pLB->pBond[at2].prev = pLB->len++;
    }
    return 0;
}
int AddLinkedBond ( AT_NUMB at1 , AT_NUMB at2 , AT_NUMB num_at , LINKED_BONDS * pLB ) { int nReqLen = inchi_max ( 2 * num_at + 2 , pLB -> len + 2 ) ; AT_NUMB prev ; #if ( FIX_GAF_2019_2 == 1 ) { if ( at2 > num_at || at1 > num_at || at1 < 0 || at2 < 0 || num_at > MAX_ATOMS ) { return RI_ERR_SYNTAX ; } } #endif if ( pLB -> len_alloc <= nReqLen ) { int nNewLen = nReqLen - nReqLen % LINKED_BOND_ADD + 2 * LINKED_BOND_ADD ; ONE_LINKED_BOND * pBond = ( ONE_LINKED_BOND * ) inchi_calloc ( nNewLen , sizeof ( pBond [ 0 ] ) ) ; if ( ! pBond ) { return RI_ERR_ALLOC ; } if ( pLB -> pBond && pLB -> len ) { memcpy ( pBond , pLB -> pBond , pLB -> len * sizeof ( pBond [ 0 ] ) ) ; } if ( pLB -> pBond ) inchi_free ( pLB -> pBond ) ; pLB -> pBond = pBond ; pLB -> len_alloc = nNewLen ; } if ( ! pLB -> len ) { pLB -> len = num_at + 1 ; memset ( pLB -> pBond , 0 , ( num_at + 1 ) * sizeof ( pLB -> pBond [ 0 ] ) ) ; } prev = pLB -> pBond [ at1 ] . prev ; if ( ! prev ) { pLB -> pBond [ at1 ] . neigh = at2 ; pLB -> pBond [ at1 ] . prev = at1 ; } else { pLB -> pBond [ pLB -> len ] . neigh = at2 ; pLB -> pBond [ pLB -> len ] . prev = prev ; pLB -> pBond [ at1 ] . prev = pLB -> len ++ ; } prev = pLB -> pBond [ at2 ] . prev ; if ( ! prev ) { pLB -> pBond [ at2 ] . neigh = at1 ; pLB -> pBond [ at2 ] . prev = at2 ; } else { pLB -> pBond [ pLB -> len ] . neigh = at1 ; pLB -> pBond [ pLB -> len ] . prev = prev ; pLB -> pBond [ at2 ] . prev = pLB -> len ++ ; } return 0 ; }
Add one more bond to the linked lists for both neighbors
Add one more bond to the linked lists for both neighbors
/*int nNewLen = nReqLen + (nReqLen + LINKED_BOND_ADD - 1)%LINKED_BOND_ADD + LINKED_BOND_ADD;*/ /* allocation error */ /* position of the last neighbor of at1 in the pLB->pBond */ /* position of the last neighbor of at2 in the pLB->pBond */