Coverage Report

Created: 2026-10-31 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/inchi/INCHI-1-SRC/INCHI_BASE/src/ichiprt1.c
Line
Count
Source
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 <math.h>
10
11
#include "mode.h"
12
13
#include "ichister.h"
14
#include "ichimain.h"
15
#include "ichimake.h"
16
#include "ichi_io.h"
17
#include "strutil.h"
18
#include "bcf_s.h"
19
20
#include "logging.h" /*(@nnuk : Nauman Ullah Khan) :: Needed for logging functionality*/
21
22
/*
23
    Local functions
24
*/
25
26
/* djb-rwth: removing redundant code */
27
static int str_LineEnd( const char *tag,
28
                        int *bOverflow,
29
                        INCHI_IOS_STRING *buf,
30
                        int ind,
31
                        int bPlainTextTags );
32
static int CleanOrigCoord( MOL_COORD szCoord, int delim );
33
static int WriteOrigCoord( int num_inp_atoms,
34
                           MOL_COORD *szMolCoord,
35
                           int *i,
36
                           char *szBuf,
37
                           int buf_len );
38
static int WriteOrigAtoms( CANON_GLOBALS *pCG,
39
                           int num_inp_atoms,
40
                           inp_ATOM *at,
41
                           int *i,
42
                           char *szBuf,
43
                           int buf_len,
44
                           STRUCT_DATA *sd );
45
static int WriteOrigBonds( CANON_GLOBALS *pCG,
46
                           int num_inp_atoms,
47
                           inp_ATOM *at,
48
                           int *i,
49
                           char *szBuf,
50
                           int buf_len,
51
                           STRUCT_DATA *sd );
52
static void GetSaveOptLetters( unsigned char save_opt_bits,
53
                               char* let1,
54
                               char* let2 );
55
static int OutputINCHI_VersionAndKind( INCHI_IOSTREAM *out_file,
56
                                       INCHI_IOS_STRING *strbuf,
57
                                       int bINChIOutputOptions,
58
                                       int is_beta,
59
                                       int is_molecular_inorganic,
60
                                       char *pLF,
61
                                       char *pTAB );
62
static int OutputINCHI_MainLayerFormula( CANON_GLOBALS *pCG,
63
                                         INCHI_IOSTREAM *out_file,
64
                                         INCHI_IOS_STRING *strbuf,
65
                                         int num_components2[],
66
                                         int *INCHI_basic_or_INCHI_reconnected,
67
                                         INCHI_OUT_CTL *io,
68
                                         char *pLF,
69
                                         char *pTAB );
70
static int OutputINCHI_MainLayerConnections( CANON_GLOBALS *pCG,
71
                                             INCHI_IOSTREAM *out_file,
72
                                             INCHI_IOS_STRING *strbuf,
73
                                             int num_components2[],
74
                                             int *INCHI_basic_or_INCHI_reconnected,
75
                                             INCHI_OUT_CTL *io,
76
                                             char *pLF,
77
                                             char *pTAB );
78
static int OutputINCHI_MainLayerHydrogens( CANON_GLOBALS *pCG,
79
                                           INCHI_IOSTREAM *out_file,
80
                                           INCHI_IOS_STRING *strbuf,
81
                                           int num_components2[],
82
                                           int *INCHI_basic_or_INCHI_reconnected,
83
                                           INCHI_OUT_CTL *io,
84
                                           char *pLF,
85
                                           char *pTAB );
86
static int OutputINCHI_ChargeAndRemovedAddedProtonsLayers( CANON_GLOBALS *pCG,
87
                                                           INCHI_IOSTREAM *out_file,
88
                                                           INCHI_IOS_STRING *strbuf,
89
                                                           INCHI_OUT_CTL *io,
90
                                                           char *pLF,
91
                                                           char *pTAB );
92
static int OutputINCHI_StereoLayer( CANON_GLOBALS *pCG,
93
                                    INCHI_IOSTREAM *out_file,
94
                                    INCHI_IOS_STRING *strbuf,
95
                                    INCHI_OUT_CTL *io,
96
                                    char *pLF,
97
                                    char *pTAB );
98
static int OutputINCHI_StereoLayer_EnhancedStereo( CANON_GLOBALS *pCG,
99
                                                   INCHI_IOSTREAM *out_file,
100
                                                   INCHI_IOS_STRING *strbuf,
101
                                                   INCHI_OUT_CTL *io,
102
                                                   ORIG_ATOM_DATA *orig_inp_data,
103
                                                   char *pLF,
104
                                                   char *pTAB );
105
static int OutputINCHI_IsotopicLayer( CANON_GLOBALS *pCG,
106
                                      INCHI_IOSTREAM *out_file,
107
                                      INCHI_IOS_STRING *strbuf,
108
                                      int *INCHI_basic_or_INCHI_reconnected,
109
                                      INCHI_OUT_CTL *io,
110
                                      char *pLF,
111
                                      char *pTAB );
112
static int OutputINCHI_FixedHLayerWithSublayers( CANON_GLOBALS *pCG,
113
                                                 INCHI_IOSTREAM *out_file,
114
                                                 INCHI_IOS_STRING *strbuf,
115
                                                 int *INCHI_basic_or_INCHI_reconnected,
116
                                                 INCHI_OUT_CTL *io,
117
                                                 char *pLF,
118
                                                 char *pTAB,
119
                                                 int *then_goto_repeat );
120
static int OutputINCHI_PolymerLayer( CANON_GLOBALS *pCG, INCHI_IOSTREAM *out_file, INCHI_IOS_STRING *strbuf,
121
                                     int *INCHI_basic_or_INCHI_reconnected,
122
                                     ORIG_ATOM_DATA *orig_inp_data, ORIG_STRUCT *pOrigStruct,
123
                                     INCHI_OUT_CTL *io, char *pLF, char *pTAB );
124
static int OutputINCHI_PolymerLayer_SingleUnit( OAD_PolymerUnit *u,
125
                                                int bPolymers,
126
                                                int total_star_atoms,
127
                                                int *n_used_stars,
128
                                                OAD_AtProps *aprops,
129
                                                int *cano_nums,
130
                                                ORIG_ATOM_DATA *orig_inp_data,
131
                                                ORIG_STRUCT *pOrigStruct,
132
                                                INCHI_IOS_STRING *strbuf );
133
static int OutputAUXINFO_HeaderAndNormalization_type( CANON_GLOBALS *pCG,
134
                                                      INCHI_IOSTREAM *out_file,
135
                                                      INCHI_IOS_STRING *strbuf,
136
                                                      int bINChIOutputOptions,
137
                                                      int *INCHI_basic_or_INCHI_reconnected,
138
                                                      int num_components2[],
139
                                                      INCHI_OUT_CTL *io,
140
                                                      char *pLF,
141
                                                      char *pTAB );
142
static int OutputAUXINFO_OriginalNumbersAndEquivalenceClasses( CANON_GLOBALS *pCG,
143
                                                               INCHI_IOSTREAM *out_file,
144
                                                               INCHI_IOS_STRING *strbuf,
145
                                                               int num_components2[],
146
                                                               INCHI_OUT_CTL *io,
147
                                                               char *pLF,
148
                                                               char *pTAB );
149
static int OutputAUXINFO_TautomericGroupsEquivalence( CANON_GLOBALS         *pCG,
150
                                                      INCHI_IOSTREAM        *out_file,
151
                                                      INCHI_IOS_STRING *strbuf,
152
                                                      INCHI_OUT_CTL   *io );
153
static int OutputAUXINFO_Stereo( CANON_GLOBALS *pCG,
154
                                  INCHI_IOSTREAM *out_file,
155
                                  INCHI_IOS_STRING *strbuf,
156
                                  INCHI_OUT_CTL *io,
157
                                  char *pLF,
158
                                  char *pTAB );
159
static int OutputAUXINFO_IsotopicInfo( CANON_GLOBALS *pCG,
160
                                       INCHI_IOSTREAM *out_file,
161
                                       INCHI_IOS_STRING *strbuf,
162
                                       int *INCHI_basic_or_INCHI_reconnected,
163
                                       INCHI_OUT_CTL *io,
164
                                       char *pLF, char *pTAB );
165
/* djb-rwth: removing redundant code */
166
static int OutputAUXINFO_ChargesRadicalsAndUnusualValences( CANON_GLOBALS *pCG,
167
                                                            INCHI_IOSTREAM *out_file,
168
                                                            INCHI_IOS_STRING *strbuf,
169
                                                            INCHI_OUT_CTL *io,
170
                                                            char *pLF,
171
                                                            char *pTAB );
172
static int OutputAUXINFO_ReversibilityInfo( CANON_GLOBALS *pCG,
173
                                            INCHI_IOSTREAM *out_file,
174
                                            INCHI_IOS_STRING *strbuf,
175
                                            ORIG_STRUCT *pOrigStruct,
176
                                            INCHI_OUT_CTL *io,
177
                                            char *pLF,
178
                                            char *pTAB );
179
180
static int OutputAUXINFO_PolymerInfo(CANON_GLOBALS *pCG,
181
                                     INCHI_IOSTREAM *out_file,
182
                                     INCHI_IOS_STRING *strbuf,
183
                                     ORIG_STRUCT *pOrigStruct,
184
                                     INCHI_OUT_CTL *io,
185
                                     char *pLF,
186
                                     char *pTAB);
187
188
static int InternallyGetCanoNumsAndComponentNums(CANON_GLOBALS *pCG,
189
                                                 INCHI_IOS_STRING *strbuf,
190
                                                 INCHI_OUT_CTL *io,
191
                                                 int nat,
192
                                                 int *cano_nums,
193
                                                 int *compnt_nums);
194
195
static int CountPseudoElementInFormula(const char *pseudo, char *s);
196
static int IsBondAtomNumsLesser(int *bond1, int *bond2);
197
198
static void inchi_sort_int_pair_ascending(int *a, int *b);
199
200
/* djb-rwth: removing redundant code */
201
202
static void MergeZzInStrHillFormulaComponent(char *s);
203
204
/*
205
    Local constants
206
*/
207
const char sCompDelim[] = ";";       /* component delimiter */
208
const char sIdenticalValues[] = "*"; /* identical component */
209
const char x_space[] = "                  ";
210
211
/*
212
    Output: words & additional tags
213
*/
214
const char x_inchi[] = INCHI_NAME;
215
const char x_inchi_ver[] = "version"; /* "InChI.version"; */
216
const char x_curr_ver[] = INCHI_VERSION;
217
const char x_structure[] = "structure";
218
const char x_number[] = "number";
219
const char x_header[] = "id.name";
220
const char x_value[] = "id.value";
221
const char x_empty[] = "";
222
const char x_type[] = "type";
223
const char x_message[] = "message";
224
const char x_text[] = "value";
225
const char x_ferr[] = "fatal (aborted)";
226
const char x_err[] = "error (no InChI)";
227
const char x_warn[] = "warning";
228
const char x_basic[] = "identifier";
229
const char x_tautomeric[] = "mobile-H";
230
const char x_reconnected[] = "reconnected";
231
const char x_ver[] = "version";
232
const char x_type_alpha[] = "alpha";
233
const char x_type_numer[] = "numeric";
234
const char x_type_predec[] = "sct";
235
const char x_type_normal[] = "normal";
236
const char x_type_short[] = "compressed";
237
const char x_basic_layer[] = "basic";
238
const char x_aux_basic[] = "identifier.auxiliary-info";
239
const char x_aux_comm[] = "!-- This section is NOT a part of the identifier, it is not unique --";
240
const char x_ign_uu_sp2[] = "omit_undef_dbond";
241
const char x_ign_uu_sp3[] = "omit_undef_sp3";
242
const char x_line_opening[] = "<";
243
const char x_line_closing[] = "</";
244
const char x_close_line[] = ">";
245
const char x_abs[] = "1";
246
const char x_rel[] = "2";
247
const char x_rac[] = "3";
248
249
typedef struct tagInchiTag
250
{
251
    const char *szPlainLabel;
252
    const char *szPlainComment;
253
    const char *szXmlLabel;
254
    int bAlwaysOutput;
255
} INCHI_TAG;
256
257
/*
258
    Identifier
259
*/
260
const INCHI_TAG IdentLbl[] =
261
    {
262
        /* prefixes: may be combined in this order */
263
        /* IL_FIXH_ORD, */ {"/", "fixed_H", "fixed-H", 0},   /* fixed H */
264
        /* IL_ISOT_ORD, */ {"/", "isotopic", "isotopic", 0}, /* isotopic */
265
        /* IL_STER_ORD, */ {"/", "stereo", "stereo", 0},     /* stereo */
266
                                                             /* items */
267
        /* IL_VERS_ORD, */ {"", "version", "version", 1},
268
        /* IL_FML__ORD, */ {"/", "formula", "formula", 1}, /* basic part formula */
269
        /* IL_CONN_ORD, */ {"/c", "connections", "connections", 1},
270
        /* IL_ALLH_ORD, */ {"/h", "H_atoms", "H", 1},
271
        /* IL_CHRG_ORD, */ {"/q", "charge", "charge", 1},
272
        /* IL_PROT_ORD, */ {"/p", "protons", "protons", 0},
273
        /* stereo */
274
        /* IL_DBND_ORD, */ {"/b", "dbond", "dbond", 0},
275
        /* IL_SP3S_ORD, */ {"/t", "sp3", "sp3", 0},
276
        /* IL_INVS_ORD, */ {"/m", "sp3:inverted", "abs.inverted", 0},       /* mirrored */
277
        /* IL_TYPS_ORD, */ {"/s", "type (1=abs, 2=rel, 3=rac)", "type", 0}, /* stereo type */
278
                                                                            /* isotopic */
279
        /* IL_ATMS_ORD, */ {"/i", "atoms", "atoms", 1},
280
        /* isotopic mobile H only */
281
        /* IL_XCGA_ORD, */ {"/h", "exchangeable_H", "H-isotopic", 1},
282
        /* fixed H only */
283
        /* IL_FMLF_ORD, */ {"/f", "formula", "formula", 1},             /* fixed H formula */
284
        /* IL_HFIX_ORD, */ {"/h", "H_fixed", "H-fixed", 1},             /* fixed-H */
285
        /* IL_TRNS_ORD, */ {"/o", "transposition", "transposition", 0}, /* order */
286
        /* IL_REC__ORD, */ {"/r", "reconnected bond(s) to metal(s) formula", "formula", 0}};
287
288
/*
289
290
  Parsing plain text InChI (FML is a chemical formula)
291
  ========================
292
293
  1.12Beta/FML       /i      /f[FML]  /i   [/o] /rFML      /i      /f[FML]  /i   [/o] end
294
          |          |       |        |         |          |       |        |         |
295
Labels    | chqpbtms | hbtms | hqbtms | btms    | chqpbtms | hbtms | hqbtms | btms    |
296
inside:   |          |       |        |         |          |       |        |         |
297
          | non-iso- | iso-  | fix-   | iso-    | non-iso- | iso-  | fix-   | iso-    |
298
meaning:  | topic    | topic | ed H   | topic   | topic    | topic | ed H   | topic   |
299
          |----------+-------+--------+---------|----------+-------+--------+---------|
300
          |        mobile-H  |   fixed-H        |        mobile-H  |   fixed-H        |
301
          |----------+-------+--------+---------|----------+-------+--------+---------|
302
          |                                     |                                     |
303
          |     normal  or disconected metal    |      reconnected bonds to metal     |
304
          |_____________________________________|_____________________________________|
305
306
  meanings of h:
307
308
       /h   -  immobile H & mobile H group(s)
309
     /i/h   -  exchangeable isotopic H (common)
310
     /f/h   -  fixed-H
311
     /f/i/h -  never happens
312
313
*/
314
typedef enum tagIdentLblOrd
315
{
316
    IL_FIXH_ORD,
317
    IL_ISOT_ORD,
318
    IL_STER_ORD,
319
320
    IL_VERS_ORD,
321
    IL_FML__ORD,
322
    IL_CONN_ORD,
323
    IL_ALLH_ORD,
324
    IL_CHRG_ORD,
325
    IL_PROT_ORD,
326
327
    IL_DBND_ORD,
328
    IL_SP3S_ORD,
329
    IL_INVS_ORD,
330
    IL_TYPS_ORD,
331
332
    IL_ATMS_ORD,
333
334
    IL_XCGA_ORD,
335
336
    IL_FMLF_ORD,
337
    IL_HFIX_ORD,
338
    IL_TRNS_ORD,
339
    IL_REC__ORD,
340
341
    IL_MAX_ORD /* max number of tags */
342
} IDENT_LBL_ORD;
343
344
typedef enum tagIdentLblBit
345
{
346
    IL_FIXH = 1 << IL_FIXH_ORD,
347
    IL_ISOT = 1 << IL_ISOT_ORD,
348
    IL_STER = 1 << IL_STER_ORD,
349
350
    IL_VERS = 1 << IL_VERS_ORD,
351
    IL_FML_ = 1 << IL_FML__ORD,
352
    IL_CONN = 1 << IL_CONN_ORD,
353
    IL_ALLH = 1 << IL_ALLH_ORD,
354
    IL_CHRG = 1 << IL_CHRG_ORD,
355
    IL_PROT = 1 << IL_PROT_ORD,
356
357
    IL_DBND = 1 << IL_DBND_ORD,
358
    IL_SP3S = 1 << IL_SP3S_ORD,
359
    IL_INVS = 1 << IL_INVS_ORD,
360
    IL_TYPS = 1 << IL_TYPS_ORD,
361
362
    IL_ATMS = 1 << IL_ATMS_ORD,
363
364
    IL_XCGA = 1 << IL_XCGA_ORD,
365
366
    IL_FMLF = 1 << IL_FMLF_ORD,
367
    IL_HFIX = 1 << IL_HFIX_ORD,
368
    IL_TRNS = 1 << IL_TRNS_ORD,
369
    IL_REC_ = 1 << IL_REC__ORD
370
} IDENT_LBL_BIT;
371
372
/*
373
    Aux Info constants
374
*/
375
const INCHI_TAG AuxLbl[] =
376
    {
377
        /* prefixes may be combined in this order */
378
        /* AL_FIXH_ORD, */ {"/", "fixed_H", "fixed-H", 0},                         /* fixed-H */
379
        /* AL_ISOT_ORD, */ {"/", "isotopic", "isotopic", 0},                       /* isotopic */
380
        /* AL_STER_ORD, */ {"/", "abs_stereo_inverted", "stereo.abs.inverted", 0}, /* inv abs sp3 stereo */
381
        /* AL_REVR_ORD, */ {"/", "reversibility", "reversibility", 0},             /* reversibility */
382
                                                                                   /* items */
383
        /* AL_VERS_ORD, */ {"", "version", "version", 1},
384
        /* AL_NORM_ORD, */ {"/", "normalization_type", "norm-type", 1},
385
        /* AL_ANBR_ORD, */ {"/N:", "original_atom_numbers", "atom.orig-nbr", 1},
386
        /* AL_AEQU_ORD, */ {"/E:", "atom_equivalence", "atom.equivalence", 0},
387
        /* AL_GEQU_ORD, */ {"/gE:", "group_equivalence", "group.equivalence", 0},
388
        /* inv abs sp3 stereo */
389
        /* AL_SP3I_ORD, */ {"/it:", "sp3", "sp3", 0},
390
        /* AL_SP3N_ORD, */ {"/iN:", "original_atom_numbers", "atom.orig-nbr", 0},
391
392
        /* AL_CRV__ORD, */ {"/CRV:", "charge_radical_valence", "charges-rad-val", 0},
393
        /* reversibility */
394
        /* AL_ATMR_ORD, */ {"/rA:", "atoms", "atoms", 0},
395
        /* AL_BNDR_ORD, */ {"/rB:", "bonds", "bonds", 0},
396
        /* AL_XYZR_ORD, */ {"/rC:", "xyz", "xyz", 0},
397
        /* fixed-H only */
398
        /* AL_FIXN_ORD, */ {"/F:", "original_atom_numbers", "atom.orig-nbr", 1},
399
        /* isotopic only */
400
        /* AL_ISON_ORD, */ {"/I:", "original_atom_numbers", "atom.orig-nbr", 1},
401
402
        /* AL_REC__ORD, */ {"/R:", "reconnected bond(s) to metal(s) part", "", 1}};
403
404
typedef enum tagAuxLblOrd
405
{
406
    AL_FIXH_ORD,
407
    AL_ISOT_ORD,
408
    AL_STER_ORD,
409
    AL_REVR_ORD,
410
411
    AL_VERS_ORD,
412
    AL_NORM_ORD,
413
    AL_ANBR_ORD,
414
    AL_AEQU_ORD,
415
    AL_GEQU_ORD,
416
417
    AL_SP3I_ORD,
418
    AL_SP3N_ORD,
419
420
    AL_CRV__ORD,
421
422
    AL_ATMR_ORD,
423
    AL_BNDR_ORD,
424
    AL_XYZR_ORD,
425
426
    AL_FIXN_ORD,
427
428
    AL_ISON_ORD,
429
430
    AL_REC__ORD,
431
432
    AL_MAX_ORD /* max number of tags */
433
} AUX_LBL_ORD;
434
435
typedef enum tagAuxLblBit
436
{
437
    AL_FIXH = 1 << AL_FIXH_ORD,
438
    AL_ISOT = 1 << AL_ISOT_ORD,
439
    AL_STER = 1 << AL_STER_ORD,
440
    AL_REVR = 1 << AL_REVR_ORD,
441
442
    AL_VERS = 1 << AL_VERS_ORD,
443
    AL_NORM = 1 << AL_NORM_ORD,
444
    AL_ANBR = 1 << AL_ANBR_ORD,
445
    AL_AEQU = 1 << AL_AEQU_ORD,
446
    AL_GEQU = 1 << AL_GEQU_ORD,
447
448
    AL_SP3I = 1 << AL_SP3I_ORD,
449
    AL_SP3N = 1 << AL_SP3N_ORD,
450
451
    AL_CRV_ = 1 << AL_CRV__ORD,
452
453
    AL_ATMR = 1 << AL_ATMR_ORD,
454
    AL_BNDR = 1 << AL_BNDR_ORD,
455
    AL_XYZR = 1 << AL_XYZR_ORD,
456
457
    AL_FIXN = 1 << AL_FIXN_ORD,
458
459
    AL_ISON = 1 << AL_ISON_ORD,
460
461
    AL_REC_ = 1 << AL_REC__ORD
462
} AUX_LBL_BIT;
463
464
/* const int MAX_TAG_NUM = inchi_max((short)IL_MAX_ORD, (short)AL_MAX_ORD); */ /* djb-rwth: fixing MSVC warning C5287 */
465
466
char *szGetTag(const INCHI_TAG *Tag, int nTag, int bTag, char *szTag, int *bAlways, short tag_flag); /* djb-rwth: fixing GHI #160 */
467
468
#define SP(N) (x_space + sizeof(x_space) - 1 - (N))
469
470
0
#define NOT_YET_I2I_FOR_POLYMERS 40
471
472
/****************************************************************************
473
  Print error message (plain text)
474
****************************************************************************/
475
int OutputINChIPlainError(INCHI_IOSTREAM *out_file,
476
                          char *pErrorText,
477
                          int bError)
478
0
{
479
    /* char szBuf[64]; */
480
0
    const char *pErr;
481
0
    char *szErrorText = pErrorText;
482
0
    int ret = 0; /* djb-rwth: removing redundant variables */
483
484
0
    switch (bError)
485
0
    {
486
0
    case _IS_WARNING:
487
0
        pErr = x_warn;
488
0
        break;
489
0
    case _IS_ERROR:
490
0
        pErr = x_err;
491
0
        break;
492
0
    default: /*  _IS_FATAL */
493
0
        pErr = x_ferr;
494
0
        break;
495
0
    }
496
497
    /* djb-rwth: removing redundant code */
498
499
0
    inchi_ios_print(out_file,
500
0
                    "%s: %s=\"%s\" %s=\"%s\"",
501
0
                    x_message, x_type, pErr, x_text, szErrorText);
502
#ifdef TARGET_LIB_FOR_WINCHI
503
    inchi_ios_print(out_file, "\n");
504
#endif
505
0
    ret = 1;
506
507
0
    return ret;
508
0
}
509
510
#ifndef OUT_TN /* defined in mode.h; quoted here for reference purposes only */
511
512
#define OUT_N1 0 /* non-tautomeric only */
513
#define OUT_T1 1 /* tautomeric if present otherwise non-tautomeric */
514
#define OUT_NT 2 /* only non-taut representations of tautomeric */
515
#define OUT_TN 3 /* tautomeric if present otherwise non-tautomeric; \
516
                    sepatately output non-taut representations of tautomeric if present */
517
/* OUT_TN = OUT_T1 + OUT_NT */
518
#endif
519
520
/****************************************************************************
521
 Calculate equivalence mark (used to check for repeating (sub)layer(s) )
522
****************************************************************************/
523
const char *EquString(int EquVal)
524
25
{
525
25
    int bFrom = EquVal & (iiSTEREO | iiSTEREO_INV | iiNUMB | iiEQU);
526
25
    int bType = EquVal & (iitISO | iitNONTAUT);
527
25
    int bEq2 = EquVal & (iiEq2NONTAUT | iiEq2ISO | iiEq2INV);
528
25
    const char *r = "";
529
530
#if (FIX_EMPTY_LAYER_BUG == 1)
531
    int bEmpty = EquVal & iiEmpty;
532
    if (bEmpty)
533
    {
534
        r = "e";
535
        return r;
536
    }
537
#endif
538
539
25
    switch (bFrom)
540
25
    {
541
542
0
    case iiSTEREO: /* ------------ Stereo --------------------*/
543
0
        switch (bType)
544
0
        {
545
0
        case iitISO: /* iso main stereo =... */
546
0
            switch (bEq2)
547
0
            {
548
0
            case 0:
549
0
                r = "m"; /* iso main stereo = main stereo */
550
0
                break;
551
0
            default:
552
0
                r = "??"; /* should not happen */
553
0
                break;
554
0
            }
555
0
            break;
556
0
        case iitNONTAUT: /* non-taut stereo =... */
557
0
            switch (bEq2)
558
0
            {
559
0
            case 0:
560
0
                r = "m"; /* non-taut stereo = main stereo */
561
0
                break;
562
0
            default:
563
0
                r = "??"; /* should not happen */
564
0
                break;
565
0
            }
566
0
            break;
567
0
        case (iitNONTAUT | iitISO): /* iso non-taut stereo = ... */
568
0
            switch (bEq2)
569
0
            {
570
0
            case 0:
571
0
                r = "m"; /* iso non-taut stereo = main stereo */
572
0
                break;
573
0
            case iiEq2ISO:
574
0
                r = "M"; /* iso non-taut stereo = main iso stereo */
575
0
                break;
576
0
            case iiEq2NONTAUT:
577
0
                r = "n"; /* iso non-taut stereo = non-taut stereo */
578
0
                break;
579
0
            default:
580
0
                r = "??"; /* should not happen */
581
0
                break;
582
0
            }
583
0
            break;
584
0
        default:
585
0
            r = "??"; /* should not happen */
586
0
            break;
587
0
        }
588
0
        break;
589
590
0
    case iiSTEREO_INV: /*---------- Inverted Aux Stereo ------*/
591
0
        if (bEq2 & iiEq2INV)
592
0
        { /* stereo = Inverted(another stereo) */
593
0
            bEq2 &= ~iiEq2INV;
594
0
            switch (bType)
595
0
            {
596
0
            case 0: /* main = ...*/
597
0
                switch (bEq2)
598
0
                {
599
0
                case 0:
600
0
                    r = "im"; /* main         = Inv(main) */
601
0
                    break;
602
0
                case iiEq2ISO:
603
0
                    r = "iM"; /* main         = Inv(main iso) */
604
0
                    break;
605
0
                case iiEq2NONTAUT:
606
0
                    r = "in"; /* maim         = Inv(non-taut) */
607
0
                    break;
608
0
                case (iiEq2NONTAUT | iiEq2ISO):
609
0
                    r = "iN"; /* maim         = Inv(non-taut iso ) */
610
0
                    break;
611
0
                default:
612
0
                    r = "??"; /* should not happen */
613
0
                    break;
614
0
                }
615
0
                break;
616
0
            case iitISO: /* main iso = ...*/
617
0
                switch (bEq2)
618
0
                {
619
0
                case 0:
620
0
                    r = "im"; /* main iso     = Inv(main) */
621
0
                    break;
622
0
                case iiEq2ISO:
623
0
                    r = "iM"; /* main iso     = Inv(main iso) */
624
0
                    break;
625
0
                case iiEq2NONTAUT:
626
0
                    r = "in"; /* maim iso     = Inv(non-taut) */
627
0
                    break;
628
0
                case (iiEq2NONTAUT | iiEq2ISO):
629
0
                    r = "iN"; /* maim         = Inv(non-taut iso ) */
630
0
                    break;
631
0
                default:
632
0
                    r = "??"; /* should not happen */
633
0
                    break;
634
0
                }
635
0
                break;
636
0
            case iitNONTAUT: /* non-taut = ... */
637
0
                switch (bEq2)
638
0
                {
639
0
                case 0:
640
0
                    r = "im"; /* non-taut     = Inv(main) */
641
0
                    break;
642
0
                case iiEq2ISO:
643
0
                    r = "iM"; /* non-taut     = Inv(main iso) */
644
0
                    break;
645
0
                case iiEq2NONTAUT:
646
0
                    r = "in"; /* non-taut     = Inv(non-taut) */
647
0
                    break;
648
0
                case (iiEq2NONTAUT | iiEq2ISO):
649
0
                    r = "iN"; /* non-taut     = Inv(non-taut iso ) */
650
0
                    break;
651
0
                default:
652
0
                    r = "??"; /* should not happen */
653
0
                    break;
654
0
                }
655
0
                break;
656
0
            case (iitNONTAUT | iitISO):
657
0
                switch (bEq2)
658
0
                {
659
0
                case 0:
660
0
                    r = "im"; /* non-taut iso = Inv(main) */
661
0
                    break;
662
0
                case iiEq2ISO:
663
0
                    r = "iM"; /* non-taut iso = Inv(main iso) */
664
0
                    break;
665
0
                case iiEq2NONTAUT:
666
0
                    r = "in"; /* non-taut iso = Inv(non-taut) */
667
0
                    break;
668
0
                case (iiEq2NONTAUT | iiEq2ISO):
669
0
                    r = "iN"; /* non-taut iso = Inv(non-taut iso ) */
670
0
                    break;
671
0
                default:
672
0
                    r = "??"; /* should not happen */
673
0
                }
674
0
                break;
675
0
            default:
676
0
                r = "??"; /* should not happen */
677
0
                break;
678
0
            }
679
0
        }
680
0
        else
681
0
        { /* Inv stereo = another (non-inverted) stereo */
682
683
0
            switch (bType)
684
0
            {
685
0
            case iitISO: /* main iso = ...*/
686
0
                switch (bEq2)
687
0
                {
688
0
                case 0:
689
0
                    r = "m"; /* main         = (inverted aux) main */
690
0
                    break;
691
0
                default:
692
0
                    r = "??"; /* should not happen */
693
0
                    break;
694
0
                }
695
0
                break;
696
0
            case iitNONTAUT: /* non-taut = ... */
697
0
                switch (bEq2)
698
0
                {
699
0
                case 0:
700
0
                    r = "m"; /* non-taut     = (inverted aux) main */
701
0
                    break;
702
0
                default:
703
0
                    r = "??"; /* should not happen */
704
0
                    break;
705
0
                }
706
0
                break;
707
0
            case (iitNONTAUT | iitISO): /* non-taut iso = ...*/
708
0
                switch (bEq2)
709
0
                {
710
0
                case 0:
711
0
                    r = "m"; /* non-taut iso  = (inverted aux) main */
712
0
                    break;
713
0
                case iiEq2ISO:
714
0
                    r = "M"; /* non-taut iso  = (inverted aux) main iso */
715
0
                    break;
716
0
                case iiEq2NONTAUT:
717
0
                    r = "n"; /* non-taut iso  = (inverted aux) non-taut */
718
0
                    break;
719
0
                default:
720
0
                    r = "??"; /* should not happen */
721
0
                    break;
722
0
                }
723
0
                break;
724
0
            default:
725
0
                r = "??"; /* should not happen */
726
0
                break;
727
0
            }
728
0
        }
729
0
        break;
730
731
0
    case (iiNUMB | iiSTEREO_INV): /*------------- Inv Stereo Numbering ------------*/
732
0
        switch (bType)
733
0
        {
734
0
        case 0: /* inv stereo numb main = ...*/
735
0
            switch (bEq2)
736
0
            {
737
0
            case 0:
738
0
                r = "m"; /* inv stereo numb main     = main numb */
739
0
                break;
740
0
            default:
741
0
                r = "??"; /* should not happen */
742
0
                break;
743
0
            }
744
0
            break;
745
0
        case iitISO: /* inv stereo iso numb main = ...*/
746
0
            switch (bEq2)
747
0
            {
748
0
            case 0:
749
0
                r = "m"; /* inv stereo iso numb main = main numb  */
750
0
                break;
751
0
            case iiEq2INV:
752
0
                r = "im"; /* inv stereo iso numb main = InvStereo(main) numb */
753
0
                break;
754
0
            case iiEq2ISO:
755
0
                r = "M"; /* inv stereo iso numb main = isotopic main numb */
756
0
                break;
757
0
            default:
758
0
                r = "??"; /* should not happen */
759
0
                break;
760
0
            }
761
0
            break;
762
0
        case iitNONTAUT: /* inv stereo numb non-taut = ... */
763
0
            switch (bEq2)
764
0
            {
765
0
            case 0:
766
0
                r = "m"; /* inv stereo numb non-taut = main numb */
767
0
                break;
768
0
            case iiEq2NONTAUT:
769
0
                r = "n"; /* inv stereo numb non-taut = non-taut numb */
770
0
                break;
771
0
            case iiEq2INV:
772
0
                r = "im"; /* inv stereo numb non-taut =  InvStereo(main) numb  */
773
0
                break;
774
0
            default:
775
0
                r = "??"; /* should not happen */
776
0
                break;
777
0
            }
778
0
            break;
779
0
        case (iitNONTAUT | iitISO): /* inv stereo numb non-taut iso = ... */
780
0
            switch (bEq2)
781
0
            {
782
0
            case 0:
783
0
                r = "m"; /* inv stereo numb non-taut iso = main numb */
784
0
                break;
785
0
            case iiEq2ISO:
786
0
                r = "M"; /* inv stereo numb non-taut iso = main numb iso */
787
0
                break;
788
0
            case (iiEq2ISO | iiEq2INV):
789
0
                r = "iM"; /* inv stereo numb non-taut iso = InvStereo(main iso) numb */
790
0
                break;
791
0
            case iiEq2NONTAUT:
792
0
                r = "n"; /* inv stereo numb non-taut iso = non-taut numb */
793
0
                break;
794
0
            case (iiEq2NONTAUT | iiEq2ISO):
795
0
                r = "N"; /* inv stereo numb non-taut iso = non-taut iso numb */
796
0
                break;
797
0
            case iiEq2INV:
798
0
                r = "im"; /* inv stereo numb non-taut iso = InvStereo(main) numb */
799
0
                break;
800
0
            case (iiEq2NONTAUT | iiEq2INV):
801
0
                r = "in"; /* inv stereo numb non-taut iso = InvStereo(non-taut) numb ) */
802
0
                break;
803
0
            default:
804
0
                r = "??"; /* should not happen  */
805
0
                break;
806
0
            }
807
0
            break;
808
0
        default:
809
0
            r = "??"; /* should not happen */
810
0
            break;
811
0
        }
812
0
        break;
813
814
0
    case iiNUMB: /*------------- Canonical Numbering ------------*/
815
0
        switch (bType)
816
0
        {
817
0
        case 0:       /* numb main = ...*/
818
0
            r = "??"; /* should not happen */
819
0
            break;
820
0
        case iitISO: /* iso numb main = ...*/
821
0
            switch (bEq2)
822
0
            {
823
0
            case 0:
824
0
                r = "m"; /* iso numb main = main numb  */
825
0
                break;
826
0
            default:
827
0
                r = "??"; /* should not happen */
828
0
            }
829
0
            break;
830
0
        case iitNONTAUT: /* numb non-taut = ... */
831
0
            switch (bEq2)
832
0
            {
833
0
            case 0:
834
0
                r = "m"; /* numb non-taut = main numb */
835
0
                break;
836
0
            default:
837
0
                r = "??"; /* should not happen */
838
0
            }
839
0
            break;
840
0
        case (iitNONTAUT | iitISO): /* numb non-taut iso = ... */
841
0
            switch (bEq2)
842
0
            {
843
0
            case 0:
844
0
                r = "m"; /* numb non-taut iso = main numb */
845
0
                break;
846
0
            case iiEq2ISO:
847
0
                r = "M"; /* numb non-taut iso = main numb iso */
848
0
                break;
849
0
            case iiEq2NONTAUT:
850
0
                r = "n"; /* numb non-taut iso = non-taut numb */
851
0
                break;
852
0
            default:
853
0
                r = "??"; /* should not happen */
854
0
                break;
855
0
            }
856
0
            break;
857
0
        default:
858
0
            r = "??"; /* should not happen */
859
0
            break;
860
0
        }
861
0
        break;
862
863
25
    case iiEQU: /*------------- Atom Equivalence ------------*/
864
25
        switch (bType)
865
25
        {
866
0
        case 0:       /* equivalence main = ...*/
867
0
            r = "??"; /* should not happen */
868
0
            break;
869
25
        case iitISO: /* equivalence main iso = ...*/
870
25
            switch (bEq2)
871
25
            {
872
25
            case 0:
873
25
                r = "m"; /* equivalence main = main equ  */
874
25
                break;
875
0
            default:
876
0
                r = "??"; /* should not happen */
877
0
                break;
878
25
            }
879
25
            break;
880
25
        case iitNONTAUT: /* equivalence non-taut = ... */
881
0
            switch (bEq2)
882
0
            {
883
0
            case 0:
884
0
                r = "m"; /* equivalence non-taut = main equ */
885
0
                break;
886
0
            default:
887
0
                r = "??"; /* should not happen */
888
0
                break;
889
0
            }
890
0
            break;
891
0
        case (iitNONTAUT | iitISO): /*  equivalence non-taut iso = ... */
892
0
            switch (bEq2)
893
0
            {
894
0
            case 0:
895
0
                r = "m"; /* equivalence non-taut iso = main equ */
896
0
                break;
897
0
            case iiEq2ISO:
898
0
                r = "M"; /* equivalence non-taut iso = main iso equ */
899
0
                break;
900
0
            case iiEq2NONTAUT:
901
0
                r = "n"; /* equivalence non-taut iso = non-taut equ */
902
0
                break;
903
0
            default:
904
0
                r = "??"; /* should not happen */
905
0
                break;
906
0
            }
907
0
            break;
908
0
        default:
909
0
            r = "??"; /* should not happen */
910
0
            break;
911
25
        }
912
25
        break;
913
25
    default:
914
0
        r = "??"; /* should not happen */
915
0
        break;
916
25
    }
917
25
    return r;
918
25
}
919
920
6.86k
#define OUT_NONTAUT OUT_NN /* was OUT_NT until 2004-04-07 */
921
922
/****************************************************************************
923
  OutputINChI2( ... ) is called from SortAndPrintINChI( ... )
924
****************************************************************************/
925
int OutputINChI2(CANON_GLOBALS *pCG,
926
                 INCHI_IOS_STRING *strbuf,
927
                 INCHI_SORT *pINChISortTautAndNonTaut2[][TAUT_NUM],
928
                 int INCHI_basic_or_INCHI_reconnected,
929
                 ORIG_ATOM_DATA *orig_inp_data,
930
                 ORIG_STRUCT *pOrigStruct,
931
                 INPUT_PARMS *ip,
932
                 int bDisconnectedCoord,
933
                 int bOutputType,
934
                 int bINChIOutputOptions,
935
                 int num_components2[],
936
                 int num_non_taut2[],
937
                 int num_taut2[],
938
                 INCHI_IOSTREAM *output_file,
939
                 INCHI_IOSTREAM *log_file,
940
                 int num_input_struct,
941
                 int *pSortPrintINChIFlags,
942
                 unsigned char save_opt_bits)
