00001 #include <stdlib.h>
00002 #include <cstdio>
00003 #include <string.h>
00004 #include <math.h>
00005 #include <ctype.h>
00006 #include <libxml/xmlmemory.h>
00007 #include <libxml/parser.h>
00008 #include "def.h"
00009 #include "run.h"
00010 #include "io.h"
00011
00012 namespace IO {
00013 int ioutput=0,
00014 nxwdump=0;
00015 void initxwd(int i)
00016 {
00017 nxwdump=i;
00018 }
00019 void xwd(char *windowname)
00020 {
00021 int sysreturned;
00022 char
00023 *s,
00024 dumpfilename[MAXLINLEN],
00025 command[MAXLINLEN];
00026 for (s=windowname; !isalpha(*s); s++);
00027 sprintf(dumpfilename,"%s%03d.xwd",s,nxwdump++);
00028 sprintf(command,"xwd -name %s -out %s",windowname,dumpfilename);
00029 if (Run::option.verbose||Run::option.debug)
00030 { printf("Dumping window to '%s' ... ",dumpfilename);fflush(stdout);
00031 }
00032 sysreturned=system(command);
00033 if (Run::option.verbose||Run::option.debug)
00034 printf("done\n");
00035 if (sysreturned!=0)
00036 { fprintf(stderr,"Window dump to %s failed with status %d\n",sysreturned);
00037 fflush(stderr);
00038 }
00039 }
00040 int getCharAttr(xmlNodePtr cur, char *attr, char *result) {
00041 xmlChar *key;
00042 key = xmlGetProp(cur, (const xmlChar *)attr);
00043 if(key!=NULL)
00044 { strncpy(result,(char*)key,MAXLINLEN);
00045 xmlFree(key);
00046 return 1;
00047 }
00048 if(Run::option.verbose||Run::option.debug)
00049 { fprintf
00050 ( stderr,
00051 "\tAttribute %s not found\n",
00052 attr
00053 );
00054 }
00055 return 0;
00056 }
00057 int getIntAttr(xmlNodePtr cur, char *attr)
00058 {
00059 xmlChar *key;
00060 key = xmlGetProp(cur, (const xmlChar *)attr);
00061 if(key!=NULL)
00062 { int result;
00063 result=atoi((char*)key);
00064 xmlFree(key);
00065 return result;
00066 }
00067 if(Run::option.verbose||Run::option.debug)
00068 { fprintf
00069 ( stderr,
00070 "\tAttribute %s not found\n",
00071 attr
00072 );
00073 }
00074 return -1;
00075 }
00076 int getIntAttr
00077 ( xmlDocPtr doc,
00078 char *tag,
00079 char *keyname,
00080 char *key,
00081 char *attr
00082 )
00083 {
00084 xmlNodePtr cur;
00086 cur = xmlDocGetRootElement(doc);
00087 cur = cur->xmlChildrenNode;
00088 if(Run::option.verbose)
00089 { printf
00090 ( "getIntAttr:tag=%s, keyname=%s, key=%s, attr=%s\n",
00091 tag,keyname,key,attr
00092 ); fflush(stdout);
00093 }
00094 while (cur != NULL)
00095 { if ((!xmlStrcmp(cur->name, (const xmlChar *)tag)))
00096 { char keyval[MAXLINLEN];
00097 if(getCharAttr(cur,keyname,keyval)!=0)
00098 { if(strcmp(keyval,key)==0)
00099 { int value=0;
00101 value=parseInt(doc,cur,attr);
00102 return value;
00103 }
00104 }
00105 }
00106 cur=cur->next;
00107 }
00108 if(Run::option.verbose||Run::option.debug) {
00109 fprintf
00110 ( stderr,
00111 "\tTag '%s' not found\n",
00112 keyname
00113 ); fflush(stderr);
00114 }
00115 return -1;
00116 }
00117 int parseWord(xmlDocPtr doc, xmlNodePtr cur, char *keyword, char *result) {
00118 xmlChar *key;
00119 cur = cur->xmlChildrenNode;
00120 while (cur != NULL)
00121 { if ((!xmlStrcmp(cur->name, (const xmlChar *)keyword)))
00122 { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00123 strncpy(result,(char*)key,MAXLINLEN);
00124 xmlFree(key);
00125 return 1;
00126 }
00127 cur = cur->next;
00128 }
00129 if(Run::option.verbose||Run::option.debug) {
00130 fprintf
00131 ( stderr,
00132 "\tTag '%s' not found\n",
00133 keyword
00134 ); fflush(stderr);
00135 }
00136 return 0;
00137 }
00138 int parseInt(xmlDocPtr doc, xmlNodePtr cur, char *keyword)
00139 {
00140 cur = cur->xmlChildrenNode;
00141 while (cur != NULL)
00142 { if ((!xmlStrcmp(cur->name, (const xmlChar *)keyword)))
00143 { xmlChar *key;
00144 int keyval;
00145 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00146 keyval=(int)atoi((char*)key);
00147 xmlFree(key);
00148 return keyval;
00149 }
00150 cur = cur->next;
00151 }
00152 if(Run::option.verbose||Run::option.debug) {
00153 fprintf(stderr,"Integer data for tag '%s' not found\n",keyword);
00154 fflush(stderr);
00155 }
00156 return -1;
00157 }
00158 int parseFloat(xmlDocPtr doc, xmlNodePtr cur, char *keyword, REAL &result) {
00159 xmlChar *key;
00160 cur = cur->xmlChildrenNode;
00161 while (cur != NULL)
00162 { if ((!xmlStrcmp(cur->name, (const xmlChar *)keyword)))
00163 {
00164 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00165
00166 if(key!=NULL) result=(REAL)atof((char*)key);
00167 xmlFree(key);
00168 return 1;
00169 }
00170 cur = cur->next;
00171 }
00172 if(Run::option.verbose||Run::option.debug) {
00173 fprintf(stderr,"Float data for tag '%s' not found\n",keyword);
00174 fflush(stderr);
00175 }
00176 return 0;
00177 }
00178 double parseFloat(xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
00179 xmlChar *key;
00180 cur = cur->xmlChildrenNode;
00181 while (cur != NULL)
00182 { if ((!xmlStrcmp(cur->name, (const xmlChar *)keyword)))
00183 { double result;
00184 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00185
00186 result=(double)atof((char*)key);
00187 xmlFree(key);
00188 return result;
00189 }
00190 cur = cur->next;
00191 }
00192 if(Run::option.verbose||Run::option.debug) {
00193 fprintf(stderr,"Float data for tag '%s' not found\n",keyword);
00194 fflush(stderr);
00195 }
00196 return 0.0;
00197 }
00198 void getTimeXML(char inpfilename[])
00199 {
00200 using namespace Run;
00201 int level=0,timefound=0;
00202 char *p,word[MAXLINLEN];
00203 FILE *inp;
00204 const char *doctype=DOCTYPE;
00205 xmlDocPtr doc;
00206 xmlNodePtr cur;
00207
00208 time.start=0.0;
00209 time.end=1.0;
00210 time.step=time.step0=0.01;
00211 doc = xmlParseFile(inpfilename);
00212 if (doc == NULL )
00213 { fprintf(stderr,"XML parser failed in %s\n",inpfilename);
00214 return;
00215 }
00216 cur = xmlDocGetRootElement(doc);
00217 if (cur == NULL)
00218 { fprintf(stderr,"Empty document: %s\n",inpfilename);
00219 xmlFreeDoc(doc);
00220 return;
00221 }
00222 if (xmlStrcmp(cur->name, (const xmlChar *) doctype))
00223 { fprintf(stderr,"document of the wrong type, root node != %s in %s\n",doctype,inpfilename);
00224 xmlFreeDoc(doc);
00225 return;
00226 }
00227 cur = cur->xmlChildrenNode;
00228 while (cur != NULL)
00229 { if ((!xmlStrcmp(cur->name, (const xmlChar *)"time")))
00230 { time.start=parseFloat(doc,cur,(char*)"start");
00231 time.end=parseFloat(doc,cur,(char*)"end");
00232 time.step=parseFloat(doc,cur,(char*)"step");
00233 time.output=parseFloat(doc,cur,(char*)"output");
00234 }
00235 cur = cur->next;
00236 }
00237 xmlFreeDoc(doc);
00238 }
00239 void getTime(char inpfilename[])
00240 { using namespace Run;
00241 if(strstr(inpfilename,".xml\0")!=NULL)
00242 getTimeXML(inpfilename);
00243 else {
00244 fprintf(stderr,"File %s should have extention 'xml'\n"); exit(1);
00245 }
00246 if (Run::option.verbose)
00247 printf("Time: start=%g, end=%g, step=%g, output=%g\n",time.start,time.end,time.step,time.output);
00248 }
00249 int getIter(char inpfilename[])
00250 { using namespace Run;
00251 if(strstr(inpfilename,".xml\0")==NULL) {
00252 fprintf(stderr,"File %s should have extention 'xml'\n"); exit(1);
00253 }
00254 int niter=0,level=0;
00255 char *p,word[MAXLINLEN];
00256 FILE *inp;
00257 const char *doctype=DOCTYPE;
00258 xmlDocPtr doc;
00259 xmlNodePtr cur;
00260 doc = xmlParseFile(inpfilename);
00261 if (doc == NULL )
00262 { fprintf(stderr,"XML parser failed in %s\n",inpfilename);
00263 return 0;
00264 }
00265 cur = xmlDocGetRootElement(doc);
00266 if (cur == NULL)
00267 { fprintf(stderr,"Empty document: %s\n",inpfilename);
00268 xmlFreeDoc(doc);
00269 return 0;
00270 }
00271 if (xmlStrcmp(cur->name, (const xmlChar *) doctype))
00272 { fprintf(stderr,"document of the wrong type, root node != %s in %s\n",doctype,inpfilename);
00273 xmlFreeDoc(doc);
00274 return 0;
00275 }
00276 cur = cur->xmlChildrenNode;
00277 while (cur != NULL)
00278 { if ((!xmlStrcmp(cur->name, (const xmlChar *)"iterations"))) {
00279 xmlChar *key;
00280 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00281 niter=(int)atoi((char*)key);
00282 xmlFree(key);
00283 break;
00284 }
00285 cur = cur->next;
00286 }
00287 xmlFreeDoc(doc);
00288 if (Run::option.verbose)
00289 printf("Read number of iterations %d from file %s\n",niter,inpfilename);
00290 return niter;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 }