Manually sort some includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
index cd8e2dec6c42308792dc0403df082234ac11f7a7..b47aa18b7c86c5acc632c6561c94a64c1186b4f5 100644 (file)
@@ -1,65 +1,63 @@
-/*  -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
+/*
+ * This file is part of the GROMACS molecular simulation package.
  *
+ * Copyright (c) 2010,2012,2013,2014, 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.
  *
- *                This source code is part of
- *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
- *
- *                        VERSION 3.2.0
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
  * of the License, or (at your option) any later version.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * For more info, check our website at http://www.gromacs.org
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
- * And Hey:
- * GROningen Mixture of Alchemy and Childrens' Stories
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
+
+#include "fflibutil.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+
 #include <fcntl.h>
-#include "sysstuff.h"
-#include "string2.h"
-#include "gromacs/fileio/futil.h"
-#include "network.h"
-#include "gmx_fatal.h"
-#include "smalloc.h"
-#include "statutil.h"
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "config.h"
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
-#ifdef GMX_THREAD_MPI
-#include "thread_mpi.h"
-#endif
-
-#include "fflibutil.h"
+#include "gromacs/legacyheaders/network.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/futil.h"
+#include "gromacs/utility/path.h"
+#include "gromacs/utility/programcontext.h"
+#include "gromacs/utility/smalloc.h"
 
 const char *fflib_forcefield_dir_ext()
 {
@@ -143,109 +141,101 @@ static int low_fflib_search_file_end(const char *ffdir,
                                      char     ***filenames,
                                      char     ***filenames_short)
 {
-    char           *lib, *dir;
-    char           *libpath;
-    gmx_bool        env_is_set;
-    int             len_fe, len_name;
-    char          **fns, **fns_short;
-    char            dir_print[GMX_PATH_MAX];
-    char           *s, fn_dir[GMX_PATH_MAX];
-    gmx_directory_t dirhandle;
-    char            nextname[STRLEN];
-    int             n, n_thisdir, rc;
-
-    len_fe = strlen(file_end);
-
-    env_is_set = FALSE;
-    if (ffdir != NULL)
+    char **fns = NULL, **fns_short = NULL;
+    int    n   = 0;
+    try
     {
-        /* Search in current dir and ffdir */
-        libpath = gmxlibfn(ffdir);
-    }
-    else
-    {
-        /* GMXLIB can be a path now */
-        lib = getenv("GMXLIB");
-        snew(libpath, GMX_PATH_MAX);
-        if (bAddCWD)
-        {
-            sprintf(libpath, "%s%s", ".", PATH_SEPARATOR);
-        }
-        if (lib != NULL)
+        std::vector<std::string> libPaths;
+        bool                     bEnvIsSet = false;
+
+        if (ffdir != NULL)
         {
-            env_is_set = TRUE;
-            strncat(libpath, lib, GMX_PATH_MAX);
+            /* Search ffdir in current dir and library dirs */
+            libPaths.push_back(gmxlibfn(ffdir));
         }
         else
         {
-            get_libdir(libpath + strlen(libpath));
+            /* GMXLIB can be a path now */
+            if (bAddCWD)
+            {
+                libPaths.push_back(".");
+            }
+            const char *lib = getenv("GMXLIB");
+            if (lib != NULL)
+            {
+                bEnvIsSet = true;
+                gmx::Path::splitPathEnvironment(lib, &libPaths);
+            }
+            else
+            {
+                libPaths.push_back(gmx::getProgramContext().defaultLibraryDataPath());
+            }
         }
-    }
-    s         = libpath;
-    n         = 0;
-    fns       = NULL;
-    fns_short = NULL;
-    /* Loop over all the entries in libpath */
-    while ((dir = gmx_strsep(&s, PATH_SEPARATOR)) != NULL)
-    {
-        rc = gmx_directory_open(&dirhandle, dir);
-        if (rc == 0)
-        {
-            strcpy(dir_print, dir);
 
-            n_thisdir = 0;
-            while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
+        const int len_fe = strlen(file_end);
+
+        std::vector<std::string>::const_iterator i;
+        for (i = libPaths.begin(); i != libPaths.end(); ++i)
+        {
+            const char      *dir = i->c_str();
+            gmx_directory_t  dirhandle;
+            const int        rc  = gmx_directory_open(&dirhandle, dir);
+            if (rc == 0)
             {
-                nextname[STRLEN-1] = 0;
-                if (debug)
+                char nextname[STRLEN];
+                int  n_thisdir = 0;
+                while (gmx_directory_nextfile(dirhandle, nextname, STRLEN-1) == 0)
                 {
-                    fprintf(debug, "dir '%s' %d file '%s'\n",
-                            dir, n_thisdir, nextname);
-                }
-                len_name = strlen(nextname);
-                /* What about case sensitivity? */
-                if (len_name >= len_fe &&
-                    strcmp(nextname+len_name-len_fe, file_end) == 0)
-                {
-                    /* We have a match */
-                    srenew(fns, n+1);
-                    sprintf(fn_dir, "%s%c%s",
-                            dir_print, DIR_SEPARATOR, nextname);
+                    nextname[STRLEN-1] = 0;
+                    if (debug)
+                    {
+                        fprintf(debug, "dir '%s' %d file '%s'\n",
+                                dir, n_thisdir, nextname);
+                    }
+                    const int len_name = strlen(nextname);
+                    /* What about case sensitivity? */
+                    if (len_name >= len_fe &&
+                        strcmp(nextname+len_name-len_fe, file_end) == 0)
+                    {
+                        char fn_dir[GMX_PATH_MAX];
+                        /* We have a match */
+                        srenew(fns, n+1);
+                        sprintf(fn_dir, "%s%c%s", dir, DIR_SEPARATOR, nextname);
 
-                    /* Copy the file name, possibly including the path. */
-                    fns[n] = strdup(fn_dir);
+                        /* Copy the file name, possibly including the path. */
+                        fns[n] = gmx_strdup(fn_dir);
 
-                    if (ffdir == NULL)
-                    {
-                        /* We are searching in a path.
-                         * Use the relative path when we use share/top
-                         * from the installation.
-                         * Add the full path when we use the current
-                         * working directory of GMXLIB.
-                         */
-                        srenew(fns_short, n+1);
-                        if (strcmp(dir, ".") == 0 || env_is_set)
+                        if (ffdir == NULL)
                         {
-                            fns_short[n] = strdup(fn_dir);
-                        }
-                        else
-                        {
-                            fns_short[n] = strdup(nextname);
+                            /* We are searching in a path.
+                             * Use the relative path when we use share/top
+                             * from the installation.
+                             * Add the full path when we use the current
+                             * working directory of GMXLIB.
+                             */
+                            srenew(fns_short, n+1);
+                            if (strcmp(dir, ".") == 0 || bEnvIsSet)
+                            {
+                                fns_short[n] = gmx_strdup(fn_dir);
+                            }
+                            else
+                            {
+                                fns_short[n] = gmx_strdup(nextname);
+                            }
                         }
+                        n++;
+                        n_thisdir++;
                     }
-                    n++;
-                    n_thisdir++;
                 }
-            }
-            gmx_directory_close(dirhandle);
+                gmx_directory_close(dirhandle);
 
-            sort_filenames(n_thisdir,
-                           fns+n-n_thisdir,
-                           fns_short == NULL ? NULL : fns_short+n-n_thisdir);
+                sort_filenames(n_thisdir,
+                               fns+n-n_thisdir,
+                               fns_short == NULL ? NULL : fns_short+n-n_thisdir);
+            }
         }
     }
-
-    sfree(libpath);
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
 
     if (n == 0 && bFatalError)
     {
@@ -306,7 +296,7 @@ int fflib_search_file_in_dirend(const char *filename, const char *dirend,
                 {
                     /* We have a match */
                     srenew(dns, n+1);
-                    dns[n] = strdup(f_short[i]);
+                    dns[n] = gmx_strdup(f_short[i]);
                     n++;
                 }
             }
@@ -349,7 +339,7 @@ FILE *fflib_open(const char *file)
 
     file_fullpath = gmxlibfn(file);
     fprintf(stderr, "Opening force field file %s\n", file_fullpath);
-    fp = ffopen(file_fullpath, "r");
+    fp = gmx_ffopen(file_fullpath, "r");
     sfree(file_fullpath);
 
     return fp;