943
2.28k
{
944
2.28k
    int bINChIOutputOptions0 = bINChIOutputOptions & ~(INCHI_OUT_PLAIN_TEXT | INCHI_OUT_PLAIN_TEXT_COMMENTS);
945
2.28k
    int bINChIOutputOptionsCur;
946
2.28k
    int bCurOption, ret, i;
947
948
2.28k
    ret = 0;
949
950
9.14k
    for (i = 0; i < 3; i++)
951
6.86k
    {
952
6.86k
        switch (i)
953
6.86k
        {
954
2.28k
        case 1:
955
2.28k
            bCurOption = INCHI_OUT_PLAIN_TEXT;
956
2.28k
            break;
957
2.28k
        case 2:
958
2.28k
            bCurOption = INCHI_OUT_PLAIN_TEXT_COMMENTS;
959
2.28k
            break;
960
2.28k
        default:
961
2.28k
            continue;
962
6.86k
        }
963
4.57k
        if (bINChIOutputOptions & bCurOption)
964
2.28k
        {
965
2.28k
            bINChIOutputOptionsCur = bINChIOutputOptions0 | bCurOption;
966
2.28k
            if (i != 1)
967
0
            {
968
0
                bINChIOutputOptionsCur &= ~INCHI_OUT_TABBED_OUTPUT;
969
0
            }
970
2.28k
            ret |= OutputINChI1(pCG,
971
2.28k
                                strbuf,
972
2.28k
                                pINChISortTautAndNonTaut2,
973
2.28k
                                INCHI_basic_or_INCHI_reconnected,
974
2.28k
                                orig_inp_data,
975
2.28k
                                pOrigStruct,
976
2.28k
                                ip,
977
2.28k
                                bDisconnectedCoord,
978
2.28k
                                bOutputType,
979
2.28k
                                bINChIOutputOptionsCur,
980
2.28k
                                num_components2,
981
2.28k
                                num_non_taut2,
982
2.28k
                                num_taut2,
983
2.28k
                                output_file,
984
2.28k
                                log_file,
985
2.28k
                                num_input_struct,
986
2.28k
                                pSortPrintINChIFlags,
987
2.28k
                                save_opt_bits);
988
2.28k
        }
989
4.57k
    }
990
991
2.28k
    return ret;
992
2.28k
}
993
994
/**
995
 * @brief Main actual worker which serializes InChI to string. Called from OutputINChI2( ... ) and from itself.
996
 *
997
 * @param pCG Pointer to global canonicalization data.
998
 * @param strbuf Pointer to the output string buffer.
999
 * @param pINChISortTautAndNonTaut2 Array of pointers to INCHI_SORT structures for tautomeric and non-tautomeric forms.
1000
 * @param INCHI_basic_or_INCHI_reconnected Flag indicating basic or reconnected InChI output.
1001
 * @param orig_inp_data Pointer to original atom data.
1002
 * @param pOrigStruct Pointer to the original structure data.
1003
 * @param ip Pointer to input parameters.
1004
 * @param bDisconnectedCoord Flag for disconnected metal coordination.
1005
 * @param bOutputType Output type (tautomeric/non-tautomeric/both).
1006
 * @param bINChIOutputOptions Bitmask of output options.
1007
 * @param num_components2 Array of component counts for each structure type.
1008
 * @param num_non_taut2 Array of non-tautomeric component counts.
1009
 * @param num_taut2 Array of tautomeric component counts.
1010
 * @param out_file Output stream for InChI string.
1011
 * @param log_file Output stream for logging.
1012
 * @param num_input_struct Number of input structures.
1013
 * @param pSortPrintINChIFlags Pointer to flags controlling sorting and printing.
1014
 * @param save_opt_bits Encoded bits for saved InChI creation options.
1015
 * @return int
1016
 */
1017
int OutputINChI1( CANON_GLOBALS *pCG,
1018
                  INCHI_IOS_STRING *strbuf,
1019
                  INCHI_SORT *pINChISortTautAndNonTaut2[][TAUT_NUM],
1020
                  int INCHI_basic_or_INCHI_reconnected,
1021
                  ORIG_ATOM_DATA *orig_inp_data,
1022
                  ORIG_STRUCT *pOrigStruct,
1023
                  INPUT_PARMS *ip,
1024
                  int bDisconnectedCoord,
1025
                  int bOutputType,
1026
                  int bINChIOutputOptions,
1027
                  int num_components2[],
1028
                  int num_non_taut2[],
1029
                  int num_taut2[],
1030
                  INCHI_IOSTREAM *out_file,
1031
                  INCHI_IOSTREAM *log_file,
1032
                  int num_input_struct,
1033
                  int *pSortPrintINChIFlags,
1034
                  unsigned char save_opt_bits )
