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 <string.h>
8 : : #include <ctype.h>
9 : : #include <limits.h>
10 : : #include <locale.h>
11 : :
12 : : #include "mode.h"
13 : : #include "ichimain.h"
14 : : #include "ichimake.h"
15 : : #include "ichi_io.h"
16 : :
17 : : #include "bcf_s.h"
18 : :
19 : : /****************************************************************************/
20 : 81 : int Eql_INChI_Stereo( INChI_Stereo *s1,
21 : : int eql1,
22 : : INChI_Stereo *s2,
23 : : int eql2,
24 : : int bRelRac )
25 : : {
26 : 81 : int inv1 = 0, inv2 = 0, len;
27 : :
28 [ - + ]: 81 : if (!s1)
29 : : {
30 : 0 : return 0;
31 : : }
32 : : #if ( REL_RAC_STEREO_IGN_1_SC == 1 )
33 : : #else
34 : 81 : bRelRac = 0;
35 : : #endif
36 : :
37 [ + + ]: 81 : if (EQL_SP2 == eql1)
38 : : {
39 [ + - + - : 1 : if (( len = s1->nNumberOfStereoBonds ) > 0 && s1->b_parity && s1->nBondAtom1 && s1->nBondAtom2)
+ - + - ]
40 : : {
41 [ - + ]: 1 : if (!s2)
42 : : {
43 [ # # ]: 0 : if (EQL_EXISTS == eql2)
44 : : {
45 : : /* find whether double bond stereo exists*/
46 : 0 : return 1;
47 : : }
48 : 0 : return 0;
49 : : }
50 [ + - ]: 1 : if (EQL_SP2 == eql2 &&
51 [ + - + - : 1 : len == s2->nNumberOfStereoBonds && s2->b_parity && s2->nBondAtom1 && s2->nBondAtom2 &&
+ - + - ]
52 [ + - ]: 1 : !memcmp( s1->nBondAtom1, s2->nBondAtom1, len * sizeof( s1->nBondAtom1[0] ) ) &&
53 [ + - ]: 1 : !memcmp( s1->nBondAtom2, s2->nBondAtom2, len * sizeof( s1->nBondAtom2[0] ) ) &&
54 [ + - ]: 1 : !memcmp( s1->b_parity, s2->b_parity, len * sizeof( s1->b_parity[0] ) ))
55 : : {
56 : 1 : return 1;
57 : : }
58 : : }
59 : 0 : return 0;
60 : : }
61 : :
62 : : else
63 : : {
64 [ + + + - ]: 80 : if (( eql1 == EQL_SP3 || ( inv1 = ( eql1 == EQL_SP3_INV ) ) ) &&
65 [ + + ]: 80 : ( len = s1->nNumberOfStereoCenters ) > ( bRelRac ? 1 : 0 )) /* djb-rwth: addressing coverity ID #499484 -- bRelRac does not have to be 0 */
66 : : {
67 : :
68 : : S_CHAR *t_parity1, *t_parity2;
69 : : AT_NUMB *nNumber1, *nNumber2;
70 [ + + ]: 78 : if (inv1)
71 : : {
72 [ + - ]: 60 : if (s1->nCompInv2Abs)
73 : : {
74 : 60 : t_parity1 = s1->t_parityInv;
75 : 60 : nNumber1 = s1->nNumberInv;
76 : : }
77 : : else
78 : : {
79 : 0 : return 0;
80 : : }
81 : : }
82 : : else
83 : : {
84 : 18 : t_parity1 = s1->t_parity;
85 : 18 : nNumber1 = s1->nNumber;
86 : : }
87 [ + - + - ]: 78 : if (t_parity1 && nNumber1)
88 : : {
89 [ - + ]: 78 : if (!s2)
90 : : {
91 [ # # # # : 0 : if (EQL_EXISTS == eql2 && ( !inv1 || s1->nCompInv2Abs ))
# # ]
92 : : {
93 : : /* the 1st sp3 (inverted if requested) stereo exists*/
94 : 0 : return 1;
95 : : }
96 : 0 : return 0; /* both sp3 do not exist */
97 : : }
98 [ - + - - ]: 78 : if (( eql2 == EQL_SP3 || ( inv2 = ( eql2 == EQL_SP3_INV ) ) ) &&
99 [ + + ]: 78 : len == s2->nNumberOfStereoCenters)
100 : : {
101 [ - + ]: 71 : if (inv2)
102 : : {
103 [ # # # # ]: 0 : if (s2->nCompInv2Abs && s1->nCompInv2Abs)
104 : : {
105 : 0 : t_parity2 = s2->t_parityInv;
106 : 0 : nNumber2 = s2->nNumberInv;
107 : : }
108 : : else
109 : : {
110 : : /* if one sp3 is inverted then another should have non-trivial inverted stereo */
111 : 0 : return 0;
112 : : }
113 : : }
114 : : else
115 : : {
116 [ + + - + ]: 71 : if (inv1 && !s2->nCompInv2Abs)
117 : : {
118 : : /* if one sp3 is inverted then another should have non-trivial inverted stereo */
119 : 0 : return 0;
120 : : }
121 : 71 : t_parity2 = s2->t_parity;
122 : 71 : nNumber2 = s2->nNumber;
123 : : }
124 [ + - + - ]: 71 : if (t_parity2 && nNumber2)
125 : : {
126 [ + + ]: 71 : if (inv1 ^ inv2)
127 : : {
128 : : int i, num_inv;
129 [ + + ]: 209 : for (i = 0, num_inv = 0; i < len; i++)
130 : : {
131 [ - + ]: 162 : if (nNumber1[i] != nNumber2[i])
132 : 0 : break;
133 [ + - + + ]: 162 : if (ATOM_PARITY_WELL_DEF( t_parity1[i] ) &&
134 [ + - + - ]: 145 : ATOM_PARITY_WELL_DEF( t_parity2[i] ))
135 : : {
136 [ + + ]: 145 : if (3 == t_parity1[i] + t_parity2[i])
137 : : {
138 : 132 : num_inv++;
139 : : }
140 : : else
141 : : {
142 : 13 : break;
143 : : }
144 : : }
145 : : else
146 [ - + ]: 17 : if (t_parity1[i] != t_parity2[i])
147 : : {
148 : 0 : break;
149 : : }
150 : : }
151 [ + + + - ]: 60 : return ( len == i && num_inv > 0 );
152 : : }
153 : : else
154 : : {
155 [ + - ]: 22 : return !memcmp( t_parity1, t_parity2, len * sizeof( t_parity1[0] ) ) &&
156 [ + + ]: 11 : !memcmp( nNumber1, nNumber2, len * sizeof( nNumber1[0] ) );
157 : : }
158 : : }
159 : : }
160 : : }
161 : : }
162 : : }
163 : :
164 : 9 : return 0;
165 : : }
166 : :
167 : :
168 : : /****************************************************************************/
169 : 7 : int Eql_INChI_Isotopic( INChI *i1, INChI *i2 )
170 : : {
171 : 7 : int eq = i1
172 [ + - ]: 1 : && i2
173 [ + - ]: 1 : && !i1->bDeleted
174 [ + - ]: 1 : && !i2->bDeleted
175 [ - + - - ]: 1 : && ( i1->nNumberOfIsotopicAtoms > 0 || i1->nNumberOfIsotopicTGroups > 0 )
176 [ - + ]: 1 : && i1->nNumberOfIsotopicAtoms == i2->nNumberOfIsotopicAtoms
177 [ # # ]: 0 : && i1->nNumberOfIsotopicTGroups == i2->nNumberOfIsotopicTGroups
178 [ # # ]: 0 : && ( !i1->nNumberOfIsotopicAtoms ||
179 [ # # # # ]: 0 : (i1->IsotopicAtom && i2->IsotopicAtom &&
180 : 0 : !memcmp( i1->IsotopicAtom, i2->IsotopicAtom,
181 [ # # ]: 0 : i1->nNumberOfIsotopicAtoms * sizeof( i1->IsotopicAtom[0] ) )) )
182 [ + + - - ]: 8 : && ( !i1->nNumberOfIsotopicTGroups ||
183 [ # # # # ]: 0 : (i1->IsotopicTGroup && i2->IsotopicTGroup &&
184 : 0 : !memcmp( i1->IsotopicTGroup, i2->IsotopicTGroup,
185 [ # # ]: 0 : i1->nNumberOfIsotopicTGroups * sizeof( i1->IsotopicAtom[0] )) )
186 : : ); /* djb-rwth: addressing LLVM warnings */
187 : :
188 : 7 : return eq;
189 : : }
190 : :
191 : :
192 : : /****************************************************************************/
193 : 89 : int Eql_INChI_Aux_Equ( INChI_Aux *a1, int eql1, INChI_Aux *a2, int eql2 )
194 : : {
195 : 89 : int t1 = 0, t2 = 0, len = 0;
196 : 89 : AT_NUMB *n1 = NULL, *n2 = NULL;
197 [ + + - + ]: 89 : if (!a1 || !a2)
198 : : {
199 : 45 : return 0;
200 : : }
201 : 44 : t1 = ( eql1 & EQL_EQU_TG );
202 : 44 : t2 = ( eql2 & EQL_EQU_TG );
203 [ + + + - ]: 44 : if (t1 && t2)
204 : : {
205 [ + + + - : 5 : if (( len = a1->nNumberOfTGroups ) > 0 && len == a2->nNumberOfTGroups && !a1->bDeleted && !a2->bDeleted)
+ - + - ]
206 : : {
207 [ - + ]: 1 : if (eql1 & EQL_EQU_ISO)
208 : : {
209 [ # # ]: 0 : if (a1->bIsIsotopic)
210 : : {
211 : 0 : n1 = a1->nConstitEquIsotopicTGroupNumbers;
212 : : }
213 : : }
214 : : else
215 : : {
216 : 1 : n1 = a1->nConstitEquTGroupNumbers;
217 : : }
218 [ - + ]: 1 : if (eql2 & EQL_EQU_ISO)
219 : : {
220 [ # # ]: 0 : if (a2->bIsIsotopic)
221 : : {
222 : 0 : n2 = a2->nConstitEquIsotopicTGroupNumbers;
223 : : }
224 : : }
225 : : else
226 : : {
227 : 1 : n2 = a2->nConstitEquTGroupNumbers;
228 : : }
229 : : }
230 : : }
231 : : else
232 [ + - + - ]: 39 : if (!t1 && !t2)
233 : : {
234 [ + - + + : 39 : if (( len = a1->nNumberOfAtoms ) > 0 && len == a2->nNumberOfAtoms && !a1->bDeleted && !a2->bDeleted)
+ - + - ]
235 : : {
236 [ - + ]: 16 : if (eql1 & EQL_EQU_ISO)
237 : : {
238 [ # # ]: 0 : if (a1->bIsIsotopic)
239 : : {
240 : 0 : n1 = a1->nConstitEquIsotopicNumbers;
241 : : }
242 : : }
243 : : else
244 : : {
245 : 16 : n1 = a1->nConstitEquNumbers;
246 : : }
247 [ - + ]: 16 : if (eql2 & EQL_EQU_ISO)
248 : : {
249 [ # # ]: 0 : if (a2->bIsIsotopic)
250 : : {
251 : 0 : n2 = a2->nConstitEquIsotopicNumbers;
252 : : }
253 : : }
254 : : else
255 : : {
256 : 16 : n2 = a2->nConstitEquNumbers;
257 : : }
258 : : }
259 : : }
260 [ + + + - : 44 : if (n1 && n2 && !memcmp( n1, n2, len * sizeof( n1[0] ) ) && bHasEquString( n1, len ))
+ - + + ]
261 : : {
262 : 2 : return 1;
263 : : }
264 : :
265 : 42 : return 0;
266 : : }
267 : :
268 : :
269 : :
270 : : /****************************************************************************/
271 : 0 : int Eql_INChI_Aux_Num( INChI_Aux *a1, int eql1, INChI_Aux *a2, int eql2 )
272 : : {
273 : : int len;
274 : 0 : AT_NUMB *n1 = NULL, *n2 = NULL;
275 [ # # # # ]: 0 : if (!a1 || !a2)
276 : : {
277 : 0 : return 0;
278 : : }
279 [ # # # # : 0 : if (( len = a1->nNumberOfAtoms ) <= 0 || len != a2->nNumberOfAtoms || a1->bDeleted || a2->bDeleted)
# # # # ]
280 : : {
281 : 0 : return 0;
282 : : }
283 [ # # # # ]: 0 : if ((( eql1 & EQL_NUM_ISO ) && !a1->bIsIsotopic) ||
284 [ # # # # ]: 0 : (( eql2 & EQL_NUM_ISO ) && !a2->bIsIsotopic)) /* djb-rwth: addressing LLVM warnings */
285 : : {
286 : 0 : return 0;
287 : : }
288 : :
289 [ # # # # : 0 : switch (eql1)
# ]
290 : : {
291 : 0 : case EQL_NUM:
292 : 0 : n1 = a1->nOrigAtNosInCanonOrd;
293 : 0 : break;
294 : 0 : case EQL_NUM_ISO:
295 : 0 : n1 = a1->nIsotopicOrigAtNosInCanonOrd;
296 : 0 : break;
297 : 0 : case EQL_NUM_INV:
298 : 0 : n1 = a1->nOrigAtNosInCanonOrdInv;
299 : 0 : break;
300 : 0 : case ( EQL_NUM_INV | EQL_NUM_ISO ):
301 : 0 : n1 = a1->nIsotopicOrigAtNosInCanonOrdInv;
302 : 0 : break;
303 : 0 : default:
304 : 0 : return 0;
305 : : }
306 : :
307 [ # # # # : 0 : switch (eql2)
# ]
308 : : {
309 : 0 : case EQL_NUM:
310 : 0 : n2 = a2->nOrigAtNosInCanonOrd;
311 : 0 : break;
312 : 0 : case EQL_NUM_ISO:
313 : 0 : n2 = a2->nIsotopicOrigAtNosInCanonOrd;
314 : 0 : break;
315 : 0 : case EQL_NUM_INV:
316 : 0 : n2 = a2->nOrigAtNosInCanonOrdInv;
317 : 0 : break;
318 : 0 : case ( EQL_NUM_INV | EQL_NUM_ISO ):
319 : 0 : n2 = a2->nIsotopicOrigAtNosInCanonOrdInv;
320 : 0 : break;
321 : 0 : default:
322 : 0 : return 0;
323 : : }
324 : :
325 [ # # # # : 0 : if (n1 && n2 && !memcmp( n1, n2, len * sizeof( n1[0] ) ))
# # ]
326 : : {
327 : 0 : return 1;
328 : : }
329 : :
330 : 0 : return 0;
331 : : }
332 : :
333 : :
334 : : /****************************************************************************/
335 : 1330 : int bHasOrigInfo( ORIG_INFO *OrigInfo, int num_atoms )
336 : : {
337 : 1330 : int i, bFound = 0;
338 [ + - + - ]: 1330 : if (OrigInfo && num_atoms > 0)
339 : : {
340 [ + + + + ]: 10644 : for (i = 0; !bFound && i < num_atoms; i++)
341 : : {
342 : 9314 : bFound |= ( 0 != OrigInfo[i].cCharge ) ||
343 [ + + + - ]: 18575 : ( 0 != OrigInfo[i].cRadical ) ||
344 [ + + ]: 9261 : ( 0 != OrigInfo[i].cUnusualValence );
345 : : }
346 : : }
347 : :
348 : 1330 : return bFound;
349 : : }
350 : :
351 : :
352 : : /****************************************************************************/
353 : 87 : int EqlOrigInfo( INChI_Aux *a1, INChI_Aux *a2 )
354 : : {
355 [ + + + + : 81 : int ret = a1 && a2 && a1->nNumberOfAtoms == a2->nNumberOfAtoms &&
+ + ]
356 [ + + + - ]: 180 : bHasOrigInfo( a1->OrigInfo, a1->nNumberOfAtoms ) && a2->OrigInfo &&
357 [ + + ]: 12 : !memcmp( a1->OrigInfo, a2->OrigInfo, a1->nNumberOfAtoms * sizeof( a1->OrigInfo[0] ) );
358 : :
359 : 87 : return ret;
360 : : }
361 : :
362 : :
363 : : /****************************************************************************/
364 : 1380 : int bHasEquString( AT_NUMB *LinearCT, int nLenCT )
365 : : {
366 : : /* produce output string; */
367 : : int i, k;
368 [ + + ]: 1380 : if (!LinearCT)
369 : : {
370 : 9 : return 0;
371 : : }
372 [ + + ]: 12353 : for (k = 0; k < nLenCT; k++)
373 : : {
374 : : /* find the first equivalence number */
375 [ + + ]: 11080 : if (k != (int) LinearCT[k] - 1)
376 : : {
377 : 10982 : continue;
378 : : }
379 [ + - ]: 196 : for (i = k; i < nLenCT; i++)
380 : : {
381 [ - + ]: 196 : if (k != (int) LinearCT[i] - 1)
382 : : {
383 : 0 : continue;
384 : : }
385 [ + + ]: 196 : if (k < i)
386 : : {
387 : 98 : return 1;
388 : : }
389 : : }
390 : : }
391 : :
392 : 1273 : return 0;
393 : : }
394 : :
395 : :
396 : : /****************************************************************************/
397 : 739 : int MakeMult( int mult,
398 : : const char *szTailingDelim,
399 : : INCHI_IOS_STRING *buf,
400 : : int nCtMode,
401 : : int *bOverflow )
402 : : {
403 : : char szValue[2048];
404 : 739 : int len = 0, len_delim, n;
405 : :
406 [ + + - + ]: 739 : if (mult == 1 || *bOverflow)
407 : : {
408 : 665 : return 0;
409 : : }
410 [ - + ]: 74 : if (nCtMode & CT_MODE_ABC_NUMBERS)
411 : : {
412 : 0 : len += MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, mult );
413 : : }
414 : : else
415 : : {
416 : 74 : len += MakeDecNumber( szValue, ( int )sizeof( szValue ), NULL, mult );
417 : : }
418 : 74 : len_delim = (int) strlen( szTailingDelim );
419 : :
420 [ + - ]: 74 : if (len + len_delim < ( int )sizeof( szValue ))
421 : : {
422 : 74 : strcpy(szValue + len, szTailingDelim);
423 : 74 : n = inchi_strbuf_printf( buf, "%s", szValue );
424 [ - + ]: 74 : if (-1 == n) *bOverflow |= 1;
425 : 74 : return n;
426 : : /*
427 : : len += len_delim;
428 : : if ( len < nLen_szLinearCT )
429 : : {
430 : : strcpy( szLinearCT, szValue );
431 : : return len;
432 : : }*/
433 : : }
434 : :
435 : 0 : *bOverflow |= 1;
436 : :
437 : 0 : return 0;
438 : : }
439 : :
440 : : /**
441 : : * @brief Adds the number to the string buffer.
442 : : *
443 : : * @param number Input number to be added
444 : : * @param szTailingDelim Pointer to the trailing delimiter string
445 : : * @param buf Pointer to the output string buffer
446 : : * @param nCtMode Mode flag for string representation
447 : : * @param bOverflow Pointer to overflow flag
448 : : * @return Returns the number of characters added to the buffer
449 : : */
450 : 163 : int MakeNumber_EnhStereo( int number,
451 : : const char *szTailingDelim,
452 : : INCHI_IOS_STRING *buf,
453 : : int nCtMode,
454 : : int *bOverflow )
455 : : {
456 : : char szValue[2048];
457 : 163 : int len = 0;
458 : : int len_delim;
459 : : int n;
460 : :
461 [ - + ]: 163 : if (*bOverflow)
462 : : {
463 : 0 : return 0;
464 : : }
465 [ - + ]: 163 : if (nCtMode & CT_MODE_ABC_NUMBERS)
466 : : {
467 : 0 : len += MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, number );
468 : : }
469 : : else
470 : : {
471 : 163 : len += MakeDecNumber( szValue, ( int )sizeof( szValue ), NULL, number );
472 : : }
473 : 163 : len_delim = (int) strlen( szTailingDelim );
474 : :
475 [ + - ]: 163 : if (len + len_delim < ( int )sizeof( szValue ))
476 : : {
477 : 163 : strcpy(szValue + len, szTailingDelim);
478 : 163 : n = inchi_strbuf_printf( buf, "%s", szValue );
479 [ - + ]: 163 : if (-1 == n) *bOverflow |= 1;
480 : 163 : return n;
481 : : }
482 : :
483 : 0 : *bOverflow |= 1;
484 : :
485 : 0 : return 0;
486 : : }
487 : :
488 : :
489 : : /****************************************************************************/
490 : : /**
491 : : * @brief Adds the delimiter to the string buffer if it is not empty and there is no overflow.
492 : : *
493 : : * @param szTailingDelim Pointer to the trailing delimiter string
494 : : * @param buf Pointer to the output string buffer
495 : : * @param bOverflow Pointer to overflow flag
496 : : * @return Returns the number of characters added to the buffer, or 0 if the delimiter is empty or there is an overflow
497 : : */
498 : 2821 : int MakeDelim( const char *szTailingDelim,
499 : : INCHI_IOS_STRING *buf,
500 : : int *bOverflow )
501 : : {
502 : : int n;
503 [ + - + - : 2821 : if (!szTailingDelim || !*szTailingDelim || *bOverflow)
- + ]
504 : : {
505 : 0 : return 0;
506 : : }
507 : :
508 : 2821 : n = inchi_strbuf_printf( buf, szTailingDelim );
509 [ - + ]: 2821 : if (-1 == n)
510 : : {
511 : 0 : *bOverflow |= 1;
512 : : }
513 : :
514 : 2821 : return n;
515 : : /*
516 : : len_delim = (int) strlen(szTailingDelim);
517 : : if ( len_delim < nLen_szLinearCT ) {
518 : : strcpy( szLinearCT, szTailingDelim );
519 : : return len_delim;
520 : : }
521 : : *bOverflow |= 1;
522 : : return 0;
523 : : */
524 : : }
525 : :
526 : :
527 : : /****************************************************************************/
528 : 44 : int MakeEqStr( const char *szTailingDelim,
529 : : int mult,
530 : : INCHI_IOS_STRING *buf,
531 : : int *bOverflow )
532 : : {
533 : 44 : int n = 0, n0;
534 : : char szValue[2048];
535 : :
536 [ + - + - : 44 : if (!szTailingDelim || !*szTailingDelim || *bOverflow)
- + ]
537 : : {
538 : 0 : return 0;
539 : : }
540 : :
541 : 44 : n0 = buf->nUsedLength;
542 : :
543 [ + + ]: 44 : if (mult != 1)
544 : : {
545 : 3 : n = MakeDecNumber( szValue, ( int )sizeof( szValue ), NULL, mult );
546 [ - + ]: 3 : if (-1 == n)
547 : : {
548 : 0 : *bOverflow |= 1;
549 : 0 : return -1;
550 : : }
551 : : }
552 [ + + ]: 44 : if (n > 0)
553 : : {
554 : 3 : n = inchi_strbuf_printf( buf, "%-s", szValue );
555 [ - + ]: 3 : if (-1 == n) *bOverflow |= 1;
556 : : }
557 : 44 : n = inchi_strbuf_printf( buf, "%-s", szTailingDelim );
558 [ - + ]: 44 : if (-1 == n)
559 : : {
560 : 0 : *bOverflow |= 1;
561 : : }
562 : :
563 : 44 : return ( buf->nUsedLength - n0 );
564 : : }
565 : :
566 : :
567 : : /****************************************************************************
568 : : Prepare InChI atomic numbers substring (connections, for InChI)
569 : : nCtMode = 0: full
570 : : 1: censored CT (no orphans)
571 : : 2: compressed CT (Abs numbers)
572 : : ****************************************************************************/
573 : 135 : int MakeCtStringNew( CANON_GLOBALS *pCG,
574 : : AT_NUMB *LinearCT,
575 : : int nLenCT,
576 : : int bAddDelim,
577 : : S_CHAR *nNum_H,
578 : : int num_atoms,
579 : : INCHI_IOS_STRING *strbuf,
580 : : int nCtMode,
581 : : int *bOverflow )
582 : : {
583 : : /* produce output string; */
584 : 135 : int nUsedLength0 = 0, nLen = 0, len, i, bOvfl = *bOverflow;
585 : : char szValue[2048];
586 : : int nValue, nDelim, num_H;
587 : 135 : AT_NUMB *nDfsOrderCT = NULL;
588 : 135 : int bNoNum_H = ( NULL == nNum_H );
589 : : int nNumRingClosures;
590 : 135 : int bAbcNumbers = ( 0 != ( nCtMode & CT_MODE_ABC_NUMBERS ) );
591 : 135 : int bPredecessors = ( 0 != ( nCtMode & CT_MODE_PREDECESSORS ) );
592 [ - + - - : 135 : int bCountRingClosures = bAbcNumbers && bPredecessors && ( nCtMode & CT_MODE_ABC_NUM_CLOSURES );
- - ]
593 : :
594 [ - + ]: 135 : if (nLenCT <= 1)
595 : : {
596 : 0 : return 0; /* no atoms or a single atom: no connection table */
597 : : }
598 : :
599 : 135 : nUsedLength0 = strbuf->nUsedLength;
600 : :
601 : : /* make array containing connection string data */
602 [ - + ]: 135 : if (!( nDfsOrderCT = GetDfsOrder4CT( pCG, LinearCT, nLenCT,
603 : : nNum_H, num_atoms, nCtMode ) ))
604 : : {
605 : 0 : ( *bOverflow )++;
606 : 0 : return 0;
607 : : }
608 : :
609 : : /* add connection table string */
610 [ + - - + ]: 135 : if (!bOvfl && bAddDelim)
611 : : {
612 : 0 : inchi_strbuf_printf( strbuf, "," );
613 : : }
614 : :
615 [ + - ]: 135 : if (!bOvfl)
616 : : {
617 : 135 : nNumRingClosures = 0;
618 [ + + ]: 8132 : for (i = 0; nDfsOrderCT[i]/* && nLen < nLen_szLinearCT*/; i += 3)
619 : : {
620 [ + - ]: 7997 : nValue = ( nDfsOrderCT[i] > MAX_ATOMS ) ? 0 : nDfsOrderCT[i];
621 [ - + ]: 7997 : num_H = nDfsOrderCT[i + 1] ? nDfsOrderCT[i + 1] - 16 : 0;
622 : 7997 : nDelim = nDfsOrderCT[i + 2];
623 : 7997 : len = 0;
624 : : /* delimiter */
625 [ - + ]: 7997 : if (bPredecessors)
626 : : {
627 [ # # ]: 0 : if (bCountRingClosures)
628 : : {
629 [ # # # # : 0 : if (nDelim == '-' && i > 3 && bNoNum_H)
# # ]
630 : : {
631 [ # # ]: 0 : if (!nNumRingClosures)
632 : : {
633 : : int j;
634 [ # # # # ]: 0 : for (j = i; nDfsOrderCT[j] && '-' == nDfsOrderCT[j + 2]; j += 3)
635 : : {
636 : 0 : nNumRingClosures++;
637 : : }
638 [ # # ]: 0 : if (nNumRingClosures)
639 : : {
640 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, nNumRingClosures );
641 : : }
642 : 0 : nNumRingClosures--;
643 : : }
644 : : else
645 : : {
646 : 0 : nNumRingClosures--;
647 : : }
648 : : }
649 : : else
650 : : {
651 : 0 : nNumRingClosures = 0;
652 : : }
653 : : }
654 [ # # # # : 0 : else if (nDelim && !( bAbcNumbers && nDelim == ',' ))
# # ]
655 : : {
656 [ # # # # ]: 0 : if (nNum_H || i > 3)
657 : : {
658 : 0 : szValue[len++] = nDelim;
659 : : }
660 : : }
661 : : }
662 : : else
663 : : {
664 [ + + - + : 7997 : if (nDelim && !( bAbcNumbers && nDelim == '-' ))
- - ]
665 : : {
666 : 7862 : szValue[len++] = nDelim;
667 : : }
668 : : }
669 [ - + ]: 7997 : if (bAbcNumbers)
670 : : {
671 [ # # # # ]: 0 : if (nValue || i)
672 : : {
673 : : /* the 1st value may be zero in case of presdecessor list */
674 : 0 : len += MakeAbcNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, nValue );
675 : : }
676 [ # # ]: 0 : if (num_H)
677 : : {
678 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, num_H );
679 : : }
680 : : }
681 : : else
682 : : {
683 [ - + - - ]: 7997 : if (nValue || i)
684 : : {
685 : : /* the 1st value may be zero in case of presdecessor list */
686 : 7997 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, nValue );
687 : : }
688 [ - + ]: 7997 : if (num_H)
689 : : {
690 : 0 : szValue[len] = 'H';
691 : 0 : len++;
692 [ # # ]: 0 : if (num_H > 1)
693 : : {
694 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, num_H );
695 : : }
696 : : }
697 : : }
698 : :
699 [ + - ]: 7997 : if (len > 0)
700 : : {
701 : 7997 : inchi_strbuf_printf( strbuf, "%s", szValue );
702 : : }
703 : : }
704 : : }
705 : :
706 : 135 : *bOverflow |= bOvfl;
707 [ + - ]: 135 : if (nDfsOrderCT)
708 : : {
709 [ + - ]: 135 : inchi_free( nDfsOrderCT );
710 : : }
711 : :
712 : 135 : nLen = strbuf->nUsedLength - nUsedLength0;
713 : :
714 : 135 : return nLen;
715 : : }
716 : :
717 : :
718 : : /****************************************************************************
719 : : Prepare InChI atomic numbers substring (for AuxInfo)
720 : :
721 : : nCtMode = 0: full
722 : : 1: censored CT (no orphans)
723 : : 2: compressed CT (Abs numbers)
724 : : ****************************************************************************/
725 : 1296 : int MakeCtStringOld( AT_NUMB *LinearCT,
726 : : int nLenCT,
727 : : int bAddDelim,
728 : : INCHI_IOS_STRING *strbuf,
729 : : int nCtMode,
730 : : int *bOverflow )
731 : : {
732 : : /* produce output string; */
733 : 1296 : int nUsedLength0 = 0, nLen = 0, len, i, bLessThanPrev, bOvfl = *bOverflow;
734 : 1296 : AT_NUMB nMax = 0;
735 : : char szValue[2048];
736 : 1296 : int nValue, bNext = 0;
737 : :
738 : 1296 : nUsedLength0 = strbuf->nUsedLength;
739 : :
740 : : /* add connection table string */
741 [ + - + - : 1296 : if (!( nCtMode & CT_MODE_ABC_NUMBERS ) && !bOvfl && bAddDelim)
- + ]
742 : : {
743 : 0 : inchi_strbuf_printf( strbuf, "," );
744 : : /*
745 : : if ( nLen_szLinearCT > 1 ) {
746 : : strcpy( szLinearCT, "," );
747 : : nLen ++;
748 : : } else {
749 : : bOvfl = 1;
750 : : }*/
751 : : }
752 [ + - ]: 1296 : if (!bOvfl)
753 : : {
754 [ + + ]: 10607 : for (i = 0; i < nLenCT/* && nLen < nLen_szLinearCT*/; i++)
755 : : {
756 : 9311 : bLessThanPrev = 0;
757 [ - + - - ]: 9311 : if (!( nCtMode & CT_MODE_NO_ORPHANS ) || ( ( bLessThanPrev = LinearCT[i] < nMax ) ||
758 [ # # # # ]: 0 : (i + 1 < nLenCT && LinearCT[i + 1] < ( nMax = LinearCT[i] )) )) /* djb-rwth: addressing LLVM warning */
759 : : {
760 : 9311 : nValue = LinearCT[i];
761 [ - + ]: 9311 : if (nCtMode & CT_MODE_ABC_NUMBERS)
762 : : {
763 [ # # # # ]: 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), ( !bNext && bAddDelim ) ? ITEM_DELIMETER : NULL, nValue );
764 : : }
765 : : else
766 : : {
767 [ - + ]: 9311 : if (nCtMode & CT_MODE_NO_ORPHANS)
768 : : {
769 : : /* censored CT */
770 : : /* output '-' as a delimiter to show a bonding for decimal output of the connection table */
771 [ # # ]: 0 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), bLessThanPrev ? "-" : ITEM_DELIMETER, nValue );
772 : : }
773 : : else
774 : : {
775 [ + + ]: 9311 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), i ? ITEM_DELIMETER : NULL, nValue );
776 : : }
777 : : }
778 [ + - ]: 9311 : if (len > 0)
779 : : {
780 : 9311 : inchi_strbuf_printf( strbuf, "%s", szValue );
781 : 9311 : bNext++;
782 : : }
783 : :
784 : : /*if ( 0 <= len && nLen+len < nLen_szLinearCT) {
785 : : if ( len ) {
786 : : strcpy( szLinearCT+nLen, szValue );
787 : : nLen += len;
788 : : bNext ++;
789 : : }
790 : : } else {
791 : : bOvfl = 1;
792 : : break;
793 : : }*/
794 : : }
795 : : }
796 : : }
797 : 1296 : *bOverflow |= bOvfl;
798 : :
799 : 1296 : nLen = strbuf->nUsedLength - nUsedLength0;
800 : :
801 : 1296 : return nLen;
802 : : }
803 : :
804 : :
805 : : /****************************************************************************
806 : : MakeHString( ... )
807 : :
808 : : nCtMode = 0: decimal
809 : : 2: compressed CT (Abc numbers)
810 : : ****************************************************************************/
811 : 166 : int MakeHString( int bAddDelim,
812 : : S_CHAR *LinearCT,
813 : : int nLenCT,
814 : : INCHI_IOS_STRING *strbuf,
815 : : int nCtMode,
816 : : int *bOverflow )
817 : : {
818 : : #define INIT_MIN_NUM_H (-4)
819 : : #define INIT_MAX_NUM_H 16
820 : : #define INIT_LEN_NUM_H (INIT_MAX_NUM_H - INIT_MIN_NUM_H + 1)
821 : :
822 : : /* produce output string; */
823 : 166 : int nUsedLength0 = 0, nLen = 0, len, i, iFirst, nVal, bOvfl = *bOverflow;
824 : : char szValue[2048];
825 : : const char *pH;
826 : 166 : int bNext = 0;
827 : :
828 : 166 : nUsedLength0 = strbuf->nUsedLength;
829 : :
830 : : /* add connection table string */
831 [ + - + - : 166 : if (!( nCtMode & CT_MODE_ABC_NUMBERS ) && !bOvfl && bAddDelim)
- + ]
832 : : {
833 : 0 : inchi_strbuf_printf( strbuf, "," );
834 : : /*if ( nLen_szLinearCT > 1 ) {
835 : : strcpy( szLinearCT, "," );
836 : : nLen ++;
837 : : } else {
838 : : bOvfl = 1;
839 : : }*/
840 : : }
841 [ + - + - : 166 : if (!bOvfl && 0 < nLenCT && LinearCT)
+ - ]
842 : : {
843 [ + - ]: 166 : if (nCtMode & CT_MODE_EQL_H_TOGETHER)
844 : : {
845 : 166 : int curMinH = INIT_MIN_NUM_H;
846 : 166 : int curMaxH = INIT_MAX_NUM_H;
847 : 166 : int curLenH = INIT_LEN_NUM_H;
848 : : int nInitNumH[INIT_LEN_NUM_H];
849 : 166 : int *nNumH = nInitNumH;
850 : : int numAt, curNumH;
851 : : int j, bOutOfRange, tot_num_no_H;
852 : : /* count atoms H */
853 : : do
854 : : {
855 : 166 : bOutOfRange = 0;
856 : 166 : tot_num_no_H = 0; /* number of atoms that have no H */
857 : 166 : memset( nNumH, 0, curLenH * sizeof( nNumH[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
858 [ + + ]: 8119 : for (i = 0; i < nLenCT; i++)
859 : : {
860 : 7953 : curNumH = LinearCT[i];
861 [ - + ]: 7953 : if (curNumH < curMinH)
862 : : {
863 : 0 : curMinH = curNumH;
864 : 0 : bOutOfRange++;
865 : : }
866 : : else
867 : : {
868 [ - + ]: 7953 : if (curNumH > curMaxH)
869 : : {
870 : 0 : curMaxH = curNumH;
871 : 0 : bOutOfRange++;
872 : : }
873 : : else
874 [ + - ]: 7953 : if (!bOutOfRange)
875 : : {
876 : 7953 : nNumH[curNumH - curMinH] ++;
877 : : }
878 : : }
879 : 7953 : tot_num_no_H += !curNumH;
880 : : }
881 [ + + ]: 166 : if (tot_num_no_H == nLenCT)
882 : : {
883 : 3 : return nLen; /* empty string */
884 : : }
885 [ - + ]: 163 : if (bOutOfRange)
886 : : {
887 : : /* for debug only */
888 [ # # ]: 0 : if (nNumH != nInitNumH)
889 : : {
890 : 0 : *bOverflow |= 1;
891 [ # # ]: 0 : inchi_free( nNumH );
892 : 0 : return nLen;
893 : : }
894 : : /* end debug */
895 : 0 : curLenH = curMaxH - curMinH + 1;
896 : 0 : nNumH = (int*) inchi_malloc( curLenH * sizeof( nNumH[0] ) );
897 [ # # ]: 0 : if (!nNumH)
898 : : {
899 : 0 : *bOverflow |= 1;
900 : 0 : return nLen;
901 : : }
902 : : }
903 : : }
904 [ - + ]: 163 : while (bOutOfRange); /* the loop may be executed 1 or 2 times only */
905 : :
906 [ + + ]: 3586 : for (curNumH = curMinH; curNumH <= curMaxH; curNumH++)
907 : : {
908 : 3423 : numAt = nNumH[curNumH - curMinH]; /* number of atoms that have curNumH atoms H */
909 [ + + + + ]: 3423 : if (!numAt || !curNumH)
910 : : {
911 : 3131 : continue; /* no atom has this number of H or number of H = 0 */
912 : : }
913 : 292 : j = 0;
914 [ + + + + ]: 5089 : while (j < nLenCT && numAt)
915 : : {
916 [ + + ]: 4797 : if (curNumH == LinearCT[j])
917 : : {
918 : 334 : iFirst = ++j;
919 : 334 : numAt--;
920 [ + + + + : 7706 : for (; j < nLenCT && curNumH == LinearCT[j] && numAt; j++)
+ - ]
921 : : {
922 : 7372 : numAt--;
923 : : }
924 [ - + ]: 334 : if (nCtMode & CT_MODE_ABC_NUMBERS)
925 : : {
926 : 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, iFirst );
927 : : }
928 : : else
929 : : {
930 [ + + ]: 334 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), bNext ? ITEM_DELIMETER : NULL, iFirst );
931 : 334 : bNext++; /* add a delimiter (comma) before all except the first */
932 : : }
933 [ + + ]: 334 : if (iFirst < j)
934 : : {
935 : : /* output last canonical number */
936 [ - + ]: 184 : if (nCtMode & CT_MODE_ABC_NUMBERS)
937 : : {
938 : 0 : len += MakeAbcNumber( szValue + len, ( int )sizeof( szValue ), NULL, j );
939 : : }
940 : : else
941 : : {
942 : 184 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, "-", j );
943 : : }
944 : : }
945 [ + + - + ]: 334 : if (!numAt || ( nCtMode & CT_MODE_ABC_NUMBERS ))
946 : : {
947 : : /* add number of H */
948 : : /* output number of H */
949 : 292 : nVal = curNumH;
950 [ - + ]: 292 : if (nCtMode & CT_MODE_ABC_NUMBERS)
951 : : {
952 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, nVal );
953 : : }
954 : : else
955 : : {
956 [ + - ]: 292 : pH = nVal > 0 ? "H" : "h";
957 : 292 : nVal = abs( nVal );
958 [ + + ]: 292 : if (nVal > 1)
959 : : {
960 : 204 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, pH, nVal );
961 : : }
962 : : else
963 : : {
964 : 88 : strcpy(szValue + len, pH);
965 : 88 : len++;
966 : : }
967 : : }
968 : : }
969 : : /* add to the output */
970 [ + - ]: 334 : if (len > 0)
971 : : {
972 : 334 : inchi_strbuf_printf( strbuf, "%-s", szValue );
973 : 334 : bNext++;
974 : : }
975 : : /*
976 : : if ( 0 <= len && nLen+len < nLen_szLinearCT ) {
977 : : if ( len ) {
978 : : strcpy( szLinearCT+nLen, szValue );
979 : : nLen += len;
980 : : bNext ++;
981 : : }
982 : : } else {
983 : : bOvfl = 1;
984 : : break;
985 : : }*/
986 : : }
987 : : else
988 : : {
989 : 4463 : j++;
990 : : }
991 : : }
992 : : }
993 [ - + ]: 163 : if (nNumH != nInitNumH)
994 : : {
995 [ # # ]: 0 : inchi_free( nNumH );
996 : : }
997 : : }
998 : : else
999 : : {
1000 : 0 : iFirst = 0;
1001 [ # # ]: 0 : for (i = iFirst + 1; i <= nLenCT/* && nLen < nLen_szLinearCT*/; i++)
1002 : : {
1003 [ # # # # ]: 0 : if (i < nLenCT && LinearCT[i] == LinearCT[iFirst])
1004 : : {
1005 : 0 : continue;
1006 : : }
1007 : : /* output identical values located at i = iFirst..i-1 */
1008 [ # # ]: 0 : if (LinearCT[iFirst])
1009 : : { /* output only non-zero values */
1010 : : /* first canonical number */
1011 : 0 : nVal = LinearCT[iFirst];
1012 : 0 : iFirst++;
1013 [ # # ]: 0 : if (nCtMode & CT_MODE_ABC_NUMBERS)
1014 : : {
1015 : 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, iFirst );
1016 : : }
1017 : : else
1018 : : {
1019 [ # # ]: 0 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), bNext ? ITEM_DELIMETER : NULL, iFirst );
1020 : : }
1021 [ # # ]: 0 : if (iFirst < i)
1022 : : {
1023 : : /* output last canonical number */
1024 [ # # ]: 0 : if (nCtMode & CT_MODE_ABC_NUMBERS)
1025 : : {
1026 : 0 : len += MakeAbcNumber( szValue + len, ( int )sizeof( szValue ), NULL, i );
1027 : : }
1028 : : else
1029 : : {
1030 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, "-", i );
1031 : : }
1032 : : }
1033 : : /* output number of H */
1034 [ # # ]: 0 : if (nCtMode & CT_MODE_ABC_NUMBERS)
1035 : : {
1036 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, nVal );
1037 : : }
1038 : : else
1039 : : {
1040 [ # # ]: 0 : pH = nVal > 0 ? "H" : "h";
1041 : 0 : nVal = abs( nVal );
1042 [ # # ]: 0 : if (nVal > 1)
1043 : : {
1044 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, pH, nVal );
1045 : : }
1046 : : else
1047 : : {
1048 : 0 : strcpy(szValue + len, pH); /* djb-rwth: GCC 14 false positive */
1049 : 0 : len++;
1050 : : }
1051 : : }
1052 [ # # ]: 0 : if (0 <= len/* && nLen+len < nLen_szLinearCT*/)
1053 : : {
1054 [ # # ]: 0 : if (len)
1055 : : {
1056 : 0 : inchi_strbuf_printf( strbuf, "%-s", szValue );
1057 : : /*strcpy( szLinearCT+nLen, szValue );
1058 : : nLen += len;*/
1059 : 0 : bNext++;
1060 : : }
1061 : : }
1062 : : else
1063 : : {
1064 : 0 : bOvfl = 1;
1065 : 0 : break;
1066 : : }
1067 : : }
1068 : 0 : iFirst = i;
1069 : : }
1070 : : }
1071 : : }
1072 : 163 : *bOverflow |= bOvfl;
1073 : :
1074 : 163 : nLen = strbuf->nUsedLength - nUsedLength0;
1075 : 163 : return nLen;
1076 : :
1077 : : #undef INIT_MIN_NUM_H
1078 : : #undef INIT_MAX_NUM_H
1079 : : #undef INIT_LEN_NUM_H
1080 : : }
1081 : :
1082 : :
1083 : : /****************************************************************************
1084 : : Prepare InChI atomic numbers substring
1085 : :
1086 : : nCtMode = 0: full
1087 : : 1: censored CT (no orphans, that CT should
1088 : : have only atoms with neighbors)
1089 : : 2: compressed CT (Abc numbers)
1090 : : ****************************************************************************/
1091 : 1296 : int MakeCtString( CANON_GLOBALS *pCG,
1092 : : AT_NUMB *LinearCT,
1093 : : int nLenCT,
1094 : : int bAddDelim,
1095 : : S_CHAR *nNum_H,
1096 : : int num_atoms,
1097 : : INCHI_IOS_STRING *strbuf,
1098 : : int nCtMode,
1099 : : int *bOverflow )
1100 : : {
1101 : :
1102 [ - + - - ]: 1296 : if (!nNum_H || !( nCtMode & CT_MODE_NO_ORPHANS ))
1103 : : {
1104 : 1296 : return MakeCtStringOld( LinearCT, nLenCT, bAddDelim, strbuf, nCtMode, bOverflow );
1105 : : }
1106 : : else
1107 : : {
1108 : 0 : return MakeCtStringNew( pCG, LinearCT, nLenCT, bAddDelim, nNum_H,
1109 : : num_atoms, strbuf, nCtMode, bOverflow );
1110 : : }
1111 : : }
1112 : :
1113 : :
1114 : : /****************************************************************************
1115 : : MakeTautString( )
1116 : :
1117 : : nCtMode = 0: full: decimal-only, with parentheses around t-groups
1118 : : 2: compressed CT: do not add comma before the output string if bAddDelim != 0
1119 : : do not add parentheses around t-groups
1120 : : atom canon numbers an Abc
1121 : : LinearCT format:
1122 : : N = number of tautomeric groups
1123 : : n = number of endpoints + 1 in a tautomeric group #1
1124 : : next INCHI_T_NUM_MOVABLE lines (any after the first non-zero):
1125 : : h = number of hydrogen atoms in the tautomeric group
1126 : : m = number of negative charges
1127 : : ... (the rest of the INCHI_T_NUM_MOVABLE has not been established, ignore them)
1128 : : c(1) = canonical number of the first atom in the t-group
1129 : : ...
1130 : : c(n-1) = canonical number of the last atom in the t-group
1131 : :
1132 : : ****************************************************************************/
1133 : 166 : int MakeTautString( AT_NUMB *LinearCT,
1134 : : int nLenCT,
1135 : : int bAddDelim,
1136 : : INCHI_IOS_STRING *strbuf,
1137 : : int nCtMode,
1138 : : int *bOverflow )
1139 : : {
1140 : : /* produce output string; */
1141 : 166 : int nUsedLength0 = 0, nLen = 0, len, i, bOvfl = *bOverflow;
1142 : : char szValue[2048];
1143 : : const char *p;
1144 : : int nValue, nGroupLen, iGroupOutputCount, bCompressed;
1145 : : /* make tautomer string */
1146 [ + + + - : 166 : if (!nLenCT || !LinearCT || !*LinearCT)
+ + ]
1147 : : {
1148 : 160 : return nLen;
1149 : : }
1150 : :
1151 : 6 : nUsedLength0 = strbuf->nUsedLength;
1152 : :
1153 : 6 : bCompressed = ( nCtMode & CT_MODE_ABC_NUMBERS );
1154 [ + - + - : 6 : if (!bCompressed && !bOvfl && bAddDelim)
+ + ]
1155 : : {
1156 : 3 : inchi_strbuf_printf( strbuf, "%s", COMMA_EXTRA_SPACE );
1157 : : /*if ( nLen_szLinearCT > 1+LEN_EXTRA_SPACE ) {
1158 : : strcpy( szLinearCT, COMMA_EXTRA_SPACE);
1159 : : nLen += 1+LEN_EXTRA_SPACE;
1160 : : } else {
1161 : : bOvfl = 1;
1162 : : }*/
1163 : : }
1164 : 6 : LinearCT++; /* bypass number of tautomeric groups */
1165 : 6 : nLenCT--;
1166 : :
1167 [ + - ]: 6 : if (!bOvfl)
1168 : : {
1169 [ + + ]: 55 : for (i = nGroupLen = iGroupOutputCount = 0; i < nLenCT/* && nLen < nLen_szLinearCT*/; i++)
1170 : : {
1171 : 49 : nValue = (int) LinearCT[i];
1172 [ + + ]: 49 : if (nGroupLen == iGroupOutputCount)
1173 : : {
1174 : 9 : nGroupLen = nValue;
1175 : 9 : iGroupOutputCount = 0;
1176 : : /* group delimiter (uncompressed) */
1177 [ + - ]: 9 : if (!bCompressed)
1178 : : {
1179 [ + + ]: 9 : if (!i)
1180 : : {
1181 : 6 : strcpy( szValue, "(" );
1182 : 6 : len = 1;
1183 : : }
1184 : : else
1185 : : {
1186 : 3 : strcpy( szValue, ")(" );
1187 : 3 : len = 2;
1188 : : }
1189 : : }
1190 : : else
1191 : : {
1192 : 0 : len = 0;
1193 : : }
1194 : : }
1195 : : else
1196 : : {
1197 [ - + - - ]: 40 : if (bCompressed && iGroupOutputCount >= INCHI_T_NUM_MOVABLE)
1198 : : {
1199 : : /* compressed canon number in Abc */
1200 : 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, nValue );
1201 : 0 : iGroupOutputCount++;
1202 : : }
1203 : : else
1204 : : {
1205 : : /* always output number of hydrogen atoms as a decimal */
1206 : : /* output leading space if: */
1207 : : /* (a) this is the first output value in compressed mode (i==1 && bCompressed) */
1208 : : /* (b) this is not the first output value in non-compressed mode ( iGroupOutputCount && !bCompressed) */
1209 [ - + ]: 40 : if (bCompressed)
1210 : : {
1211 : 0 : p = NULL;
1212 : 0 : len = 0;
1213 [ # # # # ]: 0 : switch (iGroupOutputCount)
1214 : : {
1215 : 0 : case 0:
1216 [ # # ]: 0 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), ( i == 1 ) ? ITEM_DELIMETER : NULL, nValue );
1217 : 0 : break;
1218 : 0 : case 1:
1219 : 0 : p = "-";
1220 : 0 : break;
1221 : 0 : case 2:
1222 : 0 : p = "+";
1223 : 0 : break;
1224 : : }
1225 [ # # ]: 0 : if (p)
1226 : : {
1227 [ # # # ]: 0 : switch (nValue)
1228 : : {
1229 : 0 : case 0:
1230 : 0 : len = 0;
1231 : 0 : break;
1232 : 0 : case 1:
1233 : 0 : strcpy(szValue, p);
1234 : 0 : len = (int) strlen( szValue );
1235 : 0 : break;
1236 : 0 : default:
1237 : 0 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), p, nValue );
1238 : 0 : break;
1239 : : }
1240 : : }
1241 : : }
1242 : : else
1243 : : {
1244 [ + + ]: 40 : if (iGroupOutputCount >= INCHI_T_NUM_MOVABLE)
1245 : : {
1246 : : /* canonical number of the atom in the tautomeric group */
1247 : 22 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), ITEM_DELIMETER, nValue );
1248 : : }
1249 : : else
1250 : : {
1251 : 18 : p = NULL;
1252 : 18 : len = 0;
1253 [ + + ]: 18 : if (nValue)
1254 : : {
1255 [ + - - - ]: 9 : switch (iGroupOutputCount)
1256 : : {
1257 : 9 : case 0:
1258 : 9 : p = "H";
1259 : 9 : break;
1260 : 0 : case 1:
1261 : 0 : p = "-";
1262 : 0 : break;
1263 : 0 : case 2:
1264 : 0 : p = "+";
1265 : 0 : break;
1266 : : }
1267 [ + - ]: 9 : if (p)
1268 : : {
1269 : : /* number of hydrogens */
1270 [ + + ]: 9 : if (nValue == 1)
1271 : : {
1272 : 7 : strcpy(szValue, p);
1273 : 7 : len = (int) strlen( szValue );
1274 : : }
1275 : : else
1276 : : {
1277 : 2 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), p, nValue );
1278 : : }
1279 : : }
1280 : : }
1281 : : }
1282 : : }
1283 : 40 : iGroupOutputCount++;
1284 : : }
1285 : : }
1286 : :
1287 [ + + ]: 49 : if (len > 0)
1288 : : {
1289 : 40 : inchi_strbuf_printf( strbuf, "%s", szValue );
1290 : : }
1291 : : /*if ( 0 <= len && nLen+len < nLen_szLinearCT ) {
1292 : : if ( len ) {
1293 : : strcpy( szLinearCT+nLen, szValue );
1294 : : nLen += len;
1295 : : }
1296 : : } else {
1297 : : bOvfl = 1;
1298 : : break;
1299 : : }*/
1300 : : }
1301 : :
1302 [ + - + - : 6 : if (!bOvfl && !bCompressed && i)
+ - ]
1303 : : {
1304 : 6 : inchi_strbuf_printf( strbuf, ")" );
1305 : : /*if ( nLen + 1 < nLen_szLinearCT ) {
1306 : : strcpy( szLinearCT+nLen, ")" );
1307 : : nLen ++;
1308 : : } else {
1309 : : bOvfl = 1;
1310 : : }*/
1311 : : }
1312 : : }
1313 : 6 : *bOverflow |= bOvfl;
1314 : :
1315 : 6 : nLen = strbuf->nUsedLength - nUsedLength0;
1316 : :
1317 : 6 : return nLen;
1318 : : }
1319 : :
1320 : :
1321 : : /****************************************************************************
1322 : : MakeCRVString( ... )
1323 : :
1324 : : nCtMode = 0: full
1325 : : 2: compressed CT
1326 : : 22+3s3: 22=canon. number; +3=charge; s=singlet (d=doublet, t=triplet, s is omitted if valence=0), 3 = valence
1327 : : 22+3.3, (charge, valence) 22.3 (valence) 22t3 (triplet, valence)
1328 : : Ab+3t4: Ab=canon. number; +3=charge or "." t=triplet (or s, d), 4=valence
1329 : : ****************************************************************************/
1330 : 26 : int MakeCRVString( ORIG_INFO *OrigInfo,
1331 : : int nLenCT,
1332 : : int bAddDelim,
1333 : : INCHI_IOS_STRING *strbuf,
1334 : : int nCtMode,
1335 : : int *bOverflow )
1336 : : {
1337 : : /* produce output string; */
1338 : 26 : int nUsedLength0 = 0, nLen = 0, len, k, bAbcNumbers;
1339 : 26 : int bOvfl = *bOverflow;
1340 : 26 : char szValue[2048] = { 0 };
1341 : 26 : int bNext = 0;
1342 : 26 : bAbcNumbers = ( nCtMode & CT_MODE_ABC_NUMBERS );
1343 : :
1344 : 26 : nUsedLength0 = strbuf->nUsedLength;
1345 : :
1346 : : /* add connection table string */
1347 [ + - - + ]: 26 : if (!bOvfl && bAddDelim)
1348 : : {
1349 : 0 : inchi_strbuf_printf( strbuf, ", " );
1350 : : /*if ( nLen_szLinearCT > 2 ) {
1351 : : strcpy( szLinearCT, ", " );
1352 : : nLen += 2;
1353 : : } else {
1354 : : bOvfl = 1;
1355 : : }*/
1356 : : }
1357 : :
1358 [ + - + + ]: 160 : for (k = 0; !bOvfl && k < nLenCT/* && nLen < nLen_szLinearCT*/; k++)
1359 : : {
1360 : : /* find the next non-empty entry */
1361 [ + + + - : 134 : if (OrigInfo[k].cCharge || OrigInfo[k].cRadical || OrigInfo[k].cUnusualValence)
+ + ]
1362 : : {
1363 [ - + ]: 46 : if (bAbcNumbers)
1364 : : {
1365 : : /*
1366 : : 3 items: Ad+3d4 (canon. numb=Ad, charge=+3, doublet, valence = 4
1367 : : 2 items: Ad.d4 Ad+3.4 Ad+3d
1368 : : 1 item: Ad+3 Ad.d Ad4
1369 : :
1370 : : dot output before radical: no charge, radical is present
1371 : : dot before valence: charge is present, no radical, valence is present
1372 : : */
1373 : 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), NULL, k + 1 );
1374 : :
1375 : : /* charge */
1376 [ # # ]: 0 : if (OrigInfo[k].cCharge)
1377 : : {
1378 [ # # ]: 0 : if (OrigInfo[k].cCharge > 0)
1379 : : {
1380 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, "+", OrigInfo[k].cCharge );
1381 : : }
1382 : : else
1383 : : {
1384 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, OrigInfo[k].cCharge );
1385 : : }
1386 : : }
1387 : : /* radical */
1388 [ # # ]: 0 : if (OrigInfo[k].cRadical)
1389 : : {
1390 [ # # ]: 0 : if (!OrigInfo[k].cCharge)
1391 : : {
1392 [ # # ]: 0 : if (len >= 2047) /* djb-rwth: fixing coverity ID #499515 */
1393 : : {
1394 : 0 : len = 2047;
1395 : 0 : goto early_break;
1396 : : }
1397 [ # # ]: 0 : else if (len < 0) /* djb-rwth: fixing coverity ID #500400 */
1398 : : {
1399 : 0 : len = 0;
1400 : 0 : goto early_break;
1401 : : }
1402 : : else
1403 : : {
1404 : 0 : szValue[len] = '.';
1405 : 0 : len++;
1406 : : }
1407 : : }
1408 [ # # ]: 0 : if (len >= 2047) /* djb-rwth: fixing coverity ID #499515 */
1409 : : {
1410 : 0 : len = 2047;
1411 : 0 : goto early_break;
1412 : : }
1413 [ # # ]: 0 : else if (len < 0) /* djb-rwth: fixing coverity ID #500382 */
1414 : : {
1415 : 0 : len = 0;
1416 : 0 : goto early_break;
1417 : : }
1418 : : else
1419 : : {
1420 [ # # # ]: 0 : switch (OrigInfo[k].cRadical)
1421 : : {
1422 : 0 : case 1:
1423 : : /* djb-rwth: fixing coverity ID #499515 -- false positive, len tested for overflow */
1424 : 0 : szValue[len] = 'd';
1425 : 0 : len++;
1426 : 0 : break;
1427 : 0 : case 2:
1428 : 0 : szValue[len] = 't';
1429 : 0 : len++;
1430 : 0 : break;
1431 : 0 : default:
1432 : 0 : szValue[len] = 'u';
1433 : 0 : len++;
1434 : 0 : break;
1435 : : }
1436 : : }
1437 : : }
1438 : : /* valence */
1439 [ # # ]: 0 : if (OrigInfo[k].cUnusualValence)
1440 : : {
1441 [ # # # # ]: 0 : if (OrigInfo[k].cCharge && !OrigInfo[k].cRadical)
1442 : : {
1443 [ # # ]: 0 : if (len >= 2047) /* djb-rwth: fixing coverity ID #499515 */
1444 : : {
1445 : 0 : len = 2047;
1446 : 0 : goto early_break;
1447 : : }
1448 [ # # ]: 0 : else if (len < 0) /* djb-rwth: fixing coverity ID #500382 */
1449 : : {
1450 : 0 : len = 0;
1451 : 0 : goto early_break;
1452 : : }
1453 : : else
1454 : : {
1455 : 0 : szValue[len] = '.';
1456 : 0 : len++;
1457 : : }
1458 : : }
1459 : 0 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, OrigInfo[k].cUnusualValence );
1460 : : }
1461 : : }
1462 : : else
1463 : : {
1464 : : /*
1465 : : 3 items: 22+3d4 (canon. numb=22, charge=+3, doublet, valence = 4
1466 : : 2 items: 22d4 22+3.4 22+3d
1467 : : 1 item: 22+3 22d 22.4
1468 : :
1469 : : dot output before valence:
1470 : : (a) charge, no radical, valence
1471 : : (b) no charge, no radical, valence
1472 : : that is, whenever valence is present and no radical
1473 : : */
1474 [ + + ]: 46 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), bNext ? ITEM_DELIMETER : NULL, k + 1 );
1475 : : /* charge */
1476 [ + + ]: 46 : if (OrigInfo[k].cCharge)
1477 : : {
1478 [ + + ]: 25 : if (OrigInfo[k].cCharge > 0)
1479 : : {
1480 : 3 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, "+", OrigInfo[k].cCharge );
1481 : : }
1482 : : else
1483 : : {
1484 : 22 : len += MakeDecNumber( szValue + len, ( int )sizeof( szValue ) - len, NULL, OrigInfo[k].cCharge );
1485 : : }
1486 : : }
1487 : : /* radical */
1488 [ - + ]: 46 : if (OrigInfo[k].cRadical)
1489 : : {
1490 [ # # ]: 0 : if (len >= 2047) /* djb-rwth: fixing coverity ID #499515 */
1491 : : {
1492 : 0 : len = 2047;
1493 : 0 : goto early_break;
1494 : : }
1495 [ # # ]: 0 : else if (len < 0) /* djb-rwth: fixing coverity ID #500382 */
1496 : : {
1497 : 0 : len = 0;
1498 : 0 : goto early_break;
1499 : : }
1500 : : else
1501 : : {
1502 [ # # # ]: 0 : switch (OrigInfo[k].cRadical)
1503 : : {
1504 : 0 : case 1:
1505 : 0 : szValue[len] = 'd'; /* djb-rwth: GCC 14 false positive */
1506 : 0 : len++;
1507 : 0 : break;
1508 : 0 : case 2:
1509 : 0 : szValue[len] = 't';
1510 : 0 : len++;
1511 : 0 : break;
1512 : 0 : default:
1513 : 0 : szValue[len] = 'u';
1514 : 0 : len++;
1515 : 0 : break;
1516 : : }
1517 : : }
1518 : : }
1519 : : /* valence */
1520 [ + + ]: 46 : if (OrigInfo[k].cUnusualValence)
1521 : : {
1522 [ + - ]: 23 : if (!OrigInfo[k].cRadical)
1523 : : {
1524 [ - + ]: 23 : if (len >= 2047) /* djb-rwth: fixing coverity ID #499515 */
1525 : : {
1526 : 0 : len = 2047;
1527 : 0 : goto early_break;
1528 : : }
1529 [ - + ]: 23 : else if (len < 0) /* djb-rwth: fixing coverity ID #500382 */
1530 : : {
1531 : 0 : len = 0;
1532 : 0 : goto early_break;
1533 : : }
1534 : : else
1535 : : {
1536 : 23 : szValue[len] = '.';
1537 : 23 : len++;
1538 : : }
1539 : : }
1540 : 23 : len += MakeDecNumber(szValue + len, (int)sizeof(szValue) - len, NULL, OrigInfo[k].cUnusualValence);
1541 : : }
1542 : : }
1543 : : }
1544 : : else
1545 : : {
1546 : 88 : len = 0;
1547 : : }
1548 : :
1549 : 134 : early_break:
1550 [ + + ]: 134 : if (len)
1551 : : {
1552 : 46 : szValue[len] = '\0';
1553 : 46 : inchi_strbuf_printf( strbuf, "%s", szValue );
1554 : 46 : bNext++;
1555 : 46 : szValue[0] = '\0';
1556 : : }
1557 : : /*if ( len && nLen+len < nLen_szLinearCT ) {
1558 : : strcpy( szLinearCT+nLen, szValue );
1559 : : nLen += len;
1560 : : bNext ++;
1561 : : } else
1562 : : if ( len ) {
1563 : : bOvfl = 1;
1564 : : break;
1565 : : }*/
1566 : : }
1567 : 26 : *bOverflow |= bOvfl;
1568 : :
1569 : 26 : nLen = strbuf->nUsedLength - nUsedLength0;
1570 : :
1571 : 26 : return nLen;
1572 : : }
1573 : :
1574 : :
1575 : : /****************************************************************************
1576 : : MakeEquString( ... )
1577 : :
1578 : : nCtMode = 0: full
1579 : : 2: compressed CT
1580 : : ****************************************************************************/
1581 : 47 : int MakeEquString( AT_NUMB *LinearCT,
1582 : : int nLenCT,
1583 : : int bAddDelim,
1584 : : INCHI_IOS_STRING *strbuf,
1585 : : int nCtMode,
1586 : : int *bOverflow )
1587 : : {
1588 : : /* produce output string; */
1589 : 47 : int nUsedLength0 = 0, nLen = 0, len, i, k, bAbcNumbers; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
1590 : 47 : int bOvfl = *bOverflow;
1591 : : char szValue[2048];
1592 : 47 : int bNext = 0;
1593 : :
1594 : 47 : nUsedLength0 = strbuf->nUsedLength;
1595 : :
1596 : 47 : bAbcNumbers = ( nCtMode & CT_MODE_ABC_NUMBERS );
1597 : : /* add connection table string */
1598 [ + - - + ]: 47 : if (!bOvfl && bAddDelim)
1599 : : {
1600 : 0 : inchi_strbuf_printf( strbuf, ", " );
1601 : : /*if ( nLen_szLinearCT > 2 ) {
1602 : : strcpy( szLinearCT, ", " );
1603 : : nLen += 2;
1604 : : } else {
1605 : : bOvfl = 1;
1606 : : }*/
1607 : : }
1608 [ + - + + ]: 3409 : for (k = 0; !bOvfl && k < nLenCT/* && nLen < nLen_szLinearCT*/; k++)
1609 : : {
1610 : : /* find the first equivalence number */
1611 [ + + ]: 3362 : if (k != (int) LinearCT[k] - 1)
1612 : : {
1613 : 1748 : continue;
1614 : : }
1615 [ + + ]: 754245 : for (i = k; i < nLenCT/* && nLen < nLen_szLinearCT*/; i++)
1616 : : {
1617 [ + + ]: 752631 : if (k != (int) LinearCT[i] - 1)
1618 : : {
1619 : 749370 : continue;
1620 : : }
1621 : : /* equivalence number: a minimal canon_number out of a group of equivalent atoms */
1622 : : /* is at canon_number-1 position of each equivalent atom. */
1623 [ - + ]: 3261 : if (bAbcNumbers)
1624 : : {
1625 [ # # # # ]: 0 : len = MakeAbcNumber( szValue, ( int )sizeof( szValue ), ( i == k && bNext ) ? ITEM_DELIMETER : NULL, i + 1 ); /* djb-rwth: ignoring LLVM warning: possible presence of global variables */
1626 : : }
1627 : : else
1628 : : {
1629 [ + + ]: 3261 : len = MakeDecNumber( szValue, ( int )sizeof( szValue ), ( i == k ) ? "(" : ITEM_DELIMETER, i + 1 ); /* djb-rwth: ignoring LLVM warning: possible presence of global variables */
1630 : : }
1631 : 3261 : inchi_strbuf_printf( strbuf, "%s", szValue );
1632 : 3261 : bNext++;
1633 : : /*if ( 0 <= len && nLen+len < nLen_szLinearCT ) {
1634 : : strcpy( szLinearCT+nLen, szValue );
1635 : : nLen += len;
1636 : : bNext ++;
1637 : : } else
1638 : : if ( 0 > len ) {
1639 : : bOvfl = 1;
1640 : : break;
1641 : : }*/
1642 : : }
1643 : 1614 : inchi_strbuf_printf( strbuf, ")" );
1644 : : /*if ( !bOvfl && !bAbcNumbers ) {
1645 : : if ( nLen + 2 < nLen_szLinearCT ) {
1646 : : strcpy( szLinearCT+nLen, ")" );
1647 : : nLen ++;
1648 : : } else {
1649 : : bOvfl = 1;
1650 : : }
1651 : : }*/
1652 : : }
1653 : 47 : *bOverflow |= bOvfl;
1654 : :
1655 : 47 : nLen = strbuf->nUsedLength - nUsedLength0;
1656 : :
1657 : 47 : return nLen;
1658 : : }
1659 : :
1660 : :
1661 : : /****************************************************************************
1662 : : MakeIsoAtomString( ... )
1663 : :
1664 : : nCtMode = 0: full
1665 : : 2: compressed CT
1666 : : ****************************************************************************/
1667 : 6 : int MakeIsoAtomString( INChI_IsotopicAtom *IsotopicAtom,
1668 : : int nNumberOfIsotopicAtoms,
1669 : : INCHI_IOS_STRING *strbuf,
1670 : : int nCtMode,
1671 : : int *bOverflow )
1672 : : {
1673 : : /* produce output string; */
1674 : 6 : int nUsedLength0 = 0, nLen = 0, len, tot_len, ret, i, j, bOvfl = *bOverflow;
1675 : : char szValue[2048];
1676 : : char *p;
1677 : : int nValue;
1678 : 6 : int bAbcNumbers = ( nCtMode & CT_MODE_ABC_NUMBERS );
1679 : : static const char letter[] = "itdh";
1680 : : static const char *h[] = { "T", "D", "H" };
1681 : : static const char *sign[] = { "-", "+" };
1682 : :
1683 : 6 : nUsedLength0 = strbuf->nUsedLength;
1684 : :
1685 [ + - ]: 6 : if (!bOvfl)
1686 : : {
1687 [ + + ]: 12 : for (i = 0; i < nNumberOfIsotopicAtoms/* && nLen < nLen_szLinearCT*/; i++)
1688 : : {
1689 : 6 : p = szValue;
1690 : 6 : tot_len = 0;
1691 [ + + ]: 36 : for (j = 0; j < 5; j++)
1692 : : {
1693 : 30 : len = 0;
1694 [ + + + + : 30 : switch (j)
+ - ]
1695 : : {
1696 : 6 : case 0:
1697 : 6 : nValue = (int) IsotopicAtom[i].nAtomNumber;
1698 : 6 : break;
1699 : 6 : case 1:
1700 : 6 : nValue = (int) IsotopicAtom[i].nIsoDifference;
1701 : 6 : break;
1702 : 6 : case 2:
1703 : 6 : nValue = (int) IsotopicAtom[i].nNum_T;
1704 : 6 : break;
1705 : 6 : case 3:
1706 : 6 : nValue = (int) IsotopicAtom[i].nNum_D;
1707 : 6 : break;
1708 : 6 : case 4:
1709 : 6 : nValue = (int) IsotopicAtom[i].nNum_H;
1710 : 6 : break;
1711 : : }
1712 [ + + ]: 30 : if (!j)
1713 : : {
1714 : : /* atom canonical number */
1715 [ - + + - ]: 12 : len = ( bAbcNumbers ? MakeAbcNumber : MakeDecNumber )
1716 : : ( p, ( int )sizeof( szValue ) - tot_len,
1717 [ - + ]: 6 : bAbcNumbers ? NULL : ( i ? ITEM_DELIMETER : EXTRA_SPACE ), nValue
1718 : : );
1719 : : }
1720 : : else
1721 : : {
1722 [ - + ]: 24 : if (bAbcNumbers)
1723 : : {
1724 : : /* Abc output */
1725 [ # # # ]: 0 : switch (j)
1726 : : {
1727 : 0 : case 1: /* nIsoDifference */
1728 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, NULL, nValue );
1729 : 0 : break;
1730 : 0 : case 2: /* nNum_T */
1731 : : case 3: /* nNum_D */
1732 : : case 4: /* nNum_H */
1733 [ # # ]: 0 : if (nValue)
1734 : : {
1735 [ # # ]: 0 : if (( int )sizeof( szValue ) - tot_len > 1)
1736 : : {
1737 : 0 : p[len++] = letter[j - 1];
1738 [ # # ]: 0 : if (1 == nValue)
1739 : : {
1740 : 0 : p[len] = '\0';
1741 : : }
1742 : : else
1743 : : {
1744 : 0 : ret = MakeDecNumber( p + len, ( int )sizeof( szValue ) - tot_len - len, NULL, nValue );
1745 [ # # ]: 0 : len = ( ret >= 0 ) ? len + ret : ret;
1746 : : }
1747 : : }
1748 : : else
1749 : : {
1750 : 0 : len = -1; /* overflow */
1751 : : }
1752 : : }
1753 : : }
1754 : : }
1755 : : else
1756 : : {
1757 [ + + ]: 24 : if (nValue)
1758 : : {
1759 [ + + ]: 6 : if (j == 1)
1760 : : {
1761 : : /* Decimal output */
1762 : : /* signed isotopic mass difference */
1763 : 1 : int subtract = ( nValue > 0 );
1764 : : /* (n = mass difference) > 0 corresponds to nValue = n+1 */
1765 : : /* subtract 1 from it so that mass difference for 35Cl or 12C is zero */
1766 : 1 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, sign[nValue >= 0], abs( nValue - subtract ) );
1767 : : }
1768 : : else
1769 : : {
1770 : : /* hydrogen isotope */
1771 [ - + ]: 5 : if (nValue != 1)
1772 : : {
1773 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, h[j - 2], nValue );
1774 : : }
1775 : : else
1776 : : {
1777 [ + - ]: 5 : if (( int )sizeof( szValue ) - tot_len > 1)
1778 : : {
1779 : 5 : strcpy(p, h[j - 2]);
1780 : 5 : len = 1;
1781 : : }
1782 : : else
1783 : : {
1784 : 0 : len = -1; /* overflow */
1785 : : }
1786 : : }
1787 : : }
1788 : : }
1789 : : else
1790 : : {
1791 : 18 : continue; /* do not write zeroes */
1792 : : }
1793 : : }
1794 : : }
1795 [ - + ]: 12 : if (len < 0)
1796 : : {
1797 : 0 : bOvfl = 1;
1798 : 0 : break;
1799 : : }
1800 : 12 : tot_len += len;
1801 : 12 : p += len;
1802 : : }
1803 : 6 : inchi_strbuf_printf( strbuf, "%s", szValue );
1804 : : /*if ( nLen+tot_len < nLen_szLinearCT )
1805 : : {
1806 : : memcpy( szLinearCT+nLen, szValue, tot_len+1 );
1807 : : nLen += tot_len;
1808 : : }
1809 : : else
1810 : : {
1811 : : bOvfl = 1;
1812 : : break;
1813 : : }*/
1814 : : }
1815 : : }
1816 : 6 : *bOverflow |= bOvfl;
1817 : :
1818 : 6 : nLen = strbuf->nUsedLength - nUsedLength0;
1819 : :
1820 : 6 : return nLen;
1821 : : }
1822 : :
1823 : :
1824 : : /****************************************************************************/
1825 : 0 : int MakeIsoTautString( INChI_IsotopicTGroup *IsotopicTGroup,
1826 : : int nNumberOfIsotopicTGroups,
1827 : : INCHI_IOS_STRING *strbuf,
1828 : : int nCtMode,
1829 : : int *bOverflow )
1830 : : {
1831 : : /* produce output string; */
1832 : 0 : int nUsedLength0 = 0, nLen = 0, len, tot_len, i, j, bOvfl = *bOverflow;
1833 : : /* djb-rwth: removing redundant variables */
1834 : : char szValue[2048];
1835 : : char *p;
1836 : : int nValue;
1837 : 0 : int bAbcNumbers = ( nCtMode & CT_MODE_ABC_NUMBERS );
1838 : : static const char letter[] = "tdh";
1839 : : static const char *h[] = { "T", "D", "H" };
1840 : :
1841 : 0 : nUsedLength0 = strbuf->nUsedLength;
1842 : :
1843 : : /* add connection table string */
1844 : : /* djb-rwth: removing redundant code */
1845 [ # # ]: 0 : if (!bOvfl)
1846 : : {
1847 [ # # ]: 0 : for (i = 0; i < nNumberOfIsotopicTGroups/* && nLen < nLen_szLinearCT*/; i++)
1848 : : {
1849 : 0 : p = szValue;
1850 : 0 : tot_len = 0;
1851 [ # # ]: 0 : for (j = 0; j < 4; j++)
1852 : : {
1853 [ # # # # : 0 : switch (j)
# ]
1854 : : {
1855 : 0 : case 0:
1856 : 0 : nValue = (int) IsotopicTGroup[i].nTGroupNumber;
1857 : 0 : break;
1858 : 0 : case 1:
1859 : 0 : nValue = (int) IsotopicTGroup[i].nNum_T;
1860 : 0 : break;
1861 : 0 : case 2:
1862 : 0 : nValue = (int) IsotopicTGroup[i].nNum_D;
1863 : 0 : break;
1864 : 0 : case 3:
1865 : 0 : nValue = (int) IsotopicTGroup[i].nNum_H;
1866 : 0 : break;
1867 : : }
1868 [ # # ]: 0 : if (!j)
1869 : : {
1870 : : /* atom canonical number */
1871 [ # # # # ]: 0 : len = ( bAbcNumbers ? MakeAbcNumber : MakeDecNumber )
1872 : : ( p, ( int )sizeof( szValue ) - tot_len,
1873 [ # # ]: 0 : bAbcNumbers ? NULL : ( i ? ITEM_DELIMETER : EXTRA_SPACE ),
1874 : : nValue
1875 : : );
1876 : : }
1877 : : else
1878 [ # # ]: 0 : if (nValue)
1879 : : {
1880 [ # # ]: 0 : if (bAbcNumbers)
1881 : : {
1882 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, NULL, nValue );
1883 [ # # ]: 0 : if (len > 0)
1884 : : { /* make sure overflow has not happened */
1885 [ # # ]: 0 : if (( int )sizeof( szValue ) - tot_len - len > 1)
1886 : : {
1887 : 0 : p[len++] = letter[j - 1];
1888 : 0 : p[len] = '\0';
1889 : : }
1890 : : else
1891 : : {
1892 : 0 : len = -1; /* overflow */
1893 : : }
1894 : : }
1895 : : }
1896 : : else
1897 : : {
1898 : : /* hydrogen isotope */
1899 [ # # ]: 0 : if (nValue != 1)
1900 : : {
1901 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, h[j - 1], nValue );
1902 : : }
1903 : : else
1904 [ # # ]: 0 : if (( int )sizeof( szValue ) - tot_len > 1)
1905 : : {
1906 : 0 : strcpy(p, h[j - 1]);
1907 : 0 : len = 1;
1908 : : }
1909 : : else
1910 : : {
1911 : 0 : len = -1; /* overflow */
1912 : : }
1913 : : }
1914 : : }
1915 : : else
1916 : : {
1917 : 0 : continue; /* do not write zeroes */
1918 : : }
1919 [ # # ]: 0 : if (len < 0)
1920 : : {
1921 : 0 : bOvfl = 1;
1922 : 0 : break;
1923 : : }
1924 : 0 : p += len;
1925 : 0 : tot_len += len;
1926 : : }
1927 : 0 : inchi_strbuf_printf( strbuf, "%s", szValue );
1928 : : /*if ( nLen+tot_len < nLen_szLinearCT ) {
1929 : : memcpy( szLinearCT+nLen, szValue, tot_len+1 );
1930 : : nLen += tot_len;
1931 : : } else {
1932 : : bOvfl = 1;
1933 : : break;
1934 : : }*/
1935 : : }
1936 : : }
1937 : 0 : *bOverflow |= bOvfl;
1938 : :
1939 : 0 : nLen = strbuf->nUsedLength - nUsedLength0;
1940 : :
1941 : 0 : return nLen;
1942 : : }
1943 : :
1944 : :
1945 : : /****************************************************************************/
1946 : 0 : int MakeIsoHString( int num_iso_H[],
1947 : : INCHI_IOS_STRING *strbuf,
1948 : : int nCtMode,
1949 : : int *bOverflow )
1950 : : {
1951 : : /* produce output string; */
1952 : 0 : int nUsedLength0 = 0, nLen = 0, len, tot_len, j, bOvfl = *bOverflow;
1953 : : /* djb-rwth: removing redundant variables */
1954 : : char szValue[2048];
1955 : : char *p;
1956 : : int nValue;
1957 : 0 : int bAbcNumbers = ( nCtMode & CT_MODE_ABC_NUMBERS );
1958 : : static const char letter[] = "tdh";
1959 : : static const char *h[] = { "T", "D", "H" };
1960 : :
1961 : 0 : nUsedLength0 = strbuf->nUsedLength;
1962 : :
1963 : : /* add connection table string */
1964 : : /* djb-rwth: removing redundant code */
1965 [ # # ]: 0 : if (!bOvfl)
1966 : : {
1967 : 0 : p = szValue;
1968 : 0 : tot_len = 0;
1969 [ # # ]: 0 : for (j = 1; j < 4; j++)
1970 : : {
1971 : 0 : nValue = num_iso_H[NUM_H_ISOTOPES - j];/* j: 1=>T, 2=>D, 3=>1H */
1972 [ # # ]: 0 : if (nValue)
1973 : : {
1974 [ # # ]: 0 : if (bAbcNumbers)
1975 : : {
1976 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, NULL, nValue );
1977 [ # # ]: 0 : if (len > 0)
1978 : : {
1979 : : /* make sure overflow has not happened */
1980 [ # # ]: 0 : if (( int )sizeof( szValue ) - tot_len - len > 1)
1981 : : {
1982 : 0 : p[len++] = letter[j - 1];
1983 : 0 : p[len] = '\0';
1984 : : }
1985 : : else
1986 : : {
1987 : 0 : len = -1; /* overflow */
1988 : : }
1989 : : }
1990 : : }
1991 : : else
1992 : : {
1993 : : /* hydrogen isotope */
1994 [ # # ]: 0 : if (nValue != 1)
1995 : : {
1996 : 0 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, h[j - 1], nValue );
1997 : : }
1998 : : else
1999 : : {
2000 [ # # ]: 0 : if (( int )sizeof( szValue ) - tot_len > 1)
2001 : : {
2002 : 0 : strcpy(p, h[j - 1]);
2003 : 0 : len = 1;
2004 : : }
2005 : : else
2006 : : {
2007 : 0 : len = -1; /* overflow */
2008 : : }
2009 : : }
2010 : : }
2011 : : }
2012 : : else
2013 : : {
2014 : 0 : continue; /* do not write zeroes */
2015 : : }
2016 [ # # ]: 0 : if (len < 0)
2017 : : {
2018 : 0 : bOvfl = 1;
2019 : 0 : break;
2020 : : }
2021 : 0 : p += len;
2022 : 0 : tot_len += len;
2023 : : }
2024 : 0 : inchi_strbuf_printf( strbuf, "%s", szValue );
2025 : : /*if ( nLen+tot_len < nLen_szLinearCT ) {
2026 : : memcpy( szLinearCT+nLen, szValue, tot_len+1 );
2027 : : nLen += tot_len;
2028 : : } else {
2029 : : bOvfl = 1;
2030 : : }*/
2031 : : }
2032 : 0 : *bOverflow |= bOvfl;
2033 : :
2034 : 0 : nLen = strbuf->nUsedLength - nUsedLength0;
2035 : :
2036 : 0 : return nLen;
2037 : : }
2038 : :
2039 : :
2040 : : /****************************************************************************/
2041 : 73 : int MakeStereoString( AT_NUMB *at1,
2042 : : AT_NUMB *at2,
2043 : : S_CHAR *parity,
2044 : : int bAddDelim,
2045 : : int nLenCT,
2046 : : INCHI_IOS_STRING *strbuf,
2047 : : int nCtMode,
2048 : : int *bOverflow )
2049 : : {
2050 : : /* produce output string; */
2051 : 73 : int nUsedLength0 = 0, nLen = 0, len, tot_len, i, j, bOvfl = *bOverflow;
2052 : : char szValue[2048];
2053 : : char *p;
2054 : : int nValue;
2055 : : static const char parity_char[] = "!-+u?";
2056 : :
2057 : 73 : nUsedLength0 = strbuf->nUsedLength;
2058 : 73 : bAddDelim = 0;
2059 : :
2060 [ + - ]: 73 : if (!bOvfl)
2061 : : {
2062 [ + + ]: 306 : for (i = 0; i < nLenCT/* && nLen < nLen_szLinearCT*/; i++)
2063 : : {
2064 : 233 : p = szValue;
2065 : 233 : tot_len = 0;
2066 [ + + ]: 932 : for (j = 0; j < 3; j++)
2067 : : {
2068 [ + + + - ]: 699 : if (j == 0 && at1)
2069 : : {
2070 : 233 : nValue = (int) at1[i];
2071 : : }
2072 : : else
2073 : : {
2074 [ + + + + ]: 466 : if (j == 1 && at2)
2075 : : {
2076 : 1 : nValue = (int) at2[i];
2077 : : }
2078 : : else
2079 : : {
2080 [ + + + - ]: 465 : if (j == 2 && parity)
2081 : : {
2082 : 233 : nValue = (int) parity[i];
2083 : : }
2084 : : else
2085 : : {
2086 : 232 : continue;
2087 : : }
2088 : : }
2089 : : }
2090 [ - + ]: 467 : if (nCtMode & CT_MODE_ABC_NUMBERS)
2091 : : {
2092 [ # # ]: 0 : len = ( j == 2 ? MakeDecNumber : MakeAbcNumber )( p, ( int )sizeof( szValue ) - tot_len, NULL, nValue );
2093 : : }
2094 : : else
2095 : : {
2096 [ + + ]: 467 : if (j < 2)
2097 : : {
2098 [ + + + + : 234 : len = MakeDecNumber( p, ( int )sizeof( szValue ) - tot_len, tot_len ? "-" : ( i || bAddDelim ) ? ITEM_DELIMETER : NULL, nValue );
- + ]
2099 : : }
2100 : : else
2101 : : {
2102 [ + - ]: 233 : if (tot_len + 1 < ( int )sizeof( szValue ))
2103 : : {
2104 [ + - + - ]: 233 : *p++ = ( 0 <= nValue && nValue <= 4 ) ? parity_char[nValue] : parity_char[0];
2105 : 233 : *p = '\0';
2106 : 233 : len = 1;
2107 : : }
2108 : : else
2109 : : {
2110 : 0 : len = -1; /* Overflow */
2111 : : }
2112 : : }
2113 : : }
2114 [ - + ]: 467 : if (len < 0)
2115 : : {
2116 : 0 : bOvfl = 1;
2117 : 0 : break;
2118 : : }
2119 : 467 : p += len;
2120 : 467 : tot_len += len;
2121 : : }
2122 : :
2123 : 233 : inchi_strbuf_printf( strbuf, "%s", szValue );
2124 : : /*
2125 : : if ( nLen+tot_len < nLen_szLinearCT ) {
2126 : : memcpy( szLinearCT+nLen, szValue, tot_len+1 );
2127 : : nLen += tot_len;
2128 : : } else {
2129 : : bOvfl = 1;
2130 : : break;
2131 : : }*/
2132 : : }
2133 : : }
2134 : 73 : *bOverflow |= bOvfl;
2135 : :
2136 : 73 : nLen = strbuf->nUsedLength - nUsedLength0;
2137 : :
2138 : 73 : return nLen;
2139 : : }
2140 : :
2141 : : /**
2142 : : * @brief Compares two integers for qsort.
2143 : : *
2144 : : * @param a First integer pointer.
2145 : : * @param b Second integer pointer.
2146 : : * @return Returns negative, zero, or positive value based on comparison.
2147 : : */
2148 : 207 : int compare_ints(const void *a, const void *b) {
2149 : 207 : int arg1 = *(const int *)a;
2150 : 207 : int arg2 = *(const int *)b;
2151 : 207 : return (arg1 > arg2) - (arg1 < arg2);
2152 : : }
2153 : :
2154 : : /**
2155 : : * @brief Compares the third value of two integer arrays for qsort. Used to sort enhanced stereochemistry groups based on the canonical atom number of the first atom in the group.
2156 : : *
2157 : : * @param a First integer array pointer.
2158 : : * @param b Second integer array pointer.
2159 : : * @return Returns negative, zero, or positive value based on comparison.
2160 : : */
2161 : 113 : int compare_third_value(const void *a, const void *b) {
2162 : 113 : const int *arr1 = *(const int **)a;
2163 : 113 : const int *arr2 = *(const int **)b;
2164 : 113 : return arr1[2] - arr2[2];
2165 : : }
2166 : :
2167 : : /**
2168 : : * @brief Creates the enhanced stereochemistry string for the s - layer.
2169 : : *
2170 : : * @param pAux Pointer to the INCHI_AUX structure.
2171 : : * @param conf_stereo_string Pointer to the configuration stereochemistry string (abs, rel, rac).
2172 : : * @param enh_stereo Pointer to list of enhanced stereochemistry groups.
2173 : : * @param nof_stereo_groups Number of enhanced stereochemistry groups.
2174 : : * @param strbuf Pointer to the output string buffer.
2175 : : * @param nCtMode Mode flag for string representation.
2176 : : * @param bOverflow Pointer to overflow flag.
2177 : : * @return Returns the length of the created string.
2178 : : */
2179 : 138 : int MakeEnhStereoString( INChI_Aux *pAux,
2180 : : INCHI_IOS_STRING *strbuf,
2181 : : const char* conf_stereo_string,
2182 : : int **enh_stereo,
2183 : : int nof_stereo_groups,
2184 : : int nCtMode,
2185 : : int *bOverflow )
2186 : : {
2187 : 138 : int tot_len = 0;
2188 : 138 : int count_added = 0;
2189 : :
2190 [ - + ]: 138 : if (pAux == NULL) {
2191 : 0 : return 0;
2192 : : }
2193 : :
2194 [ + + ]: 138 : if (enh_stereo == NULL) {
2195 : 46 : return 0;
2196 : : }
2197 : :
2198 [ - + ]: 92 : if (nof_stereo_groups < 1) {
2199 : 0 : return 0;
2200 : : }
2201 : :
2202 : 92 : tot_len += MakeDelim( conf_stereo_string, strbuf, bOverflow );
2203 : :
2204 : 92 : int **enh_stereo_canon = (int**)inchi_calloc(nof_stereo_groups, sizeof(int*));
2205 : :
2206 : : // Converts the original atom numbers in the enhanced stereochemistry groups to canonical atom numbers
2207 : : // and sorts the atoms within each group based on their canonical atom numbers. This ensures that the order of
2208 : : // atoms in the string representation is consistent and does not depend on the order of atoms in the input data.
2209 : : // enh_stereo_canon is a 2D array where each row corresponds to an enhanced stereochemistry group. The first element
2210 : : // of each row (index 0) stores the number of atoms in the group, the second element (index 1) stores the count of found
2211 : : // atoms, and the remaining elements (starting from index 2) store the canonical atom numbers of the atoms in the group.
2212 [ + + ]: 267 : for (int i = 0; i < nof_stereo_groups; i++) {
2213 : 175 : const int *atom_numbers = &enh_stereo[i][2];
2214 : 175 : int count_found_atoms = 0;
2215 : 175 : int nof_atoms = enh_stereo[i][1];
2216 : :
2217 : 175 : enh_stereo_canon[i] = (int*)inchi_calloc(nof_atoms + 2, sizeof(int));
2218 : 175 : enh_stereo_canon[i][0] = nof_atoms;
2219 [ + + ]: 507 : for (int j = 0; j < nof_atoms; j++) {
2220 : :
2221 : 332 : int orig_atom_num = atom_numbers[j];
2222 : 332 : int canon_atom_num = get_canonical_atom_number(pAux, orig_atom_num);
2223 [ + + ]: 332 : if (canon_atom_num != -1) {
2224 : 163 : count_found_atoms++;
2225 : : } else {
2226 : 169 : canon_atom_num = INT_MAX;
2227 : : }
2228 : :
2229 : 332 : enh_stereo_canon[i][j + 2] = canon_atom_num;
2230 : : }
2231 : 175 : enh_stereo_canon[i][1] = count_found_atoms;
2232 : :
2233 [ + + ]: 175 : if (nof_atoms > 1) {
2234 : 92 : qsort(&enh_stereo_canon[i][2], nof_atoms, sizeof(int), compare_ints);
2235 : : }
2236 : : }
2237 : :
2238 : : // Sorts the enhanced stereochemistry groups based on the canonical atom number of the first atom in the group.
2239 : : // This ensures that the groups are always in a consistent order in the string representation, regardless of the
2240 : : // order they were added to the input data (e.g. AND1, AND2, ... or OR1, OR2, ...).
2241 : 92 : qsort(enh_stereo_canon, nof_stereo_groups, sizeof(int*), compare_third_value);
2242 : :
2243 : : // Creates the string for the stereo group based on the canonical atom numbers of the atoms in the group. If no
2244 : : // atoms were found for the group, it will not be added to the string.
2245 [ + + ]: 267 : for (int i = 0; i < nof_stereo_groups; i++) {
2246 : 175 : int nof_atoms = enh_stereo_canon[i][0];
2247 : 175 : int nof_found_atoms = enh_stereo_canon[i][1];
2248 : :
2249 [ + + ]: 175 : if (nof_found_atoms > 0) {
2250 : 114 : tot_len += MakeDelim( "(", strbuf, bOverflow );
2251 [ + + ]: 277 : for (int j = 0; j < nof_found_atoms; j++) {
2252 : 163 : tot_len += MakeNumber_EnhStereo( enh_stereo_canon[i][j + 2], "", strbuf, nCtMode, bOverflow );
2253 : 163 : count_added++;
2254 : :
2255 [ + + ]: 163 : if ((j + 1) < nof_found_atoms) {
2256 : 49 : tot_len += MakeDelim( ",", strbuf, bOverflow );
2257 : : }
2258 : : }
2259 : 114 : tot_len += MakeDelim( ")", strbuf, bOverflow );
2260 : : }
2261 : : }
2262 : :
2263 [ + + ]: 267 : for (int i = 0; i < nof_stereo_groups; i++) {
2264 [ + - ]: 175 : inchi_free(enh_stereo_canon[i]);
2265 : : }
2266 [ + - ]: 92 : inchi_free(enh_stereo_canon);
2267 : :
2268 : : // Removes the last value for the stereo group (1,2,3) if no atoms were added to the string
2269 [ + + ]: 92 : if (count_added == 0) {
2270 [ + - + - ]: 8 : if (strbuf && strbuf->nUsedLength > 0) {
2271 : 8 : strbuf->nUsedLength--;
2272 : 8 : strbuf->pStr[strbuf->nUsedLength] = '\0';
2273 : : }
2274 : :
2275 : 8 : tot_len = tot_len - 1;
2276 : : }
2277 : :
2278 : 92 : return tot_len;
2279 : : }
2280 : :
2281 : : /**
2282 : : * @brief Creates the string for the s - layer based on the enhanced stereochemistry information.
2283 : : *
2284 : : * @param orig_inp_data Pointer to the original atom data.
2285 : : * @param pINChISort Pointer to the INCHI_SORT structure.
2286 : : * @param bOutType Output type flag.
2287 : : * @param num_components Number of components in the molecule.
2288 : : * @param strbuf Pointer to the output string buffer.
2289 : : * @param nCtMode Mode flag for string representation.
2290 : : * @param bOverflow Pointer to overflow flag.
2291 : : * @return Returns the length of the created string.
2292 : : */
2293 : 36 : int MakeSlayerString( ORIG_ATOM_DATA *orig_inp_data,
2294 : : INCHI_SORT *pINChISort,
2295 : : INCHI_IOS_STRING *strbuf,
2296 : : int bOutType,
2297 : : int num_components,
2298 : : int nCtMode,
2299 : : int *bOverflow )
2300 : : {
2301 : :
2302 : 36 : int tot_len = 0;
2303 : : int ii;
2304 : :
2305 : 36 : const char* x_abs = "1";
2306 : 36 : const char* x_rel = "2";
2307 : 36 : const char* x_rac = "3";
2308 : :
2309 : 36 : const INCHI_SORT *is = NULL;
2310 : 36 : const INCHI_SORT *is0 = pINChISort;
2311 : :
2312 : : // INChI *pINChI = NULL;
2313 : 36 : INChI_Aux *pAux = NULL;
2314 : :
2315 : 36 : char **dictionary = (char**)inchi_calloc(ENH_STEREO_DICT_SIZE, sizeof(char*));
2316 : 36 : int *counts = (int*)inchi_calloc(ENH_STEREO_DICT_SIZE, sizeof(int));
2317 : :
2318 [ + + ]: 3636 : for (int i = 0; i < ENH_STEREO_DICT_SIZE; i++) {
2319 : 3600 : dictionary[i] = NULL;
2320 : 3600 : counts[i] = 0;
2321 : : }
2322 : :
2323 : 36 : INCHI_IOS_STRING tmpbuf = {0};
2324 : :
2325 [ + - + + ]: 82 : for (int cur_c = 0; !*bOverflow && cur_c < num_components; cur_c++)
2326 : : {
2327 : :
2328 : 46 : is = is0 + cur_c;
2329 : : // pINChI = ( 0 <= ( ii = GET_II( bOutType, is ) ) ) ? is->pINChI[ii] : NULL;
2330 [ - + - - : 46 : pAux = ( 0 <= ( ii = GET_II( bOutType, is ) ) ) ? is->pINChI_Aux[ii] : NULL;
- - - - -
- - - - -
+ - + - +
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - ]
2331 : :
2332 : 46 : inchi_strbuf_init(&tmpbuf, INCHI_STRBUF_INITIAL_SIZE, INCHI_STRBUF_SIZE_INCREMENT);
2333 : :
2334 : : // s1
2335 : 92 : tot_len += MakeEnhStereoString( pAux,
2336 : : &tmpbuf,
2337 : : x_abs,
2338 : 46 : orig_inp_data->v3000->lists_steabs,
2339 : 46 : orig_inp_data->v3000->n_steabs,
2340 : : nCtMode,
2341 : : bOverflow);
2342 : :
2343 : : // s2
2344 : 92 : tot_len += MakeEnhStereoString( pAux,
2345 : : &tmpbuf,
2346 : : x_rel,
2347 : 46 : orig_inp_data->v3000->lists_sterel,
2348 : 46 : orig_inp_data->v3000->n_sterel,
2349 : : nCtMode,
2350 : : bOverflow);
2351 : :
2352 : : // s3
2353 : 92 : tot_len += MakeEnhStereoString( pAux,
2354 : : &tmpbuf,
2355 : : x_rac,
2356 : 46 : orig_inp_data->v3000->lists_sterac,
2357 : 46 : orig_inp_data->v3000->n_sterac,
2358 : : nCtMode,
2359 : : bOverflow);
2360 : :
2361 : 46 : int found = 0;
2362 [ + + ]: 4147 : for (int i = 0; i < ENH_STEREO_DICT_SIZE; i++) {
2363 [ + + + + ]: 4106 : if (dictionary[i] && strcmp(tmpbuf.pStr, dictionary[i]) == 0) {
2364 : 5 : counts[i]++;
2365 : 5 : found = 1;
2366 : 5 : break;
2367 : : }
2368 : : }
2369 [ + + ]: 46 : if (!found) {
2370 [ + - ]: 47 : for (int i = 0; i < ENH_STEREO_DICT_SIZE; i++) {
2371 [ + + ]: 47 : if (dictionary[i] == NULL) {
2372 : 41 : dictionary[i] = strdup(tmpbuf.pStr);
2373 : 41 : counts[i] = 1;
2374 : 41 : break;
2375 : : }
2376 : : }
2377 : : }
2378 : 46 : inchi_strbuf_close(&tmpbuf);
2379 : : }
2380 : :
2381 : : // String deduplication based on dictionary and counts
2382 : 36 : int count = 0;
2383 [ + + ]: 3636 : for (int i = 0; i < ENH_STEREO_DICT_SIZE; i++) {
2384 [ + + ]: 3600 : if (dictionary[i]) {
2385 [ + + ]: 41 : if (count > 0) {
2386 : 5 : tot_len += MakeDelim( ";", strbuf, bOverflow );
2387 : : }
2388 [ + + ]: 41 : if (counts[i] > 1) {
2389 : 5 : tot_len = inchi_strbuf_printf(strbuf, "%d*%s", counts[i], dictionary[i]);
2390 : : } else {
2391 : 36 : tot_len = inchi_strbuf_printf(strbuf, "%s", dictionary[i]);
2392 : : }
2393 [ + - ]: 41 : inchi_free(dictionary[i]);
2394 : 41 : count++;
2395 : : }
2396 : : }
2397 : :
2398 [ + - ]: 36 : inchi_free(dictionary);
2399 [ + - ]: 36 : inchi_free(counts);
2400 : :
2401 : 36 : return tot_len;
2402 : : }
2403 : :
2404 : : #ifdef ALPHA_BASE
2405 : : #if ( ALPHA_BASE != 27 )
2406 : : #error ALPHA_BASE definitions mismatch
2407 : : #endif
2408 : : #else
2409 : : #define ALPHA_BASE 27
2410 : : #endif
2411 : :
2412 : : #define ALPHA_MINUS '-'
2413 : : #define ALPHA_ZERO_VAL '.'
2414 : : #define ALPHA_ONE 'a'
2415 : : #define ALPHA_ZERO '@'
2416 : :
2417 : :
2418 : : /****************************************************************************
2419 : : MakeAbcNumber( ... )
2420 : :
2421 : : Produce an "Alphabetic" number, base 27 (27 digits: 0, a, b, ..., z)
2422 : : The leading "digit" uppercase, the rest -- lowercase
2423 : : szString length nStringLen includes 1 byte for zero termination
2424 : : Return Value: length without zero termination; -1 means not enough room
2425 : : Note: ASCII-encoding specific implementation
2426 : : ****************************************************************************/
2427 : 0 : int MakeAbcNumber( char *szString,
2428 : : int nStringLen,
2429 : : const char *szLeadingDelim,
2430 : : int nValue )
2431 : : {
2432 : 0 : char *p = szString;
2433 : : char *q;
2434 : : int nChar;
2435 : :
2436 [ # # ]: 0 : if (nStringLen < 2)
2437 : 0 : return -1;
2438 [ # # # # : 0 : while (szLeadingDelim && *szLeadingDelim && --nStringLen)
# # ]
2439 : : {
2440 : 0 : *p++ = *szLeadingDelim++;
2441 : : }
2442 [ # # ]: 0 : if (nStringLen < 2)
2443 : 0 : return -1;
2444 [ # # ]: 0 : if (!nValue)
2445 : : {
2446 : 0 : *p++ = ALPHA_ZERO_VAL; /* zero value (cannot use 0) */
2447 : 0 : *p = '\0';
2448 : 0 : return 1;
2449 : : }
2450 [ # # ]: 0 : if (nValue < 0)
2451 : : {
2452 : 0 : *p++ = ALPHA_MINUS;
2453 : 0 : nStringLen--;
2454 : 0 : nValue = -nValue;
2455 : : }
2456 [ # # # # ]: 0 : for (q = p; nValue && --nStringLen; nValue /= ALPHA_BASE)
2457 : : {
2458 [ # # ]: 0 : if ((nChar = nValue % ALPHA_BASE)) /* djb-rwth: addressing LLVM warning */
2459 : : {
2460 : 0 : nChar = ALPHA_ONE + nChar - 1;
2461 : : }
2462 : : else
2463 : : {
2464 : 0 : nChar = ALPHA_ZERO;
2465 : : }
2466 : 0 : *q++ = nChar;
2467 : : }
2468 [ # # ]: 0 : if (nStringLen <= 0)
2469 : 0 : return -1;
2470 : 0 : *q = '\0';
2471 : 0 : mystrrev( p );
2472 : 0 : p[0] = toupper( p[0] );
2473 : :
2474 : 0 : return (int) ( q - szString );
2475 : : }
2476 : :
2477 : :
2478 : : #if 1 /* ( READ_INCHI_STRING == 1 ) */
2479 : :
2480 : :
2481 : : static long abctol( const char *szString, char **q ); /* keep compiler happy */
2482 : :
2483 : :
2484 : : /****************************************************************************
2485 : : abctol( ... ) - analog of strtol() dealing with 'compressed InChI' input strings
2486 : : ****************************************************************************/
2487 : 0 : long abctol( const char *szString, char **q )
2488 : : {
2489 : : #define __MYTOLOWER(c) ( ((c) >= 'A') && ((c) <= 'Z') ? ((c) - 'A' + 'a') : (c) )
2490 : :
2491 : 0 : long val = 0;
2492 : : /* djb-rwth: removing redundant variables */
2493 : 0 : const char *p = szString, *pp = szString;
2494 : 0 : long treshold = ( LONG_MAX - 1 ) / ALPHA_BASE;
2495 : :
2496 [ # # ]: 0 : if (*p == ALPHA_MINUS)
2497 : : {
2498 : 0 : p++;
2499 : : /* djb-rwth: removing redundant code */
2500 : : }
2501 [ # # ]: 0 : if (*p == ALPHA_ZERO)
2502 : : {
2503 : 0 : p++;
2504 : 0 : goto exit_function;
2505 : : }
2506 [ # # ]: 0 : if (!isupper( UCINT *p ))
2507 : : {
2508 : 0 : p = szString;
2509 : 0 : goto exit_function; /* not an abc-number */
2510 : : }
2511 [ # # # # ]: 0 : val = __MYTOLOWER( *p ) - ALPHA_ONE + 1;
2512 : 0 : p++;
2513 [ # # ]: 0 : while (*p)
2514 : : {
2515 [ # # ]: 0 : if (islower( UCINT *p ))
2516 : : {
2517 : 0 : val *= ALPHA_BASE;
2518 : 0 : val += *p - ALPHA_ONE + 1;
2519 : : }
2520 : : else
2521 [ # # ]: 0 : if (*p == ALPHA_ZERO)
2522 : : {
2523 : : /*^^^ 1.06 */
2524 [ # # ]: 0 : if (val > treshold)
2525 : : {
2526 : 0 : p = pp;
2527 : 0 : val = 0;
2528 : 0 : goto exit_function;
2529 : : }
2530 : : else
2531 : : /* Software version 1.07 ^^^*/
2532 : 0 : val *= ALPHA_BASE; /* @1.06 fuzz testing caused overflow here! */
2533 : : }
2534 : : else
2535 : : {
2536 : 0 : break;
2537 : : }
2538 : 0 : p++;
2539 : : }
2540 : :
2541 : 0 : exit_function:
2542 [ # # ]: 0 : if (q)
2543 : : {
2544 : 0 : *q = (char *) p; /* cast deliberately discards const qualifier */
2545 : : }
2546 : :
2547 : 0 : return val;
2548 : : #undef __MYTOLOWER
2549 : : }
2550 : :
2551 : :
2552 : : /********************************************************/
2553 : 396 : long inchi_strtol(const char *str, const char **p, int base)
2554 : : {
2555 [ - + ]: 396 : if (base == ALPHA_BASE) {
2556 : 0 : return abctol(str, (char **)p); /* cast deliberately discards const qualifier */
2557 : : }
2558 : : else {
2559 : 396 : return strtol(str, (char **)p, base); /* cast deliberately discards const qualifier */
2560 : : }
2561 : : }
2562 : : #endif /* #if ( READ_INCHI_STRING == 1 ) */
2563 : :
2564 : : #undef ALPHA_BASE
2565 : : #undef ALPHA_MINUS
2566 : : #undef ALPHA_ZERO_VAL
2567 : : #undef ALPHA_ONE
2568 : : #undef ALPHA_ZERO
2569 : :
2570 : :
2571 : : /****************************************************************************/
2572 : : /* GH #100: length of the leading C-locale float in str, e.g. "-1.5e3"; 0 if none */
2573 : 224 : static size_t FloatTokenLen(const char *str)
2574 : : {
2575 : 224 : const char *p = str;
2576 : : const char *q;
2577 : 224 : int digits = 0;
2578 : :
2579 [ + + ]: 361 : while (isspace((unsigned char) *p))
2580 : : {
2581 : 137 : p++;
2582 : : }
2583 [ + - + + ]: 224 : if (*p == '+' || *p == '-')
2584 : : {
2585 : 81 : p++;
2586 : : }
2587 [ + + ]: 445 : for (; isdigit((unsigned char) *p); p++)
2588 : : {
2589 : 221 : digits++;
2590 : : }
2591 [ + + ]: 224 : if (*p == '.')
2592 : : {
2593 [ + + ]: 996 : for (p++; isdigit((unsigned char) *p); p++)
2594 : : {
2595 : 793 : digits++;
2596 : : }
2597 : : }
2598 [ - + ]: 224 : if (!digits)
2599 : : {
2600 : 0 : return 0;
2601 : : }
2602 : :
2603 : : /* exponent counts only when at least one digit follows: "1e" -> "1" */
2604 [ + - - + ]: 224 : if (*p == 'e' || *p == 'E')
2605 : : {
2606 : 0 : q = p + 1;
2607 [ # # # # ]: 0 : if (*q == '+' || *q == '-')
2608 : : {
2609 : 0 : q++;
2610 : : }
2611 [ # # ]: 0 : if (isdigit((unsigned char) *q))
2612 : : {
2613 [ # # ]: 0 : for (p = q; isdigit((unsigned char) *p); p++)
2614 : : {
2615 : : }
2616 : : }
2617 : : }
2618 : 224 : return p - str;
2619 : : }
2620 : :
2621 : :
2622 : : /****************************************************************************/
2623 : 83825 : double inchi_strtod(const char *str, const char **p)
2624 : : {
2625 : : /* GH #100: always read '.' as decimal point, whatever LC_NUMERIC says */
2626 : 83825 : const char *dp = localeconv()->decimal_point;
2627 : 83825 : size_t dplen = strlen(dp);
2628 : : char stack_buf[64];
2629 : 83825 : char *buf = stack_buf;
2630 : : size_t len, i, j;
2631 : : double val;
2632 : :
2633 [ + + ]: 83825 : if (!strcmp(dp, "."))
2634 : : {
2635 : 83601 : return strtod( str, (char **) p );
2636 : : }
2637 : :
2638 : : /* the token is scanned here, so its end never depends on the locale */
2639 : 224 : len = FloatTokenLen(str);
2640 [ + - ]: 224 : if (p)
2641 : : {
2642 : 224 : *p = str + len;
2643 : : }
2644 [ - + ]: 224 : if (!len)
2645 : : {
2646 : 0 : return 0.0;
2647 : : }
2648 : :
2649 : : /* copy the token with '.' spelled as the locale decimal point (may be multi-byte) */
2650 [ - + ]: 224 : if (len + dplen > sizeof(stack_buf))
2651 : : {
2652 : 0 : buf = (char *) inchi_malloc(len + dplen);
2653 [ # # ]: 0 : if (!buf)
2654 : : {
2655 [ # # ]: 0 : if (p)
2656 : : {
2657 : 0 : *p = str;
2658 : : }
2659 : 0 : return 0.0;
2660 : : }
2661 : : }
2662 [ + + ]: 1659 : for (i = j = 0; i < len; i++)
2663 : : {
2664 [ + + ]: 1435 : if (str[i] != '.')
2665 : : {
2666 : 1232 : buf[j++] = str[i];
2667 : 1232 : continue;
2668 : : }
2669 : 203 : memcpy(buf + j, dp, dplen);
2670 : 203 : j += dplen;
2671 : : }
2672 : 224 : buf[j] = '\0';
2673 : :
2674 : 224 : val = strtod( buf, NULL );
2675 [ - + ]: 224 : if (buf != stack_buf)
2676 : : {
2677 [ # # ]: 0 : inchi_free(buf);
2678 : : }
2679 : 224 : return val;
2680 : : }
2681 : :
2682 : :
2683 : : /****************************************************************************/
2684 : 8869 : void inchi_dot_decimal(char *str)
2685 : : {
2686 : : /* GH #100: turn printf's locale decimal point into '.' */
2687 : 8869 : const char *dp = localeconv()->decimal_point;
2688 : 8869 : size_t dplen = strlen(dp);
2689 : : char *q;
2690 : :
2691 [ + + ]: 8869 : if (!strcmp(dp, "."))
2692 : : {
2693 : 8834 : return;
2694 : : }
2695 : :
2696 : : /* a multi-byte point (e.g. U+066B) counts as one character in printf's
2697 : : field width, so dropping its extra bytes keeps the intended width */
2698 [ + + ]: 74 : for (q = strstr(str, dp); q; q = strstr(q + 1, dp))
2699 : : {
2700 : 39 : *q = '.';
2701 : 39 : memmove(q + 1, q + dplen, strlen(q + dplen) + 1);
2702 : : }
2703 : : }
2704 : :
2705 : :
2706 : : /****************************************************************************
2707 : : MakeDecNumber( ... )
2708 : :
2709 : : Produce a decimal number
2710 : :
2711 : : szString length nStringLen includes 1 byte for zero termination
2712 : : Return Value: length without zero termination; -1 means not enough room
2713 : : ****************************************************************************/
2714 : 21890 : int MakeDecNumber( char *szString,
2715 : : int nStringLen,
2716 : : const char *szLeadingDelim,
2717 : : int nValue )
2718 : : {
2719 : : #define DECIMAL_BASE 10
2720 : : #define DECIMAL_MINUS '-'
2721 : : #define DECIMAL_ZERO_VAL '0'
2722 : : #define DECIMAL_ONE '1'
2723 : : #define DECIMAL_ZERO '0'
2724 : :
2725 : 21890 : char *p = szString;
2726 : : char *q;
2727 : : int nChar;
2728 : :
2729 [ - + ]: 21890 : if (nStringLen < 2)
2730 : : {
2731 : 0 : return -1;
2732 : : }
2733 [ + + + + : 33934 : while (szLeadingDelim && *szLeadingDelim && --nStringLen)
+ - ]
2734 : : {
2735 : 12044 : *p++ = *szLeadingDelim++; /* djb-rwth: GCC 14 false positive */
2736 : : }
2737 [ - + ]: 21890 : if (nStringLen < 2)
2738 : : {
2739 : 0 : return -1;
2740 : : }
2741 [ - + ]: 21890 : if (!nValue)
2742 : : {
2743 : 0 : *p++ = DECIMAL_ZERO_VAL; /* zero value (cannot use 0) */
2744 : 0 : *p = '\0';
2745 : 0 : return (int) ( p - szString );
2746 : : }
2747 [ + + ]: 21890 : if (nValue < 0)
2748 : : {
2749 : 22 : *p++ = DECIMAL_MINUS;
2750 : 22 : nStringLen--;
2751 : 22 : nValue = -nValue;
2752 : : }
2753 [ + + + - ]: 78548 : for (q = p; nValue && --nStringLen; nValue /= DECIMAL_BASE)
2754 : : {
2755 [ + + ]: 56658 : if ((nChar = nValue % DECIMAL_BASE)) /* djb-rwth: addressing LLVM warning */
2756 : : {
2757 : 53043 : nChar = DECIMAL_ONE + nChar - 1;
2758 : : }
2759 : : else
2760 : : {
2761 : 3615 : nChar = DECIMAL_ZERO;
2762 : : }
2763 : 56658 : *q++ = nChar;
2764 : : }
2765 [ - + ]: 21890 : if (nStringLen <= 0)
2766 : : {
2767 : 0 : return -1;
2768 : : }
2769 : 21890 : *q = '\0';
2770 : 21890 : mystrrev( p );
2771 : :
2772 : 21890 : return (int) ( q - szString );
2773 : :
2774 : : #undef DECIMAL_BASE
2775 : : #undef DECIMAL_MINUS
2776 : : #undef DECIMAL_ZERO_VAL
2777 : : #undef DECIMAL_ONE
2778 : : #undef DECIMAL_ZERO
2779 : : }
2780 : :
2781 : :
2782 : : /****************************************************************************
2783 : : Print out num[0..n-1] separated with commas
2784 : : For shortness, compress consecutive numbers (>2 members in a series)
2785 : : to a range:
2786 : : '1,2,5, 7-10' means 1,2,5,7,8,9,10'
2787 : : ****************************************************************************/
2788 : 48 : void print_sequence_of_nums_compressing_ranges( int n,
2789 : : int *num,
2790 : : INCHI_IOS_STRING *strbuf )
2791 : : {
2792 : : int k, range;
2793 : :
2794 : 48 : range = 0;
2795 [ + + ]: 212 : for (k = 0; k < n - 1; k++)
2796 : : {
2797 [ + + ]: 164 : if (num[k + 1] == num[k] + 1)
2798 : : {
2799 [ + + ]: 106 : if (range)
2800 : : {
2801 : 78 : range++;
2802 : : }
2803 : : else
2804 : : {
2805 : 28 : inchi_strbuf_printf( strbuf, "%d-", num[k] );
2806 : 28 : range = 1;
2807 : : }
2808 : : }
2809 : : else
2810 : : {
2811 [ + + ]: 58 : if (range)
2812 : : {
2813 : 4 : range = 0;
2814 : : }
2815 : 58 : inchi_strbuf_printf( strbuf, "%d,", num[k] );
2816 : : }
2817 : : }
2818 : 48 : inchi_strbuf_printf( strbuf, "%d", num[n - 1] );
2819 : :
2820 : 48 : return;
2821 : : }
|