1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
|
/* xml.c - convience functions for working with xml files
*
* Part of amide - Amide's a Medical Image Dataset Examiner
* Copyright (C) 2001-2017 Andy Loening
*
* Author: Andy Loening <loening@alum.mit.edu>
*/
/*
This program 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 2, or (at your option)
any later version.
This program 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "amide_config.h"
#include "xml.h"
#include <errno.h>
#include <string.h>
#include <locale.h>
#include "amitk_common.h"
#include "amide.h"
#define BOOLEAN_STRING_MAX_LENGTH 10 /* when we stop checking */
static char * true_string = "true";
static char * false_string = "false";
/* automagically converts the string to having the right radix for the current locale */
/* this crap needs to be used because on windows, setlocale isn't implemented on mingw */
void xml_convert_radix_to_local(gchar * conv_str) {
gboolean period=TRUE;
gdouble temp_float;
gchar * radix_ptr;
/* mingw doesn't really support setlocale, so use the following to figure out if we're using
a period or comma for the radix */
sscanf("1,9", "%lf", &temp_float);
if (temp_float > 1.1) period=FALSE;
if (period == FALSE) { /* using comma's */
while ((radix_ptr = strchr(conv_str, '.')) != NULL)
*radix_ptr = ',';
} else { /* using period */
while ((radix_ptr = strchr(conv_str, ',')) != NULL)
*radix_ptr = '.';
}
return;
}
/* returns FALSE if we'll have problems reading this file on a 32bit system */
gboolean xml_check_file_32bit_okay(guint64 value) {
#if !defined (G_PLATFORM_WIN32)
/* for some reason, 64bit calculations on windows returns garbage */
if (sizeof(long) < sizeof(guint64)) {
guint64 check = ((guint64) value) >> ((guint64) 31); /* long is signed, so 31 bits */
if (check > 0)
return FALSE;
}
#endif
// the following doesn't work on win32 either
// if (sizeof(long) < sizeof(guint64)) {
// guint32 * value32;
// value32 = (guint32 *) &value;
//
//#if (G_BYTE_ORDER == G_BIG_ENDIAN)
// if (value32[0] > 0)
// return FALSE;
//#else /* little endian */
// if (value32[1] > 0)
// return FALSE;
//#endif
// }
return TRUE;
}
/* utility functions */
gboolean xml_node_exists(xmlNodePtr nodes, const gchar * descriptor) {
if (xml_get_node(nodes, descriptor) != NULL)
return TRUE;
else
return FALSE;
}
/* ----------------- the load functions ------------------ */
/* go through a list of nodes, and return a pointer to the node
matching the descriptor */
xmlNodePtr xml_get_node(xmlNodePtr nodes, const gchar * descriptor) {
if (nodes == NULL)
return NULL;
else {
if (xmlStrcmp(nodes->name, (const xmlChar *) descriptor) == 0)
return nodes;
else
return xml_get_node(nodes->next, descriptor);
}
}
/* go through a list of nodes, and return the text which matches the descriptor */
gchar * xml_get_string(xmlNodePtr nodes, const gchar * descriptor) {
gchar * xml_str;
gchar * return_str;
xml_str = (gchar *) xmlNodeGetContent(xml_get_node(nodes, descriptor));
if (xml_str != NULL)
return_str = g_strdup_printf("%s", xml_str);
else
return_str = NULL;
free(xml_str);
return return_str;
}
amide_time_t xml_get_time(xmlNodePtr nodes, const gchar * descriptor, gchar ** perror_buf) {
gchar * temp_str;
amide_time_t return_time;
gint error=0;
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
xml_convert_radix_to_local(temp_str);
#if (SIZE_OF_AMIDE_TIME_T == 8)
/* convert to double */
error = sscanf(temp_str, "%lf", &return_time);
#elif (SIZE_OF_AMIDE_TIME_T == 4)
/* convert to float */
error = sscanf(temp_str, "%f", &return_time);
#else
#error "Unknown size for SIZE_OF_AMIDE_TIME_T"
#endif
g_free(temp_str);
}
if ((temp_str == NULL) || (error == EOF)) {
return_time = 0.0;
amitk_append_str_with_newline(perror_buf,_("Couldn't read time value for %s, substituting %5.3f"),
descriptor, return_time);
}
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return return_time;
}
amide_time_t * xml_get_times(xmlNodePtr nodes, const gchar * descriptor, guint num_times, gchar ** perror_buf) {
gchar * temp_str;
gchar ** string_chunks;
amide_time_t * return_times=NULL;
gint error;
guint i;
gchar * saved_locale;
gboolean corrupted=FALSE;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
temp_str = xml_get_string(nodes, descriptor);
if ((return_times = g_try_new(amide_time_t,num_times)) == NULL) {
amitk_append_str_with_newline(perror_buf, _("Couldn't allocate memory space for time data"));
return return_times;
}
if (temp_str != NULL) {
/* split-up the string so we can process it */
string_chunks = g_strsplit(temp_str, "\t", num_times);
g_free(temp_str);
for (i=0; (i<num_times) && (!corrupted);i++) {
if (string_chunks[i] == NULL)
corrupted = TRUE;
else {
xml_convert_radix_to_local(string_chunks[i]);
#if (SIZE_OF_AMIDE_TIME_T == 8)
/* convert to doubles */
error = sscanf(string_chunks[i], "%lf", &(return_times[i]));
#elif (SIZE_OF_AMIDE_TIME_T == 4)
/* convert to float */
error = sscanf(string_chunks[i], "%f", &(return_times[i]));
#else
#error "Unknown size for SIZE_OF_AMIDE_TIME_T"
#endif
if ((error == EOF)) corrupted = TRUE;
}
}
if (corrupted) {
for (i=0; i<num_times; i++) return_times[i]=1.0;
amitk_append_str_with_newline(perror_buf, _("XIF File appears corrupted, setting frame/gate time to 1"));
}
g_strfreev(string_chunks);
}
if (temp_str == NULL) {
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting 1"),descriptor);
for (i=0; i<num_times; i++) return_times[i]=1.0;
}
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return return_times;
}
amide_data_t xml_get_data(xmlNodePtr nodes, const gchar * descriptor, gchar **perror_buf) {
gchar * temp_str;
amide_data_t return_data;
gint error=0;
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
xml_convert_radix_to_local(temp_str);
#if (SIZE_OF_AMIDE_DATA_T == 8)
/* convert to doubles */
error = sscanf(temp_str, "%lf", &return_data);
#elif (SIZE_OF_AMIDE_DATA_T == 4)
/* convert to float */
error = sscanf(temp_str, "%f", &return_data);
#else
#error "Unknown size for SIZE_OF_AMIDE_DATA_T"
#endif
g_free(temp_str);
}
if ((temp_str == NULL) || (error == EOF)) {
return_data = 0.0;
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting %5.3f"),
descriptor, return_data);
}
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return return_data;
}
amide_data_t xml_get_data_with_default(xmlNodePtr nodes, const gchar * descriptor, amide_data_t default_data) {
if (xml_node_exists(nodes, descriptor))
return xml_get_data(nodes, descriptor, NULL);
else
return default_data;
}
amide_real_t xml_get_real(xmlNodePtr nodes, const gchar * descriptor, gchar **perror_buf) {
gchar * temp_str;
amide_real_t return_data;
gint error=0;
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
xml_convert_radix_to_local(temp_str);
#if (SIZE_OF_AMIDE_REAL_T == 8)
/* convert to doubles */
error = sscanf(temp_str, "%lf", &return_data);
#elif (SIZE_OF_AMIDE_REAL_T == 4)
/* convert to float */
error = sscanf(temp_str, "%f", &return_data);
#else
#error "Unkown size for SIZE_OF_AMIDE_REAL_T"
#endif
g_free(temp_str);
if ((error == EOF)) return_data = 0.0;
} else
return_data = 0.0;
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return return_data;
}
amide_real_t xml_get_real_with_default(xmlNodePtr nodes, const gchar * descriptor, amide_real_t default_real) {
if (xml_node_exists(nodes, descriptor))
return xml_get_data(nodes, descriptor, NULL);
else
return default_real;
}
gboolean xml_get_boolean(xmlNodePtr nodes, const gchar * descriptor, gchar **perror_buf) {
gchar * temp_str;
gboolean value;
temp_str = xml_get_string(nodes, descriptor);
if (temp_str == NULL) {
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting FALSE"),descriptor);
return FALSE;
}
if (g_ascii_strncasecmp(temp_str, true_string, BOOLEAN_STRING_MAX_LENGTH) == 0)
value = TRUE;
else
value = FALSE;
g_free(temp_str);
return value;
}
gboolean xml_get_boolean_with_default(xmlNodePtr nodes, const gchar * descriptor, gboolean default_boolean) {
if (xml_node_exists(nodes, descriptor))
return xml_get_boolean(nodes, descriptor, NULL);
else
return default_boolean;
}
gint xml_get_int(xmlNodePtr nodes, const gchar * descriptor, gchar **perror_buf) {
gchar * temp_str;
gint return_int;
gint error=0;
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
/* convert to an int */
error = sscanf(temp_str, "%d", &return_int);
g_free(temp_str);
}
if ((temp_str == NULL) || (error == EOF)) {
return_int = 0;
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting %d"),
descriptor, return_int);
}
return return_int;
}
gint xml_get_int_with_default(xmlNodePtr nodes, const gchar * descriptor, gint default_int) {
if (xml_node_exists(nodes, descriptor))
return xml_get_int(nodes, descriptor, NULL);
else
return default_int;
}
guint xml_get_uint(xmlNodePtr nodes, const gchar * descriptor, gchar **perror_buf) {
gchar * temp_str;
guint return_uint;
gint error=0;
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
/* convert to an int */
error = sscanf(temp_str, "%u", &return_uint);
g_free(temp_str);
}
if ((temp_str == NULL) || (error == EOF)) {
return_uint = 0;
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting %u"),
descriptor, return_uint);
}
return return_uint;
}
guint xml_get_uint_with_default(xmlNodePtr nodes, const gchar * descriptor, guint default_uint) {
if (xml_node_exists(nodes, descriptor))
return xml_get_uint(nodes, descriptor, NULL);
else
return default_uint;
}
void xml_get_location_and_size(xmlNodePtr nodes, const gchar * descriptor,
guint64 * location, guint64 * size, gchar **perror_buf) {
gchar * temp_str;
gint error=0;
temp_str = xml_get_string(nodes, descriptor);
if (temp_str != NULL) {
#if (SIZEOF_LONG == 8)
error = sscanf(temp_str, "0x%lx 0x%lx", location, size);
#else
#if (SIZEOF_LONG_LONG == 8)
error = sscanf(temp_str, "0x%llx 0x%llx", location, size);
#else
#error "Either LONG or LONG_LONG needs to by 8 bytes long"
#endif
#endif
g_free(temp_str);
}
if ((temp_str == NULL) || (error == EOF)) {
*location = 0x0;
*size = 0x0;
amitk_append_str_with_newline(perror_buf,_("Couldn't read value for %s, substituting 0x%llx 0x%llx"),
descriptor, *location, *size);
}
return;
}
/* ----------------- the save functions ------------------ */
void xml_save_string(xmlNodePtr node, const gchar * descriptor, const gchar * string) {
xmlNewChild(node, NULL, (xmlChar *) descriptor, (xmlChar *) string);
return;
}
void xml_save_time(xmlNodePtr node, const gchar * descriptor, const amide_time_t num) {
#ifdef OLD_WIN32_HACKS
gchar temp_str[128];
#else
gchar * temp_str;
#endif
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
#ifdef OLD_WIN32_HACKS
snprintf(temp_str, 128, "%10.9f", num);
#else
temp_str = g_strdup_printf("%10.9f", num);
#endif
xml_save_string(node, descriptor, temp_str);
#ifndef OLD_WIN32_HACKS
g_free(temp_str);
#endif
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return;
}
/* This is the old version fo xml_save_times. This was replaced with the version below because
the glib g_str functions didn't seem to respect the locale on win32
void xml_save_times(xmlNodePtr node, const gchar * descriptor, const amide_time_t * numbers, const int num) {
gchar * temp_str;
int i;
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
if (num == 0)
xml_save_string(node, descriptor, NULL);
else {
temp_str = g_strdup_printf("%10.9f", numbers[0]);
for (i=1; i < num; i++) {
temp_str = g_strdup_printf("%s\t%10.9f", temp_str, numbers[i]);
}
xml_save_string(node, descriptor, temp_str);
g_free(temp_str);
}
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return;
}
*/
void xml_save_times(xmlNodePtr node, const gchar * descriptor, const amide_time_t * numbers, const int num) {
gchar num_str[128];
gchar * temp_str2=NULL;
gchar * temp_str=NULL;
int i;
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
if (num == 0)
xml_save_string(node, descriptor, NULL);
else {
for (i=0; i < num; i++) {
snprintf(num_str, 128, "%10.9f", numbers[i]);
temp_str2=temp_str;
if (temp_str2 == NULL) {
temp_str=malloc(sizeof(char)*(strlen(num_str)+1));
strcpy(temp_str, num_str);
} else {
temp_str=malloc(sizeof(char)*(strlen(num_str)+strlen(temp_str2)+2));
strcpy(temp_str, temp_str2);
strcat(temp_str, "\t");
free(temp_str2);
strcat(temp_str, num_str);
}
}
xml_save_string(node, descriptor, temp_str);
g_free(temp_str);
}
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return;
}
void xml_save_data(xmlNodePtr node, const gchar * descriptor, const amide_data_t num) {
#ifdef OLD_WIN32_HACKS
gchar temp_str[128];
#else
gchar * temp_str;
#endif
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
#ifdef OLD_WIN32_HACKS
snprintf(temp_str, 128, "%10.9f", num);
#else
temp_str = g_strdup_printf("%10.9f", num);
#endif
xml_save_string(node, descriptor, temp_str);
#ifndef OLD_WIN32_HACKS
g_free(temp_str);
#endif
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return;
}
void xml_save_real(xmlNodePtr node, const gchar * descriptor, const amide_real_t num) {
#ifdef OLD_WIN32_HACKS
gchar temp_str[128];
#else
gchar * temp_str;
#endif
gchar * saved_locale;
saved_locale = g_strdup(setlocale(LC_NUMERIC,NULL));
setlocale(LC_NUMERIC,"POSIX");
#ifdef OLD_WIN32_HACKS
snprintf(temp_str, 128, "%10.9f", num);
#else
temp_str = g_strdup_printf("%10.9f", num);
#endif
xml_save_string(node, descriptor, temp_str);
#ifndef OLD_WIN32_HACKS
g_free(temp_str);
#endif
setlocale(LC_NUMERIC, saved_locale);
g_free(saved_locale);
return;
}
void xml_save_boolean(xmlNodePtr node, const gchar * descriptor, const gboolean value) {
if (value)
xml_save_string(node, descriptor, true_string);
else
xml_save_string(node, descriptor, false_string);
}
void xml_save_int(xmlNodePtr node, const gchar * descriptor, const gint num) {
gchar * temp_str;
temp_str = g_strdup_printf("%d", num);
xml_save_string(node, descriptor, temp_str);
g_free(temp_str);
return;
}
void xml_save_uint(xmlNodePtr node, const gchar * descriptor, const guint num) {
gchar * temp_str;
temp_str = g_strdup_printf("%u", num);
xml_save_string(node, descriptor, temp_str);
g_free(temp_str);
return;
}
void xml_save_location_and_size(xmlNodePtr node, const gchar * descriptor,
const guint64 location, const guint64 size) {
gchar * temp_str;
#if (SIZEOF_LONG == 8)
temp_str = g_strdup_printf("0x%lx 0x%lx", location, size);
#else
#if (SIZEOF_LONG_LONG == 8)
temp_str = g_strdup_printf("0x%llx 0x%llx", location, size);
#endif
#endif
xml_save_string(node, descriptor, temp_str);
g_free(temp_str);
return;
}
/* will return an xmlDocPtr if given either a xml filename,
or a prexisting FILE (along with location and size of the
xml data in the FILE) */
xmlDocPtr xml_open_doc(gchar * xml_filename, FILE * study_file,
guint64 location, guint64 size,
gchar ** perror_buf) {
xmlDocPtr doc;
gchar * xml_buffer;
long location_long;
size_t size_size;
size_t bytes_read;
if (study_file == NULL) { /* directory format */
if ((doc = xmlParseFile(xml_filename)) == NULL) {
amitk_append_str_with_newline(perror_buf,_("Couldn't Parse AMIDE xml file %s"),xml_filename);
return NULL;
}
} else { /* flat file format */
/* check for file size problems */
if (!xml_check_file_32bit_okay(location)) {
g_warning(_("File to large to read on 32bit platform."));
return NULL;
}
location_long = location;
if (!xml_check_file_32bit_okay(size)) {
g_warning(_("File to large to read on 32bit platform."));
return NULL;
}
size_size = size;
xml_buffer = g_try_new(gchar, size_size);
g_return_val_if_fail(xml_buffer != NULL, NULL);
if (fseek(study_file, location_long,SEEK_SET) != 0) {
amitk_append_str_with_newline(perror_buf, _("Could not seek to location %lx in file."), location_long);
return NULL;
}
bytes_read = fread(xml_buffer, sizeof(gchar), size_size, study_file);
if (bytes_read != size_size) {
amitk_append_str_with_newline(perror_buf, _("Only read %x bytes from file, expected %x"), bytes_read, size_size);
return NULL;
}
/* and actually process the xml */
doc = xmlParseMemory(xml_buffer, size_size);
g_free(xml_buffer);
}
return doc;
}
|