1035
2.28k
{
1036
2.28k
    INCHI_OUT_CTL io;
1037
    /*
1038
        bINChIOutputOptions bits:
1039
          INCHI_OUT_NO_AUX_INFO           0x0001    do not output Aux Info
1040
          INCHI_OUT_SHORT_AUX_INFO        0x0002    output short version of Aux Info
1041
          INCHI_OUT_ONLY_AUX_INFO         0x0004    output only Aux Info
1042
          INCHI_OUT_EMBED_REC             0x0008    embed reconnected INChI into disconnected INChI
1043
1044
        bOutputType =
1045
         TAUT_YES  => tautomeric only (if no tautomeric components then no output;
1046
         TAUT_NON  => only non-tautomeric output (if no non-taut present then no output;
1047
         TAUT_BOTH => tautomeric and non-tautomeric
1048
    */
1049
2.28k
    int i, j, ii, jj, /*ii2, jj2,*/ bEmbeddedOutputCalled = 0;
1050
2.28k
    int bTautIsoHNum, bTautIsoAt, bHasIsotopicAtoms[TAUT_NUM];
1051
2.28k
    int bStereoSp2[TAUT_NUM], bStereoSp3[TAUT_NUM];
1052
2.28k
    int bIsotopicStereoSp2[TAUT_NUM], bIsotopicStereoSp3[TAUT_NUM];
1053
2.28k
    int bStereoAbsInverted[TAUT_NUM], bIsotopicStereoAbsInverted[TAUT_NUM];
1054
2.28k
    int bStereoAbs[TAUT_NUM], bIsotopicStereoAbs[TAUT_NUM];
1055
2.28k
    int bTautomericAcid, bHardAddRemProton;
1056
2.28k
    int bRequestedRacemicStereo = 0, bRequestedRelativeStereo = 0;
1057
2.28k
    int npass = 0; /* djb-rwth: removing redundant variables */
1058
2.28k
    int is_molecular_inorganic = 0;
1059
1060
2.28k
    INCHI_SORT *is, *is2;
1061
2.28k
    INChI *pINChI /*, *pINChI2*/;
1062
2.28k
    INChI_Aux *pINChI_Aux = NULL;
1063
1064
2.28k
    int ret = 0; /*  0 failed, 1 success */
1065
2.28k
    int intermediate_result = 0;
1066
2.28k
    int then_goto_repeat = 0;
1067
    /* djb-rwth: removing redundant variables */
1068
2.28k
    int bHasIsoH;
1069
    /* djb-rwth: removing redundant variables */
1070
2.28k
    int bTautAndNonTaut, bTautIsNonTaut;
1071
2.28k
    int nAtomsAllComp1, nAtomsAllComp2; /* v. 1.05 Total atoms in all components */
1072
1073
2.28k
    int bPlainText = 0 != (bINChIOutputOptions & (INCHI_OUT_PLAIN_TEXT | INCHI_OUT_PLAIN_TEXT_COMMENTS));
1074
2.28k
    int bPlainTextCommnts = 0 != (bINChIOutputOptions & INCHI_OUT_PLAIN_TEXT_COMMENTS);
1075
1076
2.28k
    char *pLF, *pTAB;
1077
#ifdef TARGET_LIB_FOR_WINCHI
1078
    int silent = 1;
1079
#endif
1080
1081
2.28k
    int bFixTranspChargeBug = 0;
1082
2.28k
#if (FIX_TRANSPOSITION_CHARGE_BUG == 1) /* 2008-01-02 */
1083
2.28k
    if (INCHI_OUT_FIX_TRANSPOSITION_CHARGE_BUG & bINChIOutputOptions)
1084
2.28k
        bFixTranspChargeBug = 1;
1085
2.28k
#endif
1086
1087
2.28k
    io.bAbcNumbers = ip->bAbcNumbers;
1088
1089
2.28k
    io.ATOM_MODE = ((io.bAbcNumbers ? CT_MODE_ABC_NUMBERS : 0) | CT_MODE_ATOM_COUNTS | CT_MODE_NO_ORPHANS
1090
2.28k
#if (EQL_H_NUM_TOGETHER == 1)
1091
2.28k
                    | CT_MODE_EQL_H_TOGETHER
1092
2.28k
#endif
1093
2.28k
#if (ABC_CT_NUM_CLOSURES == 1)
1094
2.28k
                    | (io.bAbcNumbers && ip->bCtPredecessors ? CT_MODE_ABC_NUM_CLOSURES : 0)
1095
2.28k
#endif
1096
2.28k
                    | (ip->bCtPredecessors ? CT_MODE_PREDECESSORS : 0));
1097
1098
2.28k
    io.TAUT_MODE = (io.bAbcNumbers ? CT_MODE_ABC_NUMBERS : 0);
1099
2.28k
    io.pSortPrintINChIFlags = pSortPrintINChIFlags;
1100
2.28k
    io.num_components = num_components2[INCHI_basic_or_INCHI_reconnected];
1101
2.28k
    io.pINChISortTautAndNonTaut = pINChISortTautAndNonTaut2[INCHI_basic_or_INCHI_reconnected];
1102
2.28k
    io.pINChISort = io.pINChISortTautAndNonTaut[TAUT_YES];
1103
2.28k
    io.pINChISort2 = io.pINChISortTautAndNonTaut[TAUT_YES];
1104
2.28k
    io.bAlways = 0;
1105
2.28k
    io.bUseMulipliers = 1;
1106
2.28k
    io.bOmitRepetitions = 1;
1107
2.28k
    io.bPlainTextTags = 2;        /* 0 => no plain tags, 1=> plain text tags, 2=>plaintext tags without consecutive // */
1108
2.28k
    io.bOutputType = bOutputType; /* remains constant */
1109
2.28k
    io.bOutType = bOutputType;    /* will change! */
1110
2.28k
    io.bOverflow = 0;
1111
2.28k
    io.bSecondNonTautPass = 0;
1112
2.28k
    io.bNonTautIsoIdentifierNotEmpty = 0;
1113
2.28k
    io.bNonTautNonIsoIdentifierNotEmpty = 0;
1114
2.28k
    io.bNonTautIsIdenticalToTaut = 1;
1115
2.28k
    io.bFhTag = 0;
1116
2.28k
    io.nTag = bPlainTextCommnts ? 3 : bPlainText ? 2
1117
2.28k
                                                 : 0; /* tag type */
1118
1119
2.28k
    if (NULL == orig_inp_data)
1120
0
    {
1121
        /*intermediate_result = 1;
1122
        goto exit_function;*/
1123
0
        io.n_zy = 0;
1124
0
        io.n_pzz = 0;
1125
0
        io.n_pzz = 0;
1126
0
    }
1127
2.28k
    else
1128
2.28k
    {
1129
2.28k
        io.n_zy = orig_inp_data->n_zy;
1130
2.28k
        io.n_pzz = 0;
1131
2.28k
        if (orig_inp_data->polymer)
1132
0
        {
1133
0
            io.n_pzz = orig_inp_data->polymer->n_pzz;
1134
0
        }
1135
2.28k
    }
1136
1137
    /*
1138
     * @nnuk
1139
     * The Molecular Inorganics subversion designator is structure-specific.
1140
     * For structure input it is determined from the Molecular Inorganics
1141
     * element classification when Molecular Inorganics processing is enabled.
1142
     * For InChI-derived input, the subversion designator is preserved from
1143
     * the input version prefix.
1144
     */
1145
2.28k
    if (ip->bMolecularInorganics)
1146
2.28k
    {
1147
2.28k
#if ( READ_INCHI_STRING == 1 )
1148
2.28k
        if (ip->bReadInChIOptions & (READ_INCHI_OUTPUT_INCHI | READ_INCHI_TO_STRUCTURE))
1149
0
        {
1150
0
            if (ip->bReadInChIOptions & READ_INCHI_INPUT_MI)
1151
0
            {
1152
0
                is_molecular_inorganic = 1;
1153
0
            }
1154
0
        }
1155
2.28k
        else
1156
2.28k
#endif
1157
2.28k
        if (orig_inp_data && orig_inp_data->at)
1158
2.28k
        {
1159
13.2k
            for (i = 0; i < orig_inp_data->num_inp_atoms; i++)
1160
11.7k
            {
1161
11.7k
                if (getElTypeforMolecularInorganics(orig_inp_data->at[i].el_number) != 0)
1162
830
                {
1163
830
                    is_molecular_inorganic = 1;
1164
830
                    break;
1165
830
                }
1166
11.7k
            }
1167
2.28k
        }
1168
2.28k
    }
1169
1170
2.28k
    io.bPolymers = ip->bPolymers;
1171
1172
    /* djb-rwth: removing redundant code */
1173
1174
#ifdef TARGET_LIB_FOR_WINCHI
1175
    /* @@@ From now on we will go with silent output; it ends on @@@ below */
1176
    silent = 1;
1177
#endif
1178
1179
    /* Analyze layers, make adjustments and fixes, etc. */
1180
1181
2.28k
    set_line_separators(bINChIOutputOptions, &pLF, &pTAB);
1182
2.28k
    memset(io.sDifSegs, DIFV_BOTH_EMPTY, sizeof(io.sDifSegs)); /* djb-rwth: memset_s C11/Annex K variant? */
1183
2.28k
    if (!strbuf || !(strbuf->pStr) || strbuf->nAllocatedLength <= 0)
1184
0
    {
1185
0
        inchi_ios_eprint(log_file, "Cannot allocate output buffer. No output for structure #%d.%s%s%s%s\n",
1186
0
                         num_input_struct, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
1187
0
        return ret;
1188
0
    }
1189
1190
    /*    -- commented out to allow empty InChI --
1191
    if (!io.num_components ) return 0;
1192
    */
1193
1194
6.86k
    for (i = 0; i < TAUT_NUM; i++)
1195
4.57k
    {
1196
4.57k
        bHasIsotopicAtoms[i] =
1197
4.57k
            io.num_comp[i] =
1198
4.57k
                bStereoSp2[i] =
1199
4.57k
                    bStereoSp3[i] =
1200
4.57k
                        bIsotopicStereoSp2[i] =
1201
4.57k
                            bIsotopicStereoSp3[i] =
1202
4.57k
                                io.bIsotopicOrigNumb[i] =
1203
4.57k
                                    bStereoAbs[i] =
1204
4.57k
                                        bIsotopicStereoAbs[i] =
1205
4.57k
                                            bStereoAbsInverted[i] =
1206
4.57k
                                                bIsotopicStereoAbsInverted[i] =
1207
4.57k
                                                    io.bRacemicStereo[i] =
1208
4.57k
                                                        io.bRelativeStereo[i] =
1209
4.57k
                                                            io.bIsotopicRacemicStereo[i] =
1210
4.57k
                                                                io.bIsotopicRelativeStereo[i] =
1211
4.57k
                                                                    io.bAtomEqu[i] =
1212
4.57k
                                                                        io.bTautEqu[i] =
1213
4.57k
                                                                            io.bIsotopicAtomEqu[i] =
1214
4.57k
                                                                                io.bIsotopicTautEqu[i] =
1215
4.57k
                                                                                    io.bInvStereo[i] =
1216
4.57k
                                                                                        io.bInvIsotopicStereo[i] =
1217
4.57k
                                                                                            io.bInvStereoOrigNumb[i] =
1218
4.57k
                                                                                                io.bInvIsotopicStereoOrigNumb[i] =
1219
4.57k
                                                                                                    io.bIgn_UU_Sp3[i] =
1220
4.57k
                                                                                                        io.bIgn_UU_Sp2[i] =
1221
4.57k
                                                                                                            io.bIgn_UU_Sp3_Iso[i] =
1222
4.57k
                                                                                                                io.bIgn_UU_Sp2_Iso[i] =
1223
4.57k
                                                                                                                    io.bChargesRadVal[i] =
1224
4.57k
                                                                                                                        io.bOrigCoord[i] = 0;
1225
4.57k
    }
1226
1227
    /*    Find if it is isotopic */
1228
2.28k
    io.bIsotopic =
1229
2.28k
        io.bTautomeric =
1230
2.28k
            io.bNonTautomeric =
1231
2.28k
                bTautomericAcid =
1232
2.28k
                    bHardAddRemProton =
1233
2.28k
                        bTautIsoHNum =
1234
2.28k
                            bTautIsoAt =
1235
2.28k
                                bTautAndNonTaut =
1236
2.28k
                                    bTautIsNonTaut = 0;
1237
1238
    /*
1239
         x = bStereo, bStereoSp2, bStereoSp3, bStereoAbsInverted,
1240
             bIsotopicStereo, bIsotopicStereoSp2, bIsotopicStereoSp3, bIsotopicStereoAbsInverted
1241
1242
         OUT_N1: x[TAUT_NON] refers to non-tautomeric only
1243
         OUT_T1: x[TAUT_YES] refers to tautomeric if exists otherwise non-tautomeric
1244
         OUT_NT: x[TAUT_NON] refers to non-taut representations of tautomeric
1245
         OUT_TN: x[TAUT_YES] refers to tautomeric if exists otherwise non-tautomeric
1246
                 x[TAUT_NON] refers to non-taut representations of tautomeric
1247
     */
1248
1249
2.28k
    memset(io.num_iso_H, 0, sizeof(io.num_iso_H)); /* djb-rwth: memset_s C11/Annex K variant? */
1250
2.28k
    io.nNumRemovedProtons = 0;
1251
    /* djb-rwth: removing redundant code */
1252
2.28k
    bHasIsoH = 0;
1253
2.28k
    io.bTautomericOutputAllowed = (io.bOutType == OUT_T1 || io.bOutType == OUT_TN);
1254
2.28k
    io.pINChISort = io.pINChISortTautAndNonTaut[io.bTautomericOutputAllowed ? TAUT_YES : TAUT_NON];
1255
2.28k
    is = io.pINChISort;
1256
    /* djb-rwth: removing redundant variables/code */
1257
1258
30.1k
    for (i = 0, is2 = io.pINChISortTautAndNonTaut[TAUT_NON]; i < io.num_components; i++, is++, is2 ? is2++ : NULL)
1259
27.8k
    {
1260
1261
27.8k
        CompINChILayers(is, is2, io.sDifSegs, bFixTranspChargeBug);
1262
1263
27.8k
        io.bNonTautIsIdenticalToTaut = io.bNonTautIsIdenticalToTaut && !CompINChITautVsNonTaut(is, is2, 1);
1264
1265
27.8k
        if (is && (pINChI_Aux = is->pINChI_Aux[TAUT_YES]))
1266
27.8k
        {
1267
111k
            for (j = 0; j < NUM_H_ISOTOPES; j++)
1268
83.6k
            {
1269
83.6k
                bHasIsoH += abs(pINChI_Aux->nNumRemovedIsotopicH[j]);
1270
83.6k
                io.num_iso_H[j] += pINChI_Aux->nNumRemovedIsotopicH[j];
1271
83.6k
            }
1272
27.8k
            io.nNumRemovedProtons += pINChI_Aux->nNumRemovedProtons;
1273
            /* djb-rwth: removing redundant code */
1274
27.8k
        }
1275
1276
27.8k
        if (io.bTautomericOutputAllowed && is) /* djb-rwth: fixing a NULL pointer dereference */
1277
27.8k
        {
1278
            /* Check for removed isotopic H */
1279
55.7k
            for (j = TAUT_YES; j < TAUT_NUM; j++)
1280
27.8k
            {
1281
27.8k
                switch (io.bOutType)
1282
27.8k
                {
1283
0
                case OUT_N1: /* x[TAUT_NON]: non-tautomeric only -- never happens */
1284
0
                    jj = GET_II(io.bOutType, is);
1285
0
                    if (jj != j)
1286
0
                        continue;
1287
                    /* djb-rwth: removing redundant code */
1288
0
                    break;
1289
0
                case OUT_T1: /* x[TAUT_YES]: tautomeric if present otherwise non-tautomeric */
1290
0
                    jj = GET_II(io.bOutType, is);
1291
0
                    if (jj != j)
1292
0
                        continue;
1293
                    /* djb-rwth: removing redundant code */
1294
0
                    break;
1295
0
                case OUT_NT: /* x[TAUT_NON]: only non-taut representations of tautomeric -- never happens */
1296
0
                    jj = GET_II(io.bOutType, is);
1297
0
                    if (jj != j)
1298
0
                        continue;
1299
                    /* djb-rwth: removing redundant code */
1300
0
                    break;
1301
                /* main path of control flow */
1302
27.8k
                case OUT_TN: /* x[TAUT_YES]: tautomeric if present otherwise non-tautomeric;
1303
                              * x[TAUT_NON]: non-taut only if tautomeric is present */
1304
27.8k
                    jj = (j == TAUT_YES) ? GET_II(OUT_T1, is) : (j == TAUT_NON) ? GET_II(OUT_NT, is)
1305
0
                                                                                : -1;
1306
27.8k
                    if (jj == TAUT_YES)
1307
27.8k
                    {
1308
                        /* Fix12 */
1309
27.8k
                        if (is->pINChI[jj]->lenTautomer > 0)
1310
5.71k
                        {
1311
5.71k
                            bTautAndNonTaut += (!is->pINChI[jj]->bDeleted && HAS_N(is));
1312
5.71k
                        }
1313
22.1k
                        else
1314
22.1k
                        {
1315
22.1k
                            bTautIsNonTaut++;
1316
22.1k
                        }
1317
27.8k
                    }
1318
27.8k
                    if (jj < 0)
1319
0
                        continue;
1320
                    /* djb-rwth: removing redundant code */
1321
27.8k
                    break;
1322
27.8k
                default:
1323
0
                    continue;
1324
27.8k
                }
1325
27.8k
                if (jj != j)
1326
0
                    continue;
1327
27.8k
                if ((pINChI = is->pINChI[jj]) && pINChI->nNumberOfAtoms > 0 && (pINChI_Aux = is->pINChI_Aux[jj]))
1328
27.8k
                {
1329
27.8k
                    bTautIsoHNum += (pINChI_Aux->nNumRemovedIsotopicH[0] +
1330
27.8k
                                     pINChI_Aux->nNumRemovedIsotopicH[1] +
1331
27.8k
                                     pINChI_Aux->nNumRemovedIsotopicH[2]);
1332
27.8k
                    bTautIsoAt += (pINChI->nNumberOfIsotopicAtoms > 0 || pINChI->nNumberOfIsotopicTGroups > 0);
1333
27.8k
                }
1334
27.8k
            }
1335
27.8k
        }
1336
27.8k
    }
1337
1338
2.28k
    io.sDifSegs[DIFL_M][DIFS_p_PROTONS] = io.nNumRemovedProtons ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
1339
2.28k
    io.sDifSegs[DIFL_MI][DIFS_h_H_ATOMS] = bHasIsoH ? DIFV_NEQ2PRECED : DIFV_BOTH_EMPTY;
1340
1341
2.28k
    MarkUnusedAndEmptyLayers(io.sDifSegs);
1342
1343
2.28k
    io.bNonTautIsIdenticalToTaut = io.bNonTautIsIdenticalToTaut && !bTautIsoHNum;
1344
2.28k
    nAtomsAllComp1 = nAtomsAllComp2 = 0;
1345
1346
30.1k
    for (i = 0, is = io.pINChISort; i < io.num_components; i++, is++)
1347
27.8k
    {
1348
27.8k
        int bCurIso, bCurHasIsoStereo /* Fix14 */, bCurTaut /*, bCurTaut2*/; /* djb-rwth: removing redundant variables */
1349
27.8k
        int bCompExists, bCurIsoHPos, bCurIsoHStereo;
1350
27.8k
        int bCurStereoSp2, bCurIsoStereoSp2, bCurStereoSp3, bCurIsoStereoSp3, bCurIsoStereoSp3Inv;
1351
27.8k
        int bCurRacemic, bCurRelative, bCurIsoRacemic, bCurIsoRelative;
1352
27.8k
        bCompExists = 0;
1353
1354
83.6k
        for (j = TAUT_NON; j < TAUT_NUM; j++)
1355
55.7k
        {
1356
55.7k
            switch (io.bOutType)
1357
55.7k
            {
1358
0
            case OUT_N1:
1359
                /* x[TAUT_NON]: non-tautomeric only */
1360
0
                jj = GET_II(io.bOutType, is);
1361
0
                if (jj != j)
1362
0
                    continue;
1363
0
                ii = TAUT_NON;
1364
0
                break;
1365
0
            case OUT_T1:
1366
                /* x[TAUT_YES]: tautomeric if present otherwise non-tautomeric */
1367
0
                jj = GET_II(io.bOutType, is);
1368
0
                if (jj != j)
1369
0
                    continue;
1370
0
                ii = TAUT_YES;
1371
0
                break;
1372
0
            case OUT_NT:
1373
                /* x[TAUT_NON]: only non-taut representations of tautomeric */
1374
0
                jj = GET_II(io.bOutType, is);
1375
0
                if (jj != j)
1376
0
                    continue;
1377
0
                ii = TAUT_NON;
1378
0
                break;
1379
            /* main control flow comes here: requested both mobile and fixed H results */
1380
55.7k
            case OUT_TN:
1381
                /* x[TAUT_YES]: tautomeric if present otherwise non-tautomeric; */
1382
                /* x[TAUT_NON]: non-taut only if tautomeric is present          */
1383
55.7k
                jj = (j == TAUT_YES) ? GET_II(OUT_T1, is) : (j == TAUT_NON) ? GET_II(OUT_NT, is)
1384
27.8k
                                                                            : -1;
1385
55.7k
                if (jj < 0)
1386
27.8k
                {
1387
                    /* Fix12 */
1388
27.8k
                    if (bTautAndNonTaut && bTautIsNonTaut &&
1389
0
                        j == TAUT_NON && 0 <= (jj = GET_II(OUT_T1, is)) &&
1390
0
                        !is->pINChI[jj]->bDeleted && !is->pINChI[jj]->lenTautomer)
1391
0
                    {
1392
0
                        ; /* the requested non-tautomeric component is in tautomeric position   */
1393
                          /*   (is->pINChI[TAUT_YES]);                                          */
1394
                          /*   process it also as non-tautomeric if Fixed-H layer was requested */
1395
0
                    }
1396
27.8k
                    else
1397
27.8k
                    {
1398
27.8k
                        continue;
1399
27.8k
                    }
1400
27.8k
                }
1401
1402
27.8k
                ii = j; /* ii is what we wanted; jj is what we found (0 = TAUT_NON: fixed_H, 1 = TAUT_YES: mobile_H) */
1403
1404
                /* -- not used 2004-09-16 ---
1405
                if ( is2 ) {
1406
                    jj2 = ( j == TAUT_YES )? GET_II(OUT_T1,is2) : ( j == TAUT_NON )? GET_II(OUT_NT,is2) : -1;
1407
                    if ( jj2 >= 0 ) {
1408
                        ii2 = j;
1409
                    } else {
1410
                        ii2 = -1;
1411
                    }
1412
                } else {
1413
                    jj2 = ii2 = -1;
1414
                }
1415
                -----------------------------*/
1416
27.8k
                break;
1417
1418
0
            default:
1419
0
                continue;
1420
55.7k
            }
1421
1422
27.8k
            if ((pINChI = is->pINChI[jj]) && pINChI->nNumberOfAtoms > 0)
1423
27.8k
            {
1424
                /*pINChI_Aux = is->pINChI_Aux[jj];*/
1425
27.8k
                bCompExists++;
1426
1427
27.8k
                if (j == TAUT_NON)
1428
0
                    nAtomsAllComp1 += pINChI->nNumberOfAtoms;
1429
27.8k
                else if (j == TAUT_YES)
1430
27.8k
                    nAtomsAllComp2 += pINChI->nNumberOfAtoms;
1431
1432
27.8k
                bCurTaut = (pINChI->lenTautomer > 0);
1433
27.8k
                bCurIso = (pINChI->nNumberOfIsotopicAtoms > 0 || pINChI->nNumberOfIsotopicTGroups > 0);
1434
27.8k
                bCurIsoHPos = ((pINChI->nPossibleLocationsOfIsotopicH && pINChI->nPossibleLocationsOfIsotopicH[0] > 1) || pINChI->lenTautomer > 1); /* djb-rwth: addressing LLVM warning */
1435
                /* present isotopic H + their possible positions AND/OR isotopic atoms */
1436
27.8k
                bCurIsoHStereo = (bCurIsoHPos && (bTautIsoHNum || bTautIsoAt)) || bCurIso; /* djb-rwth: addressing LLVM warning */
1437
27.8k
                if (jj == j && pINChI->bDeleted)
1438
271
                {
1439
271
                    io.num_comp[j]--;
1440
271
                    if (bCurTaut)
1441
271
                    {
1442
271
                        io.bTautomeric |= 1; /* tautomeric representation is present */
1443
271
                        io.bNonTautomeric |= HAS_N(is);
1444
271
                    }
1445
271
                    io.bIsotopic |= bCurIso;
1446
271
                    continue; /* deleted H(+) in tautomeric representation */
1447
271
                }
1448
1449
27.6k
                bCurStereoSp2 = pINChI->Stereo && (pINChI->Stereo->nNumberOfStereoBonds > 0);
1450
1451
27.6k
                bCurHasIsoStereo =
1452
27.6k
                    bCurStereoSp3 = pINChI->Stereo && (pINChI->Stereo->nNumberOfStereoCenters > 0);
1453
1454
27.6k
                bCurIsoStereoSp2 = bCurIsoHStereo && pINChI->StereoIsotopic && (pINChI->StereoIsotopic->nNumberOfStereoBonds > 0);
1455
27.6k
                bCurIsoStereoSp3 = bCurIsoHStereo && pINChI->StereoIsotopic && (pINChI->StereoIsotopic->nNumberOfStereoCenters > 0);
1456
27.6k
                bCurIsoStereoSp3Inv = bCurIsoStereoSp3 && pINChI->StereoIsotopic->nCompInv2Abs; /* inversion changes sp3 stereo */
1457
27.6k
                bRequestedRacemicStereo |= (0 != (pINChI->nFlags & INCHI_FLAG_RAC_STEREO));
1458
27.6k
                bRequestedRelativeStereo |= (0 != (pINChI->nFlags & INCHI_FLAG_REL_STEREO));
1459
1460
                /* Check whether isotopic stereo is same as non-isotopic; if same than do not output isotopic stereo */
1461
27.6k
                if (bCurStereoSp2 && bCurIsoStereoSp2)
1462
0
                {
1463
0
                    bCurIsoStereoSp2 = !Eql_INChI_Stereo(pINChI->Stereo, EQL_SP2, pINChI->StereoIsotopic, EQL_SP2, 0);
1464
0
                }
1465
27.6k
                if (bCurStereoSp3 && bCurIsoStereoSp3)
1466
0
                {
1467
                    /* bCurIsoStereoSp3=0 means (iso stereo sp3) = (non-iso stereo sp3) or (iso stereo sp3) = Inv(non-iso stereo sp3) */
1468
0
                    bCurIsoStereoSp3 = !Eql_INChI_Stereo(pINChI->Stereo, EQL_SP3, pINChI->StereoIsotopic, EQL_SP3,
1469
0
                                                         (pINChI->nFlags & INCHI_FLAG_RAC_STEREO) || (pINChI->nFlags & INCHI_FLAG_REL_STEREO));
1470
0
                    if (!bCurIsoStereoSp3)
1471
0
                    {
1472
                        /* Inversion changes iso sp3 differently from non-iso sp3 Fix11 */
1473
0
                        bCurIsoStereoSp3Inv &= (pINChI->StereoIsotopic->nCompInv2Abs != pINChI->Stereo->nCompInv2Abs);
1474
0
                    }
1475
0
                }
1476
1477
27.6k
                bCurRelative = bRequestedRelativeStereo && bCurStereoSp3;
1478
#if (REL_RAC_STEREO_IGN_1_SC == 1)
1479
                bCurRelative = bCurRelative &&
1480
                               (pINChI->Stereo->nNumberOfStereoCenters > 1) &&
1481
                               (pINChI->Stereo->nCompInv2Abs != 0) &&
1482
#endif
1483
1484
27.6k
                               bCurIsoRelative = bRequestedRelativeStereo && (bCurIsoStereoSp3 || bCurIsoStereoSp3Inv);
1485
#if (REL_RAC_STEREO_IGN_1_SC == 1)
1486
                bCurIsoRelative = bCurIsoRelative &&
1487
                                  (pINChI->StereoIsotopic->nNumberOfStereoCenters > 1) &&
1488
                                  (pINChI->StereoIsotopic->nCompInv2Abs != 0) &&
1489
#endif
1490
1491
27.6k
                                  bCurRacemic = bRequestedRacemicStereo && bCurStereoSp3;
1492
#if (REL_RAC_STEREO_IGN_1_SC == 1)
1493
                bCurRacemic = bCurRacemic &&
1494
                                      (pINChI->Stereo->nCompInv2Abs != 0) &&
1495
                                      (pINChI->Stereo->nNumberOfStereoCenters > 0)
1496
                                  ? pINChI->Stereo->nNumberOfStereoCenters
1497
                                  : 0;
1498
#endif
1499
1500
27.6k
                bCurIsoRacemic = bRequestedRacemicStereo && (bCurIsoStereoSp3 || bCurIsoStereoSp3Inv);
1501
#if (REL_RAC_STEREO_IGN_1_SC == 1)
1502
                bCurIsoRacemic = bCurIsoRacemic &
1503
                                             (pINChI->StereoIsotopic->nCompInv2Abs != 0) &&
1504
                                         (pINChI->StereoIsotopic->nNumberOfStereoCenters > 0)
1505
                                     ? pINChI->StereoIsotopic->nNumberOfStereoCenters
1506
                                     : 0;
1507
#endif
1508
27.6k
                if (bRequestedRelativeStereo)
1509
0
                {
1510
0
                    bCurStereoSp3 = bCurRelative || (bCurStereoSp3 && (pINChI->Stereo->nNumberOfStereoCenters > 1)); /* Fix11 */ /* djb-rwth: addressing LLVM warning */
1511
0
                    bCurIsoStereoSp3 = bCurIsoRelative ? bCurIsoStereoSp3 : 0;
1512
0
                }
1513
27.6k
                else
1514
27.6k
                {
1515
27.6k
                    if (bRequestedRacemicStereo)
1516
0
                    {
1517
0
                        bCurStereoSp3 = bCurRacemic > 1 || (bCurStereoSp3 && (pINChI->Stereo->nNumberOfStereoCenters > 1)); /* Fix11 */ /* djb-rwth: addressing LLVM warning */
1518
0
                        bCurIsoStereoSp3 = bCurIsoRacemic > 1 ? bCurIsoStereoSp3 : 0;
1519
0
                    }
1520
27.6k
                }
1521
                /* djb-rwth: removing redundant code */
1522
1523
27.6k
                io.bIsotopic |= bCurIso;
1524
27.6k
                bHasIsotopicAtoms[ii] |= bCurIso;
1525
27.6k
                bStereoSp2[ii] |= bCurStereoSp2;
1526
27.6k
                bStereoSp3[ii] |= bCurStereoSp3;
1527
27.6k
                io.bIgn_UU_Sp3[ii] |= !bCurStereoSp3 && (pINChI->nFlags & INCHI_FLAG_SC_IGN_ALL_UU);
1528
27.6k
                io.bIgn_UU_Sp2[ii] |= !bCurStereoSp2 && (pINChI->nFlags & INCHI_FLAG_SB_IGN_ALL_UU);
1529
27.6k
                bIsotopicStereoSp2[ii] |= bCurIsoStereoSp2;
1530
27.6k
                bIsotopicStereoSp3[ii] |= bCurIsoStereoSp3;
1531
27.6k
                io.bIgn_UU_Sp3_Iso[ii] |= !bCurIsoStereoSp3 && (pINChI->nFlags & INCHI_FLAG_SC_IGN_ALL_ISO_UU);
1532
27.6k
                io.bIgn_UU_Sp2_Iso[ii] |= !bCurIsoStereoSp2 && (pINChI->nFlags & INCHI_FLAG_SB_IGN_ALL_ISO_UU);
1533
27.6k
                bStereoAbs[ii] |= bCurStereoSp3 && (pINChI->Stereo->nCompInv2Abs != 0);
1534
1535
27.6k
                bStereoAbsInverted[ii] |= bCurStereoSp3 && (pINChI->Stereo->nCompInv2Abs < 0);
1536
1537
                /* Fix08: missing isotopic inverted flag if isotopic = inverted non-isotopic */
1538
27.6k
                bIsotopicStereoAbsInverted[ii] |= (bCurIsoStereoSp3 && (pINChI->StereoIsotopic->nCompInv2Abs < 0)) ||
1539
27.6k
                                                  (!bCurIsoStereoSp3 && pINChI->StereoIsotopic && pINChI->Stereo &&
1540
26.2k
                                                   pINChI->StereoIsotopic->nCompInv2Abs &&
1541
0
                                                   pINChI->StereoIsotopic->nCompInv2Abs != pINChI->Stereo->nCompInv2Abs); /* djb-rwth: addressing LLVM warnings */
1542
1543
                /* Fix 11: missing /s1 if only isotopic stereo is inverted */
1544
27.6k
                bIsotopicStereoAbs[ii] |= (bCurIsoStereoSp3 && (pINChI->StereoIsotopic->nCompInv2Abs != 0)) ||
1545
27.6k
                                          (!bCurIsoStereoSp3 && pINChI->StereoIsotopic && pINChI->Stereo &&
1546
26.2k
                                           pINChI->StereoIsotopic->nCompInv2Abs &&
1547
0
                                           pINChI->StereoIsotopic->nCompInv2Abs != pINChI->Stereo->nCompInv2Abs); /* djb-rwth: addressing LLVM warnings */
1548
1549
27.6k
                io.bRelativeStereo[ii] |= bCurRelative;
1550
27.6k
                io.bIsotopicRelativeStereo[ii] |= bCurIsoRelative;
1551
27.6k
                io.bRacemicStereo[ii] |= bCurRacemic;
1552
27.6k
                io.bIsotopicRacemicStereo[ii] |= bCurIsoRacemic;
1553
1554
27.6k
                bTautomericAcid |= (0 != (pINChI->nFlags & INCHI_FLAG_ACID_TAUT));
1555
27.6k
                bHardAddRemProton |= (0 != (pINChI->nFlags & INCHI_FLAG_HARD_ADD_REM_PROTON));
1556
27.6k
                if (bCurTaut)
1557
5.44k
                {
1558
5.44k
                    io.bTautomeric |= 1; /* tautomeric representation is present */
1559
                    /* does tautomeric structure have also a non-tautomeric repesentation? */
1560
5.44k
                    io.bNonTautomeric |= HAS_N(is);
1561
5.44k
                }
1562
1563
                /* Auxiliary info */
1564
27.6k
                if (!(bINChIOutputOptions & INCHI_OUT_NO_AUX_INFO) && (pINChI_Aux = is->pINChI_Aux[jj]))
1565
27.6k
                {
1566
                    /* detect presence of constitutional equivalence onfo */
1567
27.6k
                    int bCurEqu, bCurTautEqu = 0, bCurIsoEqu = 0, bCurIsoTautEqu = 0; /* Fix15-disabled */
1568
27.6k
                    io.bAtomEqu[ii] |= (bCurEqu = bHasEquString(pINChI_Aux->nConstitEquNumbers,
1569
27.6k
                                                                pINChI_Aux->nNumberOfAtoms)); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
1570
27.6k
                    if (bCurTaut)
1571
5.44k
                    {
1572
5.44k
                        io.bTautEqu[ii] |= (bCurTautEqu = bHasEquString(pINChI_Aux->nConstitEquTGroupNumbers,
1573
5.44k
                                                                        pINChI_Aux->nNumberOfTGroups)); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
1574
5.44k
                    }
1575
27.6k
                    if (bCurIso)
1576
20.2k
                    {
1577
20.2k
                        io.bIsotopicAtomEqu[ii] |= (bCurIsoEqu = bHasEquString(pINChI_Aux->nConstitEquIsotopicNumbers,
1578
20.2k
                                                                               pINChI_Aux->nNumberOfAtoms)) /*|| bCurEqu*/; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
1579
20.2k
                        if (bCurTaut)
1580
3.59k
                        {
1581
3.59k
                            io.bIsotopicTautEqu[ii] |= (bCurIsoTautEqu = bHasEquString(pINChI_Aux->nConstitEquIsotopicTGroupNumbers,
1582
3.59k
                                                                                       pINChI_Aux->nNumberOfTGroups)) /*|| bCurTautEqu*/; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
1583
3.59k
                        }
1584
                        /* non-zero if isotopic numbering for inverted isotopic stereo is different */
1585
20.2k
                        io.bIsotopicOrigNumb[ii] |= bCurHasIsoStereo && /* Fix14 */
1586
0
                                                    pINChI_Aux->nOrigAtNosInCanonOrdInv &&
1587
0
                                                    pINChI_Aux->nIsotopicOrigAtNosInCanonOrd &&
1588
0
                                                    (0 != memcmp(pINChI_Aux->nOrigAtNosInCanonOrdInv,
1589
0
                                                                 pINChI_Aux->nIsotopicOrigAtNosInCanonOrd,
1590
0
                                                                 sizeof(pINChI_Aux->nOrigAtNosInCanonOrdInv[0]) * pINChI_Aux->nNumberOfAtoms));
1591
20.2k
                    }
1592
                    /* Inverted stereo */
1593
27.6k
                    if (bCurStereoSp3 && pINChI->Stereo->nCompInv2Abs)
1594
0
                    {
1595
0
                        io.bInvStereo[ii] |= 1;
1596
0
                        io.bInvStereoOrigNumb[ii] |= pINChI_Aux->nOrigAtNosInCanonOrd &&
1597
0
                                                     pINChI_Aux->nOrigAtNosInCanonOrdInv &&
1598
0
                                                     (0 != memcmp(pINChI_Aux->nOrigAtNosInCanonOrd,
1599
0
                                                                  pINChI_Aux->nOrigAtNosInCanonOrdInv,
1600
0
                                                                  sizeof(pINChI_Aux->nOrigAtNosInCanonOrd[0]) * pINChI_Aux->nNumberOfAtoms));
1601
0
                    }
1602
1603
                    /* Inverted isotopic stereo */
1604
27.6k
                    if (bCurIsoStereoSp3 && pINChI->StereoIsotopic->nCompInv2Abs)
1605
0
                    {
1606
0
                        io.bInvIsotopicStereo[ii] |= 1;
1607
1608
0
                        io.bInvIsotopicStereoOrigNumb[ii] |= pINChI_Aux->nIsotopicOrigAtNosInCanonOrd &&
1609
0
                                                             pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv &&
1610
0
                                                             (0 != memcmp(pINChI_Aux->nIsotopicOrigAtNosInCanonOrd,
1611
0
                                                                          pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv,
1612
0
                                                                          sizeof(pINChI_Aux->nIsotopicOrigAtNosInCanonOrd[0]) * pINChI_Aux->nNumberOfAtoms));
1613
0
                    }
1614
1615
27.6k
                    if (pINChI_Aux->OrigInfo && bHasOrigInfo(pINChI_Aux->OrigInfo, pINChI_Aux->nNumberOfAtoms))
1616
13.7k
                    {
1617
13.7k
                        io.bChargesRadVal[ii] |= 1;
1618
13.7k
                    }
1619
27.6k
                }
1620
1621
27.6k
                if (ip->bEnhancedStereo)
1622
0
                {
1623
0
                    set_EnhancedStereo_t_m_layers(orig_inp_data, pINChI, pINChI_Aux);
1624
0
                }
1625
27.6k
            }
1626
27.8k
        }
1627
27.8k
        if (bCompExists)
1628
27.8k
        {
1629
83.6k
            for (j = TAUT_NON; j < TAUT_NUM; j++)
1630
55.7k
            {
1631
55.7k
                io.num_comp[j]++;
1632
55.7k
            }
1633
27.8k
        }
1634
27.8k
    }
1635
2.28k
    if (io.bTautomeric /*&& bTautomericAcid*/) /* "&& bTautomericAcid" commented out 2004-06-02 */
1636
907
    {
1637
907
        io.bTautomeric += bTautomericAcid; /* long-range tautomerism */
1638
907
        io.bTautomeric += (bHardAddRemProton ? 4 : 0);
1639
907
    }
1640
2.28k
    if (bRequestedRacemicStereo || bRequestedRelativeStereo)
1641
0
    {
1642
        /* do not output inverted stereo info */
1643
0
        for (i = 0; i < TAUT_NUM; i++)
1644
0
        {
1645
            /* Fix11 */
1646
0
            bStereoAbsInverted[i] =
1647
0
                bStereoAbs[i] =
1648
0
                    io.bInvStereo[i] =
1649
0
                        io.bInvStereoOrigNumb[i] = 0;
1650
            /* io.bIsotopicRelativeStereo[i]=0 may happen because iso stereo is same or inverted non-iso stereo */
1651
0
            bIsotopicStereoAbsInverted[i] =
1652
0
                bIsotopicStereoAbs[i] =
1653
0
                    io.bInvIsotopicStereo[i] =
1654
0
                        io.bInvIsotopicStereoOrigNumb[i] = 0;
1655
0
        }
1656
0
    }
1657
1658
2.28k
    io.iCurTautMode = io.bOutType == OUT_N1 ? TAUT_NON : /*  only non-taut */
1659
1660
2.28k
                          io.bOutType == OUT_T1 ? TAUT_YES
1661
2.28k
                                                : /*  tautomeric if present, otherwise non-tautomeric     */
1662
2.28k
                          io.bOutType == OUT_NT ? TAUT_NON
1663
2.28k
                                                : /*  only non-taut representations of tautomeric         */
1664
2.28k
                          io.bOutType == OUT_TN ? TAUT_YES
1665
2.28k
                                                : /*  tautomeric if present otherwise non-tautomeric;     */
1666
2.28k
                          -1;                     /*  separately output non-taut representations of tautomeric if present */
1667
1668
2.28k
    if (io.iCurTautMode < 0)
1669
0
    {
1670
0
        return 0; /* error */
1671
0
    }
1672
1673
    /* Now print out */
1674
1675
2.28k
    io.bOverflow = 0;
1676
2.28k
    io.num_components = io.num_comp[io.iCurTautMode];
1677
    /* djb-rwth: removing redundant code */
1678
1679
2.28k
    if (bINChIOutputOptions & INCHI_OUT_ONLY_AUX_INFO)
1680
0
    {
1681
0
        goto output_aux_info;
1682
0
    }
1683
1684
2.28k
    io.nCurINChISegment = DIFL_M;
1685
1686
    /* InChI output: version prefix */
1687
2.28k
    if (INCHI_basic_or_INCHI_reconnected == INCHI_BAS || !(bINChIOutputOptions & INCHI_OUT_EMBED_REC))
1688
2.28k
    {
1689
2.28k
        int is_beta = 0;
1690
2.28k
        int nAtomsAllComp = inchi_max(nAtomsAllComp1, nAtomsAllComp2);
1691
1692
2.28k
#if ( READ_INCHI_STRING == 1 )
1693
2.28k
        if (ip->bReadInChIOptions & (READ_INCHI_OUTPUT_INCHI | READ_INCHI_TO_STRUCTURE))
1694
0
        {
1695
0
            if (ip->bReadInChIOptions & READ_INCHI_INPUT_BETA)
1696
0
            {
1697
0
                is_beta = 1;
1698
0
            }
1699
0
        }
1700
2.28k
        else
1701
2.28k
#endif
1702
2.28k
        if (is_molecular_inorganic)
1703
830
        {
1704
            /*
1705
             * Molecular Inorganics is a Beta feature in this release.
1706
             */
1707
830
            is_beta = 1;
1708
830
        }
1709
1710
2.28k
        if (nAtomsAllComp > NORMALLY_ALLOWED_INP_MAX_ATOMS)
1711
0
        {
1712
            /* v. 1.05 for LargeMolecules */
1713
0
            is_beta = 1;
1714
0
        }
1715
2.28k
        if (pOrigStruct && pOrigStruct->polymer)
1716
0
        {
1717
            /* v. 1.05 for Polymers */
1718
0
            is_beta = 1;
1719
0
        }
1720
        /* specifically put 'B' for empty structure InChI    */
1721
        /* if "Polymers" or "LargeMolecules" requested        */
1722
2.28k
        else if (!pOrigStruct && (ip->bLargeMolecules || ip->bPolymers))
1723
0
        {
1724
0
            is_beta = 1;
1725
0
        }
1726
2.28k
        else if (pOrigStruct && pOrigStruct->n_zy)
1727
78
        {
1728
78
            is_beta = 1;
1729
78
        }
1730
2.20k
        else if (ip->bEnhancedStereo)
1731
0
        {
1732
0
            is_beta = 1;
1733
0
        }
1734
1735
2.28k
        OutputINCHI_VersionAndKind(out_file, strbuf, bINChIOutputOptions, is_beta, is_molecular_inorganic, pLF, pTAB);
1736
2.28k
    }
1737
1738
    /* InChI output: atoms */
1739
2.28k
    intermediate_result = OutputINCHI_MainLayerFormula(pCG, out_file, strbuf,
1740
2.28k
                                                       num_components2,
1741
2.28k
                                                       &INCHI_basic_or_INCHI_reconnected,
1742
2.28k
                                                       &io, pLF, pTAB);
1743
2.28k
    if (intermediate_result != 0)
1744
0
        goto exit_function;
1745
1746
    /* InChI output: connection table */
1747
2.28k
    intermediate_result = OutputINCHI_MainLayerConnections(pCG, out_file, strbuf, num_components2,
1748
2.28k
                                                           &INCHI_basic_or_INCHI_reconnected,
1749
2.28k
                                                           &io, pLF, pTAB);
1750
2.28k
    if (intermediate_result != 0)
1751
0
        goto exit_function;
1752
1753
    /* InChI output: hydrogens (with tautomeric info) */
1754
2.28k
    intermediate_result = OutputINCHI_MainLayerHydrogens(pCG, out_file, strbuf, num_components2,
1755
2.28k
                                                         &INCHI_basic_or_INCHI_reconnected,
1756
2.28k
                                                         &io, pLF, pTAB);
1757
2.28k
    if (intermediate_result != 0)
1758
0
        goto exit_function;
1759
1760
2.28k
    io.bFhTag = 0;
1761
2.28k
    npass = 0;
1762
1763
2.28k
repeat_INChI_output:
1764
1765
    /* InChI output: charge and  removed protons */
1766
2.28k
    intermediate_result = OutputINCHI_ChargeAndRemovedAddedProtonsLayers(pCG, out_file, strbuf,
1767
2.28k
                                                                         &io, pLF, pTAB);
1768
2.28k
    if (intermediate_result != 0)
1769
0
        goto exit_function;
1770
1771
    /* InChI output: polymer layer */
1772
2.28k
    if (npass == 0)
1773
2.28k
    {
1774
2.28k
        intermediate_result = OutputINCHI_PolymerLayer(pCG, out_file, strbuf,
1775
2.28k
                                                       &INCHI_basic_or_INCHI_reconnected,
1776
2.28k
                                                       orig_inp_data, pOrigStruct,
1777
2.28k
                                                       &io, pLF, pTAB);
1778
2.28k
        if (intermediate_result != 0)
1779
0
            goto exit_function;
1780
2.28k
    }
1781
1782
    /* InChI output: stereo (non-isotopic) */
1783
2.28k
    if (ip->bEnhancedStereo) {
1784
0
        intermediate_result = OutputINCHI_StereoLayer_EnhancedStereo( pCG, out_file, strbuf, &io, orig_inp_data, pLF, pTAB );
1785
2.28k
    } else {
1786
2.28k
        intermediate_result = OutputINCHI_StereoLayer( pCG, out_file, strbuf, &io, pLF, pTAB );
1787
2.28k
    }
1788
2.28k
    if (intermediate_result != 0)
1789
0
        goto exit_function;
1790
1791
    /* Switch from M to MI or from F to FI */
1792
2.28k
    io.nCurINChISegment++;
1793
1794
    /* InChI output: isotopic */
1795
2.28k
    intermediate_result = OutputINCHI_IsotopicLayer(pCG, out_file, strbuf,
1796
2.28k
                                                    &INCHI_basic_or_INCHI_reconnected,
1797
2.28k
                                                    &io, pLF, pTAB);
1798
1799
2.28k
    if (intermediate_result != 0)
1800
0
    {
1801
0
        goto exit_function;
1802
0
    }
1803
1804
    /*
1805
        At this point the INChI part of the output has been done.
1806
        If this INChI is tautomeric and non-tautomeric results exist,
1807
        then we need to output non-tautomeric data:
1808
            fixed H
1809
            stereo
1810
            isotopic
1811
            isotopic stereo
1812
    */
1813
1814
    /* InChI output: FixedH and sublayers */
1815
2.28k
    intermediate_result = OutputINCHI_FixedHLayerWithSublayers(pCG, out_file, strbuf,
1816
2.28k
                                                               &INCHI_basic_or_INCHI_reconnected,
1817
2.28k
                                                               &io, pLF, pTAB,
1818
2.28k
                                                               &then_goto_repeat);
1819
2.28k
    if (intermediate_result != 0)
1820
0
    {
1821
0
        goto exit_function;
1822
0
    }
1823
2.28k
    if (then_goto_repeat)
1824
0
    {
1825
0
        npass++;
1826
0
        goto repeat_INChI_output;
1827
0
    }
1828
1829
    /*
1830
        InChI output:  reconnected structure
1831
    */
1832
1833
2.28k
    bEmbeddedOutputCalled = 0;
1834
2.28k
    if (bDisconnectedCoord && INCHI_basic_or_INCHI_reconnected == INCHI_BAS &&
1835
0
        (bINChIOutputOptions & INCHI_OUT_EMBED_REC) && num_components2[INCHI_REC])
1836
0
    {
1837
0
        int nRet;
1838
0
        bEmbeddedOutputCalled = 1;
1839
1840
        /* output blank line before /R: in case of bPlainTextCommnts=1 */
1841
0
        inchi_ios_print_nodisplay(out_file, "%s", pLF);
1842
        /* end of disconnected INChI output */
1843
1844
0
        nRet = OutputINChI1(pCG,
1845
0
                            strbuf,
1846
0
                            pINChISortTautAndNonTaut2,
1847
0
                            INCHI_REC,
1848
0
                            orig_inp_data,
1849
0
                            pOrigStruct,
1850
0
                            ip,
1851
0
                            0 /*bDisconnectedCoord*/,
1852
0
                            bOutputType,
1853
0
                            bINChIOutputOptions | INCHI_OUT_NO_AUX_INFO,
1854
0
                            num_components2,
1855
0
                            num_non_taut2,
1856
0
                            num_taut2,
1857
0
                            out_file,
1858
0
                            log_file,
1859
0
                            num_input_struct,
1860
0
                            pSortPrintINChIFlags,
1861
0
                            save_opt_bits);
1862
1863
0
        if (!nRet)
1864
0
        {
1865
0
            goto exit_function; /* error */
1866
0
        }
1867
0
    }
1868
1869
    /* InChI output: save InChI creation options if requested */
1870
2.28k
    if (!bEmbeddedOutputCalled &&
1871
2.28k
        (bINChIOutputOptions & INCHI_OUT_SAVEOPT) &&
1872
0
        (0 == (bINChIOutputOptions & INCHI_OUT_STDINCHI)) /* not std-InChI output */
1873
2.28k
    )
1874
0
    {
1875
0
        char let1, let2;
1876
0
        GetSaveOptLetters(save_opt_bits, &let1, &let2);
1877
0
        inchi_ios_print_nodisplay(out_file, "\\%c%c", let1, let2);
1878
0
    }
1879
2.28k
    if (!bEmbeddedOutputCalled && !bPlainTextCommnts)
1880
2.28k
    { /* plain text comment earlier ended with LF */
1881
2.28k
        inchi_ios_print_nodisplay(out_file, "%s%s",
1882
2.28k
                                  (!num_components2[0] && !num_components2[1]) ? "//" : "", /* empty InChI=// */
1883
2.28k
                                  (bINChIOutputOptions & INCHI_OUT_NO_AUX_INFO) ? "\n" : pTAB);
1884
        /* end of INChI= output */
1885
2.28k
    }
1886
1887
2.28k
    inchi_strbuf_reset(strbuf);
1888
1889
#ifdef TARGET_LIB_FOR_WINCHI
1890
    /* @@@ Here we end up with silent output: display previously hidden output */
1891
    if (inchi_ios_flush_not_displayed(out_file) != -1)
1892
        silent = 0;
1893
#endif
1894
1895
2.28k
output_aux_info:
1896
1897
    /*  Output Aux Info */
1898
1899
2.28k
    io.bFhTag = 0;
1900
2.28k
    if ((bINChIOutputOptions & INCHI_OUT_NO_AUX_INFO) == 0)
1901
2.28k
    {
1902
1903
2.28k
        io.num_components = io.num_comp[io.iCurTautMode];
1904
1905
        /* AuxInfo: header and normalization type */
1906
2.28k
        intermediate_result = OutputAUXINFO_HeaderAndNormalization_type(pCG, out_file, strbuf,
1907
2.28k
                                                                        bINChIOutputOptions,
1908
2.28k
                                                                        &INCHI_basic_or_INCHI_reconnected,
1909
2.28k
                                                                        num_components2,
1910
2.28k
                                                                        &io, pLF, pTAB);
1911
2.28k
        if (intermediate_result != 0)
1912
0
            goto exit_function;
1913
1914
2.28k
    repeat_INChI_Aux_output:
1915
1916
        /* AuxInfo: original atom numbers and symmetry numbers (constit. equivalence /E: )    */
1917
2.28k
        intermediate_result = OutputAUXINFO_OriginalNumbersAndEquivalenceClasses(pCG, out_file, strbuf,
1918
2.28k
                                                                                 num_components2,
1919
2.28k
                                                                                 &io, pLF, pTAB);
1920
2.28k
        if (intermediate_result != 0)
1921
0
            goto exit_function;
1922
1923
        /* AuxInfo: tautomeric groups equivalence */
1924
2.28k
        intermediate_result = OutputAUXINFO_TautomericGroupsEquivalence(pCG, out_file, strbuf, &io);
1925
2.28k
        if (intermediate_result != 0)
1926
0
            goto exit_function;
1927
1928
        /* AuxInfo: stereo data */
1929
2.28k
        intermediate_result = OutputAUXINFO_Stereo(pCG, out_file, strbuf, &io, pLF, pTAB);
1930
2.28k
        if (intermediate_result != 0)
1931
0
            goto exit_function;
1932
1933
2.28k
    repeat_INChI_Aux_Iso_output:
1934
        /* AuxInfo: isotopic info */
1935
2.28k
        intermediate_result = OutputAUXINFO_IsotopicInfo(pCG, out_file, strbuf,
1936
2.28k
                                                         &INCHI_basic_or_INCHI_reconnected,
1937
2.28k
                                                         &io, pLF, pTAB);
1938
2.28k
        if (intermediate_result != 0)
1939
0
        {
1940
0
            goto exit_function;
1941
0
        }
1942
1943
        /*
1944
          At this point the INChI_Aux part of the output has been completed.
1945
          If this INChI is tautomeric and non-tautomeric results exist,
1946
          then we need to output non-tautomeric auxilialy data
1947
          (same as above excluding tautomeric information).
1948
          Currently, this is enabled for xml output only
1949
        */
1950
1951
2.28k
        if (io.bOutType == OUT_TN && io.bTautomeric && io.bNonTautomeric &&
1952
            /* Check whether the Fixed-H layer is empty */
1953
0
            (*pSortPrintINChIFlags & ((INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_NFIX_H_BAS : FLAG_SORT_PRINT_NO_NFIX_H_REC)) &&
1954
0
            (*pSortPrintINChIFlags & ((INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_IFIX_H_BAS : FLAG_SORT_PRINT_NO_IFIX_H_REC)))
1955
0
        {
1956
0
            io.bNonTautomeric = 0; /* bNonTautIdentifierNotEmpty == 0 => no fixed H info 02-10-2995 */
1957
0
        }
1958
1959
2.28k
        if (io.bOutType == OUT_TN && io.bTautomeric && io.bNonTautomeric)
1960
0
        {
1961
            /* add the second (non-tautomeric) output */
1962
0
            io.bOutType = OUT_NONTAUT;
1963
0
            io.iCurTautMode = TAUT_NON;
1964
0
            io.pINChISort = io.pINChISortTautAndNonTaut[TAUT_NON];
1965
0
            io.bSecondNonTautPass = 1;
1966
0
            io.num_components = io.num_comp[io.iCurTautMode];
1967
0
            io.bFhTag = AL_FIXH;
1968
0
            inchi_strbuf_reset(strbuf); /*pStr[io.tot_len=0] = '\0';*/
1969
1970
            /* if InChI Fixed-H isotopic is empty then do not output corresponding AuxInfo */
1971
0
            if (!(*pSortPrintINChIFlags &
1972
0
                  ((INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_NFIX_H_BAS : FLAG_SORT_PRINT_NO_NFIX_H_REC)))
1973
0
            {
1974
0
                npass++;
1975
0
                goto repeat_INChI_Aux_output;
1976
0
            }
1977
0
            else
1978
0
            {
1979
0
                npass++;
1980
0
                goto repeat_INChI_Aux_Iso_output;
1981
0
            }
1982
0
        }
1983
2.28k
        else
1984
2.28k
        {
1985
2.28k
            if (io.bOutType == OUT_NONTAUT && io.bOutputType == OUT_TN && io.bTautomeric && io.bNonTautomeric)
1986
0
            {
1987
                /* the second (non-taut) output has been done; restore variables */
1988
0
                io.bOutType = OUT_TN;
1989
0
                io.iCurTautMode = TAUT_YES;
1990
0
                io.pINChISort = io.pINChISortTautAndNonTaut[TAUT_YES];
1991
0
                io.bSecondNonTautPass = 0;
1992
                /* set correct num components for the reversibility info 02-10-2005 */
1993
0
                io.num_components = io.num_comp[io.iCurTautMode];
1994
0
                io.bFhTag = 0;
1995
0
            }
1996
2.28k
        }
1997
1998
        /*    Charges, radicals, unusual valences */
1999
2.28k
        intermediate_result = OutputAUXINFO_ChargesRadicalsAndUnusualValences(pCG, out_file, strbuf, &io, pLF, pTAB);
2000
2.28k
        if (intermediate_result != 0)
2001
0
        {
2002
0
            goto exit_function;
2003
0
        }
2004
2005
        /* Output the original input structure -- quick fix */
2006
2.28k
        intermediate_result = OutputAUXINFO_ReversibilityInfo(pCG, out_file, strbuf, pOrigStruct, &io, pLF, pTAB);
2007
2.28k
        if (intermediate_result != 0)
2008
0
        {
2009
0
            goto exit_function;
2010
0
        }
2011
2012
        /* Output polymeric Aux Info */
2013
2.28k
        intermediate_result = OutputAUXINFO_PolymerInfo(pCG, out_file, strbuf, pOrigStruct, &io, pLF, pTAB);
2014
2.28k
        if (intermediate_result != 0)
2015
0
        {
2016
0
            goto exit_function;
2017
0
        }
2018
2019
        /*
2020
            Output INChI_Aux of the reconnected structure
2021
        */
2022
2023
2.28k
        bEmbeddedOutputCalled = 0;
2024
2.28k
        if (bDisconnectedCoord && INCHI_basic_or_INCHI_reconnected == INCHI_BAS && (bINChIOutputOptions & INCHI_OUT_EMBED_REC) &&
2025
0
            num_components2[INCHI_REC] && !(bINChIOutputOptions & INCHI_OUT_NO_AUX_INFO))
2026
0
        {
2027
0
            int nRet;
2028
0
            bEmbeddedOutputCalled = 1;
2029
0
            inchi_ios_print(out_file, "%s", pLF);
2030
2031
0
            nRet = OutputINChI1(pCG,
2032
0
                                strbuf,
2033
0
                                pINChISortTautAndNonTaut2,
2034
0
                                INCHI_REC,
2035
0
                                NULL,
2036
0
                                NULL,
2037
0
                                ip,
2038
0
                                0 /*bDisconnectedCoord*/,
2039
0
                                bOutputType,
2040
0
                                INCHI_OUT_ONLY_AUX_INFO | bINChIOutputOptions,
2041
0
                                num_components2,
2042
0
                                num_non_taut2,
2043
0
                                num_taut2,
2044
0
                                out_file,
2045
0
                                log_file,
2046
0
                                num_input_struct,
2047
0
                                pSortPrintINChIFlags,
2048
0
                                save_opt_bits);
2049
2050
0
            if (!nRet)
2051
0
            {
2052
0
                goto exit_function; /* error */
2053
0
            }
2054
0
        }
2055
2056
        /* Close INChI_Aux */
2057
2.28k
        if (!bEmbeddedOutputCalled && !bPlainTextCommnts)
2058
2.28k
        {
2059
2.28k
            inchi_ios_print(out_file, "%s\n", (!num_components2[0] && !num_components2[1]) ? "//" : "");
2060
            /* plain text comment earlier ended with LF */
2061
2.28k
        }
2062
2063
        /* in wINChI window, separate AuxInfo: from InChIKey: with blank line */
2064
2.28k
        inchi_ios_print(out_file, "%s",
2065
2.28k
                        (bINChIOutputOptions & INCHI_OUT_WINCHI_WINDOW) ? "\n" : "");
2066
2.28k
    } /* end of output AuxInfo */
2067
2068
2.28k
    ret = 1;
2069
2070
2.28k
exit_function:
2071
2072
#ifdef TARGET_LIB_FOR_WINCHI
2073
    /* @@@ If for any error we get here silent, display previously hidden output */
2074
    if (silent)
2075
    {
2076
        /*
2077
           if ( !inchi_ios_flush_not_displayed( out_file ) != -1  )
2078
               silent = 0;
2079
       */
2080
        silent = 0;
2081
    }
2082
#endif
2083
2084
2.28k
    if (io.bOverflow)
2085
0
    {
2086
0
        inchi_ios_print(out_file, "\nFATAL ERROR: Output buffer overflow\n");
2087
0
    }
2088
2089
2.28k
    if (intermediate_result)
2090
0
    {
2091
0
        ret = 0;
2092
0
        inchi_ios_eprint(log_file, "InChI serialization error for structure #%d.%s%s%s%s\n",
2093
0
                         num_input_struct, SDF_LBL_VAL(ip->pSdfLabel, ip->pSdfValue));
2094
0
    }
2095
2096
2.28k
    return ret;
2097
2.28k
} /* OutputINChI1 */
2098
2099
/****************************************************************************/
2100
char *szGetTag(const INCHI_TAG *Tag,
2101
               int nTag,
2102
               int bTag,
2103
               char *szTag,
2104
               int *bAlways,
2105
               short tag_flag)
2106
30.4k
{
2107
30.4k
    int i, j, bit, num, len;
2108
30.4k
    const int MAX_TAG_NUM = tag_flag ? (int)IL_MAX_ORD : (int)AL_MAX_ORD; /* djb-rwth: fixing GHI #160 */
2109
30.4k
    if (0 < nTag && nTag < 3)
2110
30.4k
    {
2111
        /* no plain text comments: pick up the last tag */
2112
591k
        for (i = 0, j = -1, bit = 1; i < MAX_TAG_NUM; i++, bit <<= 1)
2113
561k
        {
2114
561k
            if (bTag & bit)
2115
39.4k
            {
2116
39.4k
                j = i;
2117
39.4k
            }
2118
561k
        }
2119
30.4k
        if (j >= 0)
2120
30.4k
        {
2121
#if USE_BCF
2122
            int stl1, stl2, dstsz;
2123
            stl1 = strlen(Tag[j].szXmlLabel) + 1;
2124
            stl2 = strlen(Tag[j].szPlainLabel) + 1;
2125
            dstsz = max_3(stl1, stl2, 5);
2126
            strcpy_s(szTag, dstsz, nTag == 1 ? Tag[j].szXmlLabel : nTag == 2 ? Tag[j].szPlainLabel
2127
                                                                             : "???"); /* djb-rwth: function replaced with its safe C11 variant */
2128
#else
2129
30.4k
            strcpy(szTag, nTag == 1 ? Tag[j].szXmlLabel : nTag == 2 ? Tag[j].szPlainLabel : "???"); /* djb-rwth: addressing coverity ID #499488 -- when nTag == 2, the "???" is avoided, which is correct */
2130
30.4k
#endif
2131
30.4k
            if (nTag != 2)
2132
0
            {
2133
0
                *bAlways = Tag[j].bAlwaysOutput;
2134
0
            }
2135
30.4k
            return szTag;
2136
30.4k
        }
2137
30.4k
    }
2138
0
    else if (nTag == 3)
2139
0
    {
2140
        /* plain text with comments */
2141
0
        szTag[0] = '{';
2142
0
        szTag[1] = '\0';
2143
0
        for (i = 0, j = -1, bit = 1, num = 0; i < MAX_TAG_NUM; i++, bit <<= 1)
2144
0
        {
2145
0
            if (bTag & bit)
2146
0
            {
2147
0
                j = i;
2148
0
                if (num++)
2149
0
                {
2150
0
                    strcat(szTag, ":");
2151
0
                }
2152
0
                strcat(szTag, Tag[i].szPlainComment);
2153
0
            }
2154
0
        }
2155
0
        if (num)
2156
0
        {
2157
0
            strcat(szTag, "}");
2158
0
            num = (int)strlen(Tag[j].szPlainLabel);
2159
0
            len = (int)strlen(szTag);
2160
0
            if (len)
2161
0
            {
2162
0
                memmove(szTag + num, szTag, (long long)len + 1); /* djb-rwth: cast operator added */
2163
0
                memcpy(szTag, Tag[j].szPlainLabel, num);
2164
0
            }
2165
0
            else
2166
0
            {
2167
0
                strcpy(szTag, Tag[j].szPlainLabel);
2168
0
            }
2169
0
            *bAlways = Tag[j].bAlwaysOutput;
2170
0
        }
2171
0
        else
2172
0
        {
2173
0
            strcpy(szTag, "???");
2174
0
        }
2175
0
        return szTag;
2176
0
    }
2177
2178
0
    strcpy(szTag, "???");
2179
0
    return szTag;
2180
30.4k
}
2181
2182
/* djb-rwth: removing redundant code */
2183
2184
/****************************************************************************
2185
    str_LineEnd( ... )
2186
2187
    First, checks if buffer overflow; then:
2188
    if ind < 0 (common usage, plain text output)
2189
        sets terminating '\0' in pStr,
2190
        optionally adds leading tag (e.g., '/' or "/c" )
2191
    *obsolete* if ind >=0 XML output
2192
2193
****************************************************************************/
2194
int str_LineEnd(const char *tag,
2195
                int *bOverflow,
2196
                INCHI_IOS_STRING *buf,
2197
                int ind,
2198
                int bPlainTextTags)
2199
17.8k
{
2200
    /* djb-rwth: removing redundant variables */
2201
17.8k
    int tag_len;
2202
2203
    /* check buffer overflow */
2204
17.8k
    if (*bOverflow)
2205
0
    {
2206
0
        return 1;
2207
0
    }
2208
2209
17.8k
    if (ind < 0)
2210
17.8k
    {
2211
        /* Plain text */
2212
        /* insert plain text tag if:
2213
           (a) pStr has non-zero length, or
2214
           (b) ind < -1
2215
        */
2216
17.8k
        if (buf->pStr[0] || ind < -1)
2217
17.8k
        {
2218
17.8k
            tag_len = bPlainTextTags ? (int)strlen(tag) : 0;
2219
17.8k
            if (tag_len > 0)
2220
15.5k
            {
2221
15.5k
                int n_added = tag_len + 2 + 2;
2222
15.5k
                inchi_strbuf_update(buf, n_added);
2223
2224
15.5k
                memmove(buf->pStr + tag_len, buf->pStr, (long long)buf->nUsedLength + 1); /* djb-rwth: cast operator added */
2225
                /* NB: trailing 0 is also memmoved */
2226
15.5k
                memcpy(buf->pStr, tag, tag_len);
2227
2228
                /* to be sure...  */
2229
15.5k
                buf->nUsedLength = strlen(buf->pStr);
2230
15.5k
            }
2231
17.8k
        }
2232
17.8k
    }
2233
2234
17.8k
    return 0;
2235
17.8k
}
2236
2237
/****************************************************************************/
2238
int CleanOrigCoord(MOL_COORD szCoord, int delim)
2239
57.4k
{
2240
172k
#define MIN_BOND_LENGTH (1.0e-6)
2241
57.4k
    char szVal[LEN_COORD + 1];
2242
57.4k
    MOL_COORD szBuf;
2243
57.4k
    char *q;
2244
57.4k
    int len, last, fst, dec_pnt, num_zer = 0, len_buf = 0, e;
2245
57.4k
    int k, i;
2246
57.4k
    double coord;
2247
2248
229k
    for (k = 0; k < NUM_COORD * LEN_COORD; k += LEN_COORD)
2249
172k
    {
2250
172k
        memcpy(szVal, szCoord + k, LEN_COORD);
2251
172k
        szVal[LEN_COORD] = '\0';
2252
172k
        lrtrim(szVal, &len);
2253
172k
        coord = strtod(szVal, &q);
2254
172k
        if (MIN_BOND_LENGTH > fabs(coord))
2255
121k
        {
2256
121k
            strcpy(szVal, "0");
2257
121k
            len = 1;
2258
121k
            num_zer++;
2259
121k
        }
2260
50.9k
        else
2261
50.9k
        {
2262
50.9k
            len = (int)(q - szVal);
2263
            /* last = (last mantissa digit position + 1)  */
2264
50.9k
            if ((q = strchr(szVal, 'e')) || (q = strchr(szVal, 'E')) ||
2265
48.7k
                (q = strchr(szVal, 'd')) || (q = strchr(szVal, 'D')))
2266
2.19k
            {
2267
                /* floating point */
2268
2.19k
                last = q - szVal;
2269
                /* remove (+) and leading zeroes from the exponent */
2270
2.19k
                e = (int)strtol(szVal + last + 1, &q, 10); /* exponent */
2271
2.19k
                if (e)
2272
1.90k
                {
2273
                    /* new exp; update the length */
2274
1.90k
                    len = last + 1 + sprintf(szVal + last + 1, "%d", e); /* print exp without leading zeroes and '+' */
2275
1.90k
                }
2276
289
                else
2277
289
                {
2278
                    /* exponent is zero */
2279
289
                    len = last;
2280
289
                }
2281
2.19k
            }
2282
48.7k
            else
2283
48.7k
            {
2284
48.7k
                last = len;
2285
48.7k
            }
2286
            /* fst = (first mantissa digit); fst=1 if the sign is present, otherwise 0 */
2287
50.9k
            fst = (szVal[0] != '.' && !isdigit(UCINT szVal[0]));
2288
            /* dec_pnt = (decimal point position) or last */
2289
50.9k
            if ((q = strchr(szVal, '.'))) /* djb-rwth: addressing LLVM warning */
2290
18.7k
            {
2291
18.7k
                dec_pnt = (int)(q - szVal);
2292
18.7k
            }
2293
32.1k
            else
2294
32.1k
            {
2295
32.1k
                dec_pnt = last;
2296
32.1k
            }
2297
50.9k
            last -= 1; /* last mantissa digit position */
2298
            /* remove trailing zeroes in the range dec_pnt+1..last-1 */
2299
62.0k
            for (i = last; dec_pnt < i && '0' == szVal[i]; i--)
2300
11.0k
                ;
2301
50.9k
            if (i == dec_pnt)
2302
3.21k
            {
2303
3.21k
                i--; /* remove decimal point, too */
2304
3.21k
            }
2305
50.9k
            if (i < last)
2306
6.72k
            {
2307
6.72k
                memmove(szVal + i + 1, szVal + last + 1, (long long)len - (long long)last); /* djb-rwth: cast operator added */
2308
6.72k
                len -= last - i;
2309
6.72k
            }
2310
            /* remove leading zeroes */
2311
66.7k
            for (i = fst; i < len && '0' == szVal[i]; i++)
2312
15.8k
            {
2313
15.8k
                ;
2314
15.8k
            }
2315
50.9k
            if ((i > fst) && (len - fst <= LEN_COORD + 1 - i) && (len - fst <= LEN_COORD + 1 - fst)) /* djb-rwth: fixing GHI #138 */
2316
3.26k
            {
2317
3.26k
                memmove(szVal + fst, szVal + i, (long long)len - (long long)fst); /* djb-rwth: cast operator added */
2318
3.26k
                len -= i - fst;
2319
3.26k
            }
2320
50.9k
        }
2321
172k
        if (len_buf && (len_buf < (int)sizeof(MOL_COORD)))
2322
114k
        {
2323
114k
#pragma warning(push)
2324
114k
#pragma warning(disable : 6386)
2325
114k
            szBuf[len_buf++] = delim;
2326
114k
#pragma warning(pop)
2327
114k
        }
2328
172k
        if (len_buf >= (int)sizeof(MOL_COORD)) /* djb-rwth: fixing coverity ID #499520 */
2329
0
        {
2330
0
            len_buf = (int)sizeof(MOL_COORD) - 1;
2331
0
            len = 0;
2332
0
        }
2333
172k
        memcpy(szBuf + len_buf, szVal, len); /* does not copy zero termination*/
2334
172k
        len_buf += len;
2335
172k
    }
2336
    /* zero termination */
2337
57.4k
    if (len_buf < (int)sizeof(MOL_COORD))
2338
54.7k
    {
2339
54.7k
        memset(szBuf + len_buf, 0, sizeof(MOL_COORD) - len_buf); /* djb-rwth: memset_s C11/Annex K variant? */
2340
54.7k
    }
2341
2342
57.4k
    memcpy(szCoord, szBuf, sizeof(MOL_COORD));
2343
2344
57.4k
    return num_zer;
2345
57.4k
#undef MIN_BOND_LENGTH
2346
57.4k
}
2347
2348
/****************************************************************************/
2349
int WriteOrigCoord(int num_inp_atoms,
2350
                   MOL_COORD *szMolCoord,
2351
                   int *i,
2352
                   char *szBuf,
2353
                   int buf_len)
2354
7.18k
{
2355
2356
7.18k
    int j, num_zer, len, cur_len;
2357
7.18k
    char *p;
2358
7.18k
    MOL_COORD szCurCoord;
2359
7.18k
    cur_len = 0;
2360
64.4k
    for (j = *i; j < num_inp_atoms;)
2361
57.4k
    {
2362
57.4k
        memcpy(szCurCoord, szMolCoord[j], sizeof(szCurCoord));
2363
57.4k
        num_zer = CleanOrigCoord(szCurCoord, ',');
2364
57.4k
        if (NUM_COORD == num_zer)
2365
29.8k
        {
2366
29.8k
            len = 0;
2367
29.8k
        }
2368
27.6k
        else
2369
27.6k
        {
2370
27.6k
            if ((p = (char *)memchr(szCurCoord, '\0', sizeof(szCurCoord)))) /* djb-rwth: addressing LLVM warning */
2371
24.9k
            {
2372
24.9k
                len = (int)(p - szCurCoord);
2373
24.9k
            }
2374
2.70k
            else
2375
2.70k
            {
2376
2.70k
                len = sizeof(szCurCoord);
2377
2.70k
            }
2378
27.6k
        }
2379
57.4k
        if (len + cur_len + 1 < buf_len)
2380
57.2k
        {
2381
57.2k
            if (len)
2382
27.3k
            {
2383
27.3k
                memcpy(szBuf + cur_len, szCurCoord, len * sizeof(szBuf[0]));
2384
27.3k
            }
2385
57.2k
            szBuf[cur_len += len] = ';';
2386
57.2k
            cur_len++;
2387
57.2k
            j++;
2388
57.2k
        }
2389
262
        else
2390
262
        {
2391
262
            break;
2392
262
        }
2393
57.4k
    }
2394
7.18k
    szBuf[cur_len] = '\0';
2395
7.18k
    *i = j; /* next item */
2396
2397
7.18k
    return cur_len;
2398
7.18k
}
2399
2400
/****************************************************************************
2401
  WriteOrigAtoms
2402
2403
  number of atoms
2404
  [c|n]              chiral/nonchiral
2405
2406
  Element
2407
  #valence
2408
  +/-[charge>1]
2409
  .#rad  (#rad=1, 2, 3: singlet, doulet, triplet)
2410
  [.]i#iso_mass
2411
  [.]{o|e|u|?} atom parity = {1:2:3:4}
2412
  [.]h[#of 1H>1]
2413
  [.]d[#of 2H>1]
2414
  [.]t[#of 3H>1]
2415
2416
  Note: . occurs only once and only if radical or 1-character element
2417
****************************************************************************/
2418
int WriteOrigAtoms(CANON_GLOBALS *pCG,
2419
                   int num_inp_atoms,
2420
                   inp_ATOM *at,
2421
                   int *i,
2422
                   char *szBuf,
2423
                   int buf_len,
2424
                   STRUCT_DATA *sd)
2425
7.08k
{
2426
7.08k
    int j, k, n, len, len0, cur_len, val, bonds_val, mw, parity, num_trans, is_ok, b_self;
2427
7.08k
    static const char szIsoH[] = "hdt";
2428
7.08k
    char szCurAtom[32];
2429
7.08k
    AT_NUMB nNeighOrder[MAXVAL], neigh;
2430
2431
7.08k
    cur_len = 0;
2432
7.08k
    if (0 == *i)
2433
4.61k
    {
2434
4.61k
        cur_len = sprintf(szBuf, "%d%s", num_inp_atoms,
2435
4.61k
                          (sd->bChiralFlag & FLAG_INP_AT_CHIRAL) ? "c" : (sd->bChiralFlag & FLAG_INP_AT_NONCHIRAL) ? "n"
2436
4.25k
                                                                                                                   : "");
2437
4.61k
    }
2438
64.2k
    for (j = *i; j < num_inp_atoms;)
2439
57.3k
    {
2440
        /* tetrahedral parity treatment */
2441
57.3k
        parity = 0;
2442
57.3k
        num_trans = 0;
2443
57.3k
        if (at[j].p_parity)
2444
0
        {
2445
            /* verify neighbors */
2446
0
            is_ok = 1;
2447
0
            b_self = 0;
2448
0
            for (n = 0, k = 0; n < MAX_NUM_STEREO_ATOM_NEIGH; n++)
2449
0
            {
2450
0
                neigh = at[j].p_orig_at_num[n] - 1;
2451
0
                if (is_in_the_list(at[j].neighbor, neigh, at[j].valence) &&
2452
0
                    at[neigh].orig_at_number == at[j].p_orig_at_num[n])
2453
0
                {
2454
                    /* real neighbor */
2455
0
                    nNeighOrder[k++] = at[j].p_orig_at_num[n];
2456
0
                }
2457
0
                else
2458
0
                {
2459
0
                    if ((int)neigh == j && at[neigh].orig_at_number == at[j].p_orig_at_num[n])
2460
0
                    {
2461
                        /* central atom is a neighbor */
2462
0
                        num_trans = n; /* move this neighbor to 0 position permutation parity */
2463
0
                        b_self++;
2464
0
                    }
2465
0
                    else
2466
0
                    {
2467
0
                        is_ok = 0;
2468
0
                        break;
2469
0
                    }
2470
0
                }
2471
0
            }
2472
0
            if (is_ok && b_self <= 1 && b_self + k == MAX_NUM_STEREO_ATOM_NEIGH)
2473
0
            {
2474
0
                num_trans += insertions_sort(pCG, nNeighOrder, k, sizeof(nNeighOrder[0]), comp_AT_RANK);
2475
0
                if (ATOM_PARITY_WELL_DEF(at[j].p_parity))
2476
0
                {
2477
0
                    parity = 2 - (num_trans + at[j].p_parity) % 2;
2478
0
                }
2479
0
                else
2480
0
                {
2481
0
                    if (ATOM_PARITY_ILL_DEF(at[j].p_parity))
2482
0
                    {
2483
0
                        parity = at[j].p_parity;
2484
0
                    }
2485
0
                    else
2486
0
                    {
2487
0
                        ; /* invalid atom parity */
2488
0
                    }
2489
0
                }
2490
0
            }
2491
0
            else
2492
0
            {
2493
0
                ; /* add error message here */
2494
0
            }
2495
0
        }
2496
2497
57.3k
        len = len0 = (int)strlen(at[j].elname);
2498
2499
57.3k
        memcpy(szCurAtom, at[j].elname, len);
2500
57.3k
        bonds_val = nBondsValenceInpAt(at + j, NULL, NULL);
2501
2502
57.3k
        if ((val = needed_unusual_el_valence(at[j].el_number, at[j].charge, at[j].radical,
2503
57.3k
                                             at[j].chem_bonds_valence, bonds_val, at[j].num_H, at[j].valence)) ||
2504
38.5k
            at[j].charge || at[j].radical || at[j].iso_atw_diff || NUM_ISO_H(at, j) || parity)
2505
51.3k
        {
2506
            /* valence */
2507
51.3k
            if (val)
2508
18.8k
            {
2509
18.8k
                len += sprintf(szCurAtom + len, "%d", val > 0 ? val : 0);
2510
18.8k
            }
2511
            /* charge */
2512
51.3k
            if ((val = at[j].charge)) /* djb-rwth: addressing LLVM warning */
2513
28.8k
            {
2514
28.8k
                szCurAtom[len++] = val > 0 ? '+' : '-';
2515
28.8k
                if ((val = abs(val)) > 1)
2516
7.53k
                {
2517
7.53k
                    len += sprintf(szCurAtom + len, "%d", val);
2518
7.53k
                }
2519
28.8k
            }
2520
            /* radical */
2521
51.3k
            if ((val = at[j].radical)) /* djb-rwth: addressing LLVM warning */
2522
4.11k
            {
2523
4.11k
                len += sprintf(szCurAtom + len, ".%d", val);
2524
4.11k
            }
2525
            /* isotopic shift */
2526
51.3k
            if ((val = at[j].iso_atw_diff)) /* djb-rwth: addressing LLVM warning */
2527
34.3k
            {
2528
34.3k
                mw = get_atomic_mass_from_elnum(at[j].el_number);
2529
34.3k
                if (val == 1)
2530
22
                    val = mw;
2531
34.2k
                else if (val > 0)
2532
33.5k
                    val = mw + val - 1;
2533
703
                else
2534
703
                    val = mw + val;
2535
2536
34.3k
                len += sprintf(szCurAtom + len, "%si%d", len == len0 ? "." : "", val);
2537
34.3k
            }
2538
            /* parity */
2539
51.3k
            if (parity)
2540
0
            {
2541
0
                len += sprintf(szCurAtom + len, "%s%s", len == len0 ? "." : "",
2542
0
                               parity == AB_PARITY_ODD ? "o" : parity == AB_PARITY_EVEN ? "e"
2543
0
                                                           : parity == AB_PARITY_UNKN   ? "u"
2544
0
                                                           : parity == AB_PARITY_UNDF   ? "?"
2545
0
                                                                                        : "");
2546
0
            }
2547
            /* implicit isotopic H */
2548
51.3k
            if (NUM_ISO_H(at, j))
2549
17.3k
            {
2550
69.4k
                for (k = 0; k < NUM_H_ISOTOPES; k++)
2551
52.0k
                {
2552
52.0k
                    if ((val = at[j].num_iso_H[k])) /* djb-rwth: addressing LLVM warning */
2553
23.5k
                    {
2554
23.5k
                        len += sprintf(szCurAtom + len, "%s%c", len == len0 ? "." : "", szIsoH[k]);
2555
23.5k
                        if (val > 1)
2556
10.1k
                        {
2557
10.1k
                            len += sprintf(szCurAtom + len, "%d", val);
2558
10.1k
                        }
2559
23.5k
                    }
2560
52.0k
                }
2561
17.3k
            }
2562
51.3k
        }
2563
2564
57.3k
        if (len + cur_len < buf_len)
2565
57.2k
        {
2566
57.2k
            memcpy(szBuf + cur_len, szCurAtom, len);
2567
57.2k
            cur_len += len;
2568
57.2k
            j++;
2569
57.2k
        }
2570
156
        else
2571
156
        {
2572
156
            break;
2573
156
        }
2574
57.2k
        szBuf[cur_len] = '\0';
2575
57.2k
        *i = j;
2576
57.2k
    }
2577
2578
7.08k
    return cur_len;
2579
7.08k
}
2580
2581
/****************************************************************************
2582
 WriteOrigBonds( ... )
2583
2584
    Output bonds in ascending order of the neighboring atom original numbers
2585
2586
<bonds> bpA;bpAbpA... </bonds>
2587
2588
b = bond type:
2589
=============
2590
w = undefined stereo, double
2591
s = single
2592
d = double
2593
t = triple
2594
a = aromatic
2595
p = up from the current atom to the neighbor
2596
P = uP from the neighbor to the current atom
2597
v = undefined stereo Either, single from the current atom to the neighbor
2598
V = undefined stereo Either, single from the neighbor to the current atom
2599
n = down from the current atom to the neighbor
2600
N = dowN from the neighbor to the current atom
2601
2602
p = bond parity:
2603
================
2604
- = odd
2605
+ = even
2606
u = unknown
2607
? = undefined
2608
  = no parity (empty)
2609
2610
2611
A = neighbor orig. atom number
2612
===============
2613
neighbor orig. atom number < number of the current atom
2614
Number of the current atom: 2 until first ";", 3 until 2nd ";", etc.
2615
****************************************************************************/
2616
int WriteOrigBonds(CANON_GLOBALS *pCG,
2617
                   int num_inp_atoms,
2618
                   inp_ATOM *at,
2619
                   int *i,
2620
                   char *szBuf,
2621
                   int buf_len,
2622
                   STRUCT_DATA *sd)
2623
6.65k
{
2624
6.65k
    int j, k, k2, kk, len, cur_len, j2 = 0, bond_stereo, bond_char, bond_parity, bond_parityNM, num_trans; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2625
6.65k
    char szCurBonds[7 * MAXVAL + 2];                                                                       /* num_neigh*(1 byte bond type + 2 bytes for bond parity up to 4 digits per neighbor number) + at the end one ';' */
2626
6.65k
    AT_RANK nNeighOrder[MAXVAL];
2627
6.65k
    int chain_len, pnxt_atom, pinxt2cur, pinxt_sb_parity_ord;
2628
6.65k
    int chain_len2, pnxt_atom2, pinxt2cur2, pinxt_sb_parity_ord2, m1, m2;
2629
6.65k
    int pcur_atom, picur2nxt, picur_sb_parity_ord;
2630
2631
6.65k
    cur_len = 0;
2632
59.2k
    for (j = *i; j < num_inp_atoms;)
2633
52.6k
    {
2634
52.6k
        len = 0;
2635
52.6k
        if (at[j].valence >= 1) /* djb-rwth: changing condition to avoid garbage values */
2636
1.39k
        {
2637
2.79k
            for (k = 0; k < at[j].valence; k++)
2638
1.39k
            {
2639
1.39k
                nNeighOrder[k] = k;
2640
1.39k
            }
2641
1.39k
            pCG->m_pn_RankForSort = at[j].neighbor;
2642
1.39k
            num_trans = insertions_sort(pCG, nNeighOrder, at[j].valence, sizeof(nNeighOrder[0]), CompRank); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2643
1.39k
        }
2644
51.2k
        else
2645
51.2k
        {
2646
51.2k
            num_trans = 0; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
2647
51.2k
            nNeighOrder[0] = 0;
2648
51.2k
        }
2649
54.0k
        for (kk = 0; kk < at[j].valence; kk++)
2650
1.39k
        {
2651
1.39k
            k = nNeighOrder[kk];
2652
1.39k
            j2 = at[j].neighbor[k];
2653
1.39k
            bond_parity = 0;
2654
1.39k
            bond_parityNM = 0;
2655
1.39k
            if (j2 < j)
2656
1.35k
            {
2657
1.35k
                bond_stereo = at[j].bond_stereo[k];
2658
1.35k
                switch (at[j].bond_type[k])
2659
1.35k
                {
2660
618
                case BOND_TYPE_SINGLE:
2661
618
                    switch (bond_stereo)
2662
618
                    {
2663
2
                    case STEREO_SNGL_UP:
2664
2
                        bond_char = 'p';
2665
2
                        break;
2666
58
                    case -STEREO_SNGL_UP:
2667
58
                        bond_char = 'P';
2668
58
                        break;
2669
4
                    case STEREO_SNGL_DOWN:
2670
4
                        bond_char = 'n';
2671
4
                        break;
2672
12
                    case -STEREO_SNGL_DOWN:
2673
12
                        bond_char = 'N';
2674
12
                        break;
2675
0
#if (FIX_EITHER_STEREO_IN_AUX_INFO == 1)
2676
2
                    case STEREO_SNGL_EITHER:
2677
2
                        bond_char = 'v';
2678
2
                        break;
2679
22
                    case -STEREO_SNGL_EITHER:
2680
22
                        bond_char = 'V';
2681
22
                        break;
2682
#else
2683
                    case STEREO_SNGL_EITHER:
2684
                    case -STEREO_SNGL_EITHER:
2685
                        bond_char = 'v';
2686
                        break;
2687
#endif
2688
518
                    default:
2689
518
                        bond_char = 's';
2690
518
                        break;
2691
618
                    }
2692
618
                    break;
2693
618
                case BOND_TYPE_DOUBLE:
2694
444
                    switch (bond_stereo)
2695
444
                    {
2696
38
                    case STEREO_DBLE_EITHER:
2697
38
                    case -STEREO_DBLE_EITHER:
2698
38
                        bond_char = 'w';
2699
38
                        break;
2700
406
                    default:
2701
406
                        bond_char = 'd';
2702
406
                        break;
2703
444
                    }
2704
444
                    break;
2705
444
                case BOND_TYPE_TRIPLE:
2706
128
                    bond_char = 't';
2707
128
                    break;
2708
0
                case BOND_TYPE_ALTERN:
2709
0
                    bond_char = 'a';
2710
0
                    break;
2711
168
                default:
2712
168
                    bond_char = 's';
2713
168
                    break;
2714
1.35k
                }
2715
                /* check for allene/cumulene */
2716
1.35k
                k2 = (int)(is_in_the_list(at[j2].neighbor, (AT_NUMB)j, at[j2].valence) - at[j2].neighbor);
2717
1.35k
                chain_len = chain_len2 = 0;
2718
1.35k
                if (at[j].sb_parity[0])
2719
0
                {
2720
0
                    for (m1 = 0; m1 < MAX_NUM_STEREO_BONDS && at[j].sb_parity[m1]; m1++)
2721
0
                    {
2722
0
                        if (k == at[j].sb_ord[m1])
2723
0
                        {
2724
0
                            chain_len = get_opposite_sb_atom(at, j, k,
2725
0
                                                             &pnxt_atom, &pinxt2cur, &pinxt_sb_parity_ord);
2726
0
                            break;
2727
0
                        }
2728
0
                    }
2729
0
                }
2730
1.35k
                if (at[j2].sb_parity[0])
2731
0
                {
2732
0
                    for (m2 = 0; m2 < MAX_NUM_STEREO_BONDS && at[j2].sb_parity[m2]; m2++)
2733
0
                    {
2734
0
                        if (k2 == at[j2].sb_ord[m2])
2735
0
                        {
2736
0
                            chain_len2 = get_opposite_sb_atom(at, j2, k2,
2737
0
                                                              &pnxt_atom2, &pinxt2cur2, &pinxt_sb_parity_ord2);
2738
0
                            break;
2739
0
                        }
2740
0
                    }
2741
0
                }
2742
1.35k
                if ((chain_len == 1 && chain_len2 == 1) || /* regular stereobond */
2743
1.35k
                    (chain_len > 1 && j > pnxt_atom))      /* djb-rwth: addressing LLVM warnings */
2744
0
                {
2745
                    /* j  is a cumulene endpoint */
2746
0
                    int m;
2747
0
                    pcur_atom = j; /* pcur_atom > pnxt_atom */
2748
0
                    picur2nxt = k;
2749
0
                    picur_sb_parity_ord = -1;
2750
0
                    for (m = 0; m < MAX_NUM_STEREO_BONDS && at[pcur_atom].sb_parity[m]; m++)
2751
0
                    {
2752
0
                        if (at[pcur_atom].sb_ord[m] == k)
2753
0
                        {
2754
0
                            picur_sb_parity_ord = m;
2755
0
                            break;
2756
0
                        }
2757
0
                    }
2758
                    /* djb-rwth: removing redundant code */
2759
0
                }
2760
1.35k
                else
2761
1.35k
                {
2762
1.35k
                    if (chain_len2 > 1 && j2 > pnxt_atom2)
2763
0
                    { /* j2 is a cumulene endpoint */
2764
0
                        int m;
2765
0
                        pcur_atom = j2;
2766
0
                        picur2nxt = k2;
2767
0
                        pnxt_atom = pnxt_atom2;
2768
0
                        pinxt2cur = pinxt2cur2;
2769
0
                        pinxt_sb_parity_ord = pinxt_sb_parity_ord2;
2770
0
                        picur_sb_parity_ord = -1;
2771
0
                        for (m = 0; m < MAX_NUM_STEREO_BONDS && at[pcur_atom].sb_parity[m]; m++)
2772
0
                        {
2773
0
                            if (at[pcur_atom].sb_ord[m] == k2)
2774
0
                                picur_sb_parity_ord = m;
2775
0
                        }
2776
0
                        chain_len = chain_len2;
2777
                        /* djb-rwth: removing redundant code */
2778
0
                    }
2779
1.35k
                    else
2780
1.35k
                    {
2781
1.35k
                        chain_len = 0; /* djb-rwth: removing redundant code */
2782
1.35k
                    }
2783
1.35k
                }
2784
                /*len += sprintf( szCurBonds + len, "%c%d", bond_char, val+1);*/
2785
1.35k
                if (chain_len)
2786
0
                {
2787
                    /* both atoms belong to a stereo bond */
2788
0
                    int kc;
2789
0
                    int p1 = 0, p2, p1NM = 0, p2NM, neigh, neigh1, neigh2, bHasMetal, bWellDef; /* djb-rwth: initialising p1 and p1NM */
2790
0
                    int bNeighSwitched1, bNeighSwitched2;
2791
2792
                    /* djb-rwth: avoiding buffer overrun as picur_sb_parity_ord == -1 is possible */
2793
0
                    if (picur_sb_parity_ord >= 0)
2794
0
                    {
2795
0
                        p1 = SB_PARITY_1(at[pcur_atom].sb_parity[picur_sb_parity_ord]);
2796
0
                        p1NM = SB_PARITY_2(at[pcur_atom].sb_parity[picur_sb_parity_ord]);
2797
0
                    }
2798
2799
0
                    p2 = SB_PARITY_1(at[pnxt_atom].sb_parity[pinxt_sb_parity_ord]);
2800
0
                    p2NM = SB_PARITY_2(at[pnxt_atom].sb_parity[pinxt_sb_parity_ord]);
2801
2802
0
                    bWellDef = ATOM_PARITY_WELL_DEF(p1) && ATOM_PARITY_WELL_DEF(p2);
2803
0
                    bHasMetal = ATOM_PARITY_WELL_DEF(p1NM) && ATOM_PARITY_WELL_DEF(p2NM);
2804
2805
0
                    bNeighSwitched1 = bNeighSwitched2 = 0;
2806
2807
0
                    if (bWellDef || bHasMetal)
2808
0
                    {
2809
2810
0
                        neigh1 = num_inp_atoms;
2811
0
                        for (kc = 0; kc < at[pcur_atom].valence; kc++)
2812
0
                        {
2813
0
                            if (kc == picur2nxt)
2814
0
                                continue;
2815
0
                            neigh = at[pcur_atom].neighbor[kc];
2816
0
                            if (bHasMetal && is_el_a_metal(at[neigh].el_number))
2817
0
                                continue;
2818
0
                            if (neigh < neigh1)
2819
0
                                neigh1 = neigh;
2820
0
                        }
2821
0
                        if (neigh1 < num_inp_atoms)
2822
0
                        {
2823
0
                            bNeighSwitched1 = (neigh1 != at[pcur_atom].neighbor[(int)at[pcur_atom].sn_ord[picur_sb_parity_ord]]);
2824
0
                        }
2825
0
                        else
2826
0
                        {
2827
0
                            AddErrorMessage(sd->pStrErrStruct, "Cannot find 0D stereobond neighbor");
2828
                            /*
2829
                            sd->nStructReadError =  99;
2830
                            sd->nErrorType = _IS_ERROR;
2831
                            */
2832
0
                        }
2833
2834
0
                        neigh2 = num_inp_atoms;
2835
0
                        for (kc = 0; kc < at[pnxt_atom].valence; kc++)
2836
0
                        {
2837
0
                            if (kc == pinxt2cur)
2838
0
                                continue;
2839
0
                            neigh = at[pnxt_atom].neighbor[kc];
2840
0
                            if (bHasMetal && is_el_a_metal(at[neigh].el_number))
2841
0
                                continue;
2842
0
                            if (neigh < neigh2)
2843
0
                                neigh2 = neigh;
2844
0
                        }
2845
0
                        if (neigh2 < num_inp_atoms)
2846
0
                        {
2847
0
                            bNeighSwitched2 = (neigh2 != at[pnxt_atom].neighbor[(int)at[pnxt_atom].sn_ord[pinxt_sb_parity_ord]]);
2848
0
                        }
2849
0
                        else
2850
0
                        {
2851
0
                            AddErrorMessage(sd->pStrErrStruct, "Cannot find 0D stereobond neighbor");
2852
                            /*
2853
                            sd->nStructReadError =  99;
2854
                            sd->nErrorType = _IS_ERROR;
2855
                            */
2856
0
                        }
2857
2858
0
                        if (neigh1 < num_inp_atoms && neigh2 < num_inp_atoms)
2859
0
                        {
2860
0
                            if (ATOM_PARITY_WELL_DEF(p1) && ATOM_PARITY_WELL_DEF(p2))
2861
0
                            {
2862
0
                                bond_parity = 2 - (p1 + p2 + bNeighSwitched1 + bNeighSwitched2) % 2;
2863
0
                            }
2864
0
                            else
2865
0
                            {
2866
0
                                bond_parity = inchi_min(p1, p2);
2867
0
                            }
2868
2869
0
                            if (bHasMetal)
2870
0
                            {
2871
0
                                bond_parityNM = 2 - (p1NM + p2NM + bNeighSwitched1 + bNeighSwitched2) % 2;
2872
0
                            }
2873
0
                            else
2874
0
                            {
2875
0
                                if (p1NM && p2NM)
2876
0
                                {
2877
0
                                    bond_parityNM = inchi_min(p1NM, p2NM);
2878
0
                                }
2879
0
                            }
2880
0
                        }
2881
0
                    }
2882
0
                    else
2883
0
                    {
2884
0
                        if (p1 && p2)
2885
0
                        {
2886
0
                            bond_parity = inchi_min(p1, p2);
2887
0
                        }
2888
0
                        if (p1NM && p2NM)
2889
0
                        {
2890
0
                            bond_parityNM = inchi_min(p1NM, p2NM);
2891
0
                        }
2892
0
                        if (bond_parityNM && !bond_parity)
2893
0
                        {
2894
0
                            bond_parity = AB_PARITY_UNDF;
2895
0
                        }
2896
0
                    }
2897
0
                }
2898
2899
1.35k
                len += sprintf(szCurBonds + len, "%c%s%s%d",
2900
1.35k
                               bond_char,
2901
2902
1.35k
                               (bond_parity == AB_PARITY_ODD) ? "-" : (bond_parity == AB_PARITY_EVEN) ? "+"
2903
1.35k
                                                                  : (bond_parity == AB_PARITY_UNKN)   ? "u"
2904
1.35k
                                                                  : (bond_parity == AB_PARITY_UNDF)   ? "?"
2905
1.35k
                                                                                                      : "",
2906
2907
1.35k
                               (bond_parityNM == AB_PARITY_ODD) ? "-" : (bond_parityNM == AB_PARITY_EVEN) ? "+"
2908
1.35k
                                                                    : (bond_parityNM == AB_PARITY_UNKN)   ? "u"
2909
1.35k
                                                                    : (bond_parityNM == AB_PARITY_UNDF)   ? "?"
2910
1.35k
                                                                                                          : "",
2911
2912
1.35k
                               j2 + 1);
2913
1.35k
            }
2914
1.39k
        }
2915
52.6k
        if (len + cur_len + 2 < buf_len)
2916
52.6k
        {
2917
52.6k
            memcpy(szBuf + cur_len, szCurBonds, len);
2918
52.6k
            cur_len += len;
2919
52.6k
            szBuf[cur_len++] = ';';
2920
52.6k
            j++;
2921
52.6k
        }
2922
0
        else
2923
0
        {
2924
0
            break;
2925
0
        }
2926
52.6k
    }
2927
6.65k
    szBuf[cur_len] = '\0';
2928
6.65k
    *i = num_inp_atoms > 0 ? j : 0;
2929
2930
6.65k
    return cur_len;
2931
6.65k
}
2932
2933
#define ORIG_STR_BUFLEN (7 * MAXVAL + 2) /* > 7*MAXVAL+2 = 142 */
2934
2935
/****************************************************************************
2936
 Fill out original input structure
2937
 ****************************************************************************/
2938
int OrigStruct_FillOut(CANON_GLOBALS *pCG,
2939
                       ORIG_ATOM_DATA *orig_inp_data,
2940
                       ORIG_STRUCT *pOrigStruct,
2941
                       STRUCT_DATA *sd)
2942
2.30k
{
2943
2.30k
    char szBuf[ORIG_STR_BUFLEN];
2944
2.30k
    int i, len, len_coord, len_atoms, len_bonds;
2945
2946
2.30k
    pOrigStruct->polymer = NULL;
2947
2.30k
    pOrigStruct->v3000 = NULL;
2948
2949
2.30k
    pOrigStruct->n_zy = orig_inp_data->n_zy;
2950
    /* Coordinates */
2951
2.30k
    len_coord = i = 0;
2952
2953
2.30k
    if (orig_inp_data->szCoord)
2954
2.30k
    {
2955
2956
4.87k
        while ((len = WriteOrigCoord(orig_inp_data->num_inp_atoms,
2957
4.87k
                                     orig_inp_data->szCoord, &i, szBuf, sizeof(szBuf)))) /* djb-rwth: addressing LLVM warning */
2958
2.57k
        {
2959
2.57k
            len_coord += len;
2960
2.57k
        }
2961
2.30k
        pOrigStruct->szCoord = (char *)inchi_malloc(((long long)len_coord + 1) * sizeof(pOrigStruct->szCoord[0])); /* djb-rwth: cast operator added */
2962
2.30k
        i = 0;
2963
2.30k
        if (pOrigStruct->szCoord &&
2964
2.30k
            len_coord == WriteOrigCoord(orig_inp_data->num_inp_atoms,
2965
2.30k
                                        orig_inp_data->szCoord, &i, pOrigStruct->szCoord, len_coord + 1) &&
2966
2.30k
            i == orig_inp_data->num_inp_atoms)
2967
2.30k
        {
2968
            /* success */
2969
2.30k
            if (orig_inp_data->szCoord)
2970
2.30k
            {
2971
2.30k
                inchi_free(orig_inp_data->szCoord);
2972
2.30k
                orig_inp_data->szCoord = NULL;
2973
2.30k
            }
2974
2.30k
        }
2975
0
        else
2976
0
        {
2977
0
            return -1;
2978
0
        }
2979
2.30k
    }
2980
2981
    /* Atoms */
2982
2.30k
    len_atoms = i = 0;
2983
4.77k
    while ((len = WriteOrigAtoms(pCG, orig_inp_data->num_inp_atoms,
2984
4.77k
                                 orig_inp_data->at, &i, szBuf, sizeof(szBuf), sd))) /* djb-rwth: addressing LLVM warning */
2985
2.46k
    {
2986
2.46k
        len_atoms += len;
2987
2.46k
        if (!orig_inp_data->num_inp_atoms)
2988
0
            break;
2989
2.46k
    }
2990
2.30k
    pOrigStruct->szAtoms = (char *)inchi_malloc(((long long)len_atoms + 1) * sizeof(pOrigStruct->szAtoms[0])); /* djb-rwth: cast operator added */
2991
2.30k
    i = 0;
2992
2.30k
    if (pOrigStruct->szAtoms &&
2993
2.30k
        len_atoms == WriteOrigAtoms(pCG, orig_inp_data->num_inp_atoms,
2994
2.30k
                                    orig_inp_data->at, &i, pOrigStruct->szAtoms, len_atoms + 1, sd) &&
2995
2.30k
        i == orig_inp_data->num_inp_atoms)
2996
2.30k
    {
2997
2.30k
        ; /* success */
2998
2.30k
    }
2999
0
    else
3000
0
    {
3001
0
        return -1;
3002
0
    }
3003
3004
    /* Bonds */
3005
2.30k
    len_bonds = 0;
3006
2.30k
    i = 1;
3007
4.34k
    while ((len = WriteOrigBonds(pCG, orig_inp_data->num_inp_atoms,
3008
4.34k
#if (FIX_CURE53_ISSUE_OOB_ALREADY_HAVE_THIS_MESSAGE == 1)
3009
4.34k
                                 orig_inp_data->at, &i, szBuf, sizeof(szBuf), sd))) /* djb-rwth: addressing LLVM warning */
3010
#else
3011
                                 orig_inp_data->at, &i, szBuf, sizeof(szBuf), NULL)))
3012
#endif
3013
2.03k
    {
3014
2.03k
        len_bonds += len;
3015
2.03k
        if (!orig_inp_data->num_inp_atoms)
3016
0
        {
3017
0
            break;
3018
0
        }
3019
2.03k
    }
3020
3021
2.30k
    pOrigStruct->szBonds = (char *)inchi_malloc(((long long)len_bonds + 2) * sizeof(pOrigStruct->szBonds[0])); /* djb-rwth: cast operator added */
3022
2.30k
    i = 1;
3023
3024
2.30k
    if (pOrigStruct->szBonds &&
3025
2.30k
        len_bonds == WriteOrigBonds(pCG, orig_inp_data->num_inp_atoms,
3026
2.30k
                                    orig_inp_data->at, &i, pOrigStruct->szBonds, len_bonds + 2, sd) &&
3027
2.30k
        i == orig_inp_data->num_inp_atoms)
3028
2.30k
    {
3029
2.30k
        ; /* success */
3030
2.30k
    }
3031
0
    else
3032
0
    {
3033
0
        return -1;
3034
0
    }
3035
2.30k
    pOrigStruct->num_atoms = orig_inp_data->num_inp_atoms;
3036
3037
    /* Extensions of v. 1.05 */
3038
2.30k
    if (orig_inp_data->polymer != NULL && orig_inp_data->polymer->n > 0 && orig_inp_data->valid_polymer)
3039
0
    {
3040
0
        pOrigStruct->polymer = orig_inp_data->polymer;
3041
        /* pointer copy, do not free after use! */
3042
0
    }
3043
2.30k
    if (orig_inp_data->v3000 != NULL)
3044
0
    {
3045
0
        pOrigStruct->v3000 = orig_inp_data->v3000;
3046
        /* pointer copy, do not free after use! */
3047
0
    }
3048
3049
2.30k
    return 0;
3050
2.30k
}
3051
3052
/****************************************************************************/
3053
void OrigStruct_Free(ORIG_STRUCT *pOrigStruct)
3054
2.30k
{
3055
2.30k
    if (pOrigStruct)
3056
2.30k
    {
3057
2.30k
        if (pOrigStruct->szAtoms)
3058
2.30k
        {
3059
2.30k
            inchi_free(pOrigStruct->szAtoms);
3060
2.30k
        }
3061
2.30k
        if (pOrigStruct->szBonds)
3062
2.30k
        {
3063
2.30k
            inchi_free(pOrigStruct->szBonds);
3064
2.30k
        }
3065
2.30k
        if (pOrigStruct->szCoord)
3066
2.30k
        {
3067
2.30k
            inchi_free(pOrigStruct->szCoord);
3068
2.30k
        }
3069
3070
        /* For
3071
3072
            OAD_Polymer *polymer;
3073
            OAD_V3000    *v3000;
3074
3075
            we used shallow (pointer) copy of analogs from orig_inp_data, so do not free these here */
3076
3077
        /*memset( pOrigStruct, 0, sizeof(*pOrigStruct) );*/
3078
2.30k
        pOrigStruct->szAtoms = NULL;
3079
2.30k
        pOrigStruct->szBonds = NULL;
3080
2.30k
        pOrigStruct->szCoord = NULL;
3081
2.30k
    }
3082
2.30k
}
3083
3084
/****************************************************************************
3085
    GetSaveOptLetters
3086
3087
    Get the two letters encoding the saved InChI creation options.
3088
3089
    The first one encodes RecMet/FixedH/SUU/SLUUD options.
3090
    Each of options is a binary switch {ON,OFF}, so it totals to 2*2*2*2=16 values
3091
    which are encoded by capital letters 'A' through 'P'.
3092
3093
    The second character encodes experimental (InChI 1 extension)
3094
    options KET and 15T.
3095
    Each of these options is a binary switch ON/OFF, so there are 2*2=4 combinations,
3096
    currently encoded by 'A' through 'D'.
3097
    Note that anything but 'A' here would indicate "extended" InChI 1
3098
    Also, there is a reservation for future needs: the 2nd memo char
3099
    may accommodate two more ON/OFF
3100
****************************************************************************/
3101
void GetSaveOptLetters(unsigned char save_opt_bits, char *let1, char *let2)
3102
0
{
3103
0
    const char a2p[] = "ABCDEFGHIJKLMNOP";
3104
    /* SaveOptBits layout: {unused|unused|Ket|15T|RecMet|FixedH|SUU|SLUUD} */
3105
0
    *let1 = a2p[(size_t)(save_opt_bits & 0x0f)];
3106
0
    *let2 = a2p[(size_t)((save_opt_bits & 0x30) >> 4)];
3107
0
}
3108
3109
/****************************************************************************
3110
Set line separators dependent on requested output mode
3111
****************************************************************************/
3112
void set_line_separators(int bINChIOutputOptions, char **pLF, char **pTAB)
3113
2.28k
{
3114
2.28k
    int bPlainTextCommnts = 0 != (bINChIOutputOptions & INCHI_OUT_PLAIN_TEXT_COMMENTS);
3115
3116
2.28k
    *pLF = (char *)(bPlainTextCommnts ? "\n" : "\0");
3117
3118
#if (!defined(TARGET_API_LIB) && !defined(TARGET_LIB_FOR_WINCHI))
3119
    {
3120
        int bPlainText = 0 != (bINChIOutputOptions & (INCHI_OUT_PLAIN_TEXT | INCHI_OUT_PLAIN_TEXT_COMMENTS));
3121
        int bPlainTabbedOutput = 0 != (bINChIOutputOptions & INCHI_OUT_TABBED_OUTPUT) &&
3122
                                 bPlainText && !bPlainTextCommnts;
3123
3124
        *pTAB = bPlainTabbedOutput ? (char*)"\t" : (char*)"\n";
3125
    }
3126
#else
3127
2.28k
    *pTAB = "\n";
3128
2.28k
#endif
3129
3130
2.28k
    return;
3131
2.28k
}
3132
3133
/****************************************************************************
3134
Output InChI: InChI version prefix
3135
****************************************************************************/
3136
int OutputINCHI_VersionAndKind(INCHI_IOSTREAM *out_file,
3137
                               INCHI_IOS_STRING *strbuf,
3138
                               int bINChIOutputOptions,
3139
                               int is_beta,
3140
                               int is_molecular_inorganic,
3141
                               char *pLF,
3142
                               char *pTAB)
3143
2.28k
{
3144
2.28k
    inchi_ios_print_nodisplay(out_file, "%s%s=%s", pLF, INCHI_NAME, pLF);
3145
3146
2.28k
    inchi_strbuf_reset(strbuf);
3147
2.28k
    inchi_strbuf_printf(strbuf, "%s", x_curr_ver);
3148
3149
    /*
3150
     * (@nnuk)
3151
     * The calculation class designator is:
3152
     *   S = Standard
3153
     *   B = Beta
3154
     *   N = non-standard
3155
     *
3156
     * Molecular Inorganics adds the subversion designator 'B'.
3157
     *
3158
     * Non-standard takes precedence over Beta because use of a non-standard
3159
     * option makes the resulting InChI non-standard irrespective of any
3160
     * experimental feature that would otherwise require the Beta designator.
3161
    */
3162
2.28k
    if (!(bINChIOutputOptions & INCHI_OUT_STDINCHI))
3163
0
    {
3164
0
        inchi_strbuf_printf(strbuf, "N");
3165
0
    }
3166
2.28k
    else if (is_beta)
3167
879
    {
3168
879
        inchi_strbuf_printf(strbuf, "B");
3169
879
    }
3170
1.40k
    else
3171
1.40k
    {
3172
1.40k
        inchi_strbuf_printf(strbuf, "S");
3173
1.40k
    }
3174
3175
2.28k
    if (is_molecular_inorganic)
3176
830
    {
3177
830
        inchi_strbuf_printf(strbuf, "B");
3178
830
    }
3179
3180
2.28k
    inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3181
3182
2.28k
    return 0;
3183
2.28k
}
3184
3185
/****************************************************************************
3186
Output InChI: main layer - formula, connections and hydrogens
3187
(incl. tautomeric info == mobile H)
3188
***************************************************************************/
3189
int OutputINCHI_MainLayerFormula(CANON_GLOBALS *pCG,
3190
                                 INCHI_IOSTREAM *out_file,
3191
                                 INCHI_IOS_STRING *strbuf,
3192
                                 int num_components2[],
3193
                                 int *INCHI_basic_or_INCHI_reconnected,
3194
                                 INCHI_OUT_CTL *io,
3195
                                 char *pLF,
3196
                                 char *pTAB)
3197
2.28k
{
3198
3199
    /* constitution ( dot-disconnected Hill formulas: <formula> ) */
3200
3201
2.28k
    if (num_components2[0] || num_components2[1])
3202
2.28k
    {
3203
2.28k
        szGetTag(IdentLbl, io->nTag, io->bTag1 = *INCHI_basic_or_INCHI_reconnected == INCHI_REC ? IL_REC_ : IL_FML_, io->szTag1, &io->bAlways, 1);
3204
2.28k
        inchi_strbuf_reset(strbuf);
3205
2.28k
        io->tot_len = str_HillFormula(io->pINChISort, strbuf, &io->bOverflow, io->bOutType,
3206
2.28k
                                      io->num_components, io->bUseMulipliers);
3207
3208
2.28k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, 1))
3209
0
        {
3210
0
            return 1;
3211
0
        }
3212
2.28k
        if (io->n_pzz > 0 && io->n_zy > 0)
3213
0
        {
3214
0
            int retm = MergeZzInHillFormula(strbuf);
3215
0
            if (0 != retm)
3216
0
            {
3217
0
                return -1;
3218
0
            }
3219
0
        }
3220
2.28k
        inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3221
2.28k
    }
