Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / futil.cpp
index 312d6a4a8e03ebc57ba6b5c307f35c846bde66cc..546b875808bc1d69105bab65ac8a1fefb263a057 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/utility/futil.h"
+#include "gmxpre.h"
+
+#include "futil.h"
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #ifdef HAVE_DIRENT_H
 /* POSIX */
@@ -60,6 +60,7 @@
 #ifdef GMX_NATIVE_WINDOWS
 #include <direct.h>
 #include <io.h>
+#include <windows.h>
 #endif
 
 /* Windows file stuff, only necessary for visual studio */
@@ -87,15 +88,15 @@ typedef struct t_pstack {
 } t_pstack;
 
 static t_pstack    *pstack      = NULL;
-static gmx_bool     bUnbuffered = FALSE;
+static bool         bUnbuffered = false;
 
 /* this linked list is an intrinsically globally shared object, so we have
    to protect it with mutexes */
 static tMPI_Thread_mutex_t pstack_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
 
-void no_buffers(void)
+void gmx_disable_file_buffering(void)
 {
-    bUnbuffered = TRUE;
+    bUnbuffered = true;
 }
 
 void push_ps(FILE *fp)
@@ -234,7 +235,11 @@ gmx_off_t gmx_ftell(FILE *stream)
     return ftello(stream);
 #else
 #ifdef HAVE__FSEEKI64
+#ifndef __MINGW32__
     return _ftelli64(stream);
+#else
+    return ftello64(stream);
+#endif
 #else
     return ftell(stream);
 #endif
@@ -434,7 +439,7 @@ FILE *gmx_ffopen(const char *file, const char *mode)
         /* Check whether we should be using buffering (default) or not
          * (for debugging)
          */
-        if (bUnbuffered || ((bufsize = getenv("LOG_BUFS")) != NULL))
+        if (bUnbuffered || ((bufsize = getenv("GMX_LOG_BUFFER")) != NULL))
         {
             /* Check whether to use completely unbuffered */
             if (bUnbuffered)
@@ -487,12 +492,12 @@ FILE *gmx_ffopen(const char *file, const char *mode)
 /* Our own implementation of dirent-like functionality to scan directories. */
 struct gmx_directory
 {
-#ifdef HAVE_DIRENT_H
-    DIR  *               dirent_handle;
-#elif (defined GMX_NATIVE_WINDOWS)
+#if defined(GMX_NATIVE_WINDOWS)
     intptr_t             windows_handle;
     struct _finddata_t   finddata;
     int                  first;
+#elif defined(HAVE_DIRENT_H)
+    DIR  *               dirent_handle;
 #else
     int                  dummy;
 #endif
@@ -509,19 +514,7 @@ gmx_directory_open(gmx_directory_t *p_gmxdir, const char *dirname)
 
     *p_gmxdir = gmxdir;
 
-#ifdef HAVE_DIRENT_H
-    if ( (gmxdir->dirent_handle = opendir(dirname)) != NULL)
-    {
-        rc = 0;
-    }
-    else
-    {
-        sfree(gmxdir);
-        *p_gmxdir = NULL;
-        rc        = EINVAL;
-    }
-#elif (defined GMX_NATIVE_WINDOWS)
-
+#if defined(GMX_NATIVE_WINDOWS)
     if (dirname != NULL && strlen(dirname) > 0)
     {
         char *     tmpname;
@@ -564,6 +557,17 @@ gmx_directory_open(gmx_directory_t *p_gmxdir, const char *dirname)
     {
         rc = EINVAL;
     }
+#elif defined(HAVE_DIRENT_H)
+    if ( (gmxdir->dirent_handle = opendir(dirname)) != NULL)
+    {
+        rc = 0;
+    }
+    else
+    {
+        sfree(gmxdir);
+        *p_gmxdir = NULL;
+        rc        = EINVAL;
+    }
 #else
     gmx_fatal(FARGS,
               "Source compiled without POSIX dirent or windows support - cannot scan directories.\n"
@@ -581,8 +585,41 @@ gmx_directory_nextfile(gmx_directory_t gmxdir, char *name, int maxlength_name)
 {
     int                     rc;
 
-#ifdef HAVE_DIRENT_H
+#if defined(GMX_NATIVE_WINDOWS)
+    if (gmxdir != NULL)
+    {
+        if (gmxdir->windows_handle <= 0)
+        {
 
+            name[0] = '\0';
+            rc      = ENOENT;
+        }
+        else if (gmxdir->first == 1)
+        {
+            strncpy(name, gmxdir->finddata.name, maxlength_name);
+            rc            = 0;
+            gmxdir->first = 0;
+        }
+        else
+        {
+            if (_findnext(gmxdir->windows_handle, &gmxdir->finddata) == 0)
+            {
+                strncpy(name, gmxdir->finddata.name, maxlength_name);
+                rc      = 0;
+            }
+            else
+            {
+                name[0] = '\0';
+                rc      = ENOENT;
+            }
+        }
+    }
+    else
+    {
+        name[0] = '\0';
+        rc      = EINVAL;
+    }
+#elif defined(HAVE_DIRENT_H)
     struct dirent *         direntp_large;
     struct dirent *         p;
 
@@ -613,38 +650,6 @@ gmx_directory_nextfile(gmx_directory_t gmxdir, char *name, int maxlength_name)
         name[0] = '\0';
         rc      = EINVAL;
     }
-
-#elif (defined GMX_NATIVE_WINDOWS)
-
-    if (gmxdir != NULL)
-    {
-        if (gmxdir->windows_handle <= 0)
-        {
-
-            name[0] = '\0';
-            rc      = ENOENT;
-        }
-        else if (gmxdir->first == 1)
-        {
-            strncpy(name, gmxdir->finddata.name, maxlength_name);
-            rc            = 0;
-            gmxdir->first = 0;
-        }
-        else
-        {
-            if (_findnext(gmxdir->windows_handle, &gmxdir->finddata) == 0)
-            {
-                strncpy(name, gmxdir->finddata.name, maxlength_name);
-                rc      = 0;
-            }
-            else
-            {
-                name[0] = '\0';
-                rc      = ENOENT;
-            }
-        }
-    }
-
 #else
     gmx_fatal(FARGS,
               "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
@@ -658,10 +663,10 @@ int
 gmx_directory_close(gmx_directory_t gmxdir)
 {
     int                     rc;
-#ifdef HAVE_DIRENT_H
-    rc = (gmxdir != NULL) ? closedir(gmxdir->dirent_handle) : EINVAL;
-#elif (defined GMX_NATIVE_WINDOWS)
+#if defined(GMX_NATIVE_WINDOWS)
     rc = (gmxdir != NULL) ? _findclose(gmxdir->windows_handle) : EINVAL;
+#elif defined(HAVE_DIRENT_H)
+    rc = (gmxdir != NULL) ? closedir(gmxdir->dirent_handle) : EINVAL;
 #else
     gmx_fatal(FARGS,
               "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
@@ -906,13 +911,15 @@ int gmx_fsync(FILE *fp)
     rc = fah_fsync(fp);
 #else /* GMX_FAHCORE */
     {
-        int fn = -1;
+        int fn;
 
         /* get the file number */
 #if defined(HAVE_FILENO)
         fn = fileno(fp);
 #elif defined(HAVE__FILENO)
         fn = _fileno(fp);
+#else
+        fn = -1;
 #endif
 
         /* do the actual fsync */