LCOV - code coverage report
Current view: top level - src - mol_fmt3.c (source / functions) Coverage Total Hit
Test: InChI Unit Test Coverage Lines: 64.5 % 871 562
Test Date: 2026-12-03 11:58:49 Functions: 85.7 % 21 18
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 47.6 % 638 304

             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                 :             : 
      20                 :             : #include "bcf_s.h"
      21                 :             : 
      22                 :             : /*
      23                 :             :     Molfile V3000 related procedures
      24                 :             : 
      25                 :             : */
      26                 :             : 
      27                 :             : static int get_actual_atom_number(int index, int n, int *orig, int *fin);
      28                 :             : 
      29                 :             : /****************************************************************************
      30                 :             :  Init V3000 reader
      31                 :             : ****************************************************************************/
      32                 :          76 : int MolfileV3000Init(MOL_FMT_CTAB *ctab,
      33                 :             :                      char *pStrErr)
      34                 :             : {
      35                 :          76 :     int ret = 0;
      36                 :             :     int i;
      37                 :             : 
      38                 :             :     /* STAR ATOMS */
      39                 :          76 :     ctab->v3000->n_star_atoms = 0;
      40                 :          76 :     ctab->v3000->n_non_star_atoms = 0;
      41                 :             : 
      42         [ +  - ]:          76 :     if (ctab->n_atoms)
      43                 :             :     {
      44                 :          76 :         ctab->v3000->atom_index_orig = (int *)inchi_calloc(ctab->n_atoms, sizeof(int));
      45                 :          76 :         ctab->v3000->atom_index_fin = (int *)inchi_calloc(ctab->n_atoms, sizeof(int));
      46   [ +  -  +  - ]:          76 :         if (ctab->v3000->atom_index_orig && ctab->v3000->atom_index_fin) /* djb-rwth: fixing a NULL pointer dereference */
      47                 :             :         {
      48         [ +  + ]:        8790 :             for (i = 0; i < ctab->n_atoms; i++) /* protective */
      49                 :             :             {
      50                 :        8714 :                 ctab->v3000->atom_index_orig[i] = -1;
      51                 :        8714 :                 ctab->v3000->atom_index_fin[i] = -1;
      52                 :             :             }
      53                 :             :         }
      54                 :             :     }
      55                 :             :     else
      56                 :             :     {
      57                 :           0 :         ctab->v3000->atom_index_orig = NULL;
      58                 :           0 :         ctab->v3000->atom_index_fin = NULL;
      59                 :             :     }
      60                 :             : 
      61                 :             :     /* HAPTIC BONDS */
      62                 :          76 :     ctab->v3000->n_haptic_bonds = 0;
      63                 :          76 :     ctab->v3000->haptic_bonds = (NUM_LISTS *)inchi_calloc(1, sizeof(NUM_LISTS));
      64         [ -  + ]:          76 :     if (!ctab->v3000->haptic_bonds)
      65                 :             :     {
      66                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
      67                 :           0 :         return -1;
      68                 :             :     }
      69                 :          76 :     ret = NumLists_Alloc(ctab->v3000->haptic_bonds, 8);
      70         [ -  + ]:          76 :     if (ret < 0)
      71                 :             :     {
      72                 :           0 :         ctab->v3000->haptic_bonds = NULL;
      73                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
      74                 :           0 :         return -1;
      75                 :             :     }
      76                 :             : 
      77                 :             :     /* STEABS */
      78                 :          76 :     ctab->v3000->n_steabs = 0;
      79                 :          76 :     ctab->v3000->steabs = (NUM_LISTS *)inchi_calloc(1, sizeof(NUM_LISTS));
      80         [ -  + ]:          76 :     if (!ctab->v3000->steabs)
      81                 :             :     {
      82                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
      83                 :           0 :         return -1;
      84                 :             :     }
      85                 :          76 :     ret = NumLists_Alloc(ctab->v3000->steabs, 1);
      86         [ -  + ]:          76 :     if (ret < 0)
      87                 :             :     {
      88                 :           0 :         ctab->v3000->steabs = NULL;
      89                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
      90                 :           0 :         return -1;
      91                 :             :     }
      92                 :             :     /* STEREL */
      93                 :          76 :     ctab->v3000->n_sterel = 0;
      94                 :          76 :     ctab->v3000->sterel = (NUM_LISTS *)inchi_calloc(1, sizeof(NUM_LISTS));
      95         [ -  + ]:          76 :     if (!ctab->v3000->sterel)
      96                 :             :     {
      97                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
      98                 :           0 :         return -1;
      99                 :             :     }
     100                 :          76 :     ret = NumLists_Alloc(ctab->v3000->sterel, 4);
     101         [ -  + ]:          76 :     if (ret < 0)
     102                 :             :     {
     103                 :           0 :         ctab->v3000->sterel = NULL;
     104                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
     105                 :           0 :         return -1;
     106                 :             :     }
     107                 :             :     /* STERAC */
     108                 :          76 :     ctab->v3000->n_sterac = 0;
     109                 :          76 :     ctab->v3000->sterac = (NUM_LISTS *)inchi_calloc(1, sizeof(NUM_LISTS));
     110         [ -  + ]:          76 :     if (!ctab->v3000->sterac)
     111                 :             :     {
     112                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
     113                 :           0 :         return -1;
     114                 :             :     }
     115                 :          76 :     ret = NumLists_Alloc(ctab->v3000->sterac, 4);
     116         [ -  + ]:          76 :     if (ret < 0)
     117                 :             :     {
     118                 :           0 :         ctab->v3000->sterac = NULL;
     119                 :           0 :         AddErrorMessage(pStrErr, "Out of RAM");
     120                 :           0 :         return -1;
     121                 :             :     }
     122                 :             : 
     123                 :          76 :     return ret;
     124                 :             : }
     125                 :             : 
     126                 :             : /****************************************************************************
     127                 :             :  Delete V3000 reader data
     128                 :             : ****************************************************************************/
     129                 :          76 : int DeleteMolfileV3000Info(MOL_FMT_v3000 *v3000)
     130                 :             : {
     131         [ +  - ]:          76 :     if (v3000)
     132                 :             :     {
     133                 :             : 
     134         [ +  - ]:          76 :         if (v3000->atom_index_orig)
     135                 :             :         {
     136         [ +  - ]:          76 :             inchi_free(v3000->atom_index_orig);
     137                 :             :         }
     138                 :             : 
     139         [ +  - ]:          76 :         if (v3000->atom_index_fin)
     140                 :             :         {
     141         [ +  - ]:          76 :             inchi_free(v3000->atom_index_fin);
     142                 :             :         }
     143                 :             : 
     144         [ +  - ]:          76 :         if (v3000->haptic_bonds)
     145                 :             :         {
     146                 :          76 :             NumLists_Free(v3000->haptic_bonds);
     147                 :          76 :             free(v3000->haptic_bonds);
     148                 :             :         }
     149                 :             : 
     150         [ +  - ]:          76 :         if (v3000->steabs)
     151                 :             :         {
     152                 :          76 :             NumLists_Free(v3000->steabs);
     153                 :          76 :             free(v3000->steabs);
     154                 :             :         }
     155                 :             : 
     156         [ +  - ]:          76 :         if (v3000->sterel)
     157                 :             :         {
     158                 :          76 :             NumLists_Free(v3000->sterel);
     159                 :          76 :             free(v3000->sterel);
     160                 :             :         }
     161                 :             : 
     162         [ +  - ]:          76 :         if (v3000->sterac)
     163                 :             :         {
     164                 :          76 :             NumLists_Free(v3000->sterac);
     165                 :          76 :             free(v3000->sterac);
     166                 :             :         }
     167                 :             : 
     168         [ +  - ]:          76 :         inchi_free(v3000);
     169                 :          76 :         v3000 = NULL;
     170                 :             :     }
     171                 :             : 
     172                 :          76 :     return 0;
     173                 :             : }
     174                 :             : 
     175                 :             : /****************************************************************************
     176                 :             :     Extended version of inchi_fgetsLf which is able of reading
     177                 :             :     concatenated lines (ending with '-') of V3000 Molfile.
     178                 :             :     Also removes "M  V30 " prefix" and normalizes the rest of string
     179                 :             : ****************************************************************************/
     180                 :           0 : char *inchi_fgetsLf_V3000(char *line, INCHI_IOSTREAM *inp_stream)
     181                 :             : {
     182                 :           0 :     char *p = NULL;
     183                 :           0 :     int len = 0;
     184                 :             : 
     185                 :           0 :     p = inchi_fgetsLf(line, MOL_FMT_V3000_INPLINELEN, inp_stream);
     186         [ #  # ]:           0 :     if (!p)
     187                 :             :     {
     188                 :           0 :         return NULL;
     189                 :             :     }
     190                 :             : 
     191                 :           0 :     len = (int)strlen(p);
     192         [ #  # ]:           0 :     if (len < 7)
     193                 :             :     {
     194                 :           0 :         return NULL;
     195                 :             :     }
     196                 :             : 
     197         [ #  # ]:           0 :     if (strncmp(p, "M  V30 ", 7))
     198                 :             :     {
     199                 :           0 :         return NULL;
     200                 :             :     }
     201                 :             : 
     202                 :           0 :     p += 7;
     203                 :           0 :     len = normalize_string(p); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
     204                 :             : 
     205                 :           0 :     return p;
     206                 :             : }
     207                 :             : 
     208                 :             : /****************************************************************************
     209                 :             :     Read V3000 field.
     210                 :             : 
     211                 :             :     It is MolfileReadField updated for V3000.
     212                 :             :     It considers right whitespace as stop sign,
     213                 :             :     no predefined len is used.
     214                 :             : 
     215                 :             :     Returns -1 on error otherwise number of bytes read.
     216                 :             : 
     217                 :             :     NB: ASSUMES THAT STRING HAS BEEN NORMALIZED with normalize_string()
     218                 :             : 
     219                 :             :     TODO: treat strings with spaces in double quotes
     220                 :             : ****************************************************************************/
     221                 :       84039 : int MolfileV3000ReadField(void *data,
     222                 :             :                           int data_type,
     223                 :             :                           char **line_ptr)
     224                 :             : {
     225                 :       84039 :     int nread = 0;
     226                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     227                 :       84039 :     const int max_field_len = sizeof(field);
     228                 :       84039 :     long ldata = 0L;
     229                 :       84039 :     double ddata = 0.0;
     230                 :             :     char *p_end;
     231                 :             : 
     232                 :       84039 :     memset(field, 0, max_field_len); /* djb-rwth: memset_s C11/Annex K variant? */
     233                 :             : 
     234                 :       84039 :     nread = read_upto_delim(line_ptr, field, max_field_len, " \t\n\v\f\r");
     235                 :             : 
     236   [ +  +  +  - ]:       84039 :     switch (data_type)
     237                 :             :     {
     238                 :        8804 :     case MOL_FMT_STRING_DATA:
     239                 :             :     {
     240   [ +  -  +  - ]:        8804 :         if (nread && (nread <= ATOM_EL_LEN)) /* djb-rwth: fixing GHI #133 -- updated 28/09/2025 */
     241                 :             :         {
     242                 :        8804 :             mystrncpy((char *)data, field, nread + 1);
     243                 :             :         }
     244                 :             :         else
     245                 :             :         {
     246                 :           0 :             ((char *)data)[0] = '\0';
     247                 :             :         }
     248                 :             :     }
     249                 :        8804 :     break;
     250                 :             : 
     251                 :       49057 :     case MOL_FMT_CHAR_INT_DATA:
     252                 :             :     case MOL_FMT_SHORT_INT_DATA:
     253                 :             :     case MOL_FMT_LONG_INT_DATA:
     254                 :             :     case MOL_FMT_INT_DATA:
     255                 :             :     {
     256                 :             :         /* assume that field ends at first non-digit */
     257                 :       49057 :         ldata = strtol(field, &p_end, 10);
     258                 :             : 
     259         [ +  + ]:       49057 :         if (p_end == field)
     260                 :             :         {
     261                 :           3 :             nread = 0;
     262                 :             :         }
     263                 :             : 
     264         [ -  + ]:       49057 :         if (data_type == MOL_FMT_LONG_INT_DATA)
     265                 :             :         {
     266   [ #  #  #  # ]:           0 :             if (LONG_MIN < ldata && ldata < LONG_MAX)
     267                 :             :             {
     268                 :           0 :                 *(long *)data = (long)ldata;
     269                 :             :             }
     270                 :             :             else
     271                 :             :             {
     272                 :           0 :                 *(long *)data = 0L;
     273                 :           0 :                 nread = -1;
     274                 :             :             }
     275                 :             :         }
     276         [ +  + ]:       49057 :         else if (data_type == MOL_FMT_INT_DATA)
     277                 :             :         {
     278   [ +  -  +  - ]:       25622 :             if (INT_MIN <= ldata && ldata <= INT_MAX) /* djb-rwth: addressing coverity ID #499496/499553 -- ldata check seems to be necessary */
     279                 :             :             {
     280                 :       25622 :                 *(int *)data = (int)ldata;
     281                 :             :             }
     282                 :             :             else
     283                 :             :             {
     284                 :           0 :                 *(int *)data = (int)0;
     285                 :           0 :                 nread = -1;
     286                 :             :             }
     287                 :             :         }
     288                 :             : 
     289         [ +  + ]:       23435 :         else if (data_type == MOL_FMT_CHAR_INT_DATA)
     290                 :             :         {
     291   [ +  -  +  - ]:        8124 :             if (SCHAR_MIN <= ldata && ldata <= SCHAR_MAX)
     292                 :             :             {
     293                 :        8124 :                 *(S_CHAR *)data = (S_CHAR)ldata;
     294                 :             :             }
     295                 :             :             else
     296                 :             :             {
     297                 :           0 :                 *(S_CHAR *)data = (S_CHAR)0;
     298                 :           0 :                 nread = -1;
     299                 :             :             }
     300                 :             :         }
     301                 :             : 
     302         [ +  - ]:       15311 :         else if (data_type == MOL_FMT_SHORT_INT_DATA)
     303                 :             :         {
     304   [ +  -  +  - ]:       15311 :             if (SHRT_MIN <= ldata && ldata <= SHRT_MAX)
     305                 :             :             {
     306                 :       15311 :                 *(S_SHORT *)data = (S_SHORT)ldata;
     307                 :             :             }
     308                 :             :             else
     309                 :             :             {
     310                 :           0 :                 *(S_SHORT *)data = (S_SHORT)0;
     311                 :           0 :                 nread = -1;
     312                 :             :             }
     313                 :             :         }
     314                 :             :         else
     315                 :             :         {
     316                 :           0 :             nread = -1;
     317                 :             :         }
     318                 :             :     }
     319                 :       49057 :     break; /* INT's */
     320                 :             : 
     321                 :       26178 :     case MOL_FMT_DOUBLE_DATA:
     322                 :             :     case MOL_FMT_FLOAT_DATA:
     323                 :             :     {
     324                 :             :         /* assume that field ends at first non-digit */
     325                 :       26178 :         ddata = inchi_strtod(field, (const char **)&p_end); /* GH #100 */
     326                 :             : 
     327         [ -  + ]:       26178 :         if (p_end == field)
     328                 :             :         {
     329                 :           0 :             nread = 0;
     330                 :             :         }
     331                 :             : 
     332         [ +  - ]:       26178 :         if (data_type == MOL_FMT_DOUBLE_DATA)
     333                 :             :         {
     334   [ +  -  +  - ]:       26178 :             if (ddata != HUGE_VAL && /*ldata*/ ddata != -HUGE_VAL)
     335                 :             :             {
     336                 :       26178 :                 *(double *)data = ddata;
     337                 :             :             }
     338                 :             :             else
     339                 :             :             {
     340                 :           0 :                 *(double *)data = 0.0;
     341                 :           0 :                 nread = -1;
     342                 :             :             }
     343                 :             :         }
     344         [ #  # ]:           0 :         else if (data_type == MOL_FMT_FLOAT_DATA)
     345                 :             :         {
     346         [ #  # ]:           0 :             if (fabs(ddata) <= (double)FLT_MIN)
     347                 :             :             {
     348                 :           0 :                 *(float *)data = 0.0;
     349                 :             :             }
     350         [ #  # ]:           0 :             else if (fabs(ddata) >= (double)FLT_MAX)
     351                 :             :             {
     352                 :           0 :                 *(float *)data = 0.0;
     353                 :           0 :                 nread = -1;
     354                 :             :             }
     355                 :             :         }
     356                 :             :         else
     357                 :             :         {
     358                 :           0 :             *(float *)data = (float)ddata; /* djb-rwth: addressing coverity ID #499519 -- probably never reached */
     359                 :             :         }
     360                 :             :     }
     361                 :       26178 :     break; /* REAL's */
     362                 :             : 
     363                 :           0 :     default:
     364                 :           0 :         nread = -1;
     365                 :             :     }
     366                 :             : 
     367                 :       84039 :     return nread;
     368                 :             : }
     369                 :             : 
     370                 :             : /****************************************************************************
     371                 :             :     Read V3000 keyword.
     372                 :             :     TODO: treat strings with spaces in double quotes
     373                 :             : ****************************************************************************/
     374                 :         459 : int MolfileV3000ReadKeyword(char *key,
     375                 :             :                             char **line_ptr)
     376                 :             : {
     377                 :         459 :     int nread = 0;
     378                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     379                 :         459 :     const int max_field_len = sizeof(field);
     380                 :             : 
     381                 :         459 :     memset(field, 0, max_field_len); /* djb-rwth: memset_s C11/Annex K variant? */
     382                 :             : 
     383                 :         459 :     nread = read_upto_delim(line_ptr, field, max_field_len, "= \t\n\v\f\r");
     384                 :             : 
     385         [ +  - ]:         459 :     if (nread)
     386                 :             :     {
     387                 :         459 :         mystrncpy(key, field, nread + 1);
     388                 :             :         /* consume '=' sign if present */
     389         [ +  + ]:         459 :         if (*line_ptr)
     390                 :             :         {
     391         [ +  - ]:         458 :             if (*line_ptr[0] == '=')
     392                 :             :             {
     393                 :         458 :                 *line_ptr = *line_ptr + 1;
     394                 :             :             }
     395                 :             :         }
     396                 :             :     }
     397                 :             :     else
     398                 :             :     {
     399                 :           0 :         key[0] = '\0';
     400                 :             :     }
     401                 :             : 
     402                 :         459 :     return nread;
     403                 :             : }
     404                 :             : 
     405                 :             : /****************************************************************************
     406                 :             :  Read V3000 head of CTab
     407                 :             : ****************************************************************************/
     408                 :          76 : int MolfileV3000ReadCTABBeginAndCountsLine(MOL_FMT_CTAB *ctab,
     409                 :             :                                            INCHI_IOSTREAM *inp_file,
     410                 :             :                                            char *pStrErr)
     411                 :             : {
     412                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     413                 :          76 :     int err = 0, len; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
     414                 :          76 :     int failed = 0;
     415                 :             : 
     416                 :             :     int nc;
     417                 :          76 :     char *p = NULL, *line = NULL;
     418                 :             :     INCHI_IOSTREAM tmpin;
     419                 :          76 :     INCHI_IOS_STRING *pin = &tmpin.s;
     420                 :          76 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
     421                 :             : 
     422                 :          76 :     field[0] = '\0'; /* djb-rwth: adding zero termination */
     423                 :             : 
     424                 :             :     /* Check for proper start */
     425                 :             : 
     426                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     427                 :          76 :     inchi_strbuf_reset(pin);
     428                 :          76 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
     429         [ -  + ]:          76 :     if (nc < 1)
     430                 :             :     {
     431                 :           0 :         p = NULL;
     432                 :             :     }
     433                 :             :     else
     434                 :             :     {
     435                 :          76 :         p = line = pin->pStr;
     436                 :             :     }
     437   [ +  -  -  + ]:          76 :     if (!p || strcmp(p, "BEGIN CTAB"))
     438                 :             :     {
     439         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 CTab start marker");
     440                 :             :     }
     441                 :          76 :     remove_one_lf(line);
     442                 :             : 
     443                 :             :     /* Reset all previosly read data from quasi-counts line            */
     444                 :             :     /* (which contains only single meaningful value, 'V3000' marker    */
     445                 :          76 :     ctab->n_atoms = -1;
     446                 :          76 :     ctab->n_bonds = -1;
     447                 :          76 :     ctab->chiral_flag = -1;
     448                 :          76 :     ctab->n_stext_entries = -1;
     449                 :             :     /* Relax stricthness of V3000 conformance: */
     450                 :             :     /* Do not check if '999' supplied, just use this. */
     451                 :          76 :     ctab->n_property_lines = 999;
     452                 :             : 
     453                 :             :     /* Read counts line */
     454                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     455                 :          76 :     inchi_strbuf_reset(pin);
     456                 :          76 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
     457         [ -  + ]:          76 :     if (nc < 1)
     458                 :             :     {
     459                 :           0 :         p = NULL;
     460                 :             :     }
     461                 :             :     else
     462                 :             :     {
     463                 :          76 :         p = line = pin->pStr;
     464                 :             :     }
     465         [ -  + ]:          76 :     if (!p)
     466                 :             :     {
     467         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Cannot read V3000 counts line");
     468                 :             :     }
     469                 :          76 :     remove_one_lf(line);
     470                 :             : 
     471                 :             :     /* Parse counts line */
     472                 :          76 :     len = MolfileV3000ReadField(field, MOL_FMT_STRING_DATA, &p); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
     473         [ -  + ]:          76 :     if (strcmp(field, "COUNTS"))
     474                 :             :     {
     475         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Cannot read V3000 counts line");
     476                 :             :     }
     477                 :          76 :     failed = 0;
     478         [ -  + ]:          76 :     if (0 > MolfileV3000ReadField(&ctab->n_atoms, MOL_FMT_INT_DATA, &p))
     479                 :             :     {
     480                 :           0 :         failed = 2;
     481                 :             :     }
     482         [ -  + ]:          76 :     else if (0 > MolfileV3000ReadField(&ctab->n_bonds, MOL_FMT_INT_DATA, &p))
     483                 :             :     {
     484                 :           0 :         failed = 1;
     485                 :             :     }
     486         [ -  + ]:          76 :     else if (0 > MolfileV3000ReadField(&ctab->v3000->n_sgroups, MOL_FMT_INT_DATA, &p))
     487                 :             :     {
     488                 :           0 :         failed = 1;
     489                 :             :     }
     490         [ -  + ]:          76 :     else if (0 > MolfileV3000ReadField(&ctab->v3000->n_3d_constraints, MOL_FMT_INT_DATA, &p))
     491                 :             :     {
     492                 :           0 :         failed = 1;
     493                 :             :     }
     494         [ -  + ]:          76 :     else if (0 > MolfileV3000ReadField(&ctab->chiral_flag, MOL_FMT_CHAR_INT_DATA, &p))
     495                 :             :     {
     496                 :           0 :         failed = 1;
     497                 :             :     }
     498                 :             : 
     499         [ +  - ]:          76 :     if (failed)
     500                 :             :     {
     501                 :           0 :         err = 3;
     502         [ #  # ]:           0 :         if (failed == 2)
     503                 :             :         {
     504         [ #  # ]:           0 :             TREAT_ERR(err, 3, "Number of atoms too large. V3000 counts line:");
     505                 :             :         }
     506                 :             :         else
     507                 :             :         {
     508                 :             :             /* too long input file line or other value min-max range mismatch */
     509         [ #  # ]:           0 :             TREAT_ERR(err, 3, "Cannot interpret V3000 counts line:");
     510                 :             :         }
     511                 :           0 :         dotify_non_printable_chars(line);
     512                 :           0 :         AddErrorMessage(pStrErr, line);
     513                 :           0 :         goto err_fin;
     514                 :             :     }
     515                 :             : 
     516                 :          76 : err_fin:
     517                 :          76 :     inchi_strbuf_close(pin);
     518                 :             : 
     519                 :          76 :     return err;
     520                 :             : }
     521                 :             : 
     522                 :             : /****************************************************************************
     523                 :             :  GH #146: return index of name in names[], -1 if absent
     524                 :             : ****************************************************************************/
     525                 :          12 : static int V3000FindName(const char *name, const char **names, int n)
     526                 :             : {
     527                 :             :     int i;
     528                 :             : 
     529         [ +  - ]:          24 :     for (i = 0; i < n; i++)
     530                 :             :     {
     531         [ +  + ]:          24 :         if (!strcmp(name, names[i]))
     532                 :             :         {
     533                 :          12 :             return i;
     534                 :             :         }
     535                 :             :     }
     536                 :             : 
     537                 :           0 :     return -1;
     538                 :             : }
     539                 :             : 
     540                 :             : /****************************************************************************
     541                 :             :  GH #146: skip one V3000 value: (...), "..." or a plain token
     542                 :             : ****************************************************************************/
     543                 :           0 : static void V3000SkipValue(char **p)
     544                 :             : {
     545                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     546                 :           0 :     char *end = NULL;
     547                 :             : 
     548         [ #  # ]:           0 :     if (!*p)
     549                 :             :     {
     550                 :           0 :         return;
     551                 :             :     }
     552                 :             : 
     553         [ #  # ]:           0 :     if (**p == '(')
     554                 :             :     {
     555                 :           0 :         end = strchr(*p, ')');
     556                 :             :     }
     557         [ #  # ]:           0 :     else if (**p == '"')
     558                 :             :     {
     559                 :           0 :         end = strchr(*p + 1, '"');
     560                 :             :     }
     561         [ #  # ]:           0 :     if (end)
     562                 :             :     {
     563                 :           0 :         *p = end + 1;
     564                 :           0 :         return;
     565                 :             :     }
     566                 :             : 
     567                 :           0 :     read_upto_delim(p, field, sizeof(field), " \t\n\v\f\r");
     568                 :             : }
     569                 :             : 
     570                 :             : /****************************************************************************
     571                 :             :  GH #146: append V3000 index list "(n i1 ... in)" to items
     572                 :             : ****************************************************************************/
     573                 :          12 : static int V3000ReadIndexList(MOL_FMT_CTAB *ctab, char **p, INT_ARRAY *items)
     574                 :             : {
     575                 :          12 :     int k, ret = 0, *num_list = NULL;
     576                 :             : 
     577   [ +  -  -  + ]:          12 :     if (0 > MolfileV3000ReadStereoCollection(ctab, p, &num_list, NULL) || !num_list)
     578                 :             :     {
     579                 :           0 :         return -1;
     580                 :             :     }
     581                 :             : 
     582   [ +  +  +  - ]:          47 :     for (k = 2; k < num_list[1] + 2 && !ret; k++)
     583                 :             :     {
     584                 :          35 :         ret = IntArray_Append(items, num_list[k]);
     585                 :             :     }
     586         [ +  - ]:          12 :     inchi_free(num_list);
     587                 :             : 
     588                 :          12 :     return ret;
     589                 :             : }
     590                 :             : 
     591                 :             : /****************************************************************************
     592                 :             :  GH #146: read bracket "(9 x1 y1 z1 x2 y2 z2 0 0 0)" as V2000 SDI x1 y1 x2 y2
     593                 :             : ****************************************************************************/
     594                 :           5 : static int V3000ReadBracket(char **p, double xbr[4])
     595                 :             : {
     596                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     597                 :             :     double x[9];
     598                 :           5 :     int i, n = 0;
     599                 :             : 
     600         [ +  + ]:           5 :     if (0 > read_upto_delim(p, field, sizeof(field), "1234567890 \t\n\v\f\r") ||
     601   [ +  -  +  -  :           4 :         strcmp(field, "(") || 0 >= MolfileV3000ReadField(&n, MOL_FMT_INT_DATA, p) || n != 9)
                   -  + ]
     602                 :             :     {
     603                 :           1 :         return -1;
     604                 :             :     }
     605                 :             : 
     606         [ +  + ]:          40 :     for (i = 0; i < n; i++)
     607                 :             :     {
     608         [ -  + ]:          36 :         if (0 >= MolfileV3000ReadField(&x[i], MOL_FMT_DOUBLE_DATA, p))
     609                 :             :         {
     610                 :           0 :             return -1;
     611                 :             :         }
     612                 :             :     }
     613                 :             : 
     614                 :           4 :     xbr[0] = x[0];
     615                 :           4 :     xbr[1] = x[1];
     616                 :           4 :     xbr[2] = x[3];
     617                 :           4 :     xbr[3] = x[4];
     618                 :             : 
     619                 :           4 :     return 0;
     620                 :             : }
     621                 :             : 
     622                 :             : /****************************************************************************
     623                 :             :  GH #146: read one V3000 polymer Sgroup line into ctab->sgroups, as the
     624                 :             :  V2000 M  STY/SLB/SST/SCN/SAL/SBL/SDI/SMT lines do, e.g.
     625                 :             :      1 SRU 1 ATOMS=(3 3 2 4) XBONDS=(2 1 4) CONNECT=HT LABEL=n
     626                 :             :  Non-polymer Sgroups are skipped. Returns 0 if OK, -1 on error.
     627                 :             : ****************************************************************************/
     628                 :           6 : static int V3000ReadPolymerSGroup(MOL_FMT_CTAB *ctab, char *p)
     629                 :             : {
     630                 :             :     static const char *types[] = {"NON", "SRU", "MON", "COP", "MOD", "CRO", "MER"};
     631                 :             :     static const char *subtypes[] = {"NON", "ALT", "RAN", "BLO"};
     632                 :             :     static const char *conns[] = {"NON", "HT", "HH", "EU"};
     633                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     634                 :             :     MOL_FMT_SGROUP *sg;
     635                 :           6 :     int id, ext_id, type, q, n_brackets = 0;
     636                 :           6 :     int n_orig_at = ctab->v3000->n_non_star_atoms + ctab->v3000->n_star_atoms;
     637                 :             : 
     638                 :             :     /* Positional fields: index, type, external index (V2000 SLB label) */
     639   [ +  -  +  - ]:          12 :     if (0 >= MolfileV3000ReadField(&id, MOL_FMT_INT_DATA, &p) ||
     640         [ -  + ]:          12 :         0 >= MolfileV3000ReadField(field, MOL_FMT_STRING_DATA, &p) ||
     641                 :           6 :         0 >= MolfileV3000ReadField(&ext_id, MOL_FMT_INT_DATA, &p))
     642                 :             :     {
     643                 :           0 :         return -1;
     644                 :             :     }
     645                 :             : 
     646                 :           6 :     type = V3000FindName(field, types, sizeof(types) / sizeof(types[0]));
     647         [ -  + ]:           6 :     if (type <= MOL_FMT_M_STY_NON)
     648                 :             :     {
     649                 :           0 :         return 0;
     650                 :             :     }
     651                 :             : 
     652         [ -  + ]:           6 :     if (MolFmtSgroups_Append(&ctab->sgroups, id, type))
     653                 :             :     {
     654                 :           0 :         return -1;
     655                 :             :     }
     656                 :           6 :     sg = ctab->sgroups.group[ctab->sgroups.used - 1];
     657                 :           6 :     sg->label = ext_id;
     658         [ +  + ]:          30 :     for (q = 0; q < 4; q++)
     659                 :             :     {
     660                 :          24 :         sg->xbr1[q] = -777777.777;
     661                 :          24 :         sg->xbr2[q] = -777777.777;
     662                 :             :     }
     663                 :             : 
     664                 :             :     /* Key=value pairs */
     665   [ +  +  +  - ]:          33 :     while (p && MolfileV3000ReadKeyword(field, &p) > 0)
     666                 :             :     {
     667         [ +  + ]:          28 :         if (!strcmp(field, "ATOMS"))
     668                 :             :         {
     669         [ -  + ]:           6 :             if (V3000ReadIndexList(ctab, &p, &sg->alist))
     670                 :             :             {
     671                 :           0 :                 return -1;
     672                 :             :             }
     673                 :             :         }
     674         [ +  + ]:          22 :         else if (!strcmp(field, "XBONDS"))
     675                 :             :         {
     676         [ -  + ]:           6 :             if (V3000ReadIndexList(ctab, &p, &sg->blist))
     677                 :             :             {
     678                 :           0 :                 return -1;
     679                 :             :             }
     680                 :             :         }
     681         [ +  + ]:          16 :         else if (!strcmp(field, "BRKXYZ"))
     682                 :             :         {
     683                 :             :             /* as V2000 SDI: 1st bracket to xbr1, later ones to xbr2 */
     684   [ +  +  +  + ]:           5 :             if (V3000ReadBracket(&p, n_brackets++ ? sg->xbr2 : sg->xbr1))
     685                 :             :             {
     686                 :           1 :                 return -1;
     687                 :             :             }
     688                 :             :         }
     689         [ +  + ]:          11 :         else if (!strcmp(field, "CONNECT"))
     690                 :             :         {
     691                 :           6 :             MolfileV3000ReadField(field, MOL_FMT_STRING_DATA, &p);
     692                 :           6 :             sg->conn = V3000FindName(field, conns, sizeof(conns) / sizeof(conns[0]));
     693         [ -  + ]:           6 :             if (sg->conn <= MOL_FMT_M_CONN_NON)
     694                 :             :             {
     695                 :           0 :                 return -1;
     696                 :             :             }
     697                 :             :         }
     698         [ -  + ]:           5 :         else if (!strcmp(field, "SUBTYPE"))
     699                 :             :         {
     700                 :           0 :             MolfileV3000ReadField(field, MOL_FMT_STRING_DATA, &p);
     701                 :           0 :             sg->subtype = V3000FindName(field, subtypes, sizeof(subtypes) / sizeof(subtypes[0]));
     702         [ #  # ]:           0 :             if (sg->subtype <= MOL_FMT_M_SST_NON)
     703                 :             :             {
     704                 :           0 :                 return -1;
     705                 :             :             }
     706                 :             :         }
     707         [ +  - ]:           5 :         else if (!strcmp(field, "LABEL"))
     708                 :             :         {
     709         [ -  + ]:           5 :             if (0 > read_upto_delim(&p, sg->smt, sizeof(sg->smt), " \t\n\v\f\r"))
     710                 :             :             {
     711                 :           0 :                 return -1;
     712                 :             :             }
     713                 :             :         }
     714                 :             :         else
     715                 :             :         {
     716                 :           0 :             V3000SkipValue(&p);
     717                 :             :         }
     718                 :             :     }
     719                 :             : 
     720                 :             :     /* V3000 atom indices -> final atom numbers, as V2000 SAL has them */
     721         [ +  + ]:          25 :     for (q = 0; q < sg->alist.used; q++)
     722                 :             :     {
     723                 :          40 :         sg->alist.item[q] = get_actual_atom_number(sg->alist.item[q], n_orig_at,
     724                 :          20 :                                                    ctab->v3000->atom_index_orig,
     725                 :          20 :                                                    ctab->v3000->atom_index_fin);
     726         [ -  + ]:          20 :         if (sg->alist.item[q] < 1)
     727                 :             :         {
     728                 :           0 :             return -1;
     729                 :             :         }
     730                 :             :     }
     731         [ +  + ]:          15 :     for (q = 0; q < sg->blist.used; q++)
     732                 :             :     {
     733   [ +  -  -  + ]:          10 :         if (sg->blist.item[q] < 1 || sg->blist.item[q] > ctab->n_bonds)
     734                 :             :         {
     735                 :           0 :             return -1;
     736                 :             :         }
     737                 :             :     }
     738                 :             : 
     739                 :           5 :     return 0;
     740                 :             : }
     741                 :             : 
     742                 :             : /****************************************************************************
     743                 :             :  Read V3000 SGroup
     744                 :             : ****************************************************************************/
     745                 :           7 : int MolfileV3000ReadSGroup(MOL_FMT_CTAB *ctab,
     746                 :             :                            INCHI_IOSTREAM *inp_file,
     747                 :             :                            int treat_polymers,
     748                 :             :                            int err,
     749                 :             :                            char *pStrErr)
     750                 :             : {
     751                 :           7 :     int nc, failed = 0;
     752                 :           7 :     char *p = NULL, *line = NULL;
     753                 :             :     INCHI_IOSTREAM tmpin;
     754                 :           7 :     INCHI_IOS_STRING *pin = &tmpin.s;
     755                 :             : 
     756                 :           7 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
     757                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     758                 :             : 
     759                 :             :     while (1)
     760                 :             :     {
     761                 :          15 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
     762                 :             : 
     763         [ +  + ]:          15 :         if (nc < 1)
     764                 :             :         {
     765                 :           2 :             p = NULL;
     766                 :             :         }
     767                 :             :         else
     768                 :             :         {
     769                 :          13 :             p = line = pin->pStr;
     770                 :          13 :             remove_one_lf(line);
     771                 :             :         }
     772         [ +  + ]:          15 :         if (!p)
     773                 :             :         {
     774                 :           2 :             break; /* GH #146: EOF or non-V30 line, no END SGROUP */
     775                 :             :         }
     776   [ +  -  +  + ]:          13 :         if (p && !strcmp(p, "END SGROUP"))
     777                 :             :         {
     778                 :           5 :             inchi_ios_close(&tmpin); /* ricrogz: fixing memory leak */
     779                 :           5 :             return failed;
     780                 :             :         }
     781                 :             : 
     782                 :             :         /* GH #146: optional "DEFAULT CLASS=..." record: it only sets defaults
     783                 :             :            for the keywords below, none of which the polymer reader uses */
     784   [ +  +  -  +  :           8 :         if (!strncmp(p, "DEFAULT", 7) && (p[7] == ' ' || p[7] == '\0'))
                   -  - ]
     785                 :             :         {
     786                 :           1 :             continue;
     787                 :             :         }
     788                 :             : 
     789                 :             :         /* GH #146: polymer Sgroups, as V2000 M  STY etc. */
     790   [ +  -  +  +  :           7 :         if (p && treat_polymers != POLYMERS_NO && V3000ReadPolymerSGroup(ctab, p))
                   +  + ]
     791                 :             :         {
     792                 :           1 :             failed = 1;
     793                 :           1 :             AddErrorMessage(pStrErr, "Could not interpret V3000 polymer Sgroup:");
     794                 :           1 :             dotify_non_printable_chars(line);
     795                 :           1 :             AddErrorMessage(pStrErr, line);
     796                 :             :         }
     797                 :             :     }
     798                 :             : 
     799                 :             :     /* if (  !p  || strcmp(p, "END SGROUP") ) */
     800                 :             :     {
     801         [ +  - ]:           2 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 SGroup end marker");
     802                 :             :     }
     803                 :             : 
     804                 :           2 : err_fin:
     805                 :             : 
     806                 :           2 :     inchi_ios_close(&tmpin); /* ricrogz: fixing memory leak */
     807                 :           2 :     return err;
     808                 :             : }
     809                 :             : 
     810                 :             : /****************************************************************************
     811                 :             :  Read V3000 3DBlock
     812                 :             : ****************************************************************************/
     813                 :           0 : int MolfileV3000Read3DBlock(MOL_FMT_CTAB *ctab,
     814                 :             :                             INCHI_IOSTREAM *inp_file,
     815                 :             :                             int err,
     816                 :             :                             char *pStrErr)
     817                 :             : {
     818                 :             :     int nc;
     819                 :           0 :     char *p = NULL, *line = NULL;
     820                 :             :     INCHI_IOSTREAM tmpin;
     821                 :           0 :     INCHI_IOS_STRING *pin = &tmpin.s;
     822                 :           0 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
     823                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     824                 :             : 
     825                 :           0 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
     826                 :             : 
     827         [ #  # ]:           0 :     if (nc < 1)
     828                 :             :     {
     829                 :           0 :         p = NULL;
     830                 :             :     }
     831                 :             :     else
     832                 :             :     {
     833                 :           0 :         p = line = pin->pStr;
     834                 :             :     }
     835                 :           0 :     remove_one_lf(line);
     836                 :             : 
     837   [ #  #  #  # ]:           0 :     if (!p || strcmp(p, "END OBJ3D"))
     838                 :             :     {
     839         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 3DBlock end marker");
     840                 :             :     }
     841                 :           0 :     goto err_fin;
     842                 :             : 
     843                 :           0 : err_fin:
     844                 :             : 
     845                 :           0 :     inchi_ios_close(&tmpin); /* ricrogz: fixing memory leak */
     846                 :           0 :     return err;
     847                 :             : }
     848                 :             : 
     849                 :             : /****************************************************************************
     850                 :             :  Read V3000 collections
     851                 :             : ****************************************************************************/
     852                 :          37 : int MolfileV3000ReadCollections(MOL_FMT_CTAB *ctab,
     853                 :             :                                 INCHI_IOSTREAM *inp_file,
     854                 :             :                                 int err,
     855                 :             :                                 char *pStrErr)
     856                 :             : {
     857                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
     858                 :          37 :     const int max_field_len = sizeof(field);
     859                 :          37 :     int nread, len, n_coll = 0;
     860                 :          37 :     int failed = 0;
     861                 :             :     int nc;
     862                 :          37 :     char *p = NULL, *line = NULL;
     863                 :             :     INCHI_IOSTREAM tmpin;
     864                 :          37 :     INCHI_IOS_STRING *pin = &tmpin.s;
     865                 :             : 
     866                 :          37 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
     867                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     868                 :             : 
     869                 :          37 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
     870                 :             : 
     871         [ -  + ]:          37 :     if (nc < 1)
     872                 :             :     {
     873                 :           0 :         p = NULL;
     874                 :             :     }
     875                 :             :     else
     876                 :             :     {
     877                 :          37 :         p = line = pin->pStr;
     878                 :             :     }
     879                 :          37 :     remove_one_lf(line);
     880                 :             : 
     881   [ +  -  +  + ]:         138 :     while (p && strcmp(p, "END COLLECTION"))
     882                 :             :     {
     883                 :         101 :         int stereo_kind = MOL_FMT_V3000_STENON;
     884                 :             :         /* stereo collection of interest */
     885                 :         101 :         NUM_LISTS *ste_coll = NULL;
     886                 :             : 
     887                 :         101 :         nread = read_upto_delim(&p, field, max_field_len, "/");
     888         [ -  + ]:         101 :         if (nread < 6)
     889                 :             :         {
     890                 :           0 :             failed = 1;
     891                 :           0 :             break;
     892                 :             :         }
     893         [ -  + ]:         101 :         if (strcmp(field, "MDLV30"))
     894                 :             :         {
     895                 :           0 :             failed = 1;
     896                 :           0 :             break;
     897                 :             :         }
     898                 :             : 
     899                 :         101 :         nread = read_upto_delim(&p, field, max_field_len, "1234567890 \t\n\v\f\r"); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
     900         [ +  + ]:         101 :         if (!strcmp(field, "/STEABS"))
     901                 :             :         {
     902                 :          23 :             n_coll = 1;
     903                 :          23 :             stereo_kind = MOL_FMT_V3000_STEABS;
     904                 :          23 :             ste_coll = ctab->v3000->steabs;
     905                 :             :         }
     906         [ +  + ]:          78 :         else if (!strcmp(field, "/STEREL"))
     907                 :             :         {
     908                 :             :             /* get number of collection */
     909         [ -  + ]:          31 :             if (0 > MolfileV3000ReadField(&n_coll, MOL_FMT_CHAR_INT_DATA, &p))
     910                 :             :             {
     911                 :           0 :                 failed = 1;
     912                 :           0 :                 break;
     913                 :             :             }
     914                 :          31 :             stereo_kind = MOL_FMT_V3000_STEREL;
     915                 :          31 :             ste_coll = ctab->v3000->sterel;
     916                 :             :         }
     917         [ +  - ]:          47 :         else if (!strcmp(field, "/STERAC"))
     918                 :             :         {
     919                 :             :             /* get number of collection */
     920         [ -  + ]:          47 :             if (0 > MolfileV3000ReadField(&n_coll, MOL_FMT_CHAR_INT_DATA, &p))
     921                 :             :             {
     922                 :           0 :                 failed = 1;
     923                 :           0 :                 break;
     924                 :             :             }
     925                 :          47 :             stereo_kind = MOL_FMT_V3000_STERAC;
     926                 :          47 :             ste_coll = ctab->v3000->sterac;
     927                 :             :         }
     928                 :             :         else
     929                 :             :         {
     930                 :             :             ;
     931                 :             :         }
     932                 :             : 
     933         [ +  - ]:         101 :         if (stereo_kind != MOL_FMT_V3000_STENON)
     934                 :             :         /* currently skip non-stereo collections */
     935                 :             :         {
     936                 :             :             /* consume atoms= */
     937         [ +  - ]:         101 :             if ((len = MolfileV3000ReadKeyword(field, &p) > 0)) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
     938                 :             :             {
     939         [ +  - ]:         101 :                 if (!strcmp(field, "ATOMS"))
     940                 :             :                 {
     941                 :         101 :                     int res, *num_list = NULL;
     942                 :             : 
     943         [ -  + ]:         101 :                     if (0 > MolfileV3000ReadStereoCollection(ctab, &p, &num_list, pStrErr))
     944                 :             :                     {
     945                 :           0 :                         failed = 1;
     946                 :             :                     }
     947         [ -  + ]:         101 :                     else if (!num_list)
     948                 :             :                     {
     949                 :           0 :                         failed = 1;
     950                 :             :                     }
     951                 :             :                     else
     952                 :             :                     {
     953                 :             :                         int k, nnum;
     954                 :         101 :                         num_list[0] = n_coll;
     955                 :         101 :                         nnum = num_list[1];
     956         [ +  + ]:         125 :                         for (k = 2; k < nnum; k++)
     957                 :             :                         {
     958                 :          24 :                             num_list[k] =
     959                 :          24 :                                 get_actual_atom_number(num_list[k],
     960                 :          24 :                                                        ctab->v3000->n_non_star_atoms + ctab->v3000->n_star_atoms,
     961                 :          24 :                                                        ctab->v3000->atom_index_orig,
     962                 :          24 :                                                        ctab->v3000->atom_index_fin);
     963                 :             :                         }
     964                 :         101 :                         res = NumLists_Append(ste_coll, num_list);
     965         [ -  + ]:         101 :                         if (res < 0)
     966                 :             :                         {
     967                 :           0 :                             failed = 1;
     968                 :             :                         }
     969                 :             :                         else
     970                 :             :                         {
     971         [ +  + ]:         101 :                             if (stereo_kind == MOL_FMT_V3000_STEABS)
     972                 :             :                             {
     973                 :          23 :                                 ctab->v3000->n_steabs++;
     974                 :          23 :                                 ctab->v3000->n_collections++;
     975                 :             :                             }
     976         [ +  + ]:          78 :                             else if (stereo_kind == MOL_FMT_V3000_STEREL)
     977                 :             :                             {
     978                 :          31 :                                 ctab->v3000->n_sterel++;
     979                 :          31 :                                 ctab->v3000->n_collections++;
     980                 :             :                             }
     981         [ +  - ]:          47 :                             else if (stereo_kind == MOL_FMT_V3000_STERAC)
     982                 :             :                             {
     983                 :          47 :                                 ctab->v3000->n_sterac++;
     984                 :          47 :                                 ctab->v3000->n_collections++;
     985                 :             :                             }
     986                 :             :                         }
     987                 :             :                     }
     988                 :             :                 }
     989                 :             :             }
     990                 :             :             else
     991                 :             :             {
     992                 :           0 :                 failed = 1;
     993                 :           0 :                 break;
     994                 :             :             }
     995                 :             :         }
     996                 :             : 
     997                 :             :         /*next_line:*/
     998                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
     999                 :         101 :         inchi_strbuf_reset(pin);
    1000                 :             : 
    1001                 :         101 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1002                 :             : 
    1003         [ -  + ]:         101 :         if (nc < 1)
    1004                 :             :         {
    1005                 :           0 :             p = NULL;
    1006                 :             :         }
    1007                 :             :         else
    1008                 :             :         {
    1009                 :         101 :             p = line = pin->pStr;
    1010                 :         101 :             remove_one_lf(line);
    1011                 :             :         }
    1012                 :             :     }
    1013                 :             : 
    1014         [ -  + ]:          37 :     if (failed)
    1015                 :             :     {
    1016                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1017                 :           0 :         inchi_strbuf_reset(pin);
    1018                 :           0 :         line = NULL; /* Reset line pointer since buffer was freed */
    1019                 :             : 
    1020                 :           0 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1021                 :             : 
    1022         [ #  # ]:           0 :         if (nc < 1)
    1023                 :             :         {
    1024                 :           0 :             p = NULL;
    1025                 :             :         }
    1026                 :             :         else
    1027                 :             :         {
    1028                 :           0 :             p = line = pin->pStr;
    1029                 :           0 :             remove_one_lf(line);
    1030                 :             :         }
    1031                 :             :     }
    1032                 :             : 
    1033         [ -  + ]:          37 :     if (!p)
    1034                 :             :     {
    1035                 :           0 :         failed = 1;
    1036                 :             :     }
    1037                 :             : 
    1038         [ +  - ]:          37 :     if (failed)
    1039                 :             :     {
    1040                 :           0 :         err = 7;
    1041         [ #  # ]:           0 :         TREAT_ERR(err, 7, "Cannot interpret V3000 collection line(s)"); /* djb-rwth: addressing coverity ID #499531 -- TREAT_ERR properly used */
    1042         [ #  # ]:           0 :         if (line)
    1043                 :             :         {
    1044                 :           0 :             dotify_non_printable_chars(line);
    1045                 :           0 :             AddErrorMessage(pStrErr, line);
    1046                 :             :         }
    1047                 :           0 :         goto err_fin;
    1048                 :             :     }
    1049                 :             : 
    1050                 :             :     // /* Error: No V3000 Collection end marker */
    1051                 :             :     // if (ctab->v3000->n_steabs ||
    1052                 :             :     //     ctab->v3000->n_sterel ||
    1053                 :             :     //     ctab->v3000->n_sterac)
    1054                 :             :     // {
    1055                 :             :     //     AddErrorMessage(pStrErr, "V3000 enhanced stereo read/stored but ignored");
    1056                 :             :     // }
    1057                 :             : 
    1058                 :          37 : err_fin:
    1059                 :             : 
    1060                 :          37 :     inchi_ios_close(&tmpin); /* ricrogz: fixing memory leak */
    1061                 :          37 :     return err;
    1062                 :             : }
    1063                 :             : 
    1064                 :             : /****************************************************************************
    1065                 :             :  Read V3000 atoms
    1066                 :             : ****************************************************************************/
    1067                 :          76 : int MolfileV3000ReadAtomsBlock(MOL_FMT_CTAB *ctab,
    1068                 :             :                                INCHI_IOSTREAM *inp_file,
    1069                 :             :                                int treat_polymers,
    1070                 :             :                                int err,
    1071                 :             :                                char *pStrErr)
    1072                 :             : {
    1073                 :             :     int i;
    1074                 :             :     /* djb-rwth: removing redundant variables */
    1075                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
    1076                 :          76 :     int nc, failed = 0;
    1077                 :          76 :     char *p = NULL, *line = NULL;
    1078                 :             :     INCHI_IOSTREAM tmpin;
    1079                 :          76 :     INCHI_IOS_STRING *pin = &tmpin.s;
    1080                 :          76 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
    1081                 :             : 
    1082                 :             :     /* Check for proper start */
    1083                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1084                 :             : 
    1085                 :          76 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1086                 :             : 
    1087         [ -  + ]:          76 :     if (nc < 1)
    1088                 :             :     {
    1089                 :           0 :         p = NULL;
    1090                 :             :     }
    1091                 :             :     else
    1092                 :             :     {
    1093                 :          76 :         p = line = pin->pStr;
    1094                 :             :     }
    1095   [ +  -  -  + ]:          76 :     if (!p || strcmp(p, "BEGIN ATOM"))
    1096                 :             :     {
    1097         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 Atom block start marker");
    1098                 :             :     }
    1099                 :          76 :     remove_one_lf(line);
    1100                 :             : 
    1101                 :          76 :     ctab->v3000->n_non_star_atoms = 0;
    1102         [ +  + ]:        8790 :     for (i = 0; i < ctab->n_atoms; i++)
    1103                 :             :     {
    1104                 :        8714 :         int ii = -1;
    1105                 :             : 
    1106                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1107                 :        8714 :         inchi_strbuf_reset(pin);
    1108                 :             : 
    1109                 :        8714 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1110                 :             : 
    1111         [ -  + ]:        8714 :         if (nc < 1)
    1112                 :             :         {
    1113                 :           0 :             p = NULL;
    1114                 :             :         }
    1115                 :             :         else
    1116                 :             :         {
    1117                 :        8714 :             p = line = pin->pStr;
    1118                 :             :         }
    1119         [ -  + ]:        8714 :         if (!p)
    1120                 :             :         {
    1121         [ #  # ]:           0 :             if (!err)
    1122                 :             :             {
    1123         [ #  # ]:           0 :                 TREAT_ERR(err, 2, "Cannot read V3000 atom block line");
    1124                 :             :             }
    1125                 :           0 :             break;
    1126                 :             :         }
    1127                 :        8714 :         remove_one_lf(line);
    1128                 :             : 
    1129         [ -  + ]:        8714 :         if (err)
    1130                 :             :         {
    1131         [ #  # ]:           0 :             if (!strcmp(line, SD_FMT_END_OF_DATA))
    1132                 :             :             {
    1133                 :           0 :                 err = -abs(err);
    1134                 :           0 :                 break;
    1135                 :             :             }
    1136                 :           0 :             continue; /* bypass the rest of the Atom block */
    1137                 :             :         }
    1138                 :             : 
    1139         [ +  - ]:        8714 :         if (ctab->atoms)
    1140                 :             :         {
    1141                 :             :             int index, aamap; /* not used actually, just read them */
    1142                 :             :             int len;
    1143                 :             :             char symbol[6]; /* TODO: treat possibly long V3000 atom names */
    1144                 :        8714 :             double fx = 0.0, fy = 0.0, fz = 0.0;
    1145                 :        8714 :             symbol[0] = '\0'; /* djb-rwth: adding zero termination */
    1146                 :             : 
    1147                 :             :             /* Read positional parameters */
    1148                 :        8714 :             failed = 0;
    1149                 :             : 
    1150         [ -  + ]:        8714 :             if (0 > MolfileV3000ReadField(&index, MOL_FMT_INT_DATA, &p))
    1151                 :             :             {
    1152                 :           0 :                 failed = 1;
    1153                 :             :             }
    1154         [ -  + ]:        8714 :             else if (0 > MolfileV3000ReadField(&symbol, MOL_FMT_STRING_DATA, &p))
    1155                 :             :             {
    1156                 :           0 :                 failed = 1;
    1157                 :             :             }
    1158         [ -  + ]:        8714 :             else if (0 > MolfileV3000ReadField(&fx, MOL_FMT_DOUBLE_DATA, &p))
    1159                 :             :             {
    1160                 :           0 :                 failed = 1;
    1161                 :             :             }
    1162         [ -  + ]:        8714 :             else if (0 > MolfileV3000ReadField(&fy, MOL_FMT_DOUBLE_DATA, &p))
    1163                 :             :             {
    1164                 :           0 :                 failed = 1;
    1165                 :             :             }
    1166         [ -  + ]:        8714 :             else if (0 > MolfileV3000ReadField(&fz, MOL_FMT_DOUBLE_DATA, &p))
    1167                 :             :             {
    1168                 :           0 :                 failed = 1;
    1169                 :             :             }
    1170         [ -  + ]:        8714 :             else if (0 > MolfileV3000ReadField(&aamap, MOL_FMT_INT_DATA, &p))
    1171                 :             :             {
    1172                 :           0 :                 failed = 1;
    1173                 :             :             }
    1174                 :             : 
    1175         [ -  + ]:        8714 :             if (failed)
    1176                 :             :             {
    1177                 :             : 
    1178                 :           0 :                 err = 4;
    1179         [ #  # ]:           0 :                 TREAT_ERR(err, 4, "Cannot interpret V3000 atom block line:"); /* djb-rwth: addressing coverity ID #499547 -- TREAT_ERR properly used */
    1180                 :           0 :                 dotify_non_printable_chars(line);
    1181                 :           0 :                 AddErrorMessage(pStrErr, line);
    1182                 :             : 
    1183         [ #  # ]:           0 :                 if (!strcmp(line, SD_FMT_END_OF_DATA))
    1184                 :             :                 {
    1185                 :           0 :                     err = -abs(err);
    1186                 :           0 :                     break;
    1187                 :             :                 }
    1188                 :           5 :                 continue; /* can't interpret a first half of atom block line */
    1189                 :             :             }
    1190                 :             : 
    1191                 :             :             /* emulate V2000 coordinates substring */
    1192         [ +  - ]:        8714 :             if (ctab->coords)
    1193                 :             :             {
    1194                 :             :                 char szcoords[40];
    1195                 :        8714 :                 sprintf(szcoords, "%10g%10g%10g", fx, fy, fz);
    1196                 :        8714 :                 inchi_dot_decimal(szcoords); /* GH #100 */
    1197                 :             :                 /* szcoords may exceed the fixed-size MOL_COORD slot for
    1198                 :             :                    large-magnitude coordinates; bound the copy as the V2000
    1199                 :             :                    reader does. */
    1200                 :        8714 :                 mystrncpy(ctab->coords[i], szcoords, sizeof(ctab->coords[i]));
    1201                 :             :             }
    1202                 :             : 
    1203                 :             :             /* GH #146: polymers need star atoms as SRU end groups */
    1204   [ +  +  +  + ]:        8714 :             if (!strcmp(symbol, "*") && treat_polymers == POLYMERS_NO)
    1205                 :             :             {
    1206                 :             :                 /* ignore star atoms but save index info */
    1207                 :           5 :                 ctab->v3000->atom_index_orig[i] = index;
    1208                 :           5 :                 ctab->v3000->atom_index_fin[i] = -1;
    1209                 :           5 :                 ctab->v3000->n_star_atoms++;
    1210                 :           5 :                 continue;
    1211                 :             :             }
    1212                 :             : 
    1213                 :        8709 :             ctab->v3000->n_non_star_atoms++;
    1214                 :        8709 :             ctab->v3000->atom_index_orig[i] = index;
    1215                 :        8709 :             ctab->v3000->atom_index_fin[i] = ctab->v3000->n_non_star_atoms;
    1216                 :        8709 :             ii = ctab->v3000->n_non_star_atoms - 1;
    1217                 :             : 
    1218                 :        8709 :             mystrncpy(ctab->atoms[ii].symbol, symbol, sizeof(ctab->atoms[ii].symbol));
    1219   [ +  +  -  + ]:        8709 :             if (2 == strlen(ctab->atoms[ii].symbol) && isupper(UCINT ctab->atoms[ii].symbol[1]))
    1220                 :             :             {
    1221                 :           0 :                 ctab->atoms[ii].symbol[1] = (char)tolower(UCINT ctab->atoms[ii].symbol[1]); /* 5-4-99 DCh*/
    1222                 :             :             }
    1223                 :        8709 :             ctab->atoms[ii].fx = fx;
    1224                 :        8709 :             ctab->atoms[ii].fy = fy;
    1225                 :        8709 :             ctab->atoms[ii].fz = fz;
    1226                 :             : 
    1227                 :             :             /* Read key-val pairs if any */
    1228   [ +  +  +  - ]:        8845 :             while (p && (len = MolfileV3000ReadKeyword(field, &p)) > 0) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    1229                 :             :             {
    1230                 :             : 
    1231                 :             :                 int itmp;
    1232                 :             :                 char ctmp;
    1233                 :             :                 char stmp[MOL_FMT_V3000_MAXFIELDLEN];
    1234                 :             : 
    1235                 :         136 :                 failed = 0;
    1236         [ +  + ]:         136 :                 if (!strcmp(field, "CHG"))
    1237                 :             :                 {
    1238         [ -  + ]:          16 :                     if (0 > MolfileV3000ReadField(&ctab->atoms[ii].charge, MOL_FMT_CHAR_INT_DATA, &p))
    1239                 :             :                     {
    1240                 :           0 :                         failed = 1;
    1241                 :             :                     }
    1242                 :             :                 }
    1243         [ -  + ]:         120 :                 else if (!strcmp(field, "RAD"))
    1244                 :             :                 {
    1245         [ #  # ]:           0 :                     if (0 > MolfileV3000ReadField(&ctab->atoms[ii].radical, MOL_FMT_CHAR_INT_DATA, &p))
    1246                 :             :                     {
    1247                 :           0 :                         failed = 1;
    1248                 :             :                     }
    1249                 :             :                 }
    1250         [ +  + ]:         120 :                 else if (!strcmp(field, "CFG"))
    1251                 :             :                 {
    1252         [ -  + ]:         112 :                     if (0 > MolfileV3000ReadField(&ctab->atoms[ii].stereo_parity, MOL_FMT_CHAR_INT_DATA, &p))
    1253                 :             :                     {
    1254                 :           0 :                         failed = 1;
    1255                 :             :                     }
    1256                 :             :                 }
    1257                 :             : 
    1258         [ +  + ]:           8 :                 else if (!strcmp(field, "MASS"))
    1259                 :             :                 {
    1260                 :             :                     /*
    1261                 :             :                         Default = natural abundance
    1262                 :             :                         A specified value indicates the absolute
    1263                 :             :                         atomic weight of the designated atom.
    1264                 :             :                     */
    1265                 :             :                     S_SHORT iso_mass;
    1266         [ -  + ]:           5 :                     if (0 > MolfileV3000ReadField(&iso_mass, MOL_FMT_SHORT_INT_DATA, &p))
    1267                 :             :                     {
    1268                 :           0 :                         failed = 1;
    1269                 :           0 :                         TREAT_ERR(err, 0, "Isotopic data not recognized:");
    1270                 :           0 :                         AddErrorMessage(pStrErr, line);
    1271                 :             :                         /* ignore isotopic error for now */
    1272                 :             :                     }
    1273                 :             :                     else
    1274                 :             :                     {
    1275                 :             :                         /*  What we read is an absolute isotopic mass, by V3000 spec.
    1276                 :             :                             Adjust this to old convention for further processing:
    1277                 :             :                             set 'ctab->atoms[ii].mass_difference' to 127
    1278                 :             :                             if isotopic mass is the same as element mass
    1279                 :             :                             in Periodic Table (rounded avg by all isotopes), 'atw'
    1280                 :             :                             delta otherwise, the value of difference 'delta' = ( isotopic mass - 'atw')
    1281                 :             :                         */
    1282                 :             :                         int atw, delta;
    1283                 :           5 :                         atw = get_atomic_mass(ctab->atoms[ii].symbol);
    1284                 :           5 :                         delta = (int)iso_mass - atw;
    1285         [ +  - ]:           5 :                         ctab->atoms[ii].mass_difference = (char)(delta ? delta : ZERO_ATW_DIFF);
    1286                 :             :                     }
    1287                 :             :                 }
    1288                 :             : 
    1289         [ +  - ]:           3 :                 else if (!strcmp(field, "VAL"))
    1290                 :             :                 {
    1291         [ -  + ]:           3 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1292                 :             :                     {
    1293                 :           0 :                         failed = 1;
    1294                 :             :                     }
    1295                 :             :                     else
    1296                 :             :                     {
    1297                 :             :                         /* adjust to old convention: was 15 for zero, now -1 for zero */
    1298         [ -  + ]:           3 :                         if (itmp == -1)
    1299                 :             :                         {
    1300                 :           0 :                             ctmp = 15;
    1301                 :             :                         }
    1302                 :             :                         else
    1303                 :             :                         {
    1304                 :           3 :                             ctmp = (char)itmp;
    1305                 :             :                         }
    1306                 :           3 :                         ctab->atoms[ii].valence = ctmp;
    1307                 :             :                     }
    1308                 :             :                 }
    1309         [ #  # ]:           0 :                 else if (!strcmp(field, "HCOUNT"))
    1310                 :             :                 {
    1311                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1312                 :             :                     {
    1313                 :             :                         ; /* skip query-related stuff */
    1314                 :             :                     }
    1315                 :             :                 }
    1316         [ #  # ]:           0 :                 else if (!strcmp(field, "STBOX"))
    1317                 :             :                 {
    1318                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1319                 :             :                     {
    1320                 :             :                         ; /* skip for now */
    1321                 :             :                     }
    1322                 :             :                 }
    1323   [ #  #  #  # ]:           0 :                 else if (!strcmp(field, "INVRET") || !strcmp(field, "EXACHG"))
    1324                 :             :                 {
    1325                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1326                 :             :                     {
    1327                 :             :                         ; /* skip reaction-related stuff */
    1328                 :             :                     }
    1329                 :             :                 }
    1330   [ #  #  #  #  :           0 :                 else if (!strcmp(field, "SUBST") || !strcmp(field, "UNSAT") || !strcmp(field, "RBCNT"))
                   #  # ]
    1331                 :             :                 {
    1332                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1333                 :             :                     {
    1334                 :             :                         ; /* skip query-related stuff */
    1335                 :             :                     }
    1336                 :             :                 }
    1337         [ #  # ]:           0 :                 else if (!strcmp(field, "ATTCHPT"))
    1338                 :             :                 {
    1339                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1340                 :             :                     {
    1341                 :             :                         ;
    1342                 :             :                     }
    1343                 :             :                 }
    1344         [ #  # ]:           0 :                 else if (!strcmp(field, "RGROUPS"))
    1345                 :             :                 {
    1346                 :           0 :                     if (0 > MolfileV3000ReadField(&stmp, MOL_FMT_STRING_DATA, &p))
    1347                 :             :                     {
    1348                 :             :                         ;
    1349                 :             :                     }
    1350                 :             :                 }
    1351         [ #  # ]:           0 :                 else if (!strcmp(field, "ATTCHORD"))
    1352                 :             :                 {
    1353                 :           0 :                     if (0 > MolfileV3000ReadField(&stmp, MOL_FMT_STRING_DATA, &p))
    1354                 :             :                     {
    1355                 :             :                         ;
    1356                 :             :                     }
    1357                 :             :                 }
    1358         [ #  # ]:           0 :                 else if (!strcmp(field, "CLASS"))
    1359                 :             :                 {
    1360                 :           0 :                     if (0 > MolfileV3000ReadField(&stmp, MOL_FMT_STRING_DATA, &p))
    1361                 :             :                     {
    1362                 :             :                         ;
    1363                 :             :                     }
    1364                 :             :                 }
    1365         [ #  # ]:           0 :                 else if (!strcmp(field, "SEQID"))
    1366                 :             :                 {
    1367                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1368                 :             :                     {
    1369                 :             :                         ;
    1370                 :             :                     }
    1371                 :             :                 }
    1372                 :             : 
    1373         [ -  + ]:         136 :                 if (failed)
    1374                 :             :                 {
    1375                 :           0 :                     err = 4;
    1376         [ #  # ]:           0 :                     TREAT_ERR(err, 4, "Cannot interpret V3000 atom block key-value pair");
    1377                 :           0 :                     dotify_non_printable_chars(line);
    1378                 :           0 :                     AddErrorMessage(pStrErr, line);
    1379                 :             : 
    1380         [ #  # ]:           0 :                     if (!strcmp(line, SD_FMT_END_OF_DATA))
    1381                 :             :                     {
    1382                 :           0 :                         err = -abs(err);
    1383                 :           0 :                         break;
    1384                 :             :                     }
    1385                 :           0 :                     continue;
    1386                 :             :                 }
    1387                 :             :             }
    1388                 :             :         } /* if ( NULL != ctab->atoms )  */
    1389                 :             :     } /* for ( i = 0; i < ctab->n_atoms; i++ )  */
    1390                 :             : 
    1391         [ +  + ]:          76 :     if (ctab->v3000->n_star_atoms)
    1392                 :             :     {
    1393                 :           3 :         AddErrorMessage(pStrErr, "V3000 star atoms ignored if MolecularInorganics or NPZz parameter not used");        /* @nnuk : when using /MolecularInorganics or /NPZz, star atoms will not be ignored */
    1394                 :           3 :         ctab->n_atoms = ctab->v3000->n_non_star_atoms;
    1395                 :             :     }
    1396                 :             : 
    1397                 :             :     /* Check for proper finish */
    1398                 :             : 
    1399                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1400                 :          76 :     inchi_strbuf_reset(pin);
    1401                 :             : 
    1402                 :          76 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1403                 :             : 
    1404         [ -  + ]:          76 :     if (nc < 1)
    1405                 :             :     {
    1406                 :           0 :         p = NULL;
    1407                 :             :     }
    1408                 :             :     else
    1409                 :             :     {
    1410                 :          76 :         p = line = pin->pStr;
    1411                 :             :     }
    1412   [ +  -  -  + ]:          76 :     if (!p || strcmp(p, "END ATOM"))
    1413                 :             :     {
    1414         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 Atom block end marker");
    1415                 :             :     }
    1416                 :          76 :     remove_one_lf(line);
    1417                 :             : 
    1418                 :          76 : err_fin:
    1419                 :          76 :     inchi_strbuf_close(pin);
    1420                 :             : 
    1421                 :          76 :     return err;
    1422                 :             : }
    1423                 :             : 
    1424                 :             : /****************************************************************************
    1425                 :             :  Read V3000 bonds
    1426                 :             : ****************************************************************************/
    1427                 :          76 : int MolfileV3000ReadBondsBlock(MOL_FMT_CTAB *ctab,
    1428                 :             :                                INCHI_IOSTREAM *inp_file,
    1429                 :             :                                int err,
    1430                 :             :                                char *pStrErr)
    1431                 :             : {
    1432                 :             :     int i;
    1433                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
    1434                 :             :     int nc;
    1435                 :          76 :     char *p = NULL, *line = NULL;
    1436                 :             :     INCHI_IOSTREAM tmpin;
    1437                 :          76 :     INCHI_IOS_STRING *pin = &tmpin.s;
    1438                 :             : 
    1439         [ +  + ]:          76 :     if (!ctab->n_bonds)
    1440                 :             :     {
    1441                 :           2 :         return 0;
    1442                 :             :     }
    1443                 :          74 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
    1444                 :             : 
    1445                 :             :     /* Check for proper start */
    1446                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1447                 :             : 
    1448                 :          74 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1449                 :             : 
    1450         [ -  + ]:          74 :     if (nc < 1)
    1451                 :             :     {
    1452                 :           0 :         p = NULL;
    1453                 :             :     }
    1454                 :             :     else
    1455                 :             :     {
    1456                 :          74 :         p = line = pin->pStr;
    1457                 :             :     }
    1458   [ +  -  -  + ]:          74 :     if (!p || strcmp(p, "BEGIN BOND"))
    1459                 :             :     {
    1460         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 Bond block start marker");
    1461                 :             :     }
    1462                 :          74 :     remove_one_lf(line);
    1463                 :             : 
    1464                 :          74 :     ctab->v3000->n_haptic_bonds = 0;
    1465                 :          74 :     ctab->v3000->n_non_haptic_bonds = 0;
    1466                 :             : 
    1467         [ +  + ]:        7727 :     for (i = 0; i < ctab->n_bonds; i++)
    1468                 :             :     {
    1469                 :        7653 :         int is_haptic = 0;
    1470                 :             : 
    1471                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1472                 :        7653 :         inchi_strbuf_reset(pin);
    1473                 :             : 
    1474                 :        7653 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1475                 :             : 
    1476         [ -  + ]:        7653 :         if (nc < 1)
    1477                 :             :         {
    1478                 :           0 :             p = NULL;
    1479                 :             :         }
    1480                 :             :         else
    1481                 :             :         {
    1482                 :        7653 :             p = line = pin->pStr;
    1483                 :             :         }
    1484         [ -  + ]:        7653 :         if (!p)
    1485                 :             :         {
    1486         [ #  # ]:           0 :             if (!err)
    1487                 :             :             {
    1488         [ #  # ]:           0 :                 TREAT_ERR(err, 2, "Cannot read V3000 bond block line"); /* djb-rwth: addressing coverity ID #499565 -- TREAT_ERR properly used */
    1489                 :             :             }
    1490                 :           0 :             break;
    1491                 :             :         }
    1492                 :        7653 :         remove_one_lf(line);
    1493                 :             : 
    1494         [ -  + ]:        7653 :         if (err)
    1495                 :             :         {
    1496         [ #  # ]:           0 :             if (!strcmp(line, SD_FMT_END_OF_DATA))
    1497                 :             :             {
    1498                 :           0 :                 err = -abs(err);
    1499                 :           0 :                 break;
    1500                 :             :             }
    1501                 :           0 :             continue;
    1502                 :             :         }
    1503                 :             : 
    1504         [ +  - ]:        7653 :         if (ctab->bonds)
    1505                 :             :         {
    1506                 :             :             int index, n_orig_at, len;
    1507                 :        7653 :             short int atnum1 = -1, atnum2 = -1;
    1508                 :        7653 :             char bond_type = 0, stereo = 0;
    1509                 :        7653 :             int failed = 0;
    1510                 :             :             /* djb-rwth: removing redundant variables */
    1511                 :             : 
    1512                 :        7653 :             n_orig_at = ctab->v3000->n_non_star_atoms + ctab->v3000->n_star_atoms;
    1513                 :             : 
    1514                 :             :             /* read positional parameters */
    1515         [ -  + ]:        7653 :             if (0 > MolfileV3000ReadField(&index, MOL_FMT_INT_DATA, &p))
    1516                 :             :             {
    1517                 :           0 :                 failed = 1;
    1518                 :             :             }
    1519         [ -  + ]:        7653 :             else if (0 > MolfileV3000ReadField(&bond_type, MOL_FMT_CHAR_INT_DATA, &p))
    1520                 :             :             {
    1521                 :           0 :                 failed = 1;
    1522                 :             :             }
    1523         [ -  + ]:        7653 :             else if (0 > MolfileV3000ReadField(&atnum1, MOL_FMT_SHORT_INT_DATA, &p))
    1524                 :             :             {
    1525                 :           0 :                 failed = 1;
    1526                 :             :             }
    1527         [ -  + ]:        7653 :             else if (0 > MolfileV3000ReadField(&atnum2, MOL_FMT_SHORT_INT_DATA, &p))
    1528                 :             :             {
    1529                 :           0 :                 failed = 1;
    1530                 :             :             }
    1531                 :             : 
    1532                 :       15306 :             atnum1 = get_actual_atom_number(atnum1, n_orig_at,
    1533                 :        7653 :                                             ctab->v3000->atom_index_orig,
    1534                 :        7653 :                                             ctab->v3000->atom_index_fin);
    1535                 :             : 
    1536                 :       15306 :             atnum2 = get_actual_atom_number(atnum2, n_orig_at,
    1537                 :        7653 :                                             ctab->v3000->atom_index_orig,
    1538                 :        7653 :                                             ctab->v3000->atom_index_fin);
    1539                 :             : 
    1540   [ +  +  -  + ]:        7653 :             if ((atnum1 < 0) && (atnum2 < 0))
    1541                 :             :             {
    1542                 :           0 :                 failed = 1;
    1543                 :             :             }
    1544                 :             :             /* djb-rwth: removing redundant code */
    1545                 :             : 
    1546         [ -  + ]:        7653 :             if (failed)
    1547                 :             :             {
    1548                 :             : 
    1549         [ #  # ]:           0 :                 if (!err)
    1550                 :             :                 {
    1551                 :             :                     /* can't interpret bonds block line */
    1552         [ #  # ]:           0 :                     TREAT_ERR(err, 4, "Cannot interpret V3000 bond block line:");
    1553                 :           0 :                     dotify_non_printable_chars(line);
    1554                 :           0 :                     AddErrorMessage(pStrErr, line);
    1555                 :             :                 }
    1556         [ #  # ]:           0 :                 if (!strcmp(line, SD_FMT_END_OF_DATA))
    1557                 :             :                 {
    1558                 :           0 :                     err = -abs(err);
    1559                 :           0 :                     break;
    1560                 :             :                 }
    1561                 :             :             }
    1562                 :             : 
    1563                 :             :             /* TODO: treat new bond types  9 10 */
    1564                 :             :             /* read key-val pairs if any */
    1565   [ +  +  +  - ]:        7847 :             while (p && (len = MolfileV3000ReadKeyword(field, &p)) > 0) /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    1566                 :             :             {
    1567                 :             : 
    1568                 :             :                 int itmp;
    1569                 :             :                 char stmp[MOL_FMT_V3000_MAXFIELDLEN];
    1570                 :         194 :                 failed = 0;
    1571                 :             : 
    1572         [ +  + ]:         194 :                 if (!strcmp(field, "CFG"))
    1573                 :             :                 {
    1574         [ -  + ]:         189 :                     if (0 > MolfileV3000ReadField(&stereo, MOL_FMT_CHAR_INT_DATA, &p))
    1575                 :             :                     {
    1576                 :           0 :                         failed = 1;
    1577                 :             :                     }
    1578                 :             :                     else
    1579                 :             :                     {
    1580                 :             :                         /*    adjust stereo to old convention for wedges which was:
    1581                 :             :                                 0 = not stereo, 1 = Up,  4 = Either, 6 = Down
    1582                 :             :                             now:
    1583                 :             :                                 0 = none (default), 1 = up, 2 = either, 3 = down
    1584                 :             :                         */
    1585         [ -  + ]:         189 :                         if (stereo == 2)
    1586                 :             :                         {
    1587                 :           0 :                             stereo = 4;
    1588                 :             :                         }
    1589         [ +  + ]:         189 :                         else if (stereo == 3)
    1590                 :             :                         {
    1591                 :          32 :                             stereo = 6;
    1592                 :             :                         }
    1593                 :             :                     }
    1594                 :             :                 }
    1595         [ -  + ]:           5 :                 else if (!strcmp(field, "TOPO"))
    1596                 :             :                 {
    1597                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1598                 :             :                     {
    1599                 :             :                         ; /* skip query-related stuff */
    1600                 :             :                     }
    1601                 :             :                 }
    1602         [ -  + ]:           5 :                 else if (!strcmp(field, "RXCTR"))
    1603                 :             :                 {
    1604                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1605                 :             :                     {
    1606                 :             :                         ; /* skip reaction-related stuff */
    1607                 :             :                     }
    1608                 :             :                 }
    1609         [ -  + ]:           5 :                 else if (!strcmp(field, "STBOX"))
    1610                 :             :                 {
    1611                 :           0 :                     if (0 > MolfileV3000ReadField(&itmp, MOL_FMT_INT_DATA, &p))
    1612                 :             :                     {
    1613                 :             :                         ; /* skip for now */
    1614                 :             :                     }
    1615                 :             :                 }
    1616         [ +  + ]:           5 :                 else if (!strcmp(field, "ENDPTS"))
    1617                 :             :                 {
    1618                 :           3 :                     int res, *num_list = NULL;
    1619         [ -  + ]:           3 :                     if (0 > MolfileV3000ReadHapticBond(ctab, &p, &num_list, pStrErr))
    1620                 :             :                     {
    1621                 :           0 :                         failed = 1;
    1622                 :             :                     }
    1623         [ -  + ]:           3 :                     else if (!num_list)
    1624                 :             :                     {
    1625                 :           0 :                         failed = 1;
    1626                 :             :                     }
    1627                 :             :                     else
    1628                 :             :                     {
    1629                 :           3 :                         int existent_atom = atnum1;
    1630         [ -  + ]:           3 :                         if (existent_atom < 0)
    1631                 :             :                         {
    1632                 :           0 :                             existent_atom = atnum2;
    1633                 :             :                         }
    1634         [ -  + ]:           3 :                         if (existent_atom < 0) /* should not be here */
    1635                 :             :                          {
    1636                 :           0 :                             failed = 1;
    1637                 :             :                         }
    1638                 :             :                         else
    1639                 :             :                         {
    1640                 :             :                             int k, nnum;
    1641                 :           3 :                             nnum = num_list[2];
    1642                 :           3 :                             num_list[1] = existent_atom;
    1643         [ +  + ]:          15 :                             for (k = 3; k < nnum + 3; k++)              /* @nnuk : wrong mapping of endpoints (+3 added for full endpoints mapping for all haptic bonds) */
    1644                 :             :                             {
    1645                 :          12 :                                 num_list[k] = get_actual_atom_number(num_list[k],
    1646                 :             :                                                                      n_orig_at,
    1647                 :          12 :                                                                      ctab->v3000->atom_index_orig,
    1648                 :          12 :                                                                      ctab->v3000->atom_index_fin);
    1649                 :             :                             }
    1650                 :           3 :                             res = NumLists_Append(ctab->v3000->haptic_bonds, num_list);
    1651         [ -  + ]:           3 :                             if (res < 0)
    1652                 :             :                             {
    1653                 :           0 :                                 failed = 1;
    1654                 :             :                             }
    1655                 :             :                             else
    1656                 :             :                             {
    1657                 :           3 :                                 is_haptic = 1;
    1658                 :             :                             }
    1659                 :             :                         }
    1660                 :             :                     }
    1661                 :             :                     /* djb-rwth: addressing coverity ID #499489 -- false positive as num_atoms allocated in MolfileV3000ReadHapticBond and returns a value in this block */
    1662                 :             :                 }
    1663         [ +  - ]:           2 :                 else if (!strcmp(field, "DISP"))
    1664                 :             :                 {
    1665                 :           2 :                     if (0 > MolfileV3000ReadField(&stmp, MOL_FMT_STRING_DATA, &p))
    1666                 :             :                     {
    1667                 :             :                         ;
    1668                 :             :                     }
    1669                 :             :                 }
    1670         [ #  # ]:           0 :                 else if (!strcmp(field, "ATTACH"))
    1671                 :             :                 {
    1672                 :           0 :                     if (0 > MolfileV3000ReadField(&stmp, MOL_FMT_STRING_DATA, &p))
    1673                 :             :                     {
    1674                 :             :                         ;
    1675                 :             :                     }
    1676                 :             :                 }
    1677                 :             : 
    1678         [ -  + ]:         194 :                 if (failed)
    1679                 :             :                 {
    1680         [ #  # ]:           0 :                     if (!err)
    1681                 :             :                     {
    1682                 :             :                         /* can't interpret bonds block line */
    1683         [ #  # ]:           0 :                         TREAT_ERR(err, 4, "Cannot interpret V3000 bond block line:");
    1684                 :           0 :                         dotify_non_printable_chars(line);
    1685                 :           0 :                         AddErrorMessage(pStrErr, line);
    1686                 :             :                     }
    1687         [ #  # ]:           0 :                     if (!strcmp(line, SD_FMT_END_OF_DATA))
    1688                 :             :                     {
    1689                 :           0 :                         err = -abs(err);
    1690                 :           0 :                         break;
    1691                 :             :                     }
    1692                 :             :                 }
    1693                 :             :             } /* while ( p && (len=MolfileV3000ReadKeyword(field, &p)) > 0 ) */
    1694                 :             : 
    1695         [ +  + ]:        7653 :             if (is_haptic)
    1696                 :           3 :             {
    1697                 :           3 :                 int ii = ctab->v3000->n_haptic_bonds;
    1698                 :           3 :                 ctab->v3000->haptic_bonds->lists[ii][0] = bond_type;
    1699                 :           3 :                 ctab->v3000->n_haptic_bonds++;
    1700                 :           3 :                 continue;
    1701                 :             :             }
    1702                 :             :             else
    1703                 :             :             {
    1704                 :        7650 :                 int ii = ctab->v3000->n_non_haptic_bonds;
    1705                 :        7650 :                 ctab->bonds[ii].atnum1 = atnum1;
    1706                 :        7650 :                 ctab->bonds[ii].atnum2 = atnum2;
    1707                 :        7650 :                 ctab->bonds[ii].bond_type = bond_type;
    1708                 :        7650 :                 ctab->bonds[ii].bond_stereo = stereo;
    1709                 :        7650 :                 ctab->v3000->n_non_haptic_bonds++;
    1710                 :             :             }
    1711                 :             :         } /* if ctab->bonds */
    1712                 :             :     } /* for ( i = 0; i < ctab->n_bonds; i++ )  */
    1713                 :             : 
    1714         [ +  + ]:          74 :     if (ctab->v3000->n_haptic_bonds)
    1715                 :             :     {
    1716                 :           2 :         AddErrorMessage(pStrErr, "V3000 haptic bonds read/stored and ignored if MolecularInorganics or NPZz parameter is not used");    /* @nnuk : when using /MolecularInorganics or /NPZz, haptic bonds will not be ignored */
    1717                 :           2 :         ctab->n_bonds = ctab->v3000->n_non_haptic_bonds;
    1718                 :             :     }
    1719                 :             : 
    1720                 :             :     /* Check for proper finish */
    1721                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1722                 :          74 :     inchi_strbuf_reset(pin);
    1723                 :             : 
    1724                 :          74 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1725                 :             : 
    1726         [ -  + ]:          74 :     if (nc < 1)
    1727                 :             :     {
    1728                 :           0 :         p = NULL;
    1729                 :             :     }
    1730                 :             :     else
    1731                 :             :     {
    1732                 :          74 :         p = line = pin->pStr;
    1733                 :             :     }
    1734   [ +  -  -  + ]:          74 :     if (!p || strcmp(p, "END BOND"))
    1735                 :             :     {
    1736         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 Bond block end marker");
    1737                 :             :     }
    1738                 :          74 :     remove_one_lf(line);
    1739                 :             : 
    1740                 :          74 : err_fin:
    1741                 :             : 
    1742                 :          74 :     inchi_ios_close(&tmpin); /* ricrogz: fixing memory leak */
    1743                 :          74 :     return err;
    1744                 :             : }
    1745                 :             : 
    1746                 :             : /****************************************************************************
    1747                 :             :  Convert atom index to the final consequitive atom number (starting from 1)
    1748                 :             :  Returns -1 for star atom or not found index
    1749                 :             : ****************************************************************************/
    1750                 :       15362 : int get_actual_atom_number(int index, int n, int *orig, int *fin)
    1751                 :             : {
    1752                 :             :     int i;
    1753         [ +  - ]:     7017638 :     for (i = 0; i < n; i++)
    1754                 :             :     {
    1755         [ +  + ]:     7017638 :         if (orig[i] == index)
    1756                 :             :         {
    1757                 :       15362 :             return fin[i];
    1758                 :             :         }
    1759                 :             :     }
    1760                 :             : 
    1761                 :           0 :     return -1;
    1762                 :             : }
    1763                 :             : 
    1764                 :             : /****************************************************************************
    1765                 :             :  Read V3000 tail of CTab
    1766                 :             : ****************************************************************************/
    1767                 :          76 : int MolfileV3000ReadTailOfCTAB(MOL_FMT_CTAB *ctab,
    1768                 :             :                                INCHI_IOSTREAM *inp_file,
    1769                 :             :                                int treat_polymers,
    1770                 :             :                                int err,
    1771                 :             :                                char *pStrErr)
    1772                 :             : {
    1773                 :          76 :     int retcode = err;
    1774                 :             :     int nc;
    1775                 :          76 :     char *p = NULL, *line = NULL;
    1776                 :             :     INCHI_IOSTREAM tmpin;
    1777                 :          76 :     INCHI_IOS_STRING *pin = &tmpin.s;
    1778                 :          76 :     inchi_ios_init(&tmpin, INCHI_IOS_TYPE_STRING, NULL);
    1779                 :             : 
    1780                 :             :     /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1781                 :             : 
    1782                 :          76 :     nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1783                 :             : 
    1784         [ -  + ]:          76 :     if (nc < 1)
    1785                 :             :     {
    1786                 :           0 :         p = NULL;
    1787                 :             :     }
    1788                 :             :     else
    1789                 :             :     {
    1790                 :          76 :         p = line = pin->pStr;
    1791                 :             :     }
    1792                 :          76 :     remove_one_lf(line);
    1793                 :             : 
    1794                 :             :     /* GHI #52: Accept an optional empty bond block when no bonds are declared. */
    1795   [ +  +  +  -  :          76 :     if (ctab->n_bonds == 0 && p && !strcmp(p, "BEGIN BOND"))
                   -  + ]
    1796                 :             :     {
    1797                 :           0 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1798                 :             : 
    1799   [ #  #  #  # ]:           0 :         if (nc < 1 || strcmp(pin->pStr, "END BOND"))
    1800                 :             :         {
    1801         [ #  # ]:           0 :             TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 Bond block end marker");
    1802                 :             :         }
    1803                 :             : 
    1804                 :             :         /* Advance to the next CTAB section or END CTAB. */
    1805                 :           0 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1806                 :             : 
    1807         [ #  # ]:           0 :         if (nc < 1)
    1808                 :             :         {
    1809                 :           0 :             p = NULL;
    1810                 :             :         }
    1811                 :             :         else
    1812                 :             :         {
    1813                 :           0 :             p = line = pin->pStr;
    1814                 :           0 :             remove_one_lf(line);
    1815                 :             :         }
    1816                 :             :     }
    1817                 :             : 
    1818   [ +  -  +  + ]:          76 :     if (p && !strcmp(p, "BEGIN SGROUP"))
    1819                 :             :     {
    1820                 :           7 :         retcode = MolfileV3000ReadSGroup(ctab, inp_file, treat_polymers, retcode, pStrErr);
    1821         [ +  + ]:           7 :         if (retcode)
    1822                 :             :         {
    1823                 :             :             /* GH #146: return the error; message already in pStrErr */
    1824                 :           3 :             err = retcode + 70;
    1825                 :           3 :             goto err_fin;
    1826                 :             :         }
    1827                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1828                 :           4 :         inchi_strbuf_reset(pin);
    1829                 :           4 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1830         [ -  + ]:           4 :         if (nc < 1)
    1831                 :             :         {
    1832                 :           0 :             p = NULL;
    1833                 :             :         }
    1834                 :             :         else
    1835                 :             :         {
    1836                 :           4 :             p = line = pin->pStr;
    1837                 :           4 :             remove_one_lf(line);
    1838                 :             :         }
    1839                 :             :     }
    1840                 :             : 
    1841   [ +  -  -  + ]:          73 :     if (p && !strcmp(p, "BEGIN OBJ3D"))
    1842                 :             :     {
    1843                 :           0 :         retcode = MolfileV3000Read3DBlock(ctab, inp_file, retcode, pStrErr);
    1844         [ #  # ]:           0 :         if (retcode)
    1845                 :             :         {
    1846                 :           0 :             retcode += 70;
    1847         [ #  # ]:           0 :             TREAT_ERR_AND_FIN(retcode, 1, err_fin, pStrErr);
    1848                 :             :         }
    1849                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1850                 :           0 :         inchi_strbuf_reset(pin);
    1851                 :             : 
    1852                 :           0 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1853         [ #  # ]:           0 :         if (nc < 1)
    1854                 :             :         {
    1855                 :           0 :             p = NULL;
    1856                 :             :         }
    1857                 :             :         else
    1858                 :             :         {
    1859                 :           0 :             p = line = pin->pStr;
    1860                 :           0 :             remove_one_lf(line);
    1861                 :             :         }
    1862                 :             :     }
    1863                 :             : 
    1864   [ +  -  -  + ]:          73 :     while (p && !strcmp(p, "LINKNODE"))
    1865                 :             :     {
    1866                 :             :         /* skip for now */
    1867                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1868                 :           0 :         inchi_strbuf_reset(pin);
    1869                 :             : 
    1870                 :           0 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1871         [ #  # ]:           0 :         if (nc < 1)
    1872                 :             :         {
    1873                 :           0 :             p = NULL;
    1874                 :             :         }
    1875                 :             :         else
    1876                 :             :         {
    1877                 :           0 :             p = line = pin->pStr;
    1878                 :           0 :             remove_one_lf(line);
    1879                 :             :         }
    1880                 :             :     }
    1881                 :             : 
    1882                 :             :     /* Collections */
    1883   [ +  -  +  + ]:         110 :     while (p && !strcmp(p, "BEGIN COLLECTION"))
    1884                 :             :     {
    1885                 :          37 :         retcode = MolfileV3000ReadCollections(ctab, inp_file, retcode, pStrErr);
    1886         [ -  + ]:          37 :         if (retcode)
    1887                 :             :         {
    1888                 :           0 :             retcode += 70;
    1889         [ #  # ]:           0 :             TREAT_ERR_AND_FIN(retcode, 1, err_fin, pStrErr);
    1890                 :             :         }
    1891                 :             :         /*p = inchi_fgetsLf_V3000( line, inp_file );*/
    1892                 :          37 :         inchi_strbuf_reset(pin);
    1893                 :             : 
    1894                 :          37 :         nc = get_V3000_input_line_to_strbuf(pin, inp_file);
    1895                 :             : 
    1896         [ -  + ]:          37 :         if (nc < 1)
    1897                 :             :         {
    1898                 :           0 :             p = NULL;
    1899                 :             :         }
    1900                 :             :         else
    1901                 :             :         {
    1902                 :          37 :             p = line = pin->pStr;
    1903                 :          37 :             remove_one_lf(line);
    1904                 :             :         }
    1905                 :             :     }
    1906                 :             : 
    1907   [ +  -  -  + ]:          73 :     if (!p || strcmp(p, "END CTAB"))
    1908                 :             :     {
    1909         [ #  # ]:           0 :         TREAT_ERR_AND_FIN(err, 1, err_fin, "Error: No V3000 CTAB end marker");
    1910                 :             :     }
    1911                 :             : 
    1912                 :          73 :     remove_one_lf(line);
    1913                 :             : 
    1914                 :          76 : err_fin:
    1915                 :          76 :     inchi_strbuf_close(pin);
    1916                 :             : 
    1917                 :          76 :     return err;
    1918                 :             : }
    1919                 :             : 
    1920                 :             : /****************************************************************************
    1921                 :             :  Read haptic bond info
    1922                 :             : ****************************************************************************/
    1923                 :           3 : int MolfileV3000ReadHapticBond(MOL_FMT_CTAB *ctab,
    1924                 :             :                                char **line_ptr,
    1925                 :             :                                int **num_list,
    1926                 :             :                                char *pStrErr)
    1927                 :             : {
    1928                 :           3 :     int nread = 0;
    1929                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
    1930                 :           3 :     const int max_field_len = sizeof(field);
    1931                 :             :     char *p_end;
    1932                 :           3 :     int i, nnum = 0;
    1933                 :             : 
    1934                 :           3 :     *num_list = NULL;
    1935                 :             : 
    1936                 :           3 :     memset(field, 0, max_field_len); /* djb-rwth: memset_s C11/Annex K variant? */
    1937                 :             : 
    1938                 :           3 :     nread = read_upto_delim(line_ptr, field, max_field_len, "1234567890 \t\n\v\f\r"); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    1939         [ -  + ]:           3 :     if (strcmp(field, "("))
    1940                 :             :     {
    1941                 :           0 :         return -1;
    1942                 :             :     }
    1943                 :             : 
    1944                 :           3 :     nread = read_upto_delim(line_ptr, field, max_field_len, " \t\n\v\f\r"); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    1945                 :             : 
    1946                 :           3 :     nnum = strtol(field, &p_end, 10);
    1947                 :             : 
    1948         [ -  + ]:           3 :     if (p_end == field)
    1949                 :             :     {
    1950                 :           0 :         return -1; /* paranoia */
    1951                 :             :     }
    1952         [ -  + ]:           3 :     if (nnum < 0)
    1953                 :             :     {
    1954                 :           0 :         return -1;
    1955                 :             :     }
    1956                 :             : 
    1957                 :           3 :     *num_list = (int *)inchi_calloc((long long)nnum + 3, sizeof(int)); /* djb-rwth: cast operator added */
    1958                 :             : 
    1959         [ -  + ]:           3 :     if (!*num_list)
    1960                 :             :     {
    1961                 :           0 :         nread = -1;
    1962                 :           0 :         goto ret;
    1963                 :             :     }
    1964                 :             : 
    1965                 :           3 :     (*num_list)[0] = -1; /* will be bond type, to be filled by caller */
    1966                 :           3 :     (*num_list)[1] = -1; /* will be atom number, to be filled by caller */
    1967                 :           3 :     (*num_list)[2] = nnum;
    1968                 :             : 
    1969         [ +  + ]:          15 :     for (i = 3; i < nnum + 3; i++)
    1970                 :             :     {
    1971         [ -  + ]:          12 :         if (0 > MolfileV3000ReadField(&((*num_list)[i]), MOL_FMT_INT_DATA, line_ptr))
    1972                 :             :         {
    1973                 :           0 :             nread = -1;
    1974                 :           0 :             goto ret;
    1975                 :             :         }
    1976                 :             :     }
    1977                 :             : 
    1978                 :             :     /* ')' should have been consumed  by strtol */
    1979                 :             : 
    1980                 :             :     /* check for ATTACH=ALL */
    1981                 :             : 
    1982                 :           3 :     nread = read_upto_delim(line_ptr, field, max_field_len, " \t\n\v\f\r");
    1983         [ -  + ]:           3 :     if (nread > 0)
    1984                 :             :     {
    1985         [ +  - ]:           3 :         if (strcmp(field, "ATTACH=ALL"))
    1986                 :             :         {
    1987                 :           0 :             nread = -1;
    1988                 :           0 :             goto ret;
    1989                 :             :         }
    1990                 :             :     }
    1991                 :             : 
    1992                 :           3 : ret:
    1993         [ -  + ]:           3 :     if (nread < 0)
    1994                 :             :     {
    1995         [ #  # ]:           0 :         if (*num_list)
    1996                 :             :         {
    1997         [ #  # ]:           0 :             inchi_free(*num_list);
    1998                 :           0 :             *num_list = NULL;
    1999                 :             :         }
    2000                 :             :     }
    2001                 :             : 
    2002                 :           3 :     return nread;
    2003                 :             : }
    2004                 :             : 
    2005                 :             : /****************************************************************************
    2006                 :             :  Read V3000 stereo collection
    2007                 :             : ****************************************************************************/
    2008                 :         113 : int MolfileV3000ReadStereoCollection(MOL_FMT_CTAB *ctab,
    2009                 :             :                                      char **line_ptr,
    2010                 :             :                                      int **num_list,
    2011                 :             :                                      char *pStrErr)
    2012                 :             : {
    2013                 :         113 :     int nread = 0;
    2014                 :             :     char field[MOL_FMT_V3000_MAXFIELDLEN];
    2015                 :         113 :     const int max_field_len = sizeof(field);
    2016                 :             :     char *p_end;
    2017                 :         113 :     int i, nnum = 0;
    2018                 :             : 
    2019                 :         113 :     *num_list = NULL;
    2020                 :             : 
    2021                 :         113 :     memset(field, 0, max_field_len); /* djb-rwth: memset_s C11/Annex K variant? */
    2022                 :             : 
    2023                 :         113 :     nread = read_upto_delim(line_ptr, field, max_field_len, "1234567890 \t\n\v\f\r"); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
    2024         [ -  + ]:         113 :     if (strcmp(field, "("))
    2025                 :             :     {
    2026                 :           0 :         return -1;
    2027                 :             :     }
    2028                 :             : 
    2029                 :         113 :     nread = read_upto_delim(line_ptr, field, max_field_len, " \t\n\v\f\r");
    2030                 :             : 
    2031                 :         113 :     nnum = strtol(field, &p_end, 10);
    2032                 :             : 
    2033         [ -  + ]:         113 :     if (p_end == field)
    2034                 :             :     {
    2035                 :           0 :         return -1; /* paranoia */
    2036                 :             :     }
    2037         [ -  + ]:         113 :     if (nnum < 0)
    2038                 :             :     {
    2039                 :           0 :         return -1;
    2040                 :             :     }
    2041                 :             : 
    2042                 :         113 :     *num_list = (int *)inchi_calloc((long long)nnum + 3, sizeof(int)); /* djb-rwth: cast operator added */
    2043                 :             : 
    2044         [ -  + ]:         113 :     if (!*num_list)
    2045                 :             :     {
    2046                 :           0 :         nread = -1;
    2047                 :           0 :         goto ret;
    2048                 :             :     }
    2049                 :             : 
    2050                 :         113 :     (*num_list)[0] = -1; /* reserved, may be filled by caller */
    2051                 :         113 :     (*num_list)[1] = nnum;
    2052                 :             : 
    2053         [ +  + ]:         319 :     for (i = 2; i < nnum + 2; i++)
    2054                 :             :     {
    2055         [ -  + ]:         206 :         if (0 > MolfileV3000ReadField(&((*num_list)[i]), MOL_FMT_INT_DATA, line_ptr))
    2056                 :             :         {
    2057                 :           0 :             nread = -1;
    2058                 :           0 :             goto ret;
    2059                 :             :         }
    2060                 :             :     }
    2061                 :             : 
    2062                 :             :     /* ')' should have been consumed  by strtol */
    2063                 :             : 
    2064                 :         113 : ret:
    2065         [ -  + ]:         113 :     if (nread < 0)
    2066                 :             :     {
    2067         [ #  # ]:           0 :         if (*num_list)
    2068                 :             :         {
    2069         [ #  # ]:           0 :             inchi_free(*num_list);
    2070                 :           0 :             *num_list = NULL;
    2071                 :             :         }
    2072                 :             :     }
    2073                 :             : 
    2074                 :         113 :     return nread;
    2075                 :             : }
    2076                 :             : 
    2077                 :             : /****************************************************************************
    2078                 :             :     Returns -1 @ error
    2079                 :             : ****************************************************************************/
    2080                 :       17089 : int get_V3000_input_line_to_strbuf(INCHI_IOS_STRING *buf,
    2081                 :             :                                    INCHI_IOSTREAM *inp_stream)
    2082                 :             : {
    2083                 :       17089 :     const int prefix_len = 7; /* "M  V30 " */
    2084                 :       17089 :     int old_used, crlf2lf = 1, preserve_lf = 0;
    2085                 :             : 
    2086                 :       17089 :     inchi_strbuf_reset(buf);
    2087                 :             : 
    2088                 :       17089 :     old_used = buf->nUsedLength;
    2089                 :             :     while (1)
    2090                 :             :     {
    2091                 :       17093 :         inchi_strbuf_addline(buf, inp_stream, crlf2lf, preserve_lf);
    2092                 :             : 
    2093         [ +  + ]:       17093 :         if (buf->nUsedLength - old_used < 8)
    2094                 :             :         {
    2095                 :           2 :             return -1;
    2096                 :             :         }
    2097         [ -  + ]:       17091 :         if (strncmp(buf->pStr + old_used, "M  V30 ", prefix_len))
    2098                 :             :         {
    2099                 :           0 :             return -1;
    2100                 :             :         }
    2101                 :             : 
    2102                 :       17091 :         memmove((void *)(buf->pStr + old_used), (void *)(buf->pStr + old_used + prefix_len), (long long)buf->nUsedLength - (long long)old_used - (long long)prefix_len + 1); /* djb-rwth: cast operators added */ /* ricrogz: fixing memory overflow error */
    2103                 :       17091 :         buf->nUsedLength -= prefix_len;
    2104                 :             : 
    2105         [ +  + ]:       17091 :         if (buf->pStr[buf->nUsedLength - 1] != '-')
    2106                 :             :         {
    2107                 :       17087 :             break;
    2108                 :             :         }
    2109                 :           4 :         buf->pStr[--buf->nUsedLength] = '\0';
    2110                 :             : 
    2111                 :           4 :         old_used = buf->nUsedLength;
    2112                 :             :     }
    2113                 :             : 
    2114                 :       17087 :     remove_trailing_spaces(buf->pStr);
    2115                 :       17087 :     buf->nUsedLength = strlen(buf->pStr);
    2116                 :             : 
    2117                 :       17087 :     return buf->nUsedLength;
    2118                 :             : }
        

Generated by: LCOV version 2.0-1