3222
3223
2.28k
    LOG_NO_ARGS("\n#################### (L3318:ichiprt1.c) ##########################\n");
3224
2.28k
    LOG_MULT_ARGS("This is the Chemical formula : %s\n", strbuf->pStr);
3225
2.28k
    LOG_NO_ARGS("####################################################################\n");
3226
3227
2.28k
    return 0;
3228
2.28k
}
3229
3230
/****************************************************************************/
3231
int OutputINCHI_MainLayerConnections(CANON_GLOBALS *pCG,
3232
                                     INCHI_IOSTREAM *out_file,
3233
                                     INCHI_IOS_STRING *strbuf,
3234
                                     int num_components2[],
3235
                                     int *INCHI_basic_or_INCHI_reconnected,
3236
                                     INCHI_OUT_CTL *io,
3237
                                     char *pLF,
3238
                                     char *pTAB)
3239
2.28k
{
3240
    /* connections ( semicolon/dot-disconnected connection tables ) */
3241
3242
2.28k
    szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_CONN, io->szTag1, &io->bAlways, 1);
3243
2.28k
    inchi_strbuf_reset(strbuf);
3244
2.28k
    io->tot_len = 0;
3245
2.28k
    io->tot_len2 = str_Connections(pCG, io->pINChISort, strbuf, &io->bOverflow, io->bOutType,
3246
2.28k
                                   io->ATOM_MODE, io->num_components, io->bUseMulipliers);
