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 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
|
/* Copyright (C) 2004 MySQL AB
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 of the License, 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 */
/**
* @file myx_grt.c
* @brief GRT environment functions
*
* See also: <a href="../grt.html#GRT">GRT</a>
*/
#include <glib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "myx_xml_aux_functions.h"
#include "myx_grt_private.h"
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#include <objbase.h>
#else
// Linux specific
#include <uuid/uuid.h>
#endif
static MYX_GRT_VALUE *unserialize_from_xml(xmlNodePtr node, GHashTable *objects_by_id);
static xmlNodePtr serialize_to_xml(xmlNodePtr parent, MYX_GRT_VALUE *value, GHashTable *saved_ids);
static void default_print(const char *msg, void *data)
{
printf("%s", msg);
}
static void default_log(MYX_GRT *grt, int code, const char *msg, const char *detail)
{
printf("%i: %s: %s\n", code, msg, detail?detail:"");
}
/**
****************************************************************************
* @brief Creates a new GRT environment.
*
* Also:
* - sets a default print callback that's equivalent to printf() and
* registers the built-in module loader type.
* - creates a server socket so that modules can connect to it for
* communicating with the grt (passing progress info, for example)
* After creating the environment, you will want to call individual
* initializers for each module loader you want to support.
*
* @return A newly created GRT.
****************************************************************************/
MYX_GRT *myx_grt_initialize()
{
MYX_GRT *grt= g_new0(MYX_GRT, 1);
grt->print= default_print;
grt->print_data= NULL;
grt->logfunc= default_log;
grt->root= myx_grt_dict_new(NULL);
grt->struct_icon_cache= g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
grt->reference_cache= g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
myx_grt_register_module_loader(grt, myx_builtin_init_loader(grt));
#ifdef remove_this_when_this_gets_used
myx_grt_setup_messaging(grt);
#endif
return grt;
}
/**
****************************************************************************
* @brief Shutdown a GRT environment and frees all used resources
*
* Will close and free all module loaders, modules and other resources
* allocated for it.
*
* @param grt the GRT environment
*
* NOTE
* This is not freeing anything atm.
****************************************************************************/
void myx_grt_finalize(MYX_GRT *grt)
{
//XXX TODO
}
/**
****************************************************************************
* @brief Sets the function to be used to print values
*
* Sets the function that is used when GRT needs to print messages.
*
* @param grt the GRT environment
* @param user_data a pointer to be passed to the callback function.
* @param process_output_func the callback function which should take
* the text and the userdata pointer as arguments
****************************************************************************/
void myx_grt_set_output_callback(MYX_GRT *grt, void *user_data, MYX_GRT_PRINT_CALLBACK process_output_func)
{
grt->print= process_output_func;
grt->print_data= user_data;
}
/**
****************************************************************************
* @brief Sets callbacks for module logging
*
* This will set the callbacks that will be called when a module calls
* a logging function, for errors or messages in general.
*
* @param grt the GRT environment
* @param log_func the function that will handle logging calls from modules
****************************************************************************/
void myx_grt_module_set_log_callback(MYX_GRT *grt, MYX_GRT_LOG_CALLBACK log_func)
{
grt->logfunc= log_func;
}
/**
****************************************************************************
* @brief Registers a module loader with the GRT
*
* Will register an already initialized module loader with the GRT.
* You can only register only one module loader of each type.
* After registration, you can scan for modules with myx_grt_scan_for_modules
* or load them individually with myx_grt_module_init
*
* @param grt the GRT environment where the loader should be registered
* @param loader an initialized loader object.
*
* @return MYX_GRT_NO_ERROR, MYX_GRT_INTERNAL_ERROR
****************************************************************************/
MYX_GRT_ERROR myx_grt_register_module_loader(MYX_GRT *grt, MYX_GRT_MODULE_LOADER *loader)
{
g_return_val_if_fail(grt != NULL, MYX_GRT_INTERNAL_ERROR);
g_return_val_if_fail(loader != NULL, MYX_GRT_INTERNAL_ERROR);
grt->loaders_num++;
grt->loaders= g_realloc(grt->loaders, sizeof(MYX_GRT_MODULE_LOADER*)*grt->loaders_num);
grt->loaders[grt->loaders_num-1]= loader;
return MYX_GRT_NO_ERROR;
}
/**
****************************************************************************
* @brief Returns a copied list of the structs registered in the GRT
*
* @param grt the GRT
*
* @return A copy of the registered structs
****************************************************************************/
MYX_GRT_STRUCTS * myx_grt_structs_get(MYX_GRT *grt)
{
MYX_GRT_STRUCTS *structs= g_malloc(sizeof(MYX_GRT_STRUCTS));
g_return_val_if_fail(grt != NULL, NULL);
structs->structs_num= grt->structs_num;
structs->structs= g_memdup(grt->structs, sizeof(MYX_GRT_STRUCT)*structs->structs_num);
return structs;
}
MYX_STRINGLIST * myx_grt_struct_packages(MYX_GRT *grt)
{
unsigned int i;
g_return_val_if_fail(grt != NULL, NULL);
{
MYX_STRINGLIST *packages= g_new0(MYX_STRINGLIST, 1);
for (i= 0; i<grt->structs_num; i++)
{
MYX_GRT_STRUCT *str= grt->structs + i;
char *str_pak= g_strdup(str->name);
unsigned int j, l= (unsigned int)strlen(str_pak);
for (j= l-1; j>0; j--)
{
if (str_pak[j] == '.')
break;
}
str_pak[j]= 0;
//See if the package name is already stored
for (j= 0; j<packages->strings_num; j++)
{
if (strcmp3(str_pak, packages->strings[j]) == 0)
break;
}
//If not, add the package name
if (j == packages->strings_num)
{
packages->strings_num++;
packages->strings= g_realloc(packages->strings, sizeof(char *)*packages->strings_num);
packages->strings[packages->strings_num-1]= g_strdup(str_pak);
}
g_free(str_pak);
}
return packages;
}
}
int myx_grt_package_count(MYX_GRT *grt)
{
unsigned int c;
MYX_STRINGLIST *packages= myx_grt_struct_packages(grt);
c= packages->strings_num;
myx_free_stringlist(packages);
return c;
}
char * myx_grt_package_by_index(MYX_GRT *grt, unsigned int index)
{
char *s;
MYX_STRINGLIST *packages= myx_grt_struct_packages(grt);
s= g_strdup(packages->strings[index]);
myx_free_stringlist(packages);
return s;
}
/**
****************************************************************************
* @brief Returns the number of structs registered in the GRT
*
* @param grt the GRT
*
* @return The number of structs
****************************************************************************/
int myx_grt_struct_get_count(MYX_GRT *grt)
{
g_return_val_if_fail(grt != NULL, -1);
return grt->structs_num;
}
/**
****************************************************************************
* @brief Returns the number of structs registered in the GRT that have
* the struct with the struct_name as their parent
*
* @param grt the GRT
* @param struct_name The name of the parent struct
*
* @return The number of structs
****************************************************************************/
int myx_grt_struct_get_child_count(MYX_GRT *grt, const char *struct_name)
{
unsigned int i, c= 0;
g_return_val_if_fail(grt != NULL, -1);
for (i= 0; i<grt->structs_num; i++)
{
MYX_GRT_STRUCT *str= grt->structs + i;
if (strcmp3(str->parent_struct_name, struct_name) == 0)
{
c++;
}
}
return c;
}
int myx_grt_package_struct_count(MYX_GRT *grt, const char *package_name)
{
unsigned int i, c= 0, l= (unsigned int)strlen(package_name);
g_return_val_if_fail(grt != NULL, 0);
g_return_val_if_fail(package_name != NULL, 0);
for (i= 0; i<grt->structs_num; i++)
{
MYX_GRT_STRUCT *str= grt->structs + i;
if (package_name[0])
{
char *begin_str= g_strdup_printf("%s.", package_name);
if (str_beginswith(str->name, begin_str) &&
(sub_str_count(".", str->name+l+1) == 0))
c++;
g_free(begin_str);
}
else
{
if (sub_str_count(".", str->name) == 0)
c++;
}
}
return c;
}
/**
****************************************************************************
* @brief Returns the GRT struct given by an index
*
* @param grt the GRT
* @param index the index of the requested struct
*
* @return The struct with the given index
****************************************************************************/
MYX_GRT_STRUCT * myx_grt_struct_get_by_index(MYX_GRT *grt, unsigned int index)
{
g_return_val_if_fail(grt != NULL, NULL);
g_return_val_if_fail(index < grt->structs_num, NULL);
return grt->structs + index;
}
/**
****************************************************************************
* @brief Returns the child GRT struct of the struct defined by struct_name
* with the given by an
*
* @param grt the GRT
* @param struct_name the name of the parent strcut
* @param index the index of the requested struct
*
* @return The struct with the given index or null if not found
****************************************************************************/
MYX_GRT_STRUCT * myx_grt_struct_get_child_by_index(MYX_GRT *grt, const char *struct_name, unsigned int index)
{
unsigned int i, c= 0;
g_return_val_if_fail(grt != NULL, NULL);
for (i= 0; i<grt->structs_num; i++)
{
MYX_GRT_STRUCT *str= grt->structs + i;
if (strcmp3(str->parent_struct_name, struct_name) == 0)
{
if (c == index)
return str;
c++;
}
}
return NULL;
}
MYX_GRT_STRUCT * myx_grt_package_struct_by_index(MYX_GRT *grt, const char *package_name, unsigned int index)
{
unsigned int i, c= 0, l= (unsigned int)strlen(package_name);
g_return_val_if_fail(grt != NULL, NULL);
for (i= 0; i<grt->structs_num; i++)
{
MYX_GRT_STRUCT *str= grt->structs + i;
if (package_name[0])
{
char *begin_str= g_strdup_printf("%s.", package_name);
if (str_beginswith(str->name, begin_str) &&
(sub_str_count(".", str->name+l+1) == 0))
{
if (c == index)
{
g_free(begin_str);
return str;
}
c++;
}
g_free(begin_str);
}
else
{
if (sub_str_count(".", str->name) == 0)
{
if (c == index)
return str;
c++;
}
}
}
return NULL;
}
/**
****************************************************************************
* @brief Fetches the root object of the GRT
*
* @param grt the GRT
*
* @return The root GRT value.
****************************************************************************/
MYX_GRT_VALUE *myx_grt_get_root(MYX_GRT *grt)
{
g_return_val_if_fail(grt != NULL, NULL);
return grt->root;
}
/**
****************************************************************************
* @brief "Loads" a value into the GRT and makes it the root object/value
*
* This will replace the root object of the GRT with the passed one.
* The old object will be completely freed.
*
* The GRT will take over ownership of the whole object passed. That means
* you should not free it.
*
* @param grt the GRT environment
* @param new_root a GRT object/value that will become the root object. It has to
* be a MYX_DICT_VALUE
*
* @return MYX_GRT_NO_ERROR if success
****************************************************************************/
MYX_GRT_ERROR myx_grt_set_root(MYX_GRT *grt, MYX_GRT_VALUE *new_root)
{
g_return_val_if_fail(grt != NULL, MYX_GRT_INTERNAL_ERROR);
g_return_val_if_fail(new_root != NULL, MYX_GRT_INTERNAL_ERROR);
if (myx_grt_value_get_type(new_root) != MYX_DICT_VALUE)
return MYX_GRT_BAD_VALUE;
myx_grt_reference_cache_clear(grt);
myx_grt_value_release(grt->root);
grt->root= new_root;
myx_grt_value_retain(new_root);
myx_grt_reference_cache_rescan(grt);
return MYX_GRT_NO_ERROR;
}
/**
****************************************************************************
* @brief Loads a previously stored GRT value from a file
*
* Loads a GRT value from a GRT XML file. Use myx_grt_store_to_file to
* create files in that format.
* Read myx_grt_store_to_file for more details.
*
* @param filename the name of the file to load. The file must be in the GRT XML format.
* Usually something stored by myx_grt_store_to_file
*
* @return NULL on error and the value if the file could be correctly loaded.
****************************************************************************/
MYX_GRT_VALUE *myx_grt_retrieve_from_file(const char *filename)
{
xmlDocPtr doc;
xmlNodePtr root;
MYX_GRT_VALUE *value;
if (!(doc= myx_xmlParseFile(filename)))
return NULL;
root= xmlDocGetRootElement(doc);
if (root)
{
root= root->children;
while (root && xmlStrcmp(root->name, "value")!=0) root= root->next;
if (root)
{
GHashTable *objects_by_id= g_hash_table_new(g_str_hash, g_str_equal);
value= unserialize_from_xml(root, objects_by_id);
g_hash_table_destroy(objects_by_id);
}
}
xmlFreeDoc(doc);
return value;
}
/**
****************************************************************************
* @brief Stores a GRT value to a file
*
* This will serialize the value to XML and store it in a file that can
* later be retrieved with myx_grt_retrieve_from_file
*
* @param value the GRT value to store
* @param filename name of file to store data
*
* @return MYX_GRT_NO_ERROR if there were no errors.
****************************************************************************/
MYX_GRT_ERROR myx_grt_store_to_file(MYX_GRT_VALUE *value, const char *filename)
{
xmlDocPtr doc;
int res;
GHashTable *saved_ids;
doc= xmlNewDoc("1.0");
doc->children= xmlNewDocRawNode(doc, NULL, "data", NULL);
saved_ids= g_hash_table_new(g_str_hash, g_str_equal);
serialize_to_xml(doc->children, value, saved_ids);
g_hash_table_destroy(saved_ids);
res= myx_xmlSaveFile(filename, doc);
xmlFreeDoc(doc);
return res == -1 ? MYX_GRT_CANT_OPEN_FILE : MYX_GRT_NO_ERROR;
}
/*static char *value_type_strings[]=
{
"all",
"int",
"bigint",
"real",
"string",
"list",
"dict"
};*/
/**
****************************************************************************
* @brief Prints a string using the print callback from GRT
*
* Prints a formated message using the callback function set up in GRT.
*
* @param grt the GRT environment. A print callback must have been previously set.
* @param fmt format string, accepts anything that printf() does.
* @param ... arguments for the formatted message
*****************************************************************************/
int myx_grt_printf(MYX_GRT *grt, const char *fmt, ...)
{
char *tmp;
va_list args;
va_start(args, fmt);
tmp= g_strdup_vprintf(fmt, args);
va_end(args);
MYX_PRINT(grt, tmp);
g_free(tmp);
return 0;
}
static xmlNodePtr serialize_to_xml(xmlNodePtr parent, MYX_GRT_VALUE *value,
GHashTable *saved_ids)
{
unsigned int i;
char buffer[100];
const char *id;
xmlNodePtr node;
switch (value->type)
{
case MYX_INT_VALUE:
g_snprintf(buffer, sizeof(buffer), "%i", value->value.i);
node= xmlNewTextChild(parent, NULL, "value", buffer);
xmlNewProp(node, "type", "int");
break;
case MYX_REAL_VALUE:
g_snprintf(buffer, sizeof(buffer), "%f", value->value.r);
node= xmlNewTextChild(parent, NULL, "value", buffer);
xmlNewProp(node, "type", "real");
break;
case MYX_STRING_VALUE:
node= xmlNewTextChild(parent, NULL, "value", value->value.s);
xmlNewProp(node, "type", "string");
break;
case MYX_LIST_VALUE:
node= xmlNewTextChild(parent, NULL, "value", NULL);
xmlNewProp(node, "type", "list");
xmlNewProp(node, "content-type", myx_get_value_type_as_string(value->value.l->content_type));
if (value->value.l->content_struct_name)
xmlNewProp(node, "content-struct-name", value->value.l->content_struct_name);
for (i= 0; i < value->value.l->items_num; i++)
{
g_return_val_if_fail((value->value.l->items[i]->type == value->value.l->content_type) ||
(value->value.l->content_type == MYX_ANY_VALUE), NULL);
serialize_to_xml(node, value->value.l->items[i], saved_ids);
}
break;
case MYX_DICT_VALUE:
node= xmlNewTextChild(parent, NULL, "value", NULL);
xmlNewProp(node, "type", "dict");
if ((value->value.d->struct_name) && (value->value.d->struct_name[0]))
xmlNewProp(node, "struct-name", value->value.d->struct_name);
if (value->value.d->content_type != MYX_ANY_VALUE)
xmlNewProp(node, "content-type", myx_get_value_type_as_string(value->value.d->content_type));
if ((value->value.d->content_struct_name) && (value->value.d->content_struct_name[0]))
xmlNewProp(node, "content-struct-name", value->value.d->content_struct_name);
if ((id= myx_grt_dict_id_item_as_string(value)))
{
if (g_hash_table_lookup(saved_ids, id))
{
// the object is already in the XML tree, just leave a link to it
xmlNewProp(node, "link", id);
return node;
}
else
// object is not yet in XML tree. put it in and record the ID
g_hash_table_insert(saved_ids, (gpointer)id, "X");
}
for (i= 0; i < value->value.d->items_num; i++)
{
xmlNodePtr child;
child= serialize_to_xml(node, value->value.d->items[i].value, saved_ids);
xmlNewProp(child, "key", value->value.d->items[i].key);
}
break;
case MYX_ANY_VALUE:
break;
}
return node;
}
static MYX_GRT_VALUE *unserialize_from_xml(xmlNodePtr node, GHashTable *objects_by_id)
{
xmlChar *str;
xmlChar *node_type= xmlGetProp(node, "type");
MYX_GRT_VALUE *value= NULL;
if (!node_type)
{
g_warning("Node '%s' in xml doesn't have a type property", node->name);
return NULL;
}
if (strcmp(node_type, "int")==0)
{
str= xmlNodeGetContent(node);
value= myx_grt_value_from_int(strtol(str, NULL, 0));
xmlFree(str);
}
else if (strcmp(node_type, "real")==0)
{
str= xmlNodeGetContent(node);
value= myx_grt_value_from_real(strtod(str, NULL));
xmlFree(str);
}
else if (strcmp(node_type, "string")==0)
{
str= xmlNodeGetContent(node);
value= myx_grt_value_from_string(str);
xmlFree(str);
}
else if (strcmp(node_type, "dict")==0)
{
xmlChar *link_id= xmlGetProp(node, "link");
xmlNodePtr child;
if (link_id)
{
MYX_GRT_VALUE *linked_object;
linked_object= g_hash_table_lookup(objects_by_id, link_id);
if (!linked_object)
{
g_warning("linked object %s was not yet parsed at the time it was found",
link_id);
}
else
{
myx_grt_value_retain(linked_object);
}
value= linked_object;
xmlFree(link_id);
}
else
{
xmlChar *prop= xmlGetProp(node, "content-type");
MYX_GRT_VALUE_TYPE content_type= MYX_ANY_VALUE;
MYX_GRT_ERROR error;
content_type= myx_get_value_type_from_string(prop, &error);
xmlFree(prop);
if (content_type != MYX_ANY_VALUE)
{
char *content_struct_name= xmlGetProp(node, "content-struct-name");
value= myx_grt_dict_new_typed(content_type, content_struct_name);
xmlFree(content_struct_name);
}
else
{
xmlChar *struct_name= xmlGetProp(node, "struct-name");
value= myx_grt_dict_new(struct_name);
xmlFree(struct_name);
}
child= node->children;
while (child)
{
MYX_GRT_VALUE *sub_value;
if (strcmp(child->name, "value")==0)
{
xmlChar *key= xmlGetProp(child, "key");
sub_value= unserialize_from_xml(child, objects_by_id);
if (sub_value)
{
myx_grt_dict_item_set_value(value, key, sub_value);
myx_grt_value_release(sub_value);
}
else
{
myx_grt_value_release(value);
value= NULL;
break;
}
if (xmlStrcmp(key, "_id")==0 && sub_value)
{
if (sub_value->type == MYX_STRING_VALUE)
g_hash_table_insert(objects_by_id, (gpointer)myx_grt_value_as_string(sub_value),
value);
}
xmlFree(key);
}
child= child->next;
}
}
}
else if (strcmp(node_type, "list")==0)
{
xmlChar *ctype= xmlGetProp(node, "content-type");
xmlChar *cstruct_name= xmlGetProp(node, "content-struct-name");
xmlNodePtr child;
MYX_GRT_ERROR error;
MYX_GRT_VALUE_TYPE content_type= myx_get_value_type_from_string(ctype, &error);
/*for (i= 0; sizeof(value_type_strings)/sizeof(char*); i++)
if (strcmp2(value_type_strings[i], ctype)==0)
{
value= myx_grt_list_new(i, NULL);
break;
}*/
if (error == MYX_GRT_NO_ERROR)
{
value= myx_grt_list_new(content_type, NULL);
value->value.l->content_struct_name= g_strdup(cstruct_name);
child= node->children;
while (child)
{
MYX_GRT_VALUE *sub_value;
if (child->type == XML_ELEMENT_NODE && strcmp(child->name, "value")==0)
{
sub_value= unserialize_from_xml(child, objects_by_id);
if (sub_value)
{
myx_grt_list_item_add(value, sub_value);
myx_grt_value_release(sub_value);
}
else
{
myx_grt_value_release(value);
value= NULL;
break;
}
}
child= child->next;
}
xmlFree(ctype);
xmlFree(cstruct_name);
}
}
xmlFree(node_type);
return value;
}
static xmlNodePtr serialize_to_xml_global_object(const char *objectPath, xmlNodePtr parent, MYX_GRT_VALUE *value,
GHashTable *saved_ids)
{
//unsigned int i;
char buffer[100];
//const char *id;
xmlNodePtr node;
switch (value->type)
{
case MYX_INT_VALUE:
g_snprintf(buffer, sizeof(buffer), "%i", value->value.i);
node= xmlNewTextChild(parent, NULL, "value", buffer);
xmlNewProp(node, "type", "int");
xmlNewProp(node, "globalObjectPath", objectPath);
break;
case MYX_REAL_VALUE:
g_snprintf(buffer, sizeof(buffer), "%f", value->value.r);
node= xmlNewTextChild(parent, NULL, "value", buffer);
xmlNewProp(node, "type", "real");
xmlNewProp(node, "global-object-path", objectPath);
break;
case MYX_STRING_VALUE:
node= xmlNewTextChild(parent, NULL, "value", value->value.s);
xmlNewProp(node, "type", "string");
xmlNewProp(node, "global-object-path", objectPath);
break;
case MYX_LIST_VALUE:
node= xmlNewTextChild(parent, NULL, "value", NULL);
xmlNewProp(node, "type", "list");
xmlNewProp(node, "content-type", myx_get_value_type_as_string(value->value.l->content_type));
if (value->value.l->content_struct_name)
xmlNewProp(node, "content-struct-name", value->value.l->content_struct_name);
/*for (i= 0; i < value->value.l->items_num; i++)
{
g_return_val_if_fail((value->value.l->items[i]->type == value->value.l->content_type) ||
(value->value.l->content_type == MYX_ANY_VALUE), NULL);
serialize_to_xml(node, value->value.l->items[i], saved_ids);
}*/
xmlNewProp(node, "global-object-path", objectPath);
break;
case MYX_DICT_VALUE:
node= xmlNewTextChild(parent, NULL, "value", NULL);
xmlNewProp(node, "type", "dict");
if ((value->value.d->struct_name) && (value->value.d->struct_name[0]))
xmlNewProp(node, "struct-name", value->value.d->struct_name);
if (value->value.d->content_type != MYX_ANY_VALUE)
xmlNewProp(node, "content-type", myx_get_value_type_as_string(value->value.d->content_type));
if ((value->value.d->content_struct_name) && (value->value.d->content_struct_name[0]))
xmlNewProp(node, "content-struct-name", value->value.d->content_struct_name);
/*if ((id= myx_grt_dict_id_item_as_string(value)))
{
if (g_hash_table_lookup(saved_ids, id))
{
// the object is already in the XML tree, just leave a link to it
xmlNewProp(node, "link", id);
return node;
}
else
// object is not yet in XML tree. put it in and record the ID
g_hash_table_insert(saved_ids, (gpointer)id, "X");
}
for (i= 0; i < value->value.d->items_num; i++)
{
xmlNodePtr child;
child= serialize_to_xml(node, value->value.d->items[i].value, saved_ids);
xmlNewProp(child, "key", value->value.d->items[i].key);
}*/
xmlNewProp(node, "global-object-path", objectPath);
break;
case MYX_ANY_VALUE:
break;
}
return node;
}
static MYX_GRT_VALUE *unserialize_from_xml_global_object(MYX_GRT *grt, xmlNodePtr node, GHashTable *objects_by_id)
{
xmlChar *node_type= xmlGetProp(node, "type");
MYX_GRT_VALUE *value= NULL;
if (!node_type)
{
g_warning("Node '%s' in xml doesn't have a type property", node->name);
return NULL;
}
// check if we have a global object
if ((strcmp(node_type, "dict")==0) || (strcmp(node_type, "list")==0))
{
xmlChar *global_object_path= xmlGetProp(node, "global-object-path");
if (global_object_path)
{
value= myx_grt_value_get_by_path(myx_grt_get_root(grt), global_object_path);
xmlFree(global_object_path);
return value;
}
}
return unserialize_from_xml(node, objects_by_id);
}
/**
****************************************************************************
* @brief Convert a GRT value into a XML string
*
* Produces a XML representation of the GRT value.
*
* @param value a GRT value
*
* @return String containing the value as a XML or NULL if there's an error.
*
* @see myx_grt_value_from_xml
*****************************************************************************/
char *myx_grt_value_to_xml(MYX_GRT_VALUE *value)
{
xmlDocPtr doc;
xmlNodePtr root;
xmlChar *buffer= NULL;
GHashTable *saved_ids;
int size;
if (value)
{
doc= xmlNewDoc("1.0");
doc->children= root= xmlNewDocRawNode(doc, NULL, "data", NULL);
saved_ids= g_hash_table_new(g_str_hash, g_str_equal);
serialize_to_xml(root, value, saved_ids);
g_hash_table_destroy(saved_ids);
xmlDocDumpFormatMemory(doc, &buffer, &size, 1);
xmlFreeDoc(doc);
return (char*)buffer;
}
else
return NULL;
}
/**
****************************************************************************
* @brief Parse a XML representation of a GRT value.
*
* Parses a XML string and rebuilds the GRT value that corresponds to it.
*
* @param str the string in XML format containing a serialized GRT value
* @param size length of the string
*
* @return The value corresponding to the passed in string or NULL if there's
* an error.
*
* @see myx_grt_value_to_xml
*****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_from_xml(const char *str, size_t size)
{
xmlDocPtr doc= xmlParseMemory(str, (int)size);
xmlNodePtr root;
MYX_GRT_VALUE *value;
if (!doc)
{
g_warning("Could not parse XML data");
return NULL;
}
root= xmlDocGetRootElement(doc);
//check if we have a <data> rootnode
if (root && (xmlStrcmp(root->name, "data")==0))
root= root->children;
//skip all nodes that are no <value> nodes
while (root && xmlStrcmp(root->name, "value")!=0)
root= root->next;
if (root)
{
GHashTable *objects_by_id= g_hash_table_new(g_str_hash, g_str_equal);
value= unserialize_from_xml(root, objects_by_id);
g_hash_table_destroy(objects_by_id);
}
else
value= NULL;
xmlFreeDoc(doc);
return value;
}
/**
****************************************************************************
* @brief Parse a XML representation of a GRT value that might be a global object.
*
* Parses a XML string and rebuilds the GRT value that corresponds to it.
*
* @param grt the GRT environment
* @param str the string in XML format containing a serialized GRT value
* @param size length of the string
*
* @return The value corresponding to the passed in string or NULL if there's
* an error.
*
* @see myx_grt_value_to_xml
*****************************************************************************/
MYX_GRT_VALUE *myx_grt_value_from_xml_global_object(MYX_GRT *grt, const char *str, size_t size)
{
xmlDocPtr doc= xmlParseMemory(str, (int)size);
xmlNodePtr root;
MYX_GRT_VALUE *value;
if (!doc)
{
g_warning("Could not parse XML data");
return NULL;
}
root= xmlDocGetRootElement(doc);
//check if we have a <data> rootnode
if (root && (xmlStrcmp(root->name, "data")==0))
root= root->children;
//skip all nodes that are no <value> nodes
while (root && xmlStrcmp(root->name, "value")!=0)
root= root->next;
if (root)
{
GHashTable *objects_by_id= g_hash_table_new(g_str_hash, g_str_equal);
value= unserialize_from_xml_global_object(grt, root, objects_by_id);
g_hash_table_destroy(objects_by_id);
}
else
value= NULL;
xmlFreeDoc(doc);
return value;
}
/**
****************************************************************************
* @brief Convert a GRT value into a XML global object string
*
* Produces a XML representation of the GRT value as a global object
*
* @param value a GRT value
*
* @return String containing the value as a XML or NULL if there's an error.
*
* @see myx_grt_value_to_xml
*****************************************************************************/
char *myx_grt_value_to_xml_global_object(const char *objectPath, MYX_GRT_VALUE *value)
{
xmlDocPtr doc;
xmlNodePtr root;
xmlChar *buffer= NULL;
GHashTable *saved_ids;
int size;
if (value)
{
doc= xmlNewDoc("1.0");
doc->children= root= xmlNewDocRawNode(doc, NULL, "data", NULL);
saved_ids= g_hash_table_new(g_str_hash, g_str_equal);
serialize_to_xml_global_object(objectPath, root, value, saved_ids);
g_hash_table_destroy(saved_ids);
xmlDocDumpFormatMemory(doc, &buffer, &size, 1);
xmlFreeDoc(doc);
return (char*)buffer;
}
else
return NULL;
}
/**
****************************************************************************
* @brief Add a message to the GRT message queue.
*
* @param grt
* @param msg_type
* @param message message string
* @param details optional string list for details of the message
* @param copy_details if 1, the function will make a copy of the details
* parameter
*
*****************************************************************************/
void myx_grt_add_msg(MYX_GRT *grt, int msg_type, const char *message, MYX_STRINGLIST *details,
int copy_details)
{
MYX_GRT_MSG *msg;
if (!grt->msgs)
grt->msgs= g_new0(MYX_GRT_MSGS, 1);
grt->msgs->msgs_num++;
grt->msgs->msgs= g_realloc(grt->msgs->msgs,
grt->msgs->msgs_num*sizeof(MYX_GRT_MSG));
msg= grt->msgs->msgs+grt->msgs->msgs_num-1;
msg->msg_type= msg_type;
msg->msg= g_strdup(message);
if (copy_details && details)
{
unsigned int i;
msg->msg_detail= g_new0(MYX_STRINGLIST, 1);
msg->msg_detail->strings= g_new0(char*, details->strings_num);
for (i= 0; i < details->strings_num; i++)
msg->msg_detail->strings[i]= g_strdup(details->strings[i]);
}
else
msg->msg_detail= details;
}
/**
****************************************************************************
* @brief Fetches messages from the GRT.
*
* @param grt
* @param count number of messages to fetch. 0 will return all messages.
*
* @return Fetched messages or NULL, if there's none. The returned struct
* must be freed with myx_grt_free_msgs()
*****************************************************************************/
MYX_GRT_MSGS * myx_grt_get_msgs(MYX_GRT *grt, unsigned int count)
{
//unsigned int i;
MYX_GRT_MSGS *msgs;
// fetch messages from modules
myx_grt_fetch_module_messages(grt);
// return what was requested
if (!grt->msgs)
return NULL;
if (count == 0)
{
msgs= grt->msgs;
grt->msgs= NULL;
}
else
{
unsigned int i;
if (count > grt->msgs->msgs_num)
count= grt->msgs->msgs_num;
msgs= g_new0(MYX_GRT_MSGS, 1);
msgs->msgs= g_new0(MYX_GRT_MSG, count);
for (i= 0; i < count; i++)
{
msgs->msgs[i]= grt->msgs->msgs[i];
}
memmove(grt->msgs->msgs, grt->msgs->msgs+count,
(grt->msgs->msgs_num-count)*sizeof(MYX_GRT_MSG));
grt->msgs->msgs_num-= count;
}
return msgs;
}
/**
****************************************************************************
* @brief
*
* @param msgs messages to be freed
*****************************************************************************/
void myx_grt_free_msgs(MYX_GRT_MSGS *msgs)
{
unsigned int i;
for (i= 0; i < msgs->msgs_num; i++)
{
g_free(msgs->msgs[i].msg);
if (msgs->msgs[i].msg_detail)
myx_free_stringlist(msgs->msgs[i].msg_detail);
}
g_free(msgs->msgs);
g_free(msgs);
}
/**
****************************************************************************
* @brief Clears the current cache of _id strings in the root value
*
* @param grt the GRT environment
*****************************************************************************/
void myx_grt_reference_cache_clear(MYX_GRT *grt)
{
// free current cache
g_hash_table_destroy(grt->reference_cache);
// reallocate cache
grt->reference_cache= g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
}
static void grt_rescan_dict(MYX_GRT *grt, MYX_GRT_VALUE *dict, const char *object_path)
{
unsigned int i;
const char *struct_name= myx_grt_dict_struct_get_name(dict);
// if this dict inherits from GrtObject, cache its _id if it not empty
// and set the object path
if (struct_name && myx_grt_struct_check_inherits_from(grt, struct_name, "GrtObject"))
{
const char *_id= myx_grt_dict_item_get_as_string(dict, "_id");
if (_id)
{
dict->value.d->grt= grt;
dict->value.d->object_path= g_strdup(object_path);
g_hash_table_insert(grt->reference_cache, g_strdup(_id), dict);
}
}
for (i= 0; i<myx_grt_dict_item_count(dict); i++)
{
const char *key= myx_grt_dict_item_key_by_index(dict, i);
MYX_GRT_VALUE *value= myx_grt_dict_item_get_value(dict, key);
if (myx_grt_value_get_type(value) == MYX_DICT_VALUE)
{
char *new_path= g_strdup(object_path);
new_path= str_g_append(new_path, "/");
new_path= str_g_append(new_path, key);
grt_rescan_dict(grt, value, new_path);
g_free(new_path);
}
else
if ( (myx_grt_value_get_type(value) == MYX_LIST_VALUE) &&
(myx_grt_list_content_get_type(value) == MYX_DICT_VALUE) )
{
unsigned int j;
for (j= 0; j<myx_grt_list_item_count(value); j++)
{
char *new_path= g_strdup_printf("%s/%s/%d", object_path, key, j);
grt_rescan_dict(grt, myx_grt_list_item_get(value, j), new_path);
g_free(new_path);
}
}
}
}
/**
****************************************************************************
* @brief Rescans the root value to cache all _id strings
*
* @param grt the GRT environment
*****************************************************************************/
void myx_grt_reference_cache_rescan(MYX_GRT *grt)
{
// start scan
grt_rescan_dict(grt, myx_grt_get_root(grt), "");
}
/**
****************************************************************************
* @brief Performs a reference cache lookup for a specific _id
*
* @param grt the GRT environment
* @param ref_id the _id that is looked up
*
* @return returns a value if it is found in the cache or NULL
*****************************************************************************/
MYX_GRT_VALUE * myx_grt_reference_cache_lookup(MYX_GRT *grt, const char *ref_id)
{
if(ref_id)
{
// try to find the value in the cache
MYX_GRT_VALUE *value= g_hash_table_lookup(grt->reference_cache, ref_id);
if (value)
return value;
else
{
// if it is not found, rescan and search again
myx_grt_reference_cache_rescan(grt);
return g_hash_table_lookup(grt->reference_cache, ref_id);
}
}
else
return NULL;
}
char * myx_grt_get_guid()
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
GUID guid;
WCHAR guid_wstr[50];
char guid_str[200];
int len;
CoCreateGuid(&guid);
len= StringFromGUID2(&guid, (LPOLESTR) guid_wstr, 50);
//Covert GUID from WideChar to utf8
WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) guid_wstr, len,
(LPSTR) guid_str, 200, NULL, NULL);
return g_strdup(guid_str);
#else
{
uuid_t gid;
char buffer[40];
uuid_generate_time(gid);
uuid_unparse(gid, buffer);
return g_strdup(buffer);
}
#endif
}
|