LCOV - code coverage report
Current view: top level - src - mol_fmt4.c (source / functions) Coverage Total Hit
Test: InChI Unit Test Coverage Lines: 15.7 % 866 136
Test Date: 2026-12-03 11:58:49 Functions: 48.6 % 35 17
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7.2 % 999 72

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * SPDX-License-Identifier: MIT
       3                 :             :  * Copyright (c) 2024 IUPAC and InChI Trust
       4                 :             :  */
       5                 :             : 
       6                 :             : #include <stdlib.h>
       7                 :             : #include <ctype.h>
       8                 :             : #include <string.h>
       9                 :             : #include <math.h>
      10                 :             : #include <float.h>
      11                 :             : #include <limits.h>
      12                 :             : 
      13                 :             : #include "mode.h"
      14                 :             : #include "mol_fmt.h"
      15                 :             : 
      16                 :             : #include "ichierr.h"
      17                 :             : #include "util.h"
      18                 :             : #include "ichi_io.h"
      19                 :             : #include "ichimain.h"
      20                 :             : 
      21                 :             : #include "bcf_s.h"
      22                 :             : 
      23                 :             : /*
      24                 :             :     SDFile related procedures
      25                 :             : 
      26                 :             : */
      27                 :             : 
      28                 :             : #define V2000_MAX_COUNT 999 /* GH #75: counts fields are 3 chars wide */
      29                 :             : #define V3000_MAX_LINE 80   /* GH #75: CTfile line length limit */
      30                 :             : #define V3000_PREFIX_LEN 7  /* "M  V30 " */
      31                 :             : #define V3000_MIN_BOND_LENGTH 1.0e-6 /* GH #75: shorter means 0D */
      32                 :             : 
      33                 :             : #define ALIASED_AT(i) (0 < NUM_ISO_H(at, i))
      34                 :             : #define IS_DEUTERIUM(i) (!strcmp(at[i].elname, "D") || at[i].iso_atw_diff == 2 && !strcmp(at[i].elname, "H"))
      35                 :             : #define IS_TRITIUM(i)   (!strcmp(at[i].elname, "T") || at[i].iso_atw_diff == 3 && !strcmp(at[i].elname, "H"))
      36                 :             : 
      37                 :             : #define ABNORMAL_ISO(i) (at[i].iso_atw_diff == 1 || at[i].iso_atw_diff < -3 || at[i].iso_atw_diff > 5)
      38                 :             : #define ABNORMAL_CHG(i) (abs(at[i].charge) > 3)
      39                 :             : #define ABNORMAL_RAD(i) (RADICAL_SINGLET <= at[i].radical && at[i].radical <= RADICAL_TRIPLET)
      40                 :             : 
      41                 :             : #define ANY_ISO(i, X)   ((X)? (at[i].iso_atw_diff && !IS_DEUTERIUM(i) && !IS_TRITIUM(i)) :\
      42                 :             :                           (at[i].iso_atw_diff ||  IS_DEUTERIUM(i) ||  IS_TRITIUM(i)))
      43                 :             : #define ANY_CHG(i)      (0 != at[i].charge)
      44                 :             : #define ANY_RAD(i)      (RADICAL_SINGLET <= at[i].radical && at[i].radical <= RADICAL_TRIPLET)
      45                 :             : 
      46                 :             : #define NORMAL_ISO(i, X)   (ANY_ISO(i, X) && !ABNORMAL_ISO(i))
      47                 :             : 
      48                 :             : /* needs additional M  CHG. M  RAD, M  ISO line */
      49                 :             : /* due to ISIS/Draw feature always include M  RAD for any radical */
      50                 :             : #define ABNORMAL_AT(i) (at[i].radical || abs(at[i].charge) > 3 || \
      51                 :             :                         ABNORMAL_ISO(i))
      52                 :             : 
      53                 :             : /* always add M  ISO, M  RAD, M  CHG; Except: (bAtomsDT && D or T) */
      54                 :             : #define ADD_LINE_AT(i) (at[i].charge ||  \
      55                 :             :                         at[i].radical || \
      56                 :             :                         at[i].iso_atw_diff && (bAtomsDT ? (at[i].iso_atw_diff != 1 || strcmp(at[i].elname, "H")) : 1))
      57                 :             : 
      58                 :             : /* Local */
      59                 :             : 
      60                 :             : static const char sdf_data_hdr_name[] = "NAME";
      61                 :             : static const char sdf_data_hdr_comm[] = "COMMENT";
      62                 :             : 
      63                 :             : enum
      64                 :             : {
      65                 :             :     SDF_START,
      66                 :             :     SDF_DATA_HEADER,
      67                 :             :     SDF_DATA_HEADER_NAME,
      68                 :             :     SDF_DATA_HEADER_COMMENT,
      69                 :             :     SDF_DATA_HEADER_CAS,
      70                 :             :     SDF_DATA_HEADER_USER,
      71                 :             :     SDF_DATA_LINE,
      72                 :             :     SD_FMT_END_OF_DATA_ITEM,
      73                 :             :     SDF_EMPTY_LINE,
      74                 :             :     SD_FMT_END_OF_DATA_BLOCK
      75                 :             : };
      76                 :             : 
      77                 :             : int OrigAtData_WriteToSDfileHeaderAndCountThings(const ORIG_ATOM_DATA *inp_at_data,
      78                 :             :                                                  INCHI_IOSTREAM *fcb,
      79                 :             :                                                  const char *name,
      80                 :             :                                                  const char *comment,
      81                 :             :                                                  int bChiralFlag,
      82                 :             :                                                  int bAtomsDT,
      83                 :             :                                                  const char *szLabel,
      84                 :             :                                                  const char *szValue,
      85                 :             :                                                  int *nNumAliasLines,
      86                 :             :                                                  int *nNumChargeLines,
      87                 :             :                                                  int *nNumRadicalLines,
      88                 :             :                                                  int *nNumIsoLines,
      89                 :             :                                                  int *nNumAddLines,
      90                 :             :                                                  int *num_bonds);
      91                 :             : int OrigAtData_WriteToSDfileAtomsBlock(const ORIG_ATOM_DATA *inp_at_data,
      92                 :             :                                        INCHI_IOSTREAM *fcb,
      93                 :             :                                        const char *name,
      94                 :             :                                        const char *comment,
      95                 :             :                                        int bAtomsDT,
      96                 :             :                                        const char *szLabel,
      97                 :             :                                        const char *szValue);
      98                 :             : 
      99                 :             : int OrigAtData_WriteToSDfileBondsBlock(const ORIG_ATOM_DATA *inp_at_data,
     100                 :             :                                        INCHI_IOSTREAM *fcb,
     101                 :             :                                        const char *name,
     102                 :             :                                        const char *comment,
     103                 :             :                                        const char *szLabel,
     104                 :             :                                        const char *szValue,
     105                 :             :                                        INT_ARRAY *written_bond_ends);
     106                 :             : 
     107                 :             : int OrigAtData_WriteToSDfileAdditionalLines(const ORIG_ATOM_DATA *inp_at_data,
     108                 :             :                                             INCHI_IOSTREAM *fcb,
     109                 :             :                                             const char *name,
     110                 :             :                                             const char *comment,
     111                 :             :                                             int bAtomsDT,
     112                 :             :                                             const char *szLabel,
     113                 :             :                                             const char *szValue,
     114                 :             :                                             int nNumAliasLines,
     115                 :             :                                             int nNumChargeLines,
     116                 :             :                                             int nNumRadicalLines,
     117                 :             :                                             int nNumIsoLines,
     118                 :             :                                             INT_ARRAY *written_bond_ends);
     119                 :             : 
     120                 :             : int OrigAtData_WriteToSDfilePolymerData(const ORIG_ATOM_DATA *inp_at_data,
     121                 :             :                                         INCHI_IOSTREAM *fcb,
     122                 :             :                                         const char *name,
     123                 :             :                                         const char *comment,
     124                 :             :                                         const char *szLabel,
     125                 :             :                                         const char *szValue,
     126                 :             :                                         INT_ARRAY *written_bond_ends);
     127                 :             : 
     128                 :             : /****************************************************************************
     129                 :             :  Skip extra data ( != Molfile) which SDF contains
     130                 :             : ****************************************************************************/
     131                 :         173 : int SDFileSkipExtraData(INCHI_IOSTREAM *inp_file,
     132                 :             :                         unsigned long *CAS_num,
     133                 :             :                         char *comment,
     134                 :             :                         int lcomment,
     135                 :             :                         char *name,
     136                 :             :                         int lname,
     137                 :             :                         int prev_err,
     138                 :             :                         const char *pSdfLabel,
     139                 :             :                         char *pSdfValue,
     140                 :             :                         char *pStrErr,
     141                 :             :                         int bNoWarnings)
     142                 :             : {
     143                 :         173 :     char *p = NULL;
     144                 :             :     char line[MOL_FMT_INPLINELEN];
     145                 :         173 :     const int line_len = sizeof( line );
     146                 :         173 :     int n_blank_lines = 0, n_lines = 0;
     147                 :         173 :     int current_state = SDF_START;
     148                 :         173 :     int err = 0;
     149                 :         173 :     int wait_for_CAS = 0;
     150                 :         173 :     int CAS_num_is_user = 0;
     151   [ -  +  -  -  :         173 :     int wait_for_name = name && lname > 0 && !name[0];
                   -  - ]
     152   [ -  +  -  -  :         173 :     int wait_for_comment = comment && lcomment > 0 && !comment[0];
                   -  - ]
     153   [ -  +  -  -  :         173 :     int wait_for_user = pSdfLabel && pSdfLabel[0] && pSdfValue;
                   -  - ]
     154                 :             : 
     155         [ +  - ]:         173 :     if (CAS_num != NULL)
     156                 :             :     {
     157                 :         173 :         wait_for_CAS = 1;
     158                 :         173 :         *CAS_num = 0LU;
     159   [ -  +  -  - ]:         173 :         CAS_num_is_user = (wait_for_user && !inchi_memicmp(pSdfLabel, "CAS", 3));
     160                 :             :     }
     161                 :             : 
     162         [ +  + ]:         357 :     while (!err &&
     163   [ +  -  +  + ]:         357 :            current_state != SD_FMT_END_OF_DATA_BLOCK &&
     164                 :         173 :            NULL != (p = inchi_fgetsLf(line, line_len, inp_file)))
     165                 :             :     {
     166   [ +  -  -  + ]:          11 :         if (!n_lines && !memcmp(line, "M  END", 6))
     167                 :             :         {
     168                 :             :             /*  allow subtle errors */
     169                 :           0 :             continue;
     170                 :             :         }
     171                 :             : 
     172                 :          11 :         n_lines++;
     173                 :          11 :         remove_trailing_spaces(line);
     174                 :             : 
     175         [ -  + ]:          11 :         if (line[MOL_FMT_MAXLINELEN])
     176                 :             :         {
     177   [ #  #  #  # ]:           0 :             if (current_state != SDF_DATA_HEADER &&
     178         [ #  # ]:           0 :                 current_state != SDF_DATA_LINE &&
     179         [ #  # ]:           0 :                 current_state != SDF_DATA_HEADER_NAME &&
     180         [ #  # ]:           0 :                 current_state != SDF_DATA_HEADER_USER &&
     181                 :             :                 current_state != SDF_DATA_HEADER_COMMENT)
     182                 :             :             {
     183                 :           0 :                 line[MOL_FMT_MAXLINELEN] = '\0';
     184         [ #  # ]:           0 :                 if (!prev_err)
     185                 :             :                 {
     186                 :           0 :                     TREAT_ERR(err, 0, "Too long SData line truncated");
     187                 :             :                 }
     188                 :             :             }
     189                 :             :             else
     190                 :             :             {
     191                 :             :                 /* allow long lines in SDF data. 9-29-00 DCh */
     192                 :           0 :                 line[MOL_FMT_MAXLINELEN] = '\0';
     193                 :             :             }
     194                 :             :         }
     195                 :             : 
     196                 :          11 :         n_blank_lines += (*line == '\0');
     197                 :             : 
     198   [ +  -  -  -  :          11 :         switch (current_state)
                -  -  - ]
     199                 :             :         {
     200                 :          11 :             case SDF_START:
     201                 :             :             case SD_FMT_END_OF_DATA_ITEM:
     202                 :             :             case SDF_EMPTY_LINE: /* Added 9-25-97 DCh */
     203                 :             : 
     204         [ +  - ]:          11 :                 if (!strcmp(line, SD_FMT_END_OF_DATA))
     205                 :             :                 {
     206                 :          11 :                     current_state = SD_FMT_END_OF_DATA_BLOCK;
     207                 :             :                 }
     208         [ #  # ]:           0 :                 else if ('>' == *line)
     209                 :             :                 {
     210   [ #  #  #  #  :           0 :                     current_state = (wait_for_name || wait_for_comment || wait_for_CAS || wait_for_user) ? SDFileIdentifyLabel( line, pSdfLabel ) : SDF_DATA_HEADER;
             #  #  #  # ]
     211                 :             :                 }
     212         [ #  # ]:           0 :                 else if (*line == '\0')
     213                 :             :                 {
     214                 :             :                     /* Added 9-25-97 DCh */
     215                 :             :                     /* Relax the strictness: Allow more than 1 empty line. */
     216                 :           0 :                     current_state = SDF_EMPTY_LINE;
     217                 :             :                 }
     218         [ #  # ]:           0 :                 else if (!prev_err)
     219                 :             :                 {
     220         [ #  # ]:           0 :                     TREAT_ERR(err, 3, "Unexpected SData header line:"); /* djb-rwth: addressing coverity ID #499557 -- TREAT_ERR properly used */
     221                 :           0 :                     dotify_non_printable_chars(line);
     222                 :           0 :                     AddErrorMessage(pStrErr, line);
     223                 :             :                     /* unexpected contents of data header line */
     224                 :             :                 }
     225                 :             :                 else
     226                 :             :                 {
     227                 :           0 :                     err = 3;
     228                 :             :                 }
     229                 :          11 :                 break;
     230                 :             : 
     231                 :           0 :             case SDF_DATA_HEADER_NAME:
     232                 :             : 
     233   [ #  #  #  # ]:           0 :                 if (wait_for_name && 0 < normalize_string(line))
     234                 :             :                 {
     235                 :           0 :                     wait_for_name = 0;
     236                 :           0 :                     mystrncpy(name, line, lname);
     237                 :             :                 }
     238                 :           0 :                 goto got_data_line;
     239                 :             : 
     240                 :           0 :             case SDF_DATA_HEADER_COMMENT:
     241                 :             : 
     242   [ #  #  #  # ]:           0 :                 if (wait_for_comment && 0 < normalize_string(line))
     243                 :             :                 {
     244                 :           0 :                     wait_for_comment = 0;
     245                 :           0 :                     mystrncpy(comment, line, lcomment);
     246                 :             :                 }
     247                 :           0 :                 goto got_data_line;
     248                 :             : 
     249                 :           0 :             case SDF_DATA_HEADER_USER:
     250                 :             : 
     251   [ #  #  #  # ]:           0 :                 if (wait_for_user && 0 < normalize_string(line))
     252                 :             :                 {
     253                 :           0 :                     wait_for_user = 0;
     254                 :           0 :                     mystrncpy(pSdfValue, line, MAX_SDF_VALUE + 1);
     255                 :             : 
     256   [ #  #  #  # ]:           0 :                     if (CAS_num_is_user && wait_for_CAS)
     257                 :             :                     {
     258                 :           0 :                         *CAS_num = SDFileExtractCASNo(line);
     259                 :           0 :                         wait_for_CAS = (0LU == *CAS_num);
     260                 :             :                     }
     261                 :             :                 }
     262                 :           0 :                 goto got_data_line;
     263                 :             : 
     264                 :           0 :             case SDF_DATA_HEADER_CAS:
     265                 :             : 
     266   [ #  #  #  # ]:           0 :                 if (wait_for_CAS && 0 < normalize_string(line))
     267                 :             :                 {
     268                 :           0 :                     *CAS_num = SDFileExtractCASNo(line);
     269                 :           0 :                     wait_for_CAS = (0LU == *CAS_num);
     270                 :             :                 }
     271                 :           0 :                 goto got_data_line;
     272                 :             : 
     273                 :             :             case SDF_DATA_HEADER:
     274                 :             :             case SDF_DATA_LINE:
     275                 :             : 
     276                 :           0 :             got_data_line:
     277         [ #  # ]:           0 :                 current_state = *line ? SDF_DATA_LINE : SD_FMT_END_OF_DATA_ITEM;
     278                 :           0 :                 break;
     279                 :             :         }
     280                 :             :     }
     281                 :             : 
     282   [ +  -  +  +  :         173 :     if (!err && SD_FMT_END_OF_DATA_BLOCK != current_state && NULL == p)
                   +  - ]
     283                 :             :     {
     284                 :             :         ; /* err = 4; */ /* unexpected end of file: missing $$$$ */
     285                 :             :     }
     286                 :             : 
     287   [ -  +  -  -  :          11 :     else if (err && (n_blank_lines == n_lines && *line == '\0'))
                   -  - ]
     288                 :             :     {
     289                 :             :         /* empty lines -- do not know when this can happen */
     290                 :           0 :         err = 5;
     291                 :             :     }
     292                 :             : 
     293   [ -  +  -  -  :         173 :     if (err && err != 5 && current_state != SD_FMT_END_OF_DATA_BLOCK && p)
             -  -  -  - ]
     294                 :             :     {
     295                 :             :         /*  bypass up to $$$$ */
     296         [ #  # ]:           0 :         while ((p = inchi_fgetsLf(line, line_len, inp_file)) &&
     297         [ #  # ]:           0 :                memcmp(line, SD_FMT_END_OF_DATA, 4))
     298                 :             :         {
     299                 :             :             ;
     300                 :             :         }
     301         [ #  # ]:           0 :         if (p)
     302                 :             :         {
     303                 :             :             /*  arrived to $$$$; non-fatal */
     304                 :           0 :             err = 9;
     305         [ #  # ]:           0 :             if (!bNoWarnings)
     306                 :             :             {
     307                 :           0 :                 WarningMessage(pStrErr, "Bypassing to next structure");
     308                 :             :             }
     309                 :             :         }
     310                 :             :     }
     311                 :             : 
     312                 :         173 :     return err;
     313                 :             : }
     314                 :             : 
     315                 :             : /****************************************************************************/
     316                 :           0 : int SDFileIdentifyLabel(char *inp_line, const char *pSdfLabel)
     317                 :             : {
     318                 :             :     char line[MOL_FMT_MAXLINELEN];
     319                 :             :     char *p, *q;
     320                 :           0 :     int i, j, len, tmp1 = 0, tmp2 = 0, cnd = 0;
     321                 :             : 
     322         [ #  # ]:           0 :     if ((p = strchr(inp_line, '<')) &&
     323         [ #  # ]:           0 :         (q = strchr(p, '>')) &&
     324   [ #  #  #  # ]:           0 :         (len = q - p - 1) > 0 && len < (int)sizeof(line))
     325                 :             :     {
     326                 :           0 :         memcpy(line, p + 1, len);
     327                 :           0 :         line[len] = '\0';
     328                 :             : 
     329   [ #  #  #  # ]:           0 :         for (i = 0; (i < len) && (cnd == 0); i++)
     330                 :             :         {
     331         [ #  # ]:           0 :             if (isspace(UCINT line[i]))
     332                 :             :             {
     333                 :           0 :                 tmp1 = i;
     334                 :           0 :                 cnd = 1;
     335                 :             :             }
     336                 :             :         }
     337                 :             : 
     338                 :           0 :         cnd = 0;
     339                 :             : 
     340   [ #  #  #  # ]:           0 :         for (j = len - 1; (j >= tmp1) && (cnd == 0); j--)
     341                 :             :         {
     342         [ #  # ]:           0 :             if (isspace(UCINT line[j]))
     343                 :             :             {
     344                 :           0 :                 tmp2 = j;
     345                 :           0 :                 cnd = 1;
     346                 :             :             }
     347                 :             :         }
     348                 :             : 
     349                 :           0 :         len = tmp2 - tmp1 + 1;
     350                 :           0 :         p = line + tmp1;
     351                 :             : 
     352   [ #  #  #  #  :           0 :         if (pSdfLabel && pSdfLabel[0] && len == (int)strlen(pSdfLabel) && !inchi_memicmp(p, pSdfLabel, len))
             #  #  #  # ]
     353                 :             :         {
     354                 :           0 :             return SDF_DATA_HEADER_USER;
     355                 :             :         }
     356                 :             : 
     357   [ #  #  #  # ]:           0 :         if (len == sizeof(sdf_data_hdr_name) - 1 && !inchi_memicmp(p, sdf_data_hdr_name, len))
     358                 :             :         {
     359                 :           0 :             return SDF_DATA_HEADER_NAME;
     360                 :             :         }
     361                 :             : 
     362   [ #  #  #  # ]:           0 :         if (len == sizeof(sdf_data_hdr_comm) - 1 && !inchi_memicmp(p, sdf_data_hdr_comm, len))
     363                 :             :         {
     364                 :           0 :             return SDF_DATA_HEADER_COMMENT;
     365                 :             :         }
     366                 :             : 
     367         [ #  # ]:           0 :         if (!inchi_memicmp(p, "CAS", 3))
     368                 :             :         {
     369                 :           0 :             return SDF_DATA_HEADER_CAS;
     370                 :             :         }
     371                 :             :     }
     372                 :             : 
     373                 :           0 :     return SDF_DATA_HEADER;
     374                 :             : }
     375                 :             : 
     376                 :             : /****************************************************************************/
     377                 :           0 : unsigned long SDFileExtractCASNo(char *line)
     378                 :             : {
     379                 :             :     int i, j;
     380                 :             : 
     381                 :           0 :     i = line[0] == '-' ? 1 : 0;
     382                 :             : 
     383         [ #  # ]:           0 :     for (j = i; line[i]; i++)
     384                 :             :     {
     385         [ #  # ]:           0 :         if (isdigit(UCINT line[i]))
     386                 :             :         {
     387                 :           0 :             line[j++] = line[i];
     388                 :             :         }
     389         [ #  # ]:           0 :         else if (line[i] != '-')
     390                 :             :         {
     391                 :           0 :             break;
     392                 :             :         }
     393                 :             :     }
     394                 :             : 
     395                 :           0 :     line[j] = '\0';
     396                 :             : 
     397                 :           0 :     return strtoul(line, NULL, 10);
     398                 :             : }
     399                 :             : 
     400                 :             : /****************************************************************************
     401                 :             :  NUM_LISTS - dynamically growing array of int lists
     402                 :             : ****************************************************************************/
     403                 :         304 : int NumLists_Alloc(NUM_LISTS *num_lists, int nlists)
     404                 :             : {
     405         [ +  - ]:         304 :     if (num_lists)
     406                 :             :     {
     407         [ +  - ]:         304 :         if ((num_lists->lists = (int **)inchi_calloc(nlists, sizeof(int *)))) /* djb-rwth: addressing LLVM warning */
     408                 :             :         {
     409                 :         304 :             num_lists->increment =
     410                 :         304 :                 num_lists->allocated = nlists;
     411                 :         304 :             return 0; /*  ok */
     412                 :             :         }
     413                 :             :     }
     414                 :             : 
     415                 :           0 :     return -1; /*  error */
     416                 :             : }
     417                 :             : 
     418                 :             : /****************************************************************************/
     419                 :           2 : int NumLists_ReAlloc(NUM_LISTS *num_lists)
     420                 :             : {
     421         [ +  - ]:           2 :     if (num_lists)
     422                 :             :     {
     423   [ +  -  +  -  :           2 :         if (num_lists->lists && num_lists->allocated > 0 && num_lists->increment > 0)
                   +  - ]
     424                 :             :         {
     425                 :           2 :             void *p = num_lists->lists;
     426                 :           2 :             if ((num_lists->lists =
     427         [ +  - ]:           2 :                      (int **)inchi_calloc((long long)num_lists->allocated + (long long)num_lists->increment, sizeof(int *)))) /* djb-rwth: cast operators added; addressing LLVM warning */
     428                 :             :             {
     429                 :           2 :                 memcpy(num_lists->lists, p, num_lists->used * sizeof(num_lists->lists[0]));
     430         [ +  - ]:           2 :                 inchi_free(p);
     431                 :           2 :                 num_lists->allocated += num_lists->increment;
     432                 :           2 :                 return 0; /*  ok */
     433                 :             :             }
     434                 :             :         }
     435                 :             :     }
     436                 :             : 
     437                 :           0 :     return -1; /*  error */
     438                 :             : }
     439                 :             : 
     440                 :             : /****************************************************************************/
     441                 :         104 : int NumLists_Append(NUM_LISTS *num_lists, int *list)
     442                 :             : {
     443         [ +  - ]:         104 :     if (num_lists)
     444                 :             :     {
     445         [ +  + ]:         104 :         if (num_lists->used + 1 > num_lists->allocated)
     446                 :             :         {
     447                 :             :             /* need to expand buffer */
     448         [ -  + ]:           2 :             if (NumLists_ReAlloc(num_lists))
     449                 :             :             {
     450                 :           0 :                 return -1; /*  error */
     451                 :             :             }
     452                 :             :         }
     453                 :         104 :         num_lists->lists[num_lists->used++] = list;
     454                 :         104 :         return 0;
     455                 :             :     }
     456                 :             : 
     457                 :           0 :     return -1;
     458                 :             : }
     459                 :             : 
     460                 :             : /****************************************************************************/
     461                 :         304 : void NumLists_Free(NUM_LISTS *num_lists)
     462                 :             : {
     463         [ +  - ]:         304 :     if (num_lists)
     464                 :             :     {
     465                 :             :         int i;
     466         [ +  + ]:         408 :         for (i = 0; i < num_lists->used; i++)
     467         [ +  - ]:         104 :             inchi_free(num_lists->lists[i]); /* djb-rwth: unresolved issue -- revision required? -- false positive as this function just does the clean-up job */
     468         [ +  - ]:         304 :         inchi_free(num_lists->lists);
     469                 :         304 :         memset(num_lists, 0, sizeof(*num_lists)); /* djb-rwth: memset_s C11/Annex K variant? */
     470                 :             :     }
     471                 :         304 : }
     472                 :             : 
     473                 :             : /****************************************************************************
     474                 :             :  INT_ARRAY - dynamically growing array of int
     475                 :             : ****************************************************************************/
     476                 :             : 
     477                 :             : /****************************************************************************
     478                 :             :  Allocate new array, return 0 if OK, -1 otherwise
     479                 :             : ****************************************************************************/
     480                 :        1728 : int IntArray_Alloc(INT_ARRAY *items, int nitems)
     481                 :             : {
     482         [ +  - ]:        1728 :     if ((items->item = (int *)inchi_calloc(nitems, sizeof(int)))) /* djb-rwth: addressing LLVM warning */
     483                 :             :     {
     484                 :        1728 :         items->increment = items->allocated = nitems;
     485                 :        1728 :         items->used = 0;
     486                 :        1728 :         return 0;
     487                 :             :     }
     488                 :             : 
     489                 :           0 :     return -1;
     490                 :             : }
     491                 :             : 
     492                 :             : /****************************************************************************
     493                 :             :  Expand array, return 0 if OK, -1 otherwise
     494                 :             : ****************************************************************************/
     495                 :           1 : int IntArray_ReAlloc(INT_ARRAY *items)
     496                 :             : {
     497         [ +  - ]:           1 :     if (items)
     498                 :             :     {
     499   [ +  -  +  -  :           1 :         if (items->item && items->allocated > 0 && items->increment > 0)
                   +  - ]
     500                 :             :         {
     501                 :           1 :             void *p = items->item;
     502                 :           1 :             if ((items->item =
     503         [ +  - ]:           1 :                      (int *)inchi_calloc((long long)items->allocated + (long long)items->increment, sizeof(items->item[0])))) /* djb-rwth: cast operators added; addressing LLVM warning */
     504                 :             :             {
     505                 :           1 :                 memcpy(items->item, p, items->used * sizeof(items->item[0]));
     506         [ +  - ]:           1 :                 inchi_free(p);
     507                 :           1 :                 items->allocated += items->increment;
     508                 :           1 :                 return 0;
     509                 :             :             }
     510         [ #  # ]:           0 :             inchi_free(p);
     511                 :             :         }
     512                 :             :     }
     513                 :             : 
     514                 :           0 :     return -1;
     515                 :             : }
     516                 :             : 
     517                 :             : /****************************************************************************
     518                 :             :  Push new item to the end of array
     519                 :             : ****************************************************************************/
     520                 :          78 : int IntArray_Append(INT_ARRAY *items, int new_item)
     521                 :             : {
     522         [ +  - ]:          78 :     if (items)
     523                 :             :     {
     524         [ +  + ]:          78 :         if (items->used + 1 > items->allocated)
     525                 :             :         {
     526                 :             :             /* need to expand buffer */
     527         [ -  + ]:           1 :             if (IntArray_ReAlloc(items))
     528                 :             :             {
     529                 :           0 :                 return -1;
     530                 :             :             }
     531                 :             :         }
     532                 :          78 :         items->item[items->used++] = new_item;
     533                 :          78 :         return 0;
     534                 :             :     }
     535                 :             : 
     536                 :           0 :     return -1;
     537                 :             : }
     538                 :             : 
     539                 :             : /****************************************************************************
     540                 :             : Push new item to the end of array only if it is absent there
     541                 :             : ****************************************************************************/
     542                 :           0 : int IntArray_AppendIfAbsent(INT_ARRAY *items, int new_item)
     543                 :             : {
     544         [ #  # ]:           0 :     if (!is_in_the_ilist(items->item, new_item, items->used))
     545                 :             :     {
     546                 :           0 :         return IntArray_Append(items, new_item);
     547                 :             :     }
     548                 :           0 :     return 0;
     549                 :             : }
     550                 :             : 
     551                 :             : /****************************************************************************/
     552                 :          30 : void IntArray_DebugPrint(INT_ARRAY *items)
     553                 :             : {
     554         [ +  - ]:          30 :     if (items)
     555                 :             :     {
     556                 :             :         int i;
     557         [ -  + ]:          30 :         if (items->used > 0)
     558                 :             :         {
     559         [ #  # ]:           0 :             for (i = 0; i < items->used - 1; i++)
     560                 :             :             {
     561                 :             :                 ITRACE_("%-d, ", items->item[i]);
     562                 :             :             }
     563                 :             :             ITRACE_("%-d\n", items->item[items->used - 1]);
     564                 :             :         }
     565                 :             :         else
     566                 :             :         {
     567                 :             :             ; /*ITRACE_( "[None]\n");*/
     568                 :             :         }
     569                 :             :     }
     570                 :          30 : }
     571                 :             : 
     572                 :             : /****************************************************************************/
     573                 :           1 : void IntArray_Reset(INT_ARRAY *items)
     574                 :             : {
     575                 :           1 :     items->used = 0;
     576                 :           1 :     return;
     577                 :             : }
     578                 :             : 
     579                 :             : /****************************************************************************/
     580                 :        1728 : void IntArray_Free(INT_ARRAY *items)
     581                 :             : {
     582         [ +  - ]:        1728 :     if (items)
     583                 :             :     {
     584         [ +  - ]:        1728 :         if (items->item)
     585                 :             :         {
     586         [ +  - ]:        1728 :             inchi_free(items->item);
     587                 :             :         }
     588                 :        1728 :         memset(items, 0, sizeof(*items)); /* djb-rwth: memset_s C11/Annex K variant? */
     589                 :             :     }
     590                 :        1728 :     return;
     591                 :             : }
     592                 :             : 
     593                 :             : /****************************************************************************
     594                 :             :     MOL_FMT_SGROUPS - dynamically growing array of pointers to SGroups
     595                 :             : ****************************************************************************/
     596                 :             : 
     597                 :             : /*
     598                 :             :     SGroup
     599                 :             : */
     600                 :             : 
     601                 :             : /****************************************************************************
     602                 :             :  Allocate new array Sgroup, return 0 if OK, -1 otherwise
     603                 :             : ****************************************************************************/
     604                 :          13 : int MolFmtSgroup_Create(MOL_FMT_SGROUP **sgroup, int id, int type)
     605                 :             : {
     606                 :          13 :     *sgroup = (MOL_FMT_SGROUP *)inchi_calloc(1, sizeof(MOL_FMT_SGROUP));
     607         [ +  - ]:          13 :     if (*sgroup)
     608                 :             :     {
     609   [ +  -  -  + ]:          26 :         if (IntArray_Alloc(&((*sgroup)->alist), 8) ||
     610                 :          13 :             IntArray_Alloc(&((*sgroup)->blist), 8))
     611                 :             :         {
     612                 :           0 :             MolFmtSgroup_Free(*sgroup);
     613                 :           0 :             return -1;
     614                 :             :         }
     615                 :          13 :         (*sgroup)->id = id;
     616                 :          13 :         (*sgroup)->type = type;
     617                 :             : 
     618                 :          13 :         (*sgroup)->subtype = 0;
     619                 :          13 :         (*sgroup)->conn = 0;
     620                 :          13 :         (*sgroup)->label = 0;
     621                 :             : 
     622                 :          13 :         return 0;
     623                 :             :     }
     624                 :           0 :     return -1;
     625                 :             : }
     626                 :             : 
     627                 :             : /****************************************************************************/
     628                 :          13 : void MolFmtSgroup_Free(MOL_FMT_SGROUP *sgroup)
     629                 :             : {
     630         [ +  - ]:          13 :     if (sgroup)
     631                 :             :     {
     632                 :          13 :         IntArray_Free(&(sgroup->alist));
     633                 :          13 :         IntArray_Free(&(sgroup->blist));
     634         [ +  - ]:          13 :         inchi_free(sgroup);
     635                 :             :     }
     636                 :          13 : }
     637                 :             : 
     638                 :             : /*
     639                 :             :     SGroups
     640                 :             : */
     641                 :             : 
     642                 :             : /****************************************************************************
     643                 :             :  Allocate new array of Sgroups, return 0 if OK, -1 otherwise
     644                 :             : ****************************************************************************/
     645                 :         173 : int MolFmtSgroups_Alloc(MOL_FMT_SGROUPS *sgroups, int nsgroups)
     646                 :             : {
     647         [ +  - ]:         173 :     if (sgroups)
     648                 :             :     {
     649         [ +  - ]:         173 :         if (NULL != (sgroups->group = (MOL_FMT_SGROUP **)inchi_calloc(nsgroups, sizeof(MOL_FMT_SGROUP *))))
     650                 :             :         {
     651                 :             :             /* ITRACE_( "\nAllocated sgroups->group at %-p \n", sgroups->group ); */
     652                 :         173 :             sgroups->increment = sgroups->allocated = nsgroups;
     653                 :         173 :             return 0;
     654                 :             :         }
     655                 :             :     }
     656                 :             : 
     657                 :           0 :     return -1;
     658                 :             : }
     659                 :             : 
     660                 :             : /****************************************************************************
     661                 :             :  Expand array of Sgroups, return 0 if OK, -1 otherwise
     662                 :             : ****************************************************************************/
     663                 :           0 : int MolFmtSgroups_ReAlloc(MOL_FMT_SGROUPS *sgroups)
     664                 :             : {
     665         [ #  # ]:           0 :     if (sgroups)
     666                 :             :     {
     667   [ #  #  #  #  :           0 :         if (sgroups->group && sgroups->allocated > 0 && sgroups->increment > 0)
                   #  # ]
     668                 :             :         {
     669                 :           0 :             void *p = sgroups->group;
     670         [ #  # ]:           0 :             if ((sgroups->group = (MOL_FMT_SGROUP **)inchi_calloc( (long long)sgroups->allocated + (long long)sgroups->increment,
     671                 :             :                 sizeof( sgroups->group[0] ) ))) /* djb-rwth: cast operators added; addressing LLVM warning */
     672                 :             :             {
     673                 :           0 :                 memcpy(sgroups->group, p, sgroups->used * sizeof(sgroups->group[0]));
     674         [ #  # ]:           0 :                 inchi_free(p);
     675                 :           0 :                 sgroups->allocated += sgroups->increment;
     676                 :           0 :                 return 0; /*  ok */
     677                 :             :             }
     678                 :             :         }
     679                 :             :     }
     680                 :             : 
     681                 :           0 :     return -1;
     682                 :             : }
     683                 :             : 
     684                 :             : /****************************************************************************/
     685                 :          13 : int MolFmtSgroups_Append(MOL_FMT_SGROUPS *sgroups, int id, int type)
     686                 :             : {
     687         [ +  - ]:          13 :     if (sgroups)
     688                 :             :     {
     689                 :             :         /* Make new Sgroup */
     690                 :          13 :         MOL_FMT_SGROUP *sgroup = NULL;
     691         [ -  + ]:          13 :         if (0 != MolFmtSgroup_Create(&sgroup, id, type))
     692                 :             :         {
     693                 :           0 :             return -1;
     694                 :             :         }
     695                 :             :         /* Add new created Sgroup to Sgroups */
     696         [ -  + ]:          13 :         if (sgroups->used + 1 > sgroups->allocated)
     697                 :             :         {
     698                 :             :             /* expand buffer */
     699         [ #  # ]:           0 :             if (MolFmtSgroups_ReAlloc(sgroups))
     700                 :             :             {
     701                 :           0 :                 MolFmtSgroup_Free(sgroup); /* djb-rwth: avoiding memory leak */
     702                 :           0 :                 return -1; /*  no RAM */
     703                 :             :             }
     704                 :             :         }
     705                 :          13 :         sgroups->group[sgroups->used++] = sgroup;
     706                 :             : 
     707                 :             :         /*
     708                 :             :         {
     709                 :             :         int num = sgroups->used-1;
     710                 :             :         printf("\nCreated/added Sgroup: id=%-d ( num in Sgroups=%-d ) of type=%-d \n", sgroups->group[num]->id, num, sgroups->group[num]->type );
     711                 :             :         }
     712                 :             :         */
     713                 :             : 
     714                 :          13 :         return 0;
     715                 :             :     }
     716                 :             : 
     717                 :           0 :     return -1;
     718                 :             : }
     719                 :             : 
     720                 :             : /****************************************************************************/
     721                 :         175 : void MolFmtSgroups_Free(MOL_FMT_SGROUPS *sgroups)
     722                 :             : {
     723         [ +  - ]:         175 :     if (sgroups)
     724                 :             :     {
     725                 :             :         int i;
     726         [ +  + ]:         188 :         for (i = 0; i < sgroups->used; i++)
     727                 :             :         {
     728                 :          13 :             MolFmtSgroup_Free(sgroups->group[i]);
     729                 :             :         }
     730                 :             : 
     731                 :             :         /* ITRACE_( "\nAbout to free sgroups->group at %-p\n", sgroups->group ); */
     732         [ +  + ]:         175 :         inchi_free(sgroups->group);
     733                 :             : 
     734                 :         175 :         memset(sgroups, 0, sizeof(MOL_FMT_SGROUPS)); /* djb-rwth: memset_s C11/Annex K variant? */
     735                 :             :     }
     736                 :         175 : }
     737                 :             : 
     738                 :             : /****************************************************************************/
     739                 :          39 : int MolFmtSgroups_GetIndexBySgroupId(int id, MOL_FMT_SGROUPS *sgroups)
     740                 :             : {
     741                 :             :     int i;
     742         [ +  + ]:          39 :     for (i = 0; i < sgroups->used; i++)
     743                 :             :     {
     744         [ +  - ]:          32 :         if (sgroups->group[i]->id == id)
     745                 :             :         {
     746                 :          32 :             return i;
     747                 :             :         }
     748                 :             :     }
     749                 :           7 :     return -1;
     750                 :             : }
     751                 :             : 
     752                 :             : /****************************************************************************
     753                 :             :  GH #75: print one V3000 token, e.g. " ATOMS=(2"; it starts with a space.
     754                 :             :  Lines would exceed V3000_MAX_LINE: end with " -", continue on a new
     755                 :             :  "M  V30 " line (the reader joins them as is) without the leading space.
     756                 :             :  A token longer than a line is cut and continued with "-" alone, so no
     757                 :             :  whitespace is inserted into it.
     758                 :             : ****************************************************************************/
     759                 :           0 : static void V3000Put(INCHI_IOSTREAM *fcb, int *col, const char *token)
     760                 :             : {
     761                 :             :     char part[V3000_MAX_LINE];
     762                 :           0 :     int len = (int)strlen(token);
     763                 :             : 
     764         [ #  # ]:           0 :     if (*col + len + 2 > V3000_MAX_LINE)
     765                 :             :     {
     766                 :           0 :         inchi_ios_print_nodisplay(fcb, " -\nM  V30 ");
     767                 :           0 :         *col = V3000_PREFIX_LEN;
     768                 :           0 :         token++;
     769                 :           0 :         len--;
     770                 :             :     }
     771                 :             : 
     772                 :             :     /* Keep room for a trailing " -" on every line */
     773         [ #  # ]:           0 :     while (*col + len + 2 > V3000_MAX_LINE)
     774                 :             :     {
     775                 :           0 :         int n = V3000_MAX_LINE - 1 - *col;
     776                 :             : 
     777                 :           0 :         memcpy(part, token, n);
     778                 :           0 :         part[n] = 0;
     779                 :           0 :         inchi_ios_print_nodisplay(fcb, "%s-\nM  V30 ", part);
     780                 :           0 :         *col = V3000_PREFIX_LEN;
     781                 :           0 :         token += n;
     782                 :           0 :         len -= n;
     783                 :             :     }
     784                 :             : 
     785                 :           0 :     inchi_ios_print_nodisplay(fcb, "%s", token);
     786                 :           0 :     *col += len;
     787                 :           0 : }
     788                 :             : 
     789                 :             : /****************************************************************************
     790                 :             :  GH #75: print V3000 index list, e.g. " ATOMS=(3 1 2 4)"
     791                 :             : ****************************************************************************/
     792                 :           0 : static void V3000PutList(INCHI_IOSTREAM *fcb, int *col, const char *key,
     793                 :             :                          int n, const int *items)
     794                 :             : {
     795                 :             :     char token[64];
     796                 :             :     int k;
     797                 :             : 
     798         [ #  # ]:           0 :     sprintf(token, " %s=(%d%s", key, n, n ? "" : ")");
     799                 :           0 :     V3000Put(fcb, col, token);
     800         [ #  # ]:           0 :     for (k = 0; k < n; k++)
     801                 :             :     {
     802         [ #  # ]:           0 :         sprintf(token, " %d%s", items[k], k == n - 1 ? ")" : "");
     803                 :           0 :         V3000Put(fcb, col, token);
     804                 :             :     }
     805                 :           0 : }
     806                 :             : 
     807                 :             : /****************************************************************************
     808                 :             :  GH #75: polymer unit types written, as V2000 M  STY
     809                 :             : ****************************************************************************/
     810                 :           0 : static int IsV3000Sgroup(const OAD_PolymerUnit *u)
     811                 :             : {
     812   [ #  #  #  # ]:           0 :     return MOL_FMT_M_STY_SRU <= u->type && u->type <= MOL_FMT_M_STY_MER;
     813                 :             : }
     814                 :             : 
     815                 :             : /****************************************************************************
     816                 :             :  GH #75: write polymer units as V3000 Sgroups, mirroring V2000
     817                 :             :  M  STY/SLB/SST/SCN/SAL/SBL/SDI, e.g.
     818                 :             :      M  V30 1 SRU 1 ATOMS=(2 2 3) XBONDS=(2 1 3) BRKXYZ=(9 ...) CONNECT=HT
     819                 :             : ****************************************************************************/
     820                 :           0 : static int WriteV3000Sgroups(const OAD_Polymer *polymer,
     821                 :             :                              INCHI_IOSTREAM *fcb,
     822                 :             :                              const INT_ARRAY *written_bond_ends,
     823                 :             :                              const inp_ATOM *at,
     824                 :             :                              const int *iso_h_first)
     825                 :             : {
     826                 :             :     static const char *sty[] = {"NON", "SRU", "MON", "COP", "MOD", "CRO", "MER"};
     827                 :             :     static const char *sst[] = {"NON", "ALT", "RAN", "BLO"};
     828                 :             :     static const char *con[] = {"NON", "HT", "HH", "EU"};
     829                 :             :     char token[128];
     830                 :             :     int j, k, m, wb, col, nb, na;
     831                 :             :     int *bonds, *atoms;
     832                 :             : 
     833                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 BEGIN SGROUP\n");
     834         [ #  # ]:           0 :     for (j = 0; j < polymer->n; j++)
     835                 :             :     {
     836                 :           0 :         const OAD_PolymerUnit *u = polymer->units[j];
     837                 :           0 :         double lo = -1.0 * (j + 1), hi = 1.0 * (j + 1); /* as V2000 SDI */
     838                 :             : 
     839         [ #  # ]:           0 :         if (!IsV3000Sgroup(u))
     840                 :             :         {
     841                 :           0 :             continue;
     842                 :             :         }
     843                 :             : 
     844                 :             :         /* Unit atoms plus the explicit D/T written for them (as V2000,
     845                 :             :            where an isotopic implicit H is part of its atom) */
     846                 :           0 :         na = u->na;
     847         [ #  # ]:           0 :         for (k = 0; k < u->na; k++)
     848                 :             :         {
     849                 :           0 :             na += at[u->alist[k] - 1].num_iso_H[1] + at[u->alist[k] - 1].num_iso_H[2];
     850                 :             :         }
     851         [ #  # ]:           0 :         if (!(atoms = (int *)inchi_calloc(na + 1, sizeof(int))))
     852                 :             :         {
     853                 :           0 :             return _IS_ERROR;
     854                 :             :         }
     855                 :           0 :         memcpy(atoms, u->alist, u->na * sizeof(int));
     856                 :           0 :         na = u->na;
     857         [ #  # ]:           0 :         for (k = 0; k < u->na; k++)
     858                 :             :         {
     859                 :           0 :             int a = u->alist[k] - 1;
     860         [ #  # ]:           0 :             for (m = 0; m < at[a].num_iso_H[1] + at[a].num_iso_H[2]; m++)
     861                 :             :             {
     862                 :           0 :                 atoms[na++] = iso_h_first[a] + m;
     863                 :             :             }
     864                 :             :         }
     865                 :             : 
     866                 :             :         /* Crossing bonds: atom pairs -> written bond numbers */
     867         [ #  # ]:           0 :         if (!(bonds = (int *)inchi_calloc(u->nb + 1, sizeof(int))))
     868                 :             :         {
     869         [ #  # ]:           0 :             inchi_free(atoms);
     870                 :           0 :             return _IS_ERROR;
     871                 :             :         }
     872                 :           0 :         nb = 0;
     873         [ #  # ]:           0 :         for (k = 0; k < u->nb; k++)
     874                 :             :         {
     875                 :           0 :             int a1 = u->blist[2 * k], a2 = u->blist[2 * k + 1];
     876         [ #  # ]:           0 :             for (wb = 0; wb < written_bond_ends->used / 2; wb++)
     877                 :             :             {
     878                 :           0 :                 int e1 = written_bond_ends->item[2 * wb], e2 = written_bond_ends->item[2 * wb + 1];
     879   [ #  #  #  #  :           0 :                 if ((a1 == e1 && a2 == e2) || (a2 == e1 && a1 == e2))
             #  #  #  # ]
     880                 :             :                 {
     881                 :           0 :                     bonds[nb++] = wb + 1;
     882                 :           0 :                     break;
     883                 :             :                 }
     884                 :             :             }
     885                 :             :         }
     886                 :             : 
     887                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  V30");
     888                 :           0 :         col = V3000_PREFIX_LEN - 1;
     889                 :           0 :         sprintf(token, " %d %s %d", u->id, sty[u->type], u->label);
     890                 :           0 :         V3000Put(fcb, &col, token);
     891                 :           0 :         V3000PutList(fcb, &col, "ATOMS", na, atoms);
     892                 :           0 :         V3000PutList(fcb, &col, "XBONDS", nb, bonds);
     893         [ #  # ]:           0 :         inchi_free(atoms);
     894         [ #  # ]:           0 :         inchi_free(bonds);
     895                 :             : 
     896                 :           0 :         sprintf(token, " BRKXYZ=(9 %.4f %.4f 0 %.4f %.4f 0 0 0 0)", lo, lo, lo, hi);
     897                 :           0 :         V3000Put(fcb, &col, token);
     898                 :           0 :         sprintf(token, " BRKXYZ=(9 %.4f %.4f 0 %.4f %.4f 0 0 0 0)", hi, hi, hi, lo);
     899                 :           0 :         V3000Put(fcb, &col, token);
     900                 :             : 
     901   [ #  #  #  # ]:           0 :         if (MOL_FMT_M_CONN_HT <= u->conn && u->conn <= MOL_FMT_M_CONN_EU)
     902                 :             :         {
     903                 :           0 :             sprintf(token, " CONNECT=%s", con[u->conn]);
     904                 :           0 :             V3000Put(fcb, &col, token);
     905                 :             :         }
     906   [ #  #  #  # ]:           0 :         if (MOL_FMT_M_SST_ALT <= u->subtype && u->subtype <= MOL_FMT_M_SST_BLK)
     907                 :             :         {
     908                 :           0 :             sprintf(token, " SUBTYPE=%s", sst[u->subtype]);
     909                 :           0 :             V3000Put(fcb, &col, token);
     910                 :             :         }
     911         [ #  # ]:           0 :         if (u->smt[0])
     912                 :             :         {
     913                 :           0 :             sprintf(token, " LABEL=%.80s", u->smt);
     914                 :           0 :             V3000Put(fcb, &col, token);
     915                 :             :         }
     916                 :           0 :         inchi_ios_print_nodisplay(fcb, "\n");
     917                 :             :     }
     918                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 END SGROUP\n");
     919                 :             : 
     920                 :           0 :     return 0;
     921                 :             : }
     922                 :             : 
     923                 :             : /****************************************************************************
     924                 :             :  GH #75: signed volume of the tetrahedron (v[0], v[1], v[2], p): its sign
     925                 :             :  is the parity of a centre with neighbours v[] and a 4th neighbour at p
     926                 :             : ****************************************************************************/
     927                 :           0 : static double V3000Volume(const double v[][3], const double *p)
     928                 :             : {
     929                 :             :     double a[3], b[3], c[3];
     930                 :             :     int k;
     931                 :             : 
     932         [ #  # ]:           0 :     for (k = 0; k < 3; k++)
     933                 :             :     {
     934                 :           0 :         a[k] = v[0][k] - p[k];
     935                 :           0 :         b[k] = v[1][k] - p[k];
     936                 :           0 :         c[k] = v[2][k] - p[k];
     937                 :             :     }
     938                 :             : 
     939                 :           0 :     return a[0] * (b[1] * c[2] - b[2] * c[1]) -
     940                 :           0 :            a[1] * (b[0] * c[2] - b[2] * c[0]) +
     941                 :           0 :            a[2] * (b[0] * c[1] - b[1] * c[0]);
     942                 :             : }
     943                 :             : 
     944                 :             : /****************************************************************************
     945                 :             :  GH #75: 2D offset of the n-th of nIsoH explicit D/T of atom i.
     946                 :             : 
     947                 :             :  ichister.c reads an implicit H as the unit vector opposite the sum of the
     948                 :             :  neighbours' unit vectors, with wedge bonds from atom i lifted out of the
     949                 :             :  plane. The D/T goes into the gap between neighbours that contains the
     950                 :             :  in-plane part of this vector, at the average bond length; several D/T
     951                 :             :  share the gap evenly. In the gap, no D/T lies on a bond, and for 3
     952                 :             :  neighbours the 2D parity (sign of the tetrahedron volume) is checked and
     953                 :             :  another gap is used if it would differ from the implicit-H reading.
     954                 :             :  0D input, or any bond too short, keeps the D/T on atom i.
     955                 :             : ****************************************************************************/
     956                 :           0 : static void IsoHOffset(const inp_ATOM *at, int i, int n, int nIsoH, double *dx, double *dy)
     957                 :             : {
     958                 :           0 :     const double pi = acos(-1.0), two_pi = 2.0 * pi;
     959                 :           0 :     double v[MAXVAL][3], ang[MAXVAL], h[3] = {0.0, 0.0, 0.0}, p[3] = {0.0, 0.0, 0.0};
     960                 :           0 :     double r = 0.0, target = 0.0, f, best_score = 0.0;
     961                 :           0 :     int j, k, nv = at[i].valence, best = -1;
     962                 :             : 
     963                 :           0 :     *dx = *dy = 0.0;
     964   [ #  #  #  # ]:           0 :     if (!nv || nv > MAXVAL)
     965                 :             :     {
     966                 :           0 :         return;
     967                 :             :     }
     968                 :             : 
     969                 :             :     /* Neighbour unit vectors (z from wedges starting at atom i), angles */
     970         [ #  # ]:           0 :     for (j = 0; j < nv; j++)
     971                 :             :     {
     972                 :           0 :         const inp_ATOM *nb = at + at[i].neighbor[j];
     973                 :           0 :         double x = nb->x - at[i].x, y = nb->y - at[i].y, len = sqrt(x * x + y * y);
     974                 :           0 :         int stereo = at[i].bond_stereo[j];
     975   [ #  #  #  # ]:           0 :         double z = stereo == STEREO_SNGL_UP ? 1.0 : stereo == STEREO_SNGL_DOWN ? -1.0 : 0.0;
     976                 :             : 
     977         [ #  # ]:           0 :         if (len < V3000_MIN_BOND_LENGTH)
     978                 :             :         {
     979                 :           0 :             return;
     980                 :             :         }
     981                 :           0 :         r += len;
     982                 :           0 :         v[j][0] = x / len;
     983                 :           0 :         v[j][1] = y / len;
     984                 :           0 :         v[j][2] = z;
     985         [ #  # ]:           0 :         if (z)
     986                 :             :         {
     987         [ #  # ]:           0 :             for (k = 0; k < 3; k++)
     988                 :             :             {
     989                 :           0 :                 v[j][k] /= sqrt(2.0);
     990                 :             :             }
     991                 :             :         }
     992                 :           0 :         ang[j] = atan2(y, x);
     993         [ #  # ]:           0 :         for (k = 0; k < 3; k++)
     994                 :             :         {
     995                 :           0 :             h[k] -= v[j][k];
     996                 :             :         }
     997                 :             :     }
     998                 :           0 :     r /= nv;
     999                 :             : 
    1000                 :             :     /* Parity that the implicit-H reading of a 3-neighbour centre has */
    1001         [ #  # ]:           0 :     if (nv == 3)
    1002                 :             :     {
    1003                 :           0 :         target = V3000Volume(v, h);
    1004                 :             :     }
    1005                 :             : 
    1006                 :             :     /* Sort neighbour angles */
    1007         [ #  # ]:           0 :     for (j = 1; j < nv; j++)
    1008                 :             :     {
    1009                 :           0 :         double a = ang[j];
    1010   [ #  #  #  # ]:           0 :         for (k = j; k > 0 && ang[k - 1] > a; k--)
    1011                 :             :         {
    1012                 :           0 :             ang[k] = ang[k - 1];
    1013                 :             :         }
    1014                 :           0 :         ang[k] = a;
    1015                 :             :     }
    1016                 :             : 
    1017                 :             :     /* Gap containing the implicit-H direction (widest gap if that is
    1018                 :             :        undefined), unless its parity differs from target */
    1019         [ #  # ]:           0 :     for (k = 0; k < nv; k++)
    1020                 :             :     {
    1021         [ #  # ]:           0 :         double lo = ang[k], hi = k + 1 < nv ? ang[k + 1] : ang[0] + two_pi;
    1022                 :           0 :         double width = hi - lo, mid = 0.5 * (lo + hi), score;
    1023                 :             : 
    1024         [ #  # ]:           0 :         if (width < 1.0e-6)
    1025                 :             :         {
    1026                 :           0 :             continue;
    1027                 :             :         }
    1028         [ #  # ]:           0 :         if (h[0] * h[0] + h[1] * h[1] >= 0.01)
    1029                 :             :         {
    1030                 :           0 :             double d = fabs(fmod(atan2(h[1], h[0]) - mid + 3.0 * pi, two_pi) - pi);
    1031                 :           0 :             score = -d; /* closest to implicit-H direction */
    1032                 :             :         }
    1033                 :             :         else
    1034                 :             :         {
    1035                 :           0 :             score = width;
    1036                 :             :         }
    1037   [ #  #  #  # ]:           0 :         if (nv == 3 && fabs(target) > 1.0e-6)
    1038                 :             :         {
    1039                 :           0 :             p[0] = cos(mid);
    1040                 :           0 :             p[1] = sin(mid);
    1041         [ #  # ]:           0 :             if (V3000Volume(v, p) * target < 0.0)
    1042                 :             :             {
    1043                 :           0 :                 continue;
    1044                 :             :             }
    1045                 :             :         }
    1046   [ #  #  #  # ]:           0 :         if (best < 0 || score > best_score)
    1047                 :             :         {
    1048                 :           0 :             best = k;
    1049                 :           0 :             best_score = score;
    1050                 :             :         }
    1051                 :             :     }
    1052         [ #  # ]:           0 :     if (best < 0)
    1053                 :             :     {
    1054                 :           0 :         return;
    1055                 :             :     }
    1056                 :             : 
    1057                 :             :     /* Spread nIsoH D/T evenly over the gap */
    1058                 :           0 :     f = ang[best];
    1059         [ #  # ]:           0 :     f += (((best + 1 < nv ? ang[best + 1] : ang[0] + two_pi) - f) * (n + 1)) / (nIsoH + 1);
    1060                 :             : 
    1061                 :           0 :     *dx = r * cos(f);
    1062                 :           0 :     *dy = r * sin(f);
    1063         [ #  # ]:           0 :     if (fabs(*dx) < 1.0e-9)
    1064                 :             :     {
    1065                 :           0 :         *dx = 0.0; /* not "-0.0000" */
    1066                 :             :     }
    1067         [ #  # ]:           0 :     if (fabs(*dy) < 1.0e-9)
    1068                 :             :     {
    1069                 :           0 :         *dy = 0.0;
    1070                 :             :     }
    1071                 :             : }
    1072                 :             : 
    1073                 :             : /****************************************************************************
    1074                 :             :  GH #75: write CTAB as V3000; V2000 counts fields are 3 chars wide.
    1075                 :             :  Isotopic implicit H (V2000: alias) become explicit D/T atoms appended
    1076                 :             :  after all atoms; polymer units become V3000 Sgroups.
    1077                 :             : ****************************************************************************/
    1078                 :           0 : static int WriteToSDfileV3000(const ORIG_ATOM_DATA *inp_at_data,
    1079                 :             :                               INCHI_IOSTREAM *fcb,
    1080                 :             :                               const char *name,
    1081                 :             :                               const char *comment,
    1082                 :             :                               int bChiralFlag,
    1083                 :             :                               int bAtomsDT,
    1084                 :             :                               int num_bonds)
    1085                 :             : {
    1086                 :           0 :     int i, j, k, m, ret = 0, nBond = 0, nIsoH = 0, nSgroups = 0, nExtra;
    1087                 :           0 :     int num_atoms = inp_at_data->num_inp_atoms;
    1088                 :           0 :     const inp_ATOM *at = inp_at_data->at;
    1089         [ #  # ]:           0 :     const OAD_Polymer *polymer = inp_at_data->valid_polymer ? inp_at_data->polymer : NULL;
    1090                 :             :     INT_ARRAY written_bond_ends;
    1091                 :             :     int *iso_h_first; /* 1-based number of the first explicit D/T of each atom */
    1092                 :             : 
    1093         [ #  # ]:           0 :     if (!(iso_h_first = (int *)inchi_calloc(num_atoms + 1, sizeof(int))))
    1094                 :             :     {
    1095                 :           0 :         return _IS_ERROR;
    1096                 :             :     }
    1097                 :             :     /* Two atom indices are stored per bond */
    1098   [ #  #  #  # ]:           0 :     if (IntArray_Alloc(&written_bond_ends, num_bonds ? 2 * num_bonds : 255))
    1099                 :             :     {
    1100         [ #  # ]:           0 :         inchi_free(iso_h_first);
    1101                 :           0 :         return _IS_ERROR;
    1102                 :             :     }
    1103                 :             : 
    1104                 :             :     /* Count explicit D/T atoms and Sgroups */
    1105         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1106                 :             :     {
    1107                 :           0 :         nIsoH += at[i].num_iso_H[1] + at[i].num_iso_H[2];
    1108                 :             :     }
    1109   [ #  #  #  # ]:           0 :     for (j = 0; polymer && j < polymer->n; j++)
    1110                 :             :     {
    1111                 :           0 :         nSgroups += IsV3000Sgroup(polymer->units[j]);
    1112                 :             :     }
    1113                 :             : 
    1114                 :             :     /* Header and counts */
    1115         [ #  # ]:           0 :     inchi_ios_print_nodisplay(fcb, "%.80s\n", name ? name : "");
    1116                 :           0 :     inchi_ios_print_nodisplay(fcb, "  InChIV10                                     \n");
    1117         [ #  # ]:           0 :     inchi_ios_print_nodisplay(fcb, "%.80s\n", comment ? comment : "");
    1118                 :           0 :     inchi_ios_print_nodisplay(fcb, "  0  0  0     0  0  0  0  0  0999 V3000\n");
    1119                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 BEGIN CTAB\n");
    1120                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 COUNTS %d %d %d 0 %d\n",
    1121                 :             :                               num_atoms + nIsoH, num_bonds + nIsoH, nSgroups,
    1122                 :             :                               bChiralFlag ? 1 : 0);
    1123                 :             : 
    1124                 :             :     /* Atoms; e.g. "M  V30 1 C 1.5000 0.0000 0.0000 0 CHG=-1 MASS=13" */
    1125                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 BEGIN ATOM\n");
    1126         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1127                 :             :     {
    1128                 :             :         char elname[ATOM_EL_LEN];
    1129   [ #  #  #  #  :           0 :         int nIsotopeH = IS_DEUTERIUM(i) ? 1 : IS_TRITIUM(i) ? 2 : 0;
          #  #  #  #  #  
                #  #  # ]
    1130                 :           0 :         int valence = needed_unusual_el_valence(at[i].el_number, at[i].charge, at[i].radical,
    1131                 :           0 :                                                 at[i].chem_bonds_valence,
    1132                 :           0 :                                                 nBondsValenceInpAt(at + i, NULL, NULL),
    1133                 :           0 :                                                 NUMH(at, i), at[i].valence);
    1134                 :             : 
    1135                 :             :         /* Same symbol as V2000 atoms block */
    1136         [ #  # ]:           0 :         if (nIsotopeH)
    1137                 :             :         {
    1138   [ #  #  #  # ]:           0 :             strcpy(elname, bAtomsDT ? (nIsotopeH == 1 ? "D" : "T") : "H");
    1139                 :             :         }
    1140                 :             :         else
    1141                 :             :         {
    1142                 :           0 :             strncpy(elname, at[i].elname, sizeof(elname) - 1);
    1143                 :           0 :             elname[sizeof(elname) - 1] = 0;
    1144                 :             :         }
    1145   [ #  #  #  # ]:           0 :         if (!strcmp(elname, "Zz") || !strcmp(elname, "Zy"))
    1146                 :             :         {
    1147                 :           0 :             strcpy(elname, "*");
    1148                 :             :         }
    1149                 :             : 
    1150                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  V30 %d %s %.4f %.4f %.4f 0",
    1151                 :           0 :                                   i + 1, elname, at[i].x, at[i].y, at[i].z);
    1152                 :             : 
    1153         [ #  # ]:           0 :         if (at[i].charge)
    1154                 :             :         {
    1155                 :           0 :             inchi_ios_print_nodisplay(fcb, " CHG=%d", (int)at[i].charge);
    1156                 :             :         }
    1157   [ #  #  #  # ]:           0 :         if (ANY_RAD(i))
    1158                 :             :         {
    1159                 :           0 :             inchi_ios_print_nodisplay(fcb, " RAD=%d", (int)at[i].radical);
    1160                 :             :         }
    1161                 :             : 
    1162                 :             :         /* MASS is absolute: element mass + shift, as V2000 M  ISO */
    1163   [ #  #  #  #  :           0 :         if (ANY_ISO(i, bAtomsDT))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1164                 :             :         {
    1165         [ #  # ]:           0 :             int iso = nIsotopeH ? nIsotopeH :
    1166         [ #  # ]:           0 :                       at[i].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 : at[i].iso_atw_diff;
    1167         [ #  # ]:           0 :             iso += get_atomic_mass_from_elnum(nIsotopeH ? 1 : at[i].el_number);
    1168                 :           0 :             inchi_ios_print_nodisplay(fcb, " MASS=%d", iso);
    1169                 :             :         }
    1170                 :             : 
    1171                 :             :         /* V3000 zero valence is -1 (V2000: 15) */
    1172         [ #  # ]:           0 :         if (valence)
    1173                 :             :         {
    1174                 :           0 :             inchi_ios_print_nodisplay(fcb, " VAL=%d", valence < 0 ? -1 : valence);
    1175                 :             :         }
    1176                 :           0 :         inchi_ios_print_nodisplay(fcb, "\n");
    1177                 :             :     }
    1178                 :             : 
    1179                 :             :     /* Explicit D/T next to their heavy atom (zero-length bonds break 2D stereo) */
    1180                 :           0 :     nExtra = num_atoms;
    1181         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1182                 :             :     {
    1183                 :           0 :         iso_h_first[i] = nExtra + 1;
    1184         [ #  # ]:           0 :         for (k = 1; k <= 2; k++)
    1185                 :             :         {
    1186         [ #  # ]:           0 :             for (m = 0; m < at[i].num_iso_H[k]; m++)
    1187                 :             :             {
    1188                 :             :                 double dx, dy;
    1189                 :             : 
    1190                 :           0 :                 IsoHOffset(at, i, m + (k == 2 ? at[i].num_iso_H[1] : 0),
    1191         [ #  # ]:           0 :                            at[i].num_iso_H[1] + at[i].num_iso_H[2], &dx, &dy);
    1192         [ #  # ]:           0 :                 inchi_ios_print_nodisplay(fcb, "M  V30 %d %s %.4f %.4f %.4f 0%s\n", ++nExtra,
    1193         [ #  # ]:           0 :                                           bAtomsDT ? (k == 1 ? "D" : "T") : "H",
    1194         [ #  # ]:           0 :                                           at[i].x + dx, at[i].y + dy, at[i].z,
    1195         [ #  # ]:           0 :                                           bAtomsDT ? "" : (k == 1 ? " MASS=2" : " MASS=3"));
    1196                 :             :             }
    1197                 :             :         }
    1198                 :             :     }
    1199                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 END ATOM\n");
    1200                 :             : 
    1201                 :             :     /* Bonds; e.g. "M  V30 1 1 1 2 CFG=1". The block is omitted when empty,
    1202                 :             :        as the reader (MolfileV3000ReadBondsBlock) expects */
    1203         [ #  # ]:           0 :     if (num_bonds + nIsoH)
    1204                 :             :     {
    1205                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  V30 BEGIN BOND\n");
    1206                 :             :     }
    1207         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1208                 :             :     {
    1209         [ #  # ]:           0 :         for (j = 0; j < at[i].valence; j++)
    1210                 :             :         {
    1211                 :           0 :             unsigned a1 = (unsigned)(i + 1), a2 = (unsigned)(at[i].neighbor[j] + 1);
    1212                 :             :             int cfg;
    1213                 :             : 
    1214         [ #  # ]:           0 :             if (i >= at[i].neighbor[j])
    1215                 :             :             {
    1216                 :           0 :                 continue;
    1217                 :             :             }
    1218                 :             : 
    1219                 :             :             /* Negative stereo means wedge starts at neighbor */
    1220                 :           0 :             k = at[i].bond_stereo[j];
    1221         [ #  # ]:           0 :             if (k < 0)
    1222                 :             :             {
    1223                 :           0 :                 a1 = a2;
    1224                 :           0 :                 a2 = (unsigned)(i + 1);
    1225                 :             :             }
    1226                 :             : 
    1227                 :             :             /* V2000 stereo 1/4/6 (3: double either) -> V3000 CFG 1/2/3 */
    1228                 :           0 :             k = abs(k);
    1229   [ #  #  #  #  :           0 :             cfg = k == 1 ? 1 : k == 6 ? 3 : (k == 3 || k == 4) ? 2 : 0;
             #  #  #  # ]
    1230                 :             : 
    1231                 :           0 :             inchi_ios_print_nodisplay(fcb, "M  V30 %d %u %u %u", ++nBond,
    1232                 :           0 :                                       (unsigned)at[i].bond_type[j], a1, a2);
    1233         [ #  # ]:           0 :             if (cfg)
    1234                 :             :             {
    1235                 :           0 :                 inchi_ios_print_nodisplay(fcb, " CFG=%d", cfg);
    1236                 :             :             }
    1237                 :           0 :             inchi_ios_print_nodisplay(fcb, "\n");
    1238                 :             : 
    1239   [ #  #  #  # ]:           0 :             if (IntArray_Append(&written_bond_ends, a1) ||
    1240                 :           0 :                 IntArray_Append(&written_bond_ends, a2))
    1241                 :             :             {
    1242                 :           0 :                 IntArray_Free(&written_bond_ends);
    1243         [ #  # ]:           0 :                 inchi_free(iso_h_first);
    1244                 :           0 :                 return _IS_ERROR;
    1245                 :             :             }
    1246                 :             :         }
    1247                 :             :     }
    1248                 :             : 
    1249                 :             :     /* Bonds to explicit D/T, in the order written above */
    1250                 :           0 :     nExtra = num_atoms;
    1251         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1252                 :             :     {
    1253         [ #  # ]:           0 :         for (m = 0; m < at[i].num_iso_H[1] + at[i].num_iso_H[2]; m++)
    1254                 :             :         {
    1255                 :           0 :             inchi_ios_print_nodisplay(fcb, "M  V30 %d 1 %d %d\n", ++nBond, i + 1, ++nExtra);
    1256                 :             :         }
    1257                 :             :     }
    1258         [ #  # ]:           0 :     if (num_bonds + nIsoH)
    1259                 :             :     {
    1260                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  V30 END BOND\n");
    1261                 :             :     }
    1262                 :             : 
    1263         [ #  # ]:           0 :     if (nSgroups)
    1264                 :             :     {
    1265                 :           0 :         ret = WriteV3000Sgroups(polymer, fcb, &written_bond_ends, at, iso_h_first);
    1266                 :             :     }
    1267                 :             : 
    1268                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  V30 END CTAB\n");
    1269                 :           0 :     inchi_ios_print_nodisplay(fcb, "M  END\n");
    1270                 :             : 
    1271                 :           0 :     IntArray_Free(&written_bond_ends);
    1272         [ #  # ]:           0 :     inchi_free(iso_h_first);
    1273                 :             : 
    1274                 :           0 :     return ret;
    1275                 :             : }
    1276                 :             : /****************************************************************************/
    1277                 :           0 : int OrigAtData_WriteToSDfile(const ORIG_ATOM_DATA *inp_at_data,
    1278                 :             :                              INCHI_IOSTREAM *fcb,
    1279                 :             :                              const char *name,
    1280                 :             :                              const char *comment,
    1281                 :             :                              int bChiralFlag,
    1282                 :             :                              int bAtomsDT,
    1283                 :             :                              const char *szLabel,
    1284                 :             :                              const char *szValue)
    1285                 :             : {
    1286                 :           0 :     int num_bonds = 0, nNumAddLines = 0, nNumIsoLines = 0, nNumChargeLines = 0,
    1287                 :           0 :         nNumRadicalLines = 0, nNumAliasLines = 0, ret = 0;
    1288                 :             :     INT_ARRAY written_bond_ends;
    1289                 :             :     int i;
    1290                 :             : 
    1291                 :           0 :     memset(&written_bond_ends, 0, sizeof(written_bond_ends));
    1292                 :             : 
    1293                 :             :     /* GH #75: V2000 counts fields hold at most 999 atoms or bonds */
    1294         [ #  # ]:           0 :     for (i = 0; i < inp_at_data->num_inp_atoms; i++)
    1295                 :             :     {
    1296                 :           0 :         num_bonds += inp_at_data->at[i].valence;
    1297                 :             :     }
    1298                 :           0 :     num_bonds /= 2;
    1299   [ #  #  #  # ]:           0 :     if (inp_at_data->num_inp_atoms > V2000_MAX_COUNT || num_bonds > V2000_MAX_COUNT)
    1300                 :             :     {
    1301                 :           0 :         ret = WriteToSDfileV3000(inp_at_data, fcb, name, comment, bChiralFlag,
    1302                 :             :                                  bAtomsDT, num_bonds);
    1303                 :           0 :         goto write_data_items;
    1304                 :             :     }
    1305                 :             : 
    1306                 :           0 :     OrigAtData_WriteToSDfileHeaderAndCountThings((ORIG_ATOM_DATA *)inp_at_data,
    1307                 :             :                                                  fcb, name, comment,
    1308                 :             :                                                  bChiralFlag, bAtomsDT,
    1309                 :             :                                                  szLabel, szValue,
    1310                 :             :                                                  &nNumAliasLines,
    1311                 :             :                                                  &nNumChargeLines,
    1312                 :             :                                                  &nNumRadicalLines,
    1313                 :             :                                                  &nNumIsoLines,
    1314                 :             :                                                  &nNumAddLines,
    1315                 :             :                                                  &num_bonds);
    1316                 :             : 
    1317                 :             :     /* Two atom indices are stored per bond */
    1318   [ #  #  #  # ]:           0 :     if (IntArray_Alloc(&written_bond_ends, num_bonds ? 2 * num_bonds : 255))
    1319                 :             :     {
    1320                 :           0 :         ret = _IS_ERROR;
    1321                 :           0 :         goto exit_function;
    1322                 :             :     }
    1323                 :             : 
    1324                 :           0 :     OrigAtData_WriteToSDfileAtomsBlock(inp_at_data, fcb, name, comment,
    1325                 :             :                                        bAtomsDT, szLabel, szValue);
    1326                 :             : 
    1327         [ #  # ]:           0 :     if (OrigAtData_WriteToSDfileBondsBlock(inp_at_data, fcb, name, comment,
    1328                 :             :                                            szLabel, szValue, &written_bond_ends))
    1329                 :             :     {
    1330                 :           0 :         ret = _IS_ERROR;
    1331                 :           0 :         goto exit_function;
    1332                 :             :     }
    1333                 :             : 
    1334         [ #  # ]:           0 :     if (nNumAddLines)
    1335                 :             :     {
    1336                 :           0 :         OrigAtData_WriteToSDfileAdditionalLines(inp_at_data, fcb, name, comment,
    1337                 :             :                                                 bAtomsDT, szLabel, szValue,
    1338                 :             :                                                 nNumAliasLines, nNumChargeLines,
    1339                 :             :                                                 nNumRadicalLines, nNumIsoLines,
    1340                 :             :                                                 &written_bond_ends);
    1341                 :             :     }
    1342                 :             : 
    1343                 :           0 : write_data_items:
    1344                 :             :     /* Add field with label/ID if applicable and mark the end of record */
    1345   [ #  #  #  # ]:           0 :     if (szValue && szValue[0])
    1346                 :             :     {
    1347   [ #  #  #  # ]:           0 :         if (szLabel && szLabel[0])
    1348                 :             :         {
    1349                 :           0 :             inchi_ios_print_nodisplay(fcb, "> <%s>\n", szLabel);
    1350                 :             :         }
    1351                 :             :         else
    1352                 :             :         {
    1353                 :           0 :             inchi_ios_print_nodisplay(fcb, "> <ID>\n");
    1354                 :             :         }
    1355                 :           0 :         inchi_ios_print_nodisplay(fcb, " %s\n\n", szValue);
    1356                 :             :     }
    1357                 :           0 :     inchi_ios_print_nodisplay(fcb, "$$$$\n");
    1358                 :             : 
    1359                 :           0 : exit_function:
    1360                 :           0 :     IntArray_Free(&written_bond_ends);
    1361                 :             : 
    1362                 :           0 :     return ret;
    1363                 :             : }
    1364                 :             : 
    1365                 :             : /****************************************************************************
    1366                 :             :  OrigAtData : Write To SDfile : Atoms Block
    1367                 :             : ****************************************************************************/
    1368                 :           0 : int OrigAtData_WriteToSDfileHeaderAndCountThings(const ORIG_ATOM_DATA *inp_at_data,
    1369                 :             :                                                  INCHI_IOSTREAM *fcb,
    1370                 :             :                                                  const char *name,
    1371                 :             :                                                  const char *comment,
    1372                 :             :                                                  int bChiralFlag,
    1373                 :             :                                                  int bAtomsDT,
    1374                 :             :                                                  const char *szLabel,
    1375                 :             :                                                  const char *szValue,
    1376                 :             :                                                  int *nNumAliasLines,
    1377                 :             :                                                  int *nNumChargeLines,
    1378                 :             :                                                  int *nNumRadicalLines,
    1379                 :             :                                                  int *nNumIsoLines,
    1380                 :             :                                                  int *nNumAddLines,
    1381                 :             :                                                  int *num_bonds)
    1382                 :             : {
    1383                 :           0 :     int i, ret = 0;
    1384                 :             :     int bAtomNeedsAlias,
    1385                 :           0 :         nNumNecessaryIsoLines = 0,
    1386                 :           0 :         nNumNecessaryChgLines = 0,
    1387                 :           0 :         nNumNecessaryRadLines = 0;
    1388                 :           0 :     int num_atoms = inp_at_data->num_inp_atoms;
    1389                 :           0 :     int bV2000 = SDF_OUTPUT_V2000;
    1390                 :           0 :     const inp_ATOM *at = inp_at_data->at;
    1391                 :             : 
    1392                 :             :     {
    1393                 :             :         char strLocName[82];
    1394                 :           0 :         memset(strLocName, 0, sizeof(strLocName)); /* djb-rwth: memset_s C11/Annex K variant? */
    1395   [ #  #  #  # ]:           0 :         if (name && *name)
    1396                 :             :         {
    1397                 :           0 :             strncpy(strLocName, name, 80);
    1398                 :             :         }
    1399                 :           0 :         inchi_ios_print_nodisplay(fcb, "%s\n", strLocName);
    1400                 :             :     }
    1401                 :             : 
    1402                 :             :     /**********************************************************************/
    1403                 :             :     /**                                                                  **/
    1404                 :             :     /** Important: Atoms with alias cannot have charge, radical          **/
    1405                 :             :     /**            isotope differences are allowed                       **/
    1406                 :             :     /**                                                                  **/
    1407                 :             :     /**            Atoms with alias cannot be abnormal.                  **/
    1408                 :             :     /**                                                                  **/
    1409                 :             :     /** Abnormal atoms are atoms which need M  CHG, M RAD, M  ISO        **/
    1410                 :             :     /**                                                                  **/
    1411                 :             :     /** Output aliased atoms if they have implicit D or T                **/
    1412                 :             :     /**                                                                  **/
    1413                 :             :     /**********************************************************************/
    1414                 :             : 
    1415                 :             :     /*                                    F10.5     F12.5       I6
    1416                 :             :                      IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR
    1417                 :             :     inchi_ios_eprint( fcb,"NISTTRANHP09089809272D 1   1.0         0.0    %6ld\n", lEpa);*/
    1418                 :             :     /*^^^
    1419                 :             :     inchi_ios_print_nodisplay( fcb,"  %s v%s SDfile Output                       \n", INCHI_NAME, INCHI_VERSION);
    1420                 :             : 
    1421                 :             :     Changed 01/10/2009 to conform CTFile specification (by Symyx request)*/
    1422                 :             : 
    1423                 :           0 :     inchi_ios_print_nodisplay(fcb,
    1424                 :             :                               /*   IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR*/
    1425                 :             :                               "  InChIV10                                     \n");
    1426                 :             :     /*y_fprintf(fcb, "  -CPSS-  1213981200n\n");*/
    1427                 :             : 
    1428                 :             :     {
    1429                 :             :         char strLocName[82];
    1430                 :             : 
    1431                 :           0 :         memset(strLocName, 0, sizeof(strLocName)); /* djb-rwth: memset_s C11/Annex K variant? */
    1432   [ #  #  #  # ]:           0 :         if (comment && *comment)
    1433                 :             :         {
    1434                 :           0 :             strncpy(strLocName, comment, 80);
    1435                 :             :         }
    1436                 :           0 :         inchi_ios_print_nodisplay(fcb, "%s\n", strLocName);
    1437                 :             :     }
    1438                 :             : 
    1439                 :           0 :     *num_bonds = 0;
    1440         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1441                 :             :     {
    1442                 :           0 :         (*num_bonds) += at[i].valence;
    1443                 :             :     }
    1444                 :           0 :     (*num_bonds) /= 2;
    1445                 :             : 
    1446                 :             :     /*find if we need "M  CHG" and "M  RAD"*/
    1447         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1448                 :             :     {
    1449         [ #  # ]:           0 :         if ((bAtomNeedsAlias = ALIASED_AT(i))) /* djb-rwth: addressing LLVM warning */
    1450                 :             :         {
    1451                 :             :             /* has isotopic implicit D or T; ignoring pure 1H */
    1452                 :           0 :             (*nNumAliasLines) += 2 * bAtomNeedsAlias;
    1453                 :             :         }
    1454                 :             :         else
    1455                 :             :         {
    1456                 :             :             /* abnormal means atom needs CHG, RAD, or ISO entry */
    1457                 :             :             /* nNumAddLines    += ABNORMAL_AT(i); */
    1458                 :             :             /* nNumIso         += ( 0 == strcmp( at[i].elname, "D" ) || ( 0 == strcmp( at[i].elname, "T" ) || at[i].iso_atw_diff ) ); */
    1459                 :             :             /* nNumAddIso      += at[i].iso_atw_diff && (at[i].iso_atw_diff == 1 || at[i].iso_atw_diff < -3 || at[i].iso_atw_diff > 5 ); */
    1460   [ #  #  #  #  :           0 :             nNumNecessaryIsoLines += ABNORMAL_ISO(i);
                   #  # ]
    1461                 :           0 :             nNumNecessaryChgLines += ABNORMAL_CHG(i);
    1462   [ #  #  #  # ]:           0 :             nNumNecessaryRadLines += ABNORMAL_RAD(i);
    1463   [ #  #  #  #  :           0 :             (*nNumIsoLines) += ANY_ISO(i, bAtomsDT);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    1464                 :           0 :             (*nNumChargeLines) += ANY_CHG(i);
    1465   [ #  #  #  # ]:           0 :             (*nNumRadicalLines) += ANY_RAD(i);
    1466                 :             :         }
    1467                 :             :     }
    1468                 :             : 
    1469                 :           0 :     *nNumChargeLines = (*nNumChargeLines + 7) / 8;
    1470                 :           0 :     *nNumRadicalLines = (*nNumRadicalLines + 7) / 8;
    1471                 :           0 :     *nNumIsoLines = (*nNumIsoLines + 7) / 8;
    1472                 :             : 
    1473         [ #  # ]:           0 :     if (!bV2000)
    1474                 :             :     {
    1475   [ #  #  #  # ]:           0 :         if (!nNumNecessaryRadLines && !nNumNecessaryChgLines)
    1476                 :             :         {
    1477                 :           0 :             *nNumRadicalLines = 0;
    1478                 :           0 :             *nNumChargeLines = 0;
    1479                 :             :         }
    1480         [ #  # ]:           0 :         if (!nNumNecessaryIsoLines)
    1481                 :             :         {
    1482                 :           0 :             *nNumIsoLines = 0;
    1483                 :             :         }
    1484                 :             :     }
    1485                 :             : 
    1486                 :             :     /* recalculate number of added lines */
    1487                 :           0 :     *nNumAddLines = *nNumChargeLines + *nNumRadicalLines + *nNumIsoLines + *nNumAliasLines; /* 1 for M  END*/
    1488                 :             : 
    1489   [ #  #  #  # ]:           0 :     if (*nNumAddLines || bV2000)
    1490                 :             :     {
    1491                 :           0 :         *nNumAddLines += 1; /* add 1 for "M  END" line*/
    1492                 :             :     }
    1493                 :             : 
    1494                 :             :     /*                         aaabbblllfffcccsssxxxrrrpppiiimmmvvvvvv                                      */
    1495                 :             : 
    1496                 :           0 :     inchi_ios_print_nodisplay(fcb, "%3d%3d  0  0%3d  0  0  0  0  0%3d%s\n",
    1497         [ #  # ]:           0 :                               num_atoms, *num_bonds, bChiralFlag ? 1 : 0, *nNumAddLines, *nNumAddLines ? " V2000" : "");
    1498                 :             : 
    1499                 :           0 :     return ret;
    1500                 :             : }
    1501                 :             : 
    1502                 :             : /* GH #100: "%10.4f" with '.' decimal point, e.g. "    1.5000" */
    1503                 :             : #define LEN_COORD_STR 32
    1504                 :           0 : static const char *Coord10_4(double x, char *buf)
    1505                 :             : {
    1506                 :           0 :     sprintf(buf, "%10.4f", x);
    1507                 :           0 :     inchi_dot_decimal(buf);
    1508                 :           0 :     return buf;
    1509                 :             : }
    1510                 :             : 
    1511                 :             : 
    1512                 :             : /****************************************************************************
    1513                 :             :  OrigAtData : Write To SDfile : Atoms Block
    1514                 :             : ****************************************************************************/
    1515                 :           0 : int OrigAtData_WriteToSDfileAtomsBlock(const ORIG_ATOM_DATA *inp_at_data,
    1516                 :             :                                         INCHI_IOSTREAM       *fcb,
    1517                 :             :                                         const char           *name,
    1518                 :             :                                         const char           *comment,
    1519                 :             :                                         int                  bAtomsDT,
    1520                 :             :                                         const char           *szLabel,
    1521                 :             :                                         const char           *szValue)
    1522                 :             : {
    1523                 :           0 :     int i, ret = 0;
    1524                 :             :     int bAtomNeedsAlias;
    1525                 :             :     /* djb-rwth: removing redundant variables */
    1526                 :           0 :     int num_atoms = inp_at_data->num_inp_atoms;
    1527                 :           0 :     const inp_ATOM *at = inp_at_data->at;
    1528                 :             :     double x, y, z;
    1529                 :             :     char bx[LEN_COORD_STR], by[LEN_COORD_STR], bz[LEN_COORD_STR];
    1530                 :             : 
    1531         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1532                 :             :     {
    1533                 :           0 :         char elname[ATOM_EL_LEN] = "\0\0\0\0\0";
    1534                 :           0 :         int iso = 0;
    1535                 :           0 :         int charge = 0;
    1536                 :           0 :         int valence = 0;
    1537   [ #  #  #  #  :           0 :         int nIsotopeH = IS_DEUTERIUM( i ) ? 1 : IS_TRITIUM( i ) ? 2 : 0;
          #  #  #  #  #  
                #  #  # ]
    1538                 :             :         int bonds_val;
    1539                 :           0 :         bAtomNeedsAlias = ALIASED_AT( i );
    1540                 :           0 :         memset( elname, 0, sizeof( elname ) ); /* djb-rwth: memset_s C11/Annex K variant? */
    1541                 :             : 
    1542         [ #  # ]:           0 :         if (bAtomNeedsAlias)
    1543                 :             :         {
    1544                 :             :             /* alias */
    1545                 :           0 :             strcpy(elname, "C");
    1546                 :             :         }
    1547                 :             :         else
    1548                 :             :         {
    1549                 :             :             /* isotope*/
    1550         [ #  # ]:           0 :             if (nIsotopeH)
    1551                 :             :             {
    1552   [ #  #  #  # ]:           0 :                 strcpy(elname, bAtomsDT ? (nIsotopeH == 1 ? "D" : "T") : "H");
    1553                 :             :             }
    1554                 :             :             else
    1555                 :             :             {
    1556                 :           0 :                 strncpy(elname, at[i].elname, sizeof(elname) - 1);
    1557                 :           0 :                 elname[sizeof(elname) - 1] = 0; /* adding zero termination after strncpy */
    1558                 :             :             }
    1559   [ #  #  #  #  :           0 :             if (!ABNORMAL_CHG(i) && !ANY_RAD(i))
                   #  # ]
    1560                 :             :             {
    1561                 :             :                 /* charge*/
    1562                 :             :                 /* Only atoms without alias can be here*/
    1563   [ #  #  #  #  :           0 :                 switch (at[i].charge)
             #  #  #  # ]
    1564                 :             :                 {
    1565                 :           0 :                 case 3:
    1566                 :           0 :                     charge = 1;
    1567                 :           0 :                     break;
    1568                 :           0 :                 case 2:
    1569                 :           0 :                     charge = 2;
    1570                 :           0 :                     break;
    1571                 :           0 :                 case 1:
    1572                 :           0 :                     charge = 3;
    1573                 :           0 :                     break;
    1574                 :           0 :                 case -1:
    1575                 :           0 :                     charge = 5;
    1576                 :           0 :                     break;
    1577                 :           0 :                 case -2:
    1578                 :           0 :                     charge = 6;
    1579                 :           0 :                     break;
    1580                 :           0 :                 case -3:
    1581                 :           0 :                     charge = 7;
    1582                 :           0 :                     break;
    1583                 :           0 :                 case 0:
    1584                 :           0 :                     charge = 0;
    1585                 :           0 :                     break;
    1586                 :           0 :                 default:
    1587                 :           0 :                     break; /* djb-rwth: removing redundant code */
    1588                 :             :                 }
    1589                 :             :             }
    1590                 :             : 
    1591                 :             :             /* radical*/
    1592   [ #  #  #  #  :           0 :             if (ANY_RAD(i) && !ANY_CHG(i))
                   #  # ]
    1593                 :             :             {
    1594         [ #  # ]:           0 :                 if (at[i].radical == RADICAL_DOUBLET)
    1595                 :             :                 {
    1596                 :           0 :                     charge = 4;
    1597                 :             :                 }
    1598                 :             :             }
    1599                 :             :         }
    1600                 :             : 
    1601                 :             :         /* allow isotopic shift for aliased atoms */
    1602   [ #  #  #  #  :           0 :         if (NORMAL_ISO(i, bAtomsDT))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1603                 :             :         {
    1604         [ #  # ]:           0 :             iso = at[i].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 :
    1605         [ #  # ]:           0 :                     at[i].iso_atw_diff < 0 ? at[i].iso_atw_diff :
    1606                 :             :                         nIsotopeH ? nIsotopeH : 0; /* djb-rwth: removing redundant code */
    1607                 :             :         }
    1608                 :             : 
    1609                 :           0 :         x = at[i].x;
    1610                 :           0 :         y = at[i].y;
    1611                 :           0 :         z = at[i].z;
    1612                 :             : 
    1613                 :             :         /* valence -- set only if needed */
    1614                 :           0 :         bonds_val = nBondsValenceInpAt(at + i, NULL, NULL);
    1615                 :           0 :         valence = needed_unusual_el_valence(at[i].el_number, at[i].charge, at[i].radical,
    1616                 :           0 :                                             at[i].chem_bonds_valence, bonds_val, NUMH(at, i), at[i].valence);
    1617                 :             : 
    1618         [ #  # ]:           0 :         if (valence < 0)
    1619                 :             :         {
    1620                 :           0 :             valence = 15; /* means no bonds nor H */
    1621                 :             :         }
    1622                 :             : 
    1623                 :             :         /* Convert "Zz" to "*" element symbol */
    1624                 :             : 
    1625   [ #  #  #  # ]:           0 :         if (!strcmp(elname, "Zz") || !strcmp(elname, "Zy"))
    1626                 :             :         {
    1627                 :           0 :             strcpy(elname, "*");
    1628                 :             :         }
    1629                 :             : 
    1630                 :             :         /*inchi_ios_eprint(fcb,"%10.4f%10.4f%10.4f %-3.3s%2d%3d  0     0  0  0  0  0  0\n",*/
    1631                 :             :         /*    (float)at[i].x, (float)(-at[i].y), fzero, at[i].elname, iso, charge);*/
    1632                 :             :         /*              xxxxxxyyyyyyzzzzzz aaa____ddcccsssnnnbbbvvvrrriiimmmeee  */
    1633                 :           0 :         inchi_ios_print_nodisplay(fcb, "%s%s%s %-3.3s%2d%3d  0     0%3d  0  0  0  0\n",
    1634                 :             :                                   Coord10_4(x, bx), Coord10_4(y, by), Coord10_4(z, bz),
    1635                 :             :                                   elname, (int)iso, (int)charge, valence /* at[i].special*/);
    1636                 :             : 
    1637                 :             :         /* Reflect image against x-axis;                                    */
    1638                 :             :         /* when transforming MOLfile back to STDATA in mol_to_stdata(...),  */
    1639                 :             :         /* make one more reflection to restore original orientation.        */
    1640                 :             :         /* Reason: in MS Search y-axis is directed from top to bottom,      */
    1641                 :             :         /*         while in MOLfile y-axis goes from bottom to top.         */
    1642                 :             :     }
    1643                 :             : 
    1644                 :           0 :     return ret;
    1645                 :             : }
    1646                 :             : 
    1647                 :             : /****************************************************************************
    1648                 :             :  OrigAtData : Write To SDfile : Bonds Block
    1649                 :             : ****************************************************************************/
    1650                 :           0 : int OrigAtData_WriteToSDfileBondsBlock( const ORIG_ATOM_DATA *inp_at_data,
    1651                 :             :                                         INCHI_IOSTREAM       *fcb,
    1652                 :             :                                         const char           *name,
    1653                 :             :                                         const char           *comment,
    1654                 :             :                                         const char           *szLabel,
    1655                 :             :                                         const char           *szValue,
    1656                 :             :                                         INT_ARRAY            *written_bond_ends )
    1657                 :             : {
    1658                 :           0 :     int i, j, k, ret = 0;
    1659                 :           0 :     int num_atoms = inp_at_data->num_inp_atoms;
    1660                 :           0 :     const inp_ATOM *at = inp_at_data->at;
    1661                 :             : 
    1662                 :             :     /* bonds*/
    1663         [ #  # ]:           0 :     for (i = 0; i < num_atoms; i++)
    1664                 :             :     {
    1665         [ #  # ]:           0 :         for (j = 0; j < at[i].valence; j++)
    1666                 :             :         {
    1667         [ #  # ]:           0 :             if (i < at[i].neighbor[j])
    1668                 :             :             {
    1669                 :             :                 unsigned a1, a2;
    1670         [ #  # ]:           0 :                 if ((k = at[i].bond_stereo[j])) /* djb-rwth: addressing LLVM warning */
    1671                 :             :                 {
    1672                 :             :                     /* bond stereo */
    1673         [ #  # ]:           0 :                     if (k < 0)
    1674                 :             :                     {
    1675                 :             :                         /* transposition */
    1676                 :           0 :                         a1 = (unsigned)(at[i].neighbor[j] + 1);
    1677                 :           0 :                         a2 = (unsigned)(i + 1);
    1678                 :           0 :                         inchi_ios_print_nodisplay(fcb, "%3u%3u%3u%3u  0  0  0\n",
    1679                 :           0 :                                                   a1, a2, (unsigned)(at[i].bond_type[j]), (unsigned)abs(k));
    1680                 :             :                     }
    1681                 :             :                     else
    1682                 :             :                     {
    1683                 :             :                         /* no transposition*/
    1684                 :           0 :                         a1 = (unsigned)(i + 1);
    1685                 :           0 :                         a2 = (unsigned)(at[i].neighbor[j] + 1);
    1686                 :           0 :                         inchi_ios_print_nodisplay(fcb, "%3u%3u%3u%3u  0  0  0\n",
    1687                 :           0 :                                                   a1, a2, (unsigned)(at[i].bond_type[j]), (unsigned)abs(k));
    1688                 :             :                     }
    1689                 :             :                 }
    1690                 :             :                 else
    1691                 :             :                 {
    1692                 :           0 :                     a1 = (unsigned)(i + 1);
    1693                 :           0 :                     a2 = (unsigned)(at[i].neighbor[j] + 1);
    1694                 :           0 :                     inchi_ios_print_nodisplay(fcb, "%3u%3u%3u  0  0  0  0\n",
    1695                 :           0 :                                               a1, a2, (unsigned)(at[i].bond_type[j]));
    1696                 :             :                 }
    1697                 :             : 
    1698   [ #  #  #  # ]:           0 :                 if (IntArray_Append(written_bond_ends, a1) ||
    1699                 :           0 :                     IntArray_Append(written_bond_ends, a2))
    1700                 :             :                 {
    1701                 :           0 :                     return _IS_ERROR;
    1702                 :             :                 }
    1703                 :             :             }
    1704                 :             :         }
    1705                 :             :     }
    1706                 :             : 
    1707                 :           0 :     return ret;
    1708                 :             : }
    1709                 :             : 
    1710                 :             : /****************************************************************************
    1711                 :             :  OrigAtData : Write To SDfile : Additional Lines
    1712                 :             : ****************************************************************************/
    1713                 :           0 : int OrigAtData_WriteToSDfileAdditionalLines( const ORIG_ATOM_DATA *inp_at_data,
    1714                 :             :                                              INCHI_IOSTREAM       *fcb,
    1715                 :             :                                              const char           *name,
    1716                 :             :                                              const char           *comment,
    1717                 :             :                                              int                  bAtomsDT,
    1718                 :             :                                              const char           *szLabel,
    1719                 :             :                                              const char           *szValue,
    1720                 :             :                                              int                  nNumAliasLines,
    1721                 :             :                                              int                  nNumChargeLines,
    1722                 :             :                                              int                  nNumRadicalLines,
    1723                 :             :                                              int                  nNumIsoLines,
    1724                 :             :                                              INT_ARRAY            *written_bond_ends )
    1725                 :             : {
    1726                 :             :     char str_m[66], entry[25];
    1727                 :           0 :     int i, num_m, k, j, ret = 0;
    1728                 :             : 
    1729         [ #  # ]:           0 :     if (inp_at_data) /* djb-rwth: fixing a NULL pointer dereference */
    1730                 :             :     {
    1731                 :           0 :         int num_atoms = inp_at_data->num_inp_atoms;
    1732   [ #  #  #  #  :           0 :         int is_polymer = inp_at_data && inp_at_data->polymer && inp_at_data->polymer->n > 0 && inp_at_data->valid_polymer;
             #  #  #  # ]
    1733                 :           0 :         const inp_ATOM *at = inp_at_data->at;
    1734                 :             : 
    1735                 :             :         /* Aliases. 5-3-99 DCh.*/
    1736         [ #  # ]:           0 :         if (nNumAliasLines)
    1737                 :             :         {
    1738                 :           0 :             num_m = 0;
    1739         [ #  # ]:           0 :             for (i = 0; i < num_atoms; i++)
    1740                 :             :             {
    1741         [ #  # ]:           0 :                 if (ALIASED_AT(i))
    1742                 :             :                 {
    1743                 :             :                     int len;
    1744                 :           0 :                     inchi_ios_print_nodisplay(fcb, "A  %d\n", i + 1);
    1745                 :           0 :                     num_m++;
    1746                 :           0 :                     len = sprintf(str_m, "%s", at[i].elname);
    1747                 :             :                     /* add isotopic H to the alias */
    1748         [ #  # ]:           0 :                     for (k = 0; k < NUM_H_ISOTOPES; k++)
    1749                 :             :                     {
    1750         [ #  # ]:           0 :                         int num_H = at[i].num_iso_H[k] + (k ? 0 : at[i].num_H);
    1751         [ #  # ]:           0 :                         if (num_H)
    1752                 :             :                         {
    1753   [ #  #  #  #  :           0 :                             len += sprintf(str_m + len, "%s", k == 0 ? "H" : k == 1 ? "D" : k == 2 ? "T" : "?");
                   #  # ]
    1754         [ #  # ]:           0 :                             if (num_H != 1)
    1755                 :             :                             {
    1756                 :           0 :                                 len += sprintf(str_m + len, "%d", num_H);
    1757                 :             :                             }
    1758                 :             :                         }
    1759                 :             :                     }
    1760                 :             : 
    1761                 :             :                     /* Add charge to the Alias */
    1762         [ #  # ]:           0 :                     if (at[i].charge)
    1763                 :             :                     {
    1764         [ #  # ]:           0 :                         len += sprintf(str_m + len, "%s", at[i].charge > 0 ? "+" : "-");
    1765         [ #  # ]:           0 :                         if (1 < (j = abs(at[i].charge)))
    1766                 :             :                         {
    1767                 :           0 :                             len += sprintf(str_m + len, "%d", j);
    1768                 :             :                         }
    1769                 :             :                     }
    1770                 :             : 
    1771                 :             :                     /* Add radical to the Alias */
    1772         [ #  # ]:           0 :                     if (at[i].radical == RADICAL_SINGLET)
    1773                 :             :                     {
    1774                 :           0 :                         len += sprintf(str_m + len, "%s", ":");
    1775                 :             :                     }
    1776         [ #  # ]:           0 :                     else if (at[i].radical == RADICAL_DOUBLET)
    1777                 :             :                     {
    1778                 :           0 :                         len += sprintf(str_m + len, "%s", "^");
    1779                 :             :                     }
    1780         [ #  # ]:           0 :                     else if (at[i].radical == RADICAL_TRIPLET)
    1781                 :             :                     {
    1782                 :           0 :                         len += sprintf(str_m + len, "%s", "^^");
    1783                 :             :                     }
    1784                 :           0 :                     inchi_ios_print_nodisplay(fcb, "%s\n", str_m);
    1785                 :           0 :                     num_m++;
    1786                 :             :                 }
    1787                 :             :             }
    1788                 :             : 
    1789         [ #  # ]:           0 :             if (num_m != nNumAliasLines)
    1790                 :             :             {
    1791                 :             :                 /* error in lines counting*/
    1792                 :           0 :                 ret++;
    1793                 :             :             }
    1794                 :             :         }
    1795                 :             : 
    1796                 :             :         /* charges*/
    1797                 :           0 :         str_m[0] = 0;
    1798                 :           0 :         num_m = 0;
    1799         [ #  # ]:           0 :         if (nNumChargeLines)
    1800                 :             :         {
    1801         [ #  # ]:           0 :             for (i = 0; i < num_atoms; i++)
    1802                 :             :             {
    1803   [ #  #  #  # ]:           0 :                 if (at[i].charge && !ALIASED_AT(i))
    1804                 :             :                 {
    1805                 :           0 :                     sprintf(entry, " %3d %3d", i + 1, (int)at[i].charge);
    1806                 :           0 :                     strcat(str_m, entry);
    1807                 :           0 :                     num_m++;
    1808                 :             :                 }
    1809   [ #  #  #  #  :           0 :                 if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    1810                 :             :                 {
    1811                 :           0 :                     inchi_ios_print_nodisplay(fcb, "M  CHG%3d%s\n", num_m, str_m);
    1812                 :           0 :                     str_m[0] = 0;
    1813                 :           0 :                     num_m = 0;
    1814                 :             :                 }
    1815                 :             :             }
    1816                 :             :         }
    1817                 :             : 
    1818                 :             :         /* radicals*/
    1819                 :           0 :         str_m[0] = 0;
    1820                 :           0 :         num_m = 0;
    1821                 :             : 
    1822         [ #  # ]:           0 :         if (nNumRadicalLines)
    1823                 :             :         {
    1824         [ #  # ]:           0 :             for (i = 0; i < num_atoms; i++)
    1825                 :             :             {
    1826   [ #  #  #  # ]:           0 :                 if (at[i].radical && !ALIASED_AT(i))
    1827                 :             :                 {
    1828                 :           0 :                     int radical = (at[i].radical == RADICAL_SINGLET ||
    1829         [ #  # ]:           0 :                                    at[i].radical == RADICAL_DOUBLET ||
    1830   [ #  #  #  # ]:           0 :                                    at[i].radical == RADICAL_TRIPLET) ? at[i].radical : 0;
    1831         [ #  # ]:           0 :                     if (radical)
    1832                 :             :                     {
    1833                 :           0 :                         sprintf(entry, " %3d %3d", i + 1, radical);
    1834                 :           0 :                         strcat(str_m, entry);
    1835                 :           0 :                         num_m++;
    1836                 :             :                     }
    1837                 :             :                 }
    1838   [ #  #  #  #  :           0 :                 if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    1839                 :             :                 {
    1840                 :           0 :                     inchi_ios_print_nodisplay(fcb, "M  RAD%3d%s\n", num_m, str_m);
    1841                 :           0 :                     str_m[0] = 0;
    1842                 :           0 :                     num_m = 0;
    1843                 :             :                 }
    1844                 :             :             }
    1845                 :             :         }
    1846                 :             : 
    1847                 :             :         /* isotopes*/
    1848                 :           0 :         str_m[0] = 0;
    1849                 :           0 :         num_m = 0;
    1850         [ #  # ]:           0 :         if (nNumIsoLines)
    1851                 :             :         {
    1852                 :             :             int el_num, iso;
    1853         [ #  # ]:           0 :             for (i = 0; i < num_atoms; i++)
    1854                 :             :             {
    1855                 :             :                 /*
    1856                 :             :                 if ( 0 == strcmp( at[i].elname, "D" ) ) {
    1857                 :             :                     sprintf( entry, " %3d %3d", i+1, 2 );
    1858                 :             :                     strcat( str_m, entry );
    1859                 :             :                     num_m ++;
    1860                 :             :                 } else
    1861                 :             :                 if ( 0 == strcmp( at[i].elname, "T" ) ) {
    1862                 :             :                     sprintf( entry, " %3d %3d", i+1, 3 );
    1863                 :             :                     strcat( str_m, entry );
    1864                 :             :                     num_m ++;
    1865                 :             :                 } else
    1866                 :             :                 if ( k = at[i].iso_atw_diff ) {
    1867                 :             :                     int mw = get_atomic_mass_from_elnum( at[i].el_number );
    1868                 :             :                     mw += (k > 0)? k-1 : k;
    1869                 :             :                     sprintf( entry, " %3d %3d", i+1, mw );
    1870                 :             :                     strcat( str_m, entry );
    1871                 :             :                     num_m ++;
    1872                 :             :                 }
    1873                 :             :                 */
    1874                 :             : 
    1875   [ #  #  #  #  :           0 :                 if (ANY_ISO(i, bAtomsDT) && !ALIASED_AT(i))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1876                 :             :                 {
    1877   [ #  #  #  #  :           0 :                     if (IS_DEUTERIUM(i))
                   #  # ]
    1878                 :             :                     {
    1879                 :           0 :                         iso = 1;
    1880                 :           0 :                         el_num = 1;
    1881                 :             :                     }
    1882   [ #  #  #  #  :           0 :                     else if (IS_TRITIUM(i))
                   #  # ]
    1883                 :             :                     {
    1884                 :           0 :                         iso = 2;
    1885                 :           0 :                         el_num = 1;
    1886                 :             :                     }
    1887                 :             :                     else
    1888                 :             :                     {
    1889         [ #  # ]:           0 :                         iso = at[i].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 : at[i].iso_atw_diff;
    1890                 :           0 :                         el_num = at[i].el_number;
    1891                 :             :                     }
    1892                 :           0 :                     iso += get_atomic_mass_from_elnum(el_num);
    1893                 :           0 :                     sprintf(entry, " %3d %3d", i + 1, iso);
    1894                 :           0 :                     strcat(str_m, entry);
    1895                 :           0 :                     num_m++;
    1896                 :             :                 }
    1897                 :             : 
    1898   [ #  #  #  #  :           0 :                 if ((i == num_atoms - 1 && num_m) || num_m == 8) /* djb-rwth: addressing LLVM warning */
                   #  # ]
    1899                 :             :                 {
    1900                 :           0 :                     inchi_ios_print_nodisplay(fcb, "M  ISO%3d%s\n", num_m, str_m);
    1901                 :           0 :                     str_m[0] = 0;
    1902                 :           0 :                     num_m = 0;
    1903                 :             :                 }
    1904                 :             :             }
    1905                 :             :         }
    1906                 :             : 
    1907         [ #  # ]:           0 :         if (is_polymer)
    1908                 :             :         {
    1909                 :           0 :             OrigAtData_WriteToSDfilePolymerData(inp_at_data, fcb, name, comment,
    1910                 :             :                                                 szLabel, szValue, written_bond_ends);
    1911                 :             :         }
    1912                 :             : 
    1913                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  END\n");
    1914                 :             :     }
    1915                 :           0 :     return ret;
    1916                 :             : }
    1917                 :             : 
    1918                 :             : /****************************************************************************
    1919                 :             : OrigAtData : Write To SDfile : Polymer Data
    1920                 :             : ****************************************************************************/
    1921                 :           0 : int OrigAtData_WriteToSDfilePolymerData( const ORIG_ATOM_DATA *inp_at_data,
    1922                 :             :                                          INCHI_IOSTREAM       *fcb,
    1923                 :             :                                          const char           * name,
    1924                 :             :                                          const char           *comment,
    1925                 :             :                                          const char           *szLabel,
    1926                 :             :                                          const char           *szValue,
    1927                 :             :                                          INT_ARRAY            *written_bond_ends )
    1928                 :             : {
    1929                 :           0 :     int j, k, ju, jj, jprev, ret = 0;
    1930                 :           0 :     const char *sty[] = {"NON", "SRU", "MON", "COP", "MOD", "CRO", "MER"};
    1931                 :           0 :     const char *sst[] = {"NON", "ALT", "RAN", "BLO"};
    1932                 :           0 :     const char *con[] = {"NON", "HT", "HH", "EU"};
    1933                 :           0 :     OAD_PolymerUnit *u = NULL;
    1934                 :             : 
    1935                 :             :     /* STY */
    1936                 :           0 :     jj = 0;
    1937                 :           0 :     jprev = -1;
    1938         [ #  # ]:           0 :     for (j = 0; j < inp_at_data->polymer->n; j++)
    1939                 :             :     {
    1940                 :           0 :         u = inp_at_data->polymer->units[j];
    1941   [ #  #  #  # ]:           0 :         if (u->type > 0 && u->type <= 6)
    1942                 :             :         {
    1943                 :           0 :             jj++;
    1944                 :             :         }
    1945   [ #  #  #  # ]:           0 :         if (jj == 8 || j == inp_at_data->polymer->n - 1)
    1946                 :             :         {
    1947         [ #  # ]:           0 :             inchi_ios_print_nodisplay(fcb, "M  STY%3d", jj % 8 ? jj % 8 : 8);
    1948         [ #  # ]:           0 :             for (k = jprev + 1; k <= j; k++)
    1949                 :             :             {
    1950                 :           0 :                 u = inp_at_data->polymer->units[k];
    1951   [ #  #  #  # ]:           0 :                 if (u->type > 0 && u->type <= 6)
    1952                 :             :                 {
    1953                 :           0 :                     inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, sty[u->type]);
    1954                 :             :                 }
    1955                 :             :             }
    1956                 :           0 :             inchi_ios_print_nodisplay(fcb, "\n");
    1957                 :           0 :             jj = 0;
    1958                 :           0 :             jprev = j;
    1959                 :             :         }
    1960                 :             :     }
    1961                 :             :     /* SLB */
    1962                 :             :     /* djb-rwth: removing redundant code */
    1963                 :           0 :     jprev = -1;
    1964         [ #  # ]:           0 :     for (j = 0; j < inp_at_data->polymer->n; j++)
    1965                 :             :     {
    1966                 :             :         /* djb-rwth: removing redundant code */
    1967   [ #  #  #  # ]:           0 :         if (j == 8 || j == inp_at_data->polymer->n - 1)
    1968                 :             :         {
    1969                 :           0 :             jj = j + 1;
    1970         [ #  # ]:           0 :             inchi_ios_print_nodisplay(fcb, "M  SLB%3d", jj % 8 ? jj % 8 : 8);
    1971         [ #  # ]:           0 :             for (k = jprev + 1; k < jj; k++)
    1972                 :             :             {
    1973                 :           0 :                 u = inp_at_data->polymer->units[k];
    1974                 :           0 :                 inchi_ios_print_nodisplay(fcb, " %3d %3d", u->id, u->label);
    1975                 :             :             }
    1976                 :           0 :             inchi_ios_print_nodisplay(fcb, "\n");
    1977                 :             :             /* djb-rwth: removing redundant code */
    1978                 :           0 :             jprev = j;
    1979                 :             :         }
    1980                 :             :     }
    1981                 :             : 
    1982                 :             :     /* SST */
    1983                 :           0 :     jj = 0;
    1984                 :             :     /* djb-rwth: removing redundant code */
    1985         [ #  # ]:           0 :     for (j = 0; j < inp_at_data->polymer->n; j++)
    1986                 :             :     {
    1987                 :           0 :         u = inp_at_data->polymer->units[j];
    1988   [ #  #  #  #  :           0 :         if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
                   #  # ]
    1989                 :             :         {
    1990                 :           0 :             jj++;
    1991                 :             :         }
    1992                 :             :     }
    1993         [ #  # ]:           0 :     if (jj)
    1994                 :             :     {
    1995                 :           0 :         jj = 0;
    1996                 :           0 :         jprev = -1;
    1997         [ #  # ]:           0 :         for (j = 0; j < inp_at_data->polymer->n; j++)
    1998                 :             :         {
    1999                 :           0 :             u = inp_at_data->polymer->units[j];
    2000   [ #  #  #  #  :           0 :             if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
                   #  # ]
    2001                 :             :             {
    2002                 :           0 :                 jj++;
    2003                 :             :             }
    2004   [ #  #  #  # ]:           0 :             if (jj == 8 || j == inp_at_data->polymer->n - 1)
    2005                 :             :             {
    2006         [ #  # ]:           0 :                 inchi_ios_print_nodisplay(fcb, "M  SST%3d", jj % 8 ? jj % 8 : 8);
    2007         [ #  # ]:           0 :                 for (k = jprev + 1; k <= j; k++)
    2008                 :             :                 {
    2009                 :           0 :                     u = inp_at_data->polymer->units[k];
    2010   [ #  #  #  #  :           0 :                     if (u->subtype == MOL_FMT_M_SST_ALT || u->subtype == MOL_FMT_M_SST_RAN || u->subtype == MOL_FMT_M_SST_BLK)
                   #  # ]
    2011                 :             :                     {
    2012                 :           0 :                         inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, sst[u->subtype]);
    2013                 :             :                     }
    2014                 :             :                 }
    2015                 :           0 :                 inchi_ios_print_nodisplay(fcb, "\n");
    2016                 :           0 :                 jj = 0;
    2017                 :           0 :                 jprev = j;
    2018                 :             :             }
    2019                 :             :         }
    2020                 :             :     }
    2021                 :             : 
    2022                 :             :     /* SCN */
    2023                 :           0 :     jj = 0;
    2024                 :             :     /* djb-rwth: removing redundant code */
    2025         [ #  # ]:           0 :     for (j = 0; j < inp_at_data->polymer->n; j++)
    2026                 :             :     {
    2027                 :           0 :         u = inp_at_data->polymer->units[j];
    2028   [ #  #  #  #  :           0 :         if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
                   #  # ]
    2029                 :             :         {
    2030                 :           0 :             jj++;
    2031                 :             :         }
    2032                 :             :     }
    2033         [ #  # ]:           0 :     if (jj)
    2034                 :             :     {
    2035                 :           0 :         jj = 0;
    2036                 :           0 :         jprev = -1;
    2037         [ #  # ]:           0 :         for (j = 0; j < inp_at_data->polymer->n; j++)
    2038                 :             :         {
    2039                 :           0 :             u = inp_at_data->polymer->units[j];
    2040   [ #  #  #  #  :           0 :             if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
                   #  # ]
    2041                 :             :             {
    2042                 :           0 :                 jj++;
    2043                 :             :             }
    2044   [ #  #  #  # ]:           0 :             if (jj == 8 || j == inp_at_data->polymer->n - 1)
    2045                 :             :             {
    2046         [ #  # ]:           0 :                 inchi_ios_print_nodisplay(fcb, "M  SCN%3d", jj % 8 ? jj % 8 : 8);
    2047         [ #  # ]:           0 :                 for (k = jprev + 1; k <= j; k++)
    2048                 :             :                 {
    2049                 :           0 :                     u = inp_at_data->polymer->units[k];
    2050   [ #  #  #  #  :           0 :                     if (u->conn == MOL_FMT_M_CONN_HT || u->conn == MOL_FMT_M_CONN_HH || u->conn == MOL_FMT_M_CONN_EU)
                   #  # ]
    2051                 :             :                     {
    2052                 :           0 :                         inchi_ios_print_nodisplay(fcb, " %3d %3s", u->id, con[u->conn]);
    2053                 :             :                     }
    2054                 :             :                 }
    2055                 :           0 :                 inchi_ios_print_nodisplay(fcb, "\n");
    2056                 :           0 :                 jj = 0;
    2057                 :           0 :                 jprev = j;
    2058                 :             :             }
    2059                 :             :         }
    2060                 :             :     }
    2061                 :             :     /* SAL */
    2062         [ #  # ]:           0 :     for (ju = 0; ju < inp_at_data->polymer->n; ju++)
    2063                 :             :     {
    2064                 :           0 :         u = inp_at_data->polymer->units[ju];
    2065                 :           0 :         jj = 0;
    2066                 :           0 :         jprev = -1;
    2067         [ #  # ]:           0 :         for (j = 0; j < u->na; j++)
    2068                 :             :         {
    2069                 :           0 :             jj++;
    2070   [ #  #  #  # ]:           0 :             if (jj == 15 || j == u->na - 1)
    2071                 :             :             {
    2072         [ #  # ]:           0 :                 inchi_ios_print_nodisplay(fcb, "M  SAL %3d%3d", u->id, jj % 15 ? jj % 15 : 15);
    2073         [ #  # ]:           0 :                 for (k = jprev + 1; k <= j; k++)
    2074                 :             :                 {
    2075                 :           0 :                     inchi_ios_print_nodisplay(fcb, " %3d", u->alist[k]);
    2076                 :             :                 }
    2077                 :           0 :                 inchi_ios_print_nodisplay(fcb, "\n");
    2078                 :           0 :                 jj = 0;
    2079                 :           0 :                 jprev = j;
    2080                 :             :             }
    2081                 :             :         }
    2082                 :             :     }
    2083                 :             :     /* SBL */
    2084         [ #  # ]:           0 :     for (ju = 0; ju < inp_at_data->polymer->n; ju++)
    2085                 :             :     {
    2086                 :           0 :         u = inp_at_data->polymer->units[ju];
    2087                 :           0 :         jj = 0;
    2088                 :           0 :         jprev = -1;
    2089         [ #  # ]:           0 :         for (j = 0; j < u->nb; j++)
    2090                 :             :         {
    2091                 :           0 :             jj++;
    2092   [ #  #  #  # ]:           0 :             if (jj == 15 || j == u->nb - 1)
    2093                 :             :             {
    2094         [ #  # ]:           0 :                 inchi_ios_print_nodisplay(fcb, "M  SBL %3d%3d", u->id, jj % 15 ? jj % 15 : 15);
    2095                 :             : 
    2096         [ #  # ]:           0 :                 for (k = jprev + 1; k <= j; k++)
    2097                 :             :                 {
    2098                 :           0 :                     int a1, a2, e1, e2, wb, bond_num = 0;
    2099                 :           0 :                     a1 = u->blist[2 * k];
    2100                 :           0 :                     a2 = u->blist[2 * k + 1];
    2101         [ #  # ]:           0 :                     for (wb = 0; wb < written_bond_ends->used / 2; wb++)
    2102                 :             :                     {
    2103                 :           0 :                         e1 = written_bond_ends->item[2 * wb];
    2104                 :           0 :                         e2 = written_bond_ends->item[2 * wb + 1];
    2105   [ #  #  #  #  :           0 :                         if ((a1 == e1 && a2 == e2) || (a2 == e1 && a1 == e2))
             #  #  #  # ]
    2106                 :             :                         {
    2107                 :           0 :                             bond_num = wb + 1;
    2108                 :           0 :                             break;
    2109                 :             :                         }
    2110                 :             :                     }
    2111         [ #  # ]:           0 :                     if (bond_num)
    2112                 :             :                     {
    2113                 :           0 :                         inchi_ios_print_nodisplay(fcb, " %3d", bond_num);
    2114                 :             :                     }
    2115                 :             :                 }
    2116                 :             : 
    2117                 :           0 :                 inchi_ios_print_nodisplay(fcb, "\n");
    2118                 :           0 :                 jj = 0;
    2119                 :           0 :                 jprev = j;
    2120                 :             :             }
    2121                 :             :         }
    2122                 :             :     }
    2123                 :             : 
    2124                 :             :     /* SDI */
    2125         [ #  # ]:           0 :     for (j = 0; j < inp_at_data->polymer->n; j++)
    2126                 :             :     {
    2127                 :             :         /* better than nothing */
    2128                 :             :         float xmin, xmax, ymin, ymax;
    2129                 :             :         char b1[LEN_COORD_STR], b2[LEN_COORD_STR], b3[LEN_COORD_STR], b4[LEN_COORD_STR];
    2130                 :           0 :         xmin = ymin = -1.0 * ((float)j + 1.0); /* djb-rwth: cast operator added, 1->1.0 */
    2131                 :           0 :         xmax = ymax = +1.0 * ((float)j + 1.0); /* djb-rwth: cast operator added, 1->1.0 */
    2132                 :           0 :         u = inp_at_data->polymer->units[j];
    2133                 :             :         /* u->xbr1[0], x1, y1, x2, y2 u->xbr1[1], u->xbr1[2], u->xbr1[3] */
    2134                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  SDI %3d%3d%s%s%s%s\n", u->id, 4,
    2135                 :             :                                   Coord10_4(xmin, b1), Coord10_4(ymin, b2),
    2136                 :             :                                   Coord10_4(xmin, b3), Coord10_4(ymax, b4));
    2137                 :             :         /* u->xbr2[0], u->xbr2[1], u->xbr2[2], u->xbr2[3] */
    2138                 :           0 :         inchi_ios_print_nodisplay(fcb, "M  SDI %3d%3d%s%s%s%s\n", u->id, 4,
    2139                 :             :                                   Coord10_4(xmax, b1), Coord10_4(ymax, b2),
    2140                 :             :                                   Coord10_4(xmax, b3), Coord10_4(ymin, b4));
    2141                 :             :     }
    2142                 :             : 
    2143                 :           0 :     return ret;
    2144                 :             : }
        

Generated by: LCOV version 2.0-1