3247
3248
    /* current version does not output empty (";;;;") connectivity */
3249
3250
2.28k
    if (io->tot_len != io->tot_len2)
3251
608
    { /* 2004-06-30: never output empty connection table */
3252
608
        io->tot_len = io->tot_len2;
3253
608
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -2, io->bPlainTextTags))
3254
0
        {
3255
0
            return 1; /* pStr overfow */
3256
0
        }
3257
608
        inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3258
608
    }
3259
3260
2.28k
    LOG_NO_ARGS("\n##################### (L3357:ichiprt1.c) #########################\n");
3261
2.28k
    LOG_MULT_ARGS("This is the Connection Layer : %s\n", strbuf->pStr);
3262
2.28k
    LOG_NO_ARGS("####################################################################\n");
3263
3264
2.28k
    return 0;
3265
2.28k
}
3266
3267
/****************************************************************************/
3268
int OutputINCHI_MainLayerHydrogens(CANON_GLOBALS *pCG,
3269
                                   INCHI_IOSTREAM *out_file,
3270
                                   INCHI_IOS_STRING *strbuf,
3271
                                   int num_components2[],
3272
                                   int *INCHI_basic_or_INCHI_reconnected,
3273
                                   INCHI_OUT_CTL *io,
3274
                                   char *pLF,
3275
                                   char *pTAB)
3276
2.28k
{
3277
3278
    /* hydrogen atoms (do not output empty) */
3279
3280
2.28k
    if (INCHI_SEGM_FILL == INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_h_H_ATOMS]))
3281
2.04k
    {
3282
2.04k
        szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_ALLH, io->szTag1, &io->bAlways, 1);
3283
2.04k
        inchi_strbuf_reset(strbuf);
3284
2.04k
        io->tot_len = 0;
3285
2.04k
        io->tot_len2 = str_H_atoms(io->pINChISort, strbuf, &io->bOverflow, io->bOutType,
3286
2.04k
                                   io->ATOM_MODE, io->TAUT_MODE,
3287
2.04k
                                   io->num_components, io->bUseMulipliers);
3288
2.04k
        if (io->tot_len != io->tot_len2)
3289
2.04k
        { /* 2004-06-21: never output empty */
3290
2.04k
            io->tot_len = io->tot_len2;
3291
2.04k
            if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -2, 1))
3292
0
            {
3293
0
                return 1;
3294
0
            }
3295
2.04k
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3296
2.04k
        }
3297
2.04k
    }
3298
3299
2.28k
    LOG_NO_ARGS("\n###################### (L3396:ichiprt1.c) ########################\n");
3300
2.28k
    LOG_MULT_ARGS("This is the Hydrogen Layer : %s\n", strbuf->pStr);
3301
2.28k
    LOG_NO_ARGS("####################################################################\n");
3302
3303
2.28k
    return 0;
3304
2.28k
}
3305
3306
/****************************************************************************
3307
Output InChI: charge and  removed protons layers
3308
****************************************************************************/
3309
int OutputINCHI_ChargeAndRemovedAddedProtonsLayers(CANON_GLOBALS *pCG,
3310
                                                   INCHI_IOSTREAM *out_file,
3311
                                                   INCHI_IOS_STRING *strbuf,
3312
                                                   INCHI_OUT_CTL *io,
3313
                                                   char *pLF,
3314
                                                   char *pTAB)
3315
2.28k
{
3316
3317
    /* charge  */
3318
3319
2.28k
    io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_q_CHARGE]);
3320
2.28k
    if (io->nSegmAction)
3321
1.44k
    {
3322
1.44k
        szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_CHRG | io->bFhTag, io->szTag1, &io->bAlways, 1);
3323
1.44k
        inchi_strbuf_reset(strbuf);
3324
1.44k
        io->tot_len = 0;
3325
1.44k
        if (INCHI_SEGM_FILL == io->nSegmAction)
3326
1.44k
        {
3327
1.44k
            io->tot_len = str_Charge2(io->pINChISort, io->pINChISort2,
3328
1.44k
                                      strbuf, &io->bOverflow, io->bOutType, io->num_components,
3329
1.44k
                                      io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
3330
1.44k
            io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3331
1.44k
        }
3332
1.44k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3333
0
        {
3334
0
            return 1;
3335
0
        }
3336
1.44k
        inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3337
1.44k
    }
3338
3339
    /* removed protons */
3340
3341
2.28k
    if (io->iCurTautMode == TAUT_YES && !io->bSecondNonTautPass)
3342
2.28k
    {
3343
3344
2.28k
        io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_p_PROTONS]);
3345
2.28k
        if (io->nSegmAction)
3346
631
        {
3347
631
            szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_PROT | io->bFhTag, io->szTag1, &io->bAlways, 1);
3348
631
            inchi_strbuf_reset(strbuf);
3349
631
            io->tot_len = 0;
3350
631
            inchi_strbuf_printf(strbuf, "%+d", io->nNumRemovedProtons);
3351
631
            if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3352
0
            {
3353
0
                return 1;
3354
0
            }
3355
631
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3356
631
        }
3357
1.65k
        else
3358
1.65k
        {
3359
1.65k
            if (io->bPlainTextTags == 1)
3360
0
                inchi_ios_print_nodisplay(out_file, "/");
3361
1.65k
        }
3362
2.28k
    }
3363
3364
2.28k
    return 0;
3365
2.28k
}
3366
3367
/**
3368
 * @brief Output InChI: stereo layer with sublayers.
3369
 *
3370
 * @param pCG Pointer to the CANON_GLOBALS structure containing global canonicalization data.
3371
 * @param out_file Pointer to the INCHI_IOSTREAM output stream where the stereo layer will be written.
3372
 * @param strbuf Pointer to an INCHI_IOS_STRING buffer used for string formatting and output.
3373
 * @param io Pointer to the INCHI_OUT_CTL structure containing output control and state information.
3374
 * @param pLF Pointer to a string used as the line feed (end-of-line) character(s).
3375
 * @param pTAB Pointer to a string used as the tab or separator character(s).
3376
 * @return Returns 0 on success, or a non-zero error code on failure.
3377
 */
3378
int OutputINCHI_StereoLayer( CANON_GLOBALS    *pCG,
3379
                             INCHI_IOSTREAM   *out_file,
3380
                             INCHI_IOS_STRING *strbuf,
3381
                             INCHI_OUT_CTL    *io,
3382
                             char             *pLF,
3383
                             char             *pTAB )
3384
2.28k
{
3385
3386
2.28k
    if (INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS] ) ||
3387
2.28k
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS] ) ||
3388
2.28k
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV] ) ||
3389
2.28k
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE] ))
3390
0
    {
3391
3392
        /*  stereo */
3393
3394
0
        szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_STER | io->bFhTag, io->szTag1, &io->bAlways, 1);
3395
3396
        /*  sp2 */
3397
3398
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS] )))
3399
0
        {
3400
0
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_DBND, io->szTag2, &io->bAlways, 1);
3401
0
            inchi_strbuf_reset(strbuf);
3402
0
            io->tot_len = 0;
3403
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3404
0
            {
3405
0
                io->tot_len = str_Sp2( io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3406
0
                                       io->bOutType, io->TAUT_MODE, io->num_components,
3407
0
                                       io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers );
3408
3409
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3410
0
            }
3411
3412
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3413
0
            {
3414
0
                return 1;
3415
0
            }
