Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : : #include <ctype.h>
8 : : #include <string.h>
9 : : #include <math.h>
10 : : #include <float.h>
11 : : #include <limits.h>
12 : :
13 : : #include "mode.h"
14 : : #include "mol_fmt.h"
15 : :
16 : : #include "ichierr.h"
17 : : #include "util.h"
18 : : #include "ichi_io.h"
19 : : #include "ichimain.h"
20 : :
21 : : #include "bcf_s.h"
22 : :
23 : : /*
24 : : SDFile related procedures
25 : :
26 : : */
27 : :
28 : : #define ALIASED_AT(i) (0 < NUM_ISO_H(at, i))
29 : : #define IS_DEUTERIUM(i) (!strcmp(at[i].elname, "D") || at[i].iso_atw_diff == 2 && !strcmp(at[i].elname, "H"))
30 : : #define IS_TRITIUM(i) (!strcmp(at[i].elname, "T") || at[i].iso_atw_diff == 3 && !strcmp(at[i].elname, "H"))
31 : :
32 : : #define ABNORMAL_ISO(i) (at[i].iso_atw_diff == 1 || at[i].iso_atw_diff < -3 || at[i].iso_atw_diff > 5)
33 : : #define ABNORMAL_CHG(i) (abs(at[i].charge) > 3)
34 : : #define ABNORMAL_RAD(i) (RADICAL_SINGLET <= at[i].radical && at[i].radical <= RADICAL_TRIPLET)
35 : :
36 : : #define ANY_ISO(i, X) ((X)? (at[i].iso_atw_diff && !IS_DEUTERIUM(i) && !IS_TRITIUM(i)) :\
37 : : (at[i].iso_atw_diff || IS_DEUTERIUM(i) || IS_TRITIUM(i)))
38 : : #define ANY_CHG(i) (0 != at[i].charge)
39 : : #define ANY_RAD(i) (RADICAL_SINGLET <= at[i].radical && at[i].radical <= RADICAL_TRIPLET)
40 : :
41 : : #define NORMAL_ISO(i, X) (ANY_ISO(i, X) && !ABNORMAL_ISO(i))
42 : :
43 : : /* needs additional M CHG. M RAD, M ISO line */
44 : : /* due to ISIS/Draw feature always include M RAD for any radical */
45 : : #define ABNORMAL_AT(i) (at[i].radical || abs(at[i].charge) > 3 || \
46 : : ABNORMAL_ISO(i))
47 : :
48 : : /* always add M ISO, M RAD, M CHG; Except: (bAtomsDT && D or T) */
49 : : #define ADD_LINE_AT(i) (at[i].charge || \
50 : : at[i].radical || \
51 : : at[i].iso_atw_diff && (bAtomsDT ? (at[i].iso_atw_diff != 1 || strcmp(at[i].elname, "H")) : 1))
52 : :
53 : : /* Local */
54 : :
55 : : static const char sdf_data_hdr_name[] = "NAME";
56 : : static const char sdf_data_hdr_comm[] = "COMMENT";
57 : :
58 : : enum
59 : : {
60 : : SDF_START,
61 : : SDF_DATA_HEADER,
62 : : SDF_DATA_HEADER_NAME,
63 : : SDF_DATA_HEADER_COMMENT,
64 : : SDF_DATA_HEADER_CAS,
65 : : SDF_DATA_HEADER_USER,
66 : : SDF_DATA_LINE,
67 : : SD_FMT_END_OF_DATA_ITEM,
68 : : SDF_EMPTY_LINE,
69 : : SD_FMT_END_OF_DATA_BLOCK
70 : : };
71 : :
72 : : int OrigAtData_WriteToSDfileHeaderAndCountThings(const ORIG_ATOM_DATA *inp_at_data,
73 : : INCHI_IOSTREAM *fcb,
74 : : const char *name,
75 : : const char *comment,
76 : : int bChiralFlag,
77 : : int bAtomsDT,
78 : : const char *szLabel,
79 : : const char *szValue,
80 : : int *nNumAliasLines,
81 : : int *nNumChargeLines,
82 : : int *nNumRadicalLines,
83 : : int *nNumIsoLines,
84 : : int *nNumAddLines,
85 : : int *num_bonds);
86 : : int OrigAtData_WriteToSDfileAtomsBlock(const ORIG_ATOM_DATA *inp_at_data,
87 : : INCHI_IOSTREAM *fcb,
88 : : const char *name,
89 : : const char *comment,
90 : : int bAtomsDT,
91 : : const char *szLabel,
92 : : const char *szValue);
93 : :
94 : : int OrigAtData_WriteToSDfileBondsBlock(const ORIG_ATOM_DATA *inp_at_data,
95 : : INCHI_IOSTREAM *fcb,
96 : : const char *name,
97 : : const char *comment,
98 : : const char *szLabel,
99 : : const char *szValue,
100 : : INT_ARRAY *written_bond_ends);
101 : :
102 : : int OrigAtData_WriteToSDfileAdditionalLines(const ORIG_ATOM_DATA *inp_at_data,
103 : : INCHI_IOSTREAM *fcb,
104 : : const char *name,
105 : : const char *comment,
106 : : int bAtomsDT,
107 : : const char *szLabel,
108 : : const char *szValue,
109 : : int nNumAliasLines,
110 : : int nNumChargeLines,
111 : : int nNumRadicalLines,
112 : : int nNumIsoLines,
113 : : INT_ARRAY *written_bond_ends);
114 : :
115 : : int OrigAtData_WriteToSDfilePolymerData(const ORIG_ATOM_DATA *inp_at_data,
116 : : INCHI_IOSTREAM *fcb,
117 : : const char *name,
118 : : const char *comment,
119 : : const char *szLabel,
120 : : const char *szValue,
121 : : INT_ARRAY *written_bond_ends);
122 : :
123 : : /****************************************************************************
124 : : Skip extra data ( != Molfile) which SDF contains
125 : : ****************************************************************************/
126 : 121 : int SDFileSkipExtraData(INCHI_IOSTREAM *inp_file,
127 : : unsigned long *CAS_num,
128 : : char *comment,
129 : : int lcomment,
130 : : char *name,
131 : : int lname,
132 : : int prev_err,
133 : : const char *pSdfLabel,
134 : : char *pSdfValue,
135 : : char *pStrErr,
136 : : int bNoWarnings)
137 : : {
138 : 121 : char *p = NULL;
139 : : char line[MOL_FMT_INPLINELEN];
140 : 121 : const int line_len = sizeof( line );
141 : 121 : int n_blank_lines = 0, n_lines = 0;
142 : 121 : int current_state = SDF_START;
143 : 121 : int err = 0;
144 : 121 : int wait_for_CAS = 0;
145 : 121 : int CAS_num_is_user = 0;
146 [ - + - - : 121 : int wait_for_name = name && lname > 0 && !name[0];
- - ]
147 [ - + - - : 121 : int wait_for_comment = comment && lcomment > 0 && !comment[0];
- - ]
148 [ - + - - : 121 : int wait_for_user = pSdfLabel && pSdfLabel[0] && pSdfValue;
- - ]
149 : :
150 [ + - ]: 121 : if (CAS_num != NULL)
151 : : {
152 : 121 : wait_for_CAS = 1;
153 : 121 : *CAS_num = 0LU;
154 [ - + - - ]: 121 : CAS_num_is_user = (wait_for_user && !inchi_memicmp(pSdfLabel, "CAS", 3));
155 : : }
156 : :
157 [ + + ]: 244 : while (!err &&
158 [ + - + + ]: 244 : current_state != SD_FMT_END_OF_DATA_BLOCK &&
159 : 121 : NULL != (p = inchi_fgetsLf(line, line_len, inp_file)))
160 : : {
161 [ + - - + ]: 2 : if (!n_lines && !memcmp(line, "M END", 6))
162 : : {
163 : : /* allow subtle errors */
164 : 0 : continue;
165 : : }
166 : :
167 : 2 : n_lines++;
168 : 2 : remove_trailing_spaces(line);
169 : :
170 [ - + ]: 2 : if (line[MOL_FMT_MAXLINELEN])
171 : : {
172 [ # # # # ]: 0 : if (current_state != SDF_DATA_HEADER &&
173 [ # # ]: 0 : current_state != SDF_DATA_LINE &&
174 [ # # ]: 0 : current_state != SDF_DATA_HEADER_NAME &&
175 [ # # ]: 0 : current_state != SDF_DATA_HEADER_USER &&
176 : : current_state != SDF_DATA_HEADER_COMMENT)
177 : : {
178 : 0 : line[MOL_FMT_MAXLINELEN] = '\0';
179 [ # # ]: 0 : if (!prev_err)
180 : : {
181 : 0 : TREAT_ERR(err, 0, "Too long SData line truncated");
182 : : }
183 : : }
184 : : else
185 : : {
186 : : /* allow long lines in SDF data. 9-29-00 DCh */
187 : 0 : line[MOL_FMT_MAXLINELEN] = '\0';
188 : : }
189 : : }
190 : :
191 : 2 : n_blank_lines += (*line == '\0');
192 : :
193 [ + - - - : 2 : switch (current_state)
- - - ]
194 : : {
195 : 2 : case SDF_START:
196 : : case SD_FMT_END_OF_DATA_ITEM:
197 : : case SDF_EMPTY_LINE: /* Added 9-25-97 DCh */
198 : :
199 [ + - ]: 2 : if (!strcmp(line, SD_FMT_END_OF_DATA))
200 : : {
201 : 2 : current_state = SD_FMT_END_OF_DATA_BLOCK;
202 : : }
203 [ # # ]: 0 : else if ('>' == *line)
204 : : {
205 [ # # # # : 0 : current_state = (wait_for_name || wait_for_comment || wait_for_CAS || wait_for_user) ? SDFileIdentifyLabel( line, pSdfLabel ) : SDF_DATA_HEADER;
# # # # ]
206 : : }
207 [ # # ]: 0 : else if (*line == '\0')
208 : : {
209 : : /* Added 9-25-97 DCh */
210 : : /* Relax the strictness: Allow more than 1 empty line. */
211 : 0 : current_state = SDF_EMPTY_LINE;
212 : : }
213 [ # # ]: 0 : else if (!prev_err)
214 : : {
215 [ # # ]: 0 : TREAT_ERR(err, 3, "Unexpected SData header line:"); /* djb-rwth: addressing coverity ID #499557 -- TREAT_ERR properly used */
216 : 0 : dotify_non_printable_chars(line);
217 : 0 : AddErrorMessage(pStrErr, line);
218 : : /* unexpected contents of data header line */
219 : : }
220 : : else
221 : : {
222 : 0 : err = 3;
223 : : }
224 : 2 : break;
225 : :
226 : 0 : case SDF_DATA_HEADER_NAME:
227 : :
228 [ # # # # ]: 0 : if (wait_for_name && 0 < normalize_string(line))
229 : : {
230 : 0 : wait_for_name = 0;
231 : 0 : mystrncpy(name, line, lname);
232 : : }
233 : 0 : goto got_data_line;
234 : :
235 : 0 : case SDF_DATA_HEADER_COMMENT:
236 : :
237 [ # # # # ]: 0 : if (wait_for_comment && 0 < normalize_string(line))
238 : : {
239 : 0 : wait_for_comment = 0;
240 : 0 : mystrncpy(comment, line, lcomment);
241 : : }
242 : 0 : goto got_data_line;
243 : :
244 : 0 : case SDF_DATA_HEADER_USER:
245 : :
246 [ # # # # ]: 0 : if (wait_for_user && 0 < normalize_string(line))
247 : : {
248 : 0 : wait_for_user = 0;
249 : 0 : mystrncpy(pSdfValue, line, MAX_SDF_VALUE + 1);
250 : :
251 [ # # # # ]: 0 : if (CAS_num_is_user && wait_for_CAS)
252 : : {
253 : 0 : *CAS_num = SDFileExtractCASNo(line);
254 : 0 : wait_for_CAS = (0LU == *CAS_num);
255 : : }
256 : : }
257 : 0 : goto got_data_line;
258 : :
259 : 0 : case SDF_DATA_HEADER_CAS:
260 : :
261 [ # # # # ]: 0 : if (wait_for_CAS && 0 < normalize_string(line))
262 : : {
263 : 0 : *CAS_num = SDFileExtractCASNo(line);
264 : 0 : wait_for_CAS = (0LU == *CAS_num);
265 : : }
266 : 0 : goto got_data_line;
267 : :
268 : : case SDF_DATA_HEADER:
269 : : case SDF_DATA_LINE:
270 : :
271 : 0 : got_data_line:
272 [ # # ]: 0 : current_state = *line ? SDF_DATA_LINE : SD_FMT_END_OF_DATA_ITEM;
273 : 0 : break;
274 : : }
275 : : }
276 : :
277 [ + - + + : 121 : if (!err && SD_FMT_END_OF_DATA_BLOCK != current_state && NULL == p)
+ - ]
278 : : {
279 : : ; /* err = 4; */ /* unexpected end of file: missing $$$$ */
280 : : }
281 : :
282 [ - + - - : 2 : else if (err && (n_blank_lines == n_lines && *line == '\0'))
- - ]
283 : : {
284 : : /* empty lines -- do not know when this can happen */
285 : 0 : err = 5;
286 : : }
287 : :
288 [ - + - - : 121 : if (err && err != 5 && current_state != SD_FMT_END_OF_DATA_BLOCK && p)
- - - - ]
289 : : {
290 : : /* bypass up to $$$$ */
291 [ # # ]: 0 : while ((p = inchi_fgetsLf(line, line_len, inp_file)) &&
292 [ # # ]: 0 : memcmp(line, SD_FMT_END_OF_DATA, 4))
293 : : {
294 : : ;
295 : : }
296 [ # # ]: 0 : if (p)
297 : : {
298 : : /* arrived to $$$$; non-fatal */
299 : 0 : err = 9;
300 [ # # ]: 0 : if (!bNoWarnings)
301 : : {
302 : 0 : WarningMessage(pStrErr, "Bypassing to next structure");
303 : : }
304 : : }
305 : : }
306 : :
307 : 121 : return err;
308 : : }
309 : :
310 : : /****************************************************************************/
311 : 0 : int SDFileIdentifyLabel(char *inp_line, const char *pSdfLabel)
312 : : {
313 : : char line[MOL_FMT_MAXLINELEN];
314 : : char *p, *q;
315 : 0 : int i, j, len, tmp1 = 0, tmp2 = 0, cnd = 0;
316 : :
317 [ # # ]: 0 : if ((p = strchr(inp_line, '<')) &&
318 [ # # ]: 0 : (q = strchr(p, '>')) &&
319 [ # # # # ]: 0 : (len = q - p - 1) > 0 && len < (int)sizeof(line))
320 : : {
321 : 0 : memcpy(line, p + 1, len);
322 : 0 : line[len] = '\0';
323 : :
324 [ # # # # ]: 0 : for (i = 0; (i < len) && (cnd == 0); i++)
325 : : {
326 [ # # ]: 0 : if (isspace(UCINT line[i]))
327 : : {
328 : 0 : tmp1 = i;
329 : 0 : cnd = 1;
330 : : }
331 : : }
332 : :
333 : 0 : cnd = 0;
334 : :
335 [ # # # # ]: 0 : for (j = len - 1; (j >= tmp1) && (cnd == 0); j--)
336 : : {
337 [ # # ]: 0 : if (isspace(UCINT line[j]))
338 : : {
339 : 0 : tmp2 = j;
340 : 0 : cnd = 1;
341 : : }
342 : : }
343 : :
344 : 0 : len = tmp2 - tmp1 + 1;
345 : 0 : p = line + tmp1;
346 : :
347 [ # # # # : 0 : if (pSdfLabel && pSdfLabel[0] && len == (int)strlen(pSdfLabel) && !inchi_memicmp(p, pSdfLabel, len))
# # # # ]
348 : : {
349 : 0 : return SDF_DATA_HEADER_USER;
350 : : }
351 : :
352 [ # # # # ]: 0 : if (len == sizeof(sdf_data_hdr_name) - 1 && !inchi_memicmp(p, sdf_data_hdr_name, len))
353 : : {
354 : 0 : return SDF_DATA_HEADER_NAME;
355 : : }
356 : :
357 [ # # # # ]: 0 : if (len == sizeof(sdf_data_hdr_comm) - 1 && !inchi_memicmp(p, sdf_data_hdr_comm, len))
358 : : {
359 : 0 : return SDF_DATA_HEADER_COMMENT;
360 : : }
361 : :
362 [ # # ]: 0 : if (!inchi_memicmp(p, "CAS", 3))
363 : : {
364 : 0 : return SDF_DATA_HEADER_CAS;
365 : : }
366 : : }
367 : :
368 : 0 : return SDF_DATA_HEADER;
369 : : }
370 : :
371 : : /****************************************************************************/
372 : 0 : unsigned long SDFileExtractCASNo(char *line)
373 : : {
374 : : int i, j;
375 : :
376 : 0 : i = line[0] == '-' ? 1 : 0;
377 : :
378 [ # # ]: 0 : for (j = i; line[i]; i++)
379 : : {
380 [ # # ]: 0 : if (isdigit(UCINT line[i]))
381 : : {
382 : 0 : line[j++] = line[i];
383 : : }
384 [ # # ]: 0 : else if (line[i] != '-')
385 : : {
386 : 0 : break;
387 : : }
388 : : }
389 : :
390 : 0 : line[j] = '\0';
391 : :
392 : 0 : return strtoul(line, NULL, 10);
393 : : }
394 : :
395 : : /****************************************************************************
396 : : NUM_LISTS - dynamically growing array of int lists
397 : : ****************************************************************************/
398 : 228 : int NumLists_Alloc(NUM_LISTS *num_lists, int nlists)
399 : : {
400 [ + - ]: 228 : if (num_lists)
401 : : {
402 [ + - ]: 228 : if ((num_lists->lists = (int **)inchi_calloc(nlists, sizeof(int *)))) /* djb-rwth: addressing LLVM warning */
403 : : {
404 : 228 : num_lists->increment =
405 : 228 : num_lists->allocated = nlists;
406 : 228 : return 0; /* ok */
407 : : }
408 : : }
409 : :
410 : 0 : return -1; /* error */
411 : : }
412 : :
413 : : /****************************************************************************/
414 : 2 : int NumLists_ReAlloc(NUM_LISTS *num_lists)
415 : : {
416 [ + - ]: 2 : if (num_lists)
417 : : {
418 [ + - + - : 2 : if (num_lists->lists && num_lists->allocated > 0 && num_lists->increment > 0)
+ - ]
419 : : {
420 : 2 : void *p = num_lists->lists;
421 : 2 : if ((num_lists->lists =
422 [ + - ]: 2 : (int **)inchi_calloc((long long)num_lists->allocated + (long long)num_lists->increment, sizeof(int *)))) /* djb-rwth: cast operators added; addressing LLVM warning */
423 : : {
424 : 2 : memcpy(num_lists->lists, p, num_lists->used * sizeof(num_lists->lists[0]));
425 [ + - ]: 2 : inchi_free(p);
426 : 2 : num_lists->allocated += num_lists->increment;
427 : 2 : return 0; /* ok */
428 : : }
429 : : }
430 : : }
431 : :
432 : 0 : return -1; /* error */
433 : : }
434 : :
435 : : /****************************************************************************/
436 : 104 : int NumLists_Append(NUM_LISTS *num_lists, int *list)
437 : : {
438 [ + - ]: 104 : if (num_lists)
439 : : {
440 [ + + ]: 104 : if (num_lists->used + 1 > num_lists->allocated)
441 : : {
442 : : /* need to expand buffer */
443 [ - + ]: 2 : if (NumLists_ReAlloc(num_lists))
444 : : {
445 : 0 : return -1; /* error */
446 : : }
447 : : }
448 : 104 : num_lists->lists[num_lists->used++] = list;
449 : 104 : return 0;
450 : : }
451 : :
452 : 0 : return -1;
453 : : }
454 : :
455 : : /****************************************************************************/
456 : 228 : void NumLists_Free(NUM_LISTS *num_lists)
457 : : {
458 [ + - ]: 228 : if (num_lists)
459 : : {
460 : : int i;
461 [ + + ]: 332 : for (i = 0; i < num_lists->used; i++)
462 [ + - ]: 104 : inchi_free(num_lists->lists[i]); /* djb-rwth: unresolved issue -- revision required? -- false positive as this function just does the clean-up job */
463 [ + - ]: 228 : inchi_free(num_lists->lists);
464 : 228 : memset(num_lists, 0, sizeof(*num_lists)); /* djb-rwth: memset_s C11/Annex K variant? */
465 : : }
466 : 228 : }
467 : :
468 : : /****************************************************************************
469 : : INT_ARRAY - dynamically growing array of int
470 : : ****************************************************************************/
471 : :
472 : : /****************************************************************************
473 : : Allocate new array, return 0 if OK, -1 otherwise
474 : : ****************************************************************************/
475 : 1212 : int IntArray_Alloc(INT_ARRAY *items, int nitems)
476 : : {
477 [ + - ]: 1212 : if ((items->item = (int *)inchi_calloc(nitems, sizeof(int)))) /* djb-rwth: addressing LLVM warning */
478 : : {
479 : 1212 : items->increment = items->allocated = nitems;
480 : 1212 : items->used = 0;
481 : 1212 : return 0;
482 : : }
483 : :
484 : 0 : return -1;
485 : : }
486 : :
487 : : /****************************************************************************
488 : : Expand array, return 0 if OK, -1 otherwise
489 : : ****************************************************************************/
490 : 0 : int IntArray_ReAlloc(INT_ARRAY *items)
491 : : {
492 [ # # ]: 0 : if (items)
493 : : {
494 [ # # # # : 0 : if (items->item && items->allocated > 0 && items->increment > 0)
# # ]
495 : : {
496 : 0 : void *p = items->item;
497 : 0 : if ((items->item =
498 [ # # ]: 0 : (int *)inchi_calloc((long long)items->allocated + (long long)items->increment, sizeof(items->item[0])))) /* djb-rwth: cast operators added; addressing LLVM warning */
499 : : {
500 : 0 : memcpy(items->item, p, items->used * sizeof(items->item[0]));
501 [ # # ]: 0 : inchi_free(p);
502 : 0 : items->allocated += items->increment;
503 : 0 : return 0;
504 : : }
505 [ # # ]: 0 : inchi_free(p);
506 : : }
507 : : }
508 : :
509 : 0 : return -1;
510 : : }
511 : :
512 : : /****************************************************************************
513 : : Push new item to the end of array
514 : : ****************************************************************************/
515 : 5 : int IntArray_Append(INT_ARRAY *items, int new_item)
516 : : {
517 [ + - ]: 5 : if (items)
518 : : {
519 [ - + ]: 5 : if (items->used + 1 > items->allocated)
520 : : {
521 : : /* need to expand buffer */
522 [ # # ]: 0 : if (IntArray_ReAlloc(items))
523 : : {
524 : 0 : return -1;
525 : : }
526 : : }
527 : 5 : items->item[items->used++] = new_item;
528 : 5 : return 0;
529 : : }
530 : :
531 : 0 : return -1;
532 : : }
533 : :
534 : : /****************************************************************************
535 : : Push new item to the end of array only if it is absent there
536 : : ****************************************************************************/
537 : 0 : int IntArray_AppendIfAbsent(INT_ARRAY *items, int new_item)
538 : : {
539 [ # # ]: 0 : if (!is_in_the_ilist(items->item, new_item, items->used))
540 : : {
541 : 0 : return IntArray_Append(items, new_item);
542 : : }
543 : 0 : return 0;
544 : : }
545 : :
546 : : /****************************************************************************/
547 : 0 : void IntArray_DebugPrint(INT_ARRAY *items)
548 : : {
549 [ # # ]: 0 : if (items)
550 : : {
551 : : int i;
552 [ # # ]: 0 : if (items->used > 0)
553 : : {
554 [ # # ]: 0 : for (i = 0; i < items->used - 1; i++)
555 : : {
556 : : ITRACE_("%-d, ", items->item[i]);
557 : : }
558 : : ITRACE_("%-d\n", items->item[items->used - 1]);
559 : : }
560 : : else
561 : : {
562 : : ; /*ITRACE_( "[None]\n");*/
563 : : }
564 : : }
565 : 0 : }
566 : :
567 : : /****************************************************************************/
568 : 0 : void IntArray_Reset(INT_ARRAY *items)
569 : : {
570 : 0 : items->used = 0;
571 : 0 : return;
572 : : }
573 : :
574 : : /****************************************************************************/
575 : 1212 : void IntArray_Free(INT_ARRAY *items)
576 : : {
577 [ + - ]: 1212 : if (items)
578 : : {
579 [ + - ]: 1212 : if (items->item)
580 : : {
581 [ + - ]: 1212 : inchi_free(items->item);
582 : : }
583 : 1212 : memset(items, 0, sizeof(*items)); /* djb-rwth: memset_s C11/Annex K variant? */
584 : : }
585 : 1212 : return;
586 : : }
587 : :
588 : : /****************************************************************************
589 : : MOL_FMT_SGROUPS - dynamically growing array of pointers to SGroups
590 : : ****************************************************************************/
591 : :
592 : : /*
593 : : SGroup
594 : : */
595 : :
596 : : /****************************************************************************
597 : : Allocate new array Sgroup, return 0 if OK, -1 otherwise
598 : : ****************************************************************************/
599 : 1 : int MolFmtSgroup_Create(MOL_FMT_SGROUP **sgroup, int id, int type)
600 : : {
601 : 1 : *sgroup = (MOL_FMT_SGROUP *)inchi_calloc(1, sizeof(MOL_FMT_SGROUP));
602 [ + - ]: 1 : if (*sgroup)
603 : : {
604 [ + - - + ]: 2 : if (IntArray_Alloc(&((*sgroup)->alist), 8) ||
605 : 1 : IntArray_Alloc(&((*sgroup)->blist), 8))
606 : : {
607 : 0 : MolFmtSgroup_Free(*sgroup);
608 : 0 : return -1;
609 : : }
610 : 1 : (*sgroup)->id = id;
611 : 1 : (*sgroup)->type = type;
612 : :
613 : 1 : (*sgroup)->subtype = 0;
614 : 1 : (*sgroup)->conn = 0;
615 : 1 : (*sgroup)->label = 0;
616 : :
617 : 1 : return 0;
618 : : }
619 : 0 : return -1;
620 : : }
621 : :
622 : : /****************************************************************************/
623 : 1 : void MolFmtSgroup_Free(MOL_FMT_SGROUP *sgroup)
624 : : {
625 [ + - ]: 1 : if (sgroup)
626 : : {
627 : 1 : IntArray_Free(&(sgroup->alist));
628 : 1 : IntArray_Free(&(sgroup->blist));
629 [ + - ]: 1 : inchi_free(sgroup);
630 : : }
631 : 1 : }
632 : :
633 : : /*
634 : : SGroups
635 : : */
636 : :
637 : : /****************************************************************************
638 : : Allocate new array of Sgroups, return 0 if OK, -1 otherwise
639 : : ****************************************************************************/
640 : 121 : int MolFmtSgroups_Alloc(MOL_FMT_SGROUPS *sgroups, int nsgroups)
641 : : {
642 [ + - ]: 121 : if (sgroups)
643 : : {
644 [ + - ]: 121 : if (NULL != (sgroups->group = (MOL_FMT_SGROUP **)inchi_calloc(nsgroups, sizeof(MOL_FMT_SGROUP *))))
645 : : {
646 : : /* ITRACE_( "\nAllocated sgroups->group at %-p \n", sgroups->group ); */
647 : 121 : sgroups->increment = sgroups->allocated = nsgroups;
648 : 121 : return 0;
649 : : }
650 : : }
651 : :
652 : 0 : return -1;
653 : : }
654 : :
655 : : /****************************************************************************
656 : : Expand array of Sgroups, return 0 if OK, -1 otherwise
657 : : ****************************************************************************/
658 : 0 : int MolFmtSgroups_ReAlloc(MOL_FMT_SGROUPS *sgroups)
659 : : {
660 [ # # ]: 0 : if (sgroups)
661 : : {
662 [ # # # # : 0 : if (sgroups->group && sgroups->allocated > 0 && sgroups->increment > 0)
# # ]
663 : : {
664 : 0 : void *p = sgroups->group;
665 [ # # ]: 0 : if ((sgroups->group = (MOL_FMT_SGROUP **)inchi_calloc( (long long)sgroups->allocated + (long long)sgroups->increment,
666 : : sizeof( sgroups->group[0] ) ))) /* djb-rwth: cast operators added; addressing LLVM warning */
667 : : {
668 : 0 : memcpy(sgroups->group, p, sgroups->used * sizeof(sgroups->group[0]));
669 [ # # ]: 0 : inchi_free(p);
670 : 0 : sgroups->allocated += sgroups->increment;
671 : 0 : return 0; /* ok */
672 : : }
673 : : }
674 : : }
675 : :
676 : 0 : return -1;
677 : : }
678 : :
679 : : /****************************************************************************/
680 : 1 : int MolFmtSgroups_Append(MOL_FMT_SGROUPS *sgroups, int id, int type)
681 : : {
682 [ + - ]: 1 : if (sgroups)
683 : : {
684 : : /* Make new Sgroup */
685 : 1 : MOL_FMT_SGROUP *sgroup = NULL;
686 [ - + ]: 1 : if (0 != MolFmtSgroup_Create(&sgroup, id, type))
687 : : {
688 : 0 : return -1;
689 : : }
690 : : /* Add new created Sgroup to Sgroups */
691 [ - + ]: 1 : if (sgroups->used + 1 > sgroups->allocated)
692 : : {
693 : : /* expand buffer */
694 [ # # ]: 0 : if (MolFmtSgroups_ReAlloc(sgroups))
695 : : {
696 : 0 : MolFmtSgroup_Free(sgroup); /* djb-rwth: avoiding memory leak */
697 : 0 : return -1; /* no RAM */
698 : : }
699 : : }
700 : 1 : sgroups->group[sgroups->used++] = sgroup;
701 : :
702 : : /*
703 : : {
704 : : int num = sgroups->used-1;
705 : : printf("\nCreated/added Sgroup: id=%-d ( num in Sgroups=%-d ) of type=%-d \n", sgroups->group[num]->id, num, sgroups->group[num]->type );
706 : : }
707 : : */
708 : :
709 : 1 : return 0;
710 : : }
711 : :
712 : 0 : return -1;
713 : : }
714 : :
715 : : /****************************************************************************/
716 : 121 : void MolFmtSgroups_Free(MOL_FMT_SGROUPS *sgroups)
717 : : {
718 [ + - ]: 121 : if (sgroups)
719 : : {
720 : : int i;
721 [ + + ]: 122 : for (i = 0; i < sgroups->used; i++)
722 : : {
723 : 1 : MolFmtSgroup_Free(sgroups->group[i]);
724 : : }
725 : :
726 : : /* ITRACE_( "\nAbout to free sgroups->group at %-p\n", sgroups->group ); */
727 [ + - ]: 121 : inchi_free(sgroups->group);
728 : :
729 : 121 : memset(sgroups, 0, sizeof(MOL_FMT_SGROUPS)); /* djb-rwth: memset_s C11/Annex K variant? */
730 : : }
731 : 121 : }
732 : :
733 : : /****************************************************************************/
734 : 8 : int MolFmtSgroups_GetIndexBySgroupId(int id, MOL_FMT_SGROUPS *sgroups)
735 : : {
736 : : int i;
737 [ + + ]: 8 : for (i = 0; i < sgroups->used; i++)
738 : : {
739 [ + - ]: 7 : if (sgroups->group[i]->id == id)
740 : : {
741 : 7 : return i;
742 : : }
743 : : }
744 : 1 : return -1;
745 : : }
746 : :
747 : : /****************************************************************************/
748 : 0 : int OrigAtData_WriteToSDfile(const ORIG_ATOM_DATA *inp_at_data,
749 : : INCHI_IOSTREAM *fcb,
750 : : const char *name,
751 : : const char *comment,
752 : : int bChiralFlag,
753 : : int bAtomsDT,
754 : : const char *szLabel,
755 : : const char *szValue)
756 : : {
757 : 0 : int num_bonds = 0, nNumAddLines = 0, nNumIsoLines = 0, nNumChargeLines = 0,
758 : 0 : nNumRadicalLines = 0, nNumAliasLines = 0, ret = 0;
759 : : INT_ARRAY written_bond_ends;
760 : :
761 : 0 : OrigAtData_WriteToSDfileHeaderAndCountThings((ORIG_ATOM_DATA *)inp_at_data,
762 : : fcb, name, comment,
763 : : bChiralFlag, bAtomsDT,
764 : : szLabel, szValue,
765 : : &nNumAliasLines,
766 : : &nNumChargeLines,
767 : : &nNumRadicalLines,
768 : : &nNumIsoLines,
769 : : &nNumAddLines,
770 : : &num_bonds);
771 : :
772 [ # # # # ]: 0 : if (IntArray_Alloc(&written_bond_ends, num_bonds ? num_bonds : 255))
773 : : {
774 : 0 : ret = _IS_ERROR;
775 : 0 : goto exit_function;
776 : : }
777 : :
778 : 0 : OrigAtData_WriteToSDfileAtomsBlock(inp_at_data, fcb, name, comment,
779 : : bAtomsDT, szLabel, szValue);
780 : :
781 : 0 : OrigAtData_WriteToSDfileBondsBlock(inp_at_data, fcb, name, comment,
782 : : szLabel, szValue, &written_bond_ends);
783 : :
784 [ # # ]: 0 : if (nNumAddLines)
785 : : {
786 : 0 : OrigAtData_WriteToSDfileAdditionalLines(inp_at_data, fcb, name, comment,
787 : : bAtomsDT, szLabel, szValue,
788 : : nNumAliasLines, nNumChargeLines,
789 : : nNumRadicalLines, nNumIsoLines,
790 : : &written_bond_ends);
791 : : }
792 : :
793 : : /* Add field with label/ID if applicable and mark the end of record */
794 [ # # # # ]: 0 : if (szValue && szValue[0])
795 : : {
796 [ # # # # ]: 0 : if (szLabel && szLabel[0])
797 : : {
798 : 0 : inchi_ios_print_nodisplay(fcb, "> <%s>\n", szLabel);
799 : : }
800 : : else
801 : : {
802 : 0 : inchi_ios_print_nodisplay(fcb, "> <ID>\n");
803 : : }
804 : 0 : inchi_ios_print_nodisplay(fcb, " %s\n\n", szValue);
805 : : }
806 : 0 : inchi_ios_print_nodisplay(fcb, "$$$$\n");
807 : :
808 : 0 : exit_function:
809 : 0 : IntArray_Free(&written_bond_ends);
810 : :
811 : 0 : return ret;
812 : : }
813 : :
814 : : /****************************************************************************
815 : : OrigAtData : Write To SDfile : Atoms Block
816 : : ****************************************************************************/
817 : 0 : int OrigAtData_WriteToSDfileHeaderAndCountThings(const ORIG_ATOM_DATA *inp_at_data,
818 : : INCHI_IOSTREAM *fcb,
819 : : const char *name,
820 : : const char *comment,
821 : : int bChiralFlag,
822 : : int bAtomsDT,
823 : : const char *szLabel,
824 : : const char *szValue,
825 : : int *nNumAliasLines,
826 : : int *nNumChargeLines,
827 : : int *nNumRadicalLines,
828 : : int *nNumIsoLines,
829 : : int *nNumAddLines,
830 : : int *num_bonds)
831 : : {
832 : 0 : int i, ret = 0;
833 : : int bAtomNeedsAlias,
834 : 0 : nNumNecessaryIsoLines = 0,
835 : 0 : nNumNecessaryChgLines = 0,
836 : 0 : nNumNecessaryRadLines = 0;
837 : 0 : int num_atoms = inp_at_data->num_inp_atoms;
838 : 0 : int bV2000 = SDF_OUTPUT_V2000;
839 : 0 : const inp_ATOM *at = inp_at_data->at;
840 : :
841 : : {
842 : : char strLocName[82];
843 : 0 : memset(strLocName, 0, sizeof(strLocName)); /* djb-rwth: memset_s C11/Annex K variant? */
844 [ # # # # ]: 0 : if (name && *name)
845 : : {
846 : 0 : strncpy(strLocName, name, 80);
847 : : }
848 : 0 : inchi_ios_print_nodisplay(fcb, "%s\n", strLocName);
849 : : }
850 : :
851 : : /**********************************************************************/
852 : : /** **/
853 : : /** Important: Atoms with alias cannot have charge, radical **/
854 : : /** isotope differences are allowed **/
855 : : /** **/
856 : : /** Atoms with alias cannot be abnormal. **/
857 : : /** **/
858 : : /** Abnormal atoms are atoms which need M CHG, M RAD, M ISO **/
859 : : /** **/
860 : : /** Output aliased atoms if they have implicit D or T **/
861 : : /** **/
862 : : /**********************************************************************/
863 : :
864 : : /* F10.5 F12.5 I6
865 : : IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR
866 : : inchi_ios_eprint( fcb,"NISTTRANHP09089809272D 1 1.0 0.0 %6ld\n", lEpa);*/
867 : : /*^^^
868 : : inchi_ios_print_nodisplay( fcb," %s v%s SDfile Output \n", INCHI_NAME, INCHI_VERSION);
869 : :
870 : : Changed 01/10/2009 to conform CTFile specification (by Symyx request)*/
871 : :
872 : 0 : inchi_ios_print_nodisplay(fcb,
873 : : /* IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR*/
874 : : " InChIV10 \n");
875 : : /*y_fprintf(fcb, " -CPSS- 1213981200n\n");*/
876 : :
877 : : {
878 : : char strLocName[82];
879 : :
880 : 0 : memset(strLocName, 0, sizeof(strLocName)); /* djb-rwth: memset_s C11/Annex K variant? */
881 [ # # # # ]: 0 : if (comment && *comment)
882 : : {
883 : 0 : strncpy(strLocName, comment, 80);
884 : : }
885 : 0 : inchi_ios_print_nodisplay(fcb, "%s\n", strLocName);
886 : : }
887 : :
888 : 0 : *num_bonds = 0;
889 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
890 : : {
891 : 0 : (*num_bonds) += at[i].valence;
892 : : }
893 : 0 : (*num_bonds) /= 2;
894 : :
895 : : /*find if we need "M CHG" and "M RAD"*/
896 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
897 : : {
898 [ # # ]: 0 : if ((bAtomNeedsAlias = ALIASED_AT(i))) /* djb-rwth: addressing LLVM warning */
899 : : {
900 : : /* has isotopic implicit D or T; ignoring pure 1H */
901 : 0 : (*nNumAliasLines) += 2 * bAtomNeedsAlias;
902 : : }
903 : : else
904 : : {
905 : : /* abnormal means atom needs CHG, RAD, or ISO entry */
906 : : /* nNumAddLines += ABNORMAL_AT(i); */
907 : : /* nNumIso += ( 0 == strcmp( at[i].elname, "D" ) || ( 0 == strcmp( at[i].elname, "T" ) || at[i].iso_atw_diff ) ); */
908 : : /* nNumAddIso += at[i].iso_atw_diff && (at[i].iso_atw_diff == 1 || at[i].iso_atw_diff < -3 || at[i].iso_atw_diff > 5 ); */
909 [ # # # # : 0 : nNumNecessaryIsoLines += ABNORMAL_ISO(i);
# # ]
910 : 0 : nNumNecessaryChgLines += ABNORMAL_CHG(i);
911 [ # # # # ]: 0 : nNumNecessaryRadLines += ABNORMAL_RAD(i);
912 [ # # # # : 0 : (*nNumIsoLines) += ANY_ISO(i, bAtomsDT);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
913 : 0 : (*nNumChargeLines) += ANY_CHG(i);
914 [ # # # # ]: 0 : (*nNumRadicalLines) += ANY_RAD(i);
915 : : }
916 : : }
917 : :
918 : 0 : *nNumChargeLines = (*nNumChargeLines + 7) / 8;
919 : 0 : *nNumRadicalLines = (*nNumRadicalLines + 7) / 8;
920 : 0 : *nNumIsoLines = (*nNumIsoLines + 7) / 8;
921 : :
922 [ # # ]: 0 : if (!bV2000)
923 : : {
924 [ # # # # ]: 0 : if (!nNumNecessaryRadLines && !nNumNecessaryChgLines)
925 : : {
926 : 0 : *nNumRadicalLines = 0;
927 : 0 : *nNumChargeLines = 0;
928 : : }
929 [ # # ]: 0 : if (!nNumNecessaryIsoLines)
930 : : {
931 : 0 : *nNumIsoLines = 0;
932 : : }
933 : : }
934 : :
935 : : /* recalculate number of added lines */
936 : 0 : *nNumAddLines = *nNumChargeLines + *nNumRadicalLines + *nNumIsoLines + *nNumAliasLines; /* 1 for M END*/
937 : :
938 [ # # # # ]: 0 : if (*nNumAddLines || bV2000)
939 : : {
940 : 0 : *nNumAddLines += 1; /* add 1 for "M END" line*/
941 : : }
942 : :
943 : : /* aaabbblllfffcccsssxxxrrrpppiiimmmvvvvvv */
944 : :
945 : 0 : inchi_ios_print_nodisplay(fcb, "%3d%3d 0 0%3d 0 0 0 0 0%3d%s\n",
946 [ # # ]: 0 : num_atoms, *num_bonds, bChiralFlag ? 1 : 0, *nNumAddLines, *nNumAddLines ? " V2000" : "");
947 : :
948 : 0 : return ret;
949 : : }
950 : :
951 : : /****************************************************************************
952 : : OrigAtData : Write To SDfile : Atoms Block
953 : : ****************************************************************************/
954 : 0 : int OrigAtData_WriteToSDfileAtomsBlock(const ORIG_ATOM_DATA *inp_at_data,
955 : : INCHI_IOSTREAM *fcb,
956 : : const char *name,
957 : : const char *comment,
958 : : int bAtomsDT,
959 : : const char *szLabel,
960 : : const char *szValue)
961 : : {
962 : 0 : int i, ret = 0;
963 : : int bAtomNeedsAlias;
964 : : /* djb-rwth: removing redundant variables */
965 : 0 : int num_atoms = inp_at_data->num_inp_atoms;
966 : 0 : const inp_ATOM *at = inp_at_data->at;
967 : : double x, y, z;
968 : :
969 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
970 : : {
971 : 0 : char elname[ATOM_EL_LEN] = "\0\0\0\0\0";
972 : 0 : int iso = 0;
973 : 0 : int charge = 0;
974 : 0 : int valence = 0;
975 [ # # # # : 0 : int nIsotopeH = IS_DEUTERIUM( i ) ? 1 : IS_TRITIUM( i ) ? 2 : 0;
# # # # #
# # # ]
976 : : int bonds_val;
977 : 0 : bAtomNeedsAlias = ALIASED_AT( i );
978 : 0 : memset( elname, 0, sizeof( elname ) ); /* djb-rwth: memset_s C11/Annex K variant? */
979 : :
980 [ # # ]: 0 : if (bAtomNeedsAlias)
981 : : {
982 : : /* alias */
983 : 0 : strcpy(elname, "C");
984 : : }
985 : : else
986 : : {
987 : : /* isotope*/
988 [ # # ]: 0 : if (nIsotopeH)
989 : : {
990 [ # # # # ]: 0 : strcpy(elname, bAtomsDT ? (nIsotopeH == 1 ? "D" : "T") : "H");
991 : : }
992 : : else
993 : : {
994 : 0 : strncpy(elname, at[i].elname, sizeof(elname) - 1);
995 : 0 : elname[sizeof(elname) - 1] = 0; /* adding zero termination after strncpy */
996 : : }
997 [ # # # # : 0 : if (!ABNORMAL_CHG(i) && !ANY_RAD(i))
# # ]
998 : : {
999 : : /* charge*/
1000 : : /* Only atoms without alias can be here*/
1001 [ # # # # : 0 : switch (at[i].charge)
# # # # ]
1002 : : {
1003 : 0 : case 3:
1004 : 0 : charge = 1;
1005 : 0 : break;
1006 : 0 : case 2:
1007 : 0 : charge = 2;
1008 : 0 : break;
1009 : 0 : case 1:
1010 : 0 : charge = 3;
1011 : 0 : break;
1012 : 0 : case -1:
1013 : 0 : charge = 5;
1014 : 0 : break;
1015 : 0 : case -2:
1016 : 0 : charge = 6;
1017 : 0 : break;
1018 : 0 : case -3:
1019 : 0 : charge = 7;
1020 : 0 : break;
1021 : 0 : case 0:
1022 : 0 : charge = 0;
1023 : 0 : break;
1024 : 0 : default:
1025 : 0 : break; /* djb-rwth: removing redundant code */
1026 : : }
1027 : : }
1028 : :
1029 : : /* radical*/
1030 [ # # # # : 0 : if (ANY_RAD(i) && !ANY_CHG(i))
# # ]
1031 : : {
1032 [ # # ]: 0 : if (at[i].radical == RADICAL_DOUBLET)
1033 : : {
1034 : 0 : charge = 4;
1035 : : }
1036 : : }
1037 : : }
1038 : :
1039 : : /* allow isotopic shift for aliased atoms */
1040 [ # # # # : 0 : if (NORMAL_ISO(i, bAtomsDT))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1041 : : {
1042 [ # # ]: 0 : iso = at[i].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 :
1043 [ # # ]: 0 : at[i].iso_atw_diff < 0 ? at[i].iso_atw_diff :
1044 : : nIsotopeH ? nIsotopeH : 0; /* djb-rwth: removing redundant code */
1045 : : }
1046 : :
1047 : 0 : x = at[i].x;
1048 : 0 : y = at[i].y;
1049 : 0 : z = at[i].z;
1050 : :
1051 : : /* valence -- set only if needed */
1052 : 0 : bonds_val = nBondsValenceInpAt(at + i, NULL, NULL);
1053 : 0 : valence = needed_unusual_el_valence(at[i].el_number, at[i].charge, at[i].radical,
1054 : 0 : at[i].chem_bonds_valence, bonds_val, NUMH(at, i), at[i].valence);
1055 : :
1056 [ # # ]: 0 : if (valence < 0)
1057 : : {
1058 : 0 : valence = 15; /* means no bonds nor H */
1059 : : }
1060 : :
1061 : : /* Convert "Zz" to "*" element symbol */
1062 : :
1063 [ # # # # ]: 0 : if (!strcmp(elname, "Zz") || !strcmp(elname, "Zy"))
1064 : : {
1065 : 0 : strcpy(elname, "*");
1066 : : }
1067 : :
1068 : : /*inchi_ios_eprint(fcb,"%10.4f%10.4f%10.4f %-3.3s%2d%3d 0 0 0 0 0 0 0\n",*/
1069 : : /* (float)at[i].x, (float)(-at[i].y), fzero, at[i].elname, iso, charge);*/
1070 : : /* xxxxxxyyyyyyzzzzzz aaa____ddcccsssnnnbbbvvvrrriiimmmeee */
1071 : 0 : inchi_ios_print_nodisplay(fcb, "%10.4f%10.4f%10.4f %-3.3s%2d%3d 0 0%3d 0 0 0 0\n",
1072 : : x, y, z, elname, (int)iso, (int)charge, valence /* at[i].special*/);
1073 : :
1074 : : /* Reflect image against x-axis; */
1075 : : /* when transforming MOLfile back to STDATA in mol_to_stdata(...), */
1076 : : /* make one more reflection to restore original orientation. */
1077 : : /* Reason: in MS Search y-axis is directed from top to bottom, */
1078 : : /* while in MOLfile y-axis goes from bottom to top. */
1079 : : }
1080 : :
1081 : 0 : return ret;
1082 : : }
1083 : :
1084 : : /****************************************************************************
1085 : : OrigAtData : Write To SDfile : Bonds Block
1086 : : ****************************************************************************/
1087 : 0 : int OrigAtData_WriteToSDfileBondsBlock( const ORIG_ATOM_DATA *inp_at_data,
1088 : : INCHI_IOSTREAM *fcb,
1089 : : const char *name,
1090 : : const char *comment,
1091 : : const char *szLabel,
1092 : : const char *szValue,
1093 : : INT_ARRAY *written_bond_ends )
1094 : : {
1095 : 0 : int i, j, k, ret = 0;
1096 : 0 : int num_atoms = inp_at_data->num_inp_atoms;
1097 : 0 : const inp_ATOM *at = inp_at_data->at;
1098 : :
1099 : : /* bonds*/
1100 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1101 : : {
1102 [ # # ]: 0 : for (j = 0; j < at[i].valence; j++)
1103 : : {
1104 [ # # ]: 0 : if (i < at[i].neighbor[j])
1105 : : {
1106 : : unsigned a1, a2;
1107 [ # # ]: 0 : if ((k = at[i].bond_stereo[j])) /* djb-rwth: addressing LLVM warning */
1108 : : {
1109 : : /* bond stereo */
1110 [ # # ]: 0 : if (k < 0)
1111 : : {
1112 : : /* transposition */
1113 : 0 : a1 = (unsigned)(at[i].neighbor[j] + 1);
1114 : 0 : a2 = (unsigned)(i + 1);
1115 : 0 : inchi_ios_print_nodisplay(fcb, "%3u%3u%3u%3u 0 0 0\n",
1116 : 0 : a1, a2, (unsigned)(at[i].bond_type[j]), (unsigned)abs(k));
1117 : : }
1118 : : else
1119 : : {
1120 : : /* no transposition*/
1121 : 0 : a1 = (unsigned)(i + 1);
1122 : 0 : a2 = (unsigned)(at[i].neighbor[j] + 1);
1123 : 0 : inchi_ios_print_nodisplay(fcb, "%3u%3u%3u%3u 0 0 0\n",
1124 : 0 : a1, a2, (unsigned)(at[i].bond_type[j]), (unsigned)abs(k));
1125 : : }
1126 : : }
1127 : : else
1128 : : {
1129 : 0 : a1 = (unsigned)(i + 1);
1130 : 0 : a2 = (unsigned)(at[i].neighbor[j] + 1);
1131 : 0 : inchi_ios_print_nodisplay(fcb, "%3u%3u%3u 0 0 0 0\n",
1132 : 0 : a1, a2, (unsigned)(at[i].bond_type[j]));
1133 : : }
1134 : :
1135 : 0 : IntArray_Append(written_bond_ends, a1);
1136 : 0 : IntArray_Append(written_bond_ends, a2);
1137 : : }
1138 : : }
1139 : : }
1140 : :
1141 : 0 : return ret;
1142 : : }
1143 : :
1144 : : /****************************************************************************
1145 : : OrigAtData : Write To SDfile : Additional Lines
1146 : : ****************************************************************************/
1147 : 0 : int OrigAtData_WriteToSDfileAdditionalLines( const ORIG_ATOM_DATA *inp_at_data,
1148 : : INCHI_IOSTREAM *fcb,
1149 : : const char *name,
1150 : : const char *comment,
1151 : : int bAtomsDT,
1152 : : const char *szLabel,
1153 : : const char *szValue,
1154 : : int nNumAliasLines,
1155 : : int nNumChargeLines,
1156 : : int nNumRadicalLines,
1157 : : int nNumIsoLines,
1158 : : INT_ARRAY *written_bond_ends )
1159 : : {
1160 : : char str_m[66], entry[25];
1161 : 0 : int i, num_m, k, j, ret = 0;
1162 : :
1163 [ # # ]: 0 : if (inp_at_data) /* djb-rwth: fixing a NULL pointer dereference */
1164 : : {
1165 : 0 : int num_atoms = inp_at_data->num_inp_atoms;
1166 [ # # # # : 0 : int is_polymer = inp_at_data && inp_at_data->polymer && inp_at_data->polymer->n > 0 && inp_at_data->valid_polymer;
# # # # ]
1167 : 0 : const inp_ATOM *at = inp_at_data->at;
1168 : :
1169 : : /* Aliases. 5-3-99 DCh.*/
1170 [ # # ]: 0 : if (nNumAliasLines)
1171 : : {
1172 : 0 : num_m = 0;
1173 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1174 : : {
1175 [ # # ]: 0 : if (ALIASED_AT(i))
1176 : : {
1177 : : int len;
1178 : 0 : inchi_ios_print_nodisplay(fcb, "A %d\n", i + 1);
1179 : 0 : num_m++;
1180 : 0 : len = sprintf(str_m, "%s", at[i].elname);
1181 : : /* add isotopic H to the alias */
1182 [ # # ]: 0 : for (k = 0; k < NUM_H_ISOTOPES; k++)
1183 : : {
1184 [ # # ]: 0 : int num_H = at[i].num_iso_H[k] + (k ? 0 : at[i].num_H);
1185 [ # # ]: 0 : if (num_H)
1186 : : {
1187 [ # # # # : 0 : len += sprintf(str_m + len, "%s", k == 0 ? "H" : k == 1 ? "D" : k == 2 ? "T" : "?");
# # ]
1188 [ # # ]: 0 : if (num_H != 1)
1189 : : {
1190 : 0 : len += sprintf(str_m + len, "%d", num_H);
1191 : : }
1192 : : }
1193 : : }
1194 : :
1195 : : /* Add charge to the Alias */
1196 [ # # ]: 0 : if (at[i].charge)
1197 : : {
1198 [ # # ]: 0 : len += sprintf(str_m + len, "%s", at[i].charge > 0 ? "+" : "-");
1199 [ # # ]: 0 : if (1 < (j = abs(at[i].charge)))
1200 : : {
1201 : 0 : len += sprintf(str_m + len, "%d", j);
1202 : : }
1203 : : }
1204 : :
1205 : : /* Add radical to the Alias */
1206 [ # # ]: 0 : if (at[i].radical == RADICAL_SINGLET)
1207 : : {
1208 : 0 : len += sprintf(str_m + len, "%s", ":");
1209 : : }
1210 [ # # ]: 0 : else if (at[i].radical == RADICAL_DOUBLET)
1211 : : {
1212 : 0 : len += sprintf(str_m + len, "%s", "^");
1213 : : }
1214 [ # # ]: 0 : else if (at[i].radical == RADICAL_TRIPLET)
1215 : : {
1216 : 0 : len += sprintf(str_m + len, "%s", "^^");
1217 : : }
1218 : 0 : inchi_ios_print_nodisplay(fcb, "%s\n", str_m);
1219 : 0 : num_m++;
1220 : : }
1221 : : }
1222 : :
1223 [ # # ]: 0 : if (num_m != nNumAliasLines)
1224 : : {
1225 : : /* error in lines counting*/
1226 : 0 : ret++;
1227 : : }
1228 : : }
1229 : :
1230 : : /* charges*/
1231 : 0 : str_m[0] = 0;
1232 : 0 : num_m = 0;
1233 [ # # ]: 0 : if (nNumChargeLines)
1234 : : {
1235 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1236 : : {
1237 [ # # # # ]: 0 : if (at[i].charge && !ALIASED_AT(i))
1238 : : {
1239 : 0 : sprintf(entry, " %3d %3d", i + 1, (int)at[i].charge);
1240 : 0 : strcat(str_m, entry);
1241 : 0 : num_m++;
1242 : : }
1243 [ # # # # : 0 : if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
# # ]
1244 : : {
1245 : 0 : inchi_ios_print_nodisplay(fcb, "M CHG%3d%s\n", num_m, str_m);
1246 : 0 : str_m[0] = 0;
1247 : 0 : num_m = 0;
1248 : : }
1249 : : }
1250 : : }
1251 : :
1252 : : /* radicals*/
1253 : 0 : str_m[0] = 0;
1254 : 0 : num_m = 0;
1255 : :
1256 [ # # ]: 0 : if (nNumRadicalLines)
1257 : : {
1258 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1259 : : {
1260 [ # # # # ]: 0 : if (at[i].radical && !ALIASED_AT(i))
1261 : : {
1262 : 0 : int radical = (at[i].radical == RADICAL_SINGLET ||
1263 [ # # ]: 0 : at[i].radical == RADICAL_DOUBLET ||
1264 [ # # # # ]: 0 : at[i].radical == RADICAL_TRIPLET) ? at[i].radical : 0;
1265 [ # # ]: 0 : if (radical)
1266 : : {
1267 : 0 : sprintf(entry, " %3d %3d", i + 1, radical);
1268 : 0 : strcat(str_m, entry);
1269 : 0 : num_m++;
1270 : : }
1271 : : }
1272 [ # # # # : 0 : if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
# # ]
1273 : : {
1274 : 0 : inchi_ios_print_nodisplay(fcb, "M RAD%3d%s\n", num_m, str_m);
1275 : 0 : str_m[0] = 0;
1276 : 0 : num_m = 0;
1277 : : }
1278 : : }
1279 : : }
1280 : :
1281 : : /* isotopes*/
1282 : 0 : str_m[0] = 0;
1283 : 0 : num_m = 0;
1284 [ # # ]: 0 : if (nNumIsoLines)
1285 : : {
1286 : : int el_num, iso;
1287 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1288 : : {
1289 : : /*
1290 : : if ( 0 == strcmp( at[i].elname, "D" ) ) {
1291 : : sprintf( entry, " %3d %3d", i+1, 2 );
1292 : : strcat( str_m, entry );
1293 : : num_m ++;
1294 : : } else
1295 : : if ( 0 == strcmp( at[i].elname, "T" ) ) {
1296 : : sprintf( entry, " %3d %3d", i+1, 3 );
1297 : : strcat( str_m, entry );
1298 : : num_m ++;
1299 : : } else
1300 : : if ( k = at[i].iso_atw_diff ) {
1301 : : int mw = get_atomic_mass_from_elnum( at[i].el_number );
1302 : : mw += (k > 0)? k-1 : k;
1303 : : sprintf( entry, " %3d %3d", i+1, mw );
1304 : : strcat( str_m, entry );
1305 : : num_m ++;
1306 : : }
1307 : : */
1308 : :
1309 [ # # # # : 0 : if (ANY_ISO(i, bAtomsDT) && !ALIASED_AT(i))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
1310 : : {
1311 [ # # # # : 0 : if (IS_DEUTERIUM(i))
# # ]
1312 : : {
1313 : 0 : iso = 1;
1314 : 0 : el_num = 1;
1315 : : }
1316 [ # # # # : 0 : else if (IS_TRITIUM(i))
# # ]
1317 : : {
1318 : 0 : iso = 2;
1319 : 0 : el_num = 1;
1320 : : }
1321 : : else
1322 : : {
1323 [ # # ]: 0 : iso = at[i].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 : at[i].iso_atw_diff;
1324 : 0 : el_num = at[i].el_number;
1325 : : }
1326 : 0 : iso += get_atomic_mass_from_elnum(el_num);
1327 : 0 : sprintf(entry, " %3d %3d", i + 1, iso);
1328 : 0 : strcat(str_m, entry);
1329 : 0 : num_m++;
1330 : : }
1331 : :
1332 [ # # # # : 0 : if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
# # ]
1333 : : {
1334 : 0 : inchi_ios_print_nodisplay(fcb, "M ISO%3d%s\n", num_m, str_m);
1335 : 0 : str_m[0] = 0;
1336 : 0 : num_m = 0;
1337 : : }
1338 : : }
1339 : : }
1340 : :
1341 [ # # ]: 0 : if (is_polymer)
1342 : : {
1343 : 0 : OrigAtData_WriteToSDfilePolymerData(inp_at_data, fcb, name, comment,
1344 : : szLabel, szValue, written_bond_ends);
1345 : : }
1346 : :
1347 : 0 : inchi_ios_print_nodisplay(fcb, "M END\n");
1348 : : }
1349 : 0 : return ret;
1350 : : }
1351 : :
1352 : : /****************************************************************************
1353 : : OrigAtData : Write To SDfile : Polymer Data
1354 : : ****************************************************************************/
1355 : 0 : int OrigAtData_WriteToSDfilePolymerData( const ORIG_ATOM_DATA *inp_at_data,
1356 : : INCHI_IOSTREAM *fcb,
1357 : : const char * name,
1358 : : const char *comment,
1359 : : const char *szLabel,
1360 : : const char *szValue,
1361 : : INT_ARRAY *written_bond_ends )
1362 : : {
1363 : 0 : int j, k, ju, jj, jprev, ret = 0;
1364 : 0 : const char *sty[] = {"NON", "SRU", "MON", "COP", "MOD", "CRO", "MER"};
1365 : 0 : const char *sst[] = {"NON", "ALT", "RAN", "BLO"};
1366 : 0 : const char *con[] = {"NON", "HT", "HH", "EU"};
1367 : 0 : OAD_PolymerUnit *u = NULL;
1368 : :
1369 : : /* STY */
1370 : 0 : jj = 0;
1371 : 0 : jprev = -1;
1372 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1373 : : {
1374 : 0 : u = inp_at_data->polymer->units[j];
1375 [ # # # # ]: 0 : if (u->type > 0 && u->type <= 6)
1376 : : {
1377 : 0 : jj++;
1378 : : }
1379 [ # # # # ]: 0 : if (jj == 8 || j == inp_at_data->polymer->n - 1)
1380 : : {
1381 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M STY%3d", jj % 8 ? jj % 8 : 8);
1382 [ # # ]: 0 : for (k = jprev + 1; k <= j; k++)
1383 : : {
1384 : 0 : u = inp_at_data->polymer->units[k];
1385 [ # # # # ]: 0 : if (u->type > 0 && u->type <= 6)
1386 : : {
1387 : 0 : inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, sty[u->type]);
1388 : : }
1389 : : }
1390 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1391 : 0 : jj = 0;
1392 : 0 : jprev = j;
1393 : : }
1394 : : }
1395 : : /* SLB */
1396 : : /* djb-rwth: removing redundant code */
1397 : 0 : jprev = -1;
1398 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1399 : : {
1400 : : /* djb-rwth: removing redundant code */
1401 [ # # # # ]: 0 : if (j == 8 || j == inp_at_data->polymer->n - 1)
1402 : : {
1403 : 0 : jj = j + 1;
1404 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M SLB%3d", jj % 8 ? jj % 8 : 8);
1405 [ # # ]: 0 : for (k = jprev + 1; k < jj; k++)
1406 : : {
1407 : 0 : u = inp_at_data->polymer->units[k];
1408 : 0 : inchi_ios_print_nodisplay(fcb, " %3d %3d", u->id, u->label);
1409 : : }
1410 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1411 : : /* djb-rwth: removing redundant code */
1412 : 0 : jprev = j;
1413 : : }
1414 : : }
1415 : :
1416 : : /* SST */
1417 : 0 : jj = 0;
1418 : : /* djb-rwth: removing redundant code */
1419 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1420 : : {
1421 : 0 : u = inp_at_data->polymer->units[j];
1422 [ # # # # : 0 : if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
# # ]
1423 : : {
1424 : 0 : jj++;
1425 : : }
1426 : : }
1427 [ # # ]: 0 : if (jj)
1428 : : {
1429 : 0 : jj = 0;
1430 : 0 : jprev = -1;
1431 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1432 : : {
1433 : 0 : u = inp_at_data->polymer->units[j];
1434 [ # # # # : 0 : if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
# # ]
1435 : : {
1436 : 0 : jj++;
1437 : : }
1438 [ # # # # ]: 0 : if (jj == 8 || j == inp_at_data->polymer->n - 1)
1439 : : {
1440 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M SST%3d", jj % 8 ? jj % 8 : 8);
1441 [ # # ]: 0 : for (k = jprev + 1; k <= j; k++)
1442 : : {
1443 : 0 : u = inp_at_data->polymer->units[k];
1444 [ # # # # : 0 : if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
# # ]
1445 : : {
1446 : 0 : inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, sst[u->subtype]);
1447 : : }
1448 : : }
1449 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1450 : 0 : jj = 0;
1451 : 0 : jprev = j;
1452 : : }
1453 : : }
1454 : : }
1455 : :
1456 : : /* SCN */
1457 : 0 : jj = 0;
1458 : : /* djb-rwth: removing redundant code */
1459 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1460 : : {
1461 : 0 : u = inp_at_data->polymer->units[j];
1462 [ # # # # : 0 : if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
# # ]
1463 : : {
1464 : 0 : jj++;
1465 : : }
1466 : : }
1467 [ # # ]: 0 : if (jj)
1468 : : {
1469 : 0 : jj = 0;
1470 : 0 : jprev = -1;
1471 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1472 : : {
1473 : 0 : u = inp_at_data->polymer->units[j];
1474 [ # # # # : 0 : if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
# # ]
1475 : : {
1476 : 0 : jj++;
1477 : : }
1478 [ # # # # ]: 0 : if (jj == 8 || j == inp_at_data->polymer->n - 1)
1479 : : {
1480 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M SCN%3d", jj % 8 ? jj % 8 : 8);
1481 [ # # ]: 0 : for (k = jprev + 1; k <= j; k++)
1482 : : {
1483 : 0 : u = inp_at_data->polymer->units[k];
1484 [ # # # # : 0 : if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
# # ]
1485 : : {
1486 : 0 : inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, con[u->conn]);
1487 : : }
1488 : : }
1489 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1490 : 0 : jj = 0;
1491 : 0 : jprev = j;
1492 : : }
1493 : : }
1494 : : }
1495 : : /* SAL */
1496 [ # # ]: 0 : for (ju = 0; ju < inp_at_data->polymer->n; ju++)
1497 : : {
1498 : 0 : u = inp_at_data->polymer->units[ju];
1499 : 0 : jj = 0;
1500 : 0 : jprev = -1;
1501 [ # # ]: 0 : for (j = 0; j < u->na; j++)
1502 : : {
1503 : 0 : jj++;
1504 [ # # # # ]: 0 : if (jj == 15 || j == u->na - 1)
1505 : : {
1506 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M SAL %3d%3d", u->id, jj % 15 ? jj % 15 : 15);
1507 [ # # ]: 0 : for (k = jprev + 1; k <= j; k++)
1508 : : {
1509 : 0 : inchi_ios_print_nodisplay(fcb, " %3d", u->alist[k]);
1510 : : }
1511 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1512 : 0 : jj = 0;
1513 : 0 : jprev = j;
1514 : : }
1515 : : }
1516 : : }
1517 : : /* SBL */
1518 [ # # ]: 0 : for (ju = 0; ju < inp_at_data->polymer->n; ju++)
1519 : : {
1520 : 0 : u = inp_at_data->polymer->units[ju];
1521 : 0 : jj = 0;
1522 : 0 : jprev = -1;
1523 [ # # ]: 0 : for (j = 0; j < u->nb; j++)
1524 : : {
1525 : 0 : jj++;
1526 [ # # # # ]: 0 : if (jj == 15 || j == u->nb - 1)
1527 : : {
1528 [ # # ]: 0 : inchi_ios_print_nodisplay(fcb, "M SBL %3d%3d", u->id, jj % 15 ? jj % 15 : 15);
1529 : :
1530 [ # # ]: 0 : for (k = jprev + 1; k <= j; k++)
1531 : : {
1532 : 0 : int a1, a2, e1, e2, wb, bond_num = 0;
1533 : 0 : a1 = u->blist[2 * k];
1534 : 0 : a2 = u->blist[2 * k + 1];
1535 [ # # ]: 0 : for (wb = 0; wb < written_bond_ends->used / 2; wb++)
1536 : : {
1537 : 0 : e1 = written_bond_ends->item[2 * wb];
1538 : 0 : e2 = written_bond_ends->item[2 * wb + 1];
1539 [ # # # # : 0 : if ((a1 == e1 && a2 == e2) || (a2 == e1 && a1 == e2))
# # # # ]
1540 : : {
1541 : 0 : bond_num = wb + 1;
1542 : 0 : break;
1543 : : }
1544 : : }
1545 [ # # ]: 0 : if (bond_num)
1546 : : {
1547 : 0 : inchi_ios_print_nodisplay(fcb, " %3d", bond_num);
1548 : : }
1549 : : }
1550 : :
1551 : 0 : inchi_ios_print_nodisplay(fcb, "\n");
1552 : 0 : jj = 0;
1553 : 0 : jprev = j;
1554 : : }
1555 : : }
1556 : : }
1557 : :
1558 : : /* SDI */
1559 [ # # ]: 0 : for (j = 0; j < inp_at_data->polymer->n; j++)
1560 : : {
1561 : : /* better than nothing */
1562 : : float xmin, xmax, ymin, ymax;
1563 : 0 : xmin = ymin = -1.0 * ((float)j + 1.0); /* djb-rwth: cast operator added, 1->1.0 */
1564 : 0 : xmax = ymax = +1.0 * ((float)j + 1.0); /* djb-rwth: cast operator added, 1->1.0 */
1565 : 0 : u = inp_at_data->polymer->units[j];
1566 : : /* u->xbr1[0], x1, y1, x2, y2 u->xbr1[1], u->xbr1[2], u->xbr1[3] */
1567 : 0 : inchi_ios_print_nodisplay(fcb, "M SDI %3d%3d%10.4f%10.4f%10.4f%10.4f\n", u->id, 4, xmin, ymin, xmin, ymax);
1568 : : /* u->xbr2[0], u->xbr2[1], u->xbr2[2], u->xbr2[3] */
1569 : 0 : inchi_ios_print_nodisplay(fcb, "M SDI %3d%3d%10.4f%10.4f%10.4f%10.4f\n", u->id, 4, xmax, ymax, xmax, ymin);
1570 : : }
1571 : :
1572 : 0 : return ret;
1573 : : }
|