yanera_xml_util.c

Go to the documentation of this file.
00001 /*============================================================================
00002 Copyright 2006 Thad Harroun, Kevin Yager
00003 
00004 This file is part of "yanera 2.0".
00005 
00006 "yanera 2.0" is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
00007 
00008 "yanera 2.0" is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00009 
00010 You should have received a copy of the GNU General Public License along with "yanera 2.0"; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
00011 ==============================================================================*/ 
00012 /*============================================================================*/
00018 #define _CRT_SECURE_NO_DEPRECATE
00019 
00020 #include "yanera.h"
00021 #include "yanera_xml_util.h"
00022 #include "yanera_util.h"
00023 #include "yanera_postfix.h"
00024 /*=========================================================================*/
00025 void linkComponentIntoContainer(yanera_layer *layer, yanera_container *yanera)
00026 {
00027   yanera_layer *l;
00028   /*
00029    * Any error checking to be done?
00030    */
00031 
00032   /* 
00033    * link-in the layer as the last component 
00034    */
00035   if (yanera->components == NULL)
00036   {
00037     yanera->components = layer;
00038   }
00039   else
00040   {
00041     l = yanera->components;
00042     while (l->next != NULL) {
00043       l = l->next;
00044     };
00045     l->next = layer;
00046   }
00047   
00048   return;
00049 }
00050 /*============================================================================*/
00051 short newParameter(double d, short i, yanera_container *yanera)
00052 {
00053   /*
00054    * Increase the size of the parameter and fix arrays by one,
00055    * and add the values passed into the funciton.
00056    */
00057   yanera->parameters.p   = realloc(yanera->parameters.p, \
00058       (yanera->parameters.n+1)*sizeof(double));
00059   yanera->parameters.f   = realloc(yanera->parameters.f, \
00060       (yanera->parameters.n+1)*sizeof(unsigned short));
00061   yanera->parameters.tmp = realloc(yanera->parameters.tmp, \
00062       (yanera->parameters.n+1)*sizeof(double));
00063   yanera->parameters.min = realloc(yanera->parameters.min, \
00064       (yanera->parameters.n+1)*sizeof(double));
00065   yanera->parameters.max = realloc(yanera->parameters.max, \
00066       (yanera->parameters.n+1)*sizeof(double));
00067   yanera->parameters.con = realloc(yanera->parameters.con, \
00068       (yanera->parameters.n+1)*sizeof(double));
00069            
00070   yanera->parameters.p[yanera->parameters.n] = d;
00071   yanera->parameters.f[yanera->parameters.n] = i;
00072   yanera->parameters.tmp[yanera->parameters.n] = 0.0;
00073   
00074   yanera->parameters.min[yanera->parameters.n] = -10*fabs(d);
00075   yanera->parameters.max[yanera->parameters.n] =  10*fabs(d);
00076   yanera->parameters.con[yanera->parameters.n] = CONSTRAIN_NONE;
00077   
00078   yanera->parameters.n += 1;
00079   
00080   /*
00081    * Return the array index value of the newly added paramter.
00082    */
00083   return (yanera->parameters.n - 1);
00084 }
00085 
00086 /*============================================================================*/
00087 short newFuncParameter(char c, double d, unsigned short i, \
00088                        yanera_layer *layer, yanera_container *yanera)
00089 {
00090   /*
00091    * Add the paramater variable to the list of variables.
00092    */
00093   layer->parm_names = realloc(layer->parm_names, \
00094                              (layer->number_of_parm+1)*sizeof(char));
00095   layer->parm_values= realloc(layer->parm_values, \
00096                              (layer->number_of_parm+1)*sizeof(short));
00097   layer->parm_names[layer->number_of_parm] = c;
00098   /*
00099    * Add the actual numerical value to the paramter array.
00100    */
00101   layer->parm_values[layer->number_of_parm] = newParameter(d, i, yanera);
00102   layer->number_of_parm += 1;
00103       
00104   return layer->parm_values[layer->number_of_parm-1];
00105 }
00106 /*============================================================================*/
00107 unsigned short getYesNoBooleanFromString(char *buf)
00108 {
00109   short i1;
00110   
00111   i1 = strncmp(buf, "YES", 3);
00112   if (i1 == 0) return YES;
00113   else
00114   {
00115     i1 = strncmp(buf, "NO", 2);
00116     if (i1 == 0) return NO;
00117   }
00118   i1 = strncmp(buf, "yes", 3);
00119   if (i1 == 0) return YES;
00120   else
00121   {
00122     i1 = strncmp(buf, "no", 2);
00123     if (i1 == 0) return NO;
00124   }
00125   return ERROR;
00126 }
00127 /*============================================================================*/
00128 void setYesNoBooleanFromString(char *buf, short i1)
00129 {
00130   if (i1 == YES) sprintf((char *)buf, "YES");
00131   else  sprintf((char *)buf, "NO");
00132   return;
00133 }
00134 /*=========================================================================*/
00135 yanera_layer *newLayer(short type)
00136 {
00137   yanera_layer *layer = NULL;
00138   
00139   /*
00140    * Any error checking to be done?
00141    */
00142   
00143   /* 
00144    * Allocate a new layer
00145    */
00146   layer = (yanera_layer *)malloc(sizeof(yanera_layer));
00147   if (layer == NULL) 
00148   {
00149     yanera_error("No memory for a new layer.", __FILE__, __LINE__, NULL);
00150   }
00151   layer->layer_id        = NULL;
00152   layer->layer_idref     = NULL;
00153   layer->component_id    = NULL;
00154   layer->component_idref = NULL;
00155   layer->type            = type;
00156   layer->name            = NULL;
00157   layer->func            = NULL;
00158   layer->func_postfix    = NULL;
00159   layer->parm_names      = NULL;
00160   layer->parm_values     = NULL;
00161   layer->number_of_parm  = 0;
00162   layer->thik            = -1;
00163   layer->sigz            = -1;
00164   layer->cntr            = -1;
00165   layer->rsld            = -1;
00166   layer->isld            = -1;
00167   layer->rmag            = -1;
00168   layer->include_flag    = YES;
00169   layer->repeat_flag     = 0;
00170   layer->polarized       = POLARIZED_NONE;
00171   layer->prev            = NULL;
00172   layer->next            = NULL;
00173   
00174   return layer;
00175 }
00176 /*=========================================================================*/
00177 yanera_layer *copyLayer(yanera_layer *source)
00178 {
00179   yanera_layer *new;
00180   yanera_postfix_queue_element *pfqe;
00181 
00182   new = newLayer(LAYER_LAYER);
00183   /*-----------------------------------
00184    * ID's and IDREF's
00185    */
00186   if (source->layer_id != NULL)
00187   {
00188     new->layer_id = (char *)calloc((strlen(source->layer_id)+1),sizeof(char));
00189     strcpy(new->layer_id, source->layer_id);
00190   }
00191   else new->layer_id = NULL;
00192   if (source->layer_idref != NULL)
00193   {
00194     new->layer_idref = (char *)calloc((strlen(source->layer_idref)+1), \
00195                                       sizeof(char));
00196     strcpy(new->layer_idref, source->layer_idref);
00197   }
00198   else new->layer_idref = NULL;
00199   if (source->component_id != NULL)
00200   {
00201     new->component_id = (char *)calloc((strlen(source->component_id)+1), \
00202                                    sizeof(char));
00203     strcpy(new->component_id, source->component_id);
00204   }
00205   else new->component_id = NULL;
00206   if (source->component_idref != NULL)
00207   {
00208     new->component_idref = (char *)calloc((strlen(source->component_idref)+1), \
00209                                    sizeof(char));
00210     strcpy(new->component_idref, source->component_idref);
00211   }
00212   else new->component_idref = NULL;
00213   /*-----------------------------------
00214    * General layer information
00215    */
00216   new->type = source->type;
00217   if (source->name != NULL)
00218   {
00219     new->name = (char *)calloc((strlen(source->name)+1),sizeof(char));
00220     strcpy(new->name, source->name);
00221   }
00222   else new->name = NULL;
00223   /*-----------------------------------
00224    * Information regarding function type layers
00225    */
00226   if (source->func != NULL)
00227   {
00228     new->func = (char *)calloc((strlen(source->func)+1),sizeof(char));
00229     strcpy(new->func, source->func);
00230   }
00231   else new->func = NULL;
00232   if (source->parm_names != NULL)
00233   {
00234     new->parm_names = (char *)calloc((source->number_of_parm+1),sizeof(char));
00235     strncpy(new->parm_names, source->parm_names, source->number_of_parm);
00236   }
00237   else new->parm_names = NULL;
00238   new->number_of_parm = source->number_of_parm;
00239   if (source->func_postfix != NULL)
00240   {
00241     new->func_postfix = initizlize_postfix_queue();
00242     pfqe = source->func_postfix->front;
00243     while (pfqe != NULL)
00244     {
00245       enqueue_postfix_queue(new->func_postfix, \
00246                             pfqe->oper, pfqe->numb, pfqe->indx, pfqe->type);
00247       pfqe = pfqe->next;
00248     }
00249   }
00250   /*-----------------------------------
00251    * All adjustable paramters
00252    */
00253   if (source->parm_values != NULL)
00254   {
00255     new->parm_values = (short *)calloc((source->number_of_parm+1), \
00256                                         sizeof(short));
00257     memcpy(new->parm_values, source->parm_values,\
00258           (source->number_of_parm*sizeof(short)));
00259   }
00260   else new->parm_values = NULL;
00261   new->thik = source->thik;
00262   new->sigz = source->sigz;
00263   new->cntr = source->cntr;
00264   new->rsld = source->rsld;
00265   new->isld = source->isld;
00266   new->rmag = source->rmag;
00267   /*-----------------------------------
00268    * Additional information flags
00269    */
00270   new->include_flag = source->include_flag;
00271   new->repeat_flag  = source->repeat_flag;
00272   new->polarized    = source->polarized;
00273   new->prev = NULL;
00274   new->next = NULL;
00275   
00276   return new;
00277 }
00278 /*============================================================================*/
00279 void linkLayerIntoModel(yanera_layer *layer, yanera_model *model)
00280 {
00281   /*
00282    * Any error checking to be done?
00283    */
00284     
00285   /* 
00286    * link-in the layer 
00287    */
00288   if (model->first_layer == NULL)
00289   {
00290     model->first_layer = layer;
00291     model->last_layer  = layer;
00292   }
00293   else
00294   {
00295     model->last_layer->next = layer;
00296     layer->prev             = model->last_layer;
00297     model->last_layer       = layer;
00298   }
00299   
00300   return;
00301 }
00302 /*============================================================================*/
00303 void  xmlErrorFunc(void *ctx, xmlErrorPtr error)
00304 {
00305   fprintf(stderr,"XML error      : %s", error->message);
00306   fprintf(stderr,"   In file     : %s\n", error->file);
00307   fprintf(stderr,"   At line     : %d\n", error->line);
00308   return;
00309 }
00310 

Generated on Thu May 29 10:56:33 2008 by  doxygen 1.5.5