Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : :
7 : : /*
8 : : General processing procedures
9 : :
10 : : */
11 : :
12 : : #include <stdlib.h>
13 : : #include <string.h>
14 : : #include <stdarg.h>
15 : : #include <errno.h>
16 : : #include <limits.h>
17 : : #include <math.h>
18 : : #include <ctype.h>
19 : : #include <locale.h>
20 : :
21 : : #include "mode.h"
22 : : #include "ichitime.h"
23 : : #ifndef COMPILE_ANSI_ONLY
24 : : #include <conio.h>
25 : : #endif
26 : :
27 : : #include "ichimain.h"
28 : : #include "ichi_io.h"
29 : : #include "mol_fmt.h"
30 : : #include "inchi_api.h"
31 : : #include "readinch.h"
32 : : #include "ichicant.h"
33 : : #ifdef TARGET_LIB_FOR_WINCHI
34 : : #include "../../../IChI_lib/src/ichi_lib.h"
35 : : #include "inchi_api.h"
36 : : #endif
37 : : #include "inchi_gui.h"
38 : : #include "readinch.h"
39 : : #include "ichirvrs.h"
40 : :
41 : : #include "bcf_s.h"
42 : :
43 : : extern int DisplayTheWholeStructure(struct tagCANON_GLOBALS* pCG,
44 : : struct tagINCHI_CLOCK* ic,
45 : : struct tagStructData* sd,
46 : : INPUT_PARMS* ip,
47 : : char* szTitle,
48 : : INCHI_IOSTREAM* inp_file,
49 : : INCHI_IOSTREAM* log_file,
50 : : ORIG_ATOM_DATA* orig_inp_data,
51 : : long num_inp,
52 : : int iINChI,
53 : : int bShowStruct,
54 : : int bINCHI_LIB_Flag);
55 : :
56 : : extern int DisplayStructure(struct tagCANON_GLOBALS* pCG,
57 : : inp_ATOM* at,
58 : : int num_at,
59 : : OAD_Polymer* polymer,
60 : : int num_removed_H,
61 : : int bAdd_DT_to_num_H,
62 : : int nNumRemovedProtons,
63 : : NUM_H* nNumRemovedProtonsIsotopic,
64 : : int bIsotopic,
65 : : int j /*bTautomeric*/,
66 : : INChI** cur_INChI,
67 : : INChI_Aux** cur_INChI_Aux,
68 : : int bAbcNumbers,
69 : : DRAW_PARMS* dp,
70 : : INCHI_MODE nMode,
71 : : char* szTitle);
72 : :
73 : : extern int ReadInChICoord(INCHI_IOSTREAM* pInp,
74 : : SEGM_LINE* pLine,
75 : : int* pState,
76 : : INChI* pInpInChI[INCHI_NUM][TAUT_NUM],
77 : : int nNumComponents[INCHI_NUM][TAUT_NUM]);
78 : :
79 : :
80 : : /* Local functions */
81 : : static int DoOneStructureEarlyPreprocessing(INCHI_CLOCK* ic,
82 : : CANON_GLOBALS* pCG,
83 : : long num_inp,
84 : : STRUCT_DATA* sd,
85 : : INPUT_PARMS* ip,
86 : : INCHI_IOSTREAM* inp_file,
87 : : INCHI_IOSTREAM* log_file,
88 : : INCHI_IOSTREAM* out_file,
89 : : INCHI_IOSTREAM* prb_file,
90 : : ORIG_ATOM_DATA* orig_inp_data,
91 : : ORIG_ATOM_DATA* prep_inp_data);
92 : :
93 : : /* Actual worker sitting under ProcessOneStructureEx */
94 : : int ProcessOneStructureExCore(struct tagINCHI_CLOCK* ic,
95 : : struct tagCANON_GLOBALS* CG,
96 : : STRUCT_DATA* sd,
97 : : INPUT_PARMS* ip,
98 : : char* szTitle,
99 : : PINChI2* pINChI2[INCHI_NUM],
100 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
101 : : INCHI_IOSTREAM* inp_file,
102 : : INCHI_IOSTREAM* log_file,
103 : : INCHI_IOSTREAM* out_file,
104 : : INCHI_IOSTREAM* prb_file,
105 : : ORIG_ATOM_DATA* orig_inp_data,
106 : : ORIG_ATOM_DATA* prep_inp_data,
107 : : long num_inp,
108 : : INCHI_IOS_STRING* strbuf,
109 : : unsigned char save_opt_bits);
110 : :
111 : :
112 : :
113 : : static ORIG_STRUCT*
114 : : OrigAtData_StoreNativeInput(CANON_GLOBALS* pCG,
115 : : int* nRet,
116 : : STRUCT_DATA* sd,
117 : : INPUT_PARMS* ip,
118 : : ORIG_ATOM_DATA* orig_inp_data,
119 : : ORIG_STRUCT* pOrigStruct);
120 : : static void PrepareSaveOptBits(unsigned char* save_opt_bits,
121 : : INPUT_PARMS* ip);
122 : :
123 : : static void DisplayOrigAndResultStructuresAndComponents(int nRet,
124 : : INCHI_CLOCK* ic,
125 : : CANON_GLOBALS* pCG,
126 : : STRUCT_DATA* sd,
127 : : INPUT_PARMS* ip,
128 : : char* szTitle,
129 : : PINChI2* pINChI[INCHI_NUM],
130 : : PINChI_Aux2* pINChI_Aux[INCHI_NUM],
131 : : INCHI_IOSTREAM* inp_file, INCHI_IOSTREAM* log_file,
132 : : INCHI_IOSTREAM* out_file,
133 : : ORIG_ATOM_DATA* orig_inp_data,
134 : : ORIG_ATOM_DATA* prep_inp_data,
135 : : long num_inp,
136 : : int maxINChI,
137 : : COMP_ATOM_DATA composite_norm_data[INCHI_NUM][TAUT_NUM + 1]);
138 : : static void SaveOkProcessedMolfile(int nRet,
139 : : STRUCT_DATA* sd,
140 : : INPUT_PARMS* ip,
141 : : INCHI_IOSTREAM* prb_file,
142 : : INCHI_IOSTREAM* inp_file);
143 : :
144 : : static int mark_atoms_to_delete_or_renumber(ORIG_ATOM_DATA* orig_at_data,
145 : : OAD_StructureEdits* ed,
146 : : int* at_renum);
147 : : static int set_renumbered_or_delete(int* number,
148 : : int* buf,
149 : : int nelems,
150 : : int* renum,
151 : : int base);
152 : :
153 : : static void swap_atoms_xyz(ORIG_ATOM_DATA* orig_at_data, int ia1, int ia2);
154 : :
155 : : /* Callbacks */
156 : :
157 : : /* Console user issued CTRL+C etc. */
158 : : int(*ConsoleQuit)(void) = NULL;
159 : : int(*UserAction) (void) = NULL;
160 : :
161 : : #if (BUILD_WITH_ENG_OPTIONS==1)
162 : : #if ALLOW_SUBSTRUCTURE_FILTERING==1
163 : : static int OrigAtData_CheckForSubstructure(ORIG_ATOM_DATA* orig_inp_data);
164 : : #endif
165 : : #endif
166 : :
167 : :
168 : : /**********************************************
169 : : * output " L=V" or " L missing" or ""
170 : : * The fprintf format string must contain %s%s%s%s
171 : : */
172 : : const char gsMissing[] = "is missing";
173 : : const char gsEmpty[] = "";
174 : : const char gsSpace[] = " ";
175 : : const char gsEqual[] = "=";
176 : :
177 : :
178 : :
179 : : /****************************************************************************
180 : : Process a portion of input data (molecule, InChI string, ...)
181 : : in a relevant way (generate InChI, restore molecule by InChI )
182 : : ****************************************************************************/
183 : 188 : int ProcessOneStructure(INCHI_CLOCK* ic,
184 : : CANON_GLOBALS* pCG,
185 : : STRUCT_DATA* sd,
186 : : INPUT_PARMS* ip,
187 : : char* szTitle,
188 : : PINChI2* pINChI[INCHI_NUM],
189 : : PINChI_Aux2* pINChI_Aux[INCHI_NUM],
190 : : INCHI_IOSTREAM* inp_file,
191 : : INCHI_IOSTREAM* log_file,
192 : : INCHI_IOSTREAM* out_file,
193 : : INCHI_IOSTREAM* prb_file,
194 : : ORIG_ATOM_DATA* orig_inp_data,
195 : : ORIG_ATOM_DATA* prep_inp_data,
196 : : long num_inp,
197 : : INCHI_IOS_STRING* strbuf,
198 : : unsigned char save_opt_bits)
199 : : {
200 : 188 : int nRet = 0,
201 : : nRet1, i, k,
202 : 188 : maxINChI = 0,
203 : 188 : bSortPrintINChIFlags = 0;
204 : : COMP_ATOM_DATA
205 : : composite_norm_data[INCHI_NUM][TAUT_NUM + 1]; /* [0]:non-taut,
206 : : [1]:taut,
207 : : [2]:intermediate taut struct */
208 : : NORM_CANON_FLAGS ncFlags;
209 : 188 : NORM_CANON_FLAGS* pncFlags = &ncFlags;
210 : : ORIG_STRUCT OrigStruct;
211 : 188 : ORIG_STRUCT* pOrigStruct = NULL;
212 : 188 : int err, ret1 = 0;
213 : :
214 : : /* djb-rwth: removing redundant code */
215 : :
216 : : /* 1. Preliminary work */
217 : :
218 : : /* djb-rwth: fixing coverity ID #499508 */
219 [ - + ]: 188 : if (!orig_inp_data)
220 : : {
221 : 0 : goto exit_function;
222 : : }
223 : :
224 : 376 : int is_polymer = orig_inp_data->valid_polymer
225 [ + - ]: 24 : && orig_inp_data->polymer
226 [ + + + - ]: 212 : && orig_inp_data->polymer->n ;
227 : :
228 [ + + - + : 188 : int is_polymer2inchi = is_polymer && (ip->nInputType == INPUT_MOLFILE || ip->nInputType == INPUT_SDFILE);
- - ]
229 : :
230 : 188 : sd->bUserQuitComponent = 0;
231 : 188 : sd->bUserQuitComponentDisplay = 0;
232 : 188 : memset(composite_norm_data, 0, sizeof(composite_norm_data)); /* djb-rwth: memset_s C11/Annex K variant? */
233 : 188 : memset(pncFlags, 0, sizeof(*pncFlags)); /* djb-rwth: memset_s C11/Annex K variant? */
234 : :
235 : :
236 : :
237 : : /* For experimental purposes only */
238 : : /*ret1 = DoOneStructureEarlyPreprocessing( num_inp, sd, ip, inp_file,
239 : : log_file, out_file, prb_file,
240 : : orig_inp_data, prep_inp_data );
241 : : */
242 : 188 : ret1 = DoOneStructureEarlyPreprocessing(ic, pCG, num_inp, sd, ip,
243 : : inp_file, log_file, out_file, prb_file,
244 : : orig_inp_data, prep_inp_data);
245 [ - + ]: 188 : switch ( ret1 )
246 : : {
247 : 0 : case _IS_SKIP:
248 : : case _IS_ERROR:
249 : : case _IS_FATAL:
250 : 0 : nRet = ret1;
251 : : }
252 : :
253 [ - + ]: 188 : if ( ret1 )
254 : : {
255 : 0 : goto exit_function;
256 : : }
257 : :
258 : : /*(@nnuk : Nauman Ullah Khan)
259 : : *invoking Molecular Inorganics preprocessing (if flag is active)
260 : : */
261 [ + + + - : 188 : if ( ip->bMolecularInorganics && nRet != _IS_FATAL && nRet != _IS_ERROR )
+ - ]
262 : : {
263 : : /* Capture the native input as OrigStruct before MolecularInorganicsPreprocessing()
264 : : * disconnects bonds in orig_inp_data. This mirrors the standard path (where
265 : : * orig_inp_data stays pristine because preprocessing works on a copy) so the
266 : : * AuxInfo reversibility layers (/rA, /rB, /rC), including atom coordinates,
267 : : * describe the original drawn structure. */
268 : 187 : pOrigStruct = &OrigStruct;
269 : 187 : memset(pOrigStruct, 0, sizeof(*pOrigStruct));
270 : 187 : OrigAtData_StoreNativeInput(pCG, &nRet, sd, ip, orig_inp_data, pOrigStruct);
271 [ + - - + ]: 187 : if ( nRet == _IS_FATAL || nRet == _IS_ERROR )
272 : : {
273 : : /* Route to the shared cleanup so OrigStruct_Free() releases pOrigStruct */
274 : 0 : goto after_structure_generation;
275 : : }
276 : :
277 : : /* Call the MolecularInorganicsPreprocessing function to handle specific inorganics-related tasks */
278 : 187 : ret1 = MolecularInorganicsPreprocessing(orig_inp_data, ip);
279 : :
280 : : /* A charge-separated bond to a non-Group-1/2 metal whose element pair
281 : : * the bond table cuts (e.g. Ag(+)-F(-)): reject this structure with
282 : : * an error and continue with the next one (issue #304). */
283 [ + + ]: 187 : if ( ret1 == MI_ERR_CHARGE_SEPARATED_NON_GROUP12 )
284 : : {
285 : 2 : long num_inp2 = num_inp;
286 : 2 : AddErrorMessage(sd->pStrErrStruct, "Charge-separated bond to a non-Group 1/2 metal");
287 : 2 : sd->nStructReadError = 99;
288 : 2 : sd->nErrorType = _IS_ERROR;
289 : 2 : TreatErrorsInReadTheStructure(sd, ip, LOG_MASK_ALL, inp_file, log_file, out_file, prb_file,
290 : : orig_inp_data, &num_inp2);
291 : 2 : nRet = _IS_ERROR;
292 : 2 : goto after_structure_generation;
293 : : }
294 : :
295 : : /* Check for errors during preprocessing */
296 [ - + ]: 185 : if ( ret1 < 0 )
297 : : {
298 : : /* Log and handle fatal error in molecular inorganics preprocessing */
299 : 0 : fprintf(stderr, "Error during Molecular Inorganics preprocessing\n");
300 : 0 : AddErrorMessage(sd->pStrErrStruct, "Molecular Inorganics preprocessing failed");
301 : 0 : nRet = _IS_FATAL;
302 : 0 : goto exit_function;
303 : : }
304 : :
305 : : /* Preserve SDF output in Molecular Inorganics mode if requested */
306 : 185 : ret1 = OrigAtData_SaveMolfile(orig_inp_data, sd, ip, num_inp, out_file);
307 [ - + ]: 185 : if (ret1)
308 : : {
309 : 0 : goto exit_function;
310 : : }
311 : :
312 : 185 : nRet1 = CreateOneStructureINChI(pCG, ic, sd, ip, szTitle,
313 : : pINChI, pINChI_Aux, INCHI_BAS,
314 : : inp_file, log_file, out_file, prb_file,
315 : : orig_inp_data, prep_inp_data,
316 : : composite_norm_data,
317 : : num_inp, strbuf, pncFlags);
318 : 185 : nRet = inchi_max(nRet, nRet1);
319 : :
320 : : /* If preprocessing indicates we need a reconnected InChI, create it here */
321 [ + + + - ]: 185 : if ( ip->bMolecularInorganicsReconnectedInChI && nRet != _IS_FATAL &&
322 [ + - ]: 28 : nRet != _IS_ERROR &&
323 [ - + ]: 28 : (sd->bTautFlagsDone[INCHI_BAS] & TG_FLAG_DISCONNECT_COORD_DONE) &&
324 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD) )
325 : : {
326 : :
327 : 0 : nRet1 = CreateOneStructureINChI(pCG, ic, sd, ip, szTitle,
328 : : pINChI, pINChI_Aux, INCHI_REC,
329 : : inp_file, log_file, out_file, prb_file,
330 : : orig_inp_data, prep_inp_data,
331 : : composite_norm_data,
332 : : num_inp, strbuf, pncFlags);
333 : 0 : nRet = inchi_max(nRet, nRet1);
334 : :
335 [ # # # # ]: 0 : if ( nRet1 == _IS_FATAL || nRet1 == _IS_ERROR )
336 : : {
337 : 0 : fprintf(stderr, "Error during reconnected InChI generation.\n");
338 : 0 : goto exit_function;
339 : : }
340 : :
341 [ # # # # ]: 0 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
342 : : {
343 : 0 : maxINChI = 2; /* Update to indicate reconnected InChI has been generated */
344 : : }
345 : : }
346 : :
347 [ + - + + ]: 185 : else if (nRet != _IS_FATAL && nRet != _IS_ERROR)
348 : : {
349 : 184 : maxINChI = 1;
350 : : }
351 : :
352 : : /*
353 : : * InChI is already generated via the Molecular Inorganics-specific path.
354 : : * Skip the standard structure-generation path to prevent duplicate
355 : : * CreateOneStructureINChI() calls and redundant allocations.
356 : : *
357 : : * pOrigStruct and OrigStruct were populated above from the native input, so the shared
358 : : * cleanup path (SortAndPrintINChI + OrigStruct_Free) emits the AuxInfo
359 : : * reversibility layers (/rA, /rB, /rC), including atom coordinates, and
360 : : * releases it.
361 : : */
362 : 185 : goto after_structure_generation;
363 : : }
364 : :
365 : :
366 [ - + ]: 1 : if ( is_polymer )
367 : : {
368 : : /* Polymer house-keeping related to possible CRU frame shift(s) */
369 : :
370 : 0 : orig_inp_data->polymer->frame_shift_scheme = ip->bFrameShiftScheme;
371 : 0 : orig_inp_data->polymer->treat = ip->bPolymers;
372 : :
373 [ # # ]: 0 : if ( !is_polymer2inchi )
374 : : {
375 : : /* Polymer structure is being restored from InChI string */
376 : : /* If CRUs were pre-cyclized, re-open them in preferred forms here */
377 [ # # ]: 0 : if ( orig_inp_data->polymer->frame_shift_scheme == FSS_STARS_CYCLED )
378 : : {
379 : 0 : OAD_Polymer_SmartReopenCyclizedUnits(orig_inp_data->polymer,
380 : : orig_inp_data->at,
381 : : orig_inp_data->num_inp_atoms,
382 : : &orig_inp_data->num_inp_bonds);
383 : : }
384 : : }
385 : : }
386 : :
387 : :
388 : 1 : ret1 = OrigAtData_SaveMolfile(orig_inp_data, sd, ip, num_inp, out_file);
389 [ - + ]: 1 : if ( ret1 )
390 : : {
391 : 0 : goto exit_function;
392 : : }
393 : :
394 : :
395 : 1 : pOrigStruct = &OrigStruct;
396 : 1 : memset(pOrigStruct, 0, sizeof(*pOrigStruct)); /* djb-rwth: memset_s C11/Annex K variant? */
397 : :
398 : 1 : OrigAtData_StoreNativeInput(pCG, &nRet, sd, ip, orig_inp_data, pOrigStruct);
399 : :
400 : : /* 2. Create INChI for the whole disconnected or original structure */
401 : :
402 [ + - + - ]: 1 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
403 : : {
404 : 1 : nRet1 = CreateOneStructureINChI(pCG, ic, sd, ip, szTitle,
405 : : pINChI, pINChI_Aux, INCHI_BAS,
406 : : inp_file, log_file, out_file, prb_file,
407 : : orig_inp_data, prep_inp_data,
408 : : composite_norm_data,
409 : : num_inp, strbuf, pncFlags);
410 : 1 : nRet = inchi_max(nRet, nRet1);
411 : :
412 : : /* If we create InChI from polymer-containing structure */
413 [ - + ]: 1 : if ( is_polymer2inchi )
414 : : {
415 : 0 : int polymer_repr_type = OAD_Polymer_GetRepresentation(orig_inp_data->polymer);
416 : :
417 : : #ifdef ALLOW_MIXED_SRU_AND_MON
418 [ # # # # ]: 0 : if ( polymer_repr_type == POLYMER_REPRESENTATION_STRUCTURE_BASED ||
419 : : polymer_repr_type == POLYMER_REPRESENTATION_MIXED )
420 : : #else
421 : : if ( polymer_repr_type == POLYMER_REPRESENTATION_STRUCTURE_BASED )
422 : : #endif
423 : : {
424 : : /* Temporarily copy ptr to polymer data to prep_inp_data */
425 : 0 : OAD_Polymer* prep_polymer = prep_inp_data->polymer; /* may be NULL */
426 : 0 : prep_inp_data->polymer = orig_inp_data->polymer;
427 : :
428 : 0 : OAD_Polymer_FindBackbones(prep_inp_data, /* NB: not orig_inp_data! */
429 : : &(composite_norm_data[INCHI_BAS][TAUT_YES]),
430 : 0 : &err, sd->pStrErrStruct);
431 [ # # ]: 0 : if ( err )
432 : : {
433 : 0 : ret1 = _IS_ERROR;
434 : : }
435 : 0 : nRet = inchi_max(nRet, ret1);
436 : 0 : prep_inp_data->polymer = prep_polymer; /* restore temp copied*/
437 : : }
438 : : }
439 : : }
440 : :
441 [ + - + - ]: 1 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
442 : : {
443 : 1 : maxINChI = 1;
444 : : }
445 : :
446 : :
447 : : /* 3. Create INChI for the whole metal-reconnected structure */
448 : :
449 [ - + ]: 1 : if ( nRet != _IS_FATAL &&
450 [ - + ]: 1 : nRet != _IS_ERROR &&
451 [ - + ]: 1 : (sd->bTautFlagsDone[INCHI_BAS] & TG_FLAG_DISCONNECT_COORD_DONE) &&
452 [ + - ]: 1 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD) )
453 : : {
454 : :
455 : 0 : nRet1 = CreateOneStructureINChI(pCG, ic, sd, ip, szTitle,
456 : : pINChI, pINChI_Aux, INCHI_REC, inp_file,
457 : : log_file, out_file, prb_file,
458 : : orig_inp_data, prep_inp_data,
459 : : composite_norm_data,
460 : : num_inp, strbuf, pncFlags);
461 : 0 : nRet = inchi_max(nRet, nRet1);
462 : :
463 [ # # ]: 0 : if ( is_polymer2inchi )
464 : : {
465 : 0 : ret1 = 0;
466 : : /* temporarily copy ptr to polymer data to prep_inp_data */
467 : 0 : prep_inp_data->polymer = orig_inp_data->polymer;
468 : :
469 : 0 : OAD_Polymer_FindBackbones(prep_inp_data, /* NB: not orig_inp_data! */
470 : : &(composite_norm_data[INCHI_REC][TAUT_YES]),
471 : 0 : &err, sd->pStrErrStruct);
472 [ # # ]: 0 : if ( err )
473 : : {
474 : 0 : ret1 = _IS_ERROR;
475 : : }
476 : 0 : nRet = inchi_max(nRet, ret1);
477 : 0 : prep_inp_data->polymer = NULL; /* remove temp copied */
478 : : }
479 [ # # # # ]: 0 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
480 : : {
481 : 0 : maxINChI = 2;
482 : : }
483 : : }
484 : :
485 : 1 : after_structure_generation:
486 : :
487 [ + - + + ]: 188 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
488 : : {
489 [ + + ]: 185 : if ( (sd->bChiralFlag & FLAG_INP_AT_CHIRAL) &&
490 [ + - ]: 8 : (ip->nMode & REQ_MODE_STEREO) &&
491 [ + - + + ]: 16 : !(ip->nMode & (REQ_MODE_RELATIVE_STEREO | REQ_MODE_RACEMIC_STEREO)) &&
492 : 8 : !bIsStructChiral(pINChI, sd->num_components) )
493 : : {
494 [ + - ]: 1 : if ( !ip->bNoWarnings )
495 : : {
496 : 1 : WarningMessage(sd->pStrErrStruct, "Not chiral");
497 : : }
498 : : }
499 [ + - + - ]: 185 : if ( !sd->bUserQuitComponent && !sd->bUserQuit )
500 : : {
501 : 185 : nRet1 = TreatCreateINChIWarning(sd, ip, prep_inp_data, num_inp,
502 : : inp_file, log_file, out_file, prb_file);
503 : 185 : nRet = inchi_max(nRet, nRet1);
504 : : }
505 : : }
506 : :
507 : :
508 : : /* 4. Sort and print INChI for the whole structure */
509 : :
510 : 188 : PrepareSaveOptBits(&save_opt_bits, ip);
511 [ + - + + ]: 188 : if ( nRet != _IS_FATAL && nRet != _IS_ERROR )
512 : : {
513 : 185 : nRet1 = SortAndPrintINChI(pCG, out_file, strbuf, log_file, ip,
514 : : orig_inp_data, prep_inp_data,
515 : : composite_norm_data,
516 : 185 : pOrigStruct, sd->num_components,
517 : 185 : sd->num_non_taut, sd->num_taut,
518 : 185 : sd->bTautFlags, sd->bTautFlagsDone,
519 : : pncFlags, num_inp,
520 : : pINChI, pINChI_Aux,
521 : : &bSortPrintINChIFlags, save_opt_bits); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
522 : : }
523 : :
524 : :
525 : : /* 5. Post-process */
526 : :
527 : 188 : DisplayOrigAndResultStructuresAndComponents(nRet, ic, pCG, sd, ip, szTitle,
528 : : pINChI, pINChI_Aux,
529 : : inp_file, log_file, out_file,
530 : : orig_inp_data, prep_inp_data,
531 : : num_inp, maxINChI,
532 : : composite_norm_data);
533 : :
534 : :
535 : 188 : SaveOkProcessedMolfile(nRet, sd, ip, prb_file, inp_file);
536 : :
537 : :
538 : : /* Cleanup */
539 : :
540 [ + + ]: 564 : for ( i = 0; i < INCHI_NUM; i++ )
541 : : {
542 [ + + ]: 1504 : for ( k = 0; k < TAUT_NUM + 1; k++ )
543 : : {
544 : 1128 : FreeCompAtomData(&composite_norm_data[i][k]);
545 : : }
546 : : }
547 : :
548 : 188 : OrigStruct_Free(pOrigStruct);
549 : :
550 : 188 : exit_function:
551 : :
552 : 188 : return nRet;
553 : : }
554 : :
555 : :
556 : :
557 : : /****************************************************************************
558 : : Early preprocessing: used if defined
559 : : REMOVE_ION_PAIRS_ORIG_STRU or UNDERIVATIZE or RING2CHAIN
560 : : ****************************************************************************/
561 : :
562 : :
563 : 188 : int DoOneStructureEarlyPreprocessing(INCHI_CLOCK* ic,
564 : : CANON_GLOBALS* pCG,
565 : : long num_inp,
566 : : STRUCT_DATA* sd,
567 : : INPUT_PARMS* ip,
568 : : INCHI_IOSTREAM* inp_file,
569 : : INCHI_IOSTREAM* log_file,
570 : : INCHI_IOSTREAM* out_file,
571 : : INCHI_IOSTREAM* prb_file,
572 : : ORIG_ATOM_DATA* orig_inp_data,
573 : : ORIG_ATOM_DATA* prep_inp_data)
574 : : {
575 : :
576 : : #if ( RING2CHAIN == 1 || UNDERIVATIZE == 1 )
577 : 188 : int ret1 = 0, ret2 = 0; /* djb-rwth: removing redundant variables */
578 : : #endif
579 : :
580 : : #if ( REMOVE_ION_PAIRS_ORIG_STRU == 1 )
581 : : fix_odd_things(orig_inp_data->num_inp_atoms, orig_inp_data->at, 0, ip->bFixNonUniformDraw);
582 : : #endif
583 : :
584 : : #if ( UNDERIVATIZE == 1 )
585 [ - + ]: 188 : if ( ip->bUnderivatize )
586 : : {
587 [ # # ]: 0 : if ( 0 > (ret2 = OAD_Edit_Underivatize(ic, pCG, orig_inp_data, (ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY), ip->bUnderivatize & 2, ip->pSdfValue)) )
588 : : {
589 : 0 : long num_inp2 = num_inp;
590 : 0 : AddErrorMessage(sd->pStrErrStruct, "Underivatization error");
591 : 0 : sd->nStructReadError = 99;
592 : 0 : sd->nErrorType = _IS_ERROR;
593 : 0 : TreatErrorsInReadTheStructure(sd, ip, LOG_MASK_ALL, inp_file, log_file, out_file, prb_file,
594 : : prep_inp_data, &num_inp2);
595 : 0 : return _IS_ERROR; /* output only if derivatives found */
596 : : }
597 [ # # ]: 0 : else if ( 0 < ret2 )
598 : : {
599 [ # # ]: 0 : if ( !ip->bNoWarnings )
600 : : {
601 : 0 : WarningMessage(sd->pStrErrStruct, "Input structure underivatized");
602 : : }
603 : : }
604 : : }
605 : : #endif /* UNDERIVATIZE == 1 */
606 : :
607 : : #if ( RING2CHAIN == 1 )
608 [ - + - - ]: 188 : if ( ip->bRing2Chain && 0 > (ret1 = Ring2Chain(ic, pCG, orig_inp_data)) )
609 : : {
610 : 0 : long num_inp2 = num_inp;
611 : 0 : AddErrorMessage(sd->pStrErrStruct, "Ring to chain error");
612 : 0 : sd->nStructReadError = 99;
613 : 0 : sd->nErrorType = _IS_ERROR;
614 : : /* djb-rwth: removing redundant code */
615 : 0 : TreatErrorsInReadTheStructure(sd, ip, LOG_MASK_ALL,
616 : : inp_file, log_file,
617 : : out_file, prb_file,
618 : : prep_inp_data, &num_inp2);
619 : 0 : return _IS_ERROR; /* output only if derivatives found */
620 : : }
621 : : #endif /* RING2CHAIN == 1 */
622 : : #if ( RING2CHAIN == 1 || UNDERIVATIZE == 1 ) /***** post v.1 feature *****/
623 [ - + - - : 188 : if ( ip->bIgnoreUnchanged && !ret1 && !ret2 )
- - ]
624 : : {
625 : 0 : return _IS_SKIP; /* output only if derivatives or ring/chain found */
626 : : }
627 : : #endif /* RING2CHAIN == 1 || UNDERIVATIZE == 1 */
628 : 188 : return 0;
629 : : }
630 : :
631 : :
632 : : /* If requested, save input data to a Molfile instead of creating INChI */
633 : : /* Also used for output in case of combination of options 'InChI2Struct' and 'OutputSDF' */
634 : 186 : int OrigAtData_SaveMolfile(ORIG_ATOM_DATA* orig_inp_data,
635 : : STRUCT_DATA* sd,
636 : : INPUT_PARMS* ip,
637 : : long num_inp,
638 : : INCHI_IOSTREAM* out_file)
639 : : {
640 : 186 : int ret = 0;
641 : :
642 [ + - ]: 186 : if ( !(ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY) )
643 : : {
644 : 186 : return _IS_OKAY;
645 : : }
646 : : else
647 : : {
648 : : char szNumber[256];
649 [ # # # # : 0 : sprintf(szNumber, "Structure #%ld. %s%s%s%s", num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
# # # # #
# # # # #
# # # # #
# # # #
# ]
650 : 0 : ret = OrigAtData_WriteToSDfile(orig_inp_data, out_file, szNumber, NULL,
651 : 0 : (sd->bChiralFlag & FLAG_INP_AT_CHIRAL) ? 1 : 0,
652 : 0 : (ip->bINChIOutputOptions & INCHI_OUT_SDFILE_ATOMS_DT) ? 1 : 0,
653 : 0 : ip->pSdfLabel, ip->pSdfValue);
654 : : }
655 : :
656 : 0 : return ret;
657 : : }
658 : :
659 : :
660 : : /****************************************************************************
661 : : Optionally save native input data as 'OrigStruct' data package
662 : : ****************************************************************************/
663 : 188 : ORIG_STRUCT* OrigAtData_StoreNativeInput(CANON_GLOBALS* pCG,
664 : : int* nRet,
665 : : STRUCT_DATA* sd,
666 : : INPUT_PARMS* ip,
667 : : ORIG_ATOM_DATA* orig_inp_data,
668 : : ORIG_STRUCT* pOrigStruct)
669 : : {
670 : :
671 : : /* v. 1.05 always create and fill OrigStruc as it may be used to store e.g. polymer info */
672 : : /* If normal AuxInfo is requested, create full reversibility information from native inp data
673 : : if ( ip->bINChIOutputOptions & (INCHI_OUT_NO_AUX_INFO | INCHI_OUT_SHORT_AUX_INFO))
674 : : return NULL; */
675 : :
676 [ - + ]: 188 : if ( OrigStruct_FillOut(pCG, orig_inp_data, pOrigStruct, sd) )
677 : : {
678 : 0 : AddErrorMessage(sd->pStrErrStruct, "Cannot interpret reversibility information");
679 : 0 : sd->nStructReadError = 99;
680 : 0 : sd->nErrorType = _IS_ERROR;
681 : 0 : *nRet = _IS_ERROR;
682 : : }
683 : :
684 : 188 : return pOrigStruct;
685 : : }
686 : :
687 : :
688 : : /****************************************************************************
689 : : Prepare SaveOpt bits
690 : : ****************************************************************************/
691 : 188 : void PrepareSaveOptBits(unsigned char* save_opt_bits, INPUT_PARMS* ip)
692 : : {
693 [ + + ]: 188 : if ( ip->nInputType != INPUT_INCHI )
694 : : {
695 : 185 : *save_opt_bits = 0;
696 [ - + ]: 185 : if ( ip->bINChIOutputOptions & INCHI_OUT_SAVEOPT )
697 : : {
698 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_RECONNECT_COORD) )
699 : : {
700 : 0 : (*save_opt_bits) |= SAVE_OPT_RECMET;
701 : : }
702 [ # # ]: 0 : if ( 0 != (ip->nMode & REQ_MODE_BASIC) )
703 : : {
704 : 0 : (*save_opt_bits) |= SAVE_OPT_FIXEDH;
705 : : }
706 [ # # ]: 0 : if ( 0 != (ip->nMode & REQ_MODE_DIFF_UU_STEREO) )
707 : : {
708 : 0 : (*save_opt_bits) |= SAVE_OPT_SLUUD;
709 : : }
710 [ # # ]: 0 : if ( 0 == (ip->nMode & (REQ_MODE_SB_IGN_ALL_UU | REQ_MODE_SC_IGN_ALL_UU)) )
711 : : {
712 : 0 : (*save_opt_bits) |= SAVE_OPT_SUU;
713 : : }
714 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_KETO_ENOL_TAUT) )
715 : : {
716 : 0 : (*save_opt_bits) |= SAVE_OPT_KET;
717 : : }
718 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_1_5_TAUT) )
719 : : {
720 : 0 : (*save_opt_bits) |= SAVE_OPT_15T;
721 : : }
722 : : /* djb-rwth: addressing coverity ID #499536 -- despite different bit-sizes, works properly */
723 [ # # ]: 0 : if (0 != (ip->bTautFlags & TG_FLAG_PT_22_00))
724 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_22_00;
725 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_PT_16_00) )
726 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_16_00;
727 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_PT_06_00) )
728 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_06_00;
729 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_PT_39_00) )
730 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_39_00;
731 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_PT_13_00) )
732 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_13_00;
733 [ # # ]: 0 : if ( 0 != (ip->bTautFlags & TG_FLAG_PT_18_00) )
734 : 0 : (*save_opt_bits) |= SAVE_OPT_PT_18_00;
735 : : /* Check if /SNon requested and turn OFF stereo bits if so */
736 [ # # ]: 0 : if ( !(ip->nMode & REQ_MODE_STEREO) )
737 : : {
738 : 0 : (*save_opt_bits) &= ~SAVE_OPT_SUU;
739 : 0 : (*save_opt_bits) &= ~SAVE_OPT_SLUUD;
740 : : }
741 : : }
742 : : }
743 : 188 : }
744 : :
745 : :
746 : : /****************************************************************************
747 : : Display structures/components on screen
748 : : ****************************************************************************/
749 : 188 : void DisplayOrigAndResultStructuresAndComponents(int nRet,
750 : : INCHI_CLOCK* ic,
751 : : CANON_GLOBALS* pCG,
752 : : STRUCT_DATA* sd,
753 : : INPUT_PARMS* ip,
754 : : char* szTitle,
755 : : PINChI2* pINChI[INCHI_NUM],
756 : : PINChI_Aux2* pINChI_Aux[INCHI_NUM],
757 : : INCHI_IOSTREAM* inp_file,
758 : : INCHI_IOSTREAM* log_file,
759 : : INCHI_IOSTREAM* out_file,
760 : : ORIG_ATOM_DATA* orig_inp_data,
761 : : ORIG_ATOM_DATA* prep_inp_data,
762 : : long num_inp,
763 : : int maxINChI,
764 : : COMP_ATOM_DATA composite_norm_data[INCHI_NUM][TAUT_NUM + 1])
765 : : {
766 : :
767 : :
768 [ - + ]: 188 : if ( ip->bDisplay ) ip->bDisplayCompositeResults = 1; /* v. 1.05 */
769 : :
770 : : #ifndef COMPILE_ANSI_ONLY /* { */
771 : :
772 : : /* Display equivalent components on original or preprocessed structure(s) */
773 : : #ifndef TARGET_LIB_FOR_WINCHI
774 : : if ( nRet != _IS_FATAL && nRet != _IS_ERROR && /*ip->bDisplay &&*/
775 : : (ip->bCompareComponents & CMP_COMPONENTS) && !sd->bUserQuit && !sd->bUserQuitComponent )
776 : : {
777 : : int j, ret, ord;
778 : : int bDisplaySaved = ip->bDisplay;
779 : : ORIG_ATOM_DATA* inp_data;
780 : : AT_NUMB nEquSet;
781 : : for ( ord = -1; ord < INCHI_NUM; ord++ )
782 : : {
783 : : switch ( ord )
784 : : {
785 : : case -1:
786 : : j = INCHI_BAS; /* preprocessed non-tautomeric */
787 : : break;
788 : : case 0:
789 : : j = INCHI_REC; /* preprocessed tautomeric */
790 : : break;
791 : : case 1:
792 : : j = -1; /* original input */
793 : : break;
794 : : default:
795 : : continue;
796 : : }
797 : : inp_data = j < 0 ? orig_inp_data : prep_inp_data + j;
798 : : if ( inp_data && inp_data->num_inp_atoms && inp_data->at &&
799 : : inp_data->nEquLabels &&
800 : : inp_data->nNumEquSets )
801 : : {
802 : : for ( nEquSet = 1; nEquSet <= inp_data->nNumEquSets; nEquSet++ )
803 : : {
804 : : ip->dp.nEquLabels = inp_data->nEquLabels;
805 : : ip->dp.nCurEquLabel = nEquSet;
806 : : ip->dp.nNumEquSets = inp_data->nNumEquSets;
807 : : ip->bDisplay = 1; /* force display if it was not requested */
808 : : ret = DisplayTheWholeStructure(pCG, ic, sd, ip, szTitle,
809 : : inp_file, log_file, inp_data,
810 : : num_inp, j, 1 /*bShowStructure*/, 0);
811 : : ip->dp.nEquLabels = NULL;
812 : : ip->dp.nCurEquLabel = 0;
813 : : ip->dp.nNumEquSets = 0;
814 : : ip->bDisplay = bDisplaySaved; /* restore display option */
815 : : if ( ret )
816 : : {
817 : : /* user pressed Esc */
818 : : goto exit_loop;
819 : : }
820 : : }
821 : : }
822 : : }
823 : : exit_loop:;
824 : : }
825 : : #endif
826 : :
827 : : /* Display composite results and equivalent components on composite results */
828 : : if ( nRet != _IS_FATAL && nRet != _IS_ERROR && /*ip->bDisplay &&*/ ip->bDisplayCompositeResults )
829 : : {
830 : : int iINChI;
831 : : for ( iINChI = 0; iINChI < maxINChI && !sd->bUserQuitComponentDisplay; iINChI++ )
832 : : {
833 : : DisplayTheWholeCompositeStructure(pCG, ic, ip, sd, num_inp,
834 : : iINChI, pINChI[iINChI], pINChI_Aux[iINChI],
835 : : orig_inp_data, prep_inp_data, composite_norm_data[iINChI]);
836 : : }
837 : : #ifndef TARGET_LIB_FOR_WINCHI
838 : : if ( !ip->bDisplay && sd->bUserQuitComponentDisplay )
839 : : {
840 : : sd->bUserQuit = 1;
841 : : }
842 : : #endif
843 : : }
844 : :
845 : : #endif /* } COMPILE_ANSI_ONLY */
846 : :
847 : 188 : return;
848 : : }
849 : :
850 : :
851 : : /****************************************************************************
852 : : Special mode (option /PGO) : extract all good MOLfiles into the problem file;
853 : : do not extract any MOLfile that could not be processed.
854 : : ****************************************************************************/
855 : 188 : void SaveOkProcessedMolfile(int nRet,
856 : : STRUCT_DATA* sd,
857 : : INPUT_PARMS* ip,
858 : : INCHI_IOSTREAM* prb_file,
859 : : INCHI_IOSTREAM* inp_file)
860 : : {
861 [ - + - - ]: 188 : if ( ip->bSaveAllGoodStructsAsProblem &&
862 [ # # ]: 0 : nRet != _IS_FATAL &&
863 [ # # ]: 0 : nRet != _IS_ERROR &&
864 : 0 : prb_file &&
865 [ # # ]: 0 : prb_file->f &&
866 [ # # ]: 0 : 0L <= sd->fPtrStart &&
867 [ # # ]: 0 : sd->fPtrStart < sd->fPtrEnd )
868 : : {
869 : 0 : MolfileSaveCopy( inp_file, sd->fPtrStart, sd->fPtrEnd, prb_file->f, 0 ); /* djb-rwth: addressing coverity ID #499510 -- return values handled properly */
870 : : }
871 : :
872 : 188 : return;
873 : : }
874 : :
875 : :
876 : : /****************************************************************************
877 : : Generate InChI for the whole (multi-component) structure
878 : : ****************************************************************************/
879 : 186 : int CreateOneStructureINChI(CANON_GLOBALS* pCG,
880 : : INCHI_CLOCK* ic,
881 : : STRUCT_DATA* sd,
882 : : INPUT_PARMS* ip,
883 : : char* szTitle,
884 : : PINChI2* pINChI2[INCHI_NUM],
885 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
886 : : int iINChI,
887 : : INCHI_IOSTREAM* inp_file,
888 : : INCHI_IOSTREAM* log_file,
889 : : INCHI_IOSTREAM* out_file,
890 : : INCHI_IOSTREAM* prb_file,
891 : : ORIG_ATOM_DATA* orig_inp_data,
892 : : ORIG_ATOM_DATA* prep_inp_data,
893 : : COMP_ATOM_DATA composite_norm_data2[][TAUT_NUM + 1],
894 : : long num_inp,
895 : : INCHI_IOS_STRING* strbuf,
896 : : NORM_CANON_FLAGS* pncFlags)
897 : : {
898 : 186 : int i, j, k, nRet = 0, n = 0l;
899 : : #if defined (TARGET_EXE_STANDALONE) && defined(_WIN32)
900 : : int err_display;
901 : : #endif
902 : :
903 : 186 : PINChI2 *pINChI = NULL;
904 : 186 : PINChI_Aux2 *pINChI_Aux = NULL;
905 : :
906 : : INP_ATOM_DATA InpCurAtData;
907 : : INP_ATOM_DATA* inp_cur_data;
908 : :
909 : : INP_ATOM_DATA InpNormAtData, InpNormTautData;
910 : : INP_ATOM_DATA* inp_norm_data[TAUT_NUM]; /* = { &InpNormAtData, &InpNormTautData }; */
911 : 186 : ORIG_ATOM_DATA* cur_prep_inp_data = prep_inp_data + iINChI;
912 : : inchiTime ulTStart;
913 : :
914 : : /* Always create info data structures (but do not display them always )
915 : : #ifndef COMPILE_ANSI_ONLY
916 : : */
917 : 186 : int bShowStructure = 0;
918 : 186 : int bStructurePreprocessed = 0; /* All changes except disconnection */
919 : 186 : int bStructureDisconnected = 0;
920 : 186 : int bAlsoOutputReconnected = 0, bINCHI_LIB_Flag = 0;
921 : 186 : COMP_ATOM_DATA* composite_norm_data = composite_norm_data2[iINChI];
922 : 186 : INP_ATOM_DATA2* all_inp_norm_data = NULL;
923 : : /*#endif*/
924 : :
925 : : /* Order of actions:
926 : :
927 : : if ( orig_inp_data is NOT empty AND
928 : : prep_inp_data[0] IS empty ) then do
929 : : in PreprocessOneStructure() :
930 : :
931 : : 1. copy orig_inp_data --> prep_inp_data[0]
932 : : 2. fix odd things in prep_inp_data[0]
933 : : 3. if( orig_inp_data->bDisconnectSalts ) then
934 : : -- disconnect salts in prep_inp_data[0]
935 : : 4. move protons to neutralize charges on heteroatoms
936 : : 5. if( orig_inp_data->bDisconnectCoord ) then
937 : : -- copy prep_inp_data[0] --> prep_inp_data[1]
938 : : -- disconnect metals in prep_inp_data[0]
939 : :
940 : : iINChI = 0
941 : : =========
942 : : (normal/disconnected layer)
943 : :
944 : : 1. normalize prep_inp_data[0] in inp_norm_data[0,1]
945 : : 2. create INChI[ iINChI ] out of inp_norm_data[0,1]
946 : :
947 : :
948 : : iINChI = 1 AND orig_inp_data->bDisconnectCoord > 0
949 : : =================================================
950 : : (reconnected layer)
951 : :
952 : : 1. normalize prep_inp_data[1] in inp_norm_data[0,1]
953 : : 2. create INChI[ iINChI ] out of inp_norm_data[0,1]
954 : :
955 : : */
956 : :
957 : 186 : ip->msec_LeftTime = ip->msec_MaxTime; /* start timeout countdown for each component */
958 : :
959 : 186 : inp_cur_data = &InpCurAtData;
960 : 186 : inp_norm_data[TAUT_NON] = &InpNormAtData;
961 : 186 : inp_norm_data[TAUT_YES] = &InpNormTautData;
962 : :
963 : 186 : memset(inp_cur_data, 0, sizeof(*inp_cur_data)); /* djb-rwth: memset_s C11/Annex K variant? */
964 : 186 : memset(inp_norm_data[TAUT_NON], 0, sizeof(*inp_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
965 : 186 : memset(inp_norm_data[TAUT_YES], 0, sizeof(*inp_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
966 : :
967 : : {
968 : : /*#ifndef COMPILE_ANSI_ONLY*/
969 : 186 : memset(composite_norm_data + TAUT_NON, 0, sizeof(composite_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
970 : 186 : memset(composite_norm_data + TAUT_YES, 0, sizeof(composite_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
971 : 186 : memset(composite_norm_data + TAUT_INI, 0, sizeof(composite_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
972 : : } /*#endif*/
973 : :
974 [ - + - - : 186 : if ( ip->bAllowEmptyStructure && !orig_inp_data->at && !orig_inp_data->num_inp_atoms )
- - ]
975 : : {
976 : : ;
977 : : }
978 [ + - - + ]: 186 : else if ( !orig_inp_data->at || !orig_inp_data->num_inp_atoms )
979 : : {
980 : 0 : return 0; /* nothing to do */
981 : : }
982 [ - + - - ]: 186 : if ( iINChI == 1 && orig_inp_data->bDisconnectCoord <= 0 )
983 : : {
984 : 0 : return 0;
985 : : }
986 : :
987 : : /* m = iINChI; */ /* orig_inp_data index */
988 [ - + - - ]: 186 : if ( iINChI != INCHI_BAS && iINChI != INCHI_REC )
989 : : {
990 : 0 : AddErrorMessage(sd->pStrErrStruct, "Fatal undetermined program error");
991 : 0 : sd->nStructReadError = 97;
992 : 0 : nRet = sd->nErrorType = _IS_FATAL;
993 [ # # ]: 0 : inchi_free(all_inp_norm_data); /* djb-rwth: avoiding memory leak */
994 : 0 : goto exit_function;
995 : : }
996 : :
997 : : /*******************************************************************
998 : : * *
999 : : * *
1000 : : * Whole structure preprocessing: 1st step of the normalization *
1001 : : * *
1002 : : * Happen only on the first call to CreateOneStructureINChI() *
1003 : : * *
1004 : : * *
1005 : : *******************************************************************/
1006 : :
1007 [ + + + - ]: 186 : if ( (!prep_inp_data->at || !prep_inp_data->num_inp_atoms) &&
1008 [ + - ]: 186 : orig_inp_data->num_inp_atoms > 0 )
1009 : : {
1010 : : /* The structure has not been preprocessed */
1011 [ - + ]: 186 : if ( ip->msec_MaxTime )
1012 : : {
1013 : 0 : InchiTimeGet(&ulTStart);
1014 : : }
1015 : :
1016 : 186 : PreprocessOneStructure(ic, sd, ip, orig_inp_data, prep_inp_data);
1017 : :
1018 : 186 : pncFlags->bTautFlags[iINChI][TAUT_YES] =
1019 : 186 : pncFlags->bTautFlags[iINChI][TAUT_NON] =
1020 : 186 : sd->bTautFlags[INCHI_BAS] | ip->bTautFlags;
1021 : :
1022 : 186 : pncFlags->bTautFlagsDone[iINChI][TAUT_YES] =
1023 : 186 : pncFlags->bTautFlagsDone[iINChI][TAUT_NON] =
1024 : 186 : sd->bTautFlagsDone[INCHI_BAS] | ip->bTautFlagsDone;
1025 : :
1026 : : {
1027 : : /*#ifndef COMPILE_ANSI_ONLY*/
1028 : : /* in this location the call happens once for each input structure, before preprocessing */
1029 : 186 : bStructurePreprocessed = (0 != (sd->bTautFlagsDone[INCHI_BAS] & (
1030 : : TG_FLAG_MOVE_HPLUS2NEUTR_DONE |
1031 : : TG_FLAG_DISCONNECT_SALTS_DONE |
1032 : : TG_FLAG_MOVE_POS_CHARGES_DONE |
1033 : : TG_FLAG_FIX_ODD_THINGS_DONE)));
1034 : :
1035 : 186 : bStructureDisconnected = (0 != (sd->bTautFlagsDone[INCHI_BAS] & TG_FLAG_DISCONNECT_COORD_DONE));
1036 : :
1037 [ + + ]: 186 : bShowStructure = (bStructurePreprocessed ||
1038 [ + - ]: 372 : bStructureDisconnected ||
1039 [ + + ]: 185 : prep_inp_data[0].num_components > 1);
1040 : :
1041 : : /* sd->bTautFlags[] contains output flags
1042 : : ip->bTautFlags contains input flags
1043 : : */
1044 [ + + ]: 187 : bAlsoOutputReconnected = (sd->bTautFlagsDone[INCHI_BAS] & TG_FLAG_DISCONNECT_COORD_DONE) &&
1045 [ - + ]: 1 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD);
1046 : 186 : bINCHI_LIB_Flag = 0;
1047 : :
1048 : : /*************** output structures to TARGET_LIB_FOR_WINCHI conditions *********************
1049 : : *
1050 : : * Send to TARGET_LIB_FOR_WINCHI:
1051 : : *
1052 : : * type component conditions
1053 : : *
1054 : : * COMPONENT_ORIGINAL #0: (num_components > 1)
1055 : : * COMPONENT_ORIGINAL_PREPROCESSED #0: (num_components > 1) && (preprocessed)
1056 : : * COMPONENT_ORIGINAL #1: (num_components = 1) && (preprocessed)
1057 : : *
1058 : : * Flags explanation:
1059 : : * MAIN => iINChI=0, RECN => iINChI=1 (Reconnected)
1060 : : * ORIG => Original, PREP => Preprocessed
1061 : : *
1062 : : * Possible flags: k
1063 : : *
1064 : : * COMP_ORIG_0_MAIN 0x0001 0 COMPONENT_ORIGINAL, bMain, component #0
1065 : : * COMP_ORIG_0_RECN 0x0002 1 COMPONENT_ORIGINAL, bRecn, component #0
1066 : : *
1067 : : * COMP_PREP_0_MAIN 0x0004 2 COMPONENT_ORIGINAL_PREPROCESSED, bMain, component #0
1068 : : * COMP_PREP_0_RECN 0x0008 3 COMPONENT_ORIGINAL_PREPROCESSED, bRecn, component #0
1069 : : *
1070 : : * COMP_ORIG_1_MAIN 0x0010 4 COMPONENT_ORIGINAL, bMain, component #1
1071 : : * COMP_ORIG_1_RECN 0x0020 5 COMPONENT_ORIGINAL, bRecn, component #1
1072 : : *
1073 : : * bReconnected = k%2 (0 or 1)
1074 : : * nComponent = k/4 (0 or 1)
1075 : : * bPreprocessed = (k/2)%2 (0 or 1)
1076 : : *
1077 : : ******************************************************************************/
1078 : :
1079 : : /* Original -> Main, component #0, Original */
1080 [ + + ]: 186 : if ( prep_inp_data[INCHI_BAS].num_components > 1 )
1081 : : {
1082 : 40 : bINCHI_LIB_Flag |= COMP_ORIG_0_MAIN;
1083 : : }
1084 : : else
1085 : : {
1086 : : /* Original -> Main, component #1, Original */
1087 [ + - - + ]: 146 : if ( prep_inp_data[INCHI_BAS].num_components == 1 && bStructurePreprocessed )
1088 : : {
1089 : 0 : bINCHI_LIB_Flag |= COMP_ORIG_1_MAIN;
1090 : : /* preprocessed will be added when output canonicalization results */
1091 : : }
1092 : : }
1093 : :
1094 [ - + ]: 186 : if ( bAlsoOutputReconnected )
1095 : : {
1096 : : /* Original -> Reconnected, component #0, Original */
1097 [ # # ]: 0 : if ( prep_inp_data[INCHI_REC].num_components > 1 )
1098 : : {
1099 : 0 : bINCHI_LIB_Flag |= COMP_ORIG_0_RECN;
1100 : : }
1101 [ # # # # ]: 0 : else if ( prep_inp_data[INCHI_BAS].num_components == 1 && bStructurePreprocessed )
1102 : : {
1103 : : /* Original -> Reconnected, component #1, Original */
1104 : 0 : bINCHI_LIB_Flag |= COMP_ORIG_1_RECN;
1105 : : /* preprocessed will be added when output canonicalization results */
1106 : : }
1107 : : }
1108 [ - + ]: 186 : if ( ip->msec_MaxTime )
1109 : : {
1110 : 0 : ip->msec_LeftTime -= InchiTimeElapsed(ic, &ulTStart);
1111 : : }
1112 : :
1113 : : /* display the ORIGINAL, UN-PREPROCESSED structure */
1114 : :
1115 [ - + ]: 186 : if ( ip->bDisplay )
1116 : : {
1117 [ # # ]: 0 : if ( DisplayTheWholeStructure(pCG, ic, sd, ip, szTitle,
1118 : : inp_file, log_file, orig_inp_data, num_inp,
1119 : : -1, bShowStructure, bINCHI_LIB_Flag) )
1120 : : {
1121 [ # # ]: 0 : inchi_free(all_inp_norm_data); /* djb-rwth: avoiding memory leak */
1122 : 0 : goto exit_function;
1123 : : }
1124 : : }
1125 : : } /*#endif */
1126 : :
1127 [ - + ]: 186 : switch ( sd->nErrorType )
1128 : : {
1129 : 0 : case _IS_ERROR:
1130 : : case _IS_FATAL:
1131 : : /* error message */
1132 : 0 : nRet = TreatErrorsInReadTheStructure(sd, ip,
1133 : : LOG_MASK_ALL,
1134 : : inp_file, log_file, out_file, prb_file,
1135 : : prep_inp_data, &num_inp);
1136 : 0 : goto exit_cycle;
1137 : : }
1138 : : }
1139 : : /* tranfer flags from INChI_Aux to sd */
1140 : :
1141 : :
1142 : : {
1143 : : /*#ifndef COMPILE_ANSI_ONLY */ /* { */
1144 : :
1145 : : /******************************************/
1146 : : /* Displaying the structures */
1147 : : /* Only under WIN32 */
1148 : : /******************************************/
1149 : 186 : if ( /* ip->bDisplayCompositeResults && !sd->bUserQuitComponentDisplay && */
1150 [ + + ]: 186 : prep_inp_data[iINChI].num_components > 1
1151 : : )
1152 : : {
1153 : 40 : all_inp_norm_data = (INP_ATOM_DATA2*)inchi_calloc(prep_inp_data[iINChI].num_components, sizeof(all_inp_norm_data[0]));
1154 : : }
1155 : :
1156 : : /* Display the input structure AFTER PREPROCESSING */
1157 [ + - - ]: 186 : switch ( iINChI )
1158 : : {
1159 : 186 : case INCHI_BAS:
1160 : : /*------------ Possibly disconnected structure -------------------*/
1161 : 186 : bStructurePreprocessed = 0 != (sd->bTautFlagsDone[iINChI] & (
1162 : : TG_FLAG_MOVE_HPLUS2NEUTR_DONE |
1163 : : TG_FLAG_DISCONNECT_SALTS_DONE |
1164 : : TG_FLAG_MOVE_POS_CHARGES_DONE |
1165 : : TG_FLAG_MOVE_CHARGE_COORD_DONE |
1166 : : TG_FLAG_DISCONNECT_COORD_DONE |
1167 : : TG_FLAG_FIX_ODD_THINGS_DONE));
1168 : 186 : bINCHI_LIB_Flag = 0;
1169 : : /* Preprocessed/Main -> Main, component #0, Preprocessed */
1170 [ + + + + ]: 186 : if ( prep_inp_data[iINChI].num_components > 1 &&
1171 : : bStructurePreprocessed )
1172 : : {
1173 : 1 : bINCHI_LIB_Flag |= COMP_PREP_0_MAIN;
1174 : : }
1175 [ + + ]: 187 : bShowStructure = (bStructurePreprocessed &&
1176 [ + - ]: 1 : prep_inp_data[iINChI].num_components > 1);
1177 : 186 : break;
1178 : :
1179 : 0 : case INCHI_REC:
1180 : : /*------------ Reconnected structure ------------------------------*/
1181 : 0 : bAlsoOutputReconnected =
1182 [ # # ]: 0 : (sd->bTautFlagsDone[INCHI_BAS] & TG_FLAG_DISCONNECT_COORD_DONE) &&
1183 [ # # ]: 0 : (ip->bTautFlags & TG_FLAG_RECONNECT_COORD);
1184 : :
1185 [ # # ]: 0 : if ( !bAlsoOutputReconnected )
1186 : : {
1187 : 0 : break;
1188 : : }
1189 : :
1190 : 0 : bStructurePreprocessed = 0 != (sd->bTautFlagsDone[iINChI] & (
1191 : : TG_FLAG_MOVE_HPLUS2NEUTR_DONE |
1192 : : TG_FLAG_DISCONNECT_SALTS_DONE |
1193 : : TG_FLAG_MOVE_POS_CHARGES_DONE |
1194 : : TG_FLAG_FIX_ODD_THINGS_DONE));
1195 : 0 : bINCHI_LIB_Flag = 0;
1196 : : /* Preprocessed/Reconnected -> Reconnected, component #0, Preprocessed */
1197 [ # # # # ]: 0 : if ( prep_inp_data[iINChI].num_components > 1 && bStructurePreprocessed )
1198 : : {
1199 : 0 : bINCHI_LIB_Flag |= COMP_PREP_0_RECN;
1200 : : }
1201 [ # # ]: 0 : bShowStructure = (bStructurePreprocessed &&
1202 [ # # ]: 0 : prep_inp_data[iINChI].num_components > 1);
1203 : 0 : break;
1204 : :
1205 : 0 : default:
1206 : 0 : bShowStructure = 0;
1207 : : }
1208 : :
1209 : :
1210 [ - + - - ]: 186 : if ( ip->bDisplay && prep_inp_data[iINChI].num_inp_atoms > 0 )
1211 : : {
1212 [ # # ]: 0 : if ( DisplayTheWholeStructure(pCG, ic, sd, ip, szTitle,
1213 : : inp_file, log_file,
1214 : 0 : prep_inp_data + iINChI,
1215 : : num_inp,
1216 : : iINChI,
1217 : : bShowStructure,
1218 : : bINCHI_LIB_Flag) )
1219 : : {
1220 [ # # ]: 0 : inchi_free(all_inp_norm_data); /* djb-rwth: avoiding memory leak */
1221 : 0 : goto exit_function;
1222 : : }
1223 : : }
1224 : : } /* #endif */ /* } ifndef COMPILE_ANSI_ONLY */
1225 : :
1226 : :
1227 : : /* allocate pINChI[iINChI] and pINChI_Aux2[iINChI] -- arrays of pointers to INChI and INChI_Aux */
1228 : : /* assign values to sd->num_components[] */
1229 : :
1230 : : /* djb-rwth: MYREALLOC2 has been replaced and the whole block rewritten to address memory leaks and reading from freed memory locations */
1231 : : do {
1232 [ + - ]: 186 : if ( (sd->num_components[iINChI]) <= ((long long)cur_prep_inp_data->num_components) ) {
1233 : 186 : PINChI2* newPTR1 = (PINChI2*)inchi_calloc(((long long)cur_prep_inp_data->num_components) + 1, sizeof(PINChI2));
1234 : 186 : PINChI_Aux2* newPTR2 = (PINChI_Aux2*)inchi_calloc(((long long)cur_prep_inp_data->num_components) + 1, sizeof(PINChI_Aux2));
1235 [ + - + - ]: 186 : if ( newPTR1 && newPTR2 ) {
1236 [ - + - - ]: 186 : if ( (pINChI2[iINChI]) && (sd->num_components[iINChI]) > 0 )
1237 : 0 : memcpy(newPTR1, pINChI2[iINChI], (sd->num_components[iINChI]) * sizeof(PINChI2));
1238 [ - + - - ]: 186 : if ( (pINChI_Aux2[iINChI]) && (sd->num_components[iINChI]) > 0 )
1239 : 0 : memcpy(newPTR2, pINChI_Aux2[iINChI], (sd->num_components[iINChI]) * sizeof(PINChI_Aux2));
1240 [ - + ]: 186 : if ( pINChI2[iINChI] )
1241 [ # # ]: 0 : inchi_free(pINChI2[iINChI]);
1242 [ - + ]: 186 : if ( pINChI_Aux2[iINChI] )
1243 [ # # ]: 0 : inchi_free(pINChI_Aux2[iINChI]);
1244 : 186 : pINChI2[iINChI] = newPTR1;
1245 : 186 : pINChI_Aux2[iINChI] = newPTR2;
1246 : 186 : sd->num_components[iINChI] = cur_prep_inp_data->num_components;
1247 : 186 : k = 0;
1248 : : }
1249 : : else {
1250 [ # # ]: 0 : inchi_free(newPTR1);
1251 [ # # ]: 0 : inchi_free(newPTR2);
1252 : 0 : k = 1;
1253 : : }
1254 : : }
1255 : 0 : else { k = 0; }
1256 : : } while ( 0 );
1257 : :
1258 [ - + ]: 186 : if ( k )
1259 : : {
1260 : 0 : AddErrorMessage(sd->pStrErrStruct, "Cannot allocate output data. Terminating");
1261 : 0 : sd->nStructReadError = 99;
1262 : 0 : sd->nErrorType = _IS_FATAL;
1263 [ # # ]: 0 : inchi_free(all_inp_norm_data); /* djb-rwth: avoiding memory leak */
1264 : 0 : goto exit_function;
1265 : : }
1266 : :
1267 : 186 : pINChI = pINChI2[iINChI];
1268 : 186 : pINChI_Aux = pINChI_Aux2[iINChI];
1269 : :
1270 : : /**************************************************************************/
1271 : : /* */
1272 : : /* */
1273 : : /* M A I N C Y C L E: P R O C E S S C O M P O N E N T S */
1274 : : /* */
1275 : : /* */
1276 : : /* O N E B Y O N E */
1277 : : /* */
1278 : : /* */
1279 : : /**************************************************************************/
1280 : :
1281 : 186 : for ( i = 0, nRet = 0;
1282 [ - + + + ]: 1447 : !sd->bUserQuitComponent && i < cur_prep_inp_data->num_components;
1283 : 1261 : i++ )
1284 : : {
1285 [ - + ]: 1262 : if ( ip->msec_MaxTime )
1286 : : {
1287 : 0 : InchiTimeGet(&ulTStart);
1288 : : }
1289 : :
1290 : : #ifndef TARGET_LIB_FOR_WINCHI /* { */
1291 : : #if ( bREUSE_INCHI == 1 )
1292 : :
1293 [ - + ]: 1262 : if ( (iINChI == INCHI_REC &&
1294 : : /*( !ip->bDisplay &&
1295 : : !ip->bDisplayCompositeResults && */
1296 [ # # ]: 0 : !(ip->bCompareComponents & CMP_COMPONENTS)) ||
1297 [ - + ]: 1262 : sd->bUserQuitComponentDisplay ) /* djb-rwth: addressing LLVM warning */
1298 : : {
1299 : : /* Reconnected structure (06-20-2005: added "&& !ip->bDisplayCompositeResults" to display composite structure) */
1300 : 0 : int m = iINChI - 1;
1301 : :
1302 : : /* Find whether we have already calculated this INChI in basic (disconnected) layer */
1303 [ # # ]: 0 : for ( j = n = 0; j < prep_inp_data[m].num_components; j++ )
1304 : : {
1305 [ # # ]: 0 : if ( i + 1 == prep_inp_data[m].nOldCompNumber[j] &&
1306 [ # # # # ]: 0 : (pINChI2[m][j][TAUT_NON] || pINChI2[m][j][TAUT_YES]) )
1307 : : {
1308 : : /* Yes, we have already done this */
1309 [ # # ]: 0 : if ( !n++ )
1310 : : {
1311 : 0 : memcpy(pINChI + i, pINChI2[m] + j, sizeof(pINChI[0]));
1312 : 0 : memcpy(pINChI_Aux + i, pINChI_Aux2[m] + j, sizeof(pINChI_Aux[0]));
1313 [ # # ]: 0 : for ( k = 0; k < TAUT_NUM; k++ )
1314 : : {
1315 [ # # ]: 0 : if ( pINChI[i][k] )
1316 : : {
1317 : 0 : pINChI[i][k]->nRefCount++;
1318 [ # # ]: 0 : if ( pINChI[i][k]->nNumberOfAtoms > 0 )
1319 : : {
1320 [ # # # ]: 0 : switch ( k )
1321 : : {
1322 : 0 : case TAUT_NON:
1323 : 0 : sd->num_non_taut[iINChI]++;
1324 : 0 : break;
1325 : 0 : case TAUT_YES:
1326 [ # # ]: 0 : if ( pINChI[i][k]->lenTautomer > 0 )
1327 : : {
1328 : 0 : sd->num_taut[iINChI]++;
1329 : : }
1330 : : else
1331 [ # # ]: 0 : if ( !pINChI[i][TAUT_NON] ||
1332 [ # # ]: 0 : !pINChI[i][TAUT_NON]->nNumberOfAtoms )
1333 : : {
1334 : 0 : sd->num_non_taut[iINChI]++;
1335 : : }
1336 : 0 : break;
1337 : : }
1338 : : }
1339 : : }
1340 [ # # ]: 0 : if ( pINChI_Aux[i][k] )
1341 : : {
1342 : 0 : pINChI_Aux[i][k]->nRefCount++;
1343 : : }
1344 : : }
1345 : : }
1346 : : }
1347 : : }
1348 : :
1349 [ # # ]: 0 : if ( n == 1 )
1350 : : {
1351 : 0 : continue;
1352 : : }
1353 [ # # ]: 0 : if ( n > 1 )
1354 : : {
1355 : : /* ith component is equivalent to more than one another component */
1356 : 0 : AddErrorMessage(sd->pStrErrStruct, "Cannot distinguish components");
1357 : 0 : sd->nStructReadError = 99;
1358 : 0 : sd->nErrorType = _IS_ERROR;
1359 [ # # ]: 0 : inchi_free(all_inp_norm_data); /* djb-rwth: avoiding memory leak */
1360 : 0 : goto exit_function;
1361 : : }
1362 : : }
1363 : :
1364 : : #endif
1365 : : #endif /* } TARGET_LIB_FOR_WINCHI */
1366 : :
1367 : : /*****************************************************/
1368 : : /* a) Allocate memory and extract current component */
1369 : : /*****************************************************/
1370 : :
1371 : 1262 : nRet = GetOneComponent(ic, sd, ip,
1372 : : log_file, out_file,
1373 : : inp_cur_data, cur_prep_inp_data,
1374 : : i, num_inp);
1375 : :
1376 [ - + ]: 1262 : if ( ip->msec_MaxTime )
1377 : : {
1378 : 0 : ip->msec_LeftTime -= InchiTimeElapsed(ic, &ulTStart);
1379 : : }
1380 : :
1381 [ - + ]: 1262 : switch ( nRet )
1382 : : {
1383 : 0 : case _IS_ERROR:
1384 : : case _IS_FATAL:
1385 : 0 : goto exit_cycle;
1386 : : }
1387 : :
1388 : : #if !defined(TARGET_API_LIB) && !defined(COMPILE_ANSI_ONLY)
1389 : : /* console request: Display the component? */
1390 : : if ( ip->bDisplay && inp_file->f != stdin )
1391 : : {
1392 : : if ( user_quit(ic, "Enter=Display Component, Esc=Stop ?", ip->ulDisplTime) )
1393 : : {
1394 : : sd->bUserQuitComponent = 1;
1395 : : break;
1396 : : }
1397 : : }
1398 : : #endif
1399 : :
1400 : : /*#ifndef COMPILE_ANSI_ONLY
1401 : : { */
1402 : :
1403 : : /* b) Display the extracted original component structure */
1404 [ - + - - : 1262 : if ( ip->bDisplay && inp_cur_data->at && !sd->bUserQuitComponentDisplay )
- - ]
1405 : : {
1406 [ # # ]: 0 : if ( cur_prep_inp_data->num_components == 1 )
1407 : : {
1408 [ # # # # ]: 0 : sprintf(szTitle, "%sInput Structure #%ld.%s%s%s%s%s",
1409 : : bStructurePreprocessed ? "Preprocessed " : "",
1410 [ # # # # : 0 : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue), iINChI ? " (Reconnected)" : "");
# # # # #
# # # # #
# # # # #
# # # #
# ]
1411 : : }
1412 : : else
1413 : : {
1414 [ # # ]: 0 : sprintf(szTitle, "Component #%d of %d, Input Structure #%ld.%s%s%s%s%s",
1415 : : i + 1, cur_prep_inp_data->num_components,
1416 [ # # # # : 0 : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue), iINChI ? " (Reconnected)" : "");
# # # # #
# # # # #
# # # # #
# # # #
# ]
1417 : : }
1418 : :
1419 : : #if defined (TARGET_EXE_STANDALONE) && defined(_WIN32)
1420 : : err_display = DisplayStructure(pCG,
1421 : : inp_cur_data->at,
1422 : : inp_cur_data->num_at,
1423 : : NULL, /* OAD_Polymer *polymer, */
1424 : : 0,
1425 : : 1,
1426 : : 0,
1427 : : NULL,
1428 : : 1 /*isotopic*/,
1429 : : 0 /*taut*/,
1430 : : NULL,
1431 : : NULL,
1432 : : ip->bAbcNumbers,
1433 : : &ip->dp,
1434 : : ip->nMode,
1435 : : szTitle);
1436 : :
1437 : : sd->bUserQuitComponentDisplay = (err_display == ESC_KEY);
1438 : :
1439 : : if ( !err_display )
1440 : : {
1441 : : inchi_fprintf(stderr, "Cannot display the structure\n");
1442 : : }
1443 : : #endif
1444 : : #ifdef TARGET_LIB_FOR_WINCHI
1445 : : if ( DRAWDATA && DRAWDATA_EXISTS )
1446 : : {
1447 : : struct DrawData vDrawData;
1448 : : int nType = COMPONENT_ORIGINAL;
1449 : : vDrawData.pWindowData = CreateWinData_(pCG,
1450 : : inp_cur_data->at,
1451 : : inp_cur_data->num_at,
1452 : : 0,
1453 : : 1 /* bAdd_DT_to_num_H */,
1454 : : 0,
1455 : : NULL,
1456 : : 1 /* display isotopic if present */,
1457 : : 0,
1458 : : NULL,
1459 : : NULL,
1460 : : ip->bAbcNumbers,
1461 : : &ip->dp,
1462 : : ip->nMode);
1463 : : if ( vDrawData.pWindowData != NULL )
1464 : : {
1465 : : if ( DRAWDATA_EXISTS(i + 1, nType, iINChI) )
1466 : : {
1467 : : /* i = component number */
1468 : : nType = COMPONENT_ORIGINAL_PREPROCESSED;
1469 : : }
1470 : : vDrawData.nComponent = i + 1;
1471 : : vDrawData.nType = nType;
1472 : : vDrawData.bReconnected = iINChI; /* 0=>main; 1=>reconnected */
1473 : : vDrawData.szTitle = inchi__strdup(szTitle);
1474 : : vDrawData.pWindowData->szTitle = inchi__strdup(szTitle);
1475 : : DRAWDATA(&vDrawData);
1476 : : }
1477 : : }
1478 : : #endif
1479 : : }
1480 : : /*#endif */ /* } COMPILE_ANSI_ONLY */
1481 : :
1482 : :
1483 : : /*******************************************************************************/
1484 : : /* */
1485 : : /* N O R M A L I Z A T I O N a n d C A N O N I C A L I Z A T I O N */
1486 : : /* */
1487 : : /* (both tautomeric and non-tautomeric if requested) */
1488 : : /* */
1489 : : /*******************************************************************************/
1490 : : /* c) Create the component's INChI ( copies ip->bTautFlags into sd->bTautFlags)*/
1491 : : /*******************************************************************************/
1492 : :
1493 : 1262 : nRet = CreateOneComponentINChI(pCG, ic, sd, ip,
1494 : : inp_cur_data, orig_inp_data,
1495 : : pINChI/*2[iINChI]*/,
1496 : : pINChI_Aux/*2[iINChI]*/,
1497 : : iINChI, i, num_inp,
1498 : : inp_norm_data, pncFlags, log_file);
1499 : :
1500 : :
1501 : :
1502 : : /* d) Display one component structure and/or INChI results only if there was no error */
1503 : :
1504 : : /* #ifndef COMPILE_ANSI_ONLY */ /* { */
1505 [ + + ]: 1262 : if ( !nRet )
1506 : : {
1507 : : /* output one component INChI to the stdout if requested */
1508 : : /*
1509 : : if ( ip->bDisplayEachComponentINChI ) {
1510 : : int cur_num_non_taut = (pINChI[i][TAUT_NON] && pINChI[i][TAUT_NON]->nNumberOfAtoms>0);
1511 : : int cur_num_taut = (pINChI[i][TAUT_YES] && pINChI[i][TAUT_YES]->nNumberOfAtoms>0);
1512 : : if ( ip->bDisplayEachComponentINChI && cur_num_non_taut + cur_num_taut ) {
1513 : : SortAndPrintINChI( pCG, stdout, pStr, nStrLen, NULL,
1514 : : ip, 1, cur_num_non_taut, cur_num_taut,
1515 : : num_inp, pINChI+i, pINChI_Aux+i,
1516 : : save_opt_bits);
1517 : : }
1518 : : }
1519 : : */
1520 : : /**************************************************************************
1521 : : * display from one up to 4 structure pictures-results for each component *
1522 : : * Enable buttons: *
1523 : : * BN (non-tautomeric non-isotopic): inp_norm_data[0]->bExists *
1524 : : * TN (tautomeric non-isotopic): inp_norm_data[1]->bExists *
1525 : : * BI (non-tautomeric isotopic): inp_norm_data[0]->bExists && *
1526 : : * inp_norm_data[0]->bHasIsotopicLayer *
1527 : : * TI (tautomeric isotopic): inp_norm_data[1]->bExists && *
1528 : : * inp_norm_data[1]->bHasIsotopicLayer *
1529 : : **************************************************************************/
1530 : :
1531 : 1261 : int bIsotopic, bTautomeric, bDisplayTaut, bHasIsotopicLayer, bFixedBondsTaut, m_max, m, nNumDisplayedFixedBondTaut = 0; /* djb-rwth: ignoring LLVM warning: variable used */
1532 : :
1533 : 1261 : for ( j = 0;
1534 [ - + - - : 1261 : ip->bDisplay && !sd->bUserQuitComponentDisplay && j < TAUT_NUM;
- - ]
1535 : 0 : j++ )
1536 : : {
1537 [ # # # # ]: 0 : if ( inp_norm_data[j]->bExists && !inp_norm_data[j]->bDeleted )
1538 : : {
1539 : 0 : bTautomeric = (pINChI[i][j]->lenTautomer > 0);
1540 : : /* same as (inp_norm_data[j]->bTautomeric > 0) */
1541 : :
1542 : : /* If requested tautomeric and no tautmerism found then do not say mobile or fixed H. 2004-10-27 */
1543 [ # # # # ]: 0 : bDisplayTaut = (!(ip->nMode & REQ_MODE_BASIC) && !bTautomeric) ? -1 : bTautomeric;
1544 : 0 : bHasIsotopicLayer = (inp_norm_data[j]->bHasIsotopicLayer > 0);
1545 : :
1546 [ # # ]: 0 : for ( k = 0; k <= bHasIsotopicLayer; k++ )
1547 : : {
1548 : 0 : bIsotopic = (k > 0);
1549 [ # # # # ]: 0 : m_max = inp_norm_data[j]->at_fixed_bonds && inp_norm_data[j]->bTautPreprocessed ? 1 : 0;
1550 [ # # ]: 0 : for ( m = m_max; 0 <= m; m-- )
1551 : : {
1552 : 0 : bFixedBondsTaut = (m > 0);
1553 : 0 : nNumDisplayedFixedBondTaut += bFixedBondsTaut;
1554 : : /* display only one time */
1555 : :
1556 : : /* Added number of components, added another format for a single component case - DCh */
1557 [ # # ]: 0 : if ( cur_prep_inp_data->num_components > 1 )
1558 : : {
1559 [ # # # # : 0 : sprintf(szTitle, "%s Component #%d of %d, Structure #%ld%s%s.%s%s%s%s%s",
# # # # ]
1560 : : bFixedBondsTaut ? "Preprocessed" : "Result for",
1561 : : i + 1, cur_prep_inp_data->num_components, num_inp,
1562 [ # # ]: 0 : bDisplayTaut == 1 ? ", mobile H" : bDisplayTaut == 0 ? ", fixed H" : "",
1563 : : bIsotopic ? ", isotopic" : "",
1564 [ # # # # : 0 : SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue), iINChI ? " (Reconnected)" : "");
# # # # #
# # # # #
# # # # #
# # # #
# ]
1565 : : }
1566 : : else
1567 : : {
1568 [ # # # # : 0 : sprintf(szTitle, "%s Structure #%ld%s%s.%s%s%s%s%s",
# # # # ]
1569 : : bFixedBondsTaut ? "Preprocessed" : "Result for",
1570 : : num_inp,
1571 [ # # ]: 0 : bDisplayTaut == 1 ? ", mobile H" : bDisplayTaut == 0 ? ", fixed H" : "",
1572 : : bIsotopic ? ", isotopic" : "",
1573 [ # # # # : 0 : SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue), iINChI ? " (Reconnected)" : "");
# # # # #
# # # # #
# # # # #
# # # #
# ]
1574 : : }
1575 : :
1576 : : #if defined (TARGET_EXE_STANDALONE) && defined(_WIN32)
1577 : : if ( bFixedBondsTaut && nNumDisplayedFixedBondTaut != 1 )
1578 : : {
1579 : : continue;
1580 : : }
1581 : : if ( ip->bDisplay )
1582 : : {
1583 : : if ( bFixedBondsTaut )
1584 : : {
1585 : : err_display = DisplayStructure(pCG,
1586 : : inp_norm_data[j]->at_fixed_bonds,
1587 : : inp_norm_data[j]->num_at,
1588 : : NULL, /* OAD_Polymer *polymer, */
1589 : : inp_norm_data[j]->num_removed_H,
1590 : : 0 /*bAdd_DT_to_num_H*/,
1591 : : inp_norm_data[j]->nNumRemovedProtons,
1592 : : inp_norm_data[j]->nNumRemovedProtonsIsotopic,
1593 : : bHasIsotopicLayer,
1594 : : j,
1595 : : NULL,
1596 : : NULL,
1597 : : ip->bAbcNumbers,
1598 : : &ip->dp,
1599 : : ip->nMode,
1600 : : szTitle);
1601 : : }
1602 : : else
1603 : : {
1604 : : err_display = DisplayStructure(pCG,
1605 : : inp_norm_data[j]->at,
1606 : : inp_norm_data[j]->num_at,
1607 : : NULL, /* OAD_Polymer *polymer, */
1608 : : 0,
1609 : : 0 /*bAdd_DT_to_num_H*/,
1610 : : 0,
1611 : : NULL,
1612 : : k,
1613 : : j,
1614 : : pINChI[i],
1615 : : pINChI_Aux[i],
1616 : : ip->bAbcNumbers,
1617 : : &ip->dp,
1618 : : ip->nMode, szTitle);
1619 : : }
1620 : : if ( (sd->bUserQuitComponentDisplay = (err_display == ESC_KEY)) ) /* djb-rwth: addressing LLVM warning */
1621 : : {
1622 : : break;
1623 : : }
1624 : : }
1625 : : #endif
1626 : : #ifdef TARGET_LIB_FOR_WINCHI
1627 : :
1628 : : if ( DRAWDATA && !bFixedBondsTaut )
1629 : : {
1630 : : struct DrawData vDrawData;
1631 : : vDrawData.pWindowData =
1632 : : CreateWinData_(pCG,
1633 : : inp_norm_data[j]->at,
1634 : : inp_norm_data[j]->num_at,
1635 : : 0,
1636 : : 0 /* bAdd_DT_to_num_H */,
1637 : : 0,
1638 : : NULL,
1639 : : k,
1640 : : j,
1641 : : pINChI[i],
1642 : : pINChI_Aux[i],
1643 : : ip->bAbcNumbers,
1644 : : &ip->dp,
1645 : : ip->nMode);
1646 : :
1647 : : if ( vDrawData.pWindowData != NULL )
1648 : : {
1649 : : int nType;
1650 : : vDrawData.nComponent = i + 1;
1651 : : if ( bTautomeric == 0 )
1652 : : nType = (bIsotopic == 0) ? COMPONENT_BN
1653 : : : COMPONENT_BI;
1654 : : else
1655 : : nType = (bIsotopic == 0) ? COMPONENT_TN
1656 : : : COMPONENT_TI;
1657 : : vDrawData.nType = nType;
1658 : :
1659 : : vDrawData.bReconnected = iINChI; /* 0=>main; 1=>reconnected */
1660 : : vDrawData.szTitle = inchi__strdup(szTitle);
1661 : : vDrawData.pWindowData->szTitle = inchi__strdup(szTitle);
1662 : : DRAWDATA(&vDrawData);
1663 : : }
1664 : : }
1665 : : else if ( DRAWDATA && bFixedBondsTaut )
1666 : : {
1667 : : struct DrawData vDrawData;
1668 : : if ( (ip->bCompareComponents & CMP_COMPONENTS) &&
1669 : : !(ip->bCompareComponents & CMP_COMPONENTS_NONTAUT) &&
1670 : : !bIsotopic == !inp_norm_data[j]->bHasIsotopicLayer )
1671 : : {
1672 : :
1673 : : vDrawData.pWindowData =
1674 : : CreateWinData_(pCG,
1675 : : inp_norm_data[j]->at_fixed_bonds,
1676 : : inp_norm_data[j]->num_at,
1677 : : inp_norm_data[j]->num_removed_H,
1678 : : 0 /* bAdd_DT_to_num_H */,
1679 : : inp_norm_data[j]->nNumRemovedProtons,
1680 : : inp_norm_data[j]->nNumRemovedProtonsIsotopic,
1681 : : k,
1682 : : j,
1683 : : NULL,
1684 : : NULL,
1685 : : ip->bAbcNumbers,
1686 : : &ip->dp,
1687 : : ip->nMode);
1688 : : }
1689 : : else
1690 : : {
1691 : : continue;
1692 : : }
1693 : : if ( vDrawData.pWindowData != NULL )
1694 : : {
1695 : : vDrawData.nComponent = i + 1;
1696 : : vDrawData.nType = COMPONENT_ORIGINAL_PREPROCESSED;
1697 : : vDrawData.bReconnected = iINChI; /* 0=>main; 1=>reconnected */
1698 : : vDrawData.szTitle = inchi__strdup(szTitle);
1699 : : vDrawData.pWindowData->szTitle = inchi__strdup(szTitle);
1700 : : DRAWDATA(&vDrawData);
1701 : : }
1702 : : }
1703 : : #endif
1704 : : }
1705 : : }
1706 : : }
1707 : : }
1708 : :
1709 : : /* Save normalized components for composite display */
1710 [ + + ]: 1261 : if ( /*ip->bDisplayCompositeResults && */
1711 : : all_inp_norm_data
1712 : : )
1713 : : {
1714 [ + + ]: 3348 : for ( j = 0; j < TAUT_NUM; j++ )
1715 : : {
1716 [ + + ]: 2232 : if ( inp_norm_data[j]->bExists )
1717 : : {
1718 : 1116 : all_inp_norm_data[i][j] = *inp_norm_data[j];
1719 : 1116 : memset(inp_norm_data[j], 0, sizeof(*inp_norm_data[0])); /* djb-rwth: memset_s C11/Annex K variant? */
1720 : : }
1721 : : }
1722 : : }
1723 : : }
1724 : :
1725 : : /* #endif */ /* } COMPILE_ANSI_ONLY */
1726 : :
1727 : :
1728 [ + + ]: 1262 : if ( nRet )
1729 : : {
1730 : 1 : nRet = TreatErrorsInCreateOneComponentINChI(sd, ip,
1731 : : cur_prep_inp_data,
1732 : : i, num_inp, inp_file,
1733 : : log_file, out_file, prb_file);
1734 : 1 : break;
1735 : : }
1736 : : }
1737 : : /**************************************************************************/
1738 : : /* */
1739 : : /* */
1740 : : /* E N D O F T H E M A I N C Y C L E P R O C E S S I N G */
1741 : : /* */
1742 : : /* C O M P O N E N T S O N E B Y O N E */
1743 : : /* */
1744 : : /* */
1745 : : /**************************************************************************/
1746 : :
1747 : 185 : exit_cycle:
1748 [ + + ]: 186 : switch ( nRet )
1749 : : {
1750 : 1 : case _IS_FATAL:
1751 : : case _IS_ERROR:
1752 : 1 : break;
1753 : 185 : default:
1754 : :
1755 : : /* #ifndef COMPILE_ANSI_ONLY *//* { */
1756 : : /* composite results picture(s) */
1757 [ + + ]: 185 : if ( all_inp_norm_data )
1758 : : {
1759 : 40 : CreateCompositeNormAtom(composite_norm_data,
1760 : : all_inp_norm_data,
1761 : 40 : prep_inp_data[iINChI].num_components);
1762 : : /*
1763 : : for ( i = 0; i < prep_inp_data[iINChI].num_components; i ++ ) {
1764 : : for ( k = 0; k < TAUT_NUM; k ++ ) {
1765 : : FreeInpAtomData( &all_inp_norm_data[i][k] );
1766 : : }
1767 : : }
1768 : : inchi_free( all_inp_norm_data );
1769 : : all_inp_norm_data = NULL;
1770 : : */
1771 : : }
1772 : : /* #endif */ /* } COMPILE_ANSI_ONLY */
1773 : :
1774 : 185 : break;
1775 : : }
1776 : :
1777 : : /*#ifndef COMPILE_ANSI_ONLY*/ /* { */
1778 : :
1779 : : /* avoid memory leaks in case of error */
1780 [ + + ]: 186 : if ( all_inp_norm_data )
1781 : : {
1782 [ + + ]: 1156 : for ( i = 0; i < prep_inp_data[iINChI].num_components; i++ )
1783 : : {
1784 [ + + ]: 3348 : for ( k = 0; k < TAUT_NUM; k++ )
1785 : : {
1786 : 2232 : FreeInpAtomData(&all_inp_norm_data[i][k]);
1787 : : }
1788 : : }
1789 [ + - ]: 40 : inchi_free(all_inp_norm_data);
1790 : 40 : all_inp_norm_data = NULL;
1791 : : }
1792 : : /*#endif */ /* } COMPILE_ANSI_ONLY */
1793 : :
1794 : 186 : FreeInpAtomData(inp_cur_data);
1795 [ + + ]: 558 : for ( i = 0; i < TAUT_NUM; i++ )
1796 : : {
1797 : 372 : FreeInpAtomData(inp_norm_data[i]);
1798 : : }
1799 : :
1800 : :
1801 : 186 : exit_function:
1802 : :
1803 : 186 : return nRet;
1804 : : }
1805 : :
1806 : :
1807 : : /****************************************************************************
1808 : : Generate InChI for one connected component
1809 : : (of possibly multi-component structure)
1810 : : ****************************************************************************/
1811 : 1262 : int CreateOneComponentINChI(CANON_GLOBALS* pCG,
1812 : : INCHI_CLOCK* ic,
1813 : : STRUCT_DATA* sd,
1814 : : INPUT_PARMS* ip,
1815 : : INP_ATOM_DATA* inp_cur_data,
1816 : : ORIG_ATOM_DATA* orig_inp_data,
1817 : : PINChI2* pINChI,
1818 : : PINChI_Aux2* pINChI_Aux,
1819 : : int iINChI,
1820 : : int i,
1821 : : long num_inp,
1822 : : INP_ATOM_DATA** inp_norm_data,
1823 : : NORM_CANON_FLAGS* pncFlags,
1824 : : INCHI_IOSTREAM* log_file)
1825 : : {
1826 : 1262 : inchiTime ulTStart, ulTEnd, * pulTEnd = NULL;
1827 : 1262 : int k, num_at, ret = 0;
1828 : : int bOrigCoord;
1829 : 1262 : INCHI_MODE bTautFlags = ip->bTautFlags;
1830 : 1262 : INCHI_MODE bTautFlagsDone = (ip->bTautFlagsDone | sd->bTautFlagsDone[INCHI_BAS]);
1831 : : INChI* cur_INChI[TAUT_NUM];
1832 : : INChI_Aux* cur_INChI_Aux[TAUT_NUM];
1833 : : long lElapsedTime;
1834 : :
1835 : 1262 : int nAllocMode = 0; /* moved from below 2024-09-01 DT */
1836 : :
1837 : 1262 : InchiTimeGet( &ulTStart );
1838 : :
1839 : 1262 : bOrigCoord =
1840 : 1262 : !(ip->bINChIOutputOptions & (INCHI_OUT_NO_AUX_INFO | INCHI_OUT_SHORT_AUX_INFO));
1841 : :
1842 [ + + ]: 3786 : for ( k = 0; k < TAUT_NUM; k++ )
1843 : : {
1844 : 2524 : cur_INChI[k] = NULL;
1845 : 2524 : cur_INChI_Aux[k] = NULL;
1846 : : }
1847 : :
1848 : : /* Allocate memory for non-tautomeric (k=0) and tautomeric (k=1) results */
1849 [ + + ]: 3786 : for ( k = 0; k < TAUT_NUM; k++ )
1850 : : {
1851 : : /* djb-rwth: introducing variables for correct nAllocMode expression */
1852 : 2524 : int nAM1 = 0, nAM2 = 0;
1853 : :
1854 [ + + ]: 2524 : if ( k == TAUT_YES )
1855 : 1262 : nAM1 = REQ_MODE_TAUT;
1856 : :
1857 [ + + ]: 2524 : if ( bTautFlagsDone & (TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE) )
1858 : 18 : nAM2 = ip->nMode & REQ_MODE_ISO;
1859 : :
1860 : 2524 : nAllocMode = nAM1 | nAM2; /* djb-rwth: original sequence of bit-wise operations had to be rewritten */
1861 : :
1862 : :
1863 [ + + + - : 2524 : if ( (k == TAUT_NON && (ip->nMode & REQ_MODE_BASIC)) ||
+ + ]
1864 [ + - ]: 1262 : (k == TAUT_YES && (ip->nMode & REQ_MODE_TAUT)) ) /* djb-rwth: addressing LLVM warning */
1865 : : {
1866 : : /* alloc INChI and INChI_Aux */
1867 : 1262 : cur_INChI[k] = Alloc_INChI(inp_cur_data->at,
1868 : : inp_cur_data->num_at,
1869 : : &inp_cur_data->num_bonds,
1870 : : &inp_cur_data->num_isotopic,
1871 : : nAllocMode);
1872 : :
1873 : 1262 : cur_INChI_Aux[k] = Alloc_INChI_Aux(inp_cur_data->num_at,
1874 : : inp_cur_data->num_isotopic,
1875 : : nAllocMode,
1876 : : bOrigCoord);
1877 [ + - ]: 1262 : if ( cur_INChI_Aux[k] )
1878 : : {
1879 : 1262 : cur_INChI_Aux[k]->bIsIsotopic = inp_cur_data->num_isotopic;
1880 : : }
1881 : : /* alloc memory for the output structure: non-tautomeric and tautomeric (for displaying) */
1882 : :
1883 : 1262 : CreateInpAtomData(inp_norm_data[k], inp_cur_data->num_at, k);
1884 : : }
1885 : : else
1886 : : {
1887 : 1262 : FreeInpAtomData(inp_norm_data[k]);
1888 : : }
1889 : : }
1890 : :
1891 : 1262 : lElapsedTime = InchiTimeElapsed(ic, &ulTStart);
1892 [ - + ]: 1262 : if ( ip->msec_MaxTime )
1893 : : {
1894 : 0 : ip->msec_LeftTime -= lElapsedTime;
1895 : : }
1896 : 1262 : sd->ulStructTime += lElapsedTime;
1897 : :
1898 : : /*^^^#if ( !defined( TARGET_LIB_FOR_WINCHI ) && !defined( TARGET_API_LIB ) ) */
1899 : : #if ( !defined( TARGET_LIB_FOR_WINCHI ) && !defined( TARGET_API_LIB ) && !defined(TARGET_EXE_STANDALONE) )
1900 : : #endif
1901 : :
1902 : : /******************************************************
1903 : : *
1904 : : * Get one component canonical numberings, etc.
1905 : : *
1906 : : ******************************************************/
1907 : :
1908 : : /* Create_INChI() return value:
1909 : : * num_at <= 0: error code
1910 : : * num_at > 0: number of atoms (excluding terminal hydrogen atoms)
1911 : : * inp_norm_data[0] => non-tautomeric, inp_norm_data[1] => tautomeric */
1912 : :
1913 : 1262 : InchiTimeGet(&ulTStart);
1914 : :
1915 [ - + ]: 1262 : if ( ip->msec_MaxTime )
1916 : : {
1917 : 0 : ulTEnd = ulTStart;
1918 : 0 : pulTEnd = &ulTEnd;
1919 [ # # ]: 0 : if ( ip->msec_LeftTime > 0 )
1920 : : {
1921 : 0 : InchiTimeAddMsec(ic, pulTEnd, ip->msec_LeftTime);
1922 : : }
1923 : : }
1924 : :
1925 : 1262 : num_at = Create_INChI(pCG, ic, ip,
1926 : : cur_INChI, cur_INChI_Aux,
1927 : : orig_inp_data/* not used */,
1928 : : inp_cur_data->at, inp_norm_data, inp_cur_data->num_at,
1929 : 1262 : inp_cur_data->keep_explicit_HDT,
1930 : : ip->nMode,
1931 : : &bTautFlags, &bTautFlagsDone,
1932 : 1262 : pulTEnd, NULL, sd->pStrErrStruct);
1933 : :
1934 : 1262 : SetConnectedComponentNumber(inp_cur_data->at, inp_cur_data->num_at, i + 1);
1935 : : /* NB: normalization alters structure component number */
1936 : :
1937 [ + + ]: 3786 : for ( k = 0; k < TAUT_NUM; k++ )
1938 : : {
1939 [ + + + - ]: 2524 : if ( cur_INChI_Aux[k] && cur_INChI_Aux[k]->nNumberOfAtoms > 0 )
1940 : : {
1941 : 1262 : pncFlags->bNormalizationFlags[iINChI][k] |=
1942 : 1262 : cur_INChI_Aux[k]->bNormalizationFlags;
1943 : 1262 : pncFlags->bTautFlags[iINChI][k] |=
1944 : 1262 : cur_INChI_Aux[k]->bTautFlags;
1945 : 1262 : pncFlags->bTautFlagsDone[iINChI][k] |=
1946 : 1262 : cur_INChI_Aux[k]->bTautFlagsDone;
1947 : 1262 : pncFlags->nCanonFlags[iINChI][k] |=
1948 : 1262 : cur_INChI_Aux[k]->nCanonFlags;
1949 : : }
1950 : : }
1951 : :
1952 : : /* Detect errors */
1953 [ + + ]: 1262 : if ( num_at < 0 )
1954 : : {
1955 : 1 : sd->nErrorCode = num_at;
1956 : : }
1957 [ - + ]: 1261 : else if ( num_at == 0 )
1958 : : {
1959 : 0 : sd->nErrorCode = -1;
1960 : : }
1961 [ - + - - ]: 1261 : else if ( cur_INChI[TAUT_NON] && cur_INChI[TAUT_NON]->nErrorCode )
1962 : : { /* non-tautomeric error */
1963 : 0 : sd->nErrorCode = cur_INChI[TAUT_NON]->nErrorCode;
1964 : : }
1965 [ + - - + ]: 1261 : else if ( cur_INChI[TAUT_YES] && cur_INChI[TAUT_YES]->nErrorCode )
1966 : : { /* tautomeric error */
1967 : 0 : sd->nErrorCode = cur_INChI[TAUT_YES]->nErrorCode;
1968 : : }
1969 : :
1970 : :
1971 : : #if ( bRELEASE_VERSION == 0 )
1972 : : if ( cur_INChI[TAUT_NON] ) sd->bExtract |= cur_INChI[TAUT_NON]->bExtract;
1973 : : if ( cur_INChI[TAUT_YES] ) sd->bExtract |= cur_INChI[TAUT_YES]->bExtract;
1974 : : if ( (TG_FLAG_TEST_TAUT3_SALTS_DONE & bTautFlagsDone) )
1975 : : {
1976 : : sd->bExtract |= EXTR_TEST_TAUT3_SALTS_DONE;
1977 : : }
1978 : : #endif
1979 : :
1980 : : /* Detect and store stereo warnings */
1981 [ + + ]: 1262 : if ( !sd->nErrorCode )
1982 : 1261 : GetProcessingWarningsOneComponentInChI(cur_INChI, inp_norm_data, sd, ip->bNoWarnings);
1983 : :
1984 : 1262 : lElapsedTime = InchiTimeElapsed(ic, &ulTStart);
1985 [ - + ]: 1262 : if ( ip->msec_MaxTime )
1986 : : {
1987 : 0 : ip->msec_LeftTime -= lElapsedTime;
1988 : : }
1989 : 1262 : sd->ulStructTime += lElapsedTime;
1990 : :
1991 : : #if !defined(TARGET_API_LIB) && !defined(COMPILE_ANSI_ONLY)
1992 : : /* Display the results */
1993 : : if ( ip->bDisplay )
1994 : : {
1995 : : eat_keyboard_input();
1996 : : }
1997 : : #endif
1998 : :
1999 : : /* a) No matter what happened save the allocated INChI pointers */
2000 : : /* save the INChI of the current component */
2001 : :
2002 : 1262 : InchiTimeGet(&ulTStart);
2003 [ + + ]: 3786 : for ( k = 0; k < TAUT_NUM; k++ )
2004 : : {
2005 : 2524 : pINChI[i][k] = cur_INChI[k];
2006 : 2524 : pINChI_Aux[i][k] = cur_INChI_Aux[k];
2007 : 2524 : cur_INChI[k] = NULL;
2008 : 2524 : cur_INChI_Aux[k] = NULL;
2009 : : }
2010 : :
2011 : : /* b) Count one component structure and/or INChI results only
2012 : : if there was no error
2013 : : Set inp_norm_data[j]->num_removed_H = number of removed explicit H
2014 : : */
2015 : :
2016 [ + + ]: 1262 : if ( !sd->nErrorCode )
2017 : : {
2018 : : /* find where the current processed structure is located */
2019 [ - + ]: 1261 : int cur_is_in_non_taut = (pINChI[i][TAUT_NON] &&
2020 [ # # ]: 0 : pINChI[i][TAUT_NON]->nNumberOfAtoms > 0);
2021 [ + - ]: 2522 : int cur_is_in_taut = (pINChI[i][TAUT_YES] &&
2022 [ + - ]: 1261 : pINChI[i][TAUT_YES]->nNumberOfAtoms > 0);
2023 : :
2024 [ - + - - : 2522 : int cur_is_non_taut = (cur_is_in_non_taut && 0 == pINChI[i][TAUT_NON]->lenTautomer) ||
+ - ]
2025 [ + + ]: 1261 : (cur_is_in_taut && 0 == pINChI[i][TAUT_YES]->lenTautomer); /* djb-rwth: addressing LLVM warnings */
2026 [ + - + + ]: 1261 : int cur_is_taut = cur_is_in_taut && 0 < pINChI[i][TAUT_YES]->lenTautomer;
2027 : :
2028 : : /*
2029 : : sd->bTautFlags[iINChI] |= bTautFlags;
2030 : : sd->bTautFlagsDone[iINChI] |= bTautFlagsDone;
2031 : : */
2032 : :
2033 [ + - ]: 1261 : if ( cur_is_non_taut + cur_is_taut )
2034 : : {
2035 : : /* count tautomeric and non-tautomeric components of the structures */
2036 : 1261 : int j1 = cur_is_in_non_taut ? TAUT_NON : TAUT_YES;
2037 : 1261 : int j2 = cur_is_in_taut ? TAUT_YES : TAUT_NON;
2038 : : int j;
2039 : 1261 : sd->num_non_taut[iINChI] += cur_is_non_taut;
2040 : 1261 : sd->num_taut[iINChI] += cur_is_taut;
2041 [ + + ]: 2522 : for ( j = j1; j <= j2; j++ )
2042 : : {
2043 : 3775 : int bIsotopic = (pINChI[i][j]->nNumberOfIsotopicAtoms ||
2044 [ + + + - ]: 2514 : pINChI[i][j]->nNumberOfIsotopicTGroups ||
2045 [ + + - + ]: 1253 : (pINChI[i][j]->nPossibleLocationsOfIsotopicH && pINChI[i][j]->nPossibleLocationsOfIsotopicH[0] > 1)); /* djb-rwth: addressing LLVM warning */
2046 [ + - + - ]: 1261 : if ( pINChI_Aux[i][j] && (j == TAUT_YES) ) /* djb-rwth: fixing a NULL pointer dereference */
2047 : : {
2048 : 1261 : bIsotopic |= (0 < pINChI_Aux[i][j]->nNumRemovedIsotopicH[0] +
2049 : 1261 : pINChI_Aux[i][j]->nNumRemovedIsotopicH[1] +
2050 : 1261 : pINChI_Aux[i][j]->nNumRemovedIsotopicH[2]);
2051 : : }
2052 : :
2053 : 1261 : inp_norm_data[j]->bExists = 1; /* j=0: non-taut exists, j=1: taut exists */
2054 : 1261 : inp_norm_data[j]->bHasIsotopicLayer = bIsotopic;
2055 : : /*inp_norm_data[j]->num_removed_H = inp_norm_data[j]->num_at - num_at;*/
2056 : : }
2057 : : }
2058 : : }
2059 : :
2060 : : /* return (sd->nErrorCode==CT_OUT_OF_RAM || sd->nErrorCode==CT_USER_QUIT_ERR)? _IS_FATAL :
2061 : : sd->nErrorCode? _IS_ERROR : 0; */
2062 : :
2063 [ + - - + ]: 1262 : if ( sd->nErrorCode == CT_OUT_OF_RAM || sd->nErrorCode == CT_USER_QUIT_ERR )
2064 : : {
2065 : 0 : ret = _IS_FATAL;
2066 : : }
2067 [ + + ]: 1262 : else if ( sd->nErrorCode )
2068 : : {
2069 : 1 : ret = _IS_ERROR;
2070 : : }
2071 : :
2072 : 1262 : lElapsedTime = InchiTimeElapsed(ic, &ulTStart);
2073 [ - + ]: 1262 : if ( ip->msec_MaxTime )
2074 : : {
2075 : 0 : ip->msec_LeftTime -= lElapsedTime;
2076 : : }
2077 : 1262 : sd->ulStructTime += lElapsedTime;
2078 : :
2079 : 1262 : return ret;
2080 : : }
2081 : :
2082 : :
2083 : : /****************************************************************************
2084 : : Extended-functionality version of ProcessOneStructure
2085 : :
2086 : : able to handle both polymer related and unrelated pseudoelement atoms
2087 : : and to perform advanced polymer treatment (v. 1.06+)
2088 : : ****************************************************************************/
2089 : 170 : int ProcessOneStructureEx(struct tagINCHI_CLOCK* ic,
2090 : : struct tagCANON_GLOBALS* CG,
2091 : : STRUCT_DATA* sd,
2092 : : INPUT_PARMS* ip,
2093 : : char* szTitle,
2094 : : PINChI2* pINChI2[INCHI_NUM],
2095 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
2096 : : INCHI_IOSTREAM* inp_file,
2097 : : INCHI_IOSTREAM* log_file,
2098 : : INCHI_IOSTREAM* out_file,
2099 : : INCHI_IOSTREAM* prb_file,
2100 : : ORIG_ATOM_DATA* orig_inp_data,
2101 : : ORIG_ATOM_DATA* prep_inp_data,
2102 : : long num_inp,
2103 : : INCHI_IOS_STRING* strbuf,
2104 : : unsigned char save_opt_bits)
2105 : : {
2106 : 170 : int ret = _IS_OKAY;
2107 : 170 : char *sinchi_noedits=NULL, *saux_noedits=NULL;
2108 : :
2109 : :
2110 : : /* PREPROCESS */
2111 : :
2112 : : #if (BUILD_WITH_ENG_OPTIONS==1)
2113 : : #if ALLOW_SUBSTRUCTURE_FILTERING==1
2114 : : if ( ip->bFilterSS )
2115 : : {
2116 : : int present, ok = 0;
2117 : :
2118 : : present = OrigAtData_CheckForSubstructure(orig_inp_data);
2119 : :
2120 : : if ( ip->bFilterSS == 1 && present ) ok = 1;
2121 : : else if ( ip->bFilterSS == -1 && !present ) ok = 1;
2122 : :
2123 : : if ( !ok )
2124 : : {
2125 : : inchi_ios_eprint(log_file, "Warning (Skip record which does not pass substructure presence/absence filter) structure #%ld.%s%s%s%s\n",
2126 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
2127 : : return _IS_SKIP;
2128 : : }
2129 : : }
2130 : : #endif
2131 : : #endif
2132 : :
2133 : : /* Preprocess Polymer CRUs (collect frame shift info and edit the original input accordingly) */
2134 : 170 : ret = PreprocessPolymerCRUData(ic, CG, sd, ip, szTitle,
2135 : : pINChI2, pINChI_Aux2,
2136 : : inp_file, log_file, out_file, prb_file,
2137 : : orig_inp_data, prep_inp_data,
2138 : : num_inp, strbuf, save_opt_bits,
2139 : : &sinchi_noedits, &saux_noedits); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2140 : :
2141 : : /* CALCULATE INCHI */
2142 : :
2143 : : /* Perform calculation as usual either for untouched (modes POLYMERS_LEGACY and POLYMERS_LEGACY_PLUS)
2144 : : or just edited, probably (mode POLYMERS_MODERN) structure as passed in orig_inp_data
2145 : : */
2146 : 170 : ret = ProcessOneStructureExCore( ic, CG, sd, ip, szTitle,
2147 : : pINChI2, pINChI_Aux2,
2148 : : inp_file, log_file, out_file, prb_file,
2149 : : orig_inp_data, prep_inp_data,
2150 : : num_inp, strbuf, save_opt_bits );
2151 : :
2152 [ - + ]: 170 : if ( ip->bINChIOutputOptions2 & INCHI_OUT_INCHI_GEN_ERROR )
2153 : : {
2154 [ # # # # ]: 0 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2155 : : {
2156 [ # # ]: 0 : if (ip->bINChIOutputOptions & INCHI_OUT_STDINCHI)
2157 : : {
2158 : 0 : inchi_ios_eprint(out_file, "InChI=1S//\n");
2159 : : }
2160 : : else
2161 : : {
2162 : 0 : inchi_ios_eprint(out_file, "InChI=1N//\n");
2163 : : }
2164 : : }
2165 : : }
2166 : :
2167 : : /* Post-process: add AuxInfo for a very unedited original structure */
2168 [ + - + + ]: 170 : if ( ret != _IS_FATAL && ret != _IS_ERROR )
2169 : : {
2170 [ + - ]: 167 : if ( !(ip->bINChIOutputOptions & INCHI_OUT_NO_AUX_INFO) &&
2171 [ + - + - : 167 : out_file->s.pStr && strstr(out_file->s.pStr, "AuxInfo=") &&
+ + ]
2172 [ + - ]: 9 : saux_noedits && strstr(saux_noedits, "AuxInfo=") )
2173 : : {
2174 : 9 : char* pp = out_file->s.pStr;
2175 [ + - ]: 9 : if ( saux_noedits[8] )
2176 : : {
2177 : 9 : remove_one_lf(pp);
2178 : 9 : out_file->s.nUsedLength = strlen(out_file->s.pStr);
2179 : 9 : inchi_ios_eprint(out_file, "/U/%-s\n", saux_noedits + 8);
2180 : : }
2181 : : }
2182 : : }
2183 : :
2184 [ + + ]: 170 : inchi_free(sinchi_noedits);
2185 [ + + ]: 170 : inchi_free(saux_noedits);
2186 : :
2187 : :
2188 : : #ifdef TARGET_LIB_FOR_WINCHI
2189 : :
2190 : : push_to_winchi_text_window(out_file);
2191 : :
2192 : : if ( sd->pStrErrStruct && FWPUSH )
2193 : : {
2194 : : FWPUSH(sd->pStrErrStruct);
2195 : : }
2196 : : inchi_ios_flush(out_file);
2197 : : #endif
2198 : :
2199 : 170 : return ret;
2200 : : }
2201 : :
2202 : :
2203 : : /****************************************************************************
2204 : : Special treatment for polymers: perform CRU frame shift analysis
2205 : : and make related edits in orig_inp_data whenever applicable
2206 : : ****************************************************************************/
2207 : 170 : int PreprocessPolymerCRUData(struct tagINCHI_CLOCK* ic,
2208 : : struct tagCANON_GLOBALS* CG,
2209 : : STRUCT_DATA* sd,
2210 : : INPUT_PARMS* ip,
2211 : : char* szTitle,
2212 : : PINChI2* pINChI2[INCHI_NUM],
2213 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
2214 : : INCHI_IOSTREAM* inp_file,
2215 : : INCHI_IOSTREAM* log_file,
2216 : : INCHI_IOSTREAM* out_file,
2217 : : INCHI_IOSTREAM* prb_file,
2218 : : ORIG_ATOM_DATA* orig_inp_data,
2219 : : ORIG_ATOM_DATA* prep_inp_data,
2220 : : long num_inp,
2221 : : INCHI_IOS_STRING* strbuf,
2222 : : unsigned char save_opt_bits,
2223 : : char** sinchi_noedits,
2224 : : char** saux_noedits)
2225 : : {
2226 : 170 : int ret = _IS_OKAY;
2227 : 170 : char *sinchi_105p = NULL, *saux_105p = NULL;
2228 : 170 : OAD_StructureEdits edits_unit_frame_shift, *ed_fs = &edits_unit_frame_shift;
2229 : 170 : OAD_StructureEdits edits_unit_folding, *ed_fold = &edits_unit_folding;
2230 : :
2231 : 170 : OAD_StructureEdits_Init(ed_fold);
2232 : 170 : OAD_StructureEdits_Init(ed_fs);
2233 : :
2234 [ - + ]: 170 : if ( orig_inp_data )
2235 : : {
2236 : 170 : orig_inp_data->valid_polymer = 0;
2237 [ + + ]: 170 : if ( orig_inp_data->polymer )
2238 : : {
2239 : 9 : orig_inp_data->polymer->treat = ip->bPolymers;
2240 [ + - ]: 9 : if ( orig_inp_data->polymer->treat != POLYMERS_NO )
2241 : : {
2242 : 9 : orig_inp_data->valid_polymer = 1;
2243 : :
2244 [ + - ]: 9 : if ( orig_inp_data->polymer->treat == POLYMERS_MODERN )
2245 : : {
2246 : 9 : int n_done, n_todo = 0, n_poly_zz = 0; /* djb-rwth: ignoring LLVM warning: variable used */
2247 : :
2248 : : /* First, get InChI and AuxInfo for the unedited original input
2249 : : (actually, we are interested in AuxInfo for original structure;
2250 : : we then append it to the final result AuxInfo, in order to
2251 : : preserve total reversibility (restoring the original structure ) */
2252 : 9 : ret = OAD_ProcessOneStructureNoEdits(ic, CG, sd, ip, szTitle,
2253 : : pINChI2, pINChI_Aux2,
2254 : : inp_file, log_file, out_file, prb_file,
2255 : : orig_inp_data, prep_inp_data,
2256 : : num_inp, strbuf, save_opt_bits,
2257 : : &n_poly_zz,
2258 : : sinchi_noedits, saux_noedits);
2259 [ + - - + ]: 9 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2260 : : {
2261 : 0 : ret = _IS_WARNING;
2262 [ # # ]: 0 : if (!ip->bNoWarnings)
2263 : : {
2264 : 0 : AddErrorMessage(sd->pStrErrStruct, "CRU folding and frame shift analysis failed");
2265 : : }
2266 : 3 : goto exit_function;
2267 : : }
2268 [ + + ]: 9 : if ( n_poly_zz < 2 )
2269 : : {
2270 : : /* For now, CRU folding and frame shift analysis are only applicable to */
2271 : : /* CRU having both caps of indefinite nature, Zz */
2272 : 3 : goto exit_function;
2273 : : }
2274 : :
2275 : :
2276 : : /* Prepare and perform CRU folding related edits */
2277 [ + - ]: 6 : if (ip->bFoldPolymerSRU != 0)
2278 : : {
2279 : : /* Get interim 105+ flavour of InChI and AuxInfo and prepare */
2280 : 0 : int old_bFrameShiftScheme = ip->bFrameShiftScheme;
2281 : 0 : ip->bFrameShiftScheme = FSS_STARS_CYCLED;
2282 : 0 : ret = OAD_ProcessOneStructure105Plus(ic, CG, sd, ip, szTitle,
2283 : : pINChI2, pINChI_Aux2,
2284 : : inp_file, log_file, out_file, prb_file,
2285 : : orig_inp_data, prep_inp_data,
2286 : : num_inp, strbuf, save_opt_bits,
2287 : : &sinchi_105p, &saux_105p);
2288 : 0 : ip->bFrameShiftScheme = old_bFrameShiftScheme;
2289 [ # # # # ]: 0 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2290 : : {
2291 : 0 : ret = _IS_WARNING;
2292 : 0 : if ( !ip->bNoWarnings )
2293 : : {
2294 : : /* AddErrorMessage(sd->pStrErrStruct, "CRU fold analysis failed");*/
2295 : : ;
2296 : : }
2297 : 0 : goto frame_shift;
2298 : : }
2299 : :
2300 : 0 : ret = OAD_Polymer_PrepareFoldCRUEdits(orig_inp_data, *sinchi_noedits, *saux_noedits, sinchi_105p, saux_105p, ed_fold);
2301 [ # # # # ]: 0 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2302 : : {
2303 : 0 : if ( !ip->bNoWarnings )
2304 : : {
2305 : : /*AddErrorMessage(sd->pStrErrStruct, "CRU fold analysis failed");*/
2306 : : ;
2307 : : }
2308 : 0 : goto frame_shift;
2309 : : }
2310 : 0 : if ( ret == _IS_WARNING )
2311 : : {
2312 : : /* inchi_ios_eprint(log_file, "Warning (CRU fold analysis failed) structure #%ld.%s%s%s%s\n",
2313 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));*/
2314 : : ;
2315 : : }
2316 : : /* else */
2317 : : {
2318 : : /* Proceed with CRU fold */
2319 : 0 : n_done = 0; /* djb-rwth: ignoring LLVM warning: variable used */
2320 : 0 : n_todo = ed_fold->del_atom->used + ed_fold->del_bond->used + ed_fold->new_bond->used + ed_fold->mod_bond->used;
2321 : 0 : ed_fold->del_side_chains = 1;
2322 : 0 : OAD_StructureEdits_DebugPrint(ed_fold);
2323 [ # # ]: 0 : if ( n_todo )
2324 : : {
2325 : : /* Edit the original input data */
2326 : 0 : ed_fold->del_side_chains = 1;
2327 : 0 : n_done = OAD_StructureEdits_Apply(sd, ip, orig_inp_data, ed_fold, &ret); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2328 [ # # # # ]: 0 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2329 : : {
2330 : 0 : ret = _IS_WARNING;
2331 : : /*inchi_ios_eprint(log_file, "Warning (CRU fold failed) structure #%ld.%s%s%s%s\n",
2332 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
2333 : : */
2334 [ # # ]: 0 : if ( !ip->bNoWarnings )
2335 : : {
2336 : 0 : AddErrorMessage(sd->pStrErrStruct, "CRU folding failed");
2337 : : }
2338 : : }
2339 : : else
2340 : : {
2341 [ # # ]: 0 : if ( !ip->bNoWarnings )
2342 : : {
2343 : 0 : WarningMessage(sd->pStrErrStruct, "Atom(s) removed due to CRU folding");
2344 : : }
2345 : : }
2346 : : }
2347 : : }
2348 : : }
2349 : :
2350 : 6 : frame_shift:;
2351 : : /* Prepare and perform frame shift related edits */
2352 [ + - ]: 6 : if (ip->bFrameShiftScheme != FSS_NONE)
2353 : : {
2354 : : /* Clear buffers */
2355 [ - + ]: 6 : if ( sinchi_105p )
2356 : : {
2357 [ # # ]: 0 : inchi_free(sinchi_105p);
2358 : : }
2359 [ - + ]: 6 : if ( saux_105p )
2360 : : {
2361 [ # # ]: 0 : inchi_free(saux_105p);
2362 : : }
2363 : : /* Get interim 105+ flavour of InChI and AuxInfo (possibly 2nd time) */
2364 : 6 : ret = OAD_ProcessOneStructure105Plus(ic, CG, sd, ip, szTitle,
2365 : : pINChI2, pINChI_Aux2,
2366 : : inp_file, log_file, out_file, prb_file,
2367 : : orig_inp_data, prep_inp_data,
2368 : : num_inp, strbuf, save_opt_bits,
2369 : : &sinchi_105p, &saux_105p);
2370 [ + - - + ]: 6 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2371 : : {
2372 : 0 : ret = _IS_WARNING;
2373 : : /*inchi_ios_eprint(log_file, "Warning (Frame shift analysis failed) structure #%ld.%s%s%s%s\n",
2374 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));*/
2375 [ # # ]: 0 : if ( !ip->bNoWarnings )
2376 : : {
2377 : 0 : AddErrorMessage(sd->pStrErrStruct, "Frame shift analysis failed");
2378 : : }
2379 : 0 : goto exit_function;
2380 : : }
2381 : :
2382 : 6 : ret = OAD_Polymer_PrepareFrameShiftEdits( orig_inp_data, sinchi_105p, saux_105p, ed_fs);
2383 : :
2384 [ + - - + ]: 6 : if (ret == _IS_FATAL || ret == _IS_ERROR) /* djb-rwth: logical operator corrected */
2385 : : {
2386 : 0 : ret = _IS_WARNING;
2387 : : /*inchi_ios_eprint(log_file, "Warning (Frame shift analysis failed) structure #%ld.%s%s%s%s\n",
2388 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));*/
2389 [ # # ]: 0 : if ( !ip->bNoWarnings )
2390 : : {
2391 : 0 : AddErrorMessage(sd->pStrErrStruct, "Frame shift analysis failed");
2392 : : }
2393 : 0 : goto exit_function;
2394 : : }
2395 : : else
2396 : : {
2397 : : /* OK, proceed with frame shift */
2398 : 6 : n_done = 0; /* djb-rwth: ignoring LLVM warning: variable used */
2399 : 6 : n_todo = ed_fs->del_atom->used +
2400 : 6 : ed_fs->del_bond->used + ed_fs->new_bond->used + ed_fs->mod_bond->used +
2401 : 6 : ed_fs->mod_coord->used;
2402 : 6 : OAD_StructureEdits_DebugPrint(ed_fs);
2403 [ - + ]: 6 : if ( n_todo )
2404 : : {
2405 : : /* Edit the original input data according to frame shift info */
2406 : 0 : n_done = OAD_StructureEdits_Apply(sd, ip, orig_inp_data, ed_fs, &ret); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2407 [ # # # # ]: 0 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
2408 : : {
2409 : 0 : ret = _IS_WARNING;
2410 : : /*inchi_ios_eprint(log_file, "Warning (Frame shift failed) structure #%ld.%s%s%s%s\n",
2411 : : num_inp, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));*/
2412 [ # # ]: 0 : if ( !ip->bNoWarnings )
2413 : : {
2414 : 0 : AddErrorMessage(sd->pStrErrStruct, "Frame shift failed");
2415 : : }
2416 : : }
2417 : : else
2418 : : {
2419 [ # # ]: 0 : if ( !ip->bNoWarnings )
2420 : : {
2421 : 0 : WarningMessage(sd->pStrErrStruct, "Bond(s) rearranged due to CRU frame shift");
2422 : : }
2423 : : }
2424 : : }
2425 : : }
2426 : : } /* if (ip->bFrameShiftScheme != FSS_NONE) */
2427 : :
2428 : :
2429 : : } /* if (orig_inp_data->polymer->treat == POLYMERS_MODERN) */
2430 : : }
2431 : :
2432 : : else /* orig_inp_data->polymer->treat == POLYMERS_NO) */
2433 : : {
2434 : : /*inchi_ios_eprint(log_file, "Ignore polymer data");*/
2435 [ # # ]: 0 : if ( !ip->bNoWarnings )
2436 : : {
2437 : 0 : AddErrorMessage(sd->pStrErrStruct, "Ignore polymer data");
2438 : : }
2439 : : }
2440 : : }
2441 : : }
2442 : :
2443 : 161 : exit_function:
2444 [ + + ]: 170 : if ( sinchi_105p )
2445 : : {
2446 [ + - ]: 6 : inchi_free(sinchi_105p);
2447 : : }
2448 [ + + ]: 170 : if ( saux_105p )
2449 : : {
2450 [ + - ]: 6 : inchi_free(saux_105p);
2451 : : }
2452 : 170 : OAD_StructureEdits_Clear(ed_fold); /* Clear edits collection */
2453 : 170 : OAD_StructureEdits_Clear(ed_fs); /* Clear edits collection */
2454 : :
2455 : :
2456 : 170 : return ret;
2457 : : }
2458 : :
2459 : :
2460 : : /****************************************************************************/
2461 : 0 : void swap_atoms_xyz(ORIG_ATOM_DATA* orig_at_data, int ia1, int ia2)
2462 : : {
2463 : : double x, y, z;
2464 : :
2465 [ # # ]: 0 : if (ia1 != ia2)
2466 : : {
2467 : 0 : x = orig_at_data->at[ia1].x;
2468 : 0 : y = orig_at_data->at[ia1].y;
2469 : 0 : z = orig_at_data->at[ia1].z;
2470 : :
2471 : 0 : orig_at_data->at[ia1].x = orig_at_data->at[ia2].x;
2472 : 0 : orig_at_data->at[ia1].y = orig_at_data->at[ia2].y;
2473 : 0 : orig_at_data->at[ia1].z = orig_at_data->at[ia2].z;
2474 : :
2475 : 0 : orig_at_data->at[ia2].x = x;
2476 : 0 : orig_at_data->at[ia2].y = y;
2477 : 0 : orig_at_data->at[ia2].z = z;
2478 : : }
2479 : :
2480 : 0 : return;
2481 : : }
2482 : :
2483 : :
2484 : : /****************************************************************************
2485 : : OAD_StructureEdits_Edit
2486 : : ****************************************************************************/
2487 : 0 : int OAD_StructureEdits_Apply( STRUCT_DATA *sd,
2488 : : INPUT_PARMS *ip,
2489 : : ORIG_ATOM_DATA *orig_at_data,
2490 : : OAD_StructureEdits *ed,
2491 : : int *ret)
2492 : : {
2493 : 0 : int ok = 0, fail;
2494 : 0 : int i, j, old_a1, old_a2, new_a1, new_a2, n_edits = 0;
2495 : : int n_del_atom, n_del_bond, n_new_bond, n_mod_bond, n_mod_coord;
2496 : : int a1, a2;
2497 : 0 : int bond_type = INCHI_BOND_TYPE_NONE, bond_stereo = INCHI_BOND_STEREO_NONE;
2498 : 0 : inp_ATOM* at = orig_at_data->at;
2499 : 0 : OAD_Polymer* p = orig_at_data->polymer;
2500 : 0 : int* at_renum = NULL;
2501 : 0 : int* ibuf = NULL;
2502 : 0 : int n_max_stored = -1;
2503 : :
2504 : 0 : *ret = _IS_OKAY;
2505 : :
2506 : 0 : n_del_atom = ed->del_atom->used;
2507 : 0 : n_del_bond = ed->del_bond->used / 2;
2508 : 0 : n_new_bond = ed->new_bond->used / 3;
2509 : 0 : n_mod_bond = ed->mod_bond->used / 4;
2510 : 0 : n_mod_coord = ed->mod_coord->used / 2;
2511 [ # # ]: 0 : if ( n_del_atom + n_del_bond + n_new_bond + n_mod_bond + n_mod_coord < 1 )
2512 : : {
2513 : 0 : return 0;
2514 : : }
2515 : :
2516 [ # # ]: 0 : n_max_stored = inchi_max(2 * (orig_at_data->num_inp_atoms + 1), 2 * (orig_at_data->num_inp_bonds + 1)); /* set all-purpose buffer */
2517 : :
2518 : : ITRACE_("\n***************************\nOrig_at_data BEFORE EDITS:\n");
2519 : 0 : OrigAtData_DebugTrace(orig_at_data);
2520 : 0 : OAD_Polymer_DebugTrace(orig_at_data->polymer);
2521 : :
2522 : : /* Delete bonds */
2523 [ # # ]: 0 : if ( n_del_bond )
2524 : : {
2525 [ # # ]: 0 : for ( i = 0; i < 2 * n_del_bond; i += 2 )
2526 : : {
2527 : 0 : a1 = ed->del_bond->item[i] - 1;
2528 : 0 : a2 = ed->del_bond->item[i + 1] - 1;
2529 : 0 : ok = OrigAtData_RemoveBond(a1, a2, at, &bond_type, &bond_stereo, &orig_at_data->num_inp_bonds);
2530 [ # # ]: 0 : if ( !ok )
2531 : : {
2532 : 0 : *ret = _IS_ERROR;
2533 : 0 : goto exit_function;
2534 : : }
2535 : 0 : n_edits++;
2536 : : }
2537 : : }
2538 : :
2539 : : /* Add bonds */
2540 [ # # ]: 0 : if ( n_new_bond )
2541 : : {
2542 [ # # ]: 0 : for ( i = 0; i < 2 * n_new_bond; i += 2 )
2543 : : {
2544 : 0 : a1 = ed->new_bond->item[i] - 1;
2545 : 0 : a2 = ed->new_bond->item[i + 1] - 1;
2546 : : /* TODO: consider real bond_type, bond_stereo */
2547 : : /* OrigAtData_AddSingleStereolessBond( a1, a2, at, &dummy ); */
2548 : 0 : ok = OrigAtData_AddBond(a1, a2, at, bond_type, bond_stereo, &orig_at_data->num_inp_bonds);
2549 [ # # ]: 0 : if ( !ok )
2550 : : {
2551 : 0 : *ret = _IS_ERROR;
2552 : 0 : goto exit_function;
2553 : : }
2554 : 0 : n_edits++;
2555 : : }
2556 : : }
2557 : :
2558 : : /* Modify bonds */
2559 [ # # ]: 0 : if ( n_mod_bond )
2560 : : {
2561 [ # # ]: 0 : for ( j = 0; j < 4 * n_mod_bond; j += 4 )
2562 : : {
2563 : 0 : old_a1 = ed->mod_bond->item[j];
2564 : 0 : old_a2 = ed->mod_bond->item[j + 1];
2565 : 0 : new_a1 = ed->mod_bond->item[j + 2];
2566 : 0 : new_a2 = ed->mod_bond->item[j + 3];
2567 [ # # # # : 0 : if ( (old_a1 == new_a1 && old_a2 == new_a2) || (old_a2 == new_a1 && old_a1 == new_a2) )
# # # # ]
2568 : : {
2569 : 0 : continue;
2570 : : }
2571 : 0 : ok = OrigAtData_RemoveBond(old_a1 - 1, old_a2 - 1, at, &bond_type, &bond_stereo, &orig_at_data->num_inp_bonds);
2572 [ # # ]: 0 : if ( !ok )
2573 : : {
2574 : 0 : *ret = _IS_ERROR;
2575 : 0 : goto exit_function;
2576 : : }
2577 : 0 : ok = OrigAtData_AddBond(new_a1 - 1, new_a2 - 1, at, bond_type, bond_stereo, &orig_at_data->num_inp_bonds);
2578 [ # # ]: 0 : if ( !ok )
2579 : : {
2580 : 0 : *ret = _IS_ERROR;
2581 : 0 : goto exit_function;
2582 : : }
2583 : : /* Correct CRU blist lists */
2584 [ # # ]: 0 : for ( i = 0; i < p->n; i++ )
2585 : : {
2586 : 0 : OAD_PolymerUnit* u = p->units[i];
2587 [ # # ]: 0 : if ( !u->blist )
2588 : : {
2589 : : /* No crossing bonds in the unit */
2590 : 0 : continue;
2591 : : }
2592 [ # # ]: 0 : if ( bIsSameBond(u->blist[0], u->blist[1], old_a1, old_a2) )
2593 : : {
2594 : 0 : u->blist[0] = new_a1;
2595 : 0 : u->blist[1] = new_a2;
2596 : : }
2597 [ # # ]: 0 : else if ( bIsSameBond(u->blist[2], u->blist[3], old_a1, old_a2) )
2598 : : {
2599 : 0 : u->blist[2] = new_a1;
2600 : 0 : u->blist[3] = new_a2;
2601 : : }
2602 : : }
2603 : :
2604 : 0 : n_edits++;
2605 : : }
2606 : : }
2607 : :
2608 : : /* Modify coordinates */
2609 [ # # ]: 0 : if ( n_mod_coord )
2610 : : {
2611 [ # # ]: 0 : for ( j = 0; j < 2 * n_mod_coord; j += 2 )
2612 : : {
2613 : 0 : old_a1 = ed->mod_coord->item[j];
2614 : 0 : new_a1 = ed->mod_bond->item[j + 1];
2615 : 0 : swap_atoms_xyz(orig_at_data, old_a1 - 1, new_a1 - 1);
2616 : 0 : n_edits++;
2617 : : }
2618 : : }
2619 : :
2620 : :
2621 : : /* Delete atoms */
2622 [ # # ]: 0 : if ( n_del_atom )
2623 : : {
2624 : : int nat0, nat, nacc;
2625 : 0 : inp_ATOM* new_at = NULL, * new_at0 = NULL;
2626 : :
2627 : 0 : at_renum = (int*)inchi_calloc(n_max_stored, sizeof(int));
2628 [ # # ]: 0 : if ( !at_renum )
2629 : : {
2630 : 0 : *ret = _IS_ERROR;
2631 : 0 : goto exit_function;
2632 : : }
2633 : : /* all-purpose buffer */
2634 : 0 : ibuf = (int*)inchi_calloc(n_max_stored, sizeof(int));
2635 [ # # ]: 0 : if ( !ibuf )
2636 : : {
2637 : 0 : *ret = _IS_ERROR;
2638 : 0 : goto exit_function;
2639 : : }
2640 : :
2641 : 0 : fail = mark_atoms_to_delete_or_renumber(orig_at_data, ed, at_renum);
2642 [ # # ]: 0 : if (fail)
2643 : : {
2644 : 0 : *ret = _IS_ERROR;
2645 : 0 : goto exit_function;
2646 : : }
2647 : : /* Now remove atom by atom */
2648 : :
2649 : 0 : nat0 = orig_at_data->num_inp_atoms;
2650 : 0 : nat = nat0 - ed->del_atom->used;
2651 : 0 : new_at = (inp_ATOM*)inchi_calloc(nat, sizeof(new_at[0]));
2652 [ # # ]: 0 : if ( !new_at )
2653 : : {
2654 : 0 : *ret = _IS_ERROR;
2655 : 0 : goto exit_function;
2656 : : }
2657 : :
2658 [ # # ]: 0 : for ( i = 0, nacc = 0; i < nat0; i++ )
2659 : : {
2660 : : AT_NUMB nbr0[MAXVAL];
2661 : : U_CHAR btype0[MAXVAL];
2662 : : int m, macc, valen;
2663 : 0 : int new_num = at_renum[i];
2664 [ # # ]: 0 : if (new_num == -1)
2665 : : {
2666 : : /* Skip removed atom */
2667 : 0 : continue;
2668 : : }
2669 : :
2670 : : /* Atom to keep; copy it */
2671 : 0 : new_at0 = new_at + nacc;
2672 : 0 : ++nacc;
2673 : 0 : memcpy(new_at0, orig_at_data->at + i, sizeof(new_at[0]));
2674 : : /* Correct its own number(s) */
2675 : 0 : new_at0->orig_at_number = new_num + 1;
2676 : :
2677 : : /* Correct its nbr number(s) */
2678 : 0 : valen = new_at0->valence;
2679 : 0 : memcpy(nbr0, new_at0->neighbor, valen * sizeof(AT_NUMB));
2680 : 0 : memcpy(btype0, new_at0->bond_type, valen);
2681 : 0 : memset(new_at0->neighbor, 0, valen); /* djb-rwth: memset_s C11/Annex K variant? */
2682 [ # # ]: 0 : for ( m = 0, macc = 0; m < valen; m++ )
2683 : : {
2684 : 0 : int num2 = nbr0[m];
2685 : 0 : int renum2 = at_renum[num2];
2686 [ # # ]: 0 : if ( renum2 == num2 )
2687 : : {
2688 : : /* keep old */
2689 : 0 : new_at0->neighbor[macc++] = num2;
2690 : : }
2691 [ # # ]: 0 : else if ( renum2 == -1 )
2692 : : {
2693 : : /* skip and decrement valences */
2694 : 0 : new_at0->chem_bonds_valence -= btype0[m];
2695 : 0 : new_at0->valence--;
2696 : : }
2697 : : else
2698 : : {
2699 : : /* set renumbered */
2700 : 0 : new_at0->neighbor[macc++] = renum2;
2701 : : }
2702 : : }
2703 : : }
2704 : :
2705 [ # # ]: 0 : if ( new_at )
2706 : : {
2707 [ # # ]: 0 : inchi_free(orig_at_data->at);
2708 : 0 : orig_at_data->at = new_at;
2709 : : }
2710 : :
2711 : 0 : orig_at_data->num_inp_atoms = nacc;
2712 : 0 : orig_at_data->num_inp_bonds = 0;
2713 [ # # ]: 0 : for ( i = 0; i < nacc; i++ )
2714 : : {
2715 : 0 : orig_at_data->num_inp_bonds += new_at[i].valence;
2716 : : }
2717 : 0 : orig_at_data->num_inp_bonds /= 2;
2718 : :
2719 : : /* Correct other data */
2720 : :
2721 : : /* Correct polymer data */
2722 [ # # ]: 0 : if ( p )
2723 : : {
2724 : : int iu;
2725 : :
2726 [ # # ]: 0 : for ( iu = 0; iu < p->n; iu++ )
2727 : : {
2728 : 0 : OAD_PolymerUnit* u = p->units[iu];
2729 : : int new_na, new_nb, new_bb;
2730 : 0 : memset(ibuf, 0, n_max_stored * sizeof(ibuf[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2731 [ # # ]: 0 : if ( u )
2732 : : {
2733 [ # # ]: 0 : if ( u->alist )
2734 : : {
2735 : 0 : memcpy(ibuf, u->alist, u->na * sizeof(ibuf[0]));
2736 : 0 : new_na = set_renumbered_or_delete(u->alist, ibuf, u->na, at_renum, 1);
2737 [ # # ]: 0 : if ( new_na == -1 )
2738 : : {
2739 : 0 : *ret = _IS_ERROR;
2740 : 0 : goto exit_function;
2741 : : }
2742 : 0 : u->na = new_na;
2743 : : }
2744 [ # # ]: 0 : if ( u->blist )
2745 : : {
2746 : 0 : memcpy(ibuf, u->blist, 2 * (long long)u->nb * sizeof(int)); /* djb-rwth: cast operator added */
2747 : 0 : new_nb = set_renumbered_or_delete(u->blist, ibuf, 2 * u->nb, at_renum, 1);
2748 : 0 : new_nb /= 2;
2749 [ # # ]: 0 : if ( new_nb == -1 )
2750 : : {
2751 : 0 : *ret = _IS_ERROR;
2752 : 0 : goto exit_function;
2753 : : }
2754 : 0 : u->nb = new_nb;
2755 : : }
2756 : :
2757 : :
2758 [ # # ]: 0 : if ( u->bkbonds )
2759 : : {
2760 : : int b, new_nbkbonds;
2761 [ # # ]: 0 : for ( b = 0, new_nbkbonds = 0; b < u->nbkbonds; b++ )
2762 : : {
2763 : : int bnd[2];
2764 : 0 : bnd[0] = u->bkbonds[b][0];
2765 : 0 : bnd[1] = u->bkbonds[b][1];
2766 : 0 : memcpy(ibuf, bnd, 2 * sizeof(ibuf[0]));
2767 : 0 : memset(u->bkbonds[b], 0, 2 * sizeof(u->bkbonds[0][0])); /* djb-rwth: memset_s C11/Annex K variant? */
2768 : 0 : new_bb = set_renumbered_or_delete(bnd, ibuf, 2, at_renum, 1);
2769 [ # # ]: 0 : if ( new_bb == -1 )
2770 : : {
2771 : 0 : *ret = _IS_ERROR;
2772 : 0 : goto exit_function;
2773 : : }
2774 [ # # ]: 0 : else if ( new_bb == 2 )
2775 : : {
2776 : 0 : u->bkbonds[new_nbkbonds][0] = bnd[0];
2777 : 0 : u->bkbonds[new_nbkbonds][1] = bnd[1];
2778 : 0 : new_nbkbonds++;
2779 : : /* OK, settled new nums or kept old */
2780 : : }
2781 : : else
2782 : : {
2783 : 0 : continue;
2784 : : }
2785 : : }
2786 : 0 : u->nbkbonds = new_nbkbonds;
2787 : : }
2788 : :
2789 [ # # ]: 0 : if ( u->blist ) /* djb-rwth: fixing a NULL pointer dereference */
2790 : : {
2791 : 0 : u->cap1 = u->blist[0];
2792 : 0 : u->end_atom1 = u->blist[1];
2793 : 0 : u->cap2 = u->blist[2];
2794 : 0 : u->end_atom2 = u->blist[3];
2795 : : }
2796 [ # # # # : 0 : if ( u->cap1 < 1 || u->cap2 < 1 || u->end_atom1 < 1 || u->end_atom2 < 1 )
# # # # ]
2797 : : {
2798 : 0 : *ret = _IS_ERROR;
2799 : 0 : goto exit_function;
2800 : : }
2801 : :
2802 : :
2803 : : }
2804 : : }
2805 : : } /* if (p) */
2806 : :
2807 : : /* Correct V300 data */
2808 : 0 : if ( orig_at_data->v3000 )
2809 : : {
2810 : : ;
2811 : : }
2812 : :
2813 : :
2814 : : } /* if (n_del_atom) */
2815 : :
2816 : :
2817 : 0 : exit_function:
2818 [ # # ]: 0 : if ( ibuf )
2819 : : {
2820 [ # # ]: 0 : inchi_free(ibuf);
2821 : : }
2822 [ # # ]: 0 : if ( at_renum )
2823 : : {
2824 [ # # ]: 0 : inchi_free(at_renum);
2825 : : }
2826 : 0 : return n_edits;
2827 : : }
2828 : :
2829 : :
2830 : : /****************************************************************************
2831 : : Set each element of number to renum[element] or delete it if renum==(base -1)
2832 : : base is either 0 (0-started numbers) or 1 (1-started)
2833 : : Returns new number of elements or -1 at error
2834 : : ****************************************************************************/
2835 : 0 : int set_renumbered_or_delete(int* number, /* numbers to correct */
2836 : : int* buf, /* must be enough size to hold nelems elements */
2837 : : int nelems, /* initial size of numbers */
2838 : : int* renum, /* new numbers in order of old numbers; always 0-based */
2839 : : int base)
2840 : : {
2841 : : int i, new_nelems;
2842 : 0 : memcpy(buf, number, nelems * sizeof(int));
2843 : 0 : memset(number, 0, nelems * sizeof(int)); /* djb-rwth: memset_s C11/Annex K variant? */
2844 [ # # ]: 0 : for ( i = 0, new_nelems = 0; i < nelems; i++ )
2845 : : {
2846 : 0 : int new_num = renum[buf[i] - base] + base;
2847 [ # # ]: 0 : if ( new_num == (base - 1) )
2848 : : {
2849 : 0 : continue;
2850 : : }
2851 : : else
2852 : : {
2853 : 0 : number[new_nelems++] = new_num;
2854 : : }
2855 : : }
2856 : 0 : return new_nelems;
2857 : : }
2858 : :
2859 : :
2860 : : /****************************************************************************
2861 : : Worker placed under ProcessOneStructure wrapper (not the last nested doll)
2862 : : ****************************************************************************/
2863 : 185 : int ProcessOneStructureExCore(struct tagINCHI_CLOCK* ic,
2864 : : struct tagCANON_GLOBALS* CG,
2865 : : STRUCT_DATA* sd,
2866 : : INPUT_PARMS* ip,
2867 : : char* szTitle,
2868 : : PINChI2* pINChI2[INCHI_NUM],
2869 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
2870 : : INCHI_IOSTREAM* inp_file,
2871 : : INCHI_IOSTREAM* log_file,
2872 : : INCHI_IOSTREAM* out_file,
2873 : : INCHI_IOSTREAM* prb_file,
2874 : : ORIG_ATOM_DATA* orig_inp_data,
2875 : : ORIG_ATOM_DATA* prep_inp_data,
2876 : : long num_inp,
2877 : : INCHI_IOS_STRING* strbuf,
2878 : : unsigned char save_opt_bits)
2879 : : {
2880 : 185 : int res = _IS_OKAY;
2881 : : int mind_polymers;
2882 : :
2883 : : #ifdef TARGET_LIB_FOR_WINCHI
2884 : : inchi_ios_free_str(out_file);
2885 : : inchi_ios_print(out_file, "Structure: %d\n", num_inp);
2886 : : #endif
2887 : :
2888 : : /* Polymer and pseudoelement specific */
2889 : 185 : res = ValidateAndPreparePolymerAndPseudoatoms(ic, CG, sd, ip, szTitle, pINChI2, pINChI_Aux2,
2890 : : inp_file, log_file, out_file, prb_file,
2891 : : orig_inp_data, prep_inp_data, num_inp, strbuf,
2892 : : save_opt_bits, &mind_polymers);
2893 [ + - - + ]: 185 : if ( res == _IS_ERROR || res == _IS_FATAL )
2894 : : {
2895 : 0 : return res;
2896 : : }
2897 : :
2898 : : /* Call the very actual worker placed under this (ProcessOneStructureCore) wrapper */
2899 : 185 : res = ProcessOneStructure(ic, CG, sd, ip, szTitle, pINChI2, pINChI_Aux2,
2900 : : inp_file, log_file, out_file, prb_file,
2901 : : orig_inp_data, prep_inp_data, num_inp, strbuf,
2902 : : save_opt_bits);
2903 : :
2904 [ + + + + : 185 : if ( (res == _IS_OKAY || res == _IS_WARNING) && mind_polymers )
+ + ]
2905 : : {
2906 : : /* Post-edit the polymer layer at older polymer treatment modes (1.05, 1.05+) */
2907 [ + - + + ]: 24 : if ( ip->bPolymers == POLYMERS_LEGACY || ip->bPolymers == POLYMERS_LEGACY_PLUS )
2908 : : {
2909 : : /* Cut and hide "Zz" and related things in InChI (AuxInfo has a specifics). */
2910 : 6 : int n_pzz = 0, n_zy = orig_inp_data->n_zy;
2911 [ + - ]: 6 : if ( orig_inp_data->polymer )
2912 : : {
2913 : 6 : n_pzz = orig_inp_data->polymer->n_pzz;
2914 : : }
2915 : :
2916 : 6 : EditINCHI_HidePolymerZz(out_file, n_pzz, n_zy);
2917 : :
2918 : : }
2919 : : }
2920 : :
2921 : : #ifdef TARGET_LIB_FOR_WINCHI
2922 : : /* if ( res == _IS_ERROR || res == _IS_FATAL )
2923 : : {
2924 : : inchi_ios_print(out_file, "Error %d (%s)\n", sd->nErrorCode, sd->pStrErrStruct);
2925 : : }
2926 : : */
2927 : : /*push_to_winchi_text_window(out_file); */
2928 : : /*inchi_ios_free_str(out_file);*/
2929 : : /*inchi_ios_flush(out_file);*/
2930 : : #endif
2931 : :
2932 : 185 : return res;
2933 : : }
2934 : :
2935 : :
2936 : : /****************************************************************************
2937 : : Treat pseudoelement and polymers: parse, validate and set details
2938 : : ****************************************************************************/
2939 : 185 : int ValidateAndPreparePolymerAndPseudoatoms(struct tagINCHI_CLOCK* ic,
2940 : : struct tagCANON_GLOBALS* CG,
2941 : : STRUCT_DATA* sd,
2942 : : INPUT_PARMS* ip,
2943 : : char* szTitle,
2944 : : PINChI2* pINChI2[INCHI_NUM],
2945 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
2946 : : INCHI_IOSTREAM* inp_file,
2947 : : INCHI_IOSTREAM* log_file,
2948 : : INCHI_IOSTREAM* out_file,
2949 : : INCHI_IOSTREAM* prb_file,
2950 : : ORIG_ATOM_DATA* orig_inp_data,
2951 : : ORIG_ATOM_DATA* prep_inp_data,
2952 : : long num_inp,
2953 : : INCHI_IOS_STRING* strbuf,
2954 : : unsigned char save_opt_bits,
2955 : : int* mind_polymers)
2956 : : {
2957 : 185 : int res = _IS_OKAY;
2958 : :
2959 : 185 : int mind_pseudoelements = 0;
2960 : :
2961 : : /* djb-rwth: fixing coverity ID #499512 */
2962 [ - + ]: 185 : if (!orig_inp_data)
2963 : : {
2964 : 0 : goto exit_function;
2965 : : }
2966 : :
2967 [ + + + - ]: 185 : *mind_polymers = orig_inp_data->polymer && orig_inp_data->polymer->n > 0;
2968 [ + + + - ]: 209 : *mind_polymers = *mind_polymers && orig_inp_data->valid_polymer &&
2969 [ - + - - ]: 24 : (ip->nInputType == INPUT_MOLFILE || ip->nInputType == INPUT_SDFILE);
2970 [ - + - - ]: 185 : mind_pseudoelements = (ip->bNPZz == 1) || (ip->bPolymers != POLYMERS_NO);
2971 : :
2972 : :
2973 : : /* Validate the data */
2974 : 185 : res = OAD_ValidatePolymerAndPseudoElementData(orig_inp_data,
2975 : : ip->bPolymers,
2976 : : ip->bNPZz,
2977 : 185 : sd->pStrErrStruct,
2978 : : ip->bNoWarnings);
2979 [ - + ]: 185 : if ( res )
2980 : : {
2981 : 0 : sd->nErrorCode = res;
2982 : 0 : inchi_ios_eprint(log_file, "Error %d (%s) structure #%ld.%s%s%s%s\n",
2983 : 0 : sd->nErrorCode, sd->pStrErrStruct, num_inp,
2984 [ # # # # : 0 : SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
# # # # #
# # # # #
# # # # #
# # # #
# ]
2985 : 0 : res = _IS_ERROR;
2986 : 0 : orig_inp_data->num_inp_atoms = -1; /* djb-rwth: fixing coverity ID #499522 */
2987 : 0 : goto exit_function;
2988 : : }
2989 : :
2990 [ + + - + ]: 185 : if ( *mind_polymers || mind_pseudoelements )
2991 : : {
2992 : : /*OrigAtData_DebugTrace(orig_inp_data);*/
2993 [ + + ]: 185 : if ( *mind_polymers &&
2994 [ + + ]: 24 : ip->bPolymers != POLYMERS_MODERN &&
2995 [ - + - - ]: 6 : (ip->bFrameShiftScheme == FSS_STARS_CYCLED || ip->bFrameShiftScheme == FSS_STARS_CYCLED_SORTED) )
2996 : : {
2997 : : /* Analyze and cyclize frame-shift eligible CRUs using InChI canonical numbers
2998 : : (do this only at older polymer treatment modes 1.05, 1.05+)
2999 : : */
3000 : 6 : res = OAD_Polymer_CyclizeCloseableUnits(orig_inp_data,
3001 : : ip->bPolymers,
3002 : 6 : sd->pStrErrStruct,
3003 : : ip->bNoWarnings);
3004 [ + - ]: 6 : if ( res )
3005 : : {
3006 : 0 : sd->nErrorCode = res;
3007 : 0 : AddErrorMessage(sd->pStrErrStruct, "Error while processing polymer-related input");
3008 : 0 : res = _IS_ERROR;
3009 : 0 : orig_inp_data->num_inp_atoms = -1;
3010 : 0 : goto exit_function;
3011 : : }
3012 : : /*OrigAtData_DebugTrace(orig_inp_data);*/
3013 : : }
3014 : : }
3015 : :
3016 : 185 : exit_function:
3017 : :
3018 : 185 : return res;
3019 : : }
3020 : :
3021 : :
3022 : : /****************************************************************************
3023 : : Get InChI and AuxInfo of totally unedited original structure.
3024 : : The intent is to preserve AuxInfo for the very original structure
3025 : : in order to keep a final ability to restore that structure.
3026 : : ****************************************************************************/
3027 : 9 : int OAD_ProcessOneStructureNoEdits( struct tagINCHI_CLOCK *ic,
3028 : : struct tagCANON_GLOBALS *CG,
3029 : : STRUCT_DATA *sd,
3030 : : INPUT_PARMS *ip,
3031 : : char *szTitle,
3032 : : PINChI2 *pINChI2[INCHI_NUM],
3033 : : PINChI_Aux2 *pINChI_Aux2[INCHI_NUM],
3034 : : INCHI_IOSTREAM *inp_file,
3035 : : INCHI_IOSTREAM *log_file,
3036 : : INCHI_IOSTREAM *out_file,
3037 : : INCHI_IOSTREAM *prb_file,
3038 : : ORIG_ATOM_DATA *orig_inp_data,
3039 : : ORIG_ATOM_DATA *prep_inp_data,
3040 : : long num_inp,
3041 : : INCHI_IOS_STRING *strbuf,
3042 : : unsigned char save_opt_bits,
3043 : : int *n_pzz,
3044 : : char **sinchi,
3045 : : char **saux)
3046 : : {
3047 : : size_t slen;
3048 : 9 : int ret = _IS_OKAY, dup_fail = 0;
3049 : 9 : POSEContext dup_context, * dup = &dup_context;
3050 : :
3051 : 9 : *n_pzz = 0;
3052 : 9 : *sinchi = NULL;
3053 : 9 : *saux = NULL;
3054 : :
3055 : : /* Make a working copy of all the native input */
3056 : 9 : dup_fail = POSEContext_Init(dup, sd, ip, szTitle, pINChI2, pINChI_Aux2,
3057 : : inp_file, log_file, out_file, prb_file,
3058 : : orig_inp_data, prep_inp_data,
3059 : : num_inp, strbuf, save_opt_bits);
3060 [ - + ]: 9 : if ( dup_fail )
3061 : : {
3062 : 0 : ret = _IS_ERROR;
3063 : 0 : goto exit_function;
3064 : : }
3065 : :
3066 : : /* Set necessary for this specific case options */
3067 : 9 : dup->orig_inp_data->polymer->treat = dup->ip.bPolymers = POLYMERS_MODERN;
3068 : 9 : dup->ip.bFoldPolymerSRU = 0;
3069 : 9 : dup->ip.bFrameShiftScheme = FSS_NONE;
3070 : 9 : dup->ip.bINChIOutputOptions &= ~(INCHI_OUT_NO_AUX_INFO | INCHI_OUT_SHORT_AUX_INFO);
3071 : 9 : dup->ip.bDisplay = dup->ip.bDisplayCompositeResults = dup->ip.bDisplayEachComponentINChI = 0;
3072 : 9 : dup->ip.bFoldPolymerSRU = 0;
3073 : : /*dup->ip.bTautFlags |= TG_FLAG_RECONNECT_COORD;*/
3074 : :
3075 : : /* Calculate */
3076 : 9 : ret = ProcessOneStructureExCore(ic, CG, &dup->sd, &dup->ip, dup->szTitle,
3077 : 9 : dup->pINChI2, dup->pINChI_Aux2,
3078 : : dup->inp_file, dup->log_file,
3079 : : dup->out_file, dup->prb_file,
3080 : : dup->orig_inp_data, dup->prep_inp_data,
3081 : 9 : dup->num_inp, dup->strbuf, dup->save_opt_bits);
3082 : :
3083 [ + - - + ]: 9 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3084 : : {
3085 : 0 : goto exit_function;
3086 : : }
3087 : :
3088 : 9 : *n_pzz = dup->orig_inp_data->polymer->n_pzz;
3089 : : /* Extract InChI */
3090 : 9 : slen = dup->out_file->s.nUsedLength;
3091 : 9 : extract_inchi_substring(sinchi, dup->out_file->s.pStr, slen);
3092 [ - + ]: 9 : if ( !*sinchi )
3093 : : {
3094 : 0 : ret = _IS_ERROR;
3095 : : }
3096 [ + - - + ]: 9 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3097 : : {
3098 : 0 : goto exit_function;
3099 : : }
3100 : : /* Extract AuxInfo */
3101 : 9 : slen = dup->out_file->s.nUsedLength;
3102 : 9 : extract_auxinfo_substring(saux, dup->out_file->s.pStr, slen);
3103 [ - + ]: 9 : if ( !*saux )
3104 : : {
3105 : 0 : ret = _IS_ERROR;
3106 : : }
3107 [ + - + - ]: 9 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3108 : : {
3109 : 0 : goto exit_function;
3110 : : }
3111 : :
3112 : 9 : exit_function:
3113 : 9 : POSEContext_Free(dup);
3114 : :
3115 : 9 : return ret;
3116 : : }
3117 : :
3118 : :
3119 : : /****************************************************************************
3120 : : Get InChI and AuxInfo of original structure using 1.05+ InChI version.
3121 : : ****************************************************************************/
3122 : 6 : int OAD_ProcessOneStructure105Plus(struct tagINCHI_CLOCK* ic,
3123 : : struct tagCANON_GLOBALS* CG,
3124 : : STRUCT_DATA* sd,
3125 : : INPUT_PARMS* ip,
3126 : : char* szTitle,
3127 : : PINChI2* pINChI2[INCHI_NUM],
3128 : : PINChI_Aux2* pINChI_Aux2[INCHI_NUM],
3129 : : INCHI_IOSTREAM* inp_file,
3130 : : INCHI_IOSTREAM* log_file,
3131 : : INCHI_IOSTREAM* out_file,
3132 : : INCHI_IOSTREAM* prb_file,
3133 : : ORIG_ATOM_DATA* orig_inp_data,
3134 : : ORIG_ATOM_DATA* prep_inp_data,
3135 : : long num_inp,
3136 : : INCHI_IOS_STRING* strbuf,
3137 : : unsigned char save_opt_bits,
3138 : : char** sinchi,
3139 : : char** saux)
3140 : : {
3141 : : size_t slen;
3142 : 6 : int ret = _IS_OKAY, dup_fail = 0;
3143 : 6 : POSEContext dup_context, * dup = &dup_context;
3144 : :
3145 : 6 : *sinchi = NULL;
3146 : 6 : *saux = NULL;
3147 : :
3148 : : /* Make a working copy of all the native input */
3149 : 6 : dup_fail = POSEContext_Init(dup, sd, ip, szTitle, pINChI2, pINChI_Aux2,
3150 : : inp_file, log_file, out_file, prb_file,
3151 : : orig_inp_data, prep_inp_data,
3152 : : num_inp, strbuf, save_opt_bits);
3153 [ - + ]: 6 : if ( dup_fail )
3154 : : {
3155 : 0 : ret = _IS_ERROR;
3156 : 0 : goto exit_function;
3157 : : }
3158 : :
3159 : : /* Set necessary for this specific case options */
3160 : : /* 1.05+ polymer treatment mode: hidden Zz, senior bkbond comes the first */
3161 : 6 : dup->orig_inp_data->polymer->treat = dup->ip.bPolymers = POLYMERS_LEGACY_PLUS;
3162 : : /* include full aux info */
3163 : 6 : dup->ip.bINChIOutputOptions &= ~(INCHI_OUT_NO_AUX_INFO | INCHI_OUT_SHORT_AUX_INFO);
3164 : : /* request no /D display in inchi-1 executable */
3165 : 6 : dup->ip.bDisplay = dup->ip.bDisplayCompositeResults = dup->ip.bDisplayEachComponentINChI = 0;
3166 : :
3167 : : /* Calculate */
3168 : 6 : ret = ProcessOneStructureExCore(ic, CG, &dup->sd, &dup->ip, dup->szTitle,
3169 : 6 : dup->pINChI2, dup->pINChI_Aux2,
3170 : : dup->inp_file, dup->log_file,
3171 : : dup->out_file, dup->prb_file,
3172 : : dup->orig_inp_data, dup->prep_inp_data,
3173 : 6 : dup->num_inp, dup->strbuf, dup->save_opt_bits);
3174 : :
3175 [ + - - + ]: 6 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3176 : : {
3177 : 0 : goto exit_function;
3178 : : }
3179 : :
3180 : : /* Extract InChI */
3181 : 6 : slen = dup->out_file->s.nUsedLength;
3182 : 6 : extract_inchi_substring(sinchi, dup->out_file->s.pStr, slen);
3183 [ - + ]: 6 : if ( !*sinchi )
3184 : : {
3185 : 0 : ret = _IS_ERROR;
3186 : : }
3187 [ + - - + ]: 6 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3188 : : {
3189 : 0 : goto exit_function;
3190 : : }
3191 : : /* Extract AuxInfo */
3192 : 6 : slen = dup->out_file->s.nUsedLength;
3193 : 6 : extract_auxinfo_substring(saux, dup->out_file->s.pStr, slen);
3194 [ - + ]: 6 : if ( !*saux )
3195 : : {
3196 : 0 : ret = _IS_ERROR;
3197 : : }
3198 [ + - + - ]: 6 : if ( ret == _IS_FATAL || ret == _IS_ERROR )
3199 : : {
3200 : 0 : goto exit_function;
3201 : : }
3202 : :
3203 : :
3204 : 6 : exit_function:
3205 : 6 : POSEContext_Free(dup);
3206 : :
3207 : 6 : return ret;
3208 : : }
3209 : :
3210 : :
3211 : : /****************************************************************************
3212 : : Mark atoms to be renumbered or removed
3213 : : at_renum[initial number] := new_number or -1 (mark of deletion)
3214 : : ****************************************************************************/
3215 : 0 : int mark_atoms_to_delete_or_renumber(ORIG_ATOM_DATA* orig_at_data,
3216 : : OAD_StructureEdits* ed,
3217 : : int* at_renum)
3218 : : {
3219 : : int i, j;
3220 : 0 : int fail = 0, ret = 0;
3221 : 0 : size_t* atnums = NULL; /* djb-rwth: needs to be size_t type */
3222 : 0 : size_t max_atoms = orig_at_data->num_inp_atoms;
3223 : :
3224 : : /* NB: new/old ORIG_ATOM_DATA atom numbers are 0-based (==orig_number-1)
3225 : : while those in ed->... are just 1-based orig_numbers */
3226 : :
3227 [ # # ]: 0 : for (i = 0; (size_t)i < max_atoms; i++)
3228 : : {
3229 : 0 : at_renum[i] = i;
3230 : : }
3231 : :
3232 [ # # ]: 0 : if ( ed->del_side_chains )
3233 : : {
3234 : : /* Extend list of atoms to be deleted with those connected to original ones
3235 : : (i.e., delete a whole connected component(s) comprising original atoms)
3236 : : */
3237 : 0 : int natnums = 0;
3238 : 0 : atnums = (size_t*)inchi_calloc(max_atoms, sizeof(size_t)); /* djb-rwth: size_t type used for max_atoms to fit the definition of inchi_calloc */
3239 [ # # ]: 0 : if ( !atnums )
3240 : : {
3241 : 0 : return _IS_ERROR;
3242 : : }
3243 [ # # ]: 0 : for ( i = 0; (size_t)i < max_atoms; i++ )
3244 : : {
3245 : 0 : int iatom = ed->del_atom->item[i] - 1;
3246 : 0 : subgraf* sg = NULL;
3247 : 0 : subgraf_pathfinder* spf = NULL;
3248 [ # # ]: 0 : if ( i >= ed->del_atom->used ) /* NB: ed->del_atom->used may be increased within this loop */
3249 : : {
3250 : 0 : break;
3251 : : }
3252 [ # # ]: 0 : for ( j = 0; (size_t)j < max_atoms; j++ )
3253 : : {
3254 : 0 : atnums[j] = orig_at_data->at[j].orig_at_number; /*j+1;*/
3255 : : }
3256 : 0 : sg = subgraf_new(orig_at_data, max_atoms, (int*)atnums);
3257 : 0 : memset(atnums, 0, max_atoms * sizeof(int)); /* djb-rwth: memset_s C11/Annex K variant? */
3258 [ # # ]: 0 : if ( !sg )
3259 : : {
3260 : 0 : ret = _IS_ERROR;
3261 : 0 : goto exit_function;
3262 : : }
3263 : 0 : spf = subgraf_pathfinder_new(sg, orig_at_data, iatom, iatom);
3264 [ # # ]: 0 : if ( !spf )
3265 : : {
3266 : 0 : ret = _IS_ERROR;
3267 : 0 : goto exit_function;
3268 : : }
3269 : 0 : spf->start = iatom;
3270 : 0 : spf->nseen = 0;
3271 : 0 : natnums = subgraf_pathfinder_collect_all(spf, 0, NULL, (int*)atnums);
3272 [ # # ]: 0 : if ( natnums )
3273 : : {
3274 : :
3275 [ # # # # ]: 0 : for (j = 0; j < natnums && j < max_atoms; j++) /* djb-rwth: fixing buffer overruns */
3276 : : {
3277 : 0 : fail = IntArray_AppendIfAbsent(ed->del_atom, atnums[j]);
3278 [ # # ]: 0 : if (fail)
3279 : : {
3280 : 0 : ret = _IS_ERROR;
3281 : 0 : goto exit_function;
3282 : : }
3283 : : }
3284 : : }
3285 : 0 : subgraf_free(sg);
3286 : 0 : subgraf_pathfinder_free(spf);
3287 : : }
3288 : :
3289 : : } /* if (ed->del_side_chains) */
3290 : :
3291 [ # # ]: 0 : for ( i = max_atoms - 1; i >= 0; i-- )
3292 : : {
3293 : 0 : int orig_num = i + 1; /* NB: ed->del_atom->item contains orig# which are (OAD# + 1) */
3294 [ # # ]: 0 : if (is_in_the_ilist(ed->del_atom->item, orig_num, ed->del_atom->used))
3295 : : {
3296 : : /* mark as deleted atnum */
3297 : 0 : at_renum[i] = -1;
3298 : : /* shift other atnums */
3299 [ # # ]: 0 : for ( j = max_atoms - 1; j > i; j-- )
3300 : : {
3301 [ # # ]: 0 : if ( at_renum[j] != -1 )
3302 : : {
3303 : 0 : at_renum[j]--;
3304 : : }
3305 : : }
3306 : : }
3307 : : }
3308 : :
3309 : :
3310 : 0 : exit_function:
3311 [ # # ]: 0 : if ( atnums )
3312 : : {
3313 [ # # ]: 0 : inchi_free(atnums);
3314 : : }
3315 : 0 : return ret;
3316 : : }
3317 : :
3318 : :
3319 : : #if (BUILD_WITH_ENG_OPTIONS==1)
3320 : : #if ALLOW_SUBSTRUCTURE_FILTERING==1
3321 : : int check_presence_of_the_encoded_substructure(ORIG_ATOM_DATA* orig_inp_data);
3322 : : int OrigAtData_CheckForSubstructure(ORIG_ATOM_DATA* orig_inp_data)
3323 : : {
3324 : : int ok = 0;
3325 : :
3326 : : ok = check_presence_of_the_encoded_substructure(orig_inp_data);
3327 : :
3328 : : return ok;
3329 : : }
3330 : : int check_presence_of_the_encoded_substructure(ORIG_ATOM_DATA* oad)
3331 : : {
3332 : : int i, ok;
3333 : :
3334 : : /* Place sub-structure filtering code below.
3335 : :
3336 : : Return 1 if structure matches some hard-coded pattern
3337 : :
3338 : : In this example pattern is a presence of a pseudo atom.
3339 : : */
3340 : :
3341 : : ok = 0;
3342 : : for ( i = 0; i < oad->num_inp_atoms; i++ )
3343 : : {
3344 : : if ( oad->at[i].el_number == EL_NUMBER_ZZ || oad->at[i].el_number == EL_NUMBER_ZY )
3345 : : {
3346 : : ok = 1;
3347 : : break;
3348 : : }
3349 : : }
3350 : : return ok;
3351 : : }
3352 : : #endif
3353 : : #endif
|