From e12d3810d648b2ab9eaac55637e2fde331a1e8b0 Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Wed, 2 Dec 2015 06:35:40 +0200 Subject: [PATCH] Move strdb.* to utility/ These are more similar to the low-level I/O routines already present in utility (most uses would be easy to replace with TextReader alone, after some minor extensions), rather than to the other fileio/ stuff. This, in part, helps breaking the fileio <-> topology cyclic dependency. Remove unused or duplicate functions or make them private to the source file. Convert existing comments to rudimentary Doxygen to avoid warnings. Change-Id: I3f5262112fabe2bffa16c14c20d987e2e63dcb4f --- src/contrib/ehdata.c | 2 +- src/contrib/hrefify.c | 2 +- src/contrib/mkice.c | 2 +- src/gromacs/gmxana/gmx_do_dssp.cpp | 4 +- src/gromacs/gmxana/gmx_editconf.cpp | 2 +- src/gromacs/gmxana/gmx_enemat.cpp | 2 +- src/gromacs/gmxana/gmx_rmsdist.cpp | 2 +- src/gromacs/gmxana/gmx_wheel.cpp | 4 +- src/gromacs/gmxana/nsfactor.cpp | 2 +- src/gromacs/gmxana/sfactor.cpp | 2 +- src/gromacs/gmxpreprocess/pdb2gmx.cpp | 2 +- src/gromacs/gmxpreprocess/pdb2top.cpp | 2 +- src/gromacs/gmxpreprocess/resall.cpp | 2 +- src/gromacs/gmxpreprocess/specbond.cpp | 2 +- src/gromacs/gmxpreprocess/ter_db.cpp | 2 +- src/gromacs/gmxpreprocess/xlate.cpp | 2 +- src/gromacs/topology/atomprop.cpp | 2 +- src/gromacs/topology/index.cpp | 2 +- src/gromacs/topology/residuetypes.cpp | 4 +- src/gromacs/{fileio => utility}/strdb.cpp | 69 +++---------------------------- src/gromacs/{fileio => utility}/strdb.h | 69 ++++++++++++++----------------- 21 files changed, 57 insertions(+), 125 deletions(-) rename src/gromacs/{fileio => utility}/strdb.cpp (80%) rename src/gromacs/{fileio => utility}/strdb.h (62%) diff --git a/src/contrib/ehdata.c b/src/contrib/ehdata.c index 4bc06da790..824aafa4b9 100644 --- a/src/contrib/ehdata.c +++ b/src/contrib/ehdata.c @@ -43,7 +43,7 @@ #include "macros.h" #include "gromacs/utility/fatalerror.h" #include "random.h" -#include "gromacs/fileio/strdb.h" +#include "gromacs/utility/strdb.h" #include "gromacs/utility/futil.h" #include "ehdata.h" diff --git a/src/contrib/hrefify.c b/src/contrib/hrefify.c index 6fc113b603..a21f14abae 100644 --- a/src/contrib/hrefify.c +++ b/src/contrib/hrefify.c @@ -48,7 +48,7 @@ #include "gromacs/commandline/pargs.h" #include "copyrite.h" #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" +#include "gromacs/utility/strdb.h" gmx_bool isword(char c) { diff --git a/src/contrib/mkice.c b/src/contrib/mkice.c index 389a20386b..983900324f 100644 --- a/src/contrib/mkice.c +++ b/src/contrib/mkice.c @@ -52,7 +52,7 @@ #include "txtdump.h" #include "gromacs/fileio/trrio.h" #include "gromacs/topology/symtab.h" -#include "gromacs/fileio/strdb.h" +#include "gromacs/utility/strdb.h" #include "gromacs/fileio/confio.h" #define TET 109.47 diff --git a/src/gromacs/gmxana/gmx_do_dssp.cpp b/src/gromacs/gmxana/gmx_do_dssp.cpp index 0e8aa59260..f41182e48a 100644 --- a/src/gromacs/gmxana/gmx_do_dssp.cpp +++ b/src/gromacs/gmxana/gmx_do_dssp.cpp @@ -46,7 +46,6 @@ #include "gromacs/fileio/confio.h" #include "gromacs/fileio/matio.h" #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/trxio.h" #include "gromacs/fileio/xvgr.h" #include "gromacs/gmxana/gmx_ana.h" @@ -60,6 +59,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" static int strip_dssp(char *dsspfile, int nres, gmx_bool bPhobres[], real t, @@ -198,7 +198,7 @@ static gmx_bool *bPhobics(t_atoms *atoms) gmx_bool *bb; - nb = get_strings("phbres.dat", &cb); + nb = get_lines("phbres.dat", &cb); snew(bb, atoms->nres); for (i = 0; (i < atoms->nres); i++) diff --git a/src/gromacs/gmxana/gmx_editconf.cpp b/src/gromacs/gmxana/gmx_editconf.cpp index 50615f8f21..0003636d71 100644 --- a/src/gromacs/gmxana/gmx_editconf.cpp +++ b/src/gromacs/gmxana/gmx_editconf.cpp @@ -45,7 +45,6 @@ #include "gromacs/commandline/viewit.h" #include "gromacs/fileio/confio.h" #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/tpxio.h" #include "gromacs/fileio/trxio.h" #include "gromacs/fileio/txtdump.h" @@ -65,6 +64,7 @@ #include "gromacs/utility/futil.h" #include "gromacs/utility/gmxassert.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" real calc_mass(t_atoms *atoms, gmx_bool bGetMass, gmx_atomprop_t aps) diff --git a/src/gromacs/gmxana/gmx_enemat.cpp b/src/gromacs/gmxana/gmx_enemat.cpp index 81f889a4ca..02687b6ddc 100644 --- a/src/gromacs/gmxana/gmx_enemat.cpp +++ b/src/gromacs/gmxana/gmx_enemat.cpp @@ -42,7 +42,6 @@ #include "gromacs/commandline/pargs.h" #include "gromacs/fileio/enxio.h" #include "gromacs/fileio/matio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/trxio.h" #include "gromacs/fileio/xvgr.h" #include "gromacs/gmxana/gmx_ana.h" @@ -56,6 +55,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" static int search_str2(int nstr, char **str, char *key) diff --git a/src/gromacs/gmxana/gmx_rmsdist.cpp b/src/gromacs/gmxana/gmx_rmsdist.cpp index e74c0a766b..a94aed8e6f 100644 --- a/src/gromacs/gmxana/gmx_rmsdist.cpp +++ b/src/gromacs/gmxana/gmx_rmsdist.cpp @@ -45,7 +45,6 @@ #include "gromacs/commandline/viewit.h" #include "gromacs/fileio/confio.h" #include "gromacs/fileio/matio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/trxio.h" #include "gromacs/fileio/xvgr.h" #include "gromacs/gmxana/gmx_ana.h" @@ -59,6 +58,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" static void calc_dist(int nind, int index[], rvec x[], int ePBC, matrix box, diff --git a/src/gromacs/gmxana/gmx_wheel.cpp b/src/gromacs/gmxana/gmx_wheel.cpp index c19228b3d1..90d497be97 100644 --- a/src/gromacs/gmxana/gmx_wheel.cpp +++ b/src/gromacs/gmxana/gmx_wheel.cpp @@ -44,7 +44,6 @@ #include #include "gromacs/commandline/pargs.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/writeps.h" #include "gromacs/gmxana/gmx_ana.h" #include "gromacs/gmxana/gstat.h" @@ -54,6 +53,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" static gmx_bool *bPhobics(int nres, char *resnm[]) { @@ -61,7 +61,7 @@ static gmx_bool *bPhobics(int nres, char *resnm[]) char **cb; gmx_bool *bb; - nb = get_strings("phbres.dat", &cb); + nb = get_lines("phbres.dat", &cb); snew(bb, nres); for (i = 0; (i < nres); i++) diff --git a/src/gromacs/gmxana/nsfactor.cpp b/src/gromacs/gmxana/nsfactor.cpp index 5ece0fe229..d1fbcd197b 100644 --- a/src/gromacs/gmxana/nsfactor.cpp +++ b/src/gromacs/gmxana/nsfactor.cpp @@ -41,7 +41,6 @@ #include #include -#include "gromacs/fileio/strdb.h" #include "gromacs/math/vec.h" #include "gromacs/random/random.h" #include "gromacs/topology/topology.h" @@ -51,6 +50,7 @@ #include "gromacs/utility/futil.h" #include "gromacs/utility/gmxomp.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" void check_binwidth(real binwidth) { diff --git a/src/gromacs/gmxana/sfactor.cpp b/src/gromacs/gmxana/sfactor.cpp index a0856f1c7a..f22174e639 100644 --- a/src/gromacs/gmxana/sfactor.cpp +++ b/src/gromacs/gmxana/sfactor.cpp @@ -44,7 +44,6 @@ #include #include "gromacs/fileio/confio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/trx.h" #include "gromacs/fileio/trxio.h" #include "gromacs/fileio/xvgr.h" @@ -58,6 +57,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" typedef struct gmx_structurefactors { diff --git a/src/gromacs/gmxpreprocess/pdb2gmx.cpp b/src/gromacs/gmxpreprocess/pdb2gmx.cpp index f509198db5..95430eefba 100644 --- a/src/gromacs/gmxpreprocess/pdb2gmx.cpp +++ b/src/gromacs/gmxpreprocess/pdb2gmx.cpp @@ -47,7 +47,6 @@ #include "gromacs/fileio/confio.h" #include "gromacs/fileio/gmxfio.h" #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxlib/conformation-utilities.h" #include "gromacs/gmxlib/readinp.h" #include "gromacs/gmxpreprocess/fflibutil.h" @@ -74,6 +73,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/gmxassert.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" #define RTP_MAXCHAR 5 typedef struct { diff --git a/src/gromacs/gmxpreprocess/pdb2top.cpp b/src/gromacs/gmxpreprocess/pdb2top.cpp index 68d64ca907..0007385884 100644 --- a/src/gromacs/gmxpreprocess/pdb2top.cpp +++ b/src/gromacs/gmxpreprocess/pdb2top.cpp @@ -50,7 +50,6 @@ #include "gromacs/fileio/copyrite.h" #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxpreprocess/add_par.h" #include "gromacs/gmxpreprocess/fflibutil.h" #include "gromacs/gmxpreprocess/gen_ad.h" @@ -74,6 +73,7 @@ #include "gromacs/utility/path.h" #include "gromacs/utility/programcontext.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" #include "gromacs/utility/stringutil.h" /* this must correspond to enum in pdb2top.h */ diff --git a/src/gromacs/gmxpreprocess/resall.cpp b/src/gromacs/gmxpreprocess/resall.cpp index b769138ed3..0755f732cb 100644 --- a/src/gromacs/gmxpreprocess/resall.cpp +++ b/src/gromacs/gmxpreprocess/resall.cpp @@ -44,7 +44,6 @@ #include -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxpreprocess/fflibutil.h" #include "gromacs/gmxpreprocess/notset.h" #include "gromacs/gmxpreprocess/pgutil.h" @@ -53,6 +52,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" gpp_atomtype_t read_atype(const char *ffdir, t_symtab *tab) { diff --git a/src/gromacs/gmxpreprocess/specbond.cpp b/src/gromacs/gmxpreprocess/specbond.cpp index 60ff21303a..265e7d4f93 100644 --- a/src/gromacs/gmxpreprocess/specbond.cpp +++ b/src/gromacs/gmxpreprocess/specbond.cpp @@ -46,12 +46,12 @@ #include #include "gromacs/fileio/pdbio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxpreprocess/pdb2top.h" #include "gromacs/math/vec.h" #include "gromacs/utility/cstringutil.h" #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" gmx_bool yesno(void) { diff --git a/src/gromacs/gmxpreprocess/ter_db.cpp b/src/gromacs/gmxpreprocess/ter_db.cpp index 0ea8d52b17..0864324551 100644 --- a/src/gromacs/gmxpreprocess/ter_db.cpp +++ b/src/gromacs/gmxpreprocess/ter_db.cpp @@ -42,7 +42,6 @@ #include #include "gromacs/fileio/gmxfio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxpreprocess/fflibutil.h" #include "gromacs/gmxpreprocess/h_db.h" #include "gromacs/gmxpreprocess/notset.h" @@ -52,6 +51,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" /* use bonded types definitions in hackblock.h */ #define ekwRepl ebtsNR+1 diff --git a/src/gromacs/gmxpreprocess/xlate.cpp b/src/gromacs/gmxpreprocess/xlate.cpp index 516bc446af..c89244bff6 100644 --- a/src/gromacs/gmxpreprocess/xlate.cpp +++ b/src/gromacs/gmxpreprocess/xlate.cpp @@ -41,7 +41,6 @@ #include #include -#include "gromacs/fileio/strdb.h" #include "gromacs/gmxpreprocess/fflibutil.h" #include "gromacs/gmxpreprocess/hackblock.h" #include "gromacs/topology/residuetypes.h" @@ -50,6 +49,7 @@ #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" typedef struct { char *filebase; diff --git a/src/gromacs/topology/atomprop.cpp b/src/gromacs/topology/atomprop.cpp index be7c5f60fa..6904b241ec 100644 --- a/src/gromacs/topology/atomprop.cpp +++ b/src/gromacs/topology/atomprop.cpp @@ -45,7 +45,6 @@ #include #include "gromacs/fileio/copyrite.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/math/utilities.h" #include "gromacs/topology/residuetypes.h" #include "gromacs/utility/cstringutil.h" @@ -53,6 +52,7 @@ #include "gromacs/utility/futil.h" #include "gromacs/utility/programcontext.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" typedef struct { gmx_bool bSet; diff --git a/src/gromacs/topology/index.cpp b/src/gromacs/topology/index.cpp index 1c407dc885..6ddd8d8b27 100644 --- a/src/gromacs/topology/index.cpp +++ b/src/gromacs/topology/index.cpp @@ -46,7 +46,6 @@ #include #include "gromacs/fileio/gmxfio.h" -#include "gromacs/fileio/strdb.h" #include "gromacs/fileio/txtdump.h" #include "gromacs/topology/atoms.h" #include "gromacs/topology/block.h" @@ -56,6 +55,7 @@ #include "gromacs/utility/cstringutil.h" #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" static gmx_bool gmx_ask_yesno(gmx_bool bASK) { diff --git a/src/gromacs/topology/residuetypes.cpp b/src/gromacs/topology/residuetypes.cpp index 6745c96d37..4c845cc91c 100644 --- a/src/gromacs/topology/residuetypes.cpp +++ b/src/gromacs/topology/residuetypes.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2010,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2010,2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -39,11 +39,11 @@ #include #include -#include "gromacs/fileio/strdb.h" #include "gromacs/utility/cstringutil.h" #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/smalloc.h" +#include "gromacs/utility/strdb.h" const char gmx_residuetype_undefined[] = "Other"; diff --git a/src/gromacs/fileio/strdb.cpp b/src/gromacs/utility/strdb.cpp similarity index 80% rename from src/gromacs/fileio/strdb.cpp rename to src/gromacs/utility/strdb.cpp index 937256031b..3b452fdd8f 100644 --- a/src/gromacs/fileio/strdb.cpp +++ b/src/gromacs/utility/strdb.cpp @@ -117,40 +117,6 @@ gmx_bool get_header(char line[], char *header) return TRUE; } -int get_strings(const char *db, char ***strings) -{ - FILE *in; - char **ptr; - char buf[STRLEN]; - int i, nstr; - - in = libopen(db); - - if (fscanf(in, "%d", &nstr) != 1) - { - gmx_warning("File %s is empty", db); - gmx_ffclose(in); - return 0; - } - snew(ptr, nstr); - for (i = 0; (i < nstr); i++) - { - if (NULL == fgets2(buf, STRLEN, in)) - { - gmx_fatal(FARGS, "Cannot read string from buffer"); - } -#ifdef DEBUG - fprintf(stderr, "Have read: %s\n", buf); -#endif - ptr[i] = gmx_strdup(buf); - } - gmx_ffclose(in); - - *strings = ptr; - - return nstr; -} - int search_str(int nstr, char **str, char *key) { int i; @@ -167,7 +133,7 @@ int search_str(int nstr, char **str, char *key) return -1; } -int fget_lines(FILE *in, char ***strings) +static int fget_lines(FILE *in, char ***strings) { char **ptr; char buf[STRLEN]; @@ -185,7 +151,10 @@ int fget_lines(FILE *in, char ***strings) snew(ptr, nstr); for (i = 0; (i < nstr); i++) { - fgets2(buf, STRLEN, in); + if (fgets2(buf, STRLEN, in) == nullptr) + { + gmx_fatal(FARGS, "Cannot read string from buffer"); + } ptr[i] = gmx_strdup(buf); } @@ -205,31 +174,3 @@ int get_lines(const char *db, char ***strings) return nstr; } - -int get_file(const char *db, char ***strings) -{ - FILE *in; - char **ptr = NULL; - char buf[STRLEN]; - int i, nstr, maxi; - - in = libopen(db); - - i = maxi = 0; - while (fgets2(buf, STRLEN-1, in)) - { - if (i >= maxi) - { - maxi += 50; - srenew(ptr, maxi); - } - ptr[i] = gmx_strdup(buf); - i++; - } - nstr = i; - gmx_ffclose(in); - srenew(ptr, nstr); - *strings = ptr; - - return nstr; -} diff --git a/src/gromacs/fileio/strdb.h b/src/gromacs/utility/strdb.h similarity index 62% rename from src/gromacs/fileio/strdb.h rename to src/gromacs/utility/strdb.h index dc35b915ad..c824466b0f 100644 --- a/src/gromacs/fileio/strdb.h +++ b/src/gromacs/utility/strdb.h @@ -3,7 +3,7 @@ * * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. - * Copyright (c) 2010,2014, by the GROMACS development team, led by + * Copyright (c) 2010,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -34,57 +34,48 @@ * To help us fund GROMACS development, we humbly ask that you cite * the research papers on the package. Check out http://www.gromacs.org. */ -#ifndef GMX_FILEIO_STRDB_H -#define GMX_FILEIO_STRDB_H +/*! \libinternal \file + * \brief + * Declares C functions for reading files with a list of strings. + * + * \inlibraryapi + * \ingroup module_utility + */ +#ifndef GMX_UTILITY_STRDB_H +#define GMX_UTILITY_STRDB_H -#include +#include #include "gromacs/utility/basedefinitions.h" -#ifdef __cplusplus -extern "C" { -#endif - -gmx_bool get_a_line(FILE *fp, char line[], int n); -/* Read a line of at most n characters form *fp to line. +/*! \brief + * Reads a line of at most n characters from *fp to line. + * * Comment ';...' and leading spaces are removed, empty lines are skipped. * Return FALSE when eof. */ +gmx_bool get_a_line(FILE *fp, char line[], int n); -gmx_bool get_header(char line[], char header[]); -/* Read a header between '[' and ']' from line to header. - * Returns FALSE no header is found. +/*! \brief + * Read a header between '[' and ']' from line to header. + * + * Returns FALSE if no header is found. */ +gmx_bool get_header(char line[], char header[]); -int fget_lines(FILE *in, char ***strings); -/* Read an array of lines from file in. strings should be - * the address of an array of strings (to be malloced by this routine) - * return the number of strings. +/*! \brief + * Opens file db, or if non-existant file $GMXLIB/db and read strings. + * + * First line in the file needs to specify the number of strings following. + * Returns the number of strings. */ int get_lines(const char *db, char ***strings); -/* Open file db, or if non-existant file $GMXLIB/db and read strings - * return the number of strings. - */ - -int search_str(int nstr, char **str, char *key); -/* Search an array of strings for key, return the index if found - * -1 if not found. - */ -int get_strings(const char *db, char ***strings); -/* Read an array of strings from file db or $GMXLIB/db. strings should be - * the address of an array of strings (to be malloced by this routine) - * return the number of strings. - */ -int get_file(const char *db, char ***strings); -/* Read an array of strings from file db or $GMXLIB/db. strings should be - * the address of an array of strings (to be malloced by this routine) - * Does not need number of lines as first line in the file. - * return the number of strings. +/*! \brief + * Searches an array of strings for key, return the index if found. + * + * Returns -1 if not found. */ - -#ifdef __cplusplus -} -#endif +int search_str(int nstr, char **str, char *key); #endif -- 2.11.4.GIT