~ubuntu-branches/debian/stretch/openbabel/stretch

« back to all changes in this revision

Viewing changes to include/inchi103/aux2atom.h

  • Committer: Package Import Robot
  • Author(s): Daniel Leidert
  • Date: 2013-05-22 19:08:27 UTC
  • mfrom: (1.1.11) (7.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20130522190827-72q0fnx5y2nm3bc0
Tags: 2.3.2+dfsg-1
* New upstream release.
* debian/control: Dropped DM-Upload-Allowed field.
  (Standards-Version): Bumped to 3.9.4.
* debian/copyright: Massive update.
* debian/upstream: Author name update.
* debian/get-orig-source.sh: Remove the windows-*/ directory too.
* debian/openbabel.install: Removed roundtrip manpage.
* debian/openbabel-gui.install: Fixed manpage name.
* debian/openbabel-gui.links: Removed unused file.
* debian/rules: Enable OpenMP. Disable tests on `nocheck'.
* debian/patches/gaussformat_nosym.patch: Dropped. Applied upstream.
* debian/patches/moldenformat_coordonly.patch: Ditto.
* debian/patches/obspectrophore_man.patch: Ditto.
* debian/patches/fix_ftbfs.patch: Added.
  - Fix several FTBFS issues in upstream build system.
* debian/patches/series: Adjusted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * International Chemical Identifier (InChI)
3
 
 * Version 1
4
 
 * Software version 1.03
5
 
 * May 9, 2010
6
 
 *
7
 
 * Originally developed at NIST
8
 
 * Modifications and additions by IUPAC and the InChI Trust
9
 
 *
10
 
 * The InChI library and programs are free software developed under the
11
 
 * auspices of the International Union of Pure and Applied Chemistry (IUPAC);
12
 
 * you can redistribute this software and/or modify it under the terms of 
13
 
 * the GNU Lesser General Public License as published by the Free Software 
14
 
 * Foundation:
15
 
 * http://www.opensource.org/licenses/lgpl-2.1.php
16
 
 */
17
 
 
18
 
 
19
 
/*
20
 
 The code in this #include file reads InChI AuxInfo
21
 
*/
22
 
 
23
 
/****************************************************************************/
24
 
#define MIN_BOND_LENGTH   (1.0e-6)
25
 
#define INCHI_LINE_LEN   512 /*1024*/ /*256*/ 
26
 
#define INCHI_LINE_ADD   384  /*128*/  /*64*/
27
 
/* Note: (INCHI_LINE_LEN - INCHI_LINE_ADD) > (length of the longest item: szCoord) = 33 */
28
 
/*****************************************************************************/
29
 
 
30
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
31
 
 
32
 
#define AB_MAX_WELL_DEFINED_PARITY inchi_max(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* 1, 2 => well defined parities, uncluding 'unknown' */
33
 
#define AB_MIN_WELL_DEFINED_PARITY inchi_min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) */
34
 
#define ATOM_PARITY_WELL_DEF(X)     (AB_MIN_WELL_DEFINED_PARITY <= (X) && (X) <= AB_MAX_WELL_DEFINED_PARITY)
35
 
 
36
 
#define inchi_NUMH2(AT,CUR_AT) ((AT[CUR_AT].num_iso_H[0]>0?AT[CUR_AT].num_iso_H[0]:0) +AT[CUR_AT].num_iso_H[1]+AT[CUR_AT].num_iso_H[2]+AT[CUR_AT].num_iso_H[3])
37
 
 
38
 
#define SB_PARITY_FLAG  0x38 /* disconnected structure has undef. parity */
39
 
#define SB_PARITY_SHFT  3
40
 
#define SB_PARITY_MASK  0x07
41
 
#define SB_PARITY_1(X) (X & SB_PARITY_MASK)  /* refers to connected structure */
42
 
#define SB_PARITY_2(X) (((X) >> SB_PARITY_SHFT) & SB_PARITY_MASK) /* refers to connected structure */
43
 
 
44
 
 
45
 
 
46
 
 
47
 
 
48
 
 
49
 
#endif
50
 
 
51
 
 
52
 
 
53
 
#ifdef INCHI_LIBRARY
54
 
 
55
 
void            FreeInchi_Atom( inchi_Atom **at );
56
 
inchi_Atom     *CreateInchi_Atom( int num_atoms );
57
 
void            FreeInchi_Input( inchi_Input *inp_at_data );
58
 
S_SHORT        *is_in_the_slist( S_SHORT *pathAtom, S_SHORT nNextAtom, int nPathLen );
59
 
int             is_element_a_metal( char szEl[] );
60
 
 
61
 
 
62
 
 
63
 
 
64
 
#endif
65
 
 
66
 
#ifndef INCHI_MAIN
67
 
 
68
 
void            FreeInchi_Stereo0D( inchi_Stereo0D **stereo0D );
69
 
inchi_Stereo0D *CreateInchi_Stereo0D( int num_stereo0D );
70
 
int Extract0DParities( inp_ATOM *at, int nNumAtoms, inchi_Stereo0D *stereo0D,
71
 
                       int num_stereo0D, char *pStrErr, int *err,
72
 
                       int vABParityUnknown);
73
 
 
74
 
#endif
75
 
 
76
 
 
77
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
78
 
 
79
 
/* inchi_fgets */
80
 
 
81
 
#endif
82
 
 
83
 
 
84
 
#ifdef INCHI_LIBRARY
85
 
/******************************************************************************************************/
86
 
void FreeInchi_Atom( inchi_Atom **at )
87
 
{
88
 
    if ( at && *at ) {
89
 
        inchi_free( *at );
90
 
        *at = NULL;
91
 
    }
92
 
}
93
 
/******************************************************************************************************/
94
 
inchi_Atom *CreateInchi_Atom( int num_atoms )
95
 
{
96
 
   inchi_Atom *p = (inchi_Atom* ) inchi_calloc(num_atoms, sizeof(inchi_Atom) );
97
 
   return p;
98
 
}
99
 
/******************************************************************************************************/
100
 
void FreeInchi_Input( inchi_Input *inp_at_data )
101
 
{
102
 
    FreeInchi_Atom( &inp_at_data->atom );
103
 
    FreeInchi_Stereo0D( &inp_at_data->stereo0D );
104
 
    memset( inp_at_data, 0, sizeof(*inp_at_data) );
105
 
}
106
 
/*************************************************************************/
107
 
S_SHORT *is_in_the_slist( S_SHORT *pathAtom, S_SHORT nNextAtom, int nPathLen )
108
 
{
109
 
    for ( ; nPathLen && *pathAtom != nNextAtom; nPathLen--,  pathAtom++ )
110
 
        ;
111
 
    return nPathLen? pathAtom : NULL;
112
 
}
113
 
/************************************************/
114
 
int is_element_a_metal( char szEl[] )
115
 
{
116
 
    static const char szMetals[] = "K;V;Y;W;U;"
117
 
        "Li;Be;Na;Mg;Al;Ca;Sc;Ti;Cr;Mn;Fe;Co;Ni;Cu;Zn;Ga;Rb;Sr;Zr;"
118
 
        "Nb;Mo;Tc;Ru;Rh;Pd;Ag;Cd;In;Sn;Sb;Cs;Ba;La;Ce;Pr;Nd;Pm;Sm;"
119
 
        "Eu;Gd;Tb;Dy;Ho;Er;Tm;Yb;Lu;Hf;Ta;Re;Os;Ir;Pt;Au;Hg;Tl;Pb;"
120
 
        "Bi;Po;Fr;Ra;Ac;Th;Pa;Np;Pu;Am;Cm;Bk;Cf;Es;Fm;Md;No;Lr;Rf;";
121
 
    const int len = strlen(szEl);
122
 
    const char *p;
123
 
 
124
 
    if ( 0 < len && len <= 2 &&
125
 
         isalpha( UCINT szEl[0] ) && isupper( szEl[0] ) &&
126
 
         (p = strstr(szMetals, szEl) ) && p[len] == ';' ) {
127
 
 
128
 
            return 1; /*return AtType_Metal;*/
129
 
    }
130
 
    return 0;
131
 
}
132
 
 
133
 
#endif
134
 
 
135
 
 
136
 
#ifndef INCHI_MAIN
137
 
/******************************************************************************************************/
138
 
inchi_Stereo0D *CreateInchi_Stereo0D( int num_stereo0D )
139
 
{
140
 
   return (inchi_Stereo0D* ) inchi_calloc(num_stereo0D, sizeof(inchi_Stereo0D) );
141
 
}
142
 
/******************************************************************************************************/
143
 
void FreeInchi_Stereo0D( inchi_Stereo0D **stereo0D )
144
 
{
145
 
    if ( stereo0D && *stereo0D ) {
146
 
        inchi_free( *stereo0D );
147
 
        *stereo0D = NULL;
148
 
    }
149
 
}
150
 
#endif
151
 
 
152
 
#define INPUT_FILE          INCHI_IOSTREAM
153
 
 
154
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
155
 
 
156
 
#ifdef INCHI_LIBRARY
157
 
#define INChITo_Atom        ll_INChIToInchi_Atom
158
 
#else
159
 
#define INChITo_Atom        ee_INChIToIbChI_Atom
160
 
#define FindToken           e_FindToken
161
 
#define LoadLine            e_LoadLine
162
 
#endif
163
 
 
164
 
#define AT_NUM_BONDS(AT)    (AT).num_bonds
165
 
#define ATOM_NUMBER         AT_NUM
166
 
#define IN_NEIGH_LIST       is_in_the_slist
167
 
/*#define INPUT_FILE          INCHI_IOSTREAM*/
168
 
#define Create_Atom         CreateInchi_Atom 
169
 
#define AT_BONDS_VAL(AT,I)  AT[I].num_iso_H[0]
170
 
#define ISOLATED_ATOM       (-15)
171
 
#define NUM_ISO_Hk(AT,I,K)  AT[I].num_iso_H[K+1]
172
 
#define IS_METAL_ATOM(AT,I) is_element_a_metal( AT[I].elname )
173
 
 
174
 
#else 
175
 
 
176
 
#define inchi_Atom          inp_ATOM
177
 
#define AT_NUM_BONDS(AT)    (AT).valence
178
 
#define ATOM_NUMBER         AT_NUMB
179
 
#define IN_NEIGH_LIST       is_in_the_list
180
 
#define inchi_NUMH2(AT,N)   NUMH(AT,N)
181
 
#define INChITo_Atom        cc_INChIToInpAtom
182
 
/*#define INPUT_FILE          FILE*/
183
 
#define Create_Atom         CreateInpAtom 
184
 
#define AT_BONDS_VAL(AT,I)  AT[I].chem_bonds_valence
185
 
#define ISOLATED_ATOM       15
186
 
#define NUM_ISO_Hk(AT,I,K)  AT[I].num_iso_H[K]
187
 
#define IS_METAL_ATOM(AT,I) is_el_a_metal( AT[I].el_number )
188
 
 
189
 
#endif
190
 
 
191
 
/*****************************************************************************/
192
 
/* local prototypes */
193
 
char *FindToken( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, const char *sToken, int lToken,
194
 
                        char *szLine, int nLenLine, char *p, int *res );
195
 
char *LoadLine( INPUT_FILE *inp_molfile, int *bTooLongLine, int *bItemIsOver, char **s,
196
 
                        char *szLine, int nLenLine, int nMinLen2Load, char *p, int *res ); 
197
 
 
198
 
 
199
 
int INChITo_Atom(INPUT_FILE *inp_molfile, MOL_COORD **szCoord,
200
 
                      inchi_Stereo0D **stereo0D, int *num_stereo0D,
201
 
                      int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inchi_Atom **at,
202
 
                      int max_num_at,
203
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
204
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
205
 
 
206
 
 
207
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
208
 
/*****************************************************************************/
209
 
int INChIToInchi_Atom ( INCHI_IOSTREAM *inp_molfile, inchi_Stereo0D **stereo0D, int *num_stereo0D,
210
 
                      int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, 
211
 
                      inchi_Atom **at, int max_num_at,
212
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
213
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
214
 
 
215
 
int INChIToInchi_Atom ( INCHI_IOSTREAM *inp_molfile, inchi_Stereo0D **stereo0D, int *num_stereo0D,
216
 
                      int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, 
217
 
                      inchi_Atom **at, int max_num_at,
218
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
219
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
220
 
{
221
 
    return INChITo_Atom ( inp_molfile, NULL, stereo0D, num_stereo0D,
222
 
                          bDoNotAddH, vABParityUnknown, nInputType, at, max_num_at,
223
 
                          num_dimensions, num_bonds, pSdfLabel, pSdfValue,
224
 
                          Id, pInpAtomFlags, err, pStrErr );
225
 
}
226
 
#else
227
 
int INChIToInpAtom (  INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
228
 
                      int bDoNotAddH, int vABParityUnknown,  INPUT_TYPE nInputType, inp_ATOM **at,
229
 
                      int max_num_at,
230
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
231
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
232
 
int INChIToInpAtom (  INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
233
 
                      int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inp_ATOM **at,
234
 
                      int max_num_at,
235
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
236
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
237
 
{
238
 
    return INChITo_Atom ( inp_molfile, szCoord, NULL, NULL,
239
 
                          bDoNotAddH, vABParityUnknown, nInputType, at, max_num_at,
240
 
                          num_dimensions, num_bonds, pSdfLabel, pSdfValue,
241
 
                          Id, pInpAtomFlags, err, pStrErr );
242
 
}
243
 
#endif
244
 
/*****************************************************************************/
245
 
char *FindToken( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, const char *sToken, int lToken,
246
 
                        char *szLine, int nLenLine, char *p, int *res )
247
 
{
248
 
    char *q;
249
 
    int   res2;
250
 
                
251
 
    while ( !(q = strstr( p, sToken ) ) ) {
252
 
        if ( (q = strrchr( p, '/' )) && (q + lToken > szLine + *res) ) {
253
 
            *res -= q - szLine; /* res = the length of the szLine to be left in */
254
 
            memmove( szLine, q, *res + 1);
255
 
        } else {
256
 
            *res = 0;
257
 
        }
258
 
        if ( !*bTooLongLine || 
259
 
             0 > (res2 = inchi_ios_getsTab1( szLine + *res, nLenLine - *res - 1,
260
 
                                       inp_molfile, bTooLongLine ) ) ) {
261
 
            /* the line is over or end of file */
262
 
            return NULL;
263
 
        } else {
264
 
            *res += res2;
265
 
            p = szLine;
266
 
        }
267
 
    }
268
 
 
269
 
    return q + lToken;
270
 
}
271
 
/*****************************************************************************/
272
 
char *LoadLine( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, int *bItemIsOver, char **s,
273
 
                        char *szLine, int nLenLine, int nMinLen2Load, char *p, int *res ) 
274
 
{
275
 
    int pos = p - szLine, res2;
276
 
    if ( !*bItemIsOver && nLenLine - (*res - pos) > nMinLen2Load ) {
277
 
        /* load the next portion if possible */
278
 
        if ( pos ) {
279
 
            *res -= pos;
280
 
            memmove( szLine, p, *res+1 );
281
 
            p = szLine;
282
 
            if ( *s ) {
283
 
                *s -= pos;
284
 
            }
285
 
            pos = 0;
286
 
        }
287
 
        res2 = inchi_ios_getsTab1( szLine + *res, nLenLine - *res - 1, inp_molfile, bTooLongLine );
288
 
        if ( res2 > 0 ) {
289
 
            *bItemIsOver = ( (*s = strchr( p + *res, '/') ) || !*bTooLongLine );
290
 
            *res += res2;
291
 
        } else {
292
 
            *bItemIsOver = 1;
293
 
        }
294
 
    }
295
 
    return p;
296
 
}
297
 
/*****************************************************************************/
298
 
int INChITo_Atom(INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
299
 
                      inchi_Stereo0D **stereo0D, int *num_stereo0D,
300
 
                      int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inchi_Atom **at,
301
 
                      int max_num_at,
302
 
                      int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
303
 
                      long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
304
 
{
305
 
    int      num_atoms = 0, bFindNext = 0, len, bHeaderRead, bItemIsOver, bErrorMsg, bRestoreInfo;
306
 
    int      bFatal = 0, num_struct = 0;
307
 
    int      i, k, k2, res, bond_type, bond_stereo1, bond_stereo2, bond_char, neigh, bond_parity, bond_parityNM;
308
 
    int      bTooLongLine, res2, bTooLongLine2, pos, hlen, hk;
309
 
    long     longID;
310
 
    char     szLine[INCHI_LINE_LEN], szNextLine[INCHI_LINE_ADD], *p, *q, *s, parity;
311
 
    int      b2D=0, b3D=0, b23D, nNumBonds = 0, bNonZeroXYZ, bNonMetal;
312
 
    int      len_stereo0D = 0, max_len_stereo0D = 0;
313
 
    inchi_Stereo0D  *atom_stereo0D = NULL;
314
 
    inchi_Atom      *atom          = NULL;
315
 
    MOL_COORD       *pszCoord      = NULL;
316
 
    INCHI_MODE InpAtomFlags = 0; /* 0 or FLAG_INP_AT_NONCHIRAL or FLAG_INP_AT_CHIRAL */
317
 
    static const char szIsoH[] = "hdt";
318
 
    /* plain tags */
319
 
    static const char sStructHdrPln[]         = "Structure:";
320
 
    static const char sStructHdrPlnNoLblVal[] = " is missing";
321
 
    static char sStructHdrPlnAuxStart[64] =""; /*"$1.1Beta/";*/
322
 
    static int  lenStructHdrPlnAuxStart = 0;
323
 
    static const char sStructHdrPlnRevAt[]    = "/rA:";
324
 
    static const char sStructHdrPlnRevBn[]    = "/rB:";
325
 
    static const char sStructHdrPlnRevXYZ[]   = "/rC:";
326
 
    const  char *sToken;
327
 
    int  lToken;
328
 
    if ( !lenStructHdrPlnAuxStart ) {
329
 
        lenStructHdrPlnAuxStart = sprintf( sStructHdrPlnAuxStart, "AuxInfo=" );
330
 
    }
331
 
 
332
 
    if ( at ) {
333
 
        if ( *at && max_num_at ) {
334
 
            memset( *at, 0, max_num_at * sizeof(**at) );
335
 
        }
336
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
337
 
        if ( stereo0D && num_stereo0D ) {
338
 
            if ( *stereo0D && *num_stereo0D ) {
339
 
                max_len_stereo0D = *num_stereo0D;
340
 
                memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ));
341
 
            } else {
342
 
                max_len_stereo0D = 0;
343
 
            }
344
 
        }
345
 
#else
346
 
        if ( szCoord && *szCoord ) {
347
 
            inchi_free( *szCoord );
348
 
            *szCoord = NULL;
349
 
        }
350
 
#endif
351
 
    } else {
352
 
        bFindNext = 1;
353
 
    }
354
 
    bHeaderRead = bErrorMsg = bRestoreInfo = 0;
355
 
    *num_dimensions = *num_bonds = 0;
356
 
 
357
 
    /*************************************************************/
358
 
    /*   extract reversibility info from plain text INChI format */
359
 
    /*************************************************************/
360
 
    if ( nInputType == INPUT_INCHI_PLAIN ) {
361
 
        bHeaderRead = hk = 0;
362
 
        while ( 0 < (res = inchi_ios_getsTab( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) ) {
363
 
 
364
 
            /********************* find and interpret structure header ************/
365
 
            if ( !bTooLongLine &&
366
 
                 (hlen=sizeof(sStructHdrPln)-1, !memcmp(szLine, sStructHdrPln, hlen)) ) {
367
 
                p = szLine + hlen;
368
 
                longID = 0;
369
 
                num_atoms = 0;
370
 
                /* structure number */
371
 
                longID = strtol( p, &q, 10 );
372
 
                if ( q && q[0] == '.' && q[1] == ' ' ) {
373
 
                    p = q+2;
374
 
                }
375
 
                p = p + strspn( p, " \n\r" );
376
 
                
377
 
                if ( pSdfLabel ) {
378
 
                    pSdfLabel[0] = '\0';
379
 
                }
380
 
                if ( pSdfValue ) {
381
 
                    pSdfValue[0] = '\0';
382
 
                }
383
 
 
384
 
                if ( *p ) {
385
 
                    /* has label name */
386
 
                    /*p ++;*/
387
 
                    if ( q = strchr( p, '=' ) ) {
388
 
                        /* '=' separates label name from the value */
389
 
                        len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
390
 
                        if ( pSdfLabel ) {
391
 
                            mystrncpy( pSdfLabel, p, len );
392
 
                            LtrimRtrim( pSdfLabel, &len );
393
 
                        }
394
 
                        p = q+1;
395
 
                        q = p + (int)strlen( p );
396
 
                        if ( q-p > 0 ) {
397
 
                            len = inchi_min( q-p+1, MAX_SDF_VALUE-1);
398
 
                            if ( pSdfValue ) {
399
 
                                mystrncpy( pSdfValue, p, len );
400
 
                            }
401
 
                            p = q;
402
 
                        }
403
 
 
404
 
                    } else
405
 
                    if ( q = strstr( p, sStructHdrPlnNoLblVal ) ) {
406
 
                        len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
407
 
                        if ( pSdfLabel ) {
408
 
                            mystrncpy( pSdfLabel, p, len );
409
 
                        }
410
 
                        p = q+1;
411
 
                    }
412
 
                }
413
 
                if ( Id )
414
 
                    *Id = longID;
415
 
 
416
 
                bHeaderRead = 1;
417
 
                bErrorMsg = bRestoreInfo = 0;
418
 
            } else
419
 
            if ( !memcmp( szLine, sStructHdrPlnAuxStart, lenStructHdrPlnAuxStart) ) {
420
 
                /* found the header of the AuxInfo, read AuxInfo head of the line */
421
 
                if ( !bHeaderRead ) {
422
 
                    longID = 0;
423
 
                    if ( Id )
424
 
                        *Id = longID;
425
 
                    if ( pSdfLabel ) {
426
 
                        pSdfLabel[0] = '\0';
427
 
                    }
428
 
                    if ( pSdfValue ) {
429
 
                        pSdfValue[0] = '\0';
430
 
                    }
431
 
                }
432
 
                bHeaderRead = 0;
433
 
                /* check for empty "AuxInfo=ver//" */
434
 
                p = strchr( szLine + lenStructHdrPlnAuxStart, '/' );
435
 
                if ( p && p[1] == '/' && (!p[2] || '\n' == p[2]) ) {
436
 
                    goto bypass_end_of_INChI_plain;
437
 
                }
438
 
                /***************** search for atoms block (plain) **********************/
439
 
                p = szLine;
440
 
                sToken = sStructHdrPlnRevAt;
441
 
                lToken = sizeof(sStructHdrPlnRevAt)-1;
442
 
                /* search for sToken in the line; load next segments of the line if sToken has not found */
443
 
                p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
444
 
                               szLine, sizeof(szLine), p, &res );
445
 
                if ( !p ) {
446
 
                    *err      = INCHI_INP_ERROR_ERR;
447
 
                    num_atoms = INCHI_INP_ERROR_RET;
448
 
                    MOLFILE_ERR_SET (*err, 0, "Missing atom data");
449
 
                    goto bypass_end_of_INChI_plain;
450
 
                } else {
451
 
                    /* atoms block started */
452
 
                    i = 0;
453
 
                    res2 = bTooLongLine2 = -1;
454
 
                    bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
455
 
                    while ( 1 ) {
456
 
                        p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
457
 
                                      szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
458
 
                        if ( !i ) {
459
 
                            /* allocate atom */
460
 
                            num_atoms = strtol( p, &q, 10 );
461
 
                            if ( !num_atoms || !q || !*q ) {
462
 
                                num_atoms = 0; /* no atom data */
463
 
                                goto bypass_end_of_INChI_plain;
464
 
                            }
465
 
                            p = q;
466
 
                            /* Molfile chirality flag */
467
 
                            switch( *p ) {
468
 
                            case 'c':
469
 
                                InpAtomFlags |= FLAG_INP_AT_CHIRAL;
470
 
                                p ++;
471
 
                                break;
472
 
                            case 'n':
473
 
                                InpAtomFlags |= FLAG_INP_AT_NONCHIRAL;
474
 
                                p ++;
475
 
                                break;
476
 
                            }
477
 
                            if ( at && *at ) {
478
 
                                if ( num_atoms > max_num_at ) {
479
 
                                    inchi_free( *at );
480
 
                                    *at = NULL;
481
 
                                } else {
482
 
                                    memset( *at, 0, max_num_at * sizeof( **at ) );
483
 
                                    atom = *at;
484
 
                                }
485
 
                            }
486
 
                            if ( !at || !*at ) {
487
 
                                atom = Create_Atom( num_atoms+1 );
488
 
                                if ( !atom ) {
489
 
                                    num_atoms = INCHI_INP_FATAL_RET; /* was -1; error */
490
 
                                    *err      = INCHI_INP_FATAL_ERR;
491
 
                                    MOLFILE_ERR_SET (*err, 0, "Out of RAM");
492
 
                                    goto bypass_end_of_INChI_plain;
493
 
                                }
494
 
                            }
495
 
                            if ( stereo0D && *stereo0D ) {
496
 
                                if ( num_atoms > max_len_stereo0D ) {
497
 
                                    FreeInchi_Stereo0D( stereo0D );
498
 
                                } else {
499
 
                                    memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ) );
500
 
                                    atom_stereo0D = *stereo0D;
501
 
                                }
502
 
                            }
503
 
                            if ( !stereo0D || !*stereo0D ) {
504
 
                                max_len_stereo0D = num_atoms+1;
505
 
                                atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D );
506
 
                                if ( !atom_stereo0D ) {
507
 
                                    num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
508
 
                                    *err      = INCHI_INP_FATAL_ERR;
509
 
                                    MOLFILE_ERR_SET (*err, 0, "Out of RAM");
510
 
                                    goto bypass_end_of_INChI_plain;
511
 
                                }
512
 
                            }
513
 
                        }
514
 
                        /* element, first char */
515
 
                        if ( !isalpha( UCINT *p ) || !isupper( UCINT *p ) || i >= num_atoms ) {
516
 
                            break; /* end of atoms block */
517
 
                        }
518
 
                        atom[i].elname[0] = *p ++;
519
 
                        /* element, second char */
520
 
                        if ( isalpha( UCINT *p ) && islower( UCINT *p ) ) {
521
 
                            atom[i].elname[1] = *p ++;
522
 
                        }
523
 
    #if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
524
 
    #else
525
 
                        atom[i].el_number = get_periodic_table_number( atom[i].elname );
526
 
    #endif
527
 
                        /* bonds' valence + number of non-isotopic H */
528
 
                        if ( isdigit( UCINT *p ) ) {
529
 
                            AT_BONDS_VAL(atom,i) = (char)strtol( p, &q, 10 );
530
 
                            if ( !AT_BONDS_VAL(atom,i) )
531
 
                                AT_BONDS_VAL(atom,i) = ISOLATED_ATOM; /* same convention as in MOLfile, found zero bonds valence */
532
 
                            p = q;
533
 
                        }
534
 
                        /* charge */
535
 
                        atom[i].charge = (*p == '+')? 1 : (*p == '-')? -1 : 0;
536
 
                        if ( atom[i].charge ) {
537
 
                            p ++;
538
 
                            if ( isdigit( UCINT *p ) ) {
539
 
                                atom[i].charge *= (S_CHAR)(strtol( p, &q, 10 ) & CHAR_MASK);
540
 
                                p = q;
541
 
                            }
542
 
                        }
543
 
                        /* radical */
544
 
                        if ( *p == '.' ) {
545
 
                            p ++;
546
 
                            if ( isdigit( UCINT *p ) ) {
547
 
                                atom[i].radical = (S_CHAR)strtol( p, &q, 10 );
548
 
                                p = q;
549
 
                            }
550
 
                        }
551
 
                        /* isotopic mass */
552
 
                        if ( *p == 'i' ) {
553
 
                            p ++;
554
 
                            if ( isdigit( UCINT *p ) ) {
555
 
                                int mw = strtol( p, &q, 10 );
556
 
                                p = q;
557
 
    #if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
558
 
                                atom[i].isotopic_mass = mw;
559
 
    #else
560
 
                                mw -= get_atw_from_elnum( atom[i].el_number );
561
 
                                if ( mw >= 0 )
562
 
                                    mw ++;
563
 
                                atom[i].iso_atw_diff = mw;
564
 
    #endif
565
 
                            }
566
 
                        }
567
 
                        /* parity */
568
 
                        switch( *p ) {
569
 
                        case 'o':
570
 
                            parity = INCHI_PARITY_ODD;
571
 
                            p ++;
572
 
                            break;
573
 
                        case 'e':
574
 
                            parity = INCHI_PARITY_EVEN;
575
 
                            p ++;
576
 
                            break;
577
 
                        case 'u':
578
 
                            parity = INCHI_PARITY_UNKNOWN;
579
 
                            p ++;
580
 
                            break;
581
 
                        case '?':
582
 
                            parity = INCHI_PARITY_UNDEFINED;
583
 
                            p ++;
584
 
                            break;
585
 
                        default:
586
 
                            parity = 0;
587
 
                            break;
588
 
                        }
589
 
                        if ( parity ) {
590
 
                            atom_stereo0D[len_stereo0D].central_atom = i;
591
 
                            atom_stereo0D[len_stereo0D].parity       = parity;
592
 
                            atom_stereo0D[len_stereo0D].type         = INCHI_StereoType_Tetrahedral;
593
 
                            len_stereo0D ++;
594
 
                        }
595
 
                        /* isotopic h, d, t */
596
 
                        for ( k = 0; k < NUM_H_ISOTOPES; k ++ ) {
597
 
                            if ( *p == szIsoH[k] ) {
598
 
                                NUM_ISO_Hk(atom,i,k) = 1;
599
 
                                p ++;
600
 
                                if ( isdigit( UCINT *p ) ) {
601
 
                                    NUM_ISO_Hk(atom,i,k) = (char)strtol( p, &q, 10 );
602
 
                                    p = q;
603
 
                                }
604
 
                            }
605
 
                        }
606
 
                        i ++;
607
 
                    }
608
 
                    if ( !bItemIsOver || i != num_atoms || s && p != s ) {
609
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error */
610
 
                        *err      = INCHI_INP_ERROR_ERR;
611
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong number of atoms");
612
 
                        goto bypass_end_of_INChI_plain;
613
 
                    }
614
 
                }
615
 
                /***************** search for bonds block (plain) and read it *****************/
616
 
                /*p = szLine;*/
617
 
                sToken = sStructHdrPlnRevBn;
618
 
                lToken = sizeof(sStructHdrPlnRevBn)-1;
619
 
                /* search for sToken in the line; load next segments of the line if sToken has not found */
620
 
                p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
621
 
                               szLine, sizeof(szLine), p, &res );
622
 
                if ( !p ) {
623
 
                    num_atoms = INCHI_INP_ERROR_RET; /* error */
624
 
                    *err      = INCHI_INP_ERROR_ERR;
625
 
                    MOLFILE_ERR_SET (*err, 0, "Missing bonds data");
626
 
                    goto bypass_end_of_INChI_plain;
627
 
                } else {
628
 
                    /* bonds block started */
629
 
                    i = 1;
630
 
                    res2 = bTooLongLine2 = -1;
631
 
                    bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
632
 
                    if ( 1 == num_atoms ) {
633
 
                        /* needed because the next '/' may be still out of szLine */
634
 
                        p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
635
 
                                      szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
636
 
                    }
637
 
                    while ( i < num_atoms ) {
638
 
                        p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
639
 
                                      szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
640
 
                        if ( i >= num_atoms || s && p >= s ) {
641
 
                            break; /* end of bonds (plain) */
642
 
                        }
643
 
                        /* bond, first char */
644
 
                        if ( *p == ';' ) {
645
 
                            p ++;
646
 
                            i ++;
647
 
                            continue;
648
 
                        }
649
 
                        if ( !isalpha( UCINT *p ) ) {
650
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error */
651
 
                            *err      = INCHI_INP_ERROR_ERR;
652
 
                            MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
653
 
                            goto bypass_end_of_INChI_plain;
654
 
                        }
655
 
                        bond_char = *p ++;
656
 
                        /* bond parity */
657
 
                        switch( *p ) {
658
 
                        case '-':
659
 
                            bond_parity = INCHI_PARITY_ODD;
660
 
                            p ++;
661
 
                            break;
662
 
                        case '+':
663
 
                            bond_parity = INCHI_PARITY_EVEN;
664
 
                            p ++;
665
 
                            break;
666
 
                        case 'u':
667
 
                            bond_parity = INCHI_PARITY_UNKNOWN;
668
 
                            p ++;
669
 
                            break;
670
 
                        case '?':
671
 
                            bond_parity = INCHI_PARITY_UNDEFINED;
672
 
                            p ++;
673
 
                            break;
674
 
                        default:
675
 
                            bond_parity = 0;
676
 
                            break;
677
 
                        }
678
 
                        if ( bond_parity ) {
679
 
                            switch( *p ) {
680
 
                            case '-':
681
 
                                bond_parityNM = INCHI_PARITY_ODD;
682
 
                                p ++;
683
 
                                break;
684
 
                            case '+':
685
 
                                bond_parityNM = INCHI_PARITY_EVEN;
686
 
                                p ++;
687
 
                                break;
688
 
                            case 'u':
689
 
                                bond_parityNM = INCHI_PARITY_UNKNOWN;
690
 
                                p ++;
691
 
                                break;
692
 
                            case '?':
693
 
                                bond_parityNM = INCHI_PARITY_UNDEFINED;
694
 
                                p ++;
695
 
                                break;
696
 
                            default:
697
 
                                bond_parityNM = 0;
698
 
                                break;
699
 
                            }
700
 
                        } else {
701
 
                            bond_parityNM = 0;
702
 
                        }
703
 
 
704
 
                        /* neighbor of the current atom */
705
 
                        if ( !isdigit( UCINT *p ) ) {
706
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error */
707
 
                            *err      = INCHI_INP_ERROR_ERR;
708
 
                            MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
709
 
                            goto bypass_end_of_INChI_plain;
710
 
                        }
711
 
                        neigh = (int)strtol( p, &q, 10 )-1;
712
 
 
713
 
                        if ( i >= num_atoms || neigh >= num_atoms ) {
714
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error */
715
 
                            *err      = INCHI_INP_ERROR_ERR;
716
 
                            MOLFILE_ERR_SET (*err, 0, "Bond to nonexistent atom");
717
 
                            goto bypass_end_of_INChI_plain;
718
 
                        }
719
 
                        p = q;
720
 
                        bond_stereo1 = bond_stereo2 = 0;
721
 
 
722
 
                        /* bond type & 2D stereo */
723
 
                        switch( bond_char ) {
724
 
                        case 'v':
725
 
                            bond_type    = INCHI_BOND_TYPE_SINGLE;
726
 
                            bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1EITHER;
727
 
                            bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2EITHER;
728
 
                            break;
729
 
                        case 'V':
730
 
                            bond_type    = INCHI_BOND_TYPE_SINGLE;
731
 
                            bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2EITHER;
732
 
                            bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1EITHER;
733
 
                            break;
734
 
                        case 'w':
735
 
                            bond_type    = INCHI_BOND_TYPE_DOUBLE;
736
 
                            bond_stereo1 = 
737
 
                            bond_stereo2 = INCHI_BOND_STEREO_DOUBLE_EITHER;
738
 
                            break;
739
 
                        case 's':
740
 
                            bond_type    = INCHI_BOND_TYPE_SINGLE;
741
 
                            break;
742
 
                        case 'd':
743
 
                            bond_type    = INCHI_BOND_TYPE_DOUBLE;
744
 
                            break;
745
 
                        case 't':
746
 
                            bond_type    = INCHI_BOND_TYPE_TRIPLE;
747
 
                            break;
748
 
                        case 'a':
749
 
                            bond_type    = INCHI_BOND_TYPE_ALTERN;
750
 
                            break;
751
 
                        case 'p':
752
 
                            bond_type    =  INCHI_BOND_TYPE_SINGLE;
753
 
                            bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_1UP;
754
 
                            bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_2UP;
755
 
                            break;
756
 
                        case 'P':
757
 
                            bond_type    =  INCHI_BOND_TYPE_SINGLE;
758
 
                            bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_2UP;
759
 
                            bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_1UP;
760
 
                            break;
761
 
                        case 'n':
762
 
                            bond_type    =  INCHI_BOND_TYPE_SINGLE;
763
 
                            bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_1DOWN;
764
 
                            bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_2DOWN;
765
 
                            break;
766
 
                        case 'N':
767
 
                            bond_type    =  INCHI_BOND_TYPE_SINGLE;
768
 
                            bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_2DOWN;
769
 
                            bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_1DOWN;
770
 
                            break;
771
 
                        default:
772
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error */
773
 
                            *err      = INCHI_INP_ERROR_ERR;
774
 
                            MOLFILE_ERR_SET (*err, 0, "Wrong bond type");
775
 
                            goto bypass_end_of_INChI_plain;
776
 
                        }
777
 
                        k = AT_NUM_BONDS(atom[i]) ++;
778
 
                        atom[i].bond_type[k]   = bond_type;
779
 
                        atom[i].bond_stereo[k] = bond_stereo1;
780
 
                        atom[i].neighbor[k]    = (ATOM_NUMBER)neigh;
781
 
                        k2 = AT_NUM_BONDS(atom[neigh]) ++;
782
 
                        atom[neigh].bond_type[k2]   = bond_type;
783
 
                        atom[neigh].bond_stereo[k2] = bond_stereo2;
784
 
                        atom[neigh].neighbor[k2]    = (ATOM_NUMBER)i;
785
 
                        bond_parity |= (bond_parityNM << SB_PARITY_SHFT);
786
 
 
787
 
                        if ( bond_parity ) {
788
 
                            if ( max_len_stereo0D <= len_stereo0D ) {
789
 
                                /* realloc atom_Stereo0D */
790
 
                                inchi_Stereo0D *new_atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D+num_atoms );
791
 
                                if ( !new_atom_stereo0D ) {
792
 
                                    num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
793
 
                                    *err      = INCHI_INP_FATAL_ERR;
794
 
                                    MOLFILE_ERR_SET (*err, 0, "Out of RAM");
795
 
                                    goto bypass_end_of_INChI_plain;
796
 
                                }
797
 
                                memcpy( new_atom_stereo0D, atom_stereo0D, len_stereo0D * sizeof(*atom_stereo0D) );
798
 
                                FreeInchi_Stereo0D( &atom_stereo0D );
799
 
                                atom_stereo0D = new_atom_stereo0D;
800
 
                                max_len_stereo0D += num_atoms;
801
 
                            }
802
 
                            /* (a) i may be allene endpoint and     neigh = allene middle point or
803
 
                               (b) i may be allene middle point and neigh = allene endpoint
804
 
                               !!!!! CURRENTLY ONLY (b) IS ALLOWED !!!!!
805
 
                            */
806
 
                            atom_stereo0D[len_stereo0D].neighbor[1] = neigh; /* neigh < i */
807
 
                            atom_stereo0D[len_stereo0D].neighbor[2] = i;
808
 
                            atom_stereo0D[len_stereo0D].parity      = bond_parity;
809
 
                            atom_stereo0D[len_stereo0D].type        = INCHI_StereoType_DoubleBond; /* incl allenes & cumulenes */
810
 
                            len_stereo0D ++;
811
 
                        }
812
 
                    }
813
 
                    if ( !bItemIsOver || i != num_atoms || s && p != s ) {
814
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error */
815
 
                        *err      = INCHI_INP_ERROR_ERR;
816
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong number of bonds");
817
 
                        goto bypass_end_of_INChI_plain;
818
 
                    }
819
 
                }
820
 
                /***************** search for coordinates block (plain) **********************/
821
 
                /*p = szLine;*/
822
 
                sToken = sStructHdrPlnRevXYZ;
823
 
                lToken = sizeof(sStructHdrPlnRevXYZ)-1;
824
 
                /* search for sToken in the line; load next segments of the line if sToken has not found */
825
 
                p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
826
 
                               szLine, sizeof(szLine), p, &res );
827
 
                if ( !p ) {
828
 
                    num_atoms = INCHI_INP_ERROR_RET; /* error */
829
 
                    *err      = INCHI_INP_ERROR_ERR;
830
 
                    MOLFILE_ERR_SET (*err, 0, "Missing atom coordinates data");
831
 
                    goto bypass_end_of_INChI_plain;
832
 
                } else {
833
 
                    /* coordinates block started */
834
 
                    if ( pszCoord = (MOL_COORD*)inchi_malloc(inchi_max(num_atoms,1) * sizeof(MOL_COORD)) ) {
835
 
                        memset( pszCoord, ' ', inchi_max(num_atoms,1) * sizeof(MOL_COORD));
836
 
                    } else {
837
 
                        num_atoms = INCHI_INP_FATAL_RET; /* allocation error */
838
 
                        *err      = INCHI_INP_FATAL_ERR;
839
 
                        MOLFILE_ERR_SET (*err, 0, "Out of RAM");
840
 
                        goto bypass_end_of_INChI_plain;
841
 
                    }
842
 
                    i = 0;
843
 
                    res2 = bTooLongLine2 = -1;
844
 
                    bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
845
 
                    while ( i < num_atoms ) {
846
 
                        p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
847
 
                                      szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
848
 
                        if ( i >= num_atoms || s && p >= s ) {
849
 
                            break; /* end of bonds (plain) */
850
 
                        }
851
 
 
852
 
                        /* coord, first char */
853
 
                        if ( *p == ';' ) {
854
 
                            for ( k = 0; k < NUM_COORD; k ++ ) {
855
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
856
 
                            }
857
 
                            p ++;
858
 
                            i ++;
859
 
                            continue;
860
 
                        }
861
 
                        for ( k = 0; k < 3; k ++ ) {
862
 
                            double xyz;
863
 
                            bNonZeroXYZ = 0;
864
 
                            if ( *p == ';' ) {
865
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
866
 
                                xyz = 0.0;
867
 
                            } else
868
 
                            if ( *p == ',' ) {
869
 
                                /* empty */
870
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
871
 
                                xyz = 0.0;
872
 
                                p ++;
873
 
                            } else {
874
 
                                xyz = strtod( p, &q );
875
 
                                bNonZeroXYZ = fabs(xyz) > MIN_BOND_LENGTH;
876
 
                                if ( q != NULL ) {
877
 
                                    memcpy( pszCoord[i]+LEN_COORD*k, p, q-p );
878
 
                                    if ( *q == ',' )
879
 
                                        q ++;
880
 
                                    p = q;
881
 
                                } else {
882
 
                                    pszCoord[i][LEN_COORD*k + 4] = '0';
883
 
                                }
884
 
                            }
885
 
                            switch( k ) {
886
 
                            case 0:
887
 
                                atom[i].x = xyz;
888
 
                                b2D |= bNonZeroXYZ;
889
 
                                break;
890
 
                            case 1:
891
 
                                atom[i].y = xyz;
892
 
                                b2D |= bNonZeroXYZ;
893
 
                                break;
894
 
                            case 2:
895
 
                                b3D |= bNonZeroXYZ;
896
 
                                atom[i].z = xyz;
897
 
                                break;
898
 
                            }
899
 
                        }
900
 
                        if ( *p == ';' ) {
901
 
                            p ++; /* end of this triple of coordinates */
902
 
                            i ++;
903
 
                        } else {
904
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
905
 
                            *err      = INCHI_INP_ERROR_ERR;
906
 
                            MOLFILE_ERR_SET (*err, 0, "Wrong atom coordinates data");
907
 
                            goto bypass_end_of_INChI_plain;
908
 
                        }
909
 
                    }
910
 
                    if ( !bItemIsOver || s && p != s || i != num_atoms ) {
911
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error */
912
 
                        *err      = INCHI_INP_ERROR_ERR;
913
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong number of coordinates");
914
 
                        goto bypass_end_of_INChI_plain;
915
 
                    }
916
 
                } /* end of coordinates */
917
 
                /* set special valences and implicit H (xml) */
918
 
                b23D = b2D | b3D;
919
 
                b2D = b3D = 0;
920
 
                if ( at ) {
921
 
                    if ( !*at ) {
922
 
                        int a1, a2, n1, n2, valence;
923
 
                        int chem_bonds_valence;
924
 
                        int    nX=0, nY=0, nZ=0, nXYZ;
925
 
                        *at = atom;
926
 
                        /* special valences */
927
 
                        for ( bNonMetal = 0; bNonMetal < 1; bNonMetal ++ ) {
928
 
                            for ( a1 = 0; a1 < num_atoms; a1 ++ ) {
929
 
                                int num_bond_type[MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE + 1];
930
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
931
 
#else
932
 
                                int bHasMetalNeighbor=0;
933
 
#endif
934
 
                                memset( num_bond_type, 0, sizeof(num_bond_type) );
935
 
 
936
 
                                valence = AT_BONDS_VAL(atom, a1); /*  save atom valence if available */
937
 
                                AT_BONDS_VAL(atom, a1) = 0;
938
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
939
 
#else
940
 
                                atom[a1].orig_at_number = a1+1;
941
 
#endif
942
 
                                nX = nY = nZ = 0;
943
 
                                for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
944
 
                                    bond_type = atom[a1].bond_type[n1] - MIN_INPUT_BOND_TYPE;
945
 
                                    if (  bond_type < 0 || bond_type > MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE ) {
946
 
                                        bond_type = 0;
947
 
                                        MOLFILE_ERR_SET (*err, 0, "Unknown bond type in InChI aux assigned as a single bond");
948
 
                                    }
949
 
 
950
 
                                    num_bond_type[ bond_type ] ++;
951
 
                                    nNumBonds ++;
952
 
                                    if ( b23D ) {
953
 
                                        neigh = atom[a1].neighbor[n1];
954
 
                                        nX |= (fabs(atom[a1].x - atom[neigh].x) > MIN_BOND_LENGTH);
955
 
                                        nY |= (fabs(atom[a1].y - atom[neigh].y) > MIN_BOND_LENGTH);
956
 
                                        nZ |= (fabs(atom[a1].z - atom[neigh].z) > MIN_BOND_LENGTH);
957
 
                                    }
958
 
                                }
959
 
                                chem_bonds_valence = 0;
960
 
                                for ( n1 = 0; MIN_INPUT_BOND_TYPE + n1 <= 3 && MIN_INPUT_BOND_TYPE + n1 <= MAX_INPUT_BOND_TYPE; n1 ++ ) {
961
 
                                    chem_bonds_valence += (MIN_INPUT_BOND_TYPE + n1) * num_bond_type[n1];
962
 
                                }
963
 
                                if ( MIN_INPUT_BOND_TYPE <= INCHI_BOND_TYPE_ALTERN && INCHI_BOND_TYPE_ALTERN <= MAX_INPUT_BOND_TYPE &&
964
 
                                     ( n2 = num_bond_type[INCHI_BOND_TYPE_ALTERN-MIN_INPUT_BOND_TYPE] ) ) {
965
 
                                    /* accept input aromatic bonds for now */
966
 
                                    switch ( n2 ) {
967
 
                                    case 2:
968
 
                                        chem_bonds_valence += 3;  /* =A- */
969
 
                                        break;
970
 
                                    case 3:
971
 
                                        chem_bonds_valence += 4;  /* =A< */
972
 
                                        break;
973
 
                                    default:
974
 
                                        /*  if 1 or >= 4 aromatic bonds then replace such bonds with single bonds */
975
 
                                        for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
976
 
                                            if ( atom[a1].bond_type[n1] == INCHI_BOND_TYPE_ALTERN ) {
977
 
                                                ATOM_NUMBER *p1;
978
 
                                                a2 = atom[a1].neighbor[n1];
979
 
                                                p1 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
980
 
                                                if ( p1 ) {
981
 
                                                    atom[a1].bond_type[n1] = 
982
 
                                                    atom[a2].bond_type[p1-atom[a2].neighbor] = INCHI_BOND_TYPE_SINGLE;
983
 
                                                } else {
984
 
                                                    *err = -2;  /*  Program error */
985
 
                                                    MOLFILE_ERR_SET (*err, 0, "Program error interpreting InChI aux");
986
 
                                                    num_atoms = 0;
987
 
                                                    goto bypass_end_of_INChI_plain; /*  no structure */
988
 
                                                }
989
 
                                            }
990
 
                                        }
991
 
                                        chem_bonds_valence += n2;
992
 
                                        *err |= 32; /*  Unrecognized aromatic bond(s) replaced with single */
993
 
                                        MOLFILE_ERR_SET (*err, 0, "Atom has 1 or more than 3 aromatic bonds");
994
 
                                        break;
995
 
                                    }
996
 
                                }
997
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
998
 
                                /*************************************************************************************
999
 
                                 *
1000
 
                                 *  Set number of hydrogen atoms
1001
 
                                 */
1002
 
                                {
1003
 
                                    int num_iso_H;
1004
 
                                    num_iso_H = atom[a1].num_iso_H[1] + atom[a1].num_iso_H[2] + atom[a1].num_iso_H[3];
1005
 
                                    if ( valence == ISOLATED_ATOM ) {
1006
 
                                        atom[a1].num_iso_H[0] = 0;
1007
 
                                    } else
1008
 
                                    if ( valence && valence >= chem_bonds_valence ) {
1009
 
                                        atom[a1].num_iso_H[0] = valence - chem_bonds_valence;
1010
 
                                    } else
1011
 
                                    if ( valence || bDoNotAddH ) {
1012
 
                                        atom[a1].num_iso_H[0] = 0;
1013
 
                                    } else
1014
 
                                    if ( !bDoNotAddH ) {
1015
 
                                        atom[a1].num_iso_H[0] = -1; /* auto add H */
1016
 
                                    }
1017
 
                                }
1018
 
#else                                
1019
 
                                /* added 2006-07-19 to process aromatic bonds same way as from molfile */
1020
 
                                if ( n2 && !valence ) {
1021
 
                                    int num_H = NUMH(atom, a1); /* only isotopic */
1022
 
                                    int chem_valence = chem_bonds_valence;
1023
 
                                    int bUnusualValenceArom = 
1024
 
                                        detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
1025
 
                                                                    atom[a1].radical, chem_valence,
1026
 
                                                                    num_H, atom[a1].valence );
1027
 
                                    int bUnusualValenceNoArom = 
1028
 
                                        detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
1029
 
                                                                    atom[a1].radical, chem_valence-1,
1030
 
                                                                    num_H, atom[a1].valence );
1031
 
#if ( CHECK_AROMBOND2ALT == 1 )
1032
 
                                    if ( bUnusualValenceArom && !bUnusualValenceNoArom && 0 == nBondsValToMetal( atom, a1) )
1033
 
#else
1034
 
                                    if ( bUnusualValenceArom && !bUnusualValenceNoArom )
1035
 
#endif                     
1036
 
                                    {
1037
 
                                        /* typically NH in 5-member aromatic ring */
1038
 
                                        chem_bonds_valence --;
1039
 
                                    }
1040
 
                                } else
1041
 
                                if ( n2 && valence ) {
1042
 
                                    /* atom has aromatic bonds AND the chemical valence is known */
1043
 
                                    int num_H = NUMH(atom, a1);
1044
 
                                    int chem_valence = chem_bonds_valence + num_H;
1045
 
                                    if ( valence == chem_valence-1 ) {
1046
 
                                        /* typically NH in 5-member aromatic ring */
1047
 
                                        chem_bonds_valence --;
1048
 
                                    }
1049
 
                                }
1050
 
 
1051
 
                                atom[a1].chem_bonds_valence = chem_bonds_valence;
1052
 
                                atom[a1].num_H = get_num_H( atom[a1].elname, atom[a1].num_H, atom[a1].num_iso_H, atom[a1].charge, atom[a1].radical,
1053
 
                                                          atom[a1].chem_bonds_valence,
1054
 
                                                          valence,
1055
 
                                                          0, bDoNotAddH, bHasMetalNeighbor );
1056
 
#endif
1057
 
                            }
1058
 
                        }
1059
 
                        nNumBonds /= 2;
1060
 
                        if ( b23D && nNumBonds ) {
1061
 
                            nXYZ = nX+nY+nZ;
1062
 
                            b2D  = (nXYZ > 0);
1063
 
                            b3D  = (nXYZ == 3);
1064
 
                            *num_dimensions = b3D? 3 : b2D? 2 : 0;
1065
 
                            *num_bonds = nNumBonds;
1066
 
                        }
1067
 
                        /*======= 0D parities =================================*/
1068
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1069
 
                        if ( len_stereo0D > 0 && atom_stereo0D && stereo0D ) {
1070
 
                            *stereo0D     = atom_stereo0D;
1071
 
                            *num_stereo0D = len_stereo0D;
1072
 
                        } else {
1073
 
                            FreeInchi_Stereo0D( &atom_stereo0D );
1074
 
                            *num_stereo0D = len_stereo0D = 0;
1075
 
                        }
1076
 
#endif
1077
 
                        for ( i = 0; i < len_stereo0D; i ++ ) {
1078
 
                            ATOM_NUMBER *p1, *p2;
1079
 
                            int     sb_ord_from_a1 = -1, sb_ord_from_a2 = -1, bEnd1 = 0, bEnd2 = 0;
1080
 
                            switch( atom_stereo0D[i].type ) {
1081
 
 
1082
 
                            case INCHI_StereoType_Tetrahedral:
1083
 
                                a1 = atom_stereo0D[i].central_atom;
1084
 
                                if ( atom_stereo0D[i].parity && (AT_NUM_BONDS(atom[a1]) == 3 || AT_NUM_BONDS(atom[a1]) == 4) ) {
1085
 
                                    int ii, kk = 0;
1086
 
                                    if ( AT_NUM_BONDS(atom[a1]) == 3 ) {
1087
 
                                        atom_stereo0D[i].neighbor[kk++] = a1;
1088
 
                                    }
1089
 
                                    for ( ii = 0; ii < AT_NUM_BONDS(atom[a1]); ii ++ ) {
1090
 
                                        atom_stereo0D[i].neighbor[kk++] = atom[a1].neighbor[ii];
1091
 
                                    }
1092
 
                                }
1093
 
                            
1094
 
                            break;
1095
 
 
1096
 
                            case INCHI_StereoType_DoubleBond:
1097
 
#define MAX_CHAIN_LEN 20
1098
 
                                a1 = atom_stereo0D[i].neighbor[1];
1099
 
                                a2 = atom_stereo0D[i].neighbor[2];
1100
 
                                p1 = IN_NEIGH_LIST( atom[a1].neighbor, (ATOM_NUMBER)a2, AT_NUM_BONDS(atom[a1]) );
1101
 
                                p2 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
1102
 
                                if ( !p1 || !p2 ) {
1103
 
                                    atom_stereo0D[i].type = INCHI_StereoType_None;
1104
 
                                    atom_stereo0D[i].central_atom = NO_ATOM;
1105
 
                                    atom_stereo0D[i].neighbor[0] =
1106
 
                                    atom_stereo0D[i].neighbor[3] = -1;
1107
 
                                    *err |= 64; /* Error in cumulene stereo */
1108
 
                                    MOLFILE_ERR_SET (*err, 0, "0D stereobond not recognized");
1109
 
                                    break;
1110
 
                                }
1111
 
                                /* streobond, allene, or cumulene */
1112
 
 
1113
 
                                sb_ord_from_a1 = p1 - atom[a1].neighbor;
1114
 
                                sb_ord_from_a2 = p2 - atom[a2].neighbor;
1115
 
                                
1116
 
                                if (  AT_NUM_BONDS(atom[a1]) == 2 &&
1117
 
                                      atom[a1].bond_type[0] + atom[a1].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1118
 
                                      0 == inchi_NUMH2(atom, a1) &&
1119
 
                                     (AT_NUM_BONDS(atom[a2]) != 2 ||
1120
 
                                      atom[a2].bond_type[0] + atom[a2].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
1121
 
                                    bEnd2 = 1; /* a2 is the end-atom, a1 is middle atom */   
1122
 
                                }
1123
 
                                if (  AT_NUM_BONDS(atom[a2]) == 2 &&
1124
 
                                      atom[a2].bond_type[0] + atom[a2].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1125
 
                                      0 == inchi_NUMH2(atom, a2) &&
1126
 
                                     (AT_NUM_BONDS(atom[a1]) != 2 ||
1127
 
                                      atom[a1].bond_type[0] + atom[a1].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
1128
 
                                    bEnd1 = 1; /* a1 is the end-atom, a2 is middle atom */   
1129
 
                                }
1130
 
                                
1131
 
                                if ( bEnd2 + bEnd1 == 1 ) {
1132
 
                                    /* allene or cumulene */
1133
 
                                    ATOM_NUMBER  chain[MAX_CHAIN_LEN+1], prev, cur, next;
1134
 
                                    if ( bEnd2 && !bEnd1 ) {
1135
 
                                        cur = a1;
1136
 
                                        a1 = a2;
1137
 
                                        a2 = cur;
1138
 
                                        sb_ord_from_a1 = sb_ord_from_a2;
1139
 
                                    }
1140
 
                                    sb_ord_from_a2 = -1;
1141
 
                                    cur  = a1;
1142
 
                                    next = a2;
1143
 
                                    len = 0;
1144
 
                                    chain[len++] = cur;
1145
 
                                    chain[len++] = next;
1146
 
                                    while ( len < MAX_CHAIN_LEN ) { /* arbitrary very high upper limit to prevent infinite loop */
1147
 
                                        prev = cur;
1148
 
                                        cur  = next;
1149
 
                                            /* follow double bond path && avoid going back */
1150
 
                                        if ( AT_NUM_BONDS(atom[cur]) == 2 &&
1151
 
                                             atom[cur].bond_type[0]+atom[cur].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1152
 
                                             0 == inchi_NUMH2(atom, cur) ) {
1153
 
                                            next     = atom[cur].neighbor[atom[cur].neighbor[0] == prev];
1154
 
                                            chain[len++] = next;
1155
 
                                        } else {
1156
 
                                            break;
1157
 
                                        }
1158
 
                                    }
1159
 
                                    if ( len > 2 &&
1160
 
                                         (p2 = IN_NEIGH_LIST( atom[cur].neighbor, (ATOM_NUMBER)prev, AT_NUM_BONDS(atom[cur]))) ) {
1161
 
                                        sb_ord_from_a2 = p2 - atom[cur].neighbor;
1162
 
                                        a2 = cur;
1163
 
                                        /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1164
 
                                        atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[sb_ord_from_a1 == 0];
1165
 
                                        atom_stereo0D[i].neighbor[1] = a1;
1166
 
                                        atom_stereo0D[i].neighbor[2] = a2;
1167
 
                                        atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[sb_ord_from_a2 == 0];
1168
 
                                        if ( len % 2 ) {
1169
 
                                            atom_stereo0D[i].central_atom = chain[len/2];
1170
 
                                            atom_stereo0D[i].type         = INCHI_StereoType_Allene; 
1171
 
                                        } else {
1172
 
                                            atom_stereo0D[i].central_atom = NO_ATOM;
1173
 
                                        }
1174
 
                                    } else {
1175
 
                                        /* error */
1176
 
                                        atom_stereo0D[i].type = INCHI_StereoType_None;
1177
 
                                        atom_stereo0D[i].central_atom = NO_ATOM;
1178
 
                                        atom_stereo0D[i].neighbor[0] =
1179
 
                                        atom_stereo0D[i].neighbor[3] = -1;
1180
 
                                        *err |= 64; /* Error in cumulene stereo */
1181
 
                                        MOLFILE_ERR_SET (*err, 0, "Cumulene stereo not recognized (0D)");
1182
 
 
1183
 
                                    }
1184
 
#undef MAX_CHAIN_LEN 
1185
 
                                } else {
1186
 
                                    /****** a normal possibly stereogenic bond -- not an allene or cumulene *******/
1187
 
                                    /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1188
 
                                    sb_ord_from_a1 = p1 - atom[a1].neighbor;
1189
 
                                    sb_ord_from_a2 = p2 - atom[a2].neighbor;
1190
 
                                    atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[p1 == atom[a1].neighbor];
1191
 
                                    atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[p2 == atom[a2].neighbor];
1192
 
                                    atom_stereo0D[i].central_atom = NO_ATOM;
1193
 
                                }
1194
 
                                if ( atom_stereo0D[i].type != INCHI_StereoType_None &&
1195
 
                                     sb_ord_from_a1 >= 0 && sb_ord_from_a2 >= 0 &&
1196
 
                                     ATOM_PARITY_WELL_DEF( SB_PARITY_2(atom_stereo0D[i].parity) ) ) {
1197
 
                                    /* Detected well-defined disconnected stereo
1198
 
                                     * locate first non-metal neighbors */
1199
 
                                    int    a, n, j, /* k,*/ sb_ord, cur_neigh, min_neigh;
1200
 
                                    for ( k = 0; k < 2; k ++ ) {
1201
 
                                        a      = k? atom_stereo0D[i].neighbor[2] : atom_stereo0D[i].neighbor[1];
1202
 
                                        sb_ord = k? sb_ord_from_a2 : sb_ord_from_a1;
1203
 
                                        min_neigh = num_atoms;
1204
 
                                        for ( n  = j = 0; j < AT_NUM_BONDS(atom[a]); j ++ ) {
1205
 
                                            cur_neigh = atom[a].neighbor[j];
1206
 
                                            if ( j != sb_ord && !IS_METAL_ATOM(atom, cur_neigh) ) {
1207
 
                                                min_neigh = inchi_min( cur_neigh, min_neigh );
1208
 
                                            }
1209
 
                                        }
1210
 
                                        if ( min_neigh < num_atoms ) {
1211
 
                                            atom_stereo0D[i].neighbor[k?3:0] = min_neigh;
1212
 
                                        } else {
1213
 
                                            MOLFILE_ERR_SET (*err, 0, "Cannot find non-metal stereobond neighor (0D)");
1214
 
                                        }
1215
 
                                    }
1216
 
                                }
1217
 
 
1218
 
                                break;
1219
 
                            }
1220
 
                        }
1221
 
                        /* end of 0D parities extraction */
1222
 
/*exit_cycle:;*/
1223
 
                    }
1224
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1225
 
#else
1226
 
                    /* transfer atom_stereo0D[] to atom[] */
1227
 
                    if ( len_stereo0D ) {
1228
 
                        Extract0DParities( atom, num_atoms, atom_stereo0D, len_stereo0D, 
1229
 
                            pStrErr, err, vABParityUnknown );
1230
 
                    }
1231
 
#endif
1232
 
                    if ( pInpAtomFlags ) {
1233
 
                        /* save chirality flag */
1234
 
                        *pInpAtomFlags |= InpAtomFlags;
1235
 
                    }
1236
 
                } else
1237
 
                if ( atom ) {
1238
 
                    inchi_free( atom );
1239
 
                    atom = NULL;
1240
 
                }
1241
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1242
 
#else
1243
 
#if( FIX_READ_AUX_MEM_LEAK == 1 )
1244
 
                /* 2005-08-04 avoid memory leak */
1245
 
                if ( atom_stereo0D && !(stereo0D && *stereo0D == atom_stereo0D) ) {
1246
 
                    FreeInchi_Stereo0D( &atom_stereo0D );
1247
 
                }
1248
 
#endif
1249
 
                if ( szCoord ) {
1250
 
                    *szCoord = pszCoord;
1251
 
                    pszCoord = NULL;
1252
 
                } else
1253
 
#endif
1254
 
                if ( pszCoord ) {
1255
 
                    inchi_free( pszCoord );
1256
 
                    pszCoord = NULL;
1257
 
                }
1258
 
                goto bypass_end_of_INChI_plain;
1259
 
                /*return num_atoms;*/
1260
 
            }
1261
 
        }
1262
 
        if ( atom_stereo0D ) {
1263
 
            FreeInchi_Stereo0D( &atom_stereo0D );
1264
 
        }
1265
 
        /* end of struct. reading cycle */
1266
 
        if ( res <= 0 ) {
1267
 
            if ( *err == INCHI_INP_ERROR_ERR ) {
1268
 
                return num_atoms;
1269
 
            }
1270
 
            *err = INCHI_INP_EOF_ERR;
1271
 
            return INCHI_INP_EOF_RET; /* no more data */
1272
 
        }
1273
 
bypass_end_of_INChI_plain:
1274
 
        /* cleanup */
1275
 
        if ( num_atoms == INCHI_INP_ERROR_RET && atom_stereo0D ) {
1276
 
            if ( stereo0D && *stereo0D == atom_stereo0D ) {
1277
 
                *stereo0D     = NULL;
1278
 
                *num_stereo0D = 0;
1279
 
            }
1280
 
            FreeInchi_Stereo0D( &atom_stereo0D );
1281
 
        }
1282
 
        while ( bTooLongLine && 
1283
 
                0 < inchi_ios_getsTab1( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) {
1284
 
            ;
1285
 
        }
1286
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1287
 
        /* cleanup */
1288
 
        if ( !*at ) {
1289
 
            if ( atom ) {
1290
 
                inchi_free( atom );
1291
 
                atom = NULL;
1292
 
            }
1293
 
            if ( pszCoord ) {
1294
 
                inchi_free( pszCoord );
1295
 
                pszCoord = NULL;
1296
 
            }
1297
 
        }
1298
 
#endif
1299
 
 
1300
 
        return num_atoms;
1301
 
    }
1302
 
    
1303
 
    /***********************************************************/
1304
 
    /*   extract reversibility info from xml text INChI format */
1305
 
    /*                                                         */
1306
 
    /*   OBSOLETE CODE because InChI output in XML             */
1307
 
    /*      does not exist anymore. Unsupported.               */
1308
 
    /*                                                         */
1309
 
    /***********************************************************/
1310
 
    if ( nInputType == INPUT_INCHI_XML ) {
1311
 
        /* xml tags */
1312
 
        static const char sStructHdrXml[]         = "<structure";
1313
 
        static const char sStructHdrXmlEnd[]      = "</structure";
1314
 
        static const char sStructHdrXmlNumber[]   = "number=\"";
1315
 
        static const char sStructHdrXmlIdName[]   = "id.name=\"";
1316
 
        static const char sStructHdrXmlIdValue[]  = "id.value=\"";
1317
 
        static const char sStructMsgXmlErr[]      = "<message type=\"error (no InChI)\" value=\"";
1318
 
        static const char sStructMsgXmlErrFatal[] = "<message type=\"fatal (aborted)\" value=\"";
1319
 
        static const char sStructRevXmlRevHdr[]   = "<reversibility>";
1320
 
        static const char sStructRevXmlRevAt[]    = "<atoms>";
1321
 
        static const char sStructRevXmlRevAtEnd[] = "</atoms>";
1322
 
        static const char sStructRevXmlRevBn[]    = "<bonds>";
1323
 
        static const char sStructRevXmlRevBnEnd[] = "</bonds>";
1324
 
        static const char sStructRevXmlRevXYZ[]   = "<xyz>";
1325
 
        static const char sStructRevXmlRevXYZEnd[]= "</xyz>";
1326
 
        static const char sStructAuxXml[]         = "<identifier.auxiliary-info";
1327
 
        static const char sStructAuxXmlEnd[]      = "</identifier.auxiliary-info";
1328
 
        int         bInTheAuxInfo           = 0;
1329
 
 
1330
 
        while ( 0 < (res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) ) {
1331
 
        
1332
 
            /********************* find and interpret structure header ************/
1333
 
            if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) ) {
1334
 
                num_struct = 1;
1335
 
                p = szLine + sizeof(sStructHdrXml)-1;
1336
 
                longID = 0;
1337
 
                num_atoms = 0;
1338
 
                /* structure number */
1339
 
                if ( q = strstr( p, sStructHdrXmlNumber ) ) {
1340
 
                    p = q + sizeof(sStructHdrXmlNumber)-1;
1341
 
                    longID = strtol( p, &q, 10);
1342
 
                    if ( q && *q == '\"' )
1343
 
                        p = q+1;
1344
 
                }
1345
 
                if ( pSdfLabel ) {
1346
 
                    pSdfLabel[0] = '\0';
1347
 
                }
1348
 
                if ( pSdfValue ) {
1349
 
                    pSdfValue[0] = '\0';
1350
 
                }
1351
 
                /* pSdfLabel */
1352
 
                if ( q = strstr( p, sStructHdrXmlIdName ) ) {
1353
 
                    p = q + sizeof(sStructHdrXmlIdName)-1;
1354
 
                    q = strchr( p, '\"' );
1355
 
                    if ( q ) {
1356
 
                        len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
1357
 
                        if ( pSdfLabel ) {
1358
 
                            mystrncpy( pSdfLabel, p, len );
1359
 
                        }
1360
 
                        p = q+1;
1361
 
                    }
1362
 
                }
1363
 
                /* pSdfValue */
1364
 
                if ( q = strstr( p, sStructHdrXmlIdValue ) ) {
1365
 
                    p = q + sizeof(sStructHdrXmlIdValue)-1;
1366
 
                    q = strchr( p, '\"' );
1367
 
                    if ( q ) {
1368
 
                        len = inchi_min( q-p+1, MAX_SDF_VALUE-1);
1369
 
                        if ( pSdfValue ) {
1370
 
                            mystrncpy( pSdfValue, p, len );
1371
 
                        }
1372
 
                        p = q+1;
1373
 
                    }
1374
 
                }
1375
 
                if ( Id )
1376
 
                    *Id = longID;
1377
 
                bHeaderRead = 1;
1378
 
                bErrorMsg = bRestoreInfo = 0;
1379
 
            } else
1380
 
            if ( bHeaderRead && (bFatal=0, len=sizeof(sStructMsgXmlErr)-1,      !memcmp(szLine, sStructMsgXmlErr, len)) ||
1381
 
                 bHeaderRead && (len=sizeof(sStructMsgXmlErrFatal)-1, !memcmp(szLine, sStructMsgXmlErrFatal, len))&&(bFatal=1)) {
1382
 
                p = szLine+len;
1383
 
                q = strchr( p, '\"' );
1384
 
                if ( q && !bFindNext ) {
1385
 
                    int c;
1386
 
                    bErrorMsg = 1;
1387
 
                    pStrErr[0] = '\0';
1388
 
                    c = *q;
1389
 
                    *q = '\0';
1390
 
                    MOLFILE_ERR_SET (*err, 0, p);
1391
 
                    *q = c;
1392
 
                }
1393
 
                *err      = bFatal? INCHI_INP_FATAL_ERR : INCHI_INP_ERROR_ERR;
1394
 
                num_atoms = bFatal? INCHI_INP_FATAL_RET : INCHI_INP_ERROR_RET;
1395
 
                goto bypass_end_of_INChI;
1396
 
            } else
1397
 
            if ( bHeaderRead && !memcmp(szLine, sStructAuxXml, sizeof(sStructAuxXml)-1) ) {
1398
 
                bInTheAuxInfo = 1;
1399
 
            } else
1400
 
            if ( bHeaderRead && !memcmp(szLine, sStructAuxXmlEnd, sizeof(sStructAuxXmlEnd)-1) ) {
1401
 
                *err      = INCHI_INP_ERROR_ERR;
1402
 
                num_atoms = INCHI_INP_ERROR_RET;
1403
 
                MOLFILE_ERR_SET (*err, 0, "Missing reversibility info" );
1404
 
                goto bypass_end_of_INChI; /* reversibility info not found */
1405
 
            } else
1406
 
            if ( bHeaderRead && bInTheAuxInfo && !memcmp(szLine, sStructRevXmlRevHdr, sizeof(sStructRevXmlRevHdr)-1) ) {
1407
 
                /***********************  atoms xml ***************************/
1408
 
                num_struct = 1;
1409
 
                res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1410
 
                if ( res <= 0 ) {
1411
 
                    num_atoms = INCHI_INP_EOF_RET; /* no data, probably end of file */
1412
 
                    *err      = INCHI_INP_EOF_ERR;
1413
 
                    goto bypass_end_of_INChI;
1414
 
                }
1415
 
                if ( memcmp(szLine, sStructRevXmlRevAt, sizeof(sStructRevXmlRevAt)-1) ) {
1416
 
                    bHeaderRead = 0; /* invalid reversibility info; look for another header */
1417
 
                    continue;
1418
 
                }
1419
 
                /* read (the head of) the atoms line */
1420
 
                res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1421
 
                if ( res <= 0 ) {
1422
 
                    num_atoms = INCHI_INP_EOF_RET; /* no data */
1423
 
                    *err      = INCHI_INP_EOF_ERR;
1424
 
                    goto bypass_end_of_INChI;
1425
 
                }
1426
 
                p = szLine;
1427
 
                num_atoms = strtol( p, &q, 10 );
1428
 
                if ( !num_atoms || !q || !*q ) {
1429
 
                    num_atoms = INCHI_INP_EOF_RET; /* no atom data */
1430
 
                    *err      = INCHI_INP_EOF_ERR;
1431
 
                    goto bypass_end_of_INChI;
1432
 
                }
1433
 
                p = q;
1434
 
                /* Molfile chirality flag */
1435
 
                switch( *p ) {
1436
 
                case 'c':
1437
 
                    InpAtomFlags |= FLAG_INP_AT_CHIRAL;
1438
 
                    p ++;
1439
 
                    break;
1440
 
                case 'n':
1441
 
                    InpAtomFlags |= FLAG_INP_AT_NONCHIRAL;
1442
 
                    p ++;
1443
 
                    break;
1444
 
                }
1445
 
                if ( at && *at ) {
1446
 
                    if ( num_atoms > max_num_at ) {
1447
 
                        inchi_free( *at );
1448
 
                        *at = NULL;
1449
 
                    } else {
1450
 
                        memset( *at, 0, max_num_at * sizeof( **at ) );
1451
 
                        atom = *at;
1452
 
                    }
1453
 
                }
1454
 
                if ( !at || !*at ) {
1455
 
                    atom = Create_Atom( num_atoms+1 );
1456
 
                    if ( !atom ) {
1457
 
                        num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1458
 
                        *err      = INCHI_INP_FATAL_ERR;
1459
 
                        MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1460
 
                        goto bypass_end_of_INChI;
1461
 
                    }
1462
 
                }
1463
 
                if ( stereo0D && *stereo0D ) {
1464
 
                    if ( num_atoms > max_len_stereo0D ) {
1465
 
                        FreeInchi_Stereo0D( stereo0D );
1466
 
                    } else {
1467
 
                        memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ) );
1468
 
                        atom_stereo0D = *stereo0D;
1469
 
                    }
1470
 
                }
1471
 
                if ( !stereo0D || !*stereo0D ) {
1472
 
                    max_len_stereo0D = num_atoms+1;
1473
 
                    atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D );
1474
 
                    if ( !atom_stereo0D ) {
1475
 
                        num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1476
 
                        *err      = INCHI_INP_FATAL_ERR;
1477
 
                        MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1478
 
                        goto bypass_end_of_INChI;
1479
 
                    }
1480
 
                }
1481
 
 
1482
 
                i = 0;
1483
 
                bItemIsOver = 0;
1484
 
                res2 = bTooLongLine2 = -1;
1485
 
                
1486
 
                /* read all atoms xml */
1487
 
                while ( i < num_atoms ) {
1488
 
                    pos = p - szLine;
1489
 
                    if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1490
 
                        /* load next line if possible */
1491
 
                        res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1492
 
                        if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevAtEnd, sizeof(sStructRevXmlRevAtEnd)-1) ) {
1493
 
                            if ( pos ) {
1494
 
                                res -= pos;  /* number of chars left to process in szLine */
1495
 
                                memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1496
 
                            }
1497
 
                            memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1498
 
                            res += res2;
1499
 
                            szLine[res] = '\0';
1500
 
                            bTooLongLine = bTooLongLine2;
1501
 
                            p = szLine;
1502
 
                        } else {
1503
 
                            bItemIsOver = 1;
1504
 
                        }
1505
 
                    }
1506
 
                    /* element, first char */
1507
 
                    if ( !isalpha( UCINT *p ) || !isupper( UCINT *p ) || i >= num_atoms ) {
1508
 
                        bHeaderRead = 0; /* wrong atom data */
1509
 
                        num_atoms = INCHI_INP_ERROR_RET; /* was 0, error */
1510
 
                        *err      = INCHI_INP_ERROR_ERR;     /* 40 */
1511
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong atoms data");
1512
 
                        goto bypass_end_of_INChI;
1513
 
                    }
1514
 
                    atom[i].elname[0] = *p ++;
1515
 
                    /* element, second char */
1516
 
                    if ( isalpha( UCINT *p ) && islower( UCINT *p ) ) {
1517
 
                        atom[i].elname[1] = *p ++;
1518
 
                    }
1519
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1520
 
#else
1521
 
                    atom[i].el_number = get_periodic_table_number( atom[i].elname );
1522
 
#endif
1523
 
                    /* bonds' valence */
1524
 
                    if ( isdigit( UCINT *p ) ) {
1525
 
                        AT_BONDS_VAL(atom,i) = (char)strtol( p, &q, 10 );
1526
 
                        if ( !AT_BONDS_VAL(atom,i) )
1527
 
                            AT_BONDS_VAL(atom,i) = ISOLATED_ATOM; /* same convention as in MOLfile, found zero bonds valence */
1528
 
                        p = q;
1529
 
                    }
1530
 
                    /* charge */
1531
 
                    atom[i].charge = (*p == '+')? 1 : (*p == '-')? -1 : 0;
1532
 
                    if ( atom[i].charge ) {
1533
 
                        p ++;
1534
 
                        if ( isdigit( UCINT *p ) ) {
1535
 
                            atom[i].charge *= (S_CHAR)(strtol( p, &q, 10 ) & CHAR_MASK);
1536
 
                            p = q;
1537
 
                        }
1538
 
                    }
1539
 
                    /* radical */
1540
 
                    if ( *p == '.' ) {
1541
 
                        p ++;
1542
 
                        if ( isdigit( UCINT *p ) ) {
1543
 
                            atom[i].radical = (S_CHAR)strtol( p, &q, 10 );
1544
 
                            p = q;
1545
 
                        }
1546
 
                    }
1547
 
                    /* isotopic mass */
1548
 
                    if ( *p == 'i' ) {
1549
 
                        p ++;
1550
 
                        if ( isdigit( UCINT *p ) ) {
1551
 
                            int mw = strtol( p, &q, 10 );
1552
 
                            p = q;
1553
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1554
 
                            atom[i].isotopic_mass = mw;
1555
 
#else
1556
 
                            mw -= get_atw_from_elnum( atom[i].el_number );
1557
 
                            if ( mw >= 0 )
1558
 
                                mw ++;
1559
 
                            atom[i].iso_atw_diff = mw;
1560
 
#endif
1561
 
                        }
1562
 
                    }
1563
 
                    /* parity */
1564
 
                    switch( *p ) {
1565
 
                    case 'o':
1566
 
                        parity = INCHI_PARITY_ODD;
1567
 
                        p ++;
1568
 
                        break;
1569
 
                    case 'e':
1570
 
                        parity = INCHI_PARITY_EVEN;
1571
 
                        p ++;
1572
 
                        break;
1573
 
                    case 'u':
1574
 
                        parity = INCHI_PARITY_UNKNOWN;
1575
 
                        p ++;
1576
 
                        break;
1577
 
                    case '?':
1578
 
                        parity = INCHI_PARITY_UNDEFINED;
1579
 
                        p ++;
1580
 
                        break;
1581
 
                    default:
1582
 
                        parity = 0;
1583
 
                        break;
1584
 
                    }
1585
 
                    if ( parity ) {
1586
 
                        atom_stereo0D[len_stereo0D].central_atom = i;
1587
 
                        atom_stereo0D[len_stereo0D].parity       = parity;
1588
 
                        atom_stereo0D[len_stereo0D].type         = INCHI_StereoType_Tetrahedral;
1589
 
                        len_stereo0D ++;
1590
 
                    }
1591
 
                    /* isotopic h, d, t */
1592
 
                    for ( k = 0; k < NUM_H_ISOTOPES; k ++ ) {
1593
 
                        if ( *p == szIsoH[k] ) {
1594
 
                            NUM_ISO_Hk(atom,i,k) = 1;
1595
 
                            p ++;
1596
 
                            if ( isdigit( UCINT *p ) ) {
1597
 
                                NUM_ISO_Hk(atom,i,k) = (char)strtol( p, &q, 10 );
1598
 
                                p = q;
1599
 
                            }
1600
 
                        }
1601
 
                    }
1602
 
                    i ++;
1603
 
                }
1604
 
                if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1605
 
                    num_atoms = INCHI_INP_ERROR_RET; /* error */
1606
 
                    *err      = INCHI_INP_ERROR_ERR;
1607
 
                    MOLFILE_ERR_SET (*err, 0, "Wrong number of atoms");
1608
 
                    goto bypass_end_of_INChI;
1609
 
                }
1610
 
                /********************** bonds xml ****************************/
1611
 
                res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1612
 
                if ( res <= 0 ) {
1613
 
                    num_atoms = 0; /* no data */
1614
 
                    goto bypass_end_of_INChI;
1615
 
                }
1616
 
                if ( memcmp(szLine, sStructRevXmlRevBn, sizeof(sStructRevXmlRevBn)-1) ) {
1617
 
                    bHeaderRead = 0; /* invalid reversibility info; look for another header */
1618
 
                    continue;
1619
 
                }
1620
 
                /* read (the head of) the xml bonds line */
1621
 
                res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1622
 
                if ( res <= 0 ) {
1623
 
                    num_atoms = INCHI_INP_ERROR_RET; /* was 0; error: no data -- eof? */
1624
 
                    *err      = INCHI_INP_ERROR_ERR;
1625
 
                    goto bypass_end_of_INChI;
1626
 
                }
1627
 
                i = 1;
1628
 
                bItemIsOver = 0;
1629
 
                res2 = bTooLongLine2 = -1;
1630
 
                p = szLine;
1631
 
                if ( !memcmp(szLine, sStructRevXmlRevBnEnd, sizeof(sStructRevXmlRevBnEnd)-1) ) {
1632
 
                    /* empty bonds section */
1633
 
                    res = 0;
1634
 
                    bItemIsOver = 1;
1635
 
                }
1636
 
                /* read all bonds (xml), starting from atom 1 (not 0) */
1637
 
                while ( i < num_atoms ) {
1638
 
                    pos = p - szLine;
1639
 
                    if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1640
 
                        /* load next line if possible */
1641
 
                        res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1642
 
                        if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevBnEnd, sizeof(sStructRevXmlRevBnEnd)-1) ) {
1643
 
                            if ( pos ) {
1644
 
                                res -= pos;  /* number of chars left to process in szLine */
1645
 
                                memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1646
 
                            }
1647
 
                            memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1648
 
                            res += res2;
1649
 
                            szLine[res] = '\0';
1650
 
                            bTooLongLine = bTooLongLine2;
1651
 
                            p = szLine;
1652
 
                        } else {
1653
 
                            bItemIsOver = 1;
1654
 
                        }
1655
 
                    }
1656
 
                    if ( i >= num_atoms ) {
1657
 
                        break;
1658
 
                    }
1659
 
                    /* bond, first char */
1660
 
                    if ( *p == ';' ) {
1661
 
                        p ++;
1662
 
                        i ++;
1663
 
                        continue;
1664
 
                    }
1665
 
                    if ( !isalpha( UCINT *p ) ) {
1666
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1667
 
                        *err      = INCHI_INP_ERROR_ERR;
1668
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
1669
 
                        goto bypass_end_of_INChI;
1670
 
                    }
1671
 
                    bond_char = *p ++;
1672
 
                    /* bond parity */
1673
 
                    switch( *p ) {
1674
 
                    case '-':
1675
 
                        bond_parity = INCHI_PARITY_ODD;
1676
 
                        p ++;
1677
 
                        break;
1678
 
                    case '+':
1679
 
                        bond_parity = INCHI_PARITY_EVEN;
1680
 
                        p ++;
1681
 
                        break;
1682
 
                    case 'u':
1683
 
                        bond_parity = INCHI_PARITY_UNKNOWN;
1684
 
                        p ++;
1685
 
                        break;
1686
 
                    case '?':
1687
 
                        bond_parity = INCHI_PARITY_UNDEFINED;
1688
 
                        p ++;
1689
 
                        break;
1690
 
                    default:
1691
 
                        bond_parity = 0;
1692
 
                        break;
1693
 
                    }
1694
 
                    if ( bond_parity ) {
1695
 
                        switch( *p ) {
1696
 
                        case '-':
1697
 
                            bond_parityNM = INCHI_PARITY_ODD;
1698
 
                            p ++;
1699
 
                            break;
1700
 
                        case '+':
1701
 
                            bond_parityNM = INCHI_PARITY_EVEN;
1702
 
                            p ++;
1703
 
                            break;
1704
 
                        case 'u':
1705
 
                            bond_parityNM = INCHI_PARITY_UNKNOWN;
1706
 
                            p ++;
1707
 
                            break;
1708
 
                        case '?':
1709
 
                            bond_parityNM = INCHI_PARITY_UNDEFINED;
1710
 
                            p ++;
1711
 
                            break;
1712
 
                        default:
1713
 
                            bond_parityNM = 0;
1714
 
                            break;
1715
 
                        }
1716
 
                    } else {
1717
 
                        bond_parityNM = 0;
1718
 
                    }
1719
 
 
1720
 
                    /* neighbor of the current atom */
1721
 
                    if ( !isdigit( UCINT *p ) ) {
1722
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1723
 
                        *err      = INCHI_INP_ERROR_ERR;
1724
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
1725
 
                        goto bypass_end_of_INChI;
1726
 
                    }
1727
 
                    neigh = (int)strtol( p, &q, 10 )-1;
1728
 
 
1729
 
                    if ( i >= num_atoms || neigh >= num_atoms ) {
1730
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1731
 
                        *err      = INCHI_INP_ERROR_ERR;
1732
 
                        MOLFILE_ERR_SET (*err, 0, "Bond to nonexistent atom");
1733
 
                        goto bypass_end_of_INChI;
1734
 
                    }
1735
 
                    p = q;
1736
 
                    bond_stereo1 = bond_stereo2 = 0;
1737
 
 
1738
 
                    /* bond type & 2D stereo */
1739
 
                    switch( bond_char ) {
1740
 
                    case 'v':
1741
 
                        bond_type    = INCHI_BOND_TYPE_SINGLE;
1742
 
                        bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1743
 
                        bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1744
 
                        break;
1745
 
                    case 'V':
1746
 
                        bond_type    = INCHI_BOND_TYPE_SINGLE;
1747
 
                        bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1748
 
                        bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1749
 
                        break;
1750
 
                    case 'w':
1751
 
                        bond_type    = INCHI_BOND_TYPE_DOUBLE;
1752
 
                        bond_stereo1 = 
1753
 
                        bond_stereo2 = INCHI_BOND_STEREO_DOUBLE_EITHER;
1754
 
                        break;
1755
 
                    case 's':
1756
 
                        bond_type    = INCHI_BOND_TYPE_SINGLE;
1757
 
                        break;
1758
 
                    case 'd':
1759
 
                        bond_type    = INCHI_BOND_TYPE_DOUBLE;
1760
 
                        break;
1761
 
                    case 't':
1762
 
                        bond_type    = INCHI_BOND_TYPE_TRIPLE;
1763
 
                        break;
1764
 
                    case 'a':
1765
 
                        bond_type    = INCHI_BOND_TYPE_ALTERN;
1766
 
                        break;
1767
 
                    case 'p':
1768
 
                        bond_type    =  INCHI_BOND_TYPE_SINGLE;
1769
 
                        bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_1UP;
1770
 
                        bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_2UP;
1771
 
                        break;
1772
 
                    case 'P':
1773
 
                        bond_type    =  INCHI_BOND_TYPE_SINGLE;
1774
 
                        bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_2UP;
1775
 
                        bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_1UP;
1776
 
                        break;
1777
 
                    case 'n':
1778
 
                        bond_type    =  INCHI_BOND_TYPE_SINGLE;
1779
 
                        bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_1DOWN;
1780
 
                        bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_2DOWN;
1781
 
                        break;
1782
 
                    case 'N':
1783
 
                        bond_type    =  INCHI_BOND_TYPE_SINGLE;
1784
 
                        bond_stereo1 =  INCHI_BOND_STEREO_SINGLE_2DOWN;
1785
 
                        bond_stereo2 =  INCHI_BOND_STEREO_SINGLE_1DOWN;
1786
 
                        break;
1787
 
                    default:
1788
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error */
1789
 
                        *err      = INCHI_INP_ERROR_ERR;
1790
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong bond type");
1791
 
                        goto bypass_end_of_INChI;
1792
 
                    }
1793
 
                    k = AT_NUM_BONDS(atom[i]) ++;
1794
 
                    atom[i].bond_type[k]   = bond_type;
1795
 
                    atom[i].bond_stereo[k] = bond_stereo1;
1796
 
                    atom[i].neighbor[k]    = (ATOM_NUMBER)neigh;
1797
 
                    k2 = AT_NUM_BONDS(atom[neigh]) ++;
1798
 
                    atom[neigh].bond_type[k2]   = bond_type;
1799
 
                    atom[neigh].bond_stereo[k2] = bond_stereo2;
1800
 
                    atom[neigh].neighbor[k2]    = (ATOM_NUMBER)i;
1801
 
                    bond_parity |= (bond_parityNM << SB_PARITY_SHFT);
1802
 
 
1803
 
                    if ( bond_parity ) {
1804
 
                        if ( max_len_stereo0D <= len_stereo0D ) {
1805
 
                            /* realloc atom_Stereo0D */
1806
 
                            inchi_Stereo0D *new_atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D+num_atoms );
1807
 
                            if ( !new_atom_stereo0D ) {
1808
 
                                num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1809
 
                                *err      = INCHI_INP_FATAL_ERR;
1810
 
                                MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1811
 
                                goto bypass_end_of_INChI;
1812
 
                            }
1813
 
                            memcpy( new_atom_stereo0D, atom_stereo0D, len_stereo0D * sizeof(*atom_stereo0D) );
1814
 
                            FreeInchi_Stereo0D( &atom_stereo0D );
1815
 
                            atom_stereo0D = new_atom_stereo0D;
1816
 
                            max_len_stereo0D += num_atoms;
1817
 
                        }
1818
 
                        /* (a) i may be allene endpoint and     neigh = allene middle point or
1819
 
                           (b) i may be allene middle point and neigh = allene endpoint
1820
 
                           !!!!! CURRENTLY ONLY (b) IS ALLOWED !!!!!
1821
 
                        */
1822
 
                        atom_stereo0D[len_stereo0D].neighbor[1] = neigh; /* neigh < i */
1823
 
                        atom_stereo0D[len_stereo0D].neighbor[2] = i;
1824
 
                        atom_stereo0D[len_stereo0D].parity      = bond_parity;
1825
 
                        atom_stereo0D[len_stereo0D].type        = INCHI_StereoType_DoubleBond; /* incl allenes & cumulenes */
1826
 
                        len_stereo0D ++;
1827
 
                    }
1828
 
                }
1829
 
                if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1830
 
                    num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1831
 
                    *err      = INCHI_INP_ERROR_ERR;
1832
 
                    MOLFILE_ERR_SET (*err, 0, "Wrong number of bonds");
1833
 
                    goto bypass_end_of_INChI;
1834
 
                }
1835
 
                /********************** coordinates xml ****************************/
1836
 
                if ( pszCoord = (MOL_COORD*)inchi_malloc(inchi_max(num_atoms,1) * sizeof(MOL_COORD)) ) {
1837
 
                    memset( pszCoord, ' ', inchi_max(num_atoms,1) * sizeof(MOL_COORD));
1838
 
                    res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1839
 
                    if ( res <= 0  ||
1840
 
                         /* compare the header */
1841
 
                         memcmp(szLine, sStructRevXmlRevXYZ, sizeof(sStructRevXmlRevXYZ)-1) ||
1842
 
                         /* read (the head of) the coordinates (xml) line */
1843
 
                         0 >= (res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ))) {
1844
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1845
 
                        *err      = INCHI_INP_ERROR_ERR;
1846
 
                        MOLFILE_ERR_SET (*err, 0, "Missing atom coordinates data");
1847
 
                        goto bypass_end_of_INChI;
1848
 
                    }
1849
 
                    i = 0;
1850
 
                    bItemIsOver = 0;
1851
 
                    res2 = bTooLongLine2 = -1;
1852
 
                    p = szLine;
1853
 
                    if ( !memcmp(szLine, sStructRevXmlRevXYZEnd, sizeof(sStructRevXmlRevXYZEnd)-1) ) {
1854
 
                        /* empty bonds section */
1855
 
                        res = 0;
1856
 
                        bItemIsOver = 1;
1857
 
                    }
1858
 
                    /* read all coordinates (xml), starting from atom 1 (not 0) */
1859
 
                    while ( i < num_atoms ) {
1860
 
                        pos = p - szLine;
1861
 
                        if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1862
 
                            /* load next line if possible */
1863
 
                            res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1864
 
                            if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevXYZEnd, sizeof(sStructRevXmlRevXYZEnd)-1) ) {
1865
 
                                if ( pos ) {
1866
 
                                    res -= pos;  /* number of chars left to process in szLine */
1867
 
                                    memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1868
 
                                }
1869
 
                                memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1870
 
                                res += res2;
1871
 
                                szLine[res] = '\0';
1872
 
                                bTooLongLine = bTooLongLine2;
1873
 
                                p = szLine;
1874
 
                            } else {
1875
 
                                bItemIsOver = 1;
1876
 
                            }
1877
 
                        }
1878
 
                        /* coord, first char */
1879
 
                        if ( *p == ';' ) {
1880
 
                            for ( k = 0; k < NUM_COORD; k ++ ) {
1881
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
1882
 
                            }
1883
 
                            p ++;
1884
 
                            i ++;
1885
 
                            continue;
1886
 
                        }
1887
 
                        for ( k = 0; k < 3; k ++ ) {
1888
 
                            double xyz;
1889
 
                            bNonZeroXYZ = 0;
1890
 
                            if ( *p == ';' ) {
1891
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
1892
 
                                xyz = 0.0;
1893
 
                            } else
1894
 
                            if ( *p == ',' ) {
1895
 
                                /* empty */
1896
 
                                pszCoord[i][LEN_COORD*k + 4] = '0';
1897
 
                                xyz = 0.0;
1898
 
                                p ++;
1899
 
                            } else {
1900
 
                                xyz = strtod( p, &q );
1901
 
                                bNonZeroXYZ = fabs(xyz) > MIN_BOND_LENGTH;
1902
 
                                if ( q != NULL ) {
1903
 
                                    memcpy( pszCoord[i]+LEN_COORD*k, p, q-p );
1904
 
                                    if ( *q == ',' )
1905
 
                                        q ++;
1906
 
                                    p = q;
1907
 
                                } else {
1908
 
                                    pszCoord[i][LEN_COORD*k + 4] = '0';
1909
 
                                }
1910
 
                            }
1911
 
                            switch( k ) {
1912
 
                            case 0:
1913
 
                                atom[i].x = xyz;
1914
 
                                b2D |= bNonZeroXYZ;
1915
 
                                break;
1916
 
                            case 1:
1917
 
                                atom[i].y = xyz;
1918
 
                                b2D |= bNonZeroXYZ;
1919
 
                                break;
1920
 
                            case 2:
1921
 
                                b3D |= bNonZeroXYZ;
1922
 
                                atom[i].z = xyz;
1923
 
                                break;
1924
 
                            }
1925
 
                        }
1926
 
                        if ( *p == ';' ) {
1927
 
                            p ++;  /* end of this triple of coordinates */
1928
 
                            i ++;
1929
 
                        } else {
1930
 
                            num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1931
 
                            *err      = INCHI_INP_ERROR_ERR;
1932
 
                            MOLFILE_ERR_SET (*err, 0, "Wrong atom coordinates data");
1933
 
                            goto bypass_end_of_INChI;
1934
 
                        }
1935
 
                    }
1936
 
                    if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1937
 
                        num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1938
 
                        *err      = INCHI_INP_ERROR_ERR;
1939
 
                        MOLFILE_ERR_SET (*err, 0, "Wrong number of coordinates");
1940
 
                        goto bypass_end_of_INChI;
1941
 
                    }
1942
 
                } else { /* allocation failed */
1943
 
                    num_atoms = INCHI_INP_FATAL_RET; 
1944
 
                    *err      = INCHI_INP_FATAL_ERR;
1945
 
                    MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1946
 
                    goto bypass_end_of_INChI;
1947
 
                }
1948
 
                /* set special valences and implicit H (xml) */
1949
 
                b23D = b2D | b3D;
1950
 
                b2D = b3D = 0;
1951
 
                if ( at ) {
1952
 
                    if ( !*at ) {
1953
 
                        int a1, a2, n1, n2, valence;
1954
 
                        int chem_bonds_valence;
1955
 
                        int    nX=0, nY=0, nZ=0, nXYZ;
1956
 
                        *at = atom;
1957
 
                        /* special valences */
1958
 
                        for ( bNonMetal = 0; bNonMetal < 1 /*2*/; bNonMetal ++ ) {
1959
 
                            for ( a1 = 0; a1 < num_atoms; a1 ++ ) {
1960
 
                                int num_bond_type[MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE + 1];
1961
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1962
 
#else
1963
 
                                int bHasMetalNeighbor=0;
1964
 
#endif
1965
 
                                memset( num_bond_type, 0, sizeof(num_bond_type) );
1966
 
 
1967
 
                                valence = AT_BONDS_VAL(atom, a1); /*  save atom valence if available */
1968
 
                                AT_BONDS_VAL(atom, a1) = 0;
1969
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1970
 
#else
1971
 
                                atom[a1].orig_at_number = a1+1;
1972
 
#endif
1973
 
                                nX = nY = nZ = 0;
1974
 
                                for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
1975
 
                                    bond_type = atom[a1].bond_type[n1] - MIN_INPUT_BOND_TYPE;
1976
 
                                    if (  bond_type < 0 || bond_type > MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE ) {
1977
 
                                        bond_type = 0; /* cannot happen */
1978
 
                                        MOLFILE_ERR_SET (*err, 0, "Unknown bond type in InChI aux assigned as a single bond");
1979
 
                                    }
1980
 
 
1981
 
                                    num_bond_type[ bond_type ] ++;
1982
 
                                    nNumBonds ++;
1983
 
                                    if ( b23D ) {
1984
 
                                        neigh = atom[a1].neighbor[n1];
1985
 
                                        nX |= (fabs(atom[a1].x - atom[neigh].x) > MIN_BOND_LENGTH);
1986
 
                                        nY |= (fabs(atom[a1].y - atom[neigh].y) > MIN_BOND_LENGTH);
1987
 
                                        nZ |= (fabs(atom[a1].z - atom[neigh].z) > MIN_BOND_LENGTH);
1988
 
                                    }
1989
 
                                }
1990
 
                                chem_bonds_valence = 0;
1991
 
                                for ( n1 = 0; MIN_INPUT_BOND_TYPE + n1 <= 3 && MIN_INPUT_BOND_TYPE + n1 <= MAX_INPUT_BOND_TYPE; n1 ++ ) {
1992
 
                                    chem_bonds_valence += (MIN_INPUT_BOND_TYPE + n1) * num_bond_type[n1];
1993
 
                                }
1994
 
                                if ( MIN_INPUT_BOND_TYPE <= INCHI_BOND_TYPE_ALTERN && INCHI_BOND_TYPE_ALTERN <= MAX_INPUT_BOND_TYPE &&
1995
 
                                     ( n2 = num_bond_type[INCHI_BOND_TYPE_ALTERN-MIN_INPUT_BOND_TYPE] ) ) {
1996
 
                                    /* accept input aromatic bonds for now */
1997
 
                                    switch ( n2 ) {
1998
 
                                    case 2:
1999
 
                                        chem_bonds_valence += 3;  /* =A- */
2000
 
                                        break;
2001
 
                                    case 3:
2002
 
                                        chem_bonds_valence += 4;  /* =A< */
2003
 
                                        break;
2004
 
                                    default:
2005
 
                                        /*  if 1 or >= 4 aromatic bonds then replace such bonds with single bonds */
2006
 
                                        for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
2007
 
                                            if ( atom[a1].bond_type[n1] == INCHI_BOND_TYPE_ALTERN ) {
2008
 
                                                ATOM_NUMBER *p1;
2009
 
                                                a2 = atom[a1].neighbor[n1];
2010
 
                                                p1 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
2011
 
                                                if ( p1 ) {
2012
 
                                                    atom[a1].bond_type[n1] = 
2013
 
                                                    atom[a2].bond_type[p1-atom[a2].neighbor] = INCHI_BOND_TYPE_SINGLE;
2014
 
                                                } else {
2015
 
                                                    *err = -2;  /*  Program error */
2016
 
                                                    MOLFILE_ERR_SET (*err, 0, "Program error interpreting InChI aux");
2017
 
                                                    num_atoms = 0;
2018
 
                                                    goto bypass_end_of_INChI; /*  no structure */
2019
 
                                                }
2020
 
                                            }
2021
 
                                        }
2022
 
                                        chem_bonds_valence += n2;
2023
 
                                        *err |= 32; /*  Unrecognized aromatic bond(s) replaced with single */
2024
 
                                        MOLFILE_ERR_SET (*err, 0, "Atom has 1 or more than 3 aromatic bonds");
2025
 
                                        break;
2026
 
                                    }
2027
 
                                }
2028
 
 
2029
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2030
 
                                /*************************************************************************************
2031
 
                                 *
2032
 
                                 *  Set number of hydrogen atoms
2033
 
                                 */
2034
 
                                {
2035
 
                                    int num_iso_H;
2036
 
                                    num_iso_H = atom[a1].num_iso_H[1] + atom[a1].num_iso_H[2] + atom[a1].num_iso_H[3];
2037
 
                                    if ( valence == ISOLATED_ATOM ) {
2038
 
                                        atom[a1].num_iso_H[0] = 0;
2039
 
                                    } else
2040
 
                                    if ( valence && valence >= chem_bonds_valence ) {
2041
 
                                        atom[a1].num_iso_H[0] = valence - chem_bonds_valence;
2042
 
                                    } else
2043
 
                                    if ( valence || bDoNotAddH ) {
2044
 
                                        atom[a1].num_iso_H[0] = 0;
2045
 
                                    } else
2046
 
                                    if ( !bDoNotAddH ) {
2047
 
                                        atom[a1].num_iso_H[0] = -1; /* auto add H */
2048
 
                                    }
2049
 
                                }
2050
 
#else                                
2051
 
                                /* added 2006-07-19 to process aromatic bonds same way as from molfile */
2052
 
                                if ( n2 && !valence ) {
2053
 
                                    int num_H = NUMH(atom, a1); /* only isotopic */
2054
 
                                    int chem_valence = chem_bonds_valence;
2055
 
                                    int bUnusualValenceArom = 
2056
 
                                        detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
2057
 
                                                                    atom[a1].radical, chem_valence,
2058
 
                                                                    num_H, atom[a1].valence );
2059
 
                                    int bUnusualValenceNoArom = 
2060
 
                                        detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
2061
 
                                                                    atom[a1].radical, chem_valence-1,
2062
 
                                                                    num_H, atom[a1].valence );
2063
 
#if ( CHECK_AROMBOND2ALT == 1 )
2064
 
                                    if ( bUnusualValenceArom && !bUnusualValenceNoArom && 0 == nBondsValToMetal( atom, a1) )
2065
 
#else
2066
 
                                    if ( bUnusualValenceArom && !bUnusualValenceNoArom )
2067
 
#endif                     
2068
 
                                    {
2069
 
                                        /* typically NH in 5-member aromatic ring */
2070
 
                                        chem_bonds_valence --;
2071
 
                                    }
2072
 
                                } else
2073
 
                                if ( n2 && valence ) {
2074
 
                                    /* atom has aromatic bonds AND the chemical valence is known */
2075
 
                                    int num_H = NUMH(atom, a1);
2076
 
                                    int chem_valence = chem_bonds_valence + num_H;
2077
 
                                    if ( valence == chem_valence-1 ) {
2078
 
                                        /* typically NH in 5-member aromatic ring */
2079
 
                                        chem_bonds_valence --;
2080
 
                                    }
2081
 
                                }
2082
 
 
2083
 
                                atom[a1].chem_bonds_valence = chem_bonds_valence;
2084
 
                                atom[a1].num_H = get_num_H( atom[a1].elname, atom[a1].num_H, atom[a1].num_iso_H, atom[a1].charge, atom[a1].radical,
2085
 
                                                          atom[a1].chem_bonds_valence,
2086
 
                                                          valence,
2087
 
                                                          0, bDoNotAddH, bHasMetalNeighbor );
2088
 
#endif
2089
 
                            }
2090
 
                        }
2091
 
                        nNumBonds /= 2;
2092
 
                        if ( b23D && nNumBonds ) {
2093
 
                            nXYZ = nX+nY+nZ;
2094
 
                            b2D  = (nXYZ > 0);
2095
 
                            b3D  = (nXYZ == 3);
2096
 
                            *num_dimensions = b3D? 3 : b2D? 2 : 0;
2097
 
                            *num_bonds = nNumBonds;
2098
 
                        }
2099
 
                        /*======= 0D parities =================================*/
2100
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2101
 
                        if ( len_stereo0D > 0 && atom_stereo0D && stereo0D ) {
2102
 
                            *stereo0D     = atom_stereo0D;
2103
 
                            *num_stereo0D = len_stereo0D;
2104
 
                        } else {
2105
 
                            FreeInchi_Stereo0D( &atom_stereo0D );
2106
 
                            *num_stereo0D = len_stereo0D = 0;
2107
 
                        }
2108
 
#endif
2109
 
                        for ( i = 0; i < len_stereo0D; i ++ ) {
2110
 
                            ATOM_NUMBER *p1, *p2;
2111
 
                            int     sb_ord_from_a1 = -1, sb_ord_from_a2 = -1, bEnd1 = 0, bEnd2 = 0;
2112
 
                            switch( atom_stereo0D[i].type ) {
2113
 
 
2114
 
                            case INCHI_StereoType_Tetrahedral:
2115
 
                                a1 = atom_stereo0D[i].central_atom;
2116
 
                                if ( atom_stereo0D[i].parity && (AT_NUM_BONDS(atom[a1]) == 3 || AT_NUM_BONDS(atom[a1]) == 4) ) {
2117
 
                                    int ii, kk = 0;
2118
 
                                    if ( AT_NUM_BONDS(atom[a1]) == 3 ) {
2119
 
                                        atom_stereo0D[i].neighbor[kk++] = a1;
2120
 
                                    }
2121
 
                                    for ( ii = 0; ii < AT_NUM_BONDS(atom[a1]); ii ++ ) {
2122
 
                                        atom_stereo0D[i].neighbor[kk++] = atom[a1].neighbor[ii];
2123
 
                                    }
2124
 
                                }
2125
 
                            
2126
 
                            break;
2127
 
 
2128
 
                            case INCHI_StereoType_DoubleBond:
2129
 
#define MAX_CHAIN_LEN 20
2130
 
                                a1 = atom_stereo0D[i].neighbor[1];
2131
 
                                a2 = atom_stereo0D[i].neighbor[2];
2132
 
                                p1 = IN_NEIGH_LIST( atom[a1].neighbor, (ATOM_NUMBER)a2, AT_NUM_BONDS(atom[a1]) );
2133
 
                                p2 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
2134
 
                                if ( !p1 || !p2 ) {
2135
 
                                    atom_stereo0D[i].type = INCHI_StereoType_None;
2136
 
                                    atom_stereo0D[i].central_atom = NO_ATOM;
2137
 
                                    atom_stereo0D[i].neighbor[0] =
2138
 
                                    atom_stereo0D[i].neighbor[3] = -1;
2139
 
                                    *err |= 64; /* Error in cumulene stereo */
2140
 
                                    MOLFILE_ERR_SET (*err, 0, "0D stereobond not recognized");
2141
 
                                    break;
2142
 
                                }
2143
 
                                /* streobond, allene, or cumulene */
2144
 
 
2145
 
                                sb_ord_from_a1 = p1 - atom[a1].neighbor;
2146
 
                                sb_ord_from_a2 = p2 - atom[a2].neighbor;
2147
 
                                
2148
 
                                if (  AT_NUM_BONDS(atom[a1]) == 2 &&
2149
 
                                      atom[a1].bond_type[0] + atom[a1].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2150
 
                                      0 == inchi_NUMH2(atom, a1) &&
2151
 
                                     (AT_NUM_BONDS(atom[a2]) != 2 ||
2152
 
                                      atom[a2].bond_type[0] + atom[a2].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
2153
 
                                    bEnd2 = 1; /* a2 is the end-atom, a1 is middle atom */   
2154
 
                                }
2155
 
                                if (  AT_NUM_BONDS(atom[a2]) == 2 &&
2156
 
                                      atom[a2].bond_type[0] + atom[a2].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2157
 
                                      0 == inchi_NUMH2(atom, a2) &&
2158
 
                                     (AT_NUM_BONDS(atom[a1]) != 2 ||
2159
 
                                      atom[a1].bond_type[0] + atom[a1].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
2160
 
                                    bEnd1 = 1; /* a1 is the end-atom, a2 is middle atom */   
2161
 
                                }
2162
 
                                
2163
 
                                if ( bEnd2 + bEnd1 == 1 ) {
2164
 
                                    /* allene or cumulene */
2165
 
                                    ATOM_NUMBER  chain[MAX_CHAIN_LEN+1], prev, cur, next;
2166
 
                                    if ( bEnd2 && !bEnd1 ) {
2167
 
                                        cur = a1;
2168
 
                                        a1 = a2;
2169
 
                                        a2 = cur;
2170
 
                                        sb_ord_from_a1 = sb_ord_from_a2;
2171
 
                                    }
2172
 
                                    sb_ord_from_a2 = -1;
2173
 
                                    cur  = a1;
2174
 
                                    next = a2;
2175
 
                                    len = 0;
2176
 
                                    chain[len++] = cur;
2177
 
                                    chain[len++] = next;
2178
 
                                    while ( len < MAX_CHAIN_LEN ) { /* arbitrary very high upper limit to prevent infinite loop */
2179
 
                                        prev = cur;
2180
 
                                        cur  = next;
2181
 
                                            /* follow double bond path && avoid going back */
2182
 
                                        if ( AT_NUM_BONDS(atom[cur]) == 2 &&
2183
 
                                             atom[cur].bond_type[0]+atom[cur].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2184
 
                                             0 == inchi_NUMH2(atom, cur) ) {
2185
 
                                            next     = atom[cur].neighbor[atom[cur].neighbor[0] == prev];
2186
 
                                            chain[len++] = next;
2187
 
                                        } else {
2188
 
                                            break;
2189
 
                                        }
2190
 
                                    }
2191
 
                                    if ( len > 2 &&
2192
 
                                         (p2 = IN_NEIGH_LIST( atom[cur].neighbor, (ATOM_NUMBER)prev, AT_NUM_BONDS(atom[cur]))) ) {
2193
 
                                        sb_ord_from_a2 = p2 - atom[cur].neighbor;
2194
 
                                        a2 = cur;
2195
 
                                        /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
2196
 
                                        atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[sb_ord_from_a1 == 0];
2197
 
                                        atom_stereo0D[i].neighbor[1] = a1;
2198
 
                                        atom_stereo0D[i].neighbor[2] = a2;
2199
 
                                        atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[sb_ord_from_a2 == 0];
2200
 
                                        if ( len % 2 ) {
2201
 
                                            atom_stereo0D[i].central_atom = chain[len/2];
2202
 
                                            atom_stereo0D[i].type         = INCHI_StereoType_Allene; 
2203
 
                                        } else {
2204
 
                                            atom_stereo0D[i].central_atom = NO_ATOM;
2205
 
                                        }
2206
 
                                    } else {
2207
 
                                        /* error */
2208
 
                                        atom_stereo0D[i].type = INCHI_StereoType_None;
2209
 
                                        atom_stereo0D[i].central_atom = NO_ATOM;
2210
 
                                        atom_stereo0D[i].neighbor[0] =
2211
 
                                        atom_stereo0D[i].neighbor[3] = -1;
2212
 
                                        *err |= 64; /* Error in cumulene stereo */
2213
 
                                        MOLFILE_ERR_SET (*err, 0, "Cumulene stereo not recognized (0D)");
2214
 
 
2215
 
                                    }
2216
 
#undef MAX_CHAIN_LEN 
2217
 
                                } else {
2218
 
                                    /****** a normal possibly stereogenic bond -- not an allene or cumulene *******/
2219
 
                                    /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
2220
 
                                    sb_ord_from_a1 = p1 - atom[a1].neighbor;
2221
 
                                    sb_ord_from_a2 = p2 - atom[a2].neighbor;
2222
 
                                    atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[p1 == atom[a1].neighbor];
2223
 
                                    atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[p2 == atom[a2].neighbor];
2224
 
                                    atom_stereo0D[i].central_atom = NO_ATOM;
2225
 
                                }
2226
 
                                if ( atom_stereo0D[i].type != INCHI_StereoType_None &&
2227
 
                                     sb_ord_from_a1 >= 0 && sb_ord_from_a2 >= 0 &&
2228
 
                                     ATOM_PARITY_WELL_DEF( SB_PARITY_2(atom_stereo0D[i].parity) ) ) {
2229
 
                                    /* Detected well-defined disconnected stereo
2230
 
                                     * locate first non-metal neighbors */
2231
 
                                    int    a, n, j, /* k,*/ sb_ord, cur_neigh, min_neigh;
2232
 
                                    for ( k = 0; k < 2; k ++ ) {
2233
 
                                        a      = k? atom_stereo0D[i].neighbor[2] : atom_stereo0D[i].neighbor[1];
2234
 
                                        sb_ord = k? sb_ord_from_a2 : sb_ord_from_a1;
2235
 
                                        min_neigh = num_atoms;
2236
 
                                        for ( n  = j = 0; j < AT_NUM_BONDS(atom[a]); j ++ ) {
2237
 
                                            cur_neigh = atom[a].neighbor[j];
2238
 
                                            if ( j != sb_ord && !IS_METAL_ATOM(atom, cur_neigh) ) {
2239
 
                                                min_neigh = inchi_min( cur_neigh, min_neigh );
2240
 
                                            }
2241
 
                                        }
2242
 
                                        if ( min_neigh < num_atoms ) {
2243
 
                                            atom_stereo0D[i].neighbor[k?3:0] = min_neigh;
2244
 
                                        } else {
2245
 
                                            MOLFILE_ERR_SET (*err, 0, "Cannot find non-metal stereobond neighor (0D)");
2246
 
                                        }
2247
 
                                    }
2248
 
                                }
2249
 
 
2250
 
                                break;
2251
 
                            }
2252
 
                        }
2253
 
                        /* end of 0D parities extraction */
2254
 
/*exit_cycle:;*/
2255
 
                    }
2256
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2257
 
#else
2258
 
                    /* transfer atom_stereo0D[] to atom[] */
2259
 
                    if ( len_stereo0D ) {
2260
 
                        Extract0DParities( atom, num_atoms, atom_stereo0D, len_stereo0D, 
2261
 
                            pStrErr, err, vABParityUnknown );
2262
 
                    }
2263
 
#endif
2264
 
                    if ( pInpAtomFlags ) {
2265
 
                        /* save chirality flag */
2266
 
                        *pInpAtomFlags |= InpAtomFlags;
2267
 
                    }
2268
 
                } else
2269
 
                if ( atom ) {
2270
 
                    inchi_free( atom );
2271
 
                    atom = NULL;
2272
 
                }
2273
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2274
 
#else
2275
 
                if ( szCoord ) {
2276
 
                    *szCoord = pszCoord;
2277
 
                    pszCoord = NULL;
2278
 
                } else
2279
 
#endif
2280
 
                if ( pszCoord ) {
2281
 
                    inchi_free( pszCoord );
2282
 
                }
2283
 
                goto bypass_end_of_INChI;
2284
 
                /*return num_atoms;*/
2285
 
            }
2286
 
        }
2287
 
        if ( atom_stereo0D ) {
2288
 
            FreeInchi_Stereo0D( &atom_stereo0D );
2289
 
        }
2290
 
        /* end of struct. reading cycle, code never used? */
2291
 
        if ( res <= 0 ) {
2292
 
            if ( *err == INCHI_INP_ERROR_ERR ) {
2293
 
                return num_atoms;
2294
 
            }
2295
 
            *err = INCHI_INP_EOF_ERR;
2296
 
            return INCHI_INP_EOF_RET; /* no more data */
2297
 
        }
2298
 
bypass_end_of_INChI:
2299
 
        /* cleanup */
2300
 
        if ( num_atoms == INCHI_INP_ERROR_RET && atom_stereo0D ) {
2301
 
            if ( stereo0D && *stereo0D == atom_stereo0D ) {
2302
 
                *stereo0D     = NULL;
2303
 
                *num_stereo0D = 0;
2304
 
            }
2305
 
            FreeInchi_Stereo0D( &atom_stereo0D );
2306
 
        }
2307
 
        if ( !memcmp(szLine, sStructHdrXmlEnd, sizeof(sStructHdrXmlEnd)-1) )
2308
 
            num_struct --;
2309
 
        if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) )
2310
 
            num_struct ++;
2311
 
 
2312
 
        while ( num_struct > 0 && 0 < inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) {
2313
 
            if ( !memcmp(szLine, sStructHdrXmlEnd, sizeof(sStructHdrXmlEnd)-1) )
2314
 
                num_struct --;
2315
 
            else
2316
 
            if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) )
2317
 
                num_struct ++;
2318
 
        }
2319
 
        return num_atoms;
2320
 
 
2321
 
    }
2322
 
    
2323
 
    return num_atoms;
2324
 
 
2325
 
#undef AT_NUM_BONDS
2326
 
#undef ATOM_NUMBER
2327
 
#undef IN_NEIGH_LIST
2328
 
#undef inchi_NUMH2
2329
 
 
2330
 
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2331
 
#else 
2332
 
#undef inchi_Atom
2333
 
#endif
2334
 
 
2335
 
#undef AT_NUM_BONDS 
2336
 
#undef ATOM_NUMBER        
2337
 
#undef IN_NEIGH_LIST      
2338
 
#undef inchi_NUMH2 
2339
 
#undef INChITo_Atom       
2340
 
#undef MoreParms          
2341
 
#undef INPUT_FILE         
2342
 
#undef Create_Atom        
2343
 
#undef AT_BONDS_VAL
2344
 
#undef ISOLATED_ATOM      
2345
 
#undef NUM_ISO_Hk
2346
 
#undef IS_METAL_ATOM
2347
 
 
2348
 
 
2349
 
}
2350
 
#ifdef INCHI_MAIN
2351
 
 
2352
 
/**********************************************************************************/
2353
 
int INChIToInchi_Input( INCHI_IOSTREAM *inp_molfile, inchi_Input *orig_at_data, int bMergeAllInputStructures,
2354
 
                       int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType,
2355
 
                       char *pSdfLabel, char *pSdfValue, long *lSdfId, INCHI_MODE *pInpAtomFlags,
2356
 
                       int *err, char *pStrErr )
2357
 
{
2358
 
    /* inp_ATOM       *at = NULL; */
2359
 
    int             num_dimensions_new;
2360
 
    int             num_inp_bonds_new;
2361
 
    int             num_inp_atoms_new;
2362
 
    int             num_inp_0D_new;
2363
 
    inchi_Atom     *at_new     = NULL;
2364
 
    inchi_Atom     *at_old     = NULL;
2365
 
    inchi_Stereo0D *stereo0D_new = NULL;
2366
 
    inchi_Stereo0D *stereo0D_old = NULL;
2367
 
    int             nNumAtoms  = 0, nNumStereo0D = 0;
2368
 
    MOL_COORD      *szCoordNew = NULL;
2369
 
    MOL_COORD      *szCoordOld = NULL;
2370
 
    int            i, j;
2371
 
 
2372
 
    if ( pStrErr ) {
2373
 
        pStrErr[0] = '\0';
2374
 
    }
2375
 
 
2376
 
    /*FreeOrigAtData( orig_at_data );*/
2377
 
    if ( lSdfId )
2378
 
        *lSdfId = 0;
2379
 
    do {
2380
 
        
2381
 
        at_old       = orig_at_data? orig_at_data->atom      : NULL; /*  save pointer to the previous allocation */
2382
 
        stereo0D_old = orig_at_data? orig_at_data->stereo0D  : NULL;
2383
 
        szCoordOld = NULL;
2384
 
        num_inp_atoms_new =
2385
 
            INChIToInchi_Atom( inp_molfile, orig_at_data? &stereo0D_new:NULL, &num_inp_0D_new,
2386
 
                          bDoNotAddH, vABParityUnknown, nInputType, 
2387
 
                          orig_at_data? &at_new:NULL, MAX_ATOMS,
2388
 
                          &num_dimensions_new, &num_inp_bonds_new,
2389
 
                          pSdfLabel, pSdfValue, lSdfId, pInpAtomFlags, err, pStrErr );
2390
 
        if ( num_inp_atoms_new <= 0 && !*err ) {
2391
 
            MOLFILE_ERR_SET (*err, 0, "Empty structure");
2392
 
            *err = 98;
2393
 
        } else
2394
 
        if ( orig_at_data && !num_inp_atoms_new && 10 < *err && *err < 20 && orig_at_data->num_atoms > 0 && bMergeAllInputStructures ) {
2395
 
            *err = 0; /* end of file */
2396
 
            break;
2397
 
        } else
2398
 
        if ( num_inp_atoms_new > 0 && orig_at_data ) {
2399
 
            /*  merge pOrigDataTmp + orig_at_data => pOrigDataTmp; */
2400
 
            nNumAtoms    = num_inp_atoms_new + orig_at_data->num_atoms;
2401
 
            nNumStereo0D = num_inp_0D_new    + orig_at_data->num_stereo0D;
2402
 
            if ( nNumAtoms >= MAX_ATOMS ) {
2403
 
                MOLFILE_ERR_SET (*err, 0, "Too many atoms");
2404
 
                *err = 70;
2405
 
                orig_at_data->num_atoms = -1;
2406
 
            } else
2407
 
            if ( !at_old ) {
2408
 
                /* the first structure */
2409
 
                orig_at_data->atom         = at_new;            at_new            = NULL;
2410
 
                orig_at_data->num_atoms    = num_inp_atoms_new; num_inp_atoms_new = 0;
2411
 
                orig_at_data->stereo0D     = stereo0D_new;      stereo0D_new      = NULL;
2412
 
                orig_at_data->num_stereo0D = num_inp_0D_new;    num_inp_0D_new    = 0;
2413
 
            } else
2414
 
            if ( orig_at_data->atom = CreateInchi_Atom( nNumAtoms ) ) {
2415
 
                /*  switch at_new <--> orig_at_data->at; */
2416
 
                if ( orig_at_data->num_atoms ) {
2417
 
                    memcpy( orig_at_data->atom, at_old, orig_at_data->num_atoms * sizeof(orig_at_data->atom[0]) );
2418
 
                    /*  adjust numbering in the newly read structure */
2419
 
                    for ( i = 0; i < num_inp_atoms_new; i ++ ) {
2420
 
                        for ( j = 0; j < at_new[i].num_bonds; j ++ ) {
2421
 
                            at_new[i].neighbor[j] += orig_at_data->num_atoms;
2422
 
                        }
2423
 
                    }
2424
 
                }
2425
 
                FreeInchi_Atom( &at_old );
2426
 
                /*  copy newly read structure */
2427
 
                memcpy( orig_at_data->atom + orig_at_data->num_atoms,
2428
 
                        at_new,
2429
 
                        num_inp_atoms_new * sizeof(orig_at_data->atom[0]) );
2430
 
                /*  cpy newly read 0D stereo */
2431
 
                if ( num_inp_0D_new > 0 && stereo0D_new ) {
2432
 
                    if ( orig_at_data->stereo0D = CreateInchi_Stereo0D( nNumStereo0D ) ) {
2433
 
                        memcpy( orig_at_data->stereo0D, stereo0D_old, orig_at_data->num_stereo0D * sizeof(orig_at_data->stereo0D[0]) );
2434
 
                        /*  adjust numbering in the newly read structure */
2435
 
                        for ( i = 0; i < num_inp_0D_new; i ++ ) {
2436
 
                            if ( stereo0D_new[i].central_atom >= 0 ) {
2437
 
                                stereo0D_new[i].central_atom += orig_at_data->num_atoms;
2438
 
                            }
2439
 
                            for ( j = 0; j < 4; j ++ ) {
2440
 
                                stereo0D_new[i].neighbor[j] += orig_at_data->num_atoms;
2441
 
                            }
2442
 
                        }
2443
 
                        FreeInchi_Stereo0D( &stereo0D_old );
2444
 
                        memcpy( orig_at_data->stereo0D+orig_at_data->num_stereo0D,
2445
 
                                stereo0D_new,
2446
 
                                num_inp_0D_new * sizeof(orig_at_data->stereo0D[0]) );
2447
 
                    } else {
2448
 
                        num_inp_0D_new = 0;
2449
 
                        MOLFILE_ERR_SET (*err, 0, "Out of RAM");
2450
 
                        *err = -1;
2451
 
                    }
2452
 
                } else {
2453
 
                    num_inp_0D_new = 0;
2454
 
                }
2455
 
                /* update lengths */
2456
 
                orig_at_data->num_atoms    += num_inp_atoms_new;
2457
 
                orig_at_data->num_stereo0D += num_inp_0D_new;
2458
 
            } else {
2459
 
                MOLFILE_ERR_SET (*err, 0, "Out of RAM");
2460
 
                *err = -1;
2461
 
            }
2462
 
        } else
2463
 
        if ( num_inp_atoms_new > 0 ) {
2464
 
            nNumAtoms += num_inp_atoms_new;
2465
 
        }
2466
 
        FreeInchi_Atom( &at_new );
2467
 
        num_inp_atoms_new = 0;
2468
 
        FreeInchi_Stereo0D( &stereo0D_new );
2469
 
        num_inp_0D_new = 0;
2470
 
 
2471
 
    } while ( !*err && bMergeAllInputStructures );
2472
 
    /*
2473
 
    if ( !*err ) {
2474
 
        orig_at_data->num_components =
2475
 
            MarkDisconnectedComponents( orig_at_data );
2476
 
        if ( orig_at_data->num_components == 0 ) {
2477
 
            MOLFILE_ERR_SET (*err, 0, "No components found");
2478
 
            *err = 99;
2479
 
        }
2480
 
        if ( orig_at_data->num_components < 0 ) {
2481
 
            MOLFILE_ERR_SET (*err, 0, "Too many components");
2482
 
            *err = 99;
2483
 
        }
2484
 
    }
2485
 
    */
2486
 
    if ( szCoordNew ) {
2487
 
        inchi_free( szCoordNew );
2488
 
    }
2489
 
    if ( at_new ) {
2490
 
        inchi_free( at_new );
2491
 
    }
2492
 
    /*
2493
 
    if ( !*err ) {
2494
 
        if ( ReconcileAllCmlBondParities( orig_at_data->atom, orig_at_data->num_atoms ) ) {
2495
 
            MOLFILE_ERR_SET (*err, 0, "Cannot reconcile stereobond parities");
2496
 
            if (!orig_at_data->num_atoms) {
2497
 
                *err = 1;
2498
 
            }
2499
 
        }
2500
 
    }
2501
 
    */
2502
 
    if ( *err ) {
2503
 
        FreeInchi_Input( orig_at_data );
2504
 
    }
2505
 
    if ( *err && !(10 < *err && *err < 20) && pStrErr && !pStrErr[0] ) {
2506
 
        MOLFILE_ERR_SET (*err, 0, "Unknown error");  /*   <BRKPT> */
2507
 
    }
2508
 
    return orig_at_data? orig_at_data->num_atoms : nNumAtoms;
2509
 
}
2510
 
 
2511
 
#endif
2512
 
 
2513
 
#ifndef INCHI_MAIN
2514
 
#undef AB_MAX_WELL_DEFINED_PARITY
2515
 
#undef AB_MIN_WELL_DEFINED_PARITY
2516
 
#include "extr_ct.h"
2517
 
/****************************************************************************************/
2518
 
int Extract0DParities( inp_ATOM *at, int nNumAtoms, inchi_Stereo0D *stereo0D,
2519
 
                       int num_stereo0D, char *pStrErr, int *err,
2520
 
                       int vABParityUnknown)
2521
 
{
2522
 
    /* vABParityUnknown holds actual value of an internal constant signifying       */
2523
 
    /* unknown parity: either the same as for undefined parity (default==standard)  */
2524
 
    /*  or a specific one (non-std; requested by SLUUD switch).                     */
2525
 
 
2526
 
    if ( stereo0D && num_stereo0D > 0 ) {
2527
 
        int i0D, a2, k, k_prev, type, j, j1, j2, len, parity, parityNM;
2528
 
        int sb_ord_from_i1, sb_ord_from_i2, sn_ord_from_i1, sn_ord_from_i2;
2529
 
        AT_NUMB i1n, i2n, i1, i2;
2530
 
        for ( i0D = 0; i0D < num_stereo0D; i0D ++ ) {
2531
 
            parity   = (stereo0D[i0D].parity & SB_PARITY_MASK);
2532
 
            parityNM = (stereo0D[i0D].parity & SB_PARITY_FLAG) >> SB_PARITY_SHFT;
2533
 
            if ( parity == INCHI_PARITY_NONE ||
2534
 
                 parity != INCHI_PARITY_ODD && parity != INCHI_PARITY_EVEN &&
2535
 
                 parity != INCHI_PARITY_UNKNOWN && parity != INCHI_PARITY_UNDEFINED ) {
2536
 
                char szTemp[16];
2537
 
                sprintf( szTemp, "#%d", i0D+1 );
2538
 
                MOLFILE_ERR_SET (*err, 0, "Wrong 0D stereo descriptor(s):");
2539
 
                MOLFILE_ERR_SET (*err, 0, szTemp);
2540
 
                continue; /* warning */
2541
 
            }
2542
 
            type   = stereo0D[i0D].type;
2543
 
            a2     = stereo0D[i0D].central_atom; /* central atom or -1 */
2544
 
            j   = -1;
2545
 
            len =  0;
2546
 
            sb_ord_from_i1 = sb_ord_from_i2 = sn_ord_from_i1 = sn_ord_from_i2 = -1;
2547
 
            i1n = i2n = i1 = i2 = MAX_ATOMS+1;
2548
 
 
2549
 
            if ( (type == INCHI_StereoType_Tetrahedral ||
2550
 
                  type == INCHI_StereoType_Allene ) &&
2551
 
                  0 <= a2 && a2 < nNumAtoms ||
2552
 
                  type == INCHI_StereoType_DoubleBond &&
2553
 
                  a2 == NO_ATOM) {
2554
 
                /* test the quadruplet */
2555
 
                for ( j = 0, k_prev = -1; j < 4; j ++, k_prev = k ) {
2556
 
                    k = stereo0D[i0D].neighbor[j];
2557
 
                    if ( k < 0 || k >= nNumAtoms || k_prev == k )
2558
 
                        break;
2559
 
                    /* tetrahedral atom connectivity test */
2560
 
                    if ( type == INCHI_StereoType_Tetrahedral &&
2561
 
                         k != a2 &&
2562
 
                         !is_in_the_list( at[a2].neighbor, (AT_NUMB)k, at[a2].valence) ) {
2563
 
                        break;
2564
 
                    }
2565
 
                    /* Double bond, Cumulene and allene are tested in the next if() */
2566
 
                }
2567
 
            }
2568
 
            /* find in the adjacency lists the double bond neighbor that leads to the opposite atom */
2569
 
            if ( j == 4 && (type == INCHI_StereoType_Allene ||
2570
 
                            type == INCHI_StereoType_DoubleBond) ) {
2571
 
                AT_NUMB *p1 = NULL, *p2 = NULL, *q1 = NULL, *q2 = NULL;
2572
 
                i1n = (AT_NUMB)stereo0D[i0D].neighbor[0];
2573
 
                i1  = (AT_NUMB)stereo0D[i0D].neighbor[1];
2574
 
                i2  = (AT_NUMB)stereo0D[i0D].neighbor[2];
2575
 
                i2n = (AT_NUMB)stereo0D[i0D].neighbor[3];
2576
 
                /* find q1 and q2 */
2577
 
                if ( !(q1 = is_in_the_list( at[i1].neighbor, i1n, at[i1].valence)) ||
2578
 
                     !(q2 = is_in_the_list( at[i2].neighbor, i2n, at[i2].valence)) ) {
2579
 
                    j = -2; /* error flag */
2580
 
                } else
2581
 
                /* allene or cumulene; follow double bonds from i1 to i2 */
2582
 
                if ( !(p1 = is_in_the_list( at[i1].neighbor, i2, at[i1].valence)) ) {
2583
 
                    /* at[i1] and at[i2] are not connected: can be only allene or cumulene */
2584
 
                    AT_NUMB prev, cur, next;
2585
 
                    int     num_dbond, i, next_ord, half_len;
2586
 
 
2587
 
                    cur = next = i1;
2588
 
                    len = half_len = 0;
2589
 
                    while ( len < 20 ) { /* arbitrary very high upper limit to prevent infinite loop */
2590
 
                        prev = cur;
2591
 
                        cur  = next;
2592
 
                        for ( i = 0, num_dbond = 0; i < at[cur].valence; i ++ ) {
2593
 
                            /* follow double bond path && avoid going back */
2594
 
                            if ( at[cur].bond_type[i] == BOND_TYPE_DOUBLE &&
2595
 
                                 prev != at[cur].neighbor[i] ) {
2596
 
                                next = at[cur].neighbor[i];
2597
 
                                next_ord = i;
2598
 
                                num_dbond ++;
2599
 
                            }
2600
 
                        }
2601
 
                        if ( num_dbond == 1 && next != i1 ) {
2602
 
                            len ++;
2603
 
                            if ( len == 1 ) {
2604
 
                                sb_ord_from_i1 = next_ord;
2605
 
                            }
2606
 
                            if ( type == INCHI_StereoType_Allene && next == (AT_NUMB)a2 ) {
2607
 
                                half_len = len;
2608
 
                            }
2609
 
                        } else {
2610
 
                            break;
2611
 
                        }
2612
 
                    }
2613
 
                    if ( cur == i2 && prev != cur && 0 == num_dbond && len > 1 &&
2614
 
                         (p2 = is_in_the_list( at[i2].neighbor, prev, at[i2].valence)) &&
2615
 
                         (type != INCHI_StereoType_Allene || len == 2*half_len )) {
2616
 
                        sb_ord_from_i2 = p2 - at[i2].neighbor;
2617
 
                        sn_ord_from_i1 = q1 - at[i1].neighbor;
2618
 
                        sn_ord_from_i2 = q2 - at[i2].neighbor;
2619
 
                    } else {
2620
 
                        j = -5; /* error flag */
2621
 
                    }
2622
 
                } else
2623
 
                /* allene must have been already processed, otherwise error */
2624
 
                if ( type == INCHI_StereoType_Allene ) {
2625
 
                    /* error: atoms #1 and #2 of allene are connected */
2626
 
                    j = -3; /* error flag */
2627
 
                } else
2628
 
                /* double bond only; the bond type is not checked because at the end
2629
 
                   of the normalization it may happen to be alternating */
2630
 
                if ( type == INCHI_StereoType_DoubleBond &&
2631
 
                     (p2 = is_in_the_list( at[i2].neighbor, i1, at[i2].valence) ) ) {
2632
 
                    sb_ord_from_i1 = p1 - at[i1].neighbor;
2633
 
                    sb_ord_from_i2 = p2 - at[i2].neighbor;
2634
 
                    sn_ord_from_i1 = q1 - at[i1].neighbor;
2635
 
                    sn_ord_from_i2 = q2 - at[i2].neighbor;
2636
 
                } else {
2637
 
                    j = -4; /* error flag */
2638
 
                }
2639
 
            }
2640
 
            if ( j != 4 ) {
2641
 
                char szTemp[16];
2642
 
                sprintf( szTemp, "#%d", i0D+1 );
2643
 
                MOLFILE_ERR_SET (*err, 0, "Wrong 0D stereo descriptor(s):");
2644
 
                MOLFILE_ERR_SET (*err, 0, szTemp);
2645
 
                continue; /* error */
2646
 
            }
2647
 
 
2648
 
            switch ( type ) {
2649
 
            case INCHI_StereoType_None:
2650
 
                continue;
2651
 
            case INCHI_StereoType_DoubleBond:
2652
 
            case INCHI_StereoType_Allene:
2653
 
                for ( j1 = 0; j1 < MAX_NUM_STEREO_BONDS && at[i1].sb_parity[j1]; j1 ++ )
2654
 
                    ;
2655
 
                for ( j2 = 0; j2 < MAX_NUM_STEREO_BONDS && at[i2].sb_parity[j2]; j2 ++ )
2656
 
                    ;
2657
 
                if ( j1 < MAX_NUM_STEREO_BONDS && j2 < MAX_NUM_STEREO_BONDS &&
2658
 
                     sb_ord_from_i1 >= 0 && sb_ord_from_i2 >= 0 &&
2659
 
                     sn_ord_from_i1 >= 0 && sn_ord_from_i2 >= 0) {
2660
 
 
2661
 
                    switch( parity ) {
2662
 
                    case INCHI_PARITY_ODD:
2663
 
                        at[i1].sb_parity[j1] = AB_PARITY_ODD;
2664
 
                        at[i2].sb_parity[j2] = AB_PARITY_EVEN;
2665
 
                        break;
2666
 
                    case INCHI_PARITY_EVEN:
2667
 
                        at[i1].sb_parity[j1] = AB_PARITY_ODD;
2668
 
                        at[i2].sb_parity[j2] = AB_PARITY_ODD;
2669
 
                        break;                    
2670
 
                    case INCHI_PARITY_UNDEFINED:
2671
 
                        at[i1].sb_parity[j1] = AB_PARITY_UNDF;
2672
 
                        at[i2].sb_parity[j2] = AB_PARITY_UNDF;
2673
 
                        break;
2674
 
                    default:
2675
 
                        if ( parity == INCHI_PARITY_UNKNOWN )
2676
 
                        {
2677
 
                            at[i1].sb_parity[j1] = vABParityUnknown;
2678
 
                            at[i2].sb_parity[j2] = vABParityUnknown;
2679
 
                        }
2680
 
                        else
2681
 
                        {
2682
 
                        at[i1].sb_parity[j1] = AB_PARITY_NONE;
2683
 
                        at[i2].sb_parity[j2] = AB_PARITY_NONE;
2684
 
                        }
2685
 
                        break;
2686
 
                    }
2687
 
 
2688
 
                    switch( parityNM ) {
2689
 
                    case INCHI_PARITY_ODD:
2690
 
                        at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2691
 
                        at[i2].sb_parity[j2] |= AB_PARITY_EVEN << SB_PARITY_SHFT;
2692
 
                        break;
2693
 
                    case INCHI_PARITY_EVEN:
2694
 
                        at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2695
 
                        at[i2].sb_parity[j2] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2696
 
                        break;
2697
 
                    case INCHI_PARITY_UNDEFINED:
2698
 
                        at[i1].sb_parity[j1] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
2699
 
                        at[i2].sb_parity[j2] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
2700
 
                        break;
2701
 
                    default:
2702
 
                        if ( parityNM == INCHI_PARITY_UNKNOWN )
2703
 
                        {
2704
 
                            at[i1].sb_parity[j1] |= vABParityUnknown << SB_PARITY_SHFT;
2705
 
                            at[i2].sb_parity[j2] |= vABParityUnknown << SB_PARITY_SHFT;
2706
 
                        }
2707
 
                        break;
2708
 
                    }
2709
 
 
2710
 
                    at[i1].sb_ord[j1]         = sb_ord_from_i1;
2711
 
                    at[i1].sn_ord[j1]         = sn_ord_from_i1;
2712
 
                    at[i1].sn_orig_at_num[j1] = at[i1n].orig_at_number;
2713
 
 
2714
 
                    at[i2].sb_ord[j2]         = sb_ord_from_i2;
2715
 
                    at[i2].sn_ord[j2]         = sn_ord_from_i2;
2716
 
                    at[i2].sn_orig_at_num[j2] = at[i2n].orig_at_number;
2717
 
                }
2718
 
                break;
2719
 
            case INCHI_StereoType_Tetrahedral:
2720
 
                    switch( parity ) {
2721
 
                    case INCHI_PARITY_ODD:
2722
 
                        at[a2].p_parity = AB_PARITY_ODD;
2723
 
                        break;
2724
 
                    case INCHI_PARITY_EVEN:
2725
 
                        at[a2].p_parity = AB_PARITY_EVEN;
2726
 
                        break;
2727
 
                    case INCHI_PARITY_UNDEFINED:
2728
 
                        at[a2].p_parity = AB_PARITY_UNDF;
2729
 
                        break;
2730
 
                    default:
2731
 
                        if (parity == INCHI_PARITY_UNKNOWN )
2732
 
                        {
2733
 
                            at[a2].p_parity = vABParityUnknown;
2734
 
                            break;
2735
 
                        }
2736
 
                        else
2737
 
                            continue;
2738
 
                    }
2739
 
                for ( j = 0; j < 4; j ++ ) {
2740
 
                    k = stereo0D[i0D].neighbor[j];
2741
 
                    at[a2].p_orig_at_num[j] = at[k].orig_at_number;
2742
 
                }
2743
 
                break;
2744
 
            default:
2745
 
                break;
2746
 
            }
2747
 
        }
2748
 
        /* take care of Unknown stereobonds:                                     */
2749
 
        /* copy their Unknown stereo descriptors to at->bond_stereo (2005-03-01) */
2750
 
        /* Note: to this stage, unk/undef set to what was requested              */
2751
 
        /*( through vABParityUnknown )  (2009-12-12)                             */
2752
 
        FixUnkn0DStereoBonds(at, nNumAtoms);
2753
 
 
2754
 
#ifdef INCHI_LIBRARY
2755
 
 
2756
 
        if ( k = ReconcileAllCmlBondParities( at, nNumAtoms, 0 ) ) {
2757
 
            char szErrCode[16];
2758
 
            sprintf( szErrCode, "%d", k);
2759
 
            AddMOLfileError( pStrErr, "0D Parities Reconciliation failed:" );
2760
 
            AddMOLfileError( pStrErr, szErrCode );
2761
 
        }
2762
 
 
2763
 
#endif
2764
 
 
2765
 
    }
2766
 
    return 0;
2767
 
}
2768
 
 
2769
 
#endif
2770