3416
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3417
0
        }
3418
0
        else
3419
0
        {
3420
0
            if (io->bPlainTextTags == 1)
3421
0
            {
3422
0
                inchi_ios_print_nodisplay(out_file, "/"); /* sp2 */
3423
0
            }
3424
0
        }
3425
3426
        /*  sp3 */
3427
3428
        /* t-layer */
3429
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS] ))) /* djb-rwth: addressing LLVM warning */
3430
0
        {
3431
0
            io->bRelRac = io->bRelativeStereo[io->iCurTautMode] || io->bRacemicStereo[io->iCurTautMode];
3432
0
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_SP3S, io->szTag2, &io->bAlways, 1);
3433
0
            inchi_strbuf_reset(strbuf);
3434
0
            io->tot_len = 0;
3435
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3436
0
            {
3437
0
                io->tot_len = str_Sp3( io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3438
0
                                       io->bOutType, io->TAUT_MODE, io->num_components, io->bRelRac,
3439
0
                                       io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers );
3440
3441
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3442
0
            }
3443
3444
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3445
0
            {
3446
0
                return 2;
3447
0
            }
3448
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3449
0
        }
3450
0
        else
3451
0
        {
3452
0
            if (io->bPlainTextTags == 1)
3453
0
            {
3454
0
                inchi_ios_print_nodisplay( out_file, "/" ); /* sp3 */
3455
0
            }
3456
0
        }
3457
3458
        /* m-layer */
3459
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV] ))) /* djb-rwth: addressing LLVM warning */
3460
0
        {
3461
0
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_INVS, io->szTag2, &io->bAlways, 1);
3462
0
            inchi_strbuf_reset(strbuf);
3463
0
            io->tot_len = 0;
3464
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3465
0
            {
3466
0
                io->tot_len = str_StereoAbsInv( io->pINChISort, strbuf,
3467
0
                                               &io->bOverflow, io->bOutType, io->num_components );
3468
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3469
0
            }
3470
3471
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3472
0
            {
3473
0
                return 3;
3474
0
            }
3475
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3476
0
        }
3477
0
        else
3478
0
        {
3479
0
            if (io->bPlainTextTags == 1)
3480
0
            {
3481
0
                inchi_ios_print_nodisplay(out_file, "/"); /* stereo-abs-inv */
3482
0
            }
3483
0
        }
3484
3485
        /* stereo type */
3486
3487
        /* s-layer */
3488
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE] )))
3489
0
        {
3490
0
            const char *p_stereo = io->bRelativeStereo[io->iCurTautMode] ? x_rel : io->bRacemicStereo[io->iCurTautMode] ? x_rac
3491
0
                                                                                                                        : x_abs;
3492
0
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_TYPS, io->szTag2, &io->bAlways, 1);
3493
0
            inchi_strbuf_reset(strbuf);
3494
0
            io->tot_len = 0;
3495
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3496
0
            {
3497
0
                (io->tot_len) += MakeDelim(p_stereo, strbuf, &io->bOverflow);
3498
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3499
0
            }
3500
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3501
0
            {
3502
0
                return 1;
3503
0
            }
3504
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3505
0
        }
3506
0
        if (io->bPlainTextTags == 1)
3507
0
        {
3508
0
            inchi_ios_print_nodisplay(out_file, "/"); /* no abs, inv or racemic stereo */
3509
0
        }
3510
0
    }
3511
2.28k
    else
3512
2.28k
    {
3513
2.28k
        if (io->bPlainTextTags == 1)
3514
0
        {
3515
0
            inchi_ios_print_nodisplay( out_file, "////" ); /* sp2, sp3, abs-inv, stereo.type */
3516
0
        }
3517
2.28k
    }
3518
3519
2.28k
    return 0;
3520
2.28k
}
3521
3522
/**
3523
 * @brief Output InChI: stereo layer with sublayers for enhanced stereochemistry (absolute, relative, racemic).
3524
 *
3525
 * @param pCG Pointer to the CANON_GLOBALS structure containing global canonicalization data.
3526
 * @param out_file Pointer to the INCHI_IOSTREAM output stream where the stereo layer will be written.
3527
 * @param strbuf Pointer to an INCHI_IOS_STRING buffer used for string formatting and output.
3528
 * @param io Pointer to the INCHI_OUT_CTL structure containing output control and state information.
3529
 * @param orig_inp_data Pointer to the ORIG_ATOM_DATA containing e.g. atom information.
3530
 * @param pLF Pointer to a string used as the line feed (end-of-line) character(s).
3531
 * @param pTAB Pointer to a string used as the tab or separator character(s).
3532
 * @return Returns 0 on success, or a non-zero error code on failure.
3533
 */
3534
int OutputINCHI_StereoLayer_EnhancedStereo(
3535
        CANON_GLOBALS    *pCG,
3536
        INCHI_IOSTREAM   *out_file,
3537
        INCHI_IOS_STRING *strbuf,
3538
        INCHI_OUT_CTL    *io,
3539
        ORIG_ATOM_DATA   *orig_inp_data,
3540
        char             *pLF,
3541
        char             *pTAB )
3542
0
{
3543
3544
0
    if (INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS] ) ||
3545
0
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS] ) ||
3546
0
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV] ) ||
3547
0
        INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE] ))
3548
0
    {
3549
3550
        /*  stereo */
3551
3552
0
        szGetTag( IdentLbl, io->nTag, io->bTag1 = IL_STER | io->bFhTag, io->szTag1, &io->bAlways, 1 );
3553
3554
        /*  sp2 */
3555
3556
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS] )))
3557
0
        {
3558
0
            szGetTag( IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_DBND, io->szTag2, &io->bAlways, 1 );
3559
0
            inchi_strbuf_reset( strbuf );
3560
0
            io->tot_len = 0;
3561
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3562
0
            {
3563
0
                io->tot_len = str_Sp2( io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3564
0
                                       io->bOutType, io->TAUT_MODE, io->num_components,
3565
0
                                       io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers );
3566
3567
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3568
0
            }
3569
3570
0
            if (str_LineEnd( io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags ))
3571
0
            {
3572
0
                return 1;
3573
0
            }
3574
0
            inchi_ios_print_nodisplay( out_file, "%s%s", strbuf->pStr, pLF );
3575
0
        }
3576
0
        else
3577
0
        {
3578
0
            if (io->bPlainTextTags == 1)
3579
0
            {
3580
0
                inchi_ios_print_nodisplay( out_file, "/" ); /* sp2 */
3581
0
            }
3582
0
        }
3583
3584
        /*  sp3 */
3585
3586
        /* t-layer */
3587
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS] ))) /* djb-rwth: addressing LLVM warning */
3588
0
        {
3589
0
            io->bRelRac = io->bRelativeStereo[io->iCurTautMode] || io->bRacemicStereo[io->iCurTautMode];
3590
0
            szGetTag( IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_SP3S, io->szTag2, &io->bAlways, 1 );
3591
0
            inchi_strbuf_reset( strbuf );
3592
0
            io->tot_len = 0;
3593
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3594
0
            {
3595
0
                io->tot_len = str_Sp3( io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3596
0
                                       io->bOutType, io->TAUT_MODE, io->num_components, io->bRelRac,
3597
0
                                       io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers );
3598
3599
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3600
0
            }
3601
3602
0
            if (str_LineEnd( io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags ))
3603
0
            {
3604
0
                return 2;
3605
0
            }
3606
0
            inchi_ios_print_nodisplay( out_file, "%s%s", strbuf->pStr, pLF );
3607
0
        }
3608
0
        else
3609
0
        {
3610
0
            if (io->bPlainTextTags == 1)
3611
0
            {
3612
0
                inchi_ios_print_nodisplay( out_file, "/" ); /* sp3 */
3613
0
            }
3614
0
        }
3615
3616
        /* m-layer */
3617
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV] ))) /* djb-rwth: addressing LLVM warning */
3618
0
        {
3619
0
            szGetTag( IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_INVS, io->szTag2, &io->bAlways, 1 );
3620
0
            inchi_strbuf_reset( strbuf );
3621
0
            io->tot_len = 0;
3622
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3623
0
            {
3624
0
                io->tot_len = str_StereoAbsInv( io->pINChISort, strbuf,
3625
0
                                               &io->bOverflow, io->bOutType, io->num_components );
3626
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3627
0
            }
3628
3629
0
            if (str_LineEnd( io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags ))
3630
0
            {
3631
0
                return 3;
3632
0
            }
3633
0
            inchi_ios_print_nodisplay( out_file, "%s%s", strbuf->pStr, pLF );
3634
0
        }
3635
0
        else
3636
0
        {
3637
0
            if (io->bPlainTextTags == 1)
3638
0
            {
3639
0
                inchi_ios_print_nodisplay( out_file, "/" ); /* stereo-abs-inv */
3640
0
            }
3641
0
        }
3642
3643
        /* stereo type */
3644
3645
        /* s-layer */
3646
0
        if ((io->nSegmAction = INChI_SegmentAction( io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE] )))
3647
0
        {
3648
0
            const char *p_stereo = io->bRelativeStereo[io->iCurTautMode] ? x_rel :
3649
0
                io->bRacemicStereo[io->iCurTautMode] ? x_rac : x_abs;
3650
3651
0
            szGetTag( IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_TYPS, io->szTag2, &io->bAlways, 1 );
3652
0
            inchi_strbuf_reset( strbuf ); io->tot_len = 0;
3653
3654
0
            io->tot_len = 0;
3655
0
            if (INCHI_SEGM_FILL == io->nSegmAction)
3656
0
            {
3657
0
                if (orig_inp_data->v3000->n_steabs > 0 ||
3658
0
                    orig_inp_data->v3000->n_sterel > 0 ||
3659
0
                    orig_inp_data->v3000->n_sterac > 0) {
3660
0
                    io->tot_len += MakeSlayerString(
3661
0
                        orig_inp_data,
3662
0
                        io->pINChISort,
3663
0
                        strbuf,
3664
0
                        io->bOutType,
3665
0
                        io->num_components,
3666
0
                        io->TAUT_MODE,
3667
0
                        &io->bOverflow
3668
0
                    );
3669
0
                } else {
3670
0
                    ( io->tot_len ) += MakeDelim( p_stereo, strbuf, &io->bOverflow );
3671
0
                }
3672
3673
0
                io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3674
0
            }
3675
0
            if (str_LineEnd( io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags ))
3676
0
            {
3677
0
                return 1;
3678
0
            }
3679
0
            inchi_ios_print_nodisplay( out_file, "%s%s", strbuf->pStr, pLF );
3680
0
        }
3681
0
        if (io->bPlainTextTags == 1)
3682
0
        {
3683
0
            inchi_ios_print_nodisplay( out_file, "/" );  /* no abs, inv or racemic stereo */
3684
0
        }
3685
0
    }
3686
0
    else
3687
0
    {
3688
0
        if (io->bPlainTextTags == 1)
3689
0
        {
3690
0
            inchi_ios_print_nodisplay( out_file, "////" ); /* sp2, sp3, abs-inv, stereo.type */
3691
0
        }
3692
0
    }
3693
3694
0
    return 0;
3695
0
}
3696
3697
/****************************************************************************
3698
Output InChI: isotopic layer and sublayers
3699
****************************************************************************/
3700
int OutputINCHI_IsotopicLayer( CANON_GLOBALS    *pCG,
3701
                               INCHI_IOSTREAM   *out_file,
3702
                               INCHI_IOS_STRING *strbuf,
3703
                               int              *INCHI_basic_or_INCHI_reconnected,
3704
                               INCHI_OUT_CTL    *io,
3705
                               char             *pLF,
3706
                               char             *pTAB )
3707
2.28k
{
3708
3709
2.28k
    if (INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_i_IATOMS]))
3710
1.72k
    {
3711
        /*  isotopic #1:  composition -- atoms -- do not output in xml if empty */
3712
1.72k
        szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_ISOT | io->bFhTag, io->szTag1, &io->bAlways, 1);
3713
        /* isotopic atoms without mobile H.
3714
         * Fixed 2004-06-15: always output if not bXml. Note:
3715
         * Previous condition if( bHasIsotopicAtoms[io->iCurTautMode] || bIsotopic && !bXml)
3716
         * did not optput /i in case of only mobile isotopic H
3717
         */
3718
1.72k
        if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_i_IATOMS]))) /* djb-rwth: addressing LLVM warning */
3719
1.72k
        {
3720
1.72k
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_ATMS, io->szTag2, &io->bAlways, 1);
3721
1.72k
            inchi_strbuf_reset(strbuf);
3722
1.72k
            io->tot_len = 0;
3723
            /*if ( bHasIsotopicAtoms[io->iCurTautMode] )*/
3724
1.72k
            if (INCHI_SEGM_FILL == io->nSegmAction)
3725
1.69k
            {
3726
1.69k
                io->tot_len2 = str_IsoAtoms(io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3727
1.69k
                                            io->bOutType, io->TAUT_MODE, io->num_components, io->bAbcNumbers,
3728
1.69k
                                            io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
3729
1.69k
                io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3730
1.69k
            }
3731
28
            else
3732
28
            {
3733
28
                io->tot_len2 = io->tot_len;
3734
28
            }
3735
3736
1.72k
            io->tot_len = io->tot_len2;
3737
1.72k
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3738
0
            {
3739
0
                return 1;
3740
0
            }
3741
1.72k
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3742
1.72k
        }
3743
3744
        /*  isotopic #1a:  composition -- exchangeable isotopic H (mobile H only) */
3745
        /*if ( !io->bSecondNonTautPass && bHasIsoH )*/
3746
1.72k
        if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_h_H_ATOMS]))) /* djb-rwth: addressing LLVM warning */
3747
278
        {
3748
278
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_XCGA, io->szTag2, &io->bAlways, 1);
3749
278
            inchi_strbuf_reset(strbuf);
3750
278
            io->tot_len = 0;
3751
278
            (io->tot_len) += MakeIsoHString(io->num_iso_H, strbuf, io->TAUT_MODE, &io->bOverflow);
3752
278
            io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3753
278
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3754
0
            {
3755
0
                return 2;
3756
0
            }
3757
278
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3758
278
        }
3759
3760
        /***************************************************
3761
         *
3762
         *       Isotopic stereo
3763
         *
3764
         ***************************************************/
3765
3766
        /*if ( bIsotopicStereo[io->iCurTautMode] )*/
3767
1.72k
        if (INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS]) ||
3768
1.72k
            INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS]) ||
3769
1.72k
            INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV]) ||
3770
1.72k
            INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE]))
3771
0
        {
3772
            /*  stereo */
3773
0
            szGetTag(IdentLbl, io->nTag, io->bTag2 = io->bTag1 | IL_STER, io->szTag2, &io->bAlways, 1);
3774
3775
            /************************
3776
              isotopic #2:  sp2
3777
             ************************/
3778
            /*if ( bIsotopicStereoSp2[io->iCurTautMode]  )*/
3779
0
            if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_b_SBONDS]))) /* djb-rwth: addressing LLVM warning */
3780
0
            {
3781
0
                szGetTag(IdentLbl, io->nTag, io->bTag3 = io->bTag2 | IL_DBND, io->szTag3, &io->bAlways, 1);
3782
0
                inchi_strbuf_reset(strbuf);
3783
0
                io->tot_len = 0;
3784
0
                if (INCHI_SEGM_FILL == io->nSegmAction)
3785
0
                {
3786
0
                    io->tot_len = str_IsoSp2(io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3787
0
                                             io->bOutType, io->TAUT_MODE, io->num_components,
3788
0
                                             io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
3789
0
                    io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3790
0
                }
3791
0
                if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3792
0
                {
3793
0
                    return 3;
3794
0
                }
3795
0
                inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3796
0
            }
3797
0
            else
3798
0
            {
3799
0
                if (io->bPlainTextTags == 1)
3800
0
                    inchi_ios_print_nodisplay(out_file, "/"); /* iso sp2 */
3801
0
            }
3802
3803
            /************************
3804
              isotopic #3:  sp3
3805
             ************************/
3806
            /*if ( bIsotopicStereoSp3[io->iCurTautMode]  )*/
3807
0
            if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_t_SATOMS]))) /* djb-rwth: addressing LLVM warning */
3808
0
            {
3809
0
                io->bRelRac = io->bIsotopicRelativeStereo[io->iCurTautMode] || io->bIsotopicRacemicStereo[io->iCurTautMode];
3810
3811
0
                szGetTag(IdentLbl, io->nTag, io->bTag3 = io->bTag2 | IL_SP3S, io->szTag3, &io->bAlways, 1);
3812
0
                inchi_strbuf_reset(strbuf);
3813
0
                io->tot_len = 0;
3814
0
                if (INCHI_SEGM_FILL == io->nSegmAction)
3815
0
                {
3816
0
                    io->tot_len = str_IsoSp3(io->pINChISort, io->pINChISort2, strbuf, &io->bOverflow,
3817
0
                                             io->bOutType, io->TAUT_MODE, io->num_components, io->bRelRac,
3818
0
                                             io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
3819
0
                    io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3820
0
                }
3821
0
                if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3822
0
                {
3823
0
                    return 5;
3824
0
                }
3825
0
                inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3826
0
            }
3827
0
            else
3828
0
            {
3829
0
                if (io->bPlainTextTags == 1)
3830
0
                {
3831
0
                    inchi_ios_print_nodisplay(out_file, "/"); /* iso-sp3 */
3832
0
                }
3833
0
            }
3834
3835
            /* isotopic #4: abs inverted */
3836
0
            if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_m_SP3INV]))) /* djb-rwth: addressing LLVM warning */
3837
0
            {
3838
0
                szGetTag(IdentLbl, io->nTag, io->bTag3 = io->bTag2 | IL_INVS, io->szTag3, &io->bAlways, 1);
3839
0
                inchi_strbuf_reset(strbuf);
3840
0
                io->tot_len = 0;
3841
0
                if (INCHI_SEGM_FILL == io->nSegmAction)
3842
0
                {
3843
0
                    io->tot_len = str_IsoStereoAbsInv(io->pINChISort, strbuf,
3844
0
                                                      &io->bOverflow, io->bOutType, io->num_components);
3845
0
                    io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3846
0
                }
3847
0
                if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3848
0
                {
3849
0
                    return 5;
3850
0
                }
3851
0
                inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3852
0
            }
3853
0
            else
3854
0
            {
3855
0
                if (io->bPlainTextTags == 1)
3856
0
                {
3857
0
                    inchi_ios_print_nodisplay(out_file, "/");
3858
0
                }
3859
0
            }
3860
3861
            /* isotopic #5: stereo type. Do not output if it has already been output in non-iso */
3862
0
            if ((io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_s_STYPE]))) /* djb-rwth: addressing LLVM warning */
3863
0
            {
3864
0
                const char *p_stereo = io->bIsotopicRelativeStereo[io->iCurTautMode] ? x_rel : io->bIsotopicRacemicStereo[io->iCurTautMode] ? x_rac
3865
0
                                                                                                                                            : x_abs;
3866
0
                szGetTag(IdentLbl, io->nTag, io->bTag3 = io->bTag2 | IL_TYPS, io->szTag3, &io->bAlways, 1);
3867
0
                inchi_strbuf_reset(strbuf);
3868
0
                io->tot_len = 0;
3869
0
                if (INCHI_SEGM_FILL == io->nSegmAction)
3870
0
                {
3871
0
                    io->tot_len += MakeDelim(p_stereo, strbuf, &io->bOverflow);
3872
0
                    io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3873
0
                }
3874
0
                if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
3875
0
                {
3876
0
                    return 6;
3877
0
                }
3878
0
                inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3879
0
            }
3880
0
            if (io->bPlainTextTags == 1)
3881
0
            {
3882
0
                inchi_ios_print_nodisplay(out_file, "/"); /* no abs, inv or racemic stereo */
3883
0
            }
3884
0
        }
3885
1.72k
        else
3886
1.72k
        {
3887
            /* no isotopic stereo */
3888
1.72k
            if (io->bPlainTextTags == 1)
3889
0
            {
3890
0
                inchi_ios_print_nodisplay(out_file, "////"); /* sp3, sp2, abs-inv, stereo.type */
3891
0
            }
3892
1.72k
        }
3893
1.72k
    }
3894
561
    else
3895
561
    {
3896
561
        if (io->bPlainTextTags == 1)
3897
0
        {
3898
0
            inchi_ios_print_nodisplay(out_file, "///"); /* isotopic composition, sp2, sp3 */
3899
0
        }
3900
561
        if (io->bPlainTextTags == 1)
3901
0
        {
3902
0
            inchi_ios_print_nodisplay(out_file, "//"); /* inv or racemic stereo */
3903
0
        }
3904
561
    }
3905
3906
2.28k
#if (CANON_FIXH_TRANS == 1)
3907
2.28k
    if (io->bOutType == OUT_NONTAUT && io->bOutputType == OUT_TN && io->bSecondNonTautPass &&
3908
0
        INCHI_SEGM_FILL == INChI_SegmentAction(io->sDifSegs[DIFL_F][DIFS_o_TRANSP]))
3909
0
    {
3910
        /* find and print non-tautomeric components transposition, if non-trivial */
3911
0
        AT_NUMB *nTrans_n, *nTrans_s;
3912
3913
0
        if (0 < bin_AuxTautTrans(io->pINChISort, io->pINChISort2, &nTrans_n, &nTrans_s, io->bOutType, io->num_components))
3914
0
        {
3915
            /* a non-trivial transposition does exist; output start tag */
3916
0
            szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_TRNS | io->bFhTag, io->szTag1, &io->bAlways, 1);
3917
0
            inchi_strbuf_reset(strbuf);
3918
0
            io->tot_len = 0;
3919
            /* print the transposition, cycle after cycle */
3920
0
            io->tot_len = str_AuxTautTrans(pCG, nTrans_n, nTrans_s, strbuf,
3921
0
                                           &io->bOverflow, io->TAUT_MODE, io->num_components);
3922
0
            io->bNonTautIsoIdentifierNotEmpty += io->bSecondNonTautPass;
3923
0
            if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
3924
0
            {
3925
0
                return 7;
3926
0
            }
3927
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
3928
            /* detected transposition */
3929
0
            (*io->pSortPrintINChIFlags) |=
3930
0
                (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_TRANSPOS_BAS : FLAG_SORT_PRINT_TRANSPOS_REC;
3931
0
        }
3932
0
        else
3933
0
        {
3934
0
            if (io->bPlainTextTags == 1)
3935
0
            {
3936
0
                inchi_ios_print_nodisplay(out_file, "/");
3937
0
            }
3938
0
        }
3939
0
    }
3940
2.28k
#endif
3941
3942
2.28k
    return 0;
3943
2.28k
}
3944
3945
/****************************************************************************
3946
Output InChI: FixedH layer and related sublayers
3947
****************************************************************************/
3948
int OutputINCHI_FixedHLayerWithSublayers(CANON_GLOBALS *pCG,
3949
                                         INCHI_IOSTREAM *out_file,
3950
                                         INCHI_IOS_STRING *strbuf,
3951
                                         int *INCHI_basic_or_INCHI_reconnected,
3952
                                         INCHI_OUT_CTL *io,
3953
                                         char *pLF,
3954
                                         char *pTAB,
3955
                                         int *then_goto_repeat)
3956
2.28k
{
3957
3958
2.28k
    *then_goto_repeat = 0;
3959
3960
2.28k
    if (io->bOutType == OUT_TN &&
3961
2.28k
        !(io->bSecondNonTautPass) &&
3962
2.28k
        io->bNonTautIsIdenticalToTaut &&
3963
2.00k
        io->bTautomeric &&
3964
629
        io->bNonTautomeric)
3965
0
    {
3966
        /* Fixed-H layer is empty in the Identifier */
3967
0
        (*io->pSortPrintINChIFlags) |=
3968
0
            (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_NFIX_H_BAS : FLAG_SORT_PRINT_NO_NFIX_H_REC;
3969
0
        (*io->pSortPrintINChIFlags) |=
3970
0
            (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_IFIX_H_BAS : FLAG_SORT_PRINT_NO_IFIX_H_REC;
3971
0
    }
3972
3973
2.28k
    if (io->bOutType == OUT_TN &&
3974
2.28k
        !io->bNonTautIsIdenticalToTaut && /* added 2004-10-04 Fix16 */
3975
#ifdef OLD_ITEM_DISCOVERY
3976
        io->bTautomeric &&
3977
        io->bNonTautomeric &&
3978
#endif
3979
278
        INChI_SegmentAction(io->sDifSegs[DIFL_F][DIFS_f_FORMULA])
3980
        /* special case: removed isolated H(+): */
3981
        /* || io->iCurTautMode == TAUT_YES && num_comp[TAUT_YES] < num_comp[TAUT_NON] &&
3982
            0 < num_comp[TAUT_NON]*/
3983
2.28k
    )
3984
3985
0
    {
3986
        /* add the second (non-tautomeric) output */
3987
0
        io->bOutType = OUT_NONTAUT; /* pick up only non-tautomeric representation of tautomeric */
3988
0
        io->iCurTautMode = TAUT_NON;
3989
0
        io->pINChISort = io->pINChISortTautAndNonTaut[TAUT_NON];
3990
0
        io->bSecondNonTautPass = 1;
3991
0
        io->nCurINChISegment = DIFL_F;
3992
0
        io->num_components = io->num_comp[io->iCurTautMode]; /* number of components could change due to removal of isolated H(+) from tautomeric */
3993
0
        io->bFhTag = IL_FIXH;
3994
0
        szGetTag(IdentLbl, io->nTag, io->bTag1 = io->bFhTag, io->szTag1, &io->bAlways, 1);
3995
        /***** constitution non-taut: dot-disconnected Hill formulas: <formula> -- only if different */
3996
0
        szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_FMLF | io->bFhTag, io->szTag1, &io->bAlways, 1);
3997
0
        inchi_strbuf_reset(strbuf);
3998
0
        io->tot_len = 0;
3999
0
        io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_f_FORMULA]);
4000
0
        if (INCHI_SEGM_FILL == io->nSegmAction)
4001
0
        {
4002
0
            io->tot_len2 = str_HillFormula2(io->pINChISort, io->pINChISort2,
4003
0
                                            strbuf, &io->bOverflow, io->bOutType,
4004
0
                                            io->num_components, io->bUseMulipliers);
4005
0
            if (io->n_pzz > 0 && io->n_zy > 0)
4006
0
            {
4007
0
                MergeZzInHillFormula(strbuf);
4008
0
            }
4009
0
            io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
4010
0
        }
4011
0
        else
4012
0
        {
4013
0
            io->tot_len2 = io->tot_len;
4014
0
        }
4015
0
        io->tot_len = io->tot_len2;
4016
0
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
4017
0
        {
4018
0
            return 1;
4019
0
        }
4020
0
        inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
4021
4022
0
        io->nSegmAction = INChI_SegmentAction(io->sDifSegs[io->nCurINChISegment][DIFS_h_H_ATOMS]);
4023
4024
0
        if (INCHI_SEGM_FILL == io->nSegmAction)
4025
0
        {
4026
0
            szGetTag(IdentLbl, io->nTag, io->bTag1 = IL_HFIX | io->bFhTag, io->szTag1, &io->bAlways, 1);
4027
0
            inchi_strbuf_reset(strbuf);
4028
0
            io->tot_len = 0; /* open H-fixed */
4029
            /* output the second non-tautomeric item: fixed H -- do not output in xml if empty */
4030
0
            io->tot_len2 = str_FixedH_atoms(io->pINChISort, strbuf,
4031
0
                                            &io->bOverflow, io->bOutType, io->ATOM_MODE,
4032
0
                                            io->num_components, io->bUseMulipliers);
4033
0
            io->tot_len = io->tot_len2;
4034
0
            if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -io->nSegmAction, io->bPlainTextTags))
4035
0
            {
4036
0
                return 2;
4037
0
            }
4038
0
            inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
4039
0
            io->bNonTautNonIsoIdentifierNotEmpty += io->bSecondNonTautPass;
4040
0
        }
4041
0
        *then_goto_repeat = 1;
4042
0
        return 0;
4043
0
    }
4044
4045
2.28k
    else
