Move fileio sources to C++
[alexxy/gromacs.git] / src / gromacs / fileio / xvgr.cpp
index fc65cf8d02b6dda047a9c1944512f82718df7fcb..1b5a47909aad4411d3e9d90d0a1b19bcb2fc6b80 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 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.
 
 #include "xvgr.h"
 
-#include "config.h"
-
-#include <ctype.h>
-#include <string.h>
-#include <time.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
+#include <cctype>
+#include <cstring>
 
 #include "gromacs/fileio/gmxfio.h"
 #include "gromacs/legacyheaders/copyrite.h"
@@ -57,6 +50,7 @@
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
 #include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
 
 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
 {
@@ -172,23 +166,23 @@ static char *xvgrstr(const char *gmx, const output_env_t oenv,
                         break;
                 }
                 g++;
-                b = strlen(buf);
+                b = std::strlen(buf);
             }
             else
             {
                 /* Check for special symbol */
                 i = 0;
                 while (sym[i] != NULL &&
-                       gmx_strncasecmp(sym[i], gmx+g, strlen(sym[i])) != 0)
+                       gmx_strncasecmp(sym[i], gmx+g, std::strlen(sym[i])) != 0)
                 {
                     i++;
                 }
                 if (sym[i] != NULL)
                 {
                     c = symc[i];
-                    if (isupper(gmx[g]))
+                    if (std::isupper(gmx[g]))
                     {
-                        c = toupper(c);
+                        c = std::toupper(c);
                     }
                     switch (xvgf)
                     {
@@ -199,17 +193,17 @@ static char *xvgrstr(const char *gmx, const output_env_t oenv,
                             sprintf(buf+b, "%s%c%s", "\\8", c, "\\4");
                             break;
                         default:
-                            strncat(buf+b, gmx+g, strlen(sym[i]));
-                            b += strlen(sym[i]);
-                            if (gmx[g+strlen(sym[i])] != ' ')
+                            std::strncat(buf+b, gmx+g, std::strlen(sym[i]));
+                            b += std::strlen(sym[i]);
+                            if (gmx[g+std::strlen(sym[i])] != ' ')
                             {
                                 buf[b++] = ' ';
                             }
                             buf[b] = '\0';
                             break;
                     }
-                    g += strlen(sym[i]);
-                    b  = strlen(buf);
+                    g += std::strlen(sym[i]);
+                    b  = std::strlen(buf);
                 }
                 else
                 {
@@ -238,12 +232,10 @@ void xvgr_header(FILE *fp, const char *title, const char *xaxis,
                  const output_env_t oenv)
 {
     char   pukestr[100], buf[STRLEN];
-    time_t t;
 
     if (output_env_get_print_xvgr_codes(oenv))
     {
-        time(&t);
-        gmx_ctime_r(&t, buf, STRLEN);
+        gmx_format_current_time(buf, STRLEN);
         fprintf(fp, "# This file was created %s", buf);
         try
         {
@@ -475,7 +467,7 @@ static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
         curp         += len_remaining-1; /* overwrite the nul char in next iteration */
         len_remaining = 1;
     }
-    while ((strchr(*ptr, '\n') == NULL) && (!feof(fp)));
+    while ((std::strchr(*ptr, '\n') == NULL) && (!feof(fp)));
 
     if (*len + STRLEN >= maxlen)
     {
@@ -489,7 +481,7 @@ static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
     }
     {
         /* now remove newline */
-        int slen = strlen(*ptr);
+        int slen = std::strlen(*ptr);
         if ((*ptr)[slen-1] == '\n')
         {
             (*ptr)[slen-1] = '\0';
@@ -501,24 +493,25 @@ static char *fgets3(FILE *fp, char **ptr, int *len, int maxlen)
 
 static int wordcount(char *ptr)
 {
-    int i, n, is[2];
+    int i, n = 0, is[2];
     int cur = 0;
 #define prev (1-cur)
 
-    if (strlen(ptr) == 0)
-    {
-        return 0;
-    }
-    /* fprintf(stderr,"ptr='%s'\n",ptr); */
-    n = 1;
-    for (i = 0; (ptr[i] != '\0'); i++)
+    if (NULL != ptr)
     {
-        is[cur] = isspace(ptr[i]);
-        if ((i > 0)  && (is[cur] && !is[prev]))
+        for (i = 0; (ptr[i] != '\0'); i++)
         {
-            n++;
+            is[cur] = std::isspace(ptr[i]);
+            if ((0 == i) && !is[cur])
+            {
+                n++;
+            }
+            else if ((i > 0)  && (!is[cur] && is[prev]))
+            {
+                n++;
+            }
+            cur = prev;
         }
-        cur = prev;
     }
     return n;
 }
@@ -528,11 +521,11 @@ static char *read_xvgr_string(const char *line)
     const char *ptr0, *ptr1;
     char       *str;
 
-    ptr0 = strchr(line, '"');
+    ptr0 = std::strchr(line, '"');
     if (ptr0 != NULL)
     {
         ptr0++;
-        ptr1 = strchr(ptr0, '"');
+        ptr1 = std::strchr(ptr0, '"');
         if (ptr1 != NULL)
         {
             str            = gmx_strdup(ptr0);
@@ -591,7 +584,7 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
                 ptr++;
                 trim(ptr);
                 set = -1;
-                if (strncmp(ptr, "subtitle", 8) == 0)
+                if (std::strncmp(ptr, "subtitle", 8) == 0)
                 {
                     ptr += 8;
                     if (subtitle != NULL)
@@ -599,7 +592,7 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
                         *subtitle = read_xvgr_string(ptr);
                     }
                 }
-                else if (strncmp(ptr, "legend string", 13) == 0)
+                else if (std::strncmp(ptr, "legend string", 13) == 0)
                 {
                     ptr += 13;
                     sscanf(ptr, "%d%n", &set, &nchar);
@@ -611,7 +604,7 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
                     sscanf(ptr, "%d%n", &set, &nchar);
                     ptr += nchar;
                     trim(ptr);
-                    if (strncmp(ptr, "legend", 6) == 0)
+                    if (std::strncmp(ptr, "legend", 6) == 0)
                     {
                         ptr += 6;
                     }
@@ -661,8 +654,8 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
             /* fprintf(stderr,"ptr='%s'\n",ptr);*/
             for (k = 0; (k < nny); k++)
             {
-                strcpy(fmt, base);
-                strcat(fmt, "%lf");
+                std::strcpy(fmt, base);
+                std::strcat(fmt, "%lf");
                 rval = sscanf(ptr, fmt, &lf);
                 /* fprintf(stderr,"rval = %d\n",rval);*/
                 if ((rval == EOF) || (rval == 0))
@@ -672,7 +665,7 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
                 yy[k][nx] = lf;
                 srenew(fmt, 3*(nny+1)+1);
                 srenew(base, 3*nny+1);
-                strcat(base, "%*s");
+                std::strcat(base, "%*s");
             }
             if (k != nny)
             {
@@ -690,6 +683,8 @@ int read_xvg_legend(const char *fn, double ***y, int *ny,
 
     *y = yy;
     sfree(tmpbuf);
+    sfree(base);
+    sfree(fmt);
 
     if (legend_nalloc > 0)
     {