void element_print(scew_element const* element, FILE* out, unsigned int indent) { unsigned int closed = 0; XML_Char const* contents; scew_element* child = NULL; scew_attribute* attribute = NULL; if (element == NULL) { return; } indent_print(out, indent); scew_fprintf(out, _XT("<%s"), scew_element_name(element)); attribute = NULL; while ((attribute = scew_attribute_next(element, attribute)) != NULL) { attribute_print(attribute, out); } contents = scew_element_contents(element); if ((contents == NULL) && (element->child == NULL) && (element->parent != NULL)) { scew_fprintf(out, _XT("/>\n")); closed = 1; } else { scew_fprintf(out, _XT(">")); if (contents == NULL) { scew_fprintf(out, _XT("\n")); } } child = NULL; while ((child = scew_element_next(element, child)) != NULL) { element_print(child, out, indent + 1); } if (contents != NULL) { scew_fprintf(out, _XT("%s"), contents); } else if (!closed) { indent_print(out, indent); } if (!closed) { scew_fprintf(out, _XT("</%s>\n"), scew_element_name(element)); } }
/******************************************************************************* * Function: Element_Contents * * This function returns a char * representing a pointer to the contents of the * element specified in the input parameter, _element. * * Parameters: * _element: A void * representing a valid pointer to a scew_element * structure. This pointer must have been created previously by * some other function in this library. * * Returns: * char *: Representing a pointer to a NULL terminated character array, * which contains the contents of the element given in the input * parameter, _element. A NULL value is returned on error. * ******************************************************************************/ DLLIMPORT char *Element_Contents( void *_element ) { char *ret_val = NULL; if ( _element != NULL) { ret_val = (char *)scew_element_contents( (scew_element *)_element ); } return ret_val; }
static void print_element (scew_element *element, unsigned int indent) { XML_Char const *contents = NULL; scew_list *list = NULL; if (element == NULL) { return; } /* Prints the starting element tag with its attributes. */ print_indent (indent); scew_printf (_XT("<%s"), scew_element_name (element)); print_attributes (element); scew_printf (_XT(">")); contents = scew_element_contents (element); if (contents == NULL) { scew_printf (_XT("\n")); } /** * Call print_element function again for each child of the current * element. */ list = scew_element_children (element); while (list != NULL) { scew_element *child = scew_list_data (list); print_element (child, indent + 1); list = scew_list_next (list); } /* Prints element's content. */ if (contents != NULL) { scew_printf (_XT("%s"), contents); } else { print_indent (indent); } /* Prints the closing element tag. */ scew_printf (_XT("</%s>\n"), scew_element_name (element)); }
/** * Adds a set of parameters to a contact spread model. */ void set_params (struct naadsm_model_t_ *self, PAR_parameter_t * params) { local_data_t *local_data = (local_data_t *) (self->model_data); unsigned int nzones = ZON_zone_list_length (local_data->zones); unsigned int nprod_types = local_data->production_types->len; scew_element *e, **ee; gboolean success; gboolean has_destruction_cost_params = FALSE; gboolean has_vaccination_cost_params = FALSE; gboolean has_surveillance_cost_param = FALSE; destruction_cost_data_t destruction_cost_params = {}; vaccination_cost_data_t vaccination_cost_params = {}; double surveillance_cost_param = 0; gboolean *production_type = NULL; gboolean *zone = NULL; unsigned int i, j; unsigned int noutputs; RPT_reporting_t *output; const XML_Char *variable_name; #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "----- ENTER set_params (%s)", MODEL_NAME); #endif /* Make sure the right XML subtree was sent. */ g_assert (strcmp (scew_element_name (params), MODEL_NAME) == 0); #if DEBUG g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setting production types"); #endif /* Destruction Cost Parameters */ e = scew_element_by_name (params, "appraisal"); if (e != NULL) { destruction_cost_params.appraisal = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-unit appraisal cost to 0", MODEL_NAME); destruction_cost_params.appraisal = 0; } } else { g_warning ("%s: per-unit appraisal cost missing, setting to 0", MODEL_NAME); destruction_cost_params.appraisal = 0; } e = scew_element_by_name (params, "euthanasia"); if (e != NULL) { destruction_cost_params.euthanasia = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal euthanasia cost to 0", MODEL_NAME); destruction_cost_params.euthanasia = 0; } } else { g_warning ("%s: per-animal euthanasia cost missing, setting to 0", MODEL_NAME); destruction_cost_params.euthanasia = 0; } e = scew_element_by_name (params, "indemnification"); if (e != NULL) { destruction_cost_params.indemnification = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal indemnification cost to 0", MODEL_NAME); destruction_cost_params.indemnification = 0; } } else { g_warning ("%s: per-animal indemnification cost missing, setting to 0", MODEL_NAME); destruction_cost_params.indemnification = 0; } e = scew_element_by_name (params, "carcass-disposal"); if (e != NULL) { destruction_cost_params.carcass_disposal = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-animal carcass disposal cost to 0", MODEL_NAME); destruction_cost_params.carcass_disposal = 0; } } else { g_warning ("%s: per-animal carcass disposal cost missing, setting to 0", MODEL_NAME); destruction_cost_params.carcass_disposal = 0; } e = scew_element_by_name (params, "cleaning-disinfecting"); if (e != NULL) { destruction_cost_params.cleaning_disinfecting = PAR_get_money (e, &success); if (success) { has_destruction_cost_params = TRUE; } else { g_warning ("%s: setting per-unit cleaning and disinfecting cost to 0", MODEL_NAME); destruction_cost_params.cleaning_disinfecting = 0; } } else { g_warning ("%s: per-unit cleaning and disinfecting cost missing, setting to 0", MODEL_NAME); destruction_cost_params.cleaning_disinfecting = 0; } /* Vaccination Cost Parameters */ e = scew_element_by_name (params, "vaccination-fixed"); if (e != NULL) { vaccination_cost_params.vaccination_fixed = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting per-unit vaccination cost to 0", MODEL_NAME); vaccination_cost_params.vaccination_fixed = 0; } } else { g_warning ("%s: per-unit vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.vaccination_fixed = 0; } e = scew_element_by_name (params, "vaccination"); if (e != NULL) { vaccination_cost_params.vaccination = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting per-animal vaccination cost to 0", MODEL_NAME); vaccination_cost_params.vaccination = 0; } } else { g_warning ("%s: per-animal vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.vaccination = 0; } e = scew_element_by_name (params, "baseline-vaccination-capacity"); if (e != NULL) { vaccination_cost_params.baseline_capacity = (unsigned int) PAR_get_unitless (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting baseline vaccination capacity to 1,000,000", MODEL_NAME); vaccination_cost_params.baseline_capacity = 1000000; } } else { g_warning ("%s: baseline vaccination capacity missing, setting to 1,000,000", MODEL_NAME); vaccination_cost_params.baseline_capacity = 1000000; } e = scew_element_by_name (params, "additional-vaccination"); if (e != NULL) { vaccination_cost_params.extra_vaccination = PAR_get_money (e, &success); if (success) { has_vaccination_cost_params = TRUE; } else { g_warning ("%s: setting additional per-animal vaccination cost to 0", MODEL_NAME); vaccination_cost_params.extra_vaccination = 0; } } else { g_warning ("%s: additional per-animal vaccination cost missing, setting to 0", MODEL_NAME); vaccination_cost_params.extra_vaccination = 0; } /* No vaccinations have been performed yet. */ vaccination_cost_params.capacity_used = 0; /* Surveillance Cost Parameters */ e = scew_element_by_name (params, "surveillance"); if (e != NULL) { surveillance_cost_param = PAR_get_money (e, &success); if (success) { has_surveillance_cost_param = TRUE; } else { g_warning ("%s: setting per-animal zone surveillance cost to 0", MODEL_NAME); surveillance_cost_param = 0; } } else { g_warning ("%s: per-animal zone surveillance cost missing, setting to 0", MODEL_NAME); surveillance_cost_param = 0; } /* Set the reporting frequency for the output variables. */ ee = scew_element_list (params, "output", &noutputs); #if DEBUG g_debug ("%i output variables", noutputs); #endif for (i = 0; i < noutputs; i++) { e = ee[i]; variable_name = scew_element_contents (scew_element_by_name (e, "variable-name")); /* Do the outputs include a variable with this name? */ for (j = 0; j < self->outputs->len; j++) { output = (RPT_reporting_t *) g_ptr_array_index (self->outputs, j); if (strcmp (output->name, variable_name) == 0) break; } if (j == self->outputs->len) g_warning ("no output variable named \"%s\", ignoring", variable_name); else { RPT_reporting_set_frequency (output, RPT_string_to_frequency (scew_element_contents (scew_element_by_name (e, "frequency")))); #if DEBUG g_debug ("report \"%s\" %s", variable_name, RPT_frequency_name[output->frequency]); #endif } } free (ee); /* Read zone and production type attributes to determine which * entries should be filled in. */ if (scew_attribute_by_name (params, "zone") != NULL) zone = naadsm_read_zone_attribute (params, local_data->zones); else zone = NULL; production_type = naadsm_read_prodtype_attribute (params, "production-type", local_data->production_types); /* Copy the parameters into the selected zone and production types. */ for (i = 0; i < nprod_types; i++) { if (production_type[i]) { if (has_destruction_cost_params) { if (local_data->destruction_cost_params == NULL) { local_data->destruction_cost_params = g_new0 (destruction_cost_data_t *, nprod_types); g_assert (local_data->destruction_cost_params != NULL); } if (local_data->destruction_cost_params[i] == NULL) { local_data->destruction_cost_params[i] = g_new0 (destruction_cost_data_t, 1); g_assert (local_data->destruction_cost_params[i] != NULL); } memcpy (local_data->destruction_cost_params[i], &destruction_cost_params, sizeof (destruction_cost_data_t)); } if (has_vaccination_cost_params) { if (local_data->vaccination_cost_params == NULL) { local_data->vaccination_cost_params = g_new0 (vaccination_cost_data_t *, nprod_types); g_assert (local_data->vaccination_cost_params != NULL); } if (local_data->vaccination_cost_params[i] == NULL) { local_data->vaccination_cost_params[i] = g_new0 (vaccination_cost_data_t, 1); g_assert (local_data->vaccination_cost_params[i] != NULL); } memcpy (local_data->vaccination_cost_params[i], &vaccination_cost_params, sizeof (vaccination_cost_data_t)); } if (has_surveillance_cost_param) { if (zone) { for (j = 0; j < nzones; j++) { if (zone[j]) { if (local_data->surveillance_cost_param == NULL) { local_data->surveillance_cost_param = g_new0 (double *, nzones); g_assert (local_data->surveillance_cost_param != NULL); } if (local_data->surveillance_cost_param[j] == NULL) { local_data->surveillance_cost_param[j] = g_new0 (double, nprod_types); g_assert (local_data->surveillance_cost_param[j] != NULL); } local_data->surveillance_cost_param[j][i] = surveillance_cost_param; } } }
/** * @brief Reads the paths data from the XML data. * This function reads the paths data inside given element in XML data. * @param [in] parent Parent element for the paths data. * @return TRUE if pathdata was found from the file. */ BOOL ProjectFile::GetPathsData(scew_element * parent) { BOOL bFoundPaths = FALSE; scew_element *paths = NULL; if (parent != NULL) { paths = scew_element_by_name(parent, Paths_element_name); } if (paths != NULL) { bFoundPaths = TRUE; scew_element *left = NULL; scew_element *right = NULL; scew_element *filter = NULL; scew_element *subfolders = NULL; scew_element *left_ro = NULL; scew_element *right_ro = NULL; left = scew_element_by_name(paths, Left_element_name); right = scew_element_by_name(paths, Right_element_name); filter = scew_element_by_name(paths, Filter_element_name); subfolders = scew_element_by_name(paths, Subfolders_element_name); left_ro = scew_element_by_name(paths, Left_ro_element_name); right_ro = scew_element_by_name(paths, Right_ro_element_name); if (left) { LPCSTR path = NULL; path = scew_element_contents(left); m_leftFile = UTF82T(path).c_str(); m_bHasLeft = TRUE; } if (right) { LPCSTR path = NULL; path = scew_element_contents(right); m_rightFile = UTF82T(path).c_str(); m_bHasRight = TRUE; } if (filter) { LPCSTR filtername = NULL; filtername = scew_element_contents(filter); m_filter = UTF82T(filtername).c_str(); m_bHasFilter = TRUE; } if (subfolders) { LPCSTR folders = NULL; folders = scew_element_contents(subfolders); m_subfolders = atoi(folders); m_bHasSubfolders = TRUE; } if (left_ro) { LPCSTR readonly = NULL; readonly = scew_element_contents(left_ro); m_bLeftReadOnly = (atoi(readonly) != 0); } if (right_ro) { LPCSTR readonly = NULL; readonly = scew_element_contents(right_ro); m_bRightReadOnly = (atoi(readonly) != 0); } } return bFoundPaths; }
arcus_system *system_from_xml(scew_element *element) { arcus_system *sys; scew_attribute *attr; const char *attr_val; scew_list *list; scew_element *e; const char *contents; if (!element) { set_error_string("system_from_xml: NULL element"); return NULL; } if (strcmp(scew_element_name(element), "system") != 0) { set_error_string("system_from_xml: element name != 'system'"); return NULL; } sys = system_create(); if (!sys) return NULL; // Get system id attr = scew_element_attribute_by_name(element, "id"); if (!attr) { set_error_string("system_from_xml: system without id"); system_destroy(sys); return NULL; } attr_val = scew_attribute_value(attr); if (!attr_val) { set_error_string("system_from_xml: attribute without value (scew bug?)"); system_destroy(sys); return NULL; } sys->id = strdup(attr_val); // Load metric list = scew_element_list_by_name(element, "metric"); if (!list) { set_error_string("system_create: no metric in system"); system_destroy(sys); return NULL; } if (scew_list_size(list) != 1) { set_error_string("system_create: more than one metric in system"); system_destroy(sys); return NULL; } sys->metric = metric_from_xml((scew_element *)scew_list_data(list)); if (!sys->metric) { system_destroy(sys); return NULL; } // Load christoffel symbols sys->cs = christoffel_from_xml(element); if (!sys->cs) { system_destroy(sys); return NULL; } // Load range list = scew_element_list_by_name(element, "range"); if (!list) { set_error_string("system_create: no range in system"); system_destroy(sys); return NULL; } if (scew_list_size(list) != 1) { set_error_string("system_create: more than one range in system"); system_destroy(sys); return NULL; } e = (scew_element *)scew_list_data(list); if (!e || strcmp(scew_element_name(e), "range") != 0) { set_error_string("system_create: no/wrong range element in list (SCEW bug?)"); system_destroy(sys); return NULL; } contents = scew_element_contents(e); if (!contents || !contents[0]) { set_error_string("system_create: no contents in range element"); system_destroy(sys); return NULL; } if (!af_formula_setexpr(sys->range, contents)) { set_error_string("system_create: cannot set range function expression"); system_destroy(sys); return NULL; } return sys; }