Remove unnecessary config.h includes
[alexxy/gromacs.git] / src / gromacs / topology / index.cpp
index 60bbef46d1f531be7725cacce73700badbe66187..615c38a1bf6c9c1b973c740794ca1b7f2ec273d3 100644 (file)
  * To help us fund GROMACS development, we humbly ask that you cite
  * the research papers on the package. Check out http://www.gromacs.org.
  */
-#include "gromacs/topology/index.h"
+#include "gmxpre.h"
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gromacs/topology/index.h"
 
 #include <assert.h>
 #include <ctype.h>
 #include "gromacs/topology/atoms.h"
 #include "gromacs/topology/block.h"
 #include "gromacs/topology/invblock.h"
-#include "gromacs/utility/common.h"
+#include "gromacs/topology/residuetypes.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
 
-const char gmx_residuetype_undefined[] = "Other";
-
-struct gmx_residuetype
-{
-    int      n;
-    char **  resname;
-    char **  restype;
-
-};
-
-
 static gmx_bool gmx_ask_yesno(gmx_bool bASK)
 {
     char c;
@@ -141,7 +127,7 @@ void add_grp(t_blocka *b, char ***gnames, int nra, atom_id a[], const char *name
 
     srenew(b->index, b->nr+2);
     srenew(*gnames, b->nr+1);
-    (*gnames)[b->nr] = strdup(name);
+    (*gnames)[b->nr] = gmx_strdup(name);
 
     srenew(b->a, b->nra+nra);
     for (i = 0; (i < nra); i++)
@@ -289,9 +275,9 @@ static void analyse_other(const char ** restype, t_atoms *atoms,
                 if (l == nrestp)
                 {
                     srenew(restp, nrestp+1);
-                    restp[nrestp].rname = strdup(rname);
+                    restp[nrestp].rname = gmx_strdup(rname);
                     restp[nrestp].bNeg  = FALSE;
-                    restp[nrestp].gname = strdup(rname);
+                    restp[nrestp].gname = gmx_strdup(rname);
                     nrestp++;
 
                 }
@@ -587,242 +573,9 @@ static void analyse_prot(const char ** restype, t_atoms *atoms,
 }
 
 
-
-
-/* Return 0 if the name was found, otherwise -1.
- * p_restype is set to a pointer to the type name, or 'Other' if we did not find it.
- */
-int
-gmx_residuetype_get_type(gmx_residuetype_t rt, const char * resname, const char ** p_restype)
-{
-    int    i, rc;
-
-    rc = -1;
-    for (i = 0; i < rt->n && rc; i++)
-    {
-        rc = gmx_strcasecmp(rt->resname[i], resname);
-    }
-
-    *p_restype = (rc == 0) ? rt->restype[i-1] : gmx_residuetype_undefined;
-
-    return rc;
-}
-
-int
-gmx_residuetype_add(gmx_residuetype_t rt, const char *newresname, const char *newrestype)
-{
-    int           found;
-    const char *  p_oldtype;
-
-    found = !gmx_residuetype_get_type(rt, newresname, &p_oldtype);
-
-    if (found && gmx_strcasecmp(p_oldtype, newrestype))
-    {
-        fprintf(stderr, "Warning: Residue '%s' already present with type '%s' in database, ignoring new type '%s'.",
-                newresname, p_oldtype, newrestype);
-    }
-
-    if (found == 0)
-    {
-        srenew(rt->resname, rt->n+1);
-        srenew(rt->restype, rt->n+1);
-        rt->resname[rt->n] = strdup(newresname);
-        rt->restype[rt->n] = strdup(newrestype);
-        rt->n++;
-    }
-
-    return 0;
-}
-
-
-int
-gmx_residuetype_init(gmx_residuetype_t *prt)
-{
-    FILE                 *  db;
-    char                    line[STRLEN];
-    char                    resname[STRLEN], restype[STRLEN], dum[STRLEN];
-    struct gmx_residuetype *rt;
-
-    snew(rt, 1);
-    *prt = rt;
-
-    rt->n        = 0;
-    rt->resname  = NULL;
-    rt->restype  = NULL;
-
-    db = libopen("residuetypes.dat");
-
-    while (get_a_line(db, line, STRLEN))
-    {
-        strip_comment(line);
-        trim(line);
-        if (line[0] != '\0')
-        {
-            if (sscanf(line, "%s %s %s", resname, restype, dum) != 2)
-            {
-                gmx_fatal(FARGS, "Incorrect number of columns (2 expected) for line in residuetypes.dat");
-            }
-            gmx_residuetype_add(rt, resname, restype);
-        }
-    }
-
-    fclose(db);
-
-    return 0;
-}
-
-
-
-int
-gmx_residuetype_destroy(gmx_residuetype_t rt)
-{
-    int i;
-
-    for (i = 0; i < rt->n; i++)
-    {
-        sfree(rt->resname[i]);
-        sfree(rt->restype[i]);
-    }
-    sfree(rt->resname);
-    sfree(rt->restype);
-    sfree(rt);
-
-    return 0;
-}
-
-int
-gmx_residuetype_get_alltypes(gmx_residuetype_t    rt,
-                             const char ***       p_typenames,
-                             int *                ntypes)
-{
-    int            i, n;
-    const char **  my_typename;
-
-    n           = 0;
-    my_typename = NULL;
-    for (i = 0; i < rt->n; i++)
-    {
-        const char *const p      = rt->restype[i];
-        bool              bFound = false;
-        for (int j = 0; j < n && !bFound; j++)
-        {
-            assert(my_typename != NULL);
-            bFound = !gmx_strcasecmp(p, my_typename[j]);
-        }
-        if (!bFound)
-        {
-            srenew(my_typename, n+1);
-            my_typename[n] = p;
-            n++;
-        }
-    }
-    *ntypes      = n;
-    *p_typenames = my_typename;
-
-    return 0;
-}
-
-
-
-gmx_bool
-gmx_residuetype_is_protein(gmx_residuetype_t rt, const char *resnm)
-{
-    gmx_bool    rc;
-    const char *p_type;
-
-    if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
-        gmx_strcasecmp(p_type, "Protein") == 0)
-    {
-        rc = TRUE;
-    }
-    else
-    {
-        rc = FALSE;
-    }
-    return rc;
-}
-
-gmx_bool
-gmx_residuetype_is_dna(gmx_residuetype_t rt, const char *resnm)
-{
-    gmx_bool    rc;
-    const char *p_type;
-
-    if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
-        gmx_strcasecmp(p_type, "DNA") == 0)
-    {
-        rc = TRUE;
-    }
-    else
-    {
-        rc = FALSE;
-    }
-    return rc;
-}
-
-gmx_bool
-gmx_residuetype_is_rna(gmx_residuetype_t rt, const char *resnm)
-{
-    gmx_bool    rc;
-    const char *p_type;
-
-    if (gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
-        gmx_strcasecmp(p_type, "RNA") == 0)
-    {
-        rc = TRUE;
-    }
-    else
-    {
-        rc = FALSE;
-    }
-    return rc;
-}
-
-/* Return the size of the arrays */
-int
-gmx_residuetype_get_size(gmx_residuetype_t rt)
-{
-    return rt->n;
-}
-
-/* Search for a residuetype with name resnm within the
- * gmx_residuetype database. Return the index if found,
- * otherwise -1.
- */
-int
-gmx_residuetype_get_index(gmx_residuetype_t rt, const char *resnm)
-{
-    int i, rc;
-
-    rc = -1;
-    for (i = 0; i < rt->n && rc; i++)
-    {
-        rc = gmx_strcasecmp(rt->resname[i], resnm);
-    }
-
-    return (0 == rc) ? i-1 : -1;
-}
-
-/* Return the name of the residuetype with the given index, or
- * NULL if not found. */
-const char *
-gmx_residuetype_get_name(gmx_residuetype_t rt, int index)
-{
-    if (index >= 0 && index < rt->n)
-    {
-        return rt->resname[index];
-    }
-    else
-    {
-        return NULL;
-    }
-}
-
-
-
 void analyse(t_atoms *atoms, t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool bVerb)
 {
-    gmx_residuetype_t rt = NULL;
+    gmx_residuetype_t*rt = NULL;
     char             *resnm;
     atom_id          *aid;
     const char    **  restype;
@@ -871,7 +624,7 @@ void analyse(t_atoms *atoms, t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool b
         if (!found)
         {
             srenew(p_typename, ntypes+1);
-            p_typename[ntypes++] = strdup(restype[i]);
+            p_typename[ntypes++] = gmx_strdup(restype[i]);
         }
     }
 
@@ -953,7 +706,7 @@ void analyse(t_atoms *atoms, t_blocka *gb, char ***gn, gmx_bool bASK, gmx_bool b
     {
         srenew(gb->index, gb->nr+2);
         srenew(*gn, gb->nr+1);
-        (*gn)[gb->nr] = strdup("Water_and_ions");
+        (*gn)[gb->nr] = gmx_strdup("Water_and_ions");
         srenew(gb->a, gb->nra+nwater+nion);
         if (nwater > 0)
         {
@@ -999,81 +752,52 @@ t_blocka *init_index(const char *gfile, char ***grpname)
 {
     FILE      *in;
     t_blocka  *b;
-    int        a, maxentries;
-    int        i, j, ng;
+    int        maxentries;
+    int        i, j;
     char       line[STRLEN], *pt, str[STRLEN];
 
     in = gmx_fio_fopen(gfile, "r");
     snew(b, 1);
-    get_a_line(in, line, STRLEN);
-    if (line[0] == '[')
+    b->nr      = 0;
+    b->index   = NULL;
+    b->nra     = 0;
+    b->a       = NULL;
+    *grpname   = NULL;
+    maxentries = 0;
+    while (get_a_line(in, line, STRLEN))
     {
-        /* new format */
-        b->nr      = 0;
-        b->index   = NULL;
-        b->nra     = 0;
-        b->a       = NULL;
-        *grpname   = NULL;
-        maxentries = 0;
-        do
+        if (get_header(line, str))
         {
-            if (get_header(line, str))
+            b->nr++;
+            srenew(b->index, b->nr+1);
+            srenew(*grpname, b->nr);
+            if (b->nr == 1)
             {
-                b->nr++;
-                srenew(b->index, b->nr+1);
-                srenew(*grpname, b->nr);
-                if (b->nr == 1)
-                {
-                    b->index[0] = 0;
-                }
-                b->index[b->nr]     = b->index[b->nr-1];
-                (*grpname)[b->nr-1] = strdup(str);
-            }
-            else
-            {
-                if (b->nr == 0)
-                {
-                    gmx_fatal(FARGS, "The first header of your indexfile is invalid");
-                }
-                pt = line;
-                while (sscanf(pt, "%s", str) == 1)
-                {
-                    i = b->index[b->nr];
-                    if (i >= maxentries)
-                    {
-                        maxentries += 1024;
-                        srenew(b->a, maxentries);
-                    }
-                    b->a[i] = strtol(str, NULL, 10)-1;
-                    b->index[b->nr]++;
-                    (b->nra)++;
-                    pt = strstr(pt, str)+strlen(str);
-                }
+                b->index[0] = 0;
             }
+            b->index[b->nr]     = b->index[b->nr-1];
+            (*grpname)[b->nr-1] = gmx_strdup(str);
         }
-        while (get_a_line(in, line, STRLEN));
-    }
-    else
-    {
-        /* old format */
-        sscanf(line, "%d%d", &b->nr, &b->nra);
-        snew(b->index, b->nr+1);
-        snew(*grpname, b->nr);
-        b->index[0] = 0;
-        snew(b->a, b->nra);
-        for (i = 0; (i < b->nr); i++)
+        else
         {
-            GMX_IGNORE_RETURN_VALUE(fscanf(in, "%s%d", str, &ng));
-            (*grpname)[i] = strdup(str);
-            b->index[i+1] = b->index[i]+ng;
-            if (b->index[i+1] > b->nra)
+            if (b->nr == 0)
             {
-                gmx_fatal(FARGS, "Something wrong in your indexfile at group %s", str);
+                gmx_fatal(FARGS, "The first header of your indexfile is invalid");
             }
-            for (j = 0; (j < ng); j++)
+            pt = line;
+            while (sscanf(pt, "%s", str) == 1)
             {
-                GMX_IGNORE_RETURN_VALUE(fscanf(in, "%d", &a));
-                b->a[b->index[i]+j] = a;
+                i = b->index[b->nr];
+                if (i >= maxentries)
+                {
+                    maxentries += 1024;
+                    srenew(b->a, maxentries);
+                }
+                assert(b->a != NULL); // for clang analyzer
+                b->a[i] = strtol(str, NULL, 10)-1;
+                b->index[b->nr]++;
+                (b->nra)++;
+                pt = strstr(pt, str)+strlen(str);
             }
         }
     }
@@ -1081,6 +805,7 @@ t_blocka *init_index(const char *gfile, char ***grpname)
 
     for (i = 0; (i < b->nr); i++)
     {
+        assert(b->a != NULL); // for clang analyzer
         for (j = b->index[i]; (j < b->index[i+1]); j++)
         {
             if (b->a[j] < 0)
@@ -1243,7 +968,7 @@ static void rd_groups(t_blocka *grps, char **grpname, char *gnames[],
             fprintf(stderr, "There is one group in the index\n");
             gnr1 = 0;
         }
-        gnames[i] = strdup(grpname[gnr1]);
+        gnames[i] = gmx_strdup(grpname[gnr1]);
         isize[i]  = grps->index[gnr1+1]-grps->index[gnr1];
         snew(index[i], isize[i]);
         for (j = 0; (j < isize[i]); j++)