Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : : #include <limits.h>
8 : : #include <math.h>
9 : : #include <float.h>
10 : : #include <string.h>
11 : : #include <ctype.h>
12 : :
13 : : #include "mode.h"
14 : :
15 : : #include "ichierr.h"
16 : : #include "extr_ct.h"
17 : : #include "ichi_io.h"
18 : :
19 : : #include "inchi_api.h"
20 : : #include "readinch.h"
21 : :
22 : : #include "bcf_s.h"
23 : :
24 : : #define NO_ATOM (-1) /* non-existent (central) atom */
25 : :
26 : :
27 : : #ifndef AB_MAX_WELL_DEFINED_PARITY
28 : : #define AB_MAX_WELL_DEFINED_PARITY inchi_max(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* 1, 2 => well defined parities, uncluding 'unknown' */
29 : : #endif
30 : :
31 : : #ifndef AB_MIN_WELL_DEFINED_PARITY
32 : : #define AB_MIN_WELL_DEFINED_PARITY inchi_min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) */
33 : : #endif
34 : :
35 : :
36 : : #if ( defined(TARGET_API_LIB) || defined(TARGET_EXE_USING_API) )
37 : :
38 : : #ifndef AB_PARITY_UNKN
39 : : #define AB_PARITY_UNKN 3 /* 3 => user marked as unknown parity */
40 : : #endif
41 : : #ifndef AB_PARITY_UNDF
42 : : #define AB_PARITY_UNDF 4 /* 4 => parity cannot be defined because of symmetry or not well defined geometry */
43 : : #endif
44 : :
45 : : #define ATOM_PARITY_WELL_DEF(X) (AB_MIN_WELL_DEFINED_PARITY <= (X) && (X) <= AB_MAX_WELL_DEFINED_PARITY)
46 : :
47 : : #define SB_PARITY_FLAG 0x38 /* disconnected structure has undef. parity */
48 : :
49 : : #define SB_PARITY_SHFT 3
50 : :
51 : : #define SB_PARITY_MASK 0x07
52 : :
53 : : #define SB_PARITY_1(X) (X & SB_PARITY_MASK) /* refers to connected structure */
54 : :
55 : : #define SB_PARITY_2(X) (((X) >> SB_PARITY_SHFT) & SB_PARITY_MASK) /* refers to connected structure */
56 : :
57 : :
58 : :
59 : : #endif /* #if ( defined(TARGET_API_LIB) || defined(TARGET_EXE_USING_API) ) */
60 : :
61 : :
62 : : #if ( defined( TARGET_LIB_FOR_WINCHI ) || defined(TARGET_EXE_STANDALONE) )
63 : : int Extract0DParities( inp_ATOM *at,
64 : : int nNumAtoms,
65 : : inchi_Stereo0D *stereo0D,
66 : : int num_stereo0D,
67 : : char *pStrErr,
68 : : int *err,
69 : : int vABParityUnknown );
70 : : #endif
71 : :
72 : :
73 : : void find_and_interpret_structure_header( char *szLine,
74 : : char *pSdfLabel,
75 : : char *pSdfValue,
76 : : unsigned long *Id,
77 : : int hlen,
78 : : ReadINCHI_CtlData *ir );
79 : :
80 : :
81 : :
82 : : /****************************************************************************/
83 : 4 : inchi_Stereo0D * CreateInchi_Stereo0D( int num_stereo0D )
84 : : {
85 : 4 : return (inchi_Stereo0D*) inchi_calloc( num_stereo0D, sizeof( inchi_Stereo0D ) );
86 : : }
87 : :
88 : :
89 : : /****************************************************************************/
90 : 14 : void FreeInchi_Stereo0D( inchi_Stereo0D **stereo0D )
91 : : {
92 [ + - + + ]: 14 : if (stereo0D && *stereo0D)
93 : : {
94 [ + - ]: 4 : inchi_free( *stereo0D );
95 : 4 : *stereo0D = NULL;
96 : : }
97 : 14 : }
98 : :
99 : :
100 : : /****************************************************************************/
101 : 5 : int Extract0DParities( inp_ATOM *at,
102 : : int nNumAtoms,
103 : : inchi_Stereo0D *stereo0D,
104 : : int num_stereo0D,
105 : : char *pStrErr,
106 : : int *err,
107 : : int vABParityUnknown )
108 : : {
109 : :
110 : : /*
111 : : vABParityUnknown holds actual value of an internal constant signifying
112 : : unknown parity: either the same as for undefined parity (default==standard)
113 : : or a specific one (non-std; requested by SLUUD switch).
114 : : */
115 [ + + + - ]: 5 : if (stereo0D && num_stereo0D > 0)
116 : : {
117 : : int i0D, a2, k, k_prev, type, j, j1, j2, len, parity, parityNM;
118 : : int sb_ord_from_i1, sb_ord_from_i2, sn_ord_from_i1, sn_ord_from_i2;
119 : : AT_NUMB i1n, i2n, i1, i2;
120 : :
121 [ + + ]: 2 : for (i0D = 0; i0D < num_stereo0D; i0D++)
122 : : {
123 : 1 : parity = ( stereo0D[i0D].parity & SB_PARITY_MASK );
124 : 1 : parityNM = ( stereo0D[i0D].parity & SB_PARITY_FLAG ) >> SB_PARITY_SHFT;
125 : :
126 [ + - - + ]: 1 : if (parity == INCHI_PARITY_NONE ||
127 [ # # # # ]: 0 : (parity != INCHI_PARITY_ODD && parity != INCHI_PARITY_EVEN &&
128 [ # # ]: 0 : parity != INCHI_PARITY_UNKNOWN && parity != INCHI_PARITY_UNDEFINED)) /* djb-rwth: addressing LLVM warning */
129 : 0 : {
130 : : char szTemp[16];
131 : 0 : sprintf(szTemp, "#%d", i0D + 1);
132 : 0 : TREAT_ERR( *err, 0, "Wrong 0D stereo descriptor(s):" );
133 : 0 : TREAT_ERR( *err, 0, szTemp );
134 : 0 : continue; /* warning */
135 : : }
136 : :
137 : 1 : type = stereo0D[i0D].type;
138 : 1 : a2 = stereo0D[i0D].central_atom; /* central atom or -1 */
139 : 1 : j = -1;
140 : : /* djb-rwth: removing redundant code */
141 : 1 : sb_ord_from_i1 = sb_ord_from_i2 = sn_ord_from_i1 = sn_ord_from_i2 = -1;
142 : 1 : i1n = i2n = i1 = i2 = MAX_ATOMS + 1;
143 : :
144 : : /* Tetrahedral and allene need a valid centre: at[a2] is read below (GH #324) */
145 [ - + - - ]: 1 : if (( ( type == INCHI_StereoType_Tetrahedral ||
146 [ - + ]: 1 : type == INCHI_StereoType_Allene ) &&
147 [ - - - + ]: 1 : 0 <= a2 && a2 < nNumAtoms) ||
148 [ # # ]: 0 : (type == INCHI_StereoType_DoubleBond &&
149 : : a2 == NO_ATOM)) /* djb-rwth: addressing LLVM warning */
150 : : {
151 : : /* test the quadruplet */
152 [ # # ]: 0 : for (j = 0, k_prev = -1; j < 4; j++, k_prev = k)
153 : : {
154 : 0 : k = stereo0D[i0D].neighbor[j];
155 [ # # # # : 0 : if (k < 0 || k >= nNumAtoms || k_prev == k)
# # ]
156 : : break;
157 : : /* tetrahedral atom connectivity test */
158 [ # # # # ]: 0 : if (type == INCHI_StereoType_Tetrahedral &&
159 [ # # ]: 0 : k != a2 &&
160 : 0 : !is_in_the_list( at[a2].neighbor, (AT_NUMB) k, at[a2].valence ))
161 : : {
162 : 0 : break;
163 : : }
164 : : /* Double bond, Cumulene and allene are tested in the next if() */
165 : : }
166 : : }
167 : :
168 : : /* Find in the adjacency lists the double bond neighbor that leads to the opposite atom */
169 [ - + - - : 1 : if (j == 4 && ( type == INCHI_StereoType_Allene ||
- - ]
170 : : type == INCHI_StereoType_DoubleBond ))
171 : : {
172 : 0 : AT_NUMB *p1 = NULL, *p2 = NULL, *q1 = NULL, *q2 = NULL;
173 : 0 : i1n = (AT_NUMB) stereo0D[i0D].neighbor[0];
174 : 0 : i1 = (AT_NUMB) stereo0D[i0D].neighbor[1];
175 : 0 : i2 = (AT_NUMB) stereo0D[i0D].neighbor[2];
176 : 0 : i2n = (AT_NUMB) stereo0D[i0D].neighbor[3];
177 : :
178 : : /* find q1 and q2 */
179 [ # # # # ]: 0 : if (!( q1 = is_in_the_list( at[i1].neighbor, i1n, at[i1].valence ) ) ||
180 : 0 : !( q2 = is_in_the_list( at[i2].neighbor, i2n, at[i2].valence ) ))
181 : : {
182 : 0 : j = -2; /* error flag */
183 : : }
184 : : else
185 : : {
186 : : /* allene or cumulene; follow double bonds from i1 to i2 */
187 [ # # ]: 0 : if (!( p1 = is_in_the_list( at[i1].neighbor, i2, at[i1].valence ) ))
188 : : {
189 : : /* at[i1] and at[i2] are not connected: can be only allene or cumulene */
190 : :
191 : : AT_NUMB prev, cur, next;
192 : : int num_dbond, i, next_ord, half_len;
193 : :
194 : 0 : cur = next = i1;
195 : 0 : len = half_len = 0;
196 [ # # ]: 0 : while (len < 20)
197 : : {
198 : : /* arbitrary very high upper limit to prevent infinite loop */
199 : 0 : prev = cur;
200 : 0 : cur = next;
201 : :
202 [ # # ]: 0 : for (i = 0, num_dbond = 0; i < at[cur].valence; i++)
203 : : {
204 : : /* follow double bond path && avoid going back */
205 [ # # ]: 0 : if (at[cur].bond_type[i] == BOND_TYPE_DOUBLE &&
206 [ # # ]: 0 : prev != at[cur].neighbor[i])
207 : : {
208 : 0 : next = at[cur].neighbor[i];
209 : 0 : next_ord = i;
210 : 0 : num_dbond++;
211 : : }
212 : : }
213 : :
214 [ # # # # ]: 0 : if (num_dbond == 1 && next != i1)
215 : : {
216 : 0 : len++;
217 [ # # ]: 0 : if (len == 1)
218 : 0 : sb_ord_from_i1 = next_ord;
219 : :
220 [ # # # # ]: 0 : if (type == INCHI_StereoType_Allene && next == (AT_NUMB) a2)
221 : 0 : half_len = len;
222 : : }
223 : : else
224 : : break;
225 : : }
226 : :
227 [ # # # # : 0 : if (cur == i2 && prev != cur && 0 == num_dbond && len > 1 &&
# # # # #
# ]
228 [ # # ]: 0 : ( p2 = is_in_the_list( at[i2].neighbor, prev, at[i2].valence ) ) &&
229 [ # # ]: 0 : ( type != INCHI_StereoType_Allene || len == 2 * half_len ))
230 : : {
231 : 0 : sb_ord_from_i2 = p2 - at[i2].neighbor;
232 : 0 : sn_ord_from_i1 = q1 - at[i1].neighbor;
233 : 0 : sn_ord_from_i2 = q2 - at[i2].neighbor;
234 : : }
235 : : else
236 : : {
237 : 0 : j = -5; /* error flag */
238 : : }
239 : : }
240 : : else
241 : : {
242 : : /* allene must have been already processed, otherwise error */
243 [ # # ]: 0 : if (type == INCHI_StereoType_Allene)
244 : : {
245 : : /* error: atoms #1 and #2 of allene are connected */
246 : 0 : j = -3; /* error flag */
247 : : }
248 : : else
249 : : {
250 : : /* double bond only; the bond type is not checked because at the end
251 : : of the normalization it may happen to be alternating */
252 [ # # # # ]: 0 : if (type == INCHI_StereoType_DoubleBond &&
253 : 0 : ( p2 = is_in_the_list( at[i2].neighbor, i1, at[i2].valence ) ))
254 : : {
255 : 0 : sb_ord_from_i1 = p1 - at[i1].neighbor;
256 : 0 : sb_ord_from_i2 = p2 - at[i2].neighbor;
257 : 0 : sn_ord_from_i1 = q1 - at[i1].neighbor;
258 : 0 : sn_ord_from_i2 = q2 - at[i2].neighbor;
259 : : }
260 : : else
261 : : {
262 : 0 : j = -4; /* error flag */
263 : : }
264 : : }
265 : : }
266 : : }
267 : : }
268 : :
269 [ + - ]: 1 : if (j != 4)
270 : 1 : {
271 : : char szTemp[16];
272 : 1 : sprintf(szTemp, "#%d", i0D + 1);
273 : 1 : TREAT_ERR( *err, 0, "Wrong 0D stereo descriptor(s):" );
274 : 1 : TREAT_ERR( *err, 0, szTemp );
275 : 1 : continue; /* error */
276 : : }
277 : :
278 [ # # # # ]: 0 : switch (type)
279 : : {
280 : 0 : case INCHI_StereoType_None:
281 : 0 : continue;
282 : 0 : case INCHI_StereoType_DoubleBond:
283 : : case INCHI_StereoType_Allene:
284 [ # # # # ]: 0 : for (j1 = 0; j1 < MAX_NUM_STEREO_BONDS && at[i1].sb_parity[j1]; j1++)
285 : : {
286 : : ;
287 : : }
288 [ # # # # ]: 0 : for (j2 = 0; j2 < MAX_NUM_STEREO_BONDS && at[i2].sb_parity[j2]; j2++)
289 : : {
290 : : ;
291 : : }
292 [ # # # # : 0 : if (j1 < MAX_NUM_STEREO_BONDS && j2 < MAX_NUM_STEREO_BONDS &&
# # ]
293 [ # # # # ]: 0 : sb_ord_from_i1 >= 0 && sb_ord_from_i2 >= 0 &&
294 [ # # ]: 0 : sn_ord_from_i1 >= 0 && sn_ord_from_i2 >= 0)
295 : : {
296 [ # # # # ]: 0 : switch (parity)
297 : : {
298 : 0 : case INCHI_PARITY_ODD:
299 : 0 : at[i1].sb_parity[j1] = AB_PARITY_ODD;
300 : 0 : at[i2].sb_parity[j2] = AB_PARITY_EVEN;
301 : 0 : break;
302 : 0 : case INCHI_PARITY_EVEN:
303 : 0 : at[i1].sb_parity[j1] = AB_PARITY_ODD;
304 : 0 : at[i2].sb_parity[j2] = AB_PARITY_ODD;
305 : 0 : break;
306 : 0 : case INCHI_PARITY_UNDEFINED:
307 : 0 : at[i1].sb_parity[j1] = AB_PARITY_UNDF;
308 : 0 : at[i2].sb_parity[j2] = AB_PARITY_UNDF;
309 : 0 : break;
310 : 0 : default:
311 [ # # ]: 0 : if (parity == INCHI_PARITY_UNKNOWN)
312 : : {
313 : 0 : at[i1].sb_parity[j1] = vABParityUnknown;
314 : 0 : at[i2].sb_parity[j2] = vABParityUnknown;
315 : : }
316 : : else
317 : : {
318 : 0 : at[i1].sb_parity[j1] = AB_PARITY_NONE;
319 : 0 : at[i2].sb_parity[j2] = AB_PARITY_NONE;
320 : : }
321 : 0 : break;
322 : : }
323 [ # # # # ]: 0 : switch (parityNM)
324 : : {
325 : 0 : case INCHI_PARITY_ODD:
326 : 0 : at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
327 : 0 : at[i2].sb_parity[j2] |= AB_PARITY_EVEN << SB_PARITY_SHFT;
328 : 0 : break;
329 : 0 : case INCHI_PARITY_EVEN:
330 : 0 : at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
331 : 0 : at[i2].sb_parity[j2] |= AB_PARITY_ODD << SB_PARITY_SHFT;
332 : 0 : break;
333 : 0 : case INCHI_PARITY_UNDEFINED:
334 : 0 : at[i1].sb_parity[j1] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
335 : 0 : at[i2].sb_parity[j2] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
336 : 0 : break;
337 : 0 : default:
338 [ # # ]: 0 : if (parityNM == INCHI_PARITY_UNKNOWN)
339 : : {
340 : 0 : at[i1].sb_parity[j1] |= vABParityUnknown << SB_PARITY_SHFT;
341 : 0 : at[i2].sb_parity[j2] |= vABParityUnknown << SB_PARITY_SHFT;
342 : : }
343 : 0 : break;
344 : : }
345 : 0 : at[i1].sb_ord[j1] = sb_ord_from_i1;
346 : 0 : at[i1].sn_ord[j1] = sn_ord_from_i1;
347 : 0 : at[i1].sn_orig_at_num[j1] = at[i1n].orig_at_number;
348 : :
349 : 0 : at[i2].sb_ord[j2] = sb_ord_from_i2;
350 : 0 : at[i2].sn_ord[j2] = sn_ord_from_i2;
351 : 0 : at[i2].sn_orig_at_num[j2] = at[i2n].orig_at_number;
352 : : }
353 : 0 : break;
354 : 0 : case INCHI_StereoType_Tetrahedral:
355 [ # # # # ]: 0 : switch (parity)
356 : : {
357 : 0 : case INCHI_PARITY_ODD:
358 : 0 : at[a2].p_parity = AB_PARITY_ODD;
359 : 0 : break;
360 : 0 : case INCHI_PARITY_EVEN:
361 : 0 : at[a2].p_parity = AB_PARITY_EVEN;
362 : 0 : break;
363 : 0 : case INCHI_PARITY_UNDEFINED:
364 : 0 : at[a2].p_parity = AB_PARITY_UNDF;
365 : 0 : break;
366 : 0 : default:
367 [ # # ]: 0 : if (parity == INCHI_PARITY_UNKNOWN)
368 : : {
369 : 0 : at[a2].p_parity = vABParityUnknown;
370 : 0 : break;
371 : : }
372 : : else
373 : : {
374 : 0 : continue;
375 : : }
376 : : }
377 [ # # ]: 0 : for (j = 0; j < 4; j++)
378 : : {
379 : 0 : k = stereo0D[i0D].neighbor[j];
380 : 0 : at[a2].p_orig_at_num[j] = at[k].orig_at_number;
381 : : }
382 : 0 : break;
383 : :
384 : 0 : default:
385 : 0 : break;
386 : : }
387 : : }
388 : : /* take care of Unknown stereobonds: */
389 : : /* copy their Unknown stereo descriptors to at->bond_stereo (2005-03-01) */
390 : : /* Note: to this stage, unk/undef set to what was requested */
391 : : /*( through vABParityUnknown ) (2009-12-12) */
392 : 1 : FixUnkn0DStereoBonds( at, nNumAtoms );
393 : : #ifdef TARGET_API_LIB
394 : :
395 [ - + ]: 1 : if ((k = ReconcileAllCmlBondParities( at, nNumAtoms, 0 ))) /* djb-rwth: addressing LLVM warning */
396 : : {
397 : : char szErrCode[16];
398 : 0 : sprintf( szErrCode, "%d", k );
399 : 0 : AddErrorMessage( pStrErr, "0D Parities Reconciliation failed:" );
400 : 0 : AddErrorMessage( pStrErr, szErrCode );
401 : : }
402 : :
403 : : #endif
404 : : }
405 : :
406 : 5 : return 0;
407 : : }
408 : :
409 : :
410 : : /****************************************************************************/
411 : 11 : char* FindToken( INCHI_IOSTREAM *inp_file,
412 : : int *bTooLongLine,
413 : : const char *sToken,
414 : : int lToken,
415 : : char *szLine,
416 : : int nLenLine,
417 : : char *p,
418 : : int *res )
419 : : {
420 : : char *q;
421 : : int res2;
422 : :
423 [ - + ]: 11 : while (!( q = strstr( p, sToken ) ))
424 : : {
425 [ # # # # ]: 0 : if (( q = strrchr( p, '/' ) ) && ( q + lToken > szLine + *res ))
426 : : {
427 : 0 : *res -= q - szLine; /* res = the length of the szLine to be left in */
428 : 0 : memmove(szLine, q, (long long)*res + 1); /* djb-rwth: cast operator added */
429 : : }
430 : : else
431 : : {
432 : 0 : *res = 0;
433 : : }
434 : :
435 : 0 : res2 = inchi_ios_getsTab1( szLine + *res, nLenLine - *res - 1,
436 : : inp_file, bTooLongLine );
437 : :
438 [ # # # # ]: 0 : if (!*bTooLongLine || 0 > res2)
439 : : {
440 : : /* the line is over or end of file */
441 : 0 : return NULL;
442 : : }
443 : : else
444 : : {
445 : 0 : *res += res2;
446 : 0 : p = szLine;
447 : : }
448 : : }
449 : :
450 : 11 : return q + lToken;
451 : : }
452 : :
453 : :
454 : : /****************************************************************************/
455 : 33 : char *LoadLine( INCHI_IOSTREAM *inp_file,
456 : : int *bTooLongLine,
457 : : int *bItemIsOver,
458 : : char **s,
459 : : char *szLine,
460 : : int nLenLine,
461 : : int nMinLen2Load,
462 : : char *p,
463 : : int *res )
464 : : {
465 : :
466 : 33 : int pos = p - szLine, res2;
467 : :
468 [ - + - - ]: 33 : if (!*bItemIsOver && nLenLine - ( *res - pos ) > nMinLen2Load)
469 : : {
470 : : /* load the next portion if possible */
471 : :
472 [ # # ]: 0 : if (pos)
473 : : {
474 : 0 : *res -= pos;
475 : 0 : memmove(szLine, p, (long long)*res + 1); /* djb-rwth: cast operator added */
476 : 0 : p = szLine;
477 [ # # ]: 0 : if (*s)
478 : : {
479 : 0 : *s -= pos;
480 : : }
481 : :
482 : : /* djb-rwth: removing redundant code */
483 : : }
484 : :
485 : 0 : res2 = inchi_ios_getsTab1( szLine + *res,
486 : 0 : nLenLine - *res - 1,
487 : : inp_file, bTooLongLine );
488 : :
489 [ # # ]: 0 : if (res2 > 0)
490 : : {
491 [ # # # # ]: 0 : *bItemIsOver = ( ( *s = strchr( p + *res, '/' ) ) || !*bTooLongLine );
492 : 0 : *res += res2;
493 : : }
494 : : else
495 : : {
496 : 0 : *bItemIsOver = 1;
497 : : }
498 : : }
499 : :
500 : 33 : return p;
501 : : }
502 : :
503 : :
504 : : /*****************************************************************************/
505 : : #define AT_BONDS_VAL(AT,I) AT[I].chem_bonds_valence
506 : : #define ISOLATED_ATOM 15
507 : : #define NUM_ISO_Hk(AT,I,K) AT[I].num_iso_H[K]
508 : : #define inchi_NUMH2(AT,N) NUMH(AT,N)
509 : : #define AT_NUM_BONDS(AT) (AT).valence
510 : : #define IS_METAL_ATOM(AT,I) is_el_a_metal( AT[I].el_number )
511 : :
512 : :
513 : : /****************************************************************************/
514 : : char szLine_i2i[INCHI_LINE_LEN]; /* djb-rwth: placed as a global variable to avoid function buffer issues */
515 : 0 : int InchiToInpAtom( INCHI_IOSTREAM *inp_file,
516 : : MOL_COORD **szCoord,
517 : : int bDoNotAddH,
518 : : int vABParityUnknown,
519 : : INPUT_TYPE nInputType,
520 : : inp_ATOM **at,
521 : : int max_num_at,
522 : : int *num_dimensions,
523 : : int *num_bonds,
524 : : char *pSdfLabel,
525 : : char *pSdfValue,
526 : : unsigned long *Id,
527 : : INCHI_MODE *pInpAtomFlags,
528 : : int *err,
529 : : char *pStrErr )
530 : : {
531 : 0 : int num_atoms = 0, bItemIsOver; /* djb-rwth: removing redundant variables */
532 : : int i, k, k2, res, bond_type, bond_stereo1, bond_stereo2, bond_char, neigh, bond_parity, bond_parityNM;
533 : : /* djb-rwth: removing redundant variables */
534 : : char *p, *q, *s, parity;
535 : 0 : int b2D = 0, b3D = 0, b23D, nNumBonds = 0, bNonZeroXYZ, bNonMetal;
536 : 0 : int len_stereo0D = 0, max_len_stereo0D = 0;
537 : 0 : inp_ATOM *atom = NULL;
538 : 0 : MOL_COORD *pszCoord = NULL;
539 : 0 : INCHI_MODE InpAtomFlags = 0; /* 0 or FLAG_INP_AT_NONCHIRAL or FLAG_INP_AT_CHIRAL */
540 : 0 : inchi_Stereo0D *atom_stereo0D = NULL;
541 : : static const char szIsoH[] = "hdt";
542 : : /* plain tags */
543 : : static const char sStructHdrPln[] = "Structure:";
544 : : static char sStructHdrPlnAuxStart[64] = ""; /*"$1.1Beta/";*/
545 : : static int lenStructHdrPlnAuxStart = 0;
546 : : static const char sStructHdrPlnRevAt[] = "/rA:";
547 : : static const char sStructHdrPlnRevBn[] = "/rB:";
548 : : static const char sStructHdrPlnRevXYZ[] = "/rC:";
549 : : const char *sToken;
550 : : int lToken, len, hlen;
551 : :
552 : : ReadINCHI_CtlData ir;
553 : :
554 [ # # ]: 0 : if (!lenStructHdrPlnAuxStart)
555 : : {
556 : 0 : lenStructHdrPlnAuxStart = sprintf(sStructHdrPlnAuxStart, "AuxInfo=");
557 : : }
558 : :
559 [ # # ]: 0 : if (at)
560 : : {
561 [ # # # # ]: 0 : if (*at && max_num_at)
562 : 0 : memset(*at, 0, max_num_at * sizeof(**at)); /* djb-rwth: memset_s C11/Annex K variant? */
563 [ # # # # ]: 0 : if (szCoord && *szCoord)
564 : : {
565 [ # # ]: 0 : inchi_free(*szCoord);
566 : 0 : *szCoord = NULL;
567 : : }
568 : : }
569 : : /* djb-rwth: removing redundant code */
570 : :
571 : 0 : ir.bHeaderRead = ir.bErrorMsg = ir.bRestoreInfo = 0;
572 : 0 : *num_dimensions = *num_bonds = 0;
573 : :
574 : :
575 [ # # ]: 0 : if (nInputType != INPUT_INCHI_PLAIN)
576 : : {
577 : 0 : return num_atoms;
578 : : }
579 : :
580 : :
581 : : /*
582 : : Extract reversibility info from plain text INChI format
583 : : */
584 : :
585 : 0 : ir.bHeaderRead = 0; /* djb-rwth: removing redundant code */
586 [ # # ]: 0 : while (0 < (res = inchi_ios_getsTab(szLine_i2i, sizeof(szLine_i2i) - 1, inp_file, &ir.bTooLongLine)))
587 : : {
588 : :
589 [ # # ]: 0 : if (!ir.bTooLongLine &&
590 [ # # ]: 0 : (hlen = sizeof(sStructHdrPln) - 1, !memcmp(szLine_i2i, sStructHdrPln, hlen)))
591 : :
592 : : {
593 : 0 : num_atoms = 0;
594 : 0 : find_and_interpret_structure_header(szLine_i2i, pSdfLabel, pSdfValue,
595 : : Id, hlen, &ir);
596 : : }
597 : :
598 [ # # ]: 0 : else if (!memcmp(szLine_i2i, sStructHdrPlnAuxStart, lenStructHdrPlnAuxStart))
599 : : {
600 : : /* Reject to deal with polymers for now */
601 [ # # ]: 0 : if (strstr(szLine_i2i, "/Z:"))
602 : : {
603 : 0 : *err = INCHI_INP_ERROR_ERR;
604 : 0 : num_atoms = INCHI_INP_ERROR_RET;
605 : 0 : TREAT_ERR(*err, 0, "Reading polymer AuxInfo is not supported yet");
606 : 0 : goto bypass_end_of_INChI_plain;
607 : : }
608 : :
609 : : /* Found the header of the AuxInfo, read AuxInfo head of the line */
610 [ # # ]: 0 : if (!ir.bHeaderRead)
611 : : {
612 : 0 : ir.ulongID = 0LU;
613 [ # # ]: 0 : if (Id)
614 : : {
615 : 0 : *Id = ir.ulongID;
616 : : }
617 [ # # ]: 0 : if (pSdfLabel)
618 : : {
619 : 0 : pSdfLabel[0] = '\0';
620 : : }
621 [ # # ]: 0 : if (pSdfValue)
622 : : {
623 : 0 : pSdfValue[0] = '\0';
624 : : }
625 : : }
626 : :
627 : 0 : ir.bHeaderRead = 0;
628 : :
629 : : /* Check for empty "AuxInfo=ver//" */
630 : 0 : p = strchr(szLine_i2i + lenStructHdrPlnAuxStart, '/');
631 : :
632 [ # # # # : 0 : if (p && p[1] == '/' && (!p[2] || '\n' == p[2]))
# # # # ]
633 : : {
634 : 0 : goto bypass_end_of_INChI_plain;
635 : : }
636 : :
637 : : /*
638 : : Search for atoms block (plain)
639 : : */
640 : :
641 : 0 : p = szLine_i2i;
642 : 0 : sToken = sStructHdrPlnRevAt;
643 : 0 : lToken = sizeof(sStructHdrPlnRevAt) - 1;
644 : :
645 : : /* Search for sToken in the line; load next segments of the line if sToken has not found */
646 : :
647 : 0 : p = FindToken(inp_file, &ir.bTooLongLine, sToken, lToken,
648 : : szLine_i2i, sizeof(szLine_i2i), p, &res);
649 : :
650 [ # # ]: 0 : if (!p)
651 : : {
652 : 0 : *err = INCHI_INP_ERROR_ERR;
653 : 0 : num_atoms = INCHI_INP_ERROR_RET;
654 : 0 : TREAT_ERR(*err, 0, "Missing atom data");
655 : 0 : goto bypass_end_of_INChI_plain;
656 : : }
657 : : else
658 : : {
659 : : /* atoms block started */
660 : 0 : i = 0;
661 : : /* djb-rwth: removing redundant code */
662 [ # # # # ]: 0 : bItemIsOver = (s = strchr(p, '/')) || !ir.bTooLongLine;
663 : : while (1)
664 : : {
665 : :
666 : 0 : p = LoadLine(inp_file, &ir.bTooLongLine, &bItemIsOver, &s,
667 : : szLine_i2i, sizeof(szLine_i2i), INCHI_LINE_ADD, p, &res);
668 : :
669 [ # # ]: 0 : if (!i)
670 : : {
671 : : /* allocate atom */
672 : 0 : num_atoms = strtol(p, &q, 10);
673 : :
674 [ # # # # : 0 : if (!num_atoms || !q || !*q)
# # ]
675 : : {
676 : 0 : num_atoms = 0; /* no atom data */
677 : 0 : goto bypass_end_of_INChI_plain;
678 : : }
679 : 0 : p = q;
680 : :
681 : : /* Molfile chirality flag */
682 [ # # # ]: 0 : switch (*p)
683 : : {
684 : 0 : case 'c':
685 : 0 : InpAtomFlags |= FLAG_INP_AT_CHIRAL;
686 : 0 : p++;
687 : 0 : break;
688 : 0 : case 'n':
689 : 0 : InpAtomFlags |= FLAG_INP_AT_NONCHIRAL;
690 : 0 : p++;
691 : 0 : break;
692 : : }
693 : :
694 [ # # # # ]: 0 : if (at && *at)
695 : : {
696 [ # # ]: 0 : if (num_atoms > max_num_at)
697 : : {
698 [ # # ]: 0 : inchi_free(*at);
699 : 0 : *at = NULL;
700 : : }
701 : : else
702 : : {
703 : 0 : memset(*at, 0, max_num_at * sizeof(**at)); /* djb-rwth: memset_s C11/Annex K variant? */
704 : 0 : atom = *at;
705 : : }
706 : : }
707 : :
708 [ # # # # ]: 0 : if (!at || !*at)
709 : : {
710 : :
711 : 0 : atom = CreateInpAtom(num_atoms + 1);
712 : :
713 [ # # ]: 0 : if (!atom)
714 : : {
715 : 0 : num_atoms = INCHI_INP_FATAL_RET; /* was -1; error */
716 : 0 : *err = INCHI_INP_FATAL_ERR;
717 : 0 : TREAT_ERR(*err, 0, "Out of RAM");
718 : 0 : goto bypass_end_of_INChI_plain;
719 : : }
720 : : }
721 : :
722 : : {
723 : 0 : max_len_stereo0D = num_atoms + 1;
724 : :
725 : 0 : atom_stereo0D = CreateInchi_Stereo0D(max_len_stereo0D);
726 : :
727 [ # # ]: 0 : if (!atom_stereo0D)
728 : : {
729 : 0 : num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
730 : 0 : *err = INCHI_INP_FATAL_ERR;
731 : 0 : TREAT_ERR(*err, 0, "Out of RAM");
732 : 0 : goto bypass_end_of_INChI_plain;
733 : : }
734 : : }
735 : : }
736 : :
737 : : /* element, first char */
738 [ # # # # : 0 : if (!isalpha(UCINT * p) || !isupper(UCINT * p) || i >= num_atoms)
# # ]
739 : : {
740 : : break; /* end of atoms block */
741 : : }
742 : :
743 : 0 : atom[i].elname[0] = *p++;
744 : :
745 : : /* element, second char */
746 [ # # # # ]: 0 : if (isalpha(UCINT * p) && islower(UCINT * p))
747 : : {
748 : 0 : atom[i].elname[1] = *p++;
749 : : }
750 : :
751 : 0 : atom[i].el_number = get_periodic_table_number(atom[i].elname);
752 : :
753 : : /* bonds' valence + number of non-isotopic H */
754 [ # # ]: 0 : if (isdigit(UCINT * p))
755 : : {
756 : 0 : AT_BONDS_VAL(atom, i) = (char)strtol(p, &q, 10);
757 [ # # ]: 0 : if (!AT_BONDS_VAL(atom, i))
758 : 0 : AT_BONDS_VAL(atom, i) = ISOLATED_ATOM; /* same convention as in MOLfile, found zero bonds valence */
759 : 0 : p = q;
760 : : }
761 : :
762 : : /* charge */
763 [ # # # # ]: 0 : atom[i].charge = (*p == '+') ? 1 : (*p == '-') ? -1 : 0;
764 [ # # ]: 0 : if (atom[i].charge)
765 : : {
766 : 0 : p++;
767 [ # # ]: 0 : if (isdigit(UCINT * p))
768 : : {
769 : 0 : atom[i].charge *= (S_CHAR)(strtol(p, &q, 10) & CHAR_MASK);
770 : 0 : p = q;
771 : : }
772 : : }
773 : :
774 : : /* radical */
775 [ # # ]: 0 : if (*p == '.')
776 : : {
777 : 0 : p++;
778 [ # # ]: 0 : if (isdigit(UCINT * p))
779 : : {
780 : 0 : atom[i].radical = (S_CHAR)strtol(p, &q, 10);
781 : 0 : p = q;
782 : : }
783 : : }
784 : :
785 : : /* isotopic mass */
786 [ # # ]: 0 : if (*p == 'i')
787 : : {
788 : 0 : p++;
789 [ # # ]: 0 : if (isdigit(UCINT * p))
790 : : {
791 : 0 : int mw = strtol(p, &q, 10);
792 : 0 : p = q;
793 : 0 : mw -= get_atomic_mass_from_elnum(atom[i].el_number);
794 [ # # ]: 0 : if (mw >= 0)
795 : 0 : mw++;
796 : 0 : atom[i].iso_atw_diff = mw;
797 : : }
798 : : }
799 : :
800 : : /* parity */
801 [ # # # # : 0 : switch (*p)
# ]
802 : : {
803 : 0 : case 'o':
804 : 0 : parity = INCHI_PARITY_ODD;
805 : 0 : p++;
806 : 0 : break;
807 : 0 : case 'e':
808 : 0 : parity = INCHI_PARITY_EVEN;
809 : 0 : p++;
810 : 0 : break;
811 : 0 : case 'u':
812 : 0 : parity = INCHI_PARITY_UNKNOWN;
813 : 0 : p++;
814 : 0 : break;
815 : 0 : case '?':
816 : 0 : parity = INCHI_PARITY_UNDEFINED;
817 : 0 : p++;
818 : 0 : break;
819 : 0 : default:
820 : 0 : parity = 0;
821 : 0 : break;
822 : : }
823 : :
824 [ # # ]: 0 : if (parity)
825 : : {
826 : 0 : atom_stereo0D[len_stereo0D].central_atom = i;
827 : 0 : atom_stereo0D[len_stereo0D].parity = parity;
828 : 0 : atom_stereo0D[len_stereo0D].type = INCHI_StereoType_Tetrahedral;
829 : 0 : len_stereo0D++;
830 : : }
831 : :
832 : : /* isotopic h, d, t */
833 [ # # ]: 0 : for (k = 0; k < NUM_H_ISOTOPES; k++)
834 : : {
835 [ # # ]: 0 : if (*p == szIsoH[k])
836 : : {
837 : 0 : NUM_ISO_Hk(atom, i, k) = 1;
838 : 0 : p++;
839 [ # # ]: 0 : if (isdigit(UCINT * p))
840 : : {
841 : 0 : NUM_ISO_Hk(atom, i, k) = (char)strtol(p, &q, 10);
842 : 0 : p = q;
843 : : }
844 : : }
845 : : }
846 : :
847 : 0 : i++;
848 : : }
849 : :
850 [ # # # # : 0 : if (!bItemIsOver || i != num_atoms || (s && p != s)) /* djb-rwth: addressing LLVM warning */
# # # # ]
851 : : {
852 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
853 : 0 : *err = INCHI_INP_ERROR_ERR;
854 : 0 : TREAT_ERR(*err, 0, "Wrong number of atoms");
855 : 0 : goto bypass_end_of_INChI_plain;
856 : : }
857 : : }
858 : :
859 : : /*
860 : : Search for bonds block (plain) and read it
861 : : */
862 : :
863 : : /*p = szLine;*/
864 : 0 : sToken = sStructHdrPlnRevBn;
865 : 0 : lToken = sizeof(sStructHdrPlnRevBn) - 1;
866 : :
867 : : /* Search for sToken in the line; load next segments of the line if sToken has not found */
868 : 0 : p = FindToken(inp_file, &ir.bTooLongLine, sToken, lToken, szLine_i2i, sizeof(szLine_i2i), p, &res);
869 : :
870 [ # # ]: 0 : if (!p)
871 : : {
872 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
873 : 0 : *err = INCHI_INP_ERROR_ERR;
874 : 0 : TREAT_ERR(*err, 0, "Missing bonds data");
875 : 0 : goto bypass_end_of_INChI_plain;
876 : : }
877 : : else
878 : : {
879 : : /* bonds block started */
880 : :
881 : 0 : i = 1;
882 : :
883 : : /* djb-rwth: removing redundant code */
884 : :
885 [ # # # # ]: 0 : bItemIsOver = (s = strchr(p, '/')) || !ir.bTooLongLine;
886 : :
887 [ # # ]: 0 : if (1 == num_atoms)
888 : : {
889 : : /* needed because the next '/' may be still out of szLine */
890 : :
891 : 0 : p = LoadLine(inp_file, &ir.bTooLongLine, &bItemIsOver, &s,
892 : : szLine_i2i, sizeof(szLine_i2i), INCHI_LINE_ADD, p, &res);
893 : : }
894 : :
895 [ # # ]: 0 : while (i < num_atoms)
896 : : {
897 : :
898 : 0 : p = LoadLine(inp_file, &ir.bTooLongLine, &bItemIsOver, &s,
899 : : szLine_i2i, sizeof(szLine_i2i), INCHI_LINE_ADD, p, &res);
900 : :
901 [ # # # # : 0 : if (i >= num_atoms || (s && p >= s)) /* djb-rwth: addressing LLVM warning */
# # ]
902 : : {
903 : : break; /* end of bonds (plain) */
904 : : }
905 : :
906 : : /* bond, first char */
907 [ # # ]: 0 : if (*p == ';')
908 : : {
909 : 0 : p++;
910 : 0 : i++;
911 : 0 : continue;
912 : : }
913 : :
914 [ # # ]: 0 : if (!isalpha(UCINT * p))
915 : : {
916 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
917 : 0 : *err = INCHI_INP_ERROR_ERR;
918 : 0 : TREAT_ERR(*err, 0, "Wrong bonds data");
919 : 0 : goto bypass_end_of_INChI_plain;
920 : : }
921 : :
922 : 0 : bond_char = *p++;
923 : :
924 : : /* bond parity */
925 [ # # # # : 0 : switch (*p)
# ]
926 : : {
927 : 0 : case '-':
928 : 0 : bond_parity = INCHI_PARITY_ODD;
929 : 0 : p++;
930 : 0 : break;
931 : 0 : case '+':
932 : 0 : bond_parity = INCHI_PARITY_EVEN;
933 : 0 : p++;
934 : 0 : break;
935 : 0 : case 'u':
936 : 0 : bond_parity = INCHI_PARITY_UNKNOWN;
937 : 0 : p++;
938 : 0 : break;
939 : 0 : case '?':
940 : 0 : bond_parity = INCHI_PARITY_UNDEFINED;
941 : 0 : p++;
942 : 0 : break;
943 : 0 : default:
944 : 0 : bond_parity = 0;
945 : 0 : break;
946 : : }
947 : :
948 [ # # ]: 0 : if (bond_parity)
949 : : {
950 [ # # # # : 0 : switch (*p)
# ]
951 : : {
952 : 0 : case '-':
953 : 0 : bond_parityNM = INCHI_PARITY_ODD;
954 : 0 : p++;
955 : 0 : break;
956 : 0 : case '+':
957 : 0 : bond_parityNM = INCHI_PARITY_EVEN;
958 : 0 : p++;
959 : 0 : break;
960 : 0 : case 'u':
961 : 0 : bond_parityNM = INCHI_PARITY_UNKNOWN;
962 : 0 : p++;
963 : 0 : break;
964 : 0 : case '?':
965 : 0 : bond_parityNM = INCHI_PARITY_UNDEFINED;
966 : 0 : p++;
967 : 0 : break;
968 : 0 : default:
969 : 0 : bond_parityNM = 0;
970 : 0 : break;
971 : : }
972 : : }
973 : : else
974 : : {
975 : 0 : bond_parityNM = 0;
976 : : }
977 : :
978 : : /* neighbor of the current atom */
979 [ # # ]: 0 : if (!isdigit(UCINT * p))
980 : : {
981 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
982 : 0 : *err = INCHI_INP_ERROR_ERR;
983 : 0 : TREAT_ERR(*err, 0, "Wrong bonds data");
984 : 0 : goto bypass_end_of_INChI_plain;
985 : : }
986 : :
987 : 0 : neigh = (int)strtol(p, &q, 10) - 1;
988 : :
989 : : #if ( FIX_CURE53_ISSUE_HEAP_BUFFER_OVERFLOW_INCHITOINPATOM==1 )
990 [ # # # # : 0 : if (i >= num_atoms || neigh >= num_atoms || neigh < 0)
# # ]
991 : : {
992 : : #else
993 : : if (i >= num_atoms || neigh >= num_atoms) {
994 : : #endif
995 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
996 : 0 : *err = INCHI_INP_ERROR_ERR;
997 : 0 : TREAT_ERR(*err, 0, "Bond to nonexistent atom");
998 : 0 : goto bypass_end_of_INChI_plain;
999 : : }
1000 : :
1001 : 0 : p = q;
1002 : 0 : bond_stereo1 = bond_stereo2 = 0;
1003 : :
1004 : : /* bond type & 2D stereo */
1005 [ # # # # : 0 : switch (bond_char)
# # # # #
# # # ]
1006 : : {
1007 : 0 : case 'v':
1008 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1009 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1010 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1011 : 0 : break;
1012 : 0 : case 'V':
1013 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1014 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1015 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1016 : 0 : break;
1017 : 0 : case 'w':
1018 : 0 : bond_type = INCHI_BOND_TYPE_DOUBLE;
1019 : 0 : bond_stereo1 =
1020 : 0 : bond_stereo2 = INCHI_BOND_STEREO_DOUBLE_EITHER;
1021 : 0 : break;
1022 : 0 : case 's':
1023 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1024 : 0 : break;
1025 : 0 : case 'd':
1026 : 0 : bond_type = INCHI_BOND_TYPE_DOUBLE;
1027 : 0 : break;
1028 : 0 : case 't':
1029 : 0 : bond_type = INCHI_BOND_TYPE_TRIPLE;
1030 : 0 : break;
1031 : 0 : case 'a':
1032 : 0 : bond_type = INCHI_BOND_TYPE_ALTERN;
1033 : 0 : break;
1034 : 0 : case 'p':
1035 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1036 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1UP;
1037 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2UP;
1038 : 0 : break;
1039 : 0 : case 'P':
1040 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1041 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2UP;
1042 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1UP;
1043 : 0 : break;
1044 : 0 : case 'n':
1045 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1046 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1DOWN;
1047 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2DOWN;
1048 : 0 : break;
1049 : 0 : case 'N':
1050 : 0 : bond_type = INCHI_BOND_TYPE_SINGLE;
1051 : 0 : bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2DOWN;
1052 : 0 : bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1DOWN;
1053 : 0 : break;
1054 : 0 : default:
1055 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
1056 : 0 : *err = INCHI_INP_ERROR_ERR;
1057 : 0 : TREAT_ERR(*err, 0, "Wrong bond type");
1058 : 0 : goto bypass_end_of_INChI_plain;
1059 : : }
1060 : :
1061 : 0 : k = AT_NUM_BONDS(atom[i])++; /* AT_NUM_BONDS(AT) ==> (AT).valence */
1062 : :
1063 : 0 : atom[i].bond_type[k] = bond_type;
1064 : 0 : atom[i].bond_stereo[k] = bond_stereo1;
1065 : 0 : atom[i].neighbor[k] = (AT_NUMB)neigh;
1066 : :
1067 : 0 : k2 = AT_NUM_BONDS(atom[neigh])++; /* AT_NUM_BONDS(AT) ==> (AT).valence */
1068 : 0 : atom[neigh].bond_type[k2] = bond_type;
1069 : 0 : atom[neigh].bond_stereo[k2] = bond_stereo2;
1070 : 0 : atom[neigh].neighbor[k2] = (AT_NUMB)i;
1071 : :
1072 : 0 : bond_parity |= (bond_parityNM << SB_PARITY_SHFT);
1073 : :
1074 [ # # ]: 0 : if (bond_parity)
1075 : : {
1076 [ # # ]: 0 : if (max_len_stereo0D <= len_stereo0D)
1077 : : {
1078 : : /* realloc atom_Stereo0D */
1079 : :
1080 : 0 : inchi_Stereo0D* new_atom_stereo0D = CreateInchi_Stereo0D(max_len_stereo0D + num_atoms);
1081 : :
1082 [ # # ]: 0 : if (!new_atom_stereo0D)
1083 : : {
1084 : 0 : num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1085 : 0 : *err = INCHI_INP_FATAL_ERR;
1086 : 0 : TREAT_ERR(*err, 0, "Out of RAM");
1087 : 0 : goto bypass_end_of_INChI_plain;
1088 : : }
1089 : :
1090 : 0 : memcpy(new_atom_stereo0D, atom_stereo0D, len_stereo0D * sizeof(*atom_stereo0D));
1091 : 0 : FreeInchi_Stereo0D(&atom_stereo0D);
1092 : 0 : atom_stereo0D = new_atom_stereo0D;
1093 : 0 : max_len_stereo0D += num_atoms;
1094 : : }
1095 : :
1096 : : /* (a) i may be allene endpoint and neigh = allene middle point or
1097 : : (b) i may be allene middle point and neigh = allene endpoint
1098 : : !!!!! CURRENTLY ONLY (b) IS ALLOWED !!!!!
1099 : : */
1100 : :
1101 : 0 : atom_stereo0D[len_stereo0D].neighbor[1] = neigh; /* neigh < i */
1102 : 0 : atom_stereo0D[len_stereo0D].neighbor[2] = i;
1103 : 0 : atom_stereo0D[len_stereo0D].parity = bond_parity;
1104 : 0 : atom_stereo0D[len_stereo0D].type = INCHI_StereoType_DoubleBond; /* incl allenes & cumulenes */
1105 : 0 : len_stereo0D++;
1106 : : }
1107 : : }
1108 : :
1109 [ # # # # : 0 : if (!bItemIsOver || i != num_atoms || (s && p != s)) /* djb-rwth: addressing LLVM warning */
# # # # ]
1110 : : {
1111 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
1112 : 0 : *err = INCHI_INP_ERROR_ERR;
1113 : 0 : TREAT_ERR(*err, 0, "Wrong number of bonds");
1114 : 0 : goto bypass_end_of_INChI_plain;
1115 : : }
1116 : : }
1117 : :
1118 : : /*
1119 : : Search for coordinates block (plain)
1120 : : */
1121 : :
1122 : : /*p = szLine;*/
1123 : 0 : sToken = sStructHdrPlnRevXYZ;
1124 : 0 : lToken = sizeof(sStructHdrPlnRevXYZ) - 1;
1125 : :
1126 : : /* search for sToken in the line; load next segments of the line if sToken has not found */
1127 : 0 : p = FindToken(inp_file, &ir.bTooLongLine, sToken, lToken, szLine_i2i, sizeof(szLine_i2i), p, &res);
1128 : :
1129 [ # # ]: 0 : if (!p)
1130 : : {
1131 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
1132 : 0 : *err = INCHI_INP_ERROR_ERR;
1133 : 0 : TREAT_ERR(*err, 0, "Missing atom coordinates data");
1134 : 0 : goto bypass_end_of_INChI_plain;
1135 : : }
1136 : : else
1137 : : {
1138 : : /* Coordinates block started */
1139 [ # # ]: 0 : if ((pszCoord = (MOL_COORD*)inchi_malloc(inchi_max(num_atoms, 1) * sizeof(MOL_COORD)))) /* djb-rwth: addressing LLVM warning */
1140 : : {
1141 : 0 : memset(pszCoord, ' ', inchi_max(num_atoms, 1) * sizeof(MOL_COORD)); /* djb-rwth: memset_s C11/Annex K variant? */
1142 : : }
1143 : : else
1144 : : {
1145 : 0 : num_atoms = INCHI_INP_FATAL_RET; /* allocation error */
1146 : 0 : *err = INCHI_INP_FATAL_ERR;
1147 : 0 : TREAT_ERR(*err, 0, "Out of RAM");
1148 : 0 : goto bypass_end_of_INChI_plain;
1149 : : }
1150 : :
1151 : 0 : i = 0;
1152 : : /* djb-rwth: removing redundant code */
1153 [ # # # # ]: 0 : bItemIsOver = (s = strchr(p, '/')) || !ir.bTooLongLine;
1154 : :
1155 [ # # ]: 0 : while (i < num_atoms)
1156 : : {
1157 : :
1158 : 0 : p = LoadLine(inp_file, &ir.bTooLongLine, &bItemIsOver, &s,
1159 : : szLine_i2i, sizeof(szLine_i2i), INCHI_LINE_ADD, p, &res);
1160 : :
1161 [ # # # # : 0 : if (i >= num_atoms || (s && p >= s)) /* djb-rwth: addressing LLVM warning */
# # ]
1162 : : {
1163 : : break; /* end of bonds (plain) */
1164 : : }
1165 : :
1166 : : /* coord, first char */
1167 [ # # ]: 0 : if (*p == ';')
1168 : : {
1169 [ # # ]: 0 : for (k = 0; k < NUM_COORD; k++)
1170 : : {
1171 : 0 : pszCoord[i][LEN_COORD * k + 4] = '0';
1172 : : }
1173 : 0 : p++;
1174 : 0 : i++;
1175 : 0 : continue;
1176 : : }
1177 : :
1178 [ # # ]: 0 : for (k = 0; k < 3; k++)
1179 : : {
1180 : : double xyz;
1181 : 0 : bNonZeroXYZ = 0;
1182 [ # # ]: 0 : if (*p == ';')
1183 : : {
1184 : 0 : pszCoord[i][LEN_COORD * k + 4] = '0';
1185 : 0 : xyz = 0.0;
1186 : : }
1187 : : else
1188 : : {
1189 [ # # ]: 0 : if (*p == ',')
1190 : : {
1191 : : /* empty */
1192 : 0 : pszCoord[i][LEN_COORD * k + 4] = '0';
1193 : 0 : xyz = 0.0;
1194 : 0 : p++;
1195 : : }
1196 : : else
1197 : : {
1198 : 0 : xyz = inchi_strtod(p, (const char **)&q); /* GH #100 */
1199 : 0 : bNonZeroXYZ = fabs(xyz) > MIN_BOND_LENGTH;
1200 [ # # ]: 0 : if (q != NULL)
1201 : : {
1202 : 0 : memcpy(pszCoord[i] + LEN_COORD * (long long)k, p, q - p); /* djb-rwth: cast operator added */
1203 [ # # ]: 0 : if (*q == ',')
1204 : 0 : q++;
1205 : 0 : p = q;
1206 : : }
1207 : : else
1208 : 0 : pszCoord[i][LEN_COORD * k + 4] = '0';
1209 : : }
1210 : : }
1211 : :
1212 [ # # # # ]: 0 : switch (k)
1213 : : {
1214 : 0 : case 0:
1215 : 0 : atom[i].x = xyz;
1216 : 0 : b2D |= bNonZeroXYZ;
1217 : 0 : break;
1218 : 0 : case 1:
1219 : 0 : atom[i].y = xyz;
1220 : 0 : b2D |= bNonZeroXYZ;
1221 : 0 : break;
1222 : 0 : case 2:
1223 : 0 : b3D |= bNonZeroXYZ;
1224 : 0 : atom[i].z = xyz;
1225 : 0 : break;
1226 : : }
1227 : : }
1228 : :
1229 [ # # ]: 0 : if (*p == ';')
1230 : : {
1231 : 0 : p++; /* end of this triple of coordinates */
1232 : 0 : i++;
1233 : : }
1234 : : else
1235 : : {
1236 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1237 : 0 : *err = INCHI_INP_ERROR_ERR;
1238 : 0 : TREAT_ERR(*err, 0, "Wrong atom coordinates data");
1239 : 0 : goto bypass_end_of_INChI_plain;
1240 : : }
1241 : : }
1242 : :
1243 [ # # # # : 0 : if (!bItemIsOver || (s && p != s) || i != num_atoms) /* djb-rwth: addressing LLVM warning */
# # # # ]
1244 : : {
1245 : 0 : num_atoms = INCHI_INP_ERROR_RET; /* error */
1246 : 0 : *err = INCHI_INP_ERROR_ERR;
1247 : 0 : TREAT_ERR(*err, 0, "Wrong number of coordinates");
1248 : 0 : goto bypass_end_of_INChI_plain;
1249 : : }
1250 : : } /* end of coordinates */
1251 : :
1252 : : /*
1253 : : Set special valences and implicit H (xml)
1254 : : */
1255 : :
1256 : 0 : b23D = b2D | b3D;
1257 : : /* djb-rwth: removing redundant code */
1258 [ # # ]: 0 : if (at)
1259 : : {
1260 [ # # ]: 0 : if (!*at)
1261 : : {
1262 : : int a1, a2, n1, n2, valence;
1263 : : int chem_bonds_valence;
1264 : 0 : int nX = 0, nY = 0, nZ = 0, nXYZ;
1265 : 0 : *at = atom;
1266 : :
1267 : : /* special valences */
1268 : :
1269 [ # # ]: 0 : for (bNonMetal = 0; bNonMetal < 1; bNonMetal++)
1270 : : {
1271 [ # # ]: 0 : for (a1 = 0; a1 < num_atoms; a1++)
1272 : : {
1273 : : int num_bond_type[MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE + 1];
1274 : 0 : int bHasMetalNeighbor = 0;
1275 : :
1276 : 0 : memset(num_bond_type, 0, sizeof(num_bond_type)); /* djb-rwth: memset_s C11/Annex K variant? */
1277 : :
1278 : 0 : valence = AT_BONDS_VAL(atom, a1); /* save atom valence if available */
1279 : 0 : AT_BONDS_VAL(atom, a1) = 0;
1280 : :
1281 : 0 : atom[a1].orig_at_number = a1 + 1;
1282 : :
1283 : 0 : nX = nY = nZ = 0;
1284 : :
1285 [ # # ]: 0 : for (n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1++) /*AT_NUM_BONDS(AT) ==> (AT).valence */
1286 : : {
1287 : 0 : bond_type = atom[a1].bond_type[n1] - MIN_INPUT_BOND_TYPE;
1288 [ # # # # ]: 0 : if (bond_type < 0 || bond_type > MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE)
1289 : : {
1290 : 0 : bond_type = 0;
1291 : 0 : TREAT_ERR(*err, 0, "Unknown bond type in InChI aux assigned as a single bond");
1292 : : }
1293 : :
1294 : 0 : num_bond_type[bond_type] ++;
1295 : 0 : nNumBonds++;
1296 [ # # ]: 0 : if (b23D)
1297 : : {
1298 : 0 : neigh = atom[a1].neighbor[n1];
1299 : 0 : nX |= (fabs(atom[a1].x - atom[neigh].x) > MIN_BOND_LENGTH);
1300 : 0 : nY |= (fabs(atom[a1].y - atom[neigh].y) > MIN_BOND_LENGTH);
1301 : 0 : nZ |= (fabs(atom[a1].z - atom[neigh].z) > MIN_BOND_LENGTH);
1302 : : }
1303 : : }
1304 : :
1305 : 0 : chem_bonds_valence = 0;
1306 [ # # # # ]: 0 : for (n1 = 0; MIN_INPUT_BOND_TYPE + n1 <= 3 && MIN_INPUT_BOND_TYPE + n1 <= MAX_INPUT_BOND_TYPE; n1++)
1307 : : {
1308 : 0 : chem_bonds_valence += (MIN_INPUT_BOND_TYPE + n1) * num_bond_type[n1];
1309 : : }
1310 : :
1311 : 0 : if (MIN_INPUT_BOND_TYPE <= INCHI_BOND_TYPE_ALTERN && INCHI_BOND_TYPE_ALTERN <= MAX_INPUT_BOND_TYPE &&
1312 [ # # ]: 0 : (n2 = num_bond_type[INCHI_BOND_TYPE_ALTERN - MIN_INPUT_BOND_TYPE]))
1313 : : {
1314 : : /* accept input aromatic bonds for now */
1315 [ # # # ]: 0 : switch (n2)
1316 : : {
1317 : 0 : case 2:
1318 : 0 : chem_bonds_valence += 3; /* =A- */
1319 : 0 : break;
1320 : :
1321 : 0 : case 3:
1322 : 0 : chem_bonds_valence += 4; /* =A< */
1323 : 0 : break;
1324 : :
1325 : 0 : default:
1326 : : /* if 1 or >= 4 aromatic bonds then replace such bonds with single bonds */
1327 [ # # ]: 0 : for (n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1++) /* AT_NUM_BONDS(AT) ==> (AT).valence */
1328 : : {
1329 [ # # ]: 0 : if (atom[a1].bond_type[n1] == INCHI_BOND_TYPE_ALTERN)
1330 : : {
1331 : : AT_NUMB* p1;
1332 : 0 : a2 = atom[a1].neighbor[n1];
1333 : 0 : p1 = is_in_the_list(atom[a2].neighbor, (AT_NUMB)a1, AT_NUM_BONDS(atom[a2])); /*AT_NUM_BONDS(AT) ==> (AT).valence*/
1334 [ # # ]: 0 : if (p1)
1335 : : {
1336 : 0 : atom[a1].bond_type[n1] =
1337 : 0 : atom[a2].bond_type[p1 - atom[a2].neighbor] = INCHI_BOND_TYPE_SINGLE;
1338 : : }
1339 : : else
1340 : : {
1341 : 0 : *err = -2; /* Program error */
1342 : 0 : TREAT_ERR(*err, 0, "Program error interpreting InChI aux");
1343 : 0 : num_atoms = INCHI_INP_FATAL_RET;
1344 : 0 : goto bypass_end_of_INChI_plain; /* no structure */
1345 : : }
1346 : : }
1347 : : }
1348 : :
1349 : 0 : chem_bonds_valence += n2;
1350 : 0 : *err |= 32; /* Unrecognized aromatic bond(s) replaced with single */
1351 : 0 : TREAT_ERR(*err, 0, "Atom has 1 or more than 3 aromatic bonds");
1352 : 0 : break;
1353 : : }
1354 : : }
1355 : :
1356 : : /* added 2006-07-19 to process aromatic bonds same way as from molfile */
1357 [ # # # # ]: 0 : if (n2 && !valence)
1358 : 0 : {
1359 : 0 : int num_H = NUMH(atom, a1); /* only isotopic */
1360 : 0 : int chem_valence = chem_bonds_valence;
1361 : : int bUnusualValenceArom =
1362 : 0 : detect_unusual_el_valence((int)atom[a1].el_number, atom[a1].charge,
1363 : 0 : atom[a1].radical, chem_valence,
1364 : 0 : num_H, atom[a1].valence);
1365 : : int bUnusualValenceNoArom =
1366 : 0 : detect_unusual_el_valence((int)atom[a1].el_number, atom[a1].charge,
1367 : 0 : atom[a1].radical, chem_valence - 1,
1368 : 0 : num_H, atom[a1].valence);
1369 : :
1370 : : #if ( CHECK_AROMBOND2ALT == 1 )
1371 [ # # # # : 0 : if (bUnusualValenceArom && !bUnusualValenceNoArom && 0 == nBondsValToMetal(atom, a1))
# # ]
1372 : : #else
1373 : : if (bUnusualValenceArom && !bUnusualValenceNoArom)
1374 : : #endif
1375 : :
1376 : : {
1377 : : /* typically NH in 5-member aromatic ring */
1378 : 0 : chem_bonds_valence--;
1379 : : }
1380 : : }
1381 [ # # # # ]: 0 : else if (n2 && valence)
1382 : : {
1383 : : /* atom has aromatic bonds AND the chemical valence is known */
1384 : 0 : int num_H = NUMH(atom, a1);
1385 : 0 : int chem_valence = chem_bonds_valence + num_H;
1386 [ # # ]: 0 : if (valence == chem_valence - 1)
1387 : : {
1388 : : /* typically NH in 5-member aromatic ring */
1389 : 0 : chem_bonds_valence--;
1390 : : }
1391 : : }
1392 : :
1393 : 0 : atom[a1].chem_bonds_valence = chem_bonds_valence;
1394 : :
1395 : 0 : atom[a1].num_H = get_num_H(atom[a1].elname,
1396 : 0 : atom[a1].num_H,
1397 : 0 : atom[a1].num_iso_H,
1398 : 0 : atom[a1].charge,
1399 : 0 : atom[a1].radical,
1400 : 0 : atom[a1].chem_bonds_valence,
1401 : : valence,
1402 : : 0,
1403 : : bDoNotAddH,
1404 : : bHasMetalNeighbor);
1405 : : }
1406 : : }
1407 : :
1408 : 0 : nNumBonds /= 2;
1409 : :
1410 [ # # # # ]: 0 : if (b23D && nNumBonds)
1411 : : {
1412 : 0 : nXYZ = nX + nY + nZ;
1413 : 0 : b2D = (nXYZ > 0);
1414 : 0 : b3D = (nXYZ == 3);
1415 [ # # # # ]: 0 : *num_dimensions = b3D ? 3 : b2D ? 2 : 0;
1416 : 0 : *num_bonds = nNumBonds;
1417 : : }
1418 : :
1419 : : /*======= 0D parities =================================*/
1420 : :
1421 [ # # ]: 0 : for (i = 0; i < len_stereo0D; i++)
1422 : : {
1423 : : AT_NUMB* p1, * p2;
1424 : 0 : int sb_ord_from_a1 = -1, sb_ord_from_a2 = -1, bEnd1 = 0, bEnd2 = 0;
1425 : :
1426 [ # # # ]: 0 : switch (atom_stereo0D[i].type)
1427 : : {
1428 : :
1429 : 0 : case INCHI_StereoType_Tetrahedral:
1430 : 0 : a1 = atom_stereo0D[i].central_atom;
1431 [ # # # # : 0 : if (atom_stereo0D[i].parity && (AT_NUM_BONDS(atom[a1]) == 3 || AT_NUM_BONDS(atom[a1]) == 4))
# # ]
1432 : : {
1433 : 0 : int ii, kk = 0;
1434 [ # # ]: 0 : if (AT_NUM_BONDS(atom[a1]) == 3)
1435 : 0 : atom_stereo0D[i].neighbor[kk++] = a1;
1436 [ # # ]: 0 : for (ii = 0; ii < AT_NUM_BONDS(atom[a1]); ii++)
1437 : 0 : atom_stereo0D[i].neighbor[kk++] = atom[a1].neighbor[ii];
1438 : : }
1439 : :
1440 : 0 : break;
1441 : :
1442 : 0 : case INCHI_StereoType_DoubleBond:
1443 : : #define MAX_CHAIN_LEN 20
1444 : 0 : a1 = atom_stereo0D[i].neighbor[1];
1445 : 0 : a2 = atom_stereo0D[i].neighbor[2];
1446 : 0 : p1 = is_in_the_list(atom[a1].neighbor, (AT_NUMB)a2, AT_NUM_BONDS(atom[a1]));
1447 : 0 : p2 = is_in_the_list(atom[a2].neighbor, (AT_NUMB)a1, AT_NUM_BONDS(atom[a2]));
1448 [ # # # # ]: 0 : if (!p1 || !p2)
1449 : : {
1450 : 0 : atom_stereo0D[i].type = INCHI_StereoType_None;
1451 : 0 : atom_stereo0D[i].central_atom = NO_ATOM;
1452 : 0 : atom_stereo0D[i].neighbor[0] =
1453 : 0 : atom_stereo0D[i].neighbor[3] = -1;
1454 : 0 : *err |= 64; /* Error in cumulene stereo */
1455 : 0 : TREAT_ERR(*err, 0, "0D stereobond not recognized");
1456 : 0 : break;
1457 : : }
1458 : :
1459 : : /* streobond, allene, or cumulene */
1460 : :
1461 : 0 : sb_ord_from_a1 = p1 - atom[a1].neighbor;
1462 : 0 : sb_ord_from_a2 = p2 - atom[a2].neighbor;
1463 : :
1464 [ # # ]: 0 : if (AT_NUM_BONDS(atom[a1]) == 2 &&
1465 [ # # ]: 0 : atom[a1].bond_type[0] + atom[a1].bond_type[1] == 2 * INCHI_BOND_TYPE_DOUBLE &&
1466 [ # # ]: 0 : 0 == inchi_NUMH2(atom, a1) &&
1467 [ # # ]: 0 : (AT_NUM_BONDS(atom[a2]) != 2 ||
1468 [ # # ]: 0 : atom[a2].bond_type[0] + atom[a2].bond_type[1] != 2 * INCHI_BOND_TYPE_DOUBLE))
1469 : : {
1470 : 0 : bEnd2 = 1; /* a2 is the end-atom, a1 is middle atom */
1471 : : }
1472 : :
1473 [ # # ]: 0 : if (AT_NUM_BONDS(atom[a2]) == 2 &&
1474 [ # # ]: 0 : atom[a2].bond_type[0] + atom[a2].bond_type[1] == 2 * INCHI_BOND_TYPE_DOUBLE &&
1475 [ # # ]: 0 : 0 == inchi_NUMH2(atom, a2) &&
1476 [ # # ]: 0 : (AT_NUM_BONDS(atom[a1]) != 2 ||
1477 [ # # ]: 0 : atom[a1].bond_type[0] + atom[a1].bond_type[1] != 2 * INCHI_BOND_TYPE_DOUBLE))
1478 : : {
1479 : 0 : bEnd1 = 1; /* a1 is the end-atom, a2 is middle atom */
1480 : : }
1481 : :
1482 [ # # ]: 0 : if (bEnd2 + bEnd1 == 1)
1483 : : {
1484 : : /* allene or cumulene */
1485 : :
1486 : : AT_NUMB chain[MAX_CHAIN_LEN + 1], prev, cur, next;
1487 : :
1488 [ # # # # ]: 0 : if (bEnd2 && !bEnd1)
1489 : : {
1490 : 0 : cur = a1;
1491 : 0 : a1 = a2;
1492 : 0 : a2 = cur;
1493 : 0 : sb_ord_from_a1 = sb_ord_from_a2;
1494 : : }
1495 : :
1496 : 0 : sb_ord_from_a2 = -1;
1497 : 0 : cur = a1;
1498 : 0 : next = a2;
1499 : 0 : len = 0;
1500 : 0 : chain[len++] = cur;
1501 : 0 : chain[len++] = next;
1502 : :
1503 [ # # ]: 0 : while (len < MAX_CHAIN_LEN)
1504 : : {
1505 : : /* arbitrary very high upper limit to prevent infinite loop */
1506 : :
1507 : 0 : prev = cur;
1508 : 0 : cur = next;
1509 : : /* follow double bond path && avoid going back */
1510 [ # # ]: 0 : if (AT_NUM_BONDS(atom[cur]) == 2 &&
1511 [ # # ]: 0 : atom[cur].bond_type[0] + atom[cur].bond_type[1] == 2 * INCHI_BOND_TYPE_DOUBLE &&
1512 [ # # ]: 0 : 0 == inchi_NUMH2(atom, cur))
1513 : : {
1514 : 0 : next = atom[cur].neighbor[atom[cur].neighbor[0] == prev];
1515 : 0 : chain[len++] = next;
1516 : : }
1517 : : else
1518 : : {
1519 : : break;
1520 : : }
1521 : : }
1522 [ # # # # ]: 0 : if (len > 2 &&
1523 : 0 : (p2 = is_in_the_list(atom[cur].neighbor, (AT_NUMB)prev, AT_NUM_BONDS(atom[cur]))))
1524 : : {
1525 : 0 : sb_ord_from_a2 = p2 - atom[cur].neighbor;
1526 : 0 : a2 = cur;
1527 : : /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1528 : 0 : atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[sb_ord_from_a1 == 0];
1529 : 0 : atom_stereo0D[i].neighbor[1] = a1;
1530 : 0 : atom_stereo0D[i].neighbor[2] = a2;
1531 : 0 : atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[sb_ord_from_a2 == 0];
1532 : :
1533 [ # # ]: 0 : if (len % 2)
1534 : : {
1535 : 0 : atom_stereo0D[i].central_atom = chain[len / 2];
1536 : 0 : atom_stereo0D[i].type = INCHI_StereoType_Allene;
1537 : : }
1538 : : else
1539 : : {
1540 : 0 : atom_stereo0D[i].central_atom = NO_ATOM;
1541 : : }
1542 : : }
1543 : : else
1544 : : {
1545 : : /* error */
1546 : 0 : atom_stereo0D[i].type = INCHI_StereoType_None;
1547 : 0 : atom_stereo0D[i].central_atom = NO_ATOM;
1548 : 0 : atom_stereo0D[i].neighbor[0] =
1549 : 0 : atom_stereo0D[i].neighbor[3] = -1;
1550 : 0 : *err |= 64; /* Error in cumulene stereo */
1551 : 0 : TREAT_ERR(*err, 0, "Cumulene stereo not recognized (0D)");
1552 : : }
1553 : : #undef MAX_CHAIN_LEN
1554 : : }
1555 : : else
1556 : : {
1557 : : /****** a normal possibly stereogenic bond -- not an allene or cumulene *******/
1558 : : /* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1559 : 0 : sb_ord_from_a1 = p1 - atom[a1].neighbor;
1560 : 0 : sb_ord_from_a2 = p2 - atom[a2].neighbor;
1561 : 0 : atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[p1 == atom[a1].neighbor];
1562 : 0 : atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[p2 == atom[a2].neighbor];
1563 : 0 : atom_stereo0D[i].central_atom = NO_ATOM;
1564 : : }
1565 : :
1566 [ # # # # ]: 0 : if (atom_stereo0D[i].type != INCHI_StereoType_None &&
1567 [ # # ]: 0 : sb_ord_from_a1 >= 0 && sb_ord_from_a2 >= 0 &&
1568 [ # # # # ]: 0 : ATOM_PARITY_WELL_DEF(SB_PARITY_2(atom_stereo0D[i].parity)))
1569 : : {
1570 : : /* Detected well-defined disconnected stereo
1571 : : * locate first non-metal neighbors */
1572 : :
1573 : : int a, j, /* k,*/ sb_ord, cur_neigh, min_neigh; /* djb-rwth: removing redundant variables */
1574 : :
1575 [ # # ]: 0 : for (k = 0; k < 2; k++)
1576 : : {
1577 [ # # ]: 0 : a = k ? atom_stereo0D[i].neighbor[2] : atom_stereo0D[i].neighbor[1];
1578 [ # # ]: 0 : sb_ord = k ? sb_ord_from_a2 : sb_ord_from_a1;
1579 : 0 : min_neigh = num_atoms;
1580 [ # # ]: 0 : for (j = 0; j < AT_NUM_BONDS(atom[a]); j++) /* djb-rwth: removing redundant code */
1581 : : {
1582 : 0 : cur_neigh = atom[a].neighbor[j];
1583 [ # # # # ]: 0 : if (j != sb_ord && !IS_METAL_ATOM(atom, cur_neigh))
1584 : : {
1585 : 0 : min_neigh = inchi_min(cur_neigh, min_neigh);
1586 : : }
1587 : : }
1588 [ # # ]: 0 : if (min_neigh < num_atoms)
1589 : : {
1590 [ # # ]: 0 : atom_stereo0D[i].neighbor[k ? 3 : 0] = min_neigh;
1591 : : }
1592 : : else
1593 : : {
1594 : 0 : TREAT_ERR(*err, 0, "Cannot find non-metal stereobond neighor (0D)");
1595 : : }
1596 : : }
1597 : : }
1598 : :
1599 : 0 : break;
1600 : : }
1601 : : }
1602 : : /* end of 0D parities extraction */
1603 : : /*exit_cycle:;*/
1604 : : }
1605 : :
1606 : : /* Transfer atom_stereo0D[] to atom[] */
1607 [ # # ]: 0 : if (len_stereo0D)
1608 : : {
1609 : 0 : Extract0DParities(atom, num_atoms, atom_stereo0D, len_stereo0D,
1610 : : pStrErr, err, vABParityUnknown);
1611 : : }
1612 : :
1613 [ # # ]: 0 : if (pInpAtomFlags)
1614 : : {
1615 : : /* save chirality flag */
1616 : 0 : *pInpAtomFlags |= InpAtomFlags;
1617 : : }
1618 : : }
1619 [ # # ]: 0 : else if (atom)
1620 : : {
1621 [ # # ]: 0 : inchi_free(atom);
1622 : 0 : atom = NULL;
1623 : : }
1624 : :
1625 : : #if ( FIX_READ_AUX_MEM_LEAK == 1 )
1626 : : /* 2005-08-04 avoid memory leak */
1627 [ # # ]: 0 : if (atom_stereo0D) /* && !(stereo0D && *stereo0D == atom_stereo0D) ) */
1628 : : {
1629 : 0 : FreeInchi_Stereo0D(&atom_stereo0D);
1630 : : }
1631 : : #endif
1632 : :
1633 [ # # ]: 0 : if (szCoord)
1634 : : {
1635 : 0 : *szCoord = pszCoord;
1636 : 0 : pszCoord = NULL;
1637 : : }
1638 [ # # ]: 0 : else if (pszCoord)
1639 : : {
1640 [ # # ]: 0 : inchi_free(pszCoord);
1641 : 0 : pszCoord = NULL;
1642 : : }
1643 : :
1644 : 0 : goto bypass_end_of_INChI_plain;
1645 : : /*return num_atoms;*/
1646 : : }
1647 : : } /* while ( 0 < (res = inchi_ios_getsTab( szLine, sizeof(szLine)-1, inp_file, &ir.bTooLongLine ) ) ) */
1648 : :
1649 : : /* End of structure reading cycle */
1650 [ # # ]: 0 : if (atom_stereo0D)
1651 : 0 : FreeInchi_Stereo0D(&atom_stereo0D);
1652 [ # # ]: 0 : if (res <= 0)
1653 : : {
1654 [ # # ]: 0 : if (*err == INCHI_INP_ERROR_ERR)
1655 : : {
1656 : 0 : return num_atoms;
1657 : : }
1658 : 0 : *err = INCHI_INP_EOF_ERR;
1659 : :
1660 : 0 : return INCHI_INP_EOF_RET; /* no more data */
1661 : : }
1662 : :
1663 : 0 : bypass_end_of_INChI_plain:
1664 : : /* Cleanup */
1665 [ # # # # ]: 0 : if (num_atoms == INCHI_INP_ERROR_RET || num_atoms == INCHI_INP_FATAL_RET)
1666 : : {
1667 [ # # ]: 0 : if (atom_stereo0D) /* djb-rwth: avoiding memory leak */
1668 : : {
1669 : 0 : FreeInchi_Stereo0D(&atom_stereo0D);
1670 : : }
1671 : :
1672 [ # # ]: 0 : if (atom) /* djb-rwth: fixing coverity ID #499615 */
1673 : : {
1674 [ # # ]: 0 : inchi_free(atom);
1675 : : }
1676 : :
1677 [ # # ]: 0 : if (pszCoord) /* djb-rwth: fixing coverity ID #499571 */
1678 : : {
1679 [ # # ]: 0 : inchi_free(pszCoord);
1680 : : }
1681 : : }
1682 : :
1683 [ # # # # ]: 0 : while (ir.bTooLongLine &&
1684 : 0 : 0 < inchi_ios_getsTab1(szLine_i2i, sizeof(szLine_i2i) - 1, inp_file, &ir.bTooLongLine))
1685 : : {
1686 : : ;
1687 : : }
1688 : :
1689 : 0 : return num_atoms;
1690 : :
1691 : : #undef AT_NUM_BONDS
1692 : : #undef AT_NUMB
1693 : : #undef is_in_the_list
1694 : : #undef inchi_NUMH2
1695 : :
1696 : : #undef MoreParms
1697 : : #undef INPUT_FILE
1698 : : #undef CreateInpAtom
1699 : : #undef AT_BONDS_VAL
1700 : : #undef ISOLATED_ATOM
1701 : : #undef NUM_ISO_Hk
1702 : : #undef IS_METAL_ATOM
1703 : : }
1704 : :
1705 : :
1706 : :
1707 : : /****************************************************************************/
1708 : 0 : void find_and_interpret_structure_header( char *szLine,
1709 : : char *pSdfLabel,
1710 : : char *pSdfValue,
1711 : : unsigned long *Id,
1712 : : int hlen,
1713 : : ReadINCHI_CtlData *ir )
1714 : : {
1715 : : int len;
1716 : : char *p, *q;
1717 : : static const char sStructHdrPlnNoLblVal[] = " is missing";
1718 : :
1719 : :
1720 : 0 : p = szLine + hlen;
1721 : 0 : ir->ulongID = 0LU;
1722 : :
1723 : : /* structure number */
1724 : 0 : ir->ulongID = strtoul( p, &q, 10 );
1725 [ # # # # : 0 : if (q && q[0] == '.' && q[1] == ' ')
# # ]
1726 : : {
1727 : 0 : p = q + 2;
1728 : : }
1729 : 0 : p = p + strspn( p, " \n\r" );
1730 : :
1731 [ # # ]: 0 : if (pSdfLabel)
1732 : : {
1733 : 0 : pSdfLabel[0] = '\0';
1734 : : }
1735 [ # # ]: 0 : if (pSdfValue)
1736 : : {
1737 : 0 : pSdfValue[0] = '\0';
1738 : : }
1739 : :
1740 [ # # ]: 0 : if (*p)
1741 : : {
1742 : : /* has label name */
1743 : :
1744 : : /*p ++;*/
1745 [ # # ]: 0 : if ((q = strchr( p, '=' ))) /* djb-rwth: addressing LLVM warning */
1746 : : {
1747 : :
1748 : : /* '=' separates label name from the value */
1749 : 0 : len = inchi_min( q - p + 1, MAX_SDF_HEADER - 1 );
1750 : :
1751 [ # # ]: 0 : if (pSdfLabel)
1752 : : {
1753 : 0 : mystrncpy( pSdfLabel, p, len );
1754 : 0 : lrtrim( pSdfLabel, &len );
1755 : : }
1756 : :
1757 : 0 : p = q + 1;
1758 : 0 : q = p + (int) strlen( p );
1759 : :
1760 [ # # ]: 0 : if (q - p > 0)
1761 : : {
1762 : 0 : len = inchi_min( q - p + 1, MAX_SDF_VALUE - 1 );
1763 [ # # ]: 0 : if (pSdfValue)
1764 : : {
1765 : 0 : mystrncpy( pSdfValue, p, len );
1766 : : }
1767 : : /* djb-rwth: removing redundant code */
1768 : : }
1769 : : }
1770 [ # # ]: 0 : else if ((q = strstr( p, sStructHdrPlnNoLblVal ))) /* djb-rwth: addressing LLVM warning */
1771 : : {
1772 : 0 : len = inchi_min( q - p + 1, MAX_SDF_HEADER - 1 );
1773 [ # # ]: 0 : if (pSdfLabel)
1774 : : {
1775 : 0 : mystrncpy( pSdfLabel, p, len );
1776 : : }
1777 : : /* djb-rwth: removing redundant code */
1778 : : }
1779 : : }
1780 : :
1781 [ # # ]: 0 : if (Id)
1782 : : {
1783 : 0 : *Id = ir->ulongID;
1784 : : }
1785 : :
1786 : 0 : ir->bHeaderRead = 1;
1787 : 0 : ir->bErrorMsg = ir->bRestoreInfo = 0;
1788 : :
1789 : 0 : return;
1790 : : }
|