4046
2.28k
    {
4047
2.28k
        if (io->bOutType == OUT_NONTAUT && io->bOutputType == OUT_TN && io->bSecondNonTautPass /* && io->bTautomeric && io->bNonTautomeric*/)
4048
0
        {
4049
            /* the second (non-taut) output has been done; restore variables */
4050
0
            io->bOutType = OUT_TN;
4051
0
            io->iCurTautMode = TAUT_YES;
4052
0
            io->pINChISort = io->pINChISortTautAndNonTaut[TAUT_YES];
4053
0
            io->bSecondNonTautPass = 0;
4054
0
            io->num_components = io->num_comp[io->iCurTautMode];
4055
0
            if (!io->bNonTautNonIsoIdentifierNotEmpty)
4056
0
            {
4057
                /* Fixed-H layer is empty in the Identifier */
4058
0
                (*io->pSortPrintINChIFlags) |= (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_NFIX_H_BAS : FLAG_SORT_PRINT_NO_NFIX_H_REC;
4059
0
            }
4060
0
            if (!io->bNonTautIsoIdentifierNotEmpty)
4061
0
            {
4062
                /* Fixed-H layer is empty in the Identifier */
4063
0
                (*io->pSortPrintINChIFlags) |= (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_IFIX_H_BAS : FLAG_SORT_PRINT_NO_IFIX_H_REC;
4064
0
            }
4065
0
            io->bFhTag = 0;
4066
0
        }
4067
2.28k
    }
4068
4069
2.28k
    return 0;
4070
2.28k
}
4071
4072
/****************************************************************************
4073
Output InChI: polymer layer
4074
****************************************************************************/
4075
static int OutputINCHI_PolymerLayer(CANON_GLOBALS *pCG,
4076
                                    INCHI_IOSTREAM *out_file,
4077
                                    INCHI_IOS_STRING *strbuf,
4078
                                    int *INCHI_basic_or_INCHI_reconnected,
4079
                                    ORIG_ATOM_DATA *orig_inp_data,
4080
                                    ORIG_STRUCT *pOrigStruct,
4081
                                    INCHI_OUT_CTL *io,
4082
                                    char *pLF,
4083
                                    char *pTAB)
4084
2.28k
{
4085
2.28k
    int i, err = 0;
4086
2.28k
    int nunits2 = 0;
4087
2.28k
    int n_used_stars = 0;
4088
2.28k
    int *cano_nums = NULL, *compnt_nums = NULL, *unum = NULL, *old_stars = NULL;
4089
2.28k
    OAD_PolymerUnit *u = NULL;
4090
2.28k
    OAD_PolymerUnit **units2 = NULL;
4091
2.28k
    OAD_Polymer *p = NULL;
4092
2.28k
    OAD_AtProps *aprops = NULL;
4093
2.28k
    int nat, num_inp_bonds;
4094
2.28k
    inp_ATOM *at = NULL;
4095
2.28k
    int is_inchi2inchi = 0;
4096
4097
2.28k
    if (!orig_inp_data)
4098
0
    {
4099
0
        goto exit_function;
4100
0
    }
4101
4102
2.28k
    at = orig_inp_data->at;
4103
2.28k
    nat = orig_inp_data->num_inp_atoms;
4104
2.28k
    num_inp_bonds = orig_inp_data->num_inp_bonds;
4105
4106
4107
2.28k
    if (pOrigStruct && !pOrigStruct->polymer)
4108
2.28k
    {
4109
2.28k
        return 0;
4110
2.28k
    }
4111
4112
0
    if (pOrigStruct)
4113
0
    {
4114
0
        p = pOrigStruct->polymer;
4115
0
        is_inchi2inchi = !pOrigStruct->szAtoms && !pOrigStruct->szBonds && !pOrigStruct->szCoord;
4116
4117
0
        if (is_inchi2inchi)
4118
0
        {
4119
0
            err = NOT_YET_I2I_FOR_POLYMERS;
4120
0
            goto exit_function;
4121
0
        }
4122
4123
        /*OAD_Polymer_DebugTrace( p );*/
4124
4125
        /* Get canonical numbers and numbers-of-components for each original atom */
4126
0
        cano_nums = (int *)inchi_calloc((long long)pOrigStruct->num_atoms + 1, sizeof(int)); /* djb-rwth: cast operator added */
4127
0
        if (!cano_nums)
4128
0
        {
4129
0
            err = 1;
4130
0
            goto exit_function;
4131
0
        }
4132
0
        compnt_nums = (int *)inchi_calloc((long long)pOrigStruct->num_atoms + 1, sizeof(int)); /* djb-rwth: cast operator added */
4133
0
        if (!compnt_nums)
4134
0
        {
4135
0
            err = 2;
4136
0
            goto exit_function;
4137
0
        }
4138
0
        err = InternallyGetCanoNumsAndComponentNums(pCG,
4139
0
                                                    strbuf,
4140
0
                                                    io,
4141
0
                                                    pOrigStruct->num_atoms,
4142
0
                                                    cano_nums,
4143
0
                                                    compnt_nums);
4144
0
        if (err != 0)
4145
0
        {
4146
0
            err = 3;
4147
0
            goto exit_function;
4148
0
        }
4149
4150
        /* Set atom properties for sorting */
4151
0
        aprops = (OAD_AtProps *)inchi_calloc((long long)nat + 1, sizeof(OAD_AtProps)); /* djb-rwth: cast operator added */
4152
        /* nat + 1: add extra element for possibe 1-based indexing */
4153
0
        if (!aprops)
4154
0
        {
4155
            /* djb-rwth: avoiding memory leak */
4156
0
            if (cano_nums)
4157
0
            {
4158
0
                inchi_free(cano_nums);
4159
0
            }
4160
0
            if (compnt_nums)
4161
0
            {
4162
0
                inchi_free(compnt_nums);
4163
0
            }
4164
0
            if (aprops)
4165
0
            {
4166
0
                inchi_free(aprops);
4167
0
            }
4168
0
            return 0;
4169
0
        }
4170
4171
        /* Note that aprops[] is in orig_atoms domain (0-based) and      */
4172
        /* u (from units) are in cano_nums domain (1-based)              */
4173
        /* Supply non-NULL cano_nums to adjust the domains (base will be adjusted at place) */
4174
0
        OAD_Polymer_SetAtProps(p, at, nat, &num_inp_bonds, aprops, cano_nums);
4175
4176
        /* Make a working copy of polymer units data: units2 is a copy        */
4177
        /* of original polymer units (p->units) with atomic numbers changed    */
4178
        /* to curr canonical ones; atoms in alists sorted; atoms in blists    */
4179
        /* and blists themselves sorted                                     */
4180
0
        units2 = (OAD_PolymerUnit **)inchi_calloc(p->n, sizeof(OAD_PolymerUnit *));
4181
4182
0
        if (NULL == units2)
4183
0
        {
4184
0
            err = 3;
4185
0
            goto exit_function;
4186
0
        }
4187
0
        memset(units2, 0, sizeof(*units2)); /* djb-rwth: memset_s C11/Annex K variant? */
4188
4189
0
        old_stars = (int *)inchi_calloc(pOrigStruct->polymer->n_pzz, sizeof(int));
4190
0
        if (NULL == old_stars)
4191
0
        {
4192
0
            err = 3;
4193
0
            goto exit_function;
4194
0
        }
4195
0
        for (i = 0; i < pOrigStruct->polymer->n_pzz; i++)
4196
0
        {
4197
0
            old_stars[i] = pOrigStruct->polymer->pzz[i];
4198
0
        }
4199
4200
0
        for (i = 0; i < p->n; i++)
4201
0
        {
4202
0
            units2[i] = OAD_PolymerUnit_CreateCopy(p->units[i]);
4203
0
            if (NULL == units2[i]) /* djb-rwth: unresolved issue -- revision required? -- units2 properly allocated, and loop index well defined */
4204
0
            {
4205
0
                err = 4;
4206
0
                goto exit_function;
4207
0
            }
4208
0
            nunits2 = i + 1;
4209
0
        }
4210
4211
        /* unum contains numbers of units (0..p->n) as they go  */
4212
        /* when sorted by alist's in lexicographic order        */
4213
0
        unum = (int *)inchi_calloc(p->n, sizeof(int));
4214
0
        if (NULL == unum)
4215
0
        {
4216
0
            err = 4;
4217
0
            goto exit_function;
4218
0
        }
4219
4220
0
        err = OAD_Polymer_PrepareWorkingSet(p, cano_nums, compnt_nums, units2, unum);
4221
4222
0
        if (err != 0)
4223
0
        {
4224
0
            err = 5;
4225
0
            goto exit_function;
4226
0
        }
4227
4228
        /* Prepare polymer substring */
4229
4230
        /* Mark layer beginning */
4231
0
        inchi_strbuf_printf(strbuf, "%s", "/z");
4232
4233
        /* Print polymer units data */
4234
0
        n_used_stars = 0;
4235
0
        for (i = 0; i < p->n; i++)
4236
0
        {
4237
            /* For each unit u ... */
4238
0
            u = units2[unum[i]];
4239
            /* djb-rwth: addressing coverity ID #499574 -- all NULL checks already done above */
4240
0
            err = OutputINCHI_PolymerLayer_SingleUnit(u,
4241
0
                                                      io->bPolymers,
4242
0
                                                      pOrigStruct->polymer->n_pzz,
4243
0
                                                      &n_used_stars, aprops,
4244
0
                                                      cano_nums,
4245
0
                                                      orig_inp_data,
4246
0
                                                      pOrigStruct, strbuf);
4247
0
            if (err)
4248
0
            {
4249
0
                goto exit_function;
4250
0
            }
4251
0
            if (i < p->n - 1)
4252
0
            {
4253
0
                inchi_strbuf_printf(strbuf, ";");
4254
0
            }
4255
0
        }
4256
0
        inchi_ios_print_nodisplay(out_file, "%s%s", strbuf->pStr, pLF);
4257
4258
0
        LOG_NO_ARGS("\n******************* (L4184:ichiprt1.c) ********************\n");
4259
0
        LOG_MULT_ARGS("Polymer Layer start: %s\n", strbuf->pStr);
4260
0
        LOG_NO_ARGS("\n***********************************************************\n");
4261
4262
0
    exit_function:
4263
0
        if (cano_nums)
4264
0
        {
4265
0
            inchi_free(cano_nums);
4266
0
        }
4267
0
        if (compnt_nums)
4268
0
        {
4269
0
            inchi_free(compnt_nums);
4270
0
        }
4271
0
        if (aprops)
4272
0
        {
4273
0
            inchi_free(aprops);
4274
0
        }
4275
0
        if (unum)
4276
0
        {
4277
0
            inchi_free(unum);
4278
0
        }
4279
0
        if (units2)
4280
0
        {
4281
0
            for (i = 0; i < nunits2; i++)
4282
0
            {
4283
0
                OAD_PolymerUnit_Free(units2[i]);
4284
0
            }
4285
0
            inchi_free(units2);
4286
0
        }
4287
0
        if (old_stars)
4288
0
        {
4289
0
            for (i = 0; i < pOrigStruct->polymer->n_pzz; i++)
4290
0
                pOrigStruct->polymer->pzz[i] = old_stars[i];
4291
0
            inchi_free(old_stars);
4292
0
        }
4293
0
    }
4294
0
    return err;
4295
0
}
4296
4297
/****************************************************************************
4298
Output InChI: polymer layer, single CRU data
4299
****************************************************************************/
4300
static int OutputINCHI_PolymerLayer_SingleUnit(OAD_PolymerUnit *u,
4301
                                               int bPolymers,
4302
                                               int total_star_atoms,
4303
                                               int *n_used_stars,
4304
                                               OAD_AtProps *aprops,
4305
                                               int *cano_nums,
4306
                                               ORIG_ATOM_DATA *orig_inp_data,
4307
                                               ORIG_STRUCT *pOrigStruct,
4308
                                               INCHI_IOS_STRING *strbuf)
4309
0
{
4310
0
    int j, k, tmp, a1 = 0, a2 = 0, a3 = 0, a4 = 0, b, curr_star_num;
4311
0
    int err = 0;
4312
0
    OAD_Polymer *p = orig_inp_data->polymer;
4313
0
    inp_ATOM *at = orig_inp_data->at;
4314
4315
    /* print unit type and subtype */
4316
0
    inchi_strbuf_printf(strbuf, "%-d%-d%-d-", u->type, u->subtype, u->conn);
4317
4318
    /* print unit atoms */
4319
0
    print_sequence_of_nums_compressing_ranges(u->na, u->alist, strbuf);
4320
4321
    /* Print the crossing bonds or frame-shiftable pattern */
4322
0
    if (u->nb > 2)
4323
0
    {
4324
        /* not supported yet, too many bonds in SBL */
4325
0
        err = 12;
4326
0
        goto exit_function;
4327
0
    }
4328
4329
    /* Print crossing bonds "(cap1-partner1,cap2-partner2)"    */
4330
0
    if (u->nb == 2 && (!u->cyclizable || !u->cyclized))
4331
0
    {
4332
0
        int swap = 0;
4333
0
        a1 = u->blist[0];
4334
0
        a2 = u->blist[1];
4335
0
        a3 = u->blist[2];
4336
0
        a4 = u->blist[3];
4337
4338
0
        if (is_in_the_ilist( u->alist, a1, u->na ))
4339
0
        {
4340
0
            tmp = a2;
4341
0
            a2 = a1;
4342
0
            a1 = tmp;
4343
0
        }
4344
0
        if (is_in_the_ilist(u->alist, a3, u->na))
4345
0
        {
4346
0
            tmp = a4;
4347
0
            a4 = a3;
4348
0
            a3 = tmp;
4349
0
        }
4350
4351
        /* Always print first the crossing bond pointing to more senior CRU end ("head")    */
4352
0
        if (bPolymers == POLYMERS_LEGACY)
4353
0
        {
4354
            /* old, v. 1.05 */
4355
            /* The first printed is the crossing bond with higher canonical number of the cap */
4356
0
            swap = a3 < a1;
4357
0
        }
4358
0
        else
4359
0
        {
4360
            /* new in v. 1.06 */
4361
            /* The first printed is the crossing bond pointing to more senior CRU end ("head")    */
4362
0
            swap = (OAD_Polymer_IsFirstAtomRankLower(a2, a4, aprops) == 1);
4363
0
        }
4364
4365
0
        if (swap)
4366
0
        {
4367
0
            inchi_strbuf_printf(strbuf, "(%-d-%-d,%-d-%-d)", a3, a4, a1, a2);
4368
0
        }
4369
0
        else
4370
0
        {
4371
0
            inchi_strbuf_printf(strbuf, "(%-d-%-d,%-d-%-d)", a1, a2, a3, a4);
4372
0
        }
4373
0
    }
4374
4375
0
    else if (u->nb <= 2 && (u->cyclizable || u->nbkbonds > 0))
4376
0
    {
4377
        /* Print frame-shiftable pattern "cap1,cap2-(b1a1,b1a2, b2a1,b2a2, ... )"     */
4378
        /* where b1, b2, ... are CRU bonds potentially invilved in frame shift          */
4379
4380
        /*  Get actual star atoms numbers from all-units pool                           */
4381
        /*  NB: ordered according to already established in units2/unum                 */
4382
0
        if (u->cap1 > 0 || u->cap2 > 0)
4383
0
        {
4384
0
            int n_expl_H = 0, pos = 0;
4385
0
            char *sza = pOrigStruct->szAtoms;
4386
0
            while (sza[pos])
4387
0
            {
4388
0
                if (sza[pos] == 'H')
4389
0
                {
4390
0
                    if (isupper(UCINT sza[pos + 1]) || !sza[pos + 1]) /* if ( next_c is Uppercase or NUL ) */
4391
0
                    {
4392
0
                        n_expl_H++;
4393
0
                    }
4394
0
                    if (!sza[pos + 1])
4395
0
                    {
4396
0
                        break;
4397
0
                    }
4398
0
                }
4399
0
                pos++;
4400
0
            }
4401
4402
0
            if (u->cap1 > 0)
4403
0
            {
4404
0
                curr_star_num = pOrigStruct->num_atoms - n_expl_H - total_star_atoms + *n_used_stars + 1;
4405
0
                if (curr_star_num > pOrigStruct->num_atoms)
4406
0
                {
4407
0
                    err = 11;
4408
0
                    goto exit_function;
4409
0
                }
4410
0
                a1 = curr_star_num;
4411
0
                (*n_used_stars)++;
4412
0
            }
4413
0
            if (u->cap2 > 0)
4414
0
            {
4415
0
                curr_star_num = pOrigStruct->num_atoms - n_expl_H - total_star_atoms + *n_used_stars + 1;
4416
0
                if (curr_star_num > pOrigStruct->num_atoms)
4417
0
                {
4418
0
                    err = 11;
4419
0
                    goto exit_function;
4420
0
                }
4421
0
                a2 = curr_star_num;
4422
0
                (*n_used_stars)++;
4423
0
            }
4424
0
        }
4425
        /* a1 and a2 are number of star atoms associated (but actually  */
4426
        /* disconnected at this moment ) with SRU head and tail atoms   */
4427
0
        inchi_strbuf_printf(strbuf, "(%-d,%-d-", a1, a2);
4428
4429
0
        if (u->cyclizable == CLOSING_SRU_DIRADICAL)
4430
0
        {
4431
0
            inchi_strbuf_printf(strbuf, "%-d)", u->end_atom1);
4432
0
        }
4433
0
        else if (u->cyclizable == CLOSING_SRU_HIGHER_ORDER_BOND)
4434
0
        {
4435
0
            a3 = u->end_atom1;
4436
0
            a4 = u->end_atom2;
4437
0
            inchi_sort_int_pair_ascending(&a3, &a4);
4438
            /* if ( a3 > a4 )   { tmp = a4; a4 = a3;  a3 = tmp;                  }*/
4439
0
            inchi_strbuf_printf(strbuf, "%-d.%-d)", a3, a4);
4440
0
        }
4441
0
        else if (u->cyclizable == CLOSING_SRU_RING)
4442
0
        {
4443
0
            if (u->nbkbonds == 0)
4444
0
            {
4445
                /* last resort */
4446
0
                a3 = u->end_atom1;
4447
0
                a4 = u->end_atom2;
4448
0
                inchi_sort_int_pair_ascending(&a3, &a4);
4449
                /* if ( a3 > a4 ) { tmp = a4; a4 = a3; a3 = tmp; } */
4450
0
                inchi_strbuf_printf(strbuf, "%-d,%-d)", a3, a4);
4451
0
            }
4452
0
            else
4453
0
            {
4454
                /* Sort all backbone bonds in min-at-number order */
4455
0
                for (b = 1; b < u->nbkbonds; b++)
4456
0
                {
4457
0
                    int *tmp_psbond = u->bkbonds[b];
4458
0
                    j = b - 1;
4459
0
                    while (j >= 0 && IsBondAtomNumsLesser(u->bkbonds[j], tmp_psbond) > 0)
4460
0
                    {
4461
0
                        u->bkbonds[j + 1] = u->bkbonds[j];
4462
0
                        j--;
4463
0
                    }
4464
0
                    u->bkbonds[j + 1] = tmp_psbond;
4465
0
                }
4466
4467
0
                if (p->treat == POLYMERS_MODERN || p->treat == POLYMERS_LEGACY_PLUS)
4468
0
                {
4469
                    /* was #if DO_POLYMER_FRAME_SHIFT_AT_STRUCT_TO_INCHI_CONVERSION==1 */
4470
                    /* was: OAD_PolymerUnit_ReorderPolymerFrameShiftLinks( u, orig_inp_data, aprops, cano_nums );
4471
                    OAD_Polymer_DebugTrace(p);*/
4472
4473
                    /*  Find senior link and move it to the beginning of list
4474
                        If necessary, swap atoms in link so that the first
4475
                        points to SRU head and the next to SRU tail
4476
4477
                        The net result of ordering is as follows:
4478
                        at1,at2,  at3,at4,  at5,at6, ...
4479
                        here
4480
                        at1, at2 is the most senior bond, and at1 is more senior than at2
4481
                        all other pairs at3,at4,  at5,at6, ... are sorted just in increasing
4482
                        order of first number in pair, then second one, e.g.: at3<at4; at3<=at5
4483
                        (and if at3==at5 then at4<at6)
4484
                    */
4485
4486
0
                    if (p->frame_shift_scheme != FSS_NONE && u->nbkbonds >= 1 && u->cap1 >= 1 && u->cap2 >= 1)
4487
0
                    {
4488
0
                        if (OAD_PolymerUnit_SetReopeningDetails(u, at))
4489
0
                        {
4490
                            /* Find senior backbone bond and move it to the beginning of list */
4491
0
                            {
4492
0
                                int senior_bond, bond0at1, bond0at2;
4493
0
                                OAD_PolymerUnit_SortBackboneBondsAndSetSeniors(u, at, aprops, &senior_bond);
4494
0
                                if (senior_bond)
4495
0
                                {
4496
0
                                    bond0at1 = u->bkbonds[0][0];
4497
0
                                    bond0at2 = u->bkbonds[0][1];
4498
0
                                    u->bkbonds[0][0] = u->bkbonds[senior_bond][0];
4499
0
                                    u->bkbonds[0][1] = u->bkbonds[senior_bond][1];
4500
0
                                    u->end_atom1 = u->bkbonds[0][0];
4501
0
                                    u->end_atom2 = u->bkbonds[0][1];
4502
0
                                    u->bkbonds[senior_bond][0] = bond0at1;
4503
0
                                    u->bkbonds[senior_bond][1] = bond0at2;
4504
0
                                }
4505
0
                            }
4506
0
                        }
4507
0
                    }
4508
                    /* p->really_do_frame_shift = 0; */
4509
0
                }
4510
4511
0
                for (k = 0; k < u->nbkbonds; k++)
4512
0
                {
4513
0
                    a3 = u->bkbonds[k][0];
4514
0
                    a4 = u->bkbonds[k][1];
4515
                    /*if ( a3 > a4 )    { tmp = a4; a4 = a3; a3 = tmp; }*/
4516
0
                    inchi_strbuf_printf(strbuf, "%-d,%-d%-c", a3, a4, k == u->nbkbonds - 1 ? ')' : ',');
4517
0
                }
4518
0
            }
4519
0
        }
4520
0
    }
4521
4522
0
exit_function:
4523
4524
0
    return err;
4525
0
}
4526
4527
/****************************************************************************
4528
Output AuxInfo: header and normalization type
4529
****************************************************************************/
4530
int OutputAUXINFO_HeaderAndNormalization_type(CANON_GLOBALS *pCG,
4531
                                              INCHI_IOSTREAM *out_file,
4532
                                              INCHI_IOS_STRING *strbuf,
4533
                                              int bINChIOutputOptions,
4534
                                              int *INCHI_basic_or_INCHI_reconnected,
4535
                                              int num_components2[],
4536
                                              INCHI_OUT_CTL *io,
4537
                                              char *pLF,
4538
                                              char *pTAB)
4539
2.28k
{
4540
    /* AuxInfo header  */
4541
2.28k
    if (*INCHI_basic_or_INCHI_reconnected == INCHI_BAS)
4542
2.28k
    {
4543
2.28k
        inchi_strbuf_printf(strbuf, "AuxInfo="); /* in wINChI window, separate INChI: from AuxInfo: with blank line */
4544
2.28k
        inchi_ios_print(out_file, "%s%s%s",
4545
                        /* blank line before AuxInfo in winchi window unless it is an annotation */
4546
2.28k
                        (bINChIOutputOptions & INCHI_OUT_WINCHI_WINDOW) ? "\n" : "",
4547
2.28k
                        strbuf->pStr, pLF);
4548
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_VERS, io->szTag1, &io->bAlways, 0);
4549
2.28k
        inchi_strbuf_reset(strbuf);
4550
2.28k
        io->tot_len = 0;
4551
2.28k
        inchi_strbuf_printf(strbuf, "%s", x_curr_ver);
4552
        /* avoid leading slash in plain output */
4553
2.28k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4554
0
        {
4555
0
            return 1;
4556
0
        }
4557
2.28k
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4558
2.28k
    }
4559
0
    else
4560
0
    {
4561
0
        if (*INCHI_basic_or_INCHI_reconnected == INCHI_REC)
4562
0
        {
4563
0
            szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_REC_, io->szTag1, &io->bAlways, 0);
4564
0
            inchi_ios_print(out_file, "%s%s", io->szTag1, pLF);
4565
0
        }
4566
0
    }
4567
4568
    /* AuxInfo normalization type */
4569
2.28k
    if (num_components2[0] || num_components2[1])
4570
2.28k
    {
4571
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_NORM, io->szTag1, &io->bAlways, 0);
4572
2.28k
        inchi_strbuf_reset(strbuf);
4573
2.28k
        io->tot_len = 0;
4574
2.28k
        inchi_strbuf_printf(strbuf, "%d", (io->bTautomeric && io->bTautomericOutputAllowed) ? io->bTautomeric : 0);
4575
2.28k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4576
0
        {
4577
0
            return 1;
4578
0
        }
4579
2.28k
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4580
2.28k
    }
4581
4582
2.28k
    return 0;
4583
2.28k
}
4584
4585
/****************************************************************************
4586
Output AuxInfo: original atom numbers and symmetry numbers (constit. equivalence /E: )
4587
****************************************************************************/
4588
int OutputAUXINFO_OriginalNumbersAndEquivalenceClasses(CANON_GLOBALS *pCG,
4589
                                                       INCHI_IOSTREAM *out_file,
4590
                                                       INCHI_IOS_STRING *strbuf,
4591
                                                       int num_components2[],
4592
                                                       INCHI_OUT_CTL *io,
4593
                                                       char *pLF,
4594
                                                       char *pTAB)
4595
2.28k
{
4596
    /* Original atom numbers in order of canonical numbers */
4597
2.28k
    if (num_components2[0] || num_components2[1])
4598
2.28k
    {
4599
2.28k
        szGetTag(AuxLbl, io->nTag,
4600
2.28k
                 io->bTag1 = (io->bSecondNonTautPass ? AL_FIXN : AL_ANBR) | io->bFhTag, io->szTag1, &io->bAlways, 0);
4601
2.28k
        inchi_strbuf_reset(strbuf);
4602
2.28k
        io->tot_len = 0;
4603
        /* Original numbering output */
4604
2.28k
        io->tot_len = str_AuxNumb(pCG, io->pINChISort, io->pINChISort2,
4605
2.28k
                                  strbuf, &io->bOverflow,
4606
2.28k
                                  io->bOutType, io->TAUT_MODE, io->num_components,
4607
2.28k
                                  io->bSecondNonTautPass, io->bOmitRepetitions);
4608
4609
2.28k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4610
0
        {
4611
0
            return 1;
4612
0
        }
4613
2.28k
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4614
2.28k
    }
4615
4616
    /*
4617
        Symmetry numbers (constit. equivalence)    /E:
4618
    */
4619
2.28k
    if (io->bAtomEqu[io->iCurTautMode])
4620
162
    {
4621
        /*  aux equ atoms */
4622
        /* 1. Compare to tautomeric equivalence (in case of second, non-taut, pass only) */
4623
        /* 2. Compare to the previous component if (1) failed to find equivalence */
4624
162
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_AEQU | io->bFhTag, io->szTag1, &io->bAlways, 0);
4625
162
        inchi_strbuf_reset(strbuf);
4626
162
        io->tot_len = 0;
4627
162
        io->tot_len = str_AuxEqu(io->pINChISort, io->pINChISort2,
4628
162
                                 strbuf, &io->bOverflow, io->bOutType, io->TAUT_MODE,
4629
162
                                 io->num_components, io->bSecondNonTautPass,
4630
162
                                 io->bOmitRepetitions, io->bUseMulipliers);
4631
4632
162
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4633
0
        {
4634
0
            return 1;
4635
0
        }
4636
162
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4637
162
    }
4638
2.12k
    else
4639
2.12k
    {
4640
2.12k
        if (io->bPlainTextTags == 1)
4641
0
        {
4642
0
            inchi_ios_print(out_file, "/");
4643
0
        }
4644
2.12k
    }
4645
4646
2.28k
    return 0;
4647
2.28k
}
4648
4649
/****************************************************************************
4650
Output AuxInfo: tautomeric groups equivalence
4651
****************************************************************************/
4652
int OutputAUXINFO_TautomericGroupsEquivalence(CANON_GLOBALS *pCG,
4653
                                              INCHI_IOSTREAM *out_file,
4654
                                              INCHI_IOS_STRING *strbuf,
4655
                                              INCHI_OUT_CTL *io)
4656
2.28k
{
4657
2.28k
    if (io->bTautomericOutputAllowed && io->bTautomeric && io->bTautEqu[io->iCurTautMode] && !io->bSecondNonTautPass)
4658
0
    {
4659
        /*-- Tautomeric groups constitutional equivalence */
4660
4661
        /*-- aux tgroup equ */
4662
0
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_GEQU | io->bFhTag, io->szTag1, &io->bAlways, 0);
4663
0
        inchi_strbuf_reset(strbuf);
4664
0
        io->tot_len = 0;
4665
0
        io->tot_len = str_AuxTgroupEqu(io->pINChISort,
4666
0
                                       strbuf, &io->bOverflow, io->bOutType, io->TAUT_MODE,
4667
0
                                       io->num_components, io->bUseMulipliers);
4668
0
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4669
0
        {
4670
0
            return 1;
4671
0
        }
4672
0
        inchi_ios_print(out_file, "%s", strbuf->pStr);
4673
0
    }
4674
2.28k
    else
4675
2.28k
    {
4676
2.28k
        if (io->bTautomericOutputAllowed && io->bTautomeric)
4677
907
        {
4678
907
            if (io->bPlainTextTags == 1)
4679
0
            {
4680
0
                inchi_ios_print(out_file, "/");
4681
0
            }
4682
907
        }
4683
2.28k
    }
4684
4685
2.28k
    return 0;
4686
2.28k
}
4687
4688
/****************************************************************************
4689
Output AuxInfo: stereo info
4690
****************************************************************************/
4691
int OutputAUXINFO_Stereo(CANON_GLOBALS *pCG,
4692
                         INCHI_IOSTREAM *out_file,
4693
                         INCHI_IOS_STRING *strbuf,
4694
                         INCHI_OUT_CTL *io,
4695
                         char *pLF,
4696
                         char *pTAB)
4697
2.28k
{
4698
    /*--    Inverted stereo -- sp3 only + canonical numbering
4699
     */
4700
2.28k
    if (io->bInvStereo[io->iCurTautMode])
4701
0
    {
4702
0
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_STER | io->bFhTag, io->szTag1, &io->bAlways, 0);
4703
        /*-- inverted sp3 start tag */
4704
0
        szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_SP3I, io->szTag2, &io->bAlways, 0);
4705
0
        inchi_strbuf_reset(strbuf);
4706
0
        io->tot_len = 0;
4707
0
        io->tot_len = str_AuxInvSp3(io->pINChISort, io->pINChISort2, strbuf,
4708
0
                                    &io->bOverflow, io->bOutType, io->TAUT_MODE, io->num_components,
4709
0
                                    io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
4710
0
        if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4711
0
            return 1;
4712
0
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4713
4714
        /*-- inverted sp3  canonical numbering */
4715
0
        if (io->bInvStereoOrigNumb[io->iCurTautMode])
4716
0
        {
4717
0
            szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_SP3N, io->szTag2, &io->bAlways, 0);
4718
0
            inchi_strbuf_reset(strbuf);
4719
0
            io->tot_len = 0;
4720
4721
0
            io->tot_len = str_AuxInvSp3Numb(pCG, io->pINChISort, io->pINChISort2,
4722
0
                                            strbuf, &io->bOverflow, io->bOutType,
4723
0
                                            io->TAUT_MODE, io->num_components,
4724
0
                                            io->bSecondNonTautPass, io->bOmitRepetitions);
4725
4726
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4727
0
            {
4728
0
                return 1;
4729
0
            }
4730
0
            inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4731
0
        }
4732
0
        else
4733
0
        {
4734
0
            if (io->bPlainTextTags == 1)
4735
0
                inchi_ios_print(out_file, "/");
4736
0
        }
4737
0
    }
4738
2.28k
    else
4739
2.28k
    {
4740
2.28k
        if (io->bPlainTextTags == 1)
4741
0
        {
4742
0
            inchi_ios_print(out_file, "//");
4743
0
        }
4744
        /* Inverted stereo -- sp3 only + canonical numbering */
4745
2.28k
    }
4746
4747
2.28k
    return 0;
4748
2.28k
}
4749
4750
/****************************************************************************
4751
Output AuxInfo: isotopic info
4752
****************************************************************************/
4753
int OutputAUXINFO_IsotopicInfo(CANON_GLOBALS *pCG,
4754
                               INCHI_IOSTREAM *out_file,
4755
                               INCHI_IOS_STRING *strbuf,
4756
                               int *INCHI_basic_or_INCHI_reconnected,
4757
                               INCHI_OUT_CTL *io,
4758
                               char *pLF,
4759
                               char *pTAB)
4760
2.28k
{
4761
2.28k
    int i;
4762
4763
    /* if InChI Fixed-H isotopic is empty, then do not output corresponding AuxInfo */
4764
4765
2.28k
    i = io->bSecondNonTautPass &&
4766
0
        (*io->pSortPrintINChIFlags & ((*INCHI_basic_or_INCHI_reconnected == INCHI_BAS) ? FLAG_SORT_PRINT_NO_IFIX_H_BAS : FLAG_SORT_PRINT_NO_IFIX_H_REC));
4767
4768
2.28k
    if (io->bIsotopic && !i &&
4769
1.70k
        (io->bIsotopicOrigNumb[io->iCurTautMode] ||
4770
1.70k
         io->bIsotopicAtomEqu[io->iCurTautMode] ||
4771
1.68k
         (io->bTautomericOutputAllowed && io->bTautomeric && io->bIsotopicTautEqu[io->iCurTautMode]) ||
4772
1.68k
         (io->bInvIsotopicStereo[io->iCurTautMode] && (io->bIgn_UU_Sp3_Iso[io->iCurTautMode])) || io->bIgn_UU_Sp2_Iso[io->iCurTautMode])) /* djb-rwth: addressing LLVM warnings */
4773
25
    {
4774
        /*-- isotopic aux info header */
4775
25
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_ISOT | io->bFhTag, io->szTag1, &io->bAlways, 0);
4776
25
        inchi_strbuf_reset(strbuf); /* pStr[io->tot_len = 0] = '\0'; */
4777
        /*-- Original atom numbers in order of isotopic canonical numbers */
4778
25
        szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_ISON, io->szTag2, &io->bAlways, 0);
4779
25
        if (io->bIsotopicOrigNumb[io->iCurTautMode])
4780
0
        {
4781
0
            inchi_strbuf_reset(strbuf);
4782
0
            io->tot_len = 0;
4783
0
            io->tot_len = str_AuxIsoNumb(pCG, io->pINChISort, io->pINChISort2,
4784
0
                                         strbuf, &io->bOverflow, io->bOutType,
4785
0
                                         io->TAUT_MODE, io->num_components,
4786
0
                                         io->bSecondNonTautPass, io->bOmitRepetitions);
4787
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4788
0
            {
4789
0
                return 1;
4790
0
            }
4791
0
            inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4792
0
        }
4793
25
        else
4794
25
        {
4795
            /*if ( io->bPlainTextTags == 1 ) inchi_ios_print( out_file, "/" );*/
4796
25
            inchi_ios_print(out_file, "%s%s", io->szTag2, pLF); /* mark isotopic output */
4797
25
        }
4798
4799
        /*-- Isotopic symmetry */
4800
25
        if (io->bIsotopicAtomEqu[io->iCurTautMode])
4801
25
        {
4802
            /*-- atoms */
4803
25
            szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_AEQU, io->szTag2, &io->bAlways, 0);
4804
25
            inchi_strbuf_reset(strbuf);
4805
25
            io->tot_len = 0;
4806
25
            io->tot_len = str_AuxIsoEqu(io->pINChISort, io->pINChISort2,
4807
25
                                        strbuf,
4808
25
                                        &io->bOverflow, io->bOutType, io->TAUT_MODE, io->num_components,
4809
25
                                        io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
4810
25
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -2 /*was -1: Fix15*/, io->bPlainTextTags))
4811
0
            {
4812
0
                return 1;
4813
0
            }
4814
25
            inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4815
25
        }
4816
0
        else
4817
0
        {
4818
0
            if (io->bPlainTextTags == 1)
4819
0
            {
4820
0
                inchi_ios_print(out_file, "/");
4821
0
            }
4822
0
        }
4823
4824
        /*-- Tautomeric groups, isotopic */
4825
25
        if (io->bTautomericOutputAllowed && io->bTautomeric && io->bIsotopicTautEqu[io->iCurTautMode])
4826
0
        {
4827
            /*-- Isotopic tautomeric groups equivalence */
4828
0
            szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_GEQU, io->szTag2, &io->bAlways, 0);
4829
0
            inchi_strbuf_reset(strbuf);
4830
0
            io->tot_len = 0;
4831
0
            io->tot_len = str_AuxIsoTgroupEqu(io->pINChISort,
4832
0
                                              strbuf, &io->bOverflow,
4833
0
                                              io->bOutType, io->TAUT_MODE, io->num_components,
4834
0
                                              io->bOmitRepetitions, io->bUseMulipliers);
4835
0
            if (str_LineEnd(io->szTag2, &io->bOverflow, strbuf, -2 /*was -1: Fix15*/, io->bPlainTextTags))
4836
0
            {
4837
0
                return 1;
4838
0
            }
4839
0
            inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4840
0
        }
4841
25
        else
4842
25
        {
4843
25
            if (io->bTautomericOutputAllowed && io->bTautomeric)
4844
5
            {
4845
5
                if (io->bPlainTextTags == 1)
4846
0
                {
4847
0
                    inchi_ios_print(out_file, "/");
4848
0
                }
4849
5
            }
4850
25
        }
4851
        /*-- Isotopic inverted stereo */
4852
25
        if (io->bInvIsotopicStereo[io->iCurTautMode])
4853
0
        {
4854
0
            szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_STER, io->szTag2, &io->bAlways, 0);
4855
            /*-- inverted isotopic sp3 start tag */
4856
0
            szGetTag(AuxLbl, io->nTag, io->bTag3 = io->bTag2 | AL_SP3I, io->szTag3, &io->bAlways, 0);
4857
0
            inchi_strbuf_reset(strbuf);
4858
0
            io->tot_len = 0;
4859
0
            io->tot_len = str_AuxInvIsoSp3(io->pINChISort, io->pINChISort2,
4860
0
                                           strbuf, &io->bOverflow,
4861
0
                                           io->bOutType, io->TAUT_MODE, io->num_components,
4862
0
                                           io->bSecondNonTautPass, io->bOmitRepetitions, io->bUseMulipliers);
4863
0
            if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4864
0
            {
4865
0
                return 1;
4866
0
            }
4867
0
            inchi_ios_print(out_file, "%s", strbuf->pStr);
4868
            /*-- inverted isotopic sp3  canonical numbering */
4869
0
            if (io->bInvIsotopicStereoOrigNumb[io->iCurTautMode])
