Merge branch 'release-4-6', adds the nbnxn functionality
[alexxy/gromacs.git] / src / gromacs / gmxlib / warninp.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <sysstuff.h>
41 #include <string.h>
42 #include "smalloc.h"
43 #include "statutil.h"
44 #include "string2.h"
45 #include "gmx_fatal.h"
46 #include "warninp.h"
47
48 typedef struct warninp {
49     gmx_bool bAllowWarnings;
50     int  nwarn_note;
51     int  nwarn_warn;
52     int  nwarn_error;
53     int  maxwarn;
54     int  lineno;
55     char filenm[256];
56 } t_warninp;
57
58 warninp_t init_warning(gmx_bool bAllowWarnings,int maxwarning)
59 {
60     warninp_t wi;
61
62     snew(wi,1);
63
64     wi->bAllowWarnings = bAllowWarnings;
65     wi->maxwarn        = maxwarning;
66     wi->nwarn_note     = 0;
67     wi->nwarn_warn     = 0;
68     wi->nwarn_error    = 0;
69     strcpy(wi->filenm,"unknown");
70     wi->lineno         = 0;
71
72     return wi;
73 }
74
75 void set_warning_line(warninp_t wi,const char *s,int line)
76 {
77     if (s == NULL)
78     {
79         gmx_incons("Calling set_warning_line with NULL pointer");
80     }
81
82     strcpy(wi->filenm,s);
83     wi->lineno = line;
84 }
85
86 int get_warning_line(warninp_t wi)
87 {
88     return wi->lineno;
89 }
90
91 const char *get_warning_file(warninp_t wi)
92 {
93     return wi->filenm;
94 }
95
96 static void low_warning(warninp_t wi,const char *wtype,int n,const char *s)
97 {
98 #define indent 2 
99     char *temp, *temp2;
100     int  i;
101     
102     if (s == NULL)
103     {
104         s = "Empty error message.";
105     }
106     snew(temp,strlen(s)+indent+1);
107     for(i=0; i<indent; i++)
108     {
109         temp[i] = ' ';
110     }
111     temp[indent] = '\0';
112     strcat(temp,s);
113     temp2 = wrap_lines(temp,78-indent,indent,FALSE);
114     if (strlen(wi->filenm) > 0)
115     {
116         if (wi->lineno != -1)
117         {
118             fprintf(stderr,"\n%s %d [file %s, line %d]:\n%s\n\n",
119                     wtype,n,wi->filenm,wi->lineno,temp2);
120         }
121         else
122         {
123             fprintf(stderr,"\n%s %d [file %s]:\n%s\n\n",
124                     wtype,n,wi->filenm,temp2);
125         }   
126     }
127     else
128     {
129         fprintf(stderr,"\n%s %d:\n%s\n\n",wtype,n,temp2);
130     }
131     sfree(temp);
132     sfree(temp2);
133 }
134
135 void warning(warninp_t wi,const char *s)
136 {
137     if (wi->bAllowWarnings)
138     {
139         wi->nwarn_warn++;
140         low_warning(wi,"WARNING",wi->nwarn_warn,s);
141     }
142     else
143     {
144         warning_error(wi,s);
145     }
146 }
147
148 void warning_note(warninp_t wi,const char *s)
149 {
150     wi->nwarn_note++;
151     low_warning(wi,"NOTE",wi->nwarn_note,s);
152 }
153
154 void warning_error(warninp_t wi,const char *s)
155 {
156     wi->nwarn_error++;
157     low_warning(wi,"ERROR",wi->nwarn_error,s);
158 }
159
160 static void print_warn_count(const char *type,int n)
161 {
162     if (n > 0)
163     {
164         fprintf(stderr,"\nThere %s %d %s%s\n",
165                 (n==1) ? "was" : "were", n, type, (n==1) ? "" : "s");
166     }
167 }
168
169 void check_warning_error(warninp_t wi,int f_errno,const char *file,int line)
170 {
171     if (wi->nwarn_error > 0)
172     {
173         print_warn_count("note",wi->nwarn_note);
174         print_warn_count("warning",wi->nwarn_warn);
175
176         gmx_fatal(f_errno,file,line,"There %s %d error%s in input file(s)",
177                   (wi->nwarn_error==1) ? "was" : "were",wi->nwarn_error,
178                   (wi->nwarn_error==1) ? ""    : "s");
179     }
180 }
181
182 void done_warning(warninp_t wi,int f_errno,const char *file,int line)
183 {
184     print_warn_count("note",wi->nwarn_note);
185     print_warn_count("warning",wi->nwarn_warn);
186
187     check_warning_error(wi,f_errno,file,line);
188
189     if (wi->maxwarn >= 0 && wi->nwarn_warn > wi->maxwarn)
190     {
191         gmx_fatal(f_errno,file,line,
192                   "Too many warnings (%d), %s terminated.\n"
193                   "If you are sure all warnings are harmless, use the -maxwarn option.",
194                   wi->nwarn_warn,Program());
195     }
196
197     sfree(wi);
198 }
199
200 void _too_few(warninp_t wi,const char *fn,int line)
201 {
202     char buf[STRLEN];
203     
204     sprintf(buf,
205             "Too few parameters on line (source file %s, line %d)",
206             fn,line);
207     warning(wi,buf);
208 }
209
210 void _incorrect_n_param(warninp_t wi,const char *fn,int line)
211 {
212     char buf[STRLEN];
213
214     sprintf(buf,
215             "Incorrect number of parameters on line (source file %s, line %d)",
216             fn,line);
217     warning(wi,buf);
218 }