cf34cebbf9fcb12e408e50fe1340e55ce75531a3
[alexxy/gromacs.git] / src / mdlib / gmx_qhop_parm.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 4.5
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2008, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14  
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Groningen Machine for Chemical Simulation
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include "smalloc.h"
42 #include "string2.h"
43
44 #include "gmx_qhop_parm.h"      
45         
46 typedef struct gmx_qhop {
47   char *donor,*acceptor;
48   int nparam,nparam_c;
49   char **value,**unit,**name;
50 } gmx_qhop;
51
52 #define assign_str(dst,src)  if (NULL != src) { if (NULL != dst) *dst = strdup(src); } else { *dst = NULL; }
53 #define assign_scal(dst,src) if (NULL != dst) *dst = src
54
55 /* Return a new gmx_qhop structure */
56 gmx_qhop_t gmx_qhop_init()
57 {
58   struct gmx_qhop *qht;
59   
60   snew(qht,1);
61
62   return qht;
63 }
64
65 void gmx_qhop_set_donor(gmx_qhop_t gqh,char *donor)
66 {
67   gqh->donor = strdup(donor);
68 }
69
70 void gmx_qhop_set_acceptor(gmx_qhop_t gqh,char *acceptor)
71 {
72   gqh->acceptor = strdup(acceptor);
73 }
74
75 char *gmx_qhop_get_donor(gmx_qhop_t gqh)
76 {
77   return gqh->donor;
78 }
79
80 char *gmx_qhop_get_acceptor(gmx_qhop_t gqh)
81 {
82   return gqh->acceptor;
83 }
84
85 /* Add parameter to gqh, return 1 if OK, 0 if not OK */
86 int gmx_qhop_add_param(gmx_qhop_t gqh,char *name,char *value,char *unit)
87 {
88   srenew(gqh->name,gqh->nparam+1);
89   srenew(gqh->value,gqh->nparam+1);
90   srenew(gqh->unit,gqh->nparam+1);
91   gqh->name[gqh->nparam]  = strdup(name);
92   gqh->value[gqh->nparam] = strdup(value);
93   gqh->unit[gqh->nparam]  = strdup(unit);
94   gqh->nparam++;
95   
96   return 1;
97 }
98
99 /* Lists the parameters, one by one on repeatedly calling the
100    function. Returns 1 if OK, 0 if not OK */
101 int gmx_qhop_get_param(gmx_qhop_t gqh,char **name,char **value,char **unit)
102 {
103   if (gqh->nparam_c < gqh->nparam) {
104     assign_str(name,gqh->name[gqh->nparam_c]);
105     assign_str(value,gqh->value[gqh->nparam_c]);
106     assign_str(unit,gqh->unit[gqh->nparam_c]);
107     gqh->nparam_c++;
108     
109     return 1;
110   }
111   else
112     gqh->nparam_c = 0;
113     
114   return 0;
115 }
116
117 /* Return a value corresponding to name */
118 int gmx_qhop_get_value(gmx_qhop_t gqh,char *name,double *x)
119 {
120   int i;
121   
122   for(i=0; (i<gqh->nparam); i++) 
123     if (gmx_strcasecmp(gqh->name[i],name) == 0) {
124       *x = strtod(gqh->value[i],NULL);
125       return 1;
126     }
127     
128   return 0;
129 }
130
131 /* Liberate memory */
132 void gmx_qhop_done(gmx_qhop_t gqh)
133 {
134   int i;
135   
136   for(i=0; (i<gqh->nparam); i++) {
137     sfree(gqh->name[i]);
138     sfree(gqh->value[i]);
139     sfree(gqh->unit[i]);
140   }
141   if (gqh->nparam > 0) {
142     sfree(gqh->name);
143     sfree(gqh->value);
144     sfree(gqh->unit);
145   }
146   if (gqh->donor)
147     sfree(gqh->donor);
148   if (gqh->acceptor)
149     sfree(gqh->acceptor);
150 }
151