4870
0
            {
4871
0
                szGetTag(AuxLbl, io->nTag, io->bTag3 = io->bTag2 | AL_SP3N, io->szTag3, &io->bAlways, 0);
4872
0
                inchi_strbuf_reset(strbuf);
4873
0
                io->tot_len = 0;
4874
0
                io->tot_len = str_AuxInvIsoSp3Numb(pCG, io->pINChISort, io->pINChISort2,
4875
0
                                                   strbuf, &io->bOverflow,
4876
0
                                                   io->bOutType, io->TAUT_MODE,
4877
0
                                                   io->num_components,
4878
0
                                                   io->bSecondNonTautPass,
4879
0
                                                   io->bOmitRepetitions);
4880
4881
0
                if (str_LineEnd(io->szTag3, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4882
0
                {
4883
0
                    return 1;
4884
0
                }
4885
0
                inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4886
0
            }
4887
0
            else
4888
0
            {
4889
0
                if (io->bPlainTextTags == 1)
4890
0
                {
4891
0
                    inchi_ios_print(out_file, "/");
4892
0
                }
4893
0
            }
4894
0
        }
4895
25
        else
4896
25
        {
4897
25
            if (io->bPlainTextTags == 1)
4898
0
            {
4899
0
                inchi_ios_print(out_file, "//");
4900
0
            }
4901
25
        }
4902
        /*-- totally omitted undefined/unknown isotopic stereo */
4903
25
    } /* Aux info isotopic */
4904
4905
2.28k
    return 0;
4906
2.28k
}
4907
4908
/****************************************************************************
4909
Output AuxInfo: charges, radicals, unusual valences
4910
****************************************************************************/
4911
int OutputAUXINFO_ChargesRadicalsAndUnusualValences(CANON_GLOBALS *pCG,
4912
                                                    INCHI_IOSTREAM *out_file,
4913
                                                    INCHI_IOS_STRING *strbuf,
4914
                                                    INCHI_OUT_CTL *io,
4915
                                                    char *pLF,
4916
                                                    char *pTAB)
4917
2.28k
{
4918
2.28k
    if (!io->bSecondNonTautPass && io->bChargesRadVal[io->iCurTautMode])
4919
1.81k
    {
4920
        /*  aux equ atoms */
4921
        /* 1. Compare to tautomeric equivalence (in case of second, non-taut, pass only) */
4922
        /* 2. Compare to the previous component if (1) failed to find equivalence */
4923
1.81k
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_CRV_ | io->bFhTag, io->szTag1, &io->bAlways, 0);
4924
4925
1.81k
        inchi_strbuf_reset(strbuf);
4926
1.81k
        io->tot_len = 0;
4927
4928
1.81k
        io->tot_len = str_AuxChargeRadVal(io->pINChISort, strbuf,
4929
1.81k
                                          &io->bOverflow, io->bOutType, io->TAUT_MODE,
4930
1.81k
                                          io->num_components, io->bUseMulipliers);
4931
4932
1.81k
        if (str_LineEnd(io->szTag1, &io->bOverflow, strbuf, -1, io->bPlainTextTags))
4933
0
        {
4934
0
            return 1;
4935
0
        }
4936
4937
1.81k
        inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
4938
1.81k
    }
4939
4940
2.28k
    return 0;
4941
2.28k
}
4942
4943
/****************************************************************************
4944
Output AuxInfo: reversibility info (to restore orig. structure)
4945
****************************************************************************/
4946
int OutputAUXINFO_ReversibilityInfo(CANON_GLOBALS *pCG,
4947
                                    INCHI_IOSTREAM *out_file,
4948
                                    INCHI_IOS_STRING *strbuf,
4949
                                    ORIG_STRUCT *pOrigStruct,
4950
                                    INCHI_OUT_CTL *io,
4951
                                    char *pLF,
4952
                                    char *pTAB)
4953
2.28k
{
4954
2.28k
    if (!io->bSecondNonTautPass &&
4955
2.28k
        pOrigStruct && pOrigStruct->num_atoms &&
4956
2.28k
        pOrigStruct->szAtoms && pOrigStruct->szBonds && pOrigStruct->szCoord)
4957
2.28k
    {
4958
2.28k
        int length, cur_pos, line_len, last_pos, nMaxLineLen;
4959
2.28k
        char *p;
4960
2.28k
        nMaxLineLen = inchi_min(80, strbuf->nAllocatedLength); /* restrict line length to 80 characters */
4961
4962
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag1 = AL_REVR | io->bFhTag, io->szTag1, &io->bAlways, 0);
4963
4964
        /* Atoms /A: */
4965
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_ATMR, io->szTag2, &io->bAlways, 0);
4966
2.28k
        inchi_strbuf_reset(strbuf);
4967
2.28k
        inchi_ios_print(out_file, "%s%s", io->szTag2, strbuf->pStr);
4968
2.28k
        p = pOrigStruct->szAtoms;
4969
2.28k
        length = (int)strlen(p);
4970
2.28k
        io->tot_len = strbuf->nUsedLength;
4971
2.28k
        line_len = nMaxLineLen - io->tot_len;
4972
5.81k
        for (cur_pos = 0; cur_pos < length; cur_pos = last_pos)
4973
3.52k
        {
4974
3.52k
            if (length - cur_pos >= line_len)
4975
1.23k
            {
4976
1.23k
                last_pos = cur_pos + line_len;
4977
                /* search backward for the nearest first atom letter (always uppercase) */
4978
4.40k
                while (cur_pos < last_pos && !isupper(UCINT p[last_pos]))
4979
3.16k
                {
4980
3.16k
                    last_pos--;
4981
3.16k
                }
4982
1.23k
            }
4983
2.28k
            else
4984
2.28k
            {
4985
2.28k
                last_pos = length;
4986
2.28k
            }
4987
3.52k
            if (last_pos > cur_pos)
4988
3.52k
            {
4989
3.52k
                memcpy(strbuf->pStr + strbuf->nUsedLength, p + cur_pos, (long long)last_pos - (long long)cur_pos); /* djb-rwth: cast operators added */
4990
3.52k
                strbuf->pStr[strbuf->nUsedLength + last_pos - cur_pos] = '\0';
4991
                /*strbuf->nUsedLength = strbuf->nUsedLength + last_pos - cur_pos;*/
4992
4993
3.52k
                if (1) /* always show "Zy" as "Zz" */
4994
3.52k
                {
4995
3.52k
                    char *pzy, *pstart = strbuf->pStr + strbuf->nUsedLength;
4996
4.01k
                    while ((pzy = strstr(pstart, "Zy"))) /* djb-rwth: addressing LLVM warning */
4997
491
                    {
4998
491
                        *(++pzy) = 'z';
4999
491
                        pstart = pzy;
5000
491
                    }
5001
3.52k
                }
5002
5003
3.52k
                inchi_ios_print(out_file, "%s%s", strbuf->pStr, io->bPlainTextTags ? "" : "\n");
5004
3.52k
            }
5005
0
            else
5006
0
            {
5007
0
                break;
5008
0
            }
5009
3.52k
        }
5010
2.28k
        if (pLF[0])
5011
0
        {
5012
0
            inchi_ios_print(out_file, "%s", pLF);
5013
0
        }
5014
5015
2.28k
        inchi_strbuf_reset(strbuf);
5016
5017
        /* Bonds /B: */
5018
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_BNDR, io->szTag2, &io->bAlways, 0);
5019
2.28k
        inchi_strbuf_reset(strbuf);
5020
2.28k
        inchi_ios_print(out_file, "%s%s", io->szTag2, strbuf->pStr);
5021
5022
2.28k
        p = pOrigStruct->szBonds;
5023
2.28k
        length = (int)strlen(p);
5024
2.28k
        line_len = nMaxLineLen - io->tot_len;
5025
4.45k
        for (cur_pos = 0; cur_pos < length; cur_pos = last_pos)
5026
2.17k
        {
5027
2.17k
            if (length - cur_pos >= line_len)
5028
156
            {
5029
156
                last_pos = cur_pos + line_len - 1;
5030
                /* search backward for the nearest first bond delimiter ";" */
5031
156
                while (cur_pos < last_pos && p[last_pos] != ';')
5032
0
                {
5033
0
                    last_pos--;
5034
0
                }
5035
156
                if (cur_pos < last_pos)
5036
156
                {
5037
156
                    last_pos++; /* include ';' at the end of the line */
5038
156
                }
5039
156
            }
5040
2.01k
            else
5041
2.01k
            {
5042
2.01k
                last_pos = length;
5043
2.01k
            }
5044
2.17k
            if (last_pos > cur_pos)
5045
2.17k
            {
5046
2.17k
                memcpy(strbuf->pStr, p + cur_pos, (long long)last_pos - (long long)cur_pos); /* djb-rwth: cast operators added */
5047
2.17k
                strbuf->pStr[last_pos - cur_pos] = '\0';
5048
2.17k
                strbuf->nUsedLength = last_pos - cur_pos;
5049
2.17k
                inchi_ios_print(out_file, "%s%s", strbuf->pStr, io->bPlainTextTags ? "" : "\n");
5050
2.17k
                inchi_strbuf_reset(strbuf);
5051
2.17k
            }
5052
0
            else
5053
0
            {
5054
0
                break;
5055
0
            }
5056
2.17k
        }
5057
2.28k
        if (pLF[0])
5058
0
        {
5059
0
            inchi_ios_print(out_file, "%s", pLF);
5060
0
        }
5061
5062
        /* Coordinates /C:    */
5063
2.28k
        szGetTag(AuxLbl, io->nTag, io->bTag2 = io->bTag1 | AL_XYZR, io->szTag2, &io->bAlways, 0);
5064
2.28k
        inchi_strbuf_reset(strbuf);
5065
2.28k
        inchi_ios_print(out_file, "%s%s", io->szTag2, strbuf->pStr);
5066
5067
2.28k
        p = pOrigStruct->szCoord;
5068
2.28k
        length = (int)strlen(p);
5069
2.28k
        line_len = nMaxLineLen - io->tot_len;
5070
6.81k
        for (cur_pos = 0; cur_pos < length; cur_pos = last_pos)
5071
4.53k
        {
5072
4.53k
            if (length - cur_pos >= line_len)
5073
2.25k
            {
5074
2.25k
                last_pos = cur_pos + line_len - 1;
5075
                /* search backward for the nearest first coord. delimiter ";" */
5076
27.2k
                while (cur_pos < last_pos && p[last_pos] != ';')
5077
24.9k
                {
5078
24.9k
                    last_pos--;
5079
24.9k
                }
5080
2.25k
                if (cur_pos < last_pos)
5081
2.25k
                {
5082
2.25k
                    last_pos++; /* include ';' at the end of the line */
5083
2.25k
                }
5084
2.25k
            }
5085
2.27k
            else
5086
2.27k
            {
5087
2.27k
                last_pos = length;
5088
2.27k
            }
5089
4.53k
            if (last_pos > cur_pos)
5090
4.53k
            {
5091
4.53k
                memcpy(strbuf->pStr, p + cur_pos, (long long)last_pos - (long long)cur_pos); /* djb-rwth: cast operator added */
5092
4.53k
                strbuf->pStr[last_pos - cur_pos] = '\0';
5093
4.53k
                strbuf->nUsedLength = last_pos - cur_pos;
5094
4.53k
                inchi_ios_print(out_file, "%s%s", strbuf->pStr, io->bPlainTextTags ? "" : "\n");
5095
4.53k
                inchi_strbuf_reset(strbuf);
5096
4.53k
            }
5097
0
            else
5098
0
            {
5099
0
                break;
5100
0
            }
5101
4.53k
        }
5102
5103
2.28k
        if (pLF[0])
5104
0
        {
5105
0
            inchi_ios_print(out_file, "%s", pLF);
5106
0
        }
5107
2.28k
    }
5108
5109
2.28k
    return 0;
5110
2.28k
}
5111
5112
/****************************************************************************/
5113
int OutputAUXINFO_PolymerInfo(CANON_GLOBALS *pCG,
5114
                              INCHI_IOSTREAM *out_file,
5115
                              INCHI_IOS_STRING *strbuf,
5116
                              ORIG_STRUCT *pOrigStruct,
5117
                              INCHI_OUT_CTL *io,
5118
                              char *pLF,
5119
                              char *pTAB)
5120
2.28k
{
5121
2.28k
    int k, i, q;
5122
2.28k
    OAD_Polymer *p;
5123
2.28k
    OAD_PolymerUnit *u;
5124
5125
2.28k
    if (!pOrigStruct)
5126
0
    {
5127
0
        return 0;
5128
0
    }
5129
2.28k
    p = pOrigStruct->polymer;
5130
2.28k
    if (!p)
5131
2.28k
    {
5132
2.28k
        return 0;
5133
2.28k
    }
5134
5135
0
    inchi_strbuf_reset(strbuf);
5136
5137
0
    inchi_ios_print(out_file, "/Z:");
5138
5139
    /* Print polymer units data */
5140
0
    for (i = 0; i < p->n; i++)
5141
0
    {
5142
        /* For each unit u ... */
5143
0
        u = p->units[i];
5144
5145
        /* print kinds of unit */
5146
0
        inchi_strbuf_printf(strbuf, "%-d%-d%-d-", u->type, u->subtype, u->conn);
5147
0
        inchi_strbuf_printf(strbuf, "%-s-", u->smt[0] ? u->smt : "n");
5148
5149
        /* Print unit atoms */
5150
0
        print_sequence_of_nums_compressing_ranges(u->na, u->alist, strbuf);
5151
5152
        /* Print bonds from unit to otside */
5153
0
        if (u->nb > 0)
5154
0
        {
5155
0
            inchi_strbuf_printf(strbuf, "(");
5156
0
            for (k = 0; k < 2 * u->nb - 1; k++)
5157
0
            {
5158
0
                inchi_strbuf_printf(strbuf, "%-d,", u->blist[k]);
5159
0
            }
5160
0
            inchi_strbuf_printf(strbuf, "%-d)", u->blist[2 * u->nb - 1]);
5161
0
        }
5162
5163
0
        if (fabs(-fabs(u->xbr1[0]) + 777777.777) > 1.e-7)
5164
0
        {
5165
0
            inchi_strbuf_printf(strbuf, "[");
5166
0
            for (q = 0; q < 3; q++)
5167
0
            {
5168
0
                inchi_strbuf_printf(strbuf, "%-f,", u->xbr1[q]);
5169
0
            }
5170
0
            inchi_strbuf_printf(strbuf, "%-f]", u->xbr1[3]);
5171
0
        }
5172
0
        if (fabs(-fabs(u->xbr2[0]) + 777777.777) > 1.e-7)
5173
0
        {
5174
0
            inchi_strbuf_printf(strbuf, "[");
5175
0
            for (q = 0; q < 3; q++)
5176
0
            {
5177
0
                inchi_strbuf_printf(strbuf, "%-f,", u->xbr2[q]);
5178
0
            }
5179
0
            inchi_strbuf_printf(strbuf, "%-f]", u->xbr2[3]);
5180
0
        }
5181
5182
0
        if (i < p->n - 1)
5183
0
        {
5184
0
            inchi_strbuf_printf(strbuf, ";");
5185
0
        }
5186
0
    }
5187
5188
0
    inchi_ios_print(out_file, "%s%s", strbuf->pStr, pLF);
5189
5190
0
    return 0;
5191
2.28k
}
5192
5193
/****************************************************************************/
5194
int IsBondAtomNumsLesser(int *bond1, int *bond2)
5195
0
{
5196
0
    int min1 = inchi_min(bond1[0], bond1[1]);
5197
0
    int min2 = inchi_min(bond2[0], bond2[1]);
5198
0
    int max1 = inchi_max(bond1[0], bond1[1]);
5199
0
    int max2 = inchi_max(bond2[0], bond2[1]);
5200
5201
0
    if (min1 < min2)
5202
0
    {
5203
0
        return -1;
5204
0
    }
5205
0
    if (min1 > min2)
5206
0
    {
5207
0
        return 1;
5208
0
    }
5209
0
    if (min1 == min2)
5210
0
    {
5211
0
        if (max1 < max2)
5212
0
        {
5213
0
            return -1;
5214
0
        }
5215
0
        if (max1 > max2)
5216
0
        {
5217
0
            return 1;
5218
0
        }
5219
0
    }
5220
5221
0
    return 0;
5222
0
}
5223
5224
/****************************************************************************/
5225
void EditINCHI_HidePolymerZz(INCHI_IOSTREAM *out, int n_pzz, int n_zy)
5226
0
{
5227
0
    char *s = out->s.pStr, *s0, *buf = NULL;
5228
0
    char prev_layer_symbol = '0';
5229
0
    int i, j, ii, nzz,
5230
0
        nzz1 = 0, nslash = 0, ncopied = 0,
5231
0
        start = 0, skip = 0, is_in_z_layer = 0,
5232
0
        eol_was_consumed = 0, pre_eol = 0,
5233
0
        nonprt_sym = 0, nonprt_prev = 0;
5234
5235
0
    if (n_zy > 0)
5236
0
    {
5237
        /* We have some placeholder pseudo atoms which should not be removed below (if anyway they are allowed) */
5238
0
        if (n_pzz == 0)
5239
0
        {
5240
            /* Have nothing to remove, just exit */
5241
0
            return;
5242
0
        }
5243
        /* Have both polymer-related and placeholder pseudo atoms */
5244
0
        if (n_pzz < 2)
5245
0
        {
5246
            /* Something strange, should not arrive here, cowardly exit */
5247
0
            return;
5248
0
        }
5249
0
    }
5250
5251
    /* Ensure that polymeric layer is present */
5252
0
    if (!strstr(s, "/z"))
5253
0
    {
5254
0
        return;
5255
0
    }
5256
0
    s0 = strstr(s, INCHI_NAME INCHI_NAM_VER_DELIM);
5257
0
    if (!s0)
5258
0
    {
5259
0
        return;
5260
0
    }
5261
5262
#if 0
5263
    nzz1 = CountPseudoElementInFormula("Zz", s0 + strlen("InChI=1B/"));
5264
    if (nzz1 == 0)
5265
    {
5266
        return;
5267
    }
5268
    if (nzz1 != (n_pzz + n_zy))
5269
    {
5270
        /* Something strange, should not arrive here, cowardly exit */
5271
        return;
5272
    }
5273
#endif
5274
0
    nzz1 = n_pzz;
5275
5276
    /* OK, we must hide n_pzz Zz's*/
5277
0
    buf = (char *)inchi_calloc((long long)out->s.nUsedLength + 1, sizeof(char)); /* djb-rwth: cast operator added */
5278
0
    if (!buf)
5279
0
    {
5280
0
        return;
5281
0
    }
5282
5283
    /* Consume '\n' temporarily */
5284
0
    if (s[out->s.nUsedLength - 1] == '\n')
5285
0
    {
5286
0
        s[out->s.nUsedLength - 1] = '\0';
5287
0
        out->s.nUsedLength--;
5288
0
        eol_was_consumed = 1;
5289
0
    }
5290
5291
0
    start = s0 - s;
5292
0
    nzz = nzz1;
5293
0
    is_in_z_layer = skip = 0;
5294
0
    for (i = start; i < out->s.nUsedLength; i++)
5295
0
    {
5296
0
        pre_eol = (i == out->s.nUsedLength - 1);
5297
0
        nonprt_sym = s[i] == '\n' || s[i] == '\r' || s[i] == '\t';
5298
5299
0
        if (!skip)
5300
0
        {
5301
0
            buf[ncopied] = s[i];
5302
0
            ncopied++;
5303
0
            if (nonprt_sym && nonprt_prev)
5304
0
            {
5305
0
                continue;
5306
0
            }
5307
0
        }
5308
0
        nonprt_prev = nonprt_sym;
5309
5310
0
        if (is_in_z_layer && !skip)
5311
0
        {
5312
0
            if (s[i] == '(')
5313
0
            {
5314
                /* Software version 1.07 : skip pattern "(cap,cap-bkbonds)" but not "(cap-end, cap-end)" */
5315
0
                const char *q;
5316
0
                const char *p = out->s.pStr + i + 1;
5317
0
                AT_NUMB ia = (AT_NUMB)inchi_strtol(p, &q, 10); /* make compiler happy: */ /* djb-rwth: removing redundant code; ignoring LLVM warning: variable used to store function return value */
5318
0
                if (*q != '-')
5319
0
                {
5320
0
                    skip = 1;
5321
0
                }
5322
0
            }
5323
0
        }
5324
0
        else if (is_in_z_layer && skip)
5325
0
        {
5326
0
            if (s[i] == '-')
5327
0
            {
5328
0
                skip = 0;
5329
0
            }
5330
0
        }
5331
5332
0
        if (s[i] == '/' || pre_eol || nonprt_sym)
5333
0
        {
5334
0
            if (is_in_z_layer)
5335
0
            {
5336
0
                is_in_z_layer = 0;
5337
0
            }
5338
5339
0
            if (s[i] == '/')
5340
0
            {
5341
0
                nslash++;
5342
0
            }
5343
5344
0
            if (nslash == 2 ||
5345
0
                (nslash == 1 && pre_eol) ||
5346
0
                (prev_layer_symbol == 'f'))
5347
0
            {
5348
0
                if (nzz)
5349
0
                {
5350
                    /* eat Zz's */
5351
0
                    ii = i;
5352
0
                    if (pre_eol)
5353
0
                    {
5354
0
                        ii = i + 1;
5355
0
                    }
5356
0
                    if (s[ii - 1] == 'z' && s[ii - 2] == 'Z')
5357
0
                    {
5358
0
                        ncopied -= 2;
5359
0
                        for (j = ii - 3; j >= 0; j--)
5360
0
                        {
5361
0
                            if (s[j] == '.')
5362
0
                            {
5363
0
                                break;
5364
0
                            }
5365
0
                            ncopied--;
5366
0
                        }
5367
0
                        ncopied--;
5368
0
                        if (!pre_eol)
5369
0
                        {
5370
0
                            buf[ncopied - 1] = '/';
5371
0
                        }
5372
0
                        else
5373
0
                        {
5374
0
                            buf[ncopied - 1] = '\0';
5375
0
                        }
5376
0
                    }
5377
0
                }
5378
0
            }
5379
0
            else if (nslash > 2 || pre_eol || s[i] == '\n')
5380
0
            {
5381
0
                if (nzz)
5382
0
                {
5383
0
                    if (prev_layer_symbol != 'p' &&
5384
0
                        prev_layer_symbol != 's' &&
5385
0
                        prev_layer_symbol != 'f' &&
5386
0
                        prev_layer_symbol != 'z')
5387
0
                    {
5388
                        /* eat nzz last ; if any */
5389
0
                        int n_eaten = 0;
5390
0
                        char eatable = ';';
5391
0
                        if (prev_layer_symbol == 'm')
5392
0
                        {
5393
0
                            eatable = '.';
5394
0
                        }
5395
0
                        ii = i;
5396
0
                        if (pre_eol)
5397
0
                        {
5398
0
                            ii = i + 1;
5399
0
                        }
5400
0
                        for (j = ii - 1; j >= 0; j--)
5401
0
                        {
5402
0
                            if (s[j] == eatable && n_eaten < nzz)
5403
0
                            {
5404
0
                                ncopied--;
5405
0
                                n_eaten++;
5406
0
                            }
5407
0
                            else
5408
0
                            {
5409
0
                                break;
5410
0
                            }
5411
0
                        }
5412
0
                        if (!pre_eol)
5413
0
                        {
5414
0
                            if (s[i] == '\n')
5415
0
                            {
5416
0
                                buf[ncopied - 1] = '\n';
5417
0
                            }
5418
0
                            else
5419
0
                            {
5420
0
                                buf[ncopied - 1] = '/';
5421
0
                            }
5422
0
                        }
5423
0
                        else
5424
0
                        {
5425
0
                            buf[ncopied] = '\0';
5426
0
                            break;
5427
0
                        }
5428
0
                    }
5429
0
                }
5430
0
            }
5431
5432
0
            prev_layer_symbol = s[i + 1];
5433
0
            if (s[i + 1] == 'r')
5434
0
            {
5435
0
                if (i + 3 < out->s.nUsedLength && s[i + 3] == ':')
5436
0
                {
5437
                    /* ra: rB; rC: in AuxInfo */
5438
0
                    ;
5439
0
                }
5440
0
                else
5441
0
                {
5442
                    /* reconnected layer */
5443
0
                    nslash = 1;
5444
                    /* nzz = nzz2;    paranoidal */
5445
0
                }
5446
0
            }
5447
0
            else if (s[i + 1] == 'z')
5448
0
            {
5449
0
                is_in_z_layer = 1;
5450
0
            }
5451
0
        }
5452
0
    }
5453
5454
0
    out->s.nUsedLength = 0;
5455
0
    inchi_ios_print_nodisplay(out, "%s%s", buf, eol_was_consumed ? "\n" : "");
5456
0
    inchi_free(buf);
5457
5458
0
    return;
5459
0
}
5460
5461
/****************************************************************************/
5462
int CountPseudoElementInFormula(const char *pseudo, char *s) /* djb-rwth: ignoring LLVM warning: function used */
5463
0
{
5464
0
    int npseudo = 0, mult = 1, index = 1, new_component = 1;
5465
0
    const char *p, *q;
5466
0
    char prev = '/';
5467
0
5468
0
    /*
5469
0
        format is
5470
0
        [sequence of] [.[int_mult[Zz[int_index]]]]
5471
0
    */
5472
0
5473
0
    if (!s)
5474
0
    {
5475
0
        return 0;
5476
0
    }
5477
0
5478
0
    p = s;
5479
0
    while (*p)
5480
0
    /*for (p = s ; *p; p++)*/
5481
0
    {
5482
0
        if (*p == '/')
5483
0
        {
5484
0
            /* end of formula layer */
5485
0
            break;
5486
0
        }
5487
0
        else if (*p == '.')
5488
0
        {
5489
0
            /* start of new component subformula */
5490
0
            new_component = 1;
5491
0
            prev = *p;
5492
0
            p++;
5493
0
            continue;
5494
0
        }
5495
0
5496
0
        if (new_component)
5497
0
        {
5498
0
            new_component = 0;
5499
0
            if (isdigit(*p))
5500
0
            {
5501
0
                mult = (int)inchi_strtol(p, &q, 10);
5502
0
                p = q;
5503
0
                prev = *q--;
5504
0
                continue;
5505
0
            }
5506
0
            else
5507
0
            {
5508
0
                mult = 1;
5509
0
            }
5510
0
            if (!mult)
5511
0
            {
5512
0
                break;
5513
0
            }
5514
0
        }
5515
0
        else if (*p == pseudo[1] && prev == pseudo[0])
5516
0
        {
5517
0
            q = p++;
5518
0
            if (*q && isdigit(*q))
5519
0
            {
5520
0
                index = (int)inchi_strtol(q, &p, 10);
5521
0
            }
5522
0
            else
5523
0
            {
5524
0
                index = 1;
5525
0
                p = q;
5526
0
            }
5527
0
            npseudo += mult * index;
5528
0
        }
5529
0
        prev = *p;
5530
0
        p++;
5531
0
    }
5532
0
5533
0
    return npseudo;
5534
0
}
5535
5536
/****************************************************************************
5537
    Get canonical numbers and component numbers for each original atom
5538
    NB:  cano_nums[orig_num]   - in orig_nums order,
5539
         compnt_nums[cano_num] - in cano_nums  order
5540
    NB': orig_nums   are 1-based
5541
         cano_nums   are 0-based
5542
         compnt_nums are 1-based
5543
****************************************************************************/
5544
int InternallyGetCanoNumsAndComponentNums(CANON_GLOBALS *pCG,
5545
                                          INCHI_IOS_STRING *strbuf,
5546
                                          INCHI_OUT_CTL *io,
5547
                                          int nat,
5548
                                          int *cano_nums,
5549
                                          int *compnt_nums)
5550
0
{
5551
0
    int orig_num, cano_num, icompnt, i, k, ndigit, err;
5552
0
    char c, cnum[8];
5553
5554
0
    if (!cano_nums || !compnt_nums || !strbuf->pStr)
5555
0
    {
5556
0
        return 1;
5557
0
    }
5558
5559
0
    inchi_strbuf_reset(strbuf);
5560
0
    io->tot_len = str_AuxNumb(pCG, io->pINChISort, io->pINChISort2,
5561
0
                              strbuf, &io->bOverflow, io->bOutType,
5562
0
                              io->TAUT_MODE, io->num_components,
5563
0
                              io->bSecondNonTautPass, io->bOmitRepetitions);
5564
0
    for (i = 0; i < nat; i++)
5565
0
    {
5566
0
        compnt_nums[i] = -1;
5567
0
        cano_nums[i + 1] = -1;
5568
0
    }
5569
5570
0
    ndigit = 0;
5571
0
    err = 0;
5572
    /* djb-rwth: removing redundant code */
5573
0
    icompnt = 1;
5574
0
    cano_num = 0;
5575
0
    for (k = 0; k <= strbuf->nUsedLength; k++)
5576
0
    {
5577
0
        c = strbuf->pStr[k];
5578
0
        if (c == ',' || c == ';' || c == '\0')
5579
0
        {
5580
0
            cnum[ndigit] = '\0';
5581
0
            orig_num = atoi(cnum);
5582
0
            cano_nums[orig_num] = cano_num;
5583
0
            compnt_nums[cano_num] = icompnt;
5584
0
            cnum[0] = '\0';
5585
0
            ndigit = 0;
5586
0
            cano_num++;
5587
0
            if (c == ';')
5588
0
            {
5589
0
                icompnt++;
5590
0
            }
5591
0
            if (c == '\0')
5592
0
            {
5593
0
                break;
5594
0
            }
5595
0
            continue;
5596
0
        }
5597
0
        else if (isdigit(c))
5598
0
        {
5599
0
            cnum[ndigit] = c;
5600
0
            ndigit++;
5601
0
        }
5602
0
        else
5603
0
        {
5604
0
            err = 2;
5605
0
            goto exit_function;
5606
0
        }
5607
0
    }
5608
5609
0
exit_function:
5610
0
    inchi_strbuf_reset(strbuf);
5611
5612
0
    return err;
5613
0
}
5614
5615
/***************************************************************************/
5616
int MergeZzInHillFormula(INCHI_IOS_STRING *strbuf)
5617
0
{
5618
0
    char *p, *scopy = NULL, *stmp = NULL, *pend = NULL, *p0 = NULL; /* djb-rwth: removing redundant variables */
5619
0
    size_t sublen;                                                  /* djb-rwth: removing redundant variables */
5620
5621
0
    if (!strbuf->pStr || strbuf->nUsedLength < 1)
5622
0
    {
5623
0
        return 0;
5624
0
    }
5625
0
    scopy = (char *)inchi_calloc((long long)strbuf->nAllocatedLength + 1, sizeof(char)); /* djb-rwth: cast operator added */
5626
0
    if (!scopy)
5627
0
    {
5628
0
        inchi_free(scopy); /* djb-rwth: avoiding memory leak */
5629
0
        return -1; /* failed */
5630
0
    }
5631
0
    memcpy(scopy, strbuf->pStr, strbuf->nAllocatedLength);
5632
0
    stmp = (char *)inchi_calloc((long long)strbuf->nAllocatedLength + 1, sizeof(char)); /* djb-rwth: cast operator added */
5633
0
    if (!stmp)
5634
0
    {
5635
0
        inchi_free(scopy); /* djb-rwth: avoiding memory leak */
5636
0
        return -1;         /* failed */
5637
0
    }
5638
5639
0
    inchi_strbuf_reset(strbuf);
5640
0
    p0 = scopy;
5641
0
    p = p0;
5642
0
    do
5643
0
    {
5644
        /* djb-rwth: removing redundant code */
5645
0
        pend = strchr(p, '.');
5646
0
        if (!pend)
5647
0
        {
5648
0
            pend = strchr(p, '\0');
5649
0
        }
5650
0
        sublen = pend - p;
5651
0
        memcpy(stmp, p, sublen);
5652
0
        stmp[sublen] = '\0';
5653
0
        MergeZzInStrHillFormulaComponent(stmp);
5654
0
        if (stmp)
5655
0
        {
5656
0
            inchi_strbuf_printf(strbuf, "%-s%-c", stmp, *pend);
5657
0
        }
5658
        /* djb-rwth: removing redundant code */
5659
0
    } while (*pend && (p = pend + 1));
5660
5661
0
    if (scopy)
5662
0
    {
5663
0
        inchi_free(scopy);
5664
0
    }
5665
0
    if (stmp)
5666
0
    {
5667
0
        inchi_free(stmp);
5668
0
    }
5669
5670
0
    return 0;
5671
0
}
5672
5673
/***************************************************************************/
5674
void MergeZzInStrHillFormulaComponent(char *s)
5675
0
{
5676
0
    char *pz = strstr(s, "Zz");
5677
0
    if (pz)
5678
0
    {
5679
0
        int n = 1, offset;
5680
0
        char *pz2 = pz + 2, *pd = pz + 2;
5681
0
        if (*pd && (isdigit(*pd)))
5682
0
        {
5683
0
            n = strtol(pd, &pz2, 10);
5684
0
        }
5685
0
        pz2 = strstr(pz2, "Zz");
5686
0
        if (pz2)
5687
0
        {
5688
0
            int n2 = 1;
5689
0
            char *pd2 = pz2 + 2;
5690
0
            if (*pd2 && (isdigit(*pd2)))
5691
0
            {
5692
0
                n2 = strtol(pd2, &pz2, 10);
5693
0
            }
5694
0
            n += n2;
5695
0
            offset = (int)(pd - s);
5696
0
            sprintf(s + offset, "%d", n);
5697
0
        }
5698
0
    }
5699
0
    return;
5700
0
}
5701
5702
/****************************************************************************/
5703
static void inchi_sort_int_pair_ascending(int *a, int *b)
5704
0
{
5705
0
    int tmp;
5706
0
    if (*a > *b)
5707
0
    {
5708
0
        tmp = *b;
5709
0
        *b = *a;
5710
0
        *a = tmp;
5711
0
    }
5712
5713
0
    return;
5714
0
}