Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
read_SiestaXML.cpp
Go to the documentation of this file.
1 
19 #include "mex.h"
20 #include <unistd.h>
21 #include <string.h>
22 #include "EQuUs_MATLAB.h"
23 
24 #ifndef MAX_PROP_LENGTH
25 #define MAX_PROP_LENGTH 100
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 
33 #ifdef __INTEL_COMPILER
34 void fox_sax_equus_mp_read_xml_( char* fnamget_eq_structurese, long *fname_len, char** property_names, int* numodProperties );
35 void fox_sax_equus_mp_get_eq_structure_( EQstruct* eq_struct , int* idx);
36 void fox_sax_equus_mp_deallocatetargetnodes_();
37 int fox_sax_equus_mp_numtargetnodes_;
38 #else
39 void __fox_sax_equus_MOD_read_xml( char* fnamget_eq_structurese, long *fname_len, char** property_names, int* numodProperties );
40 void __fox_sax_equus_MOD_get_eq_structure( EQstruct* eq_struct , int* idx);
43 #endif
44 
45 #ifdef __cplusplus
46  }
47 #endif
48 
56 void XMLnode2MatlabStruct( EQstruct* eq_struct, mxArray* Mstruct );
57 
58 
65 void mexFunction(int nlhs, mxArray *plhs[],
66  int nrhs, const mxArray *prhs[])
67 {
68  /* input filename */
69  char *filename;
70  /* length of the filename */
71  long buflen;
72  /* field names of the output structure */
73  char **fieldnames;
74  /* names of the properties to be retrived from the XML */
75  char** property_names;
76  /* number of the properties to be retrived from the XML */
77  int numofProperties;
78  /* NUmber of obtained target XML nodes */
79  int numTargetNodes;
80 
81 /*-----------------------------------------------------------------------*/
82 /* Check for proper number of arguments. */
83 
84  if(nrhs != 2) {
85  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:nInput", "Two inputs required.");
86  }
87  else if (nlhs > 1) {
88  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:nOutput", "Too many output arguments.");
89  }
90 
91  /* Validate inputs */
92 
93  /* Check that the first input is a string.*/
94  if ((mxIsChar(prhs[0]) != 1)) {
95  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:NonString", "First input must be a string.");
96  }
97 
98 
99  /* copy the string data from prhs[0] into a C string. */
100  filename = mxArrayToString(prhs[0]);
101 #ifdef DEBUG
102  mexPrintf( "%s\n", filename );
103 #endif
104 
105  if (access(filename, F_OK) == -1) {
106  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:FileNotExist", "The given file %s does not exist.", filename);
107  }
108 
109 
110 
111  /* Check that the second input is a cell of strings.*/
112  int idx;
113  mxArray* cell;
114  if ((mxIsCell(prhs[1]) != 1)) {
115  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:NonCell", "Second input must be a cell.");
116  }
117  if ( (mxGetN(prhs[1]) >1) && (mxGetM(prhs[1]) >1) ) {
118  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:NonVector", "Second input must be a cell vector.");
119  }
120  numofProperties = mxGetN(prhs[1]) * mxGetM(prhs[1]);
121  property_names = (char**)malloc( numofProperties*sizeof(char*) );
122  for ( idx = 0; idx<numofProperties; idx++ ) {
123  cell = mxGetCell( prhs[1], idx);
124  if ( mxGetN(cell)*mxGetM(cell)+1 > MAX_PROP_LENGTH ) {
125  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:MAX_PROP_LENGTH", "Increase the maximal length of the proeprty names.");
126  }
127  if (!mxIsChar(cell)) {
128  mexErrMsgIdAndTxt ("siesta_EQuUs:read_SiestaXML:NonString", "Second input must contain of strings.");
129  }
130  property_names[idx] = (char*)malloc( MAX_PROP_LENGTH*sizeof(char) );
131  buflen = (mxGetM(cell) * mxGetN(cell)) + 1;
132  mxGetString(cell, property_names[idx], MAX_PROP_LENGTH);
134  }
135 
136 
137 
138  /* get the length of the input file */
139  buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
140 
141  /* Call the fortran routines to parse the XML file */
142 #ifdef __INTEL_COMPILER
143  fox_sax_equus_mp_read_xml_( filename, &buflen, property_names, &numofProperties);
144  numTargetNodes = fox_sax_equus_mp_numtargetnodes_;
145 #else
146  __fox_sax_equus_MOD_read_hsx( filename, &buflen, eq_structs);
147  numTargetNodes = __fox_sax_equus_MOD_numtargetnodes;
148 #endif
149 
150 
151 #ifdef DEBUG
152  mexPrintf( "Number of obtained target nodes: %d\n", numTargetNodes);
153 #endif
154 
155 
156  /* creating en empty structure for MATLAB */
157  plhs[0] = mxCreateStructMatrix(1, 1, 0, NULL);
158 
159 
160  /* retriving the obtained XML nodes */
161  EQstruct eq_struct;
162  for( idx=1; idx<=numTargetNodes; idx++ ) {
163 
164 #ifdef __INTEL_COMPILER
165  fox_sax_equus_mp_get_eq_structure_( &eq_struct, &idx );
166 #else
167  __fox_sax_equus_MOD_get_eq_structure( eq_struct, &idx );
168 #endif
169 
170  /* regularize the obtained structure */
171  regularizeEQstruct( &eq_struct );
172 //continue;
173  /* adding XML node to the output structure */
174  XMLnode2MatlabStruct( &eq_struct, plhs[0] );
175 
176 
177  }
178 
179 #ifdef DEBUG
180  mexPrintf( "\nDeallocating Target nodes\n*********************************\n" );
181 #endif
182 
183 #ifdef __INTEL_COMPILER
184  fox_sax_equus_mp_deallocatetargetnodes_();
185 #else
187 #endif
188 
189 }
190 
191 
196 void XMLnode2MatlabStruct( EQstruct* eq_struct, mxArray* Mstruct ) {
197 
198 
199  int idx;
200  EQelement* field;
201  mxArray* struct_tmp;
202  char** localname = NULL;
203 
204  if (mxIsStruct( Mstruct ) == 0) {
205  mexErrMsgIdAndTxt ("read_SiestaXML:XMLnode2MatlabStruct:NotStructure", "The given mxArray is not a structure.");
206  }
207 
208  struct_tmp = mxCreateStructMatrix(1, 1, 0, NULL);
209 
210  for( idx=0; idx<=eq_struct->numfields-1; idx++ ) {
211  field = &eq_struct->fields[idx];
212  if (( strcmp(field->elementname, "localname") == 0) && localname==NULL) {
213  localname = (char**)field->value;
214  }
215  else if ( strcmp(field->elementname, "subnodes") == 0) {
216  EQstruct* subnodes = (EQstruct*)field->value;
217  EQstruct* subnode;
218  mxArray* Msubnode = mxCreateStructMatrix(1, 1, 0, NULL);
219 #ifdef DEBUG
220  mexPrintf("number of subnodes: %d\n", subnodes->numfields );
221 #endif
222  int jdx;
223  for( jdx=0; jdx<subnodes->numfields; jdx++ ) {
224  subnode = (EQstruct*) subnodes->fields[jdx].value;
225  XMLnode2MatlabStruct( subnode, Msubnode );
226  }
227  setField( Msubnode, field->elementname, struct_tmp );
228  }
229  else {
230  setField( field, struct_tmp );
231  }
232  }
233 
234 
235  if ( localname == NULL ) {
236  localname = (char**)mxMalloc( sizeof(char*) );
237  strcpy( localname[0], "Unknown");
238  }
239 
240  setField( struct_tmp, localname[0], Mstruct );
241 
242  return;
243 }
244 
245 
246 
247 
248 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Gateway routine to read in the hsx structure generated by the SIESTA package (see http://www....
void __fox_sax_equus_MOD_read_xml(char *fnamget_eq_structurese, long *fname_len, char **property_names, int *numodProperties)
void __fox_sax_equus_MOD_get_eq_structure(EQstruct *eq_struct, int *idx)
Header file for felper functions for interlanguage conversions.
int numfields
Definition: EQuUs_MATLAB.h:19
void __fox_sax_equus_MOD_deallocatetargetnodes()
void * value
Definition: EQuUs_MATLAB.h:11
#define MAX_PROP_LENGTH
====================================================================== Gateway routine to read in the...
void convert2FortranString(char *cstring, int strlength)
Converts a C string into fortran compatible character array.
void regularizeEQstruct(EQstruct *eq_struct)
Sort out invalid EQelements from an EQstruct structure prototype.
void setField(EQelement *eq_field, mxArray *Mstruct)
Set a field in a MATLAB structure.
void XMLnode2MatlabStruct(EQstruct *eq_struct, mxArray *Mstruct)
Routine to turn nested EQstruct structure prototypes into MATLAB structure.
int __fox_sax_equus_MOD_numtargetnodes
EQelement * fields
Definition: EQuUs_MATLAB.h:18
char * elementname
Definition: EQuUs_MATLAB.h:13
character(len=max_prop_length, kind=c_char), dimension(:), pointer property_names