1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2 * $Id: gmx_matrix.c,v 1.4 2008/12/02 18:27:57 spoel Exp $
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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-2008, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
34 * Groningen Machine for Chemical Simulation
42 #include "gmx_fatal.h"
46 #include "gmx_qhop_parm.h"
49 #include <libxml/parser.h>
50 #include <libxml/tree.h>
52 extern int xmlDoValidityCheckingDefaultValue;
54 #define NN(x) (NULL != (x))
56 static const char *xmltypes[] = {
61 "XML_CDATA_SECTION_NODE",
62 "XML_ENTITY_REF_NODE",
67 "XML_DOCUMENT_TYPE_NODE",
68 "XML_DOCUMENT_FRAG_NODE",
70 "XML_HTML_DOCUMENT_NODE",
79 #define NXMLTYPES asize(xmltypes)
83 exmlQHOP, exmlDONOR, exmlACCEPTOR,
84 exmlPARAM, exmlNAME, exmlVALUE,
89 static const char *exml_names[exmlNR] = {
91 "qhop", "donor", "acceptor", "parameter",
92 "name", "value", "unit"
100 static int find_elem(char *name,int nr,const char *names[])
104 for(i=0; (i<nr); i++)
105 if (strcmp(name,names[i]) == 0)
108 gmx_fatal(FARGS,"Unknown element name %s",name);
113 void add_xml_int(xmlNodePtr ptr,const char *name,int val)
117 sprintf((char *)buf,"%d",val);
118 if (xmlSetProp(ptr,(xmlChar *)name,buf) == 0)
119 gmx_fatal(FARGS,"Setting",(char *)name);
122 void add_xml_double(xmlNodePtr ptr,const char *name,double val)
126 sprintf((char *)buf,"%g",val);
127 if (xmlSetProp(ptr,(xmlChar *)name,buf) == 0)
128 gmx_fatal(FARGS,"Setting",(char *)name);
131 void add_xml_char(xmlNodePtr ptr,const char *name,char *val)
133 if (xmlSetProp(ptr,(xmlChar *)name,(xmlChar *)val) == 0)
134 gmx_fatal(FARGS,"Setting",(char *)name);
137 xmlNodePtr add_xml_child(xmlNodePtr parent,const char *type)
141 if ((child = xmlNewChild(parent,NULL,(xmlChar *)type,NULL)) == NULL)
142 gmx_fatal(FARGS,"Creating element",(char *)type);
147 xmlNodePtr add_xml_comment(xmlDocPtr doc,
148 xmlNodePtr prev,char *comment)
152 if ((comm = xmlNewComment((xmlChar *)comment)) == NULL)
153 gmx_fatal(FARGS,"Creating doc comment element","");
155 while (ptr->next != NULL)
164 static char *sp(int n, char buf[], int maxindent)
170 /* Don't indent more than maxindent characters */
178 static void qhop_process_attr(FILE *fp,xmlAttrPtr attr,int parent,
179 int elem,int indent,gmx_qhop_t qht)
181 char *attrname,*attrval;
186 for(i=0; (i<exmlNR); i++)
188 while (attr != NULL) {
189 attrname = (char *)attr->name;
190 attrval = (char *)attr->children->content;
192 #define atest(s) ((gmx_strcasecmp(attrname,s) == 0) && (attrval != NULL))
193 kkk = find_elem(attrname,exmlNR,exml_names);
195 xbuf[kkk] = strdup(attrval);
198 fprintf(fp,"%sProperty: '%s' Value: '%s'\n",sp(indent,buf,99),
206 if (NN(xbuf[exmlDONOR]) && NN(xbuf[exmlACCEPTOR])) {
207 gmx_qhop_set_donor(qht,xbuf[exmlDONOR]);
208 gmx_qhop_set_acceptor(qht,xbuf[exmlACCEPTOR]);
212 if (NN(xbuf[exmlNAME]) && NN(xbuf[exmlUNIT]) && NN(xbuf[exmlVALUE])) {
213 gmx_qhop_add_param(qht,xbuf[exmlNAME],xbuf[exmlVALUE],
220 for(i=0; (i<exmlNR); i++)
225 static void qhop_process_element(FILE *fp,xmlNodePtr tree,int parent,
226 int indent,t_xmlrec *xml)
231 elem = find_elem((char *)tree->name,exmlNR,exml_names);
233 fprintf(fp,"%sElement node name %s\n",sp(indent,buf,99),
235 if (elem == exmlQHOP) {
237 srenew(xml->gqh,xml->nqh);
238 xml->gqh[xml->nqh-1] = gmx_qhop_init();
240 if (elem != exmlQHOPS)
241 qhop_process_attr(fp,tree->properties,parent,
242 elem,indent+2,xml->gqh[xml->nqh-1]);
245 static void qhop_process_tree(FILE *fp,xmlNodePtr tree,int parent,
246 int indent,t_xmlrec *xml)
251 while (tree != NULL) {
253 if ((tree->type > 0) && (tree->type < NXMLTYPES))
254 fprintf(fp,"Node type %s encountered with name %s\n",
255 xmltypes[tree->type],(char *)tree->name);
257 fprintf(fp,"Node type %d encountered\n",tree->type);
260 switch (tree->type) {
261 case XML_ELEMENT_NODE:
262 qhop_process_element(fp,tree,parent,indent+2,xml);
264 if (tree->children) {
265 elem = find_elem((char *)tree->name,exmlNR,exml_names);
266 qhop_process_tree(fp,tree->children,elem,indent+2,xml);
277 gmx_qhops_read(char *fn,int *nqhop)
282 const char *db="qhops.dat";
285 xmlDoValidityCheckingDefaultValue = 0;
288 fn = (char *)gmxlibfn(db);
291 if ((doc = xmlParseFile(fn)) == NULL) {
292 fprintf(stderr,"Reading XML file %s. Run a syntax checker such as nsgmls.",
298 qhop_process_tree(NULL,doc->children,0,0,xml);
309 static void add_xml_qhop(xmlNodePtr parent,gmx_qhop_t qht)
311 xmlNodePtr ptr,child,grandchild,comp;
312 char *name,*type,*value,*unit;
314 ptr = add_xml_child(parent,exml_names[exmlQHOP]);
315 add_xml_char(ptr,exml_names[exmlDONOR],gmx_qhop_get_donor(qht));
316 add_xml_char(ptr,exml_names[exmlACCEPTOR],gmx_qhop_get_acceptor(qht));
318 while (gmx_qhop_get_param(qht,&name,&value,&unit) == 1) {
319 child = add_xml_child(ptr,exml_names[exmlPARAM]);
320 add_xml_char(child,exml_names[exmlNAME],name);
321 add_xml_char(child,exml_names[exmlVALUE],value);
322 add_xml_char(child,exml_names[exmlUNIT],unit);
329 void gmx_qhops_write(char *fn,int nqhop,gmx_qhop_t qht[])
335 xmlChar *libdtdname,*dtdname,*gmx;
337 gmx = (xmlChar *) "qhops";
338 dtdname = (xmlChar *) "qhops.dtd";
339 libdtdname = dtdname;
341 if ((doc = xmlNewDoc((xmlChar *)"1.0")) == NULL)
342 gmx_fatal(FARGS,"Creating XML document","");
344 if ((dtd = xmlCreateIntSubset(doc,dtdname,libdtdname,dtdname)) == NULL)
345 gmx_fatal(FARGS,"Creating XML DTD","");
347 if ((myroot = xmlNewDocNode(doc,NULL,gmx,NULL)) == NULL)
348 gmx_fatal(FARGS,"Creating root element","");
350 myroot->prev = (xmlNodePtr) dtd;
352 /* Add molecule definitions */
353 for(i=0; (i<nqhop); i++)
354 add_xml_qhop(myroot,qht[i]);
356 xmlSetDocCompressMode(doc,0);
357 xmlIndentTreeOutput = 1;
358 if (xmlSaveFormatFileEnc(fn,doc,"ISO-8859-1",2) == 0)
359 gmx_fatal(FARGS,"Saving file",fn);
367 gmx_qhop_t gmx_qhops_read(char *fn,int *nqhop)
369 gmx_fatal(FARGS,"You need to configure the software with --with-xml for function gmx_qhops_read to work");
373 void gmx_qhops_write(char *fn,int nqhop,gmx_qhop_t qht)
375 gmx_fatal(FARGS,"You need to configure the software with --with-xml for function gmx_qhops_write to work");