Tidy: modernize-use-nullptr
[alexxy/gromacs.git] / src / gromacs / utility / txtdump.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2017, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 /* This file is completely threadsafe - please keep it that way! */
40
41 #include "txtdump.h"
42
43 #include <cstdio>
44 #include <cstdlib>
45
46 #include "gromacs/utility/cstringutil.h"
47
48 int pr_indent(FILE *fp, int n)
49 {
50     int i;
51
52     for (i = 0; i < n; i++)
53     {
54         fprintf(fp, " ");
55     }
56     return n;
57 }
58
59 int available(FILE *fp, const void *p, int indent, const char *title)
60 {
61     if (!p)
62     {
63         if (indent > 0)
64         {
65             pr_indent(fp, indent);
66         }
67         fprintf(fp, "%s: not available\n", title);
68     }
69     return (p != nullptr);
70 }
71
72 int pr_title(FILE *fp, int indent, const char *title)
73 {
74     pr_indent(fp, indent);
75     fprintf(fp, "%s:\n", title);
76     return (indent+INDENT);
77 }
78
79 int pr_title_n(FILE *fp, int indent, const char *title, int n)
80 {
81     pr_indent(fp, indent);
82     fprintf(fp, "%s (%d):\n", title, n);
83     return (indent+INDENT);
84 }
85
86 int pr_title_nxn(FILE *fp, int indent, const char *title, int n1, int n2)
87 {
88     pr_indent(fp, indent);
89     fprintf(fp, "%s (%dx%d):\n", title, n1, n2);
90     return (indent+INDENT);
91 }
92
93 void pr_reals(FILE *fp, int indent, const char *title, const real *vec, int n)
94 {
95     int i;
96
97     if (available(fp, vec, indent, title))
98     {
99         pr_indent(fp, indent);
100         fprintf(fp, "%s:\t", title);
101         for (i = 0; i < n; i++)
102         {
103             fprintf(fp, "  %10g", vec[i]);
104         }
105         fprintf(fp, "\n");
106     }
107 }
108
109 void pr_doubles(FILE *fp, int indent, const char *title, const double *vec, int n)
110 {
111     int i;
112
113     if (available(fp, vec, indent, title))
114     {
115         pr_indent(fp, indent);
116         fprintf(fp, "%s:\t", title);
117         for (i = 0; i < n; i++)
118         {
119             fprintf(fp, "  %10g", vec[i]);
120         }
121         fprintf(fp, "\n");
122     }
123 }
124
125 void pr_reals_of_dim(FILE *fp, int indent, const char *title, const real *vec, int n, int dim)
126 {
127     int         i, j;
128     const char *fshort = "%12.5e";
129     const char *flong  = "%15.8e";
130     const char *format;
131
132     if (getenv("GMX_PRINT_LONGFORMAT") != nullptr)
133     {
134         format = flong;
135     }
136     else
137     {
138         format = fshort;
139     }
140
141     if (available(fp, vec, indent, title))
142     {
143         indent = pr_title_nxn(fp, indent, title, n, dim);
144         for (i = 0; i < n; i++)
145         {
146             pr_indent(fp, indent);
147             fprintf(fp, "%s[%5d]={", title, i);
148             for (j = 0; j < dim; j++)
149             {
150                 if (j != 0)
151                 {
152                     fprintf(fp, ", ");
153                 }
154                 fprintf(fp, format, vec[i * dim  + j]);
155             }
156             fprintf(fp, "}\n");
157         }
158     }
159 }
160
161 void pr_int(FILE *fp, int indent, const char *title, int i)
162 {
163     pr_indent(fp, indent);
164     fprintf(fp, "%-30s = %d\n", title, i);
165 }
166
167 void pr_int64(FILE *fp, int indent, const char *title, gmx_int64_t i)
168 {
169     char buf[STEPSTRSIZE];
170
171     pr_indent(fp, indent);
172     fprintf(fp, "%-30s = %s\n", title, gmx_step_str(i, buf));
173 }
174
175 void pr_real(FILE *fp, int indent, const char *title, real r)
176 {
177     pr_indent(fp, indent);
178     fprintf(fp, "%-30s = %g\n", title, r);
179 }
180
181 void pr_double(FILE *fp, int indent, const char *title, double d)
182 {
183     pr_indent(fp, indent);
184     fprintf(fp, "%-30s = %g\n", title, d);
185 }
186
187 void pr_str(FILE *fp, int indent, const char *title, const char *s)
188 {
189     pr_indent(fp, indent);
190     fprintf(fp, "%-30s = %s\n", title, s);
191 }
192
193 void pr_strings(FILE *fp, int indent, const char *title, char ***nm, int n, gmx_bool bShowNumbers)
194 {
195     int i;
196
197     if (available(fp, nm, indent, title))
198     {
199         indent = pr_title_n(fp, indent, title, n);
200         for (i = 0; i < n; i++)
201         {
202             pr_indent(fp, indent);
203             fprintf(fp, "%s[%d]={name=\"%s\"}\n",
204                     title, bShowNumbers ? i : -1, *(nm[i]));
205         }
206     }
207 }