Merge branch release-2018
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_trjconv.cpp
index 3bd115e7fd982e8d852f30facc811213a2ec8c2a..583549c9ab60de22decc93c129b8ccde3e7ca870 100644 (file)
@@ -44,6 +44,7 @@
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/commandline/viewit.h"
+#include "gromacs/compat/make_unique.h"
 #include "gromacs/fileio/confio.h"
 #include "gromacs/fileio/g96io.h"
 #include "gromacs/fileio/gmxfio.h"
@@ -569,20 +570,21 @@ static void do_trunc(const char *fn, real t0)
  * molecule information will generally be present if the input TNG
  * file was written by a GROMACS tool, this seems like reasonable
  * behaviour. */
-static gmx_mtop_t *read_mtop_for_tng(const char *tps_file,
-                                     const char *input_file,
-                                     const char *output_file)
+static std::unique_ptr<gmx_mtop_t>
+read_mtop_for_tng(const char *tps_file,
+                  const char *input_file,
+                  const char *output_file)
 {
-    gmx_mtop_t *mtop = nullptr;
+    std::unique_ptr<gmx_mtop_t> mtop;
 
     if (fn2bTPX(tps_file) &&
         efTNG != fn2ftp(input_file) &&
         efTNG == fn2ftp(output_file))
     {
         int temp_natoms = -1;
-        snew(mtop, 1);
+        mtop = gmx::compat::make_unique<gmx_mtop_t>();
         read_tpx(tps_file, nullptr, nullptr, &temp_natoms,
-                 nullptr, nullptr, mtop);
+                 nullptr, nullptr, mtop.get());
     }
 
     return mtop;
@@ -871,7 +873,6 @@ int gmx_trjconv(int argc, char *argv[])
     int               m, i, d, frame, outframe, natoms, nout, ncent, newstep = 0, model_nr;
 #define SKIP 10
     t_topology        top;
-    gmx_mtop_t       *mtop  = nullptr;
     gmx_conect        gc    = nullptr;
     int               ePBC  = -1;
     t_atoms          *atoms = nullptr, useatoms;
@@ -1079,9 +1080,10 @@ int gmx_trjconv(int argc, char *argv[])
         /* skipping */
         if (skip_nr <= 0)
         {
+            gmx_fatal(FARGS, "Argument for -skip (%d) needs to be greater or equal to 1.", skip_nr);
         }
 
-        mtop = read_mtop_for_tng(top_file, in_file, out_file);
+        std::unique_ptr<gmx_mtop_t> mtop = read_mtop_for_tng(top_file, in_file, out_file);
 
         /* Determine whether to read a topology */
         bTPS = (ftp2bSet(efTPS, NFILE, fnm) ||
@@ -1360,15 +1362,14 @@ int gmx_trjconv(int argc, char *argv[])
             switch (ftp)
             {
                 case efTNG:
-                    trjtools_gmx_prepare_tng_writing(out_file,
-                                                     filemode[0],
-                                                     trxin,
-                                                     &trxout,
-                                                     nullptr,
-                                                     nout,
-                                                     mtop,
-                                                     index,
-                                                     grpnm);
+                    trxout = trjtools_gmx_prepare_tng_writing(out_file,
+                                                              filemode[0],
+                                                              trxin,
+                                                              nullptr,
+                                                              nout,
+                                                              mtop.get(),
+                                                              index,
+                                                              grpnm);
                     break;
                 case efXTC:
                 case efTRR:
@@ -1977,8 +1978,10 @@ int gmx_trjconv(int argc, char *argv[])
         }
     }
 
-    sfree(mtop);
-    done_top(&top);
+    if (bTPS)
+    {
+        done_top(&top);
+    }
     sfree(xp);
     sfree(xmem);
     sfree(vmem);
@@ -1986,7 +1989,6 @@ int gmx_trjconv(int argc, char *argv[])
     sfree(grpnm);
     sfree(index);
     sfree(cindex);
-    done_filenms(NFILE, fnm);
     done_frame(&fr);
 
     do_view(oenv, out_file, nullptr);