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
|
/*
*
* Copyright (C) 2009-2011 Colomban Wendling <ban@herbesfolles.org>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "ctpl-value.h"
#include "ctpl-mathutils.h"
#include <glib.h>
#include <stdarg.h>
/**
* SECTION:value
* @short_description: Generic values
* @include: ctpl/ctpl.h
*
* A generic value manager.
*
* Dynamically allocated #CtplValue are created with ctpl_value_new() and freed
* with ctpl_value_free().
* Statically allocated ones are initialized with ctpl_value_init() and
* uninitialized with ctpl_value_free_value().
*
* You can set the data they holds with ctpl_value_set_int(),
* ctpl_value_set_float(), ctpl_value_set_string() and ctpl_value_set_array();
* you can add elements to an array value with ctpl_value_array_append(),
* ctpl_value_array_prepend(), ctpl_value_array_append_int(),
* ctpl_value_array_prepend_int(), ctpl_value_array_append_float(),
* ctpl_value_array_prepend_float(), ctpl_value_array_append_string() and
* ctpl_value_array_prepend_string().
*
* To get the value held by a #CtplValue, use ctpl_value_get_int(),
* ctpl_value_get_float(), ctpl_value_get_string(), ctpl_value_get_array_int(),
* ctpl_value_get_array_float() or ctpl_value_get_array_string() depending on
* the type of the value.
* For array value, yo can also use ctpl_value_get_array() to get the list of
* the different values in that array.
* You can get the type held by a value with ctpl_value_get_held_type().
*
* Value may be converted to other types with ctpl_value_convert(), and to a
* string representation using ctpl_value_to_string().
*
* <example>
* <title>Simple usage of dynamically allocated generic values</title>
* <programlisting>
* CtplValue *val;
*
* val = ctpl_value_new ();
* ctpl_value_set_int (val, 42);
*
* /<!-- -->* Free all data allocated for the value and the held data *<!-- -->/
* ctpl_value_free (val);
* </programlisting>
* </example>
*
* <example>
* <title>Simple usage of statically allocated generic values</title>
* <programlisting>
* CtplValue val;
*
* ctpl_value_init (&val);
* ctpl_value_set_int (&val, 42);
*
* /<!-- -->* Free all memory that might have been allocated for the held
* * data *<!-- -->/
* ctpl_value_free_value (&val);
* </programlisting>
* </example>
*/
static void ctpl_value_set_array_internal (CtplValue *value,
const GSList *values);
/**
* ctpl_value_init:
* @value: An uninitialized #CtplValue
*
* Initializes a #CtplValue.
* This function is useful for statically allocated values, and is not required
* for dynamically allocated values created by ctpl_value_new().
*/
void
ctpl_value_init (CtplValue *value)
{
value->value.v_int = 0l;
value->value.v_float = 0.0f;
value->value.v_string = NULL;
value->value.v_array = NULL;
value->type = CTPL_VTYPE_INT; /* defaults to a simple non-allocated type */
}
/**
* ctpl_value_new:
*
* Creates a new empty #CtplValue.
*
* Returns: A newly allocated #CtplValue that should be freed using
* ctpl_value_free()
*/
CtplValue *
ctpl_value_new (void)
{
CtplValue *value;
value = g_slice_alloc (sizeof *value);
if (value) {
ctpl_value_init (value);
}
return value;
}
/**
* ctpl_value_copy:
* @src_value: A #CtplValue to copy
* @dst_value: A #CtplValue into which copy @src_value
*
* Copies the value of a #CtplValue into another.
* See ctpl_value_dup() if you want to duplicate the value and not only its
* content.
*/
void
ctpl_value_copy (const CtplValue *src_value,
CtplValue *dst_value)
{
switch (ctpl_value_get_held_type (src_value)) {
case CTPL_VTYPE_INT:
ctpl_value_set_int (dst_value, ctpl_value_get_int (src_value));
break;
case CTPL_VTYPE_FLOAT:
ctpl_value_set_float (dst_value, ctpl_value_get_float (src_value));
break;
case CTPL_VTYPE_STRING:
ctpl_value_set_string (dst_value, ctpl_value_get_string (src_value));
break;
case CTPL_VTYPE_ARRAY:
ctpl_value_set_array_internal (dst_value,
ctpl_value_get_array (src_value));
break;
}
}
/**
* ctpl_value_dup:
* @value: A #CtplValue to copy
*
* Duplicates a #CtplValue.
* This function simply creates a new #CtplValue with ctpl_value_new() then
* copies @value into it using ctpl_value_copy().
*
* Returns: A newly allocated #CtplValue
*/
CtplValue *
ctpl_value_dup (const CtplValue *value)
{
CtplValue *new_value;
new_value = ctpl_value_new ();
ctpl_value_copy (value, new_value);
return new_value;
}
/**
* ctpl_value_free_value:
* @value: A #CtplValue
*
* Frees the data held by a #CtplValue.
* This function is only useful to the end user for statically allocated values
* since ctpl_value_free() does all the job needed to completely release an
* allocated #CtplValue.
*/
void
ctpl_value_free_value (CtplValue *value)
{
switch (value->type) {
case CTPL_VTYPE_STRING:
g_free (value->value.v_string);
value->value.v_string = NULL;
break;
case CTPL_VTYPE_ARRAY: {
GSList *i;
for (i = value->value.v_array; i != NULL; i = i->next) {
ctpl_value_free (i->data);
}
g_slist_free (value->value.v_array);
value->value.v_array = NULL;
break;
}
}
}
/**
* ctpl_value_free:
* @value: A #CtplValue
*
* Frees all resources used by a #CtplValue.
* This function can't be used with statically allocated values since it also
* frees the value itself and not only its content. If you want to free a
* statically allocated value, use ctpl_value_free_value().
*/
void
ctpl_value_free (CtplValue *value)
{
if (value) {
ctpl_value_free_value (value);
g_slice_free1 (sizeof *value, value);
}
}
/**
* ctpl_value_new_int:
* @val: An integer
*
* Creates a new #CtplValue and sets its value to @val.
* See ctpl_value_new() and ctpl_value_set_int().
*
* Returns: A newly allocated #CtplValue holding @val.
*/
CtplValue *
ctpl_value_new_int (glong val)
{
CtplValue *value;
value = ctpl_value_new ();
ctpl_value_set_int (value, val);
return value;
}
/**
* ctpl_value_new_float:
* @val: A float
*
* Creates a new #CtplValue and sets its value to @val.
* See ctpl_value_new() and ctpl_value_set_float().
*
* Returns: A newly allocated #CtplValue holding @val.
*/
CtplValue *
ctpl_value_new_float (gdouble val)
{
CtplValue *value;
value = ctpl_value_new ();
ctpl_value_set_float (value, val);
return value;
}
/**
* ctpl_value_new_string:
* @val: A string
*
* Creates a new #CtplValue and sets its value to @val.
* See ctpl_value_new() and ctpl_value_set_string().
*
* Returns: A newly allocated #CtplValue holding @val.
*/
CtplValue *
ctpl_value_new_string (const gchar *val)
{
CtplValue *value;
value = ctpl_value_new ();
ctpl_value_set_string (value, val);
return value;
}
/**
* ctpl_value_new_arrayv:
* @type: The type of the array's elements
* @count: The number of elements
* @ap: A va_list containing the values of the type specified by @type,
* ended by a %NULL value
*
* Creates a new #CtplValue and sets its values to the given ones.
* See ctpl_value_new() and ctpl_value_set_arrayv().
*
* <warning><para>
* As this function takes a variadic argument, there is no control on the values
* neither on their type nor on any other of their properties. Then, you have to
* take care to pass strictly right data to it if you won't see your program
* crash -- in the better case.
* </para></warning>
*
* Returns: A newly allocated #CtplValue holding given values.
*/
CtplValue *
ctpl_value_new_arrayv (CtplValueType type,
gsize count,
va_list ap)
{
CtplValue *value;
value = ctpl_value_new ();
ctpl_value_set_arrayv (value, type, count, ap);
return value;
}
/**
* ctpl_value_new_array:
* @type: The type of the array's elements
* @count: The number of elements
* @...: A %NULL-ended list of elements of the type specified by @type
*
* Creates a new #CtplValue and sets its values to the given ones.
* See ctpl_value_new_arrayv().
*
* <warning><para>
* As this function takes a variadic argument, there is no control on the values
* neither on their type nor on any other of their properties. Then, you have to
* take care to pass strictly right data to it if you won't see your program
* crash -- in the better case.
* </para></warning>
*
* Returns: A newly allocated #CtplValue holding given values.
*/
CtplValue *
ctpl_value_new_array (CtplValueType type,
gsize count,
...)
{
CtplValue *value;
va_list ap;
va_start (ap, count);
value = ctpl_value_new_arrayv (type, count, ap);
va_end (ap);
return value;
}
/**
* ctpl_value_set_int:
* @value: A #CtplValue
* @val: An integer
*
* Sets the value of a #CtplValue to the given integer.
*/
void
ctpl_value_set_int (CtplValue *value,
glong val)
{
ctpl_value_free_value (value);
value->type = CTPL_VTYPE_INT;
value->value.v_int = val;
}
/**
* ctpl_value_set_float:
* @value: A #CtplValue
* @val: A float
*
* Sets the value of a #CtplValue to the given float.
*/
void
ctpl_value_set_float (CtplValue *value,
gdouble val)
{
ctpl_value_free_value (value);
value->type = CTPL_VTYPE_FLOAT;
value->value.v_float = val;
}
/**
* ctpl_value_set_string:
* @value: A #CtplValue
* @val: A string
*
* Sets the value of a #CtplValue to the given string.
* The string is copied.
*/
void
ctpl_value_set_string (CtplValue *value,
const gchar *val)
{
gchar *val_dup;
val_dup = g_strdup (val);
ctpl_value_free_value (value);
value->type = CTPL_VTYPE_STRING;
value->value.v_string = val_dup;
}
/*
* ctpl_value_set_array_internal:
* @value: A #CtplValue
* @values: A GSList containing values to set.
*
* This function duplicates all the given #GSList.
*/
static void
ctpl_value_set_array_internal (CtplValue *value,
const GSList *values)
{
GSList *new_values = NULL;
for (; values != NULL; values = values->next) {
new_values = g_slist_prepend (new_values, ctpl_value_dup (values->data));
}
new_values = g_slist_reverse (new_values);
ctpl_value_free_value (value);
value->type = CTPL_VTYPE_ARRAY;
value->value.v_array = new_values;
}
/**
* ctpl_value_set_arrayv:
* @value: A #CtplValue
* @type: The type of the given elements
* @count: The number of elements
* @ap: A %NULL-ended va_list of the elements
*
* Sets the value of a #CtplValue from the given list of elements.
* See ctpl_value_array_append(), ctpl_value_array_append_int(),
* ctpl_value_array_append_float() and ctpl_value_array_append_string().
*
* <warning><para>
* As this function takes a variadic argument, there is no control on the values
* neither on their type nor on any other of their properties. Then, you have to
* take care to pass strictly right data to it if you won't see your program
* crash -- in the better case.
* </para></warning>
*/
void
ctpl_value_set_arrayv (CtplValue *value,
CtplValueType type,
gsize count,
va_list ap)
{
ctpl_value_free_value (value);
value->type = CTPL_VTYPE_ARRAY;
value->value.v_array = NULL; /* needed by the GSList at first appending */
switch (type) {
case CTPL_VTYPE_INT: {
gsize i;
for (i = 0; i < count; i++) {
ctpl_value_array_append_int (value, va_arg (ap, glong));
}
break;
}
case CTPL_VTYPE_FLOAT: {
gsize i;
for (i = 0; i < count; i++) {
ctpl_value_array_append_float (value, va_arg (ap, gdouble));
}
break;
}
case CTPL_VTYPE_STRING: {
gsize i;
for (i = 0; i < count; i++) {
ctpl_value_array_append_string (value, va_arg (ap, const gchar *));
}
break;
}
case CTPL_VTYPE_ARRAY: {
g_critical ("Cannot build arrays of arrays this way");
break;
}
}
/* finally, red the sentinel */
if (va_arg (ap, const gchar *) != NULL) {
g_critical ("Last read parameter is not a NULL pointer. You probably gave "
"a wrong count of arguments, missed the sentinel or gave an "
"argument of the wrong type.");
}
}
/**
* ctpl_value_set_array:
* @value: A #CtplValue
* @type: The type of the given elements
* @count: The number of elements
* @...: A %NULL-ended list of elements
*
* Sets the value of a #CtplValue from the given elements.
* See ctpl_value_set_arrayv().
*
* <warning><para>
* As this function takes a variadic argument, there is no control on the values
* neither on their type nor on any other of their properties. Then, you have to
* take care to pass strictly right data to it if you won't see your program
* crash -- in the better case.
* </para></warning>
*/
void
ctpl_value_set_array (CtplValue *value,
CtplValueType type,
gsize count,
...)
{
va_list ap;
va_start (ap, count);
ctpl_value_set_arrayv (value, type, count, ap);
va_end (ap);
}
/**
* ctpl_value_set_array_intv:
* @value: A #CtplValue
* @count: The number of given elements
* @ap: A %NULL-ended va_list of integers
*
* Sets the value of a #CtplValue from the given integers.
* This is a convenience wrapper around ctpl_value_set_arrayv(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_intv (CtplValue *value,
gsize count,
va_list ap)
{
ctpl_value_set_arrayv (value, CTPL_VTYPE_INT, count, ap);
}
/**
* ctpl_value_set_array_int:
* @value: A #CtplValue
* @count: The number of given elements
* @...: A %NULL-ended list of integers
*
* Sets the value of a #CtplValue from the given integers.
* This is a convenience wrapper around ctpl_value_set_array(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_int (CtplValue *value,
gsize count,
...)
{
va_list ap;
va_start (ap, count);
ctpl_value_set_array_intv (value, count, ap);
va_end (ap);
}
/**
* ctpl_value_set_array_floatv:
* @value: A #CtplValue
* @count: The number of given elements
* @ap: A %NULL-ended va_list of floats
*
* Sets the value of a #CtplValue from the given floats.
* This is a convenience wrapper around ctpl_value_set_arrayv(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_floatv (CtplValue *value,
gsize count,
va_list ap)
{
ctpl_value_set_arrayv (value, CTPL_VTYPE_FLOAT, count, ap);
}
/**
* ctpl_value_set_array_float:
* @value: A #CtplValue
* @count: The number of given elements
* @...: A %NULL-ended list of floats
*
* Sets the value of a #CtplValue from the given floats.
* This is a convenience wrapper around ctpl_value_set_array(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_float (CtplValue *value,
gsize count,
...)
{
va_list ap;
va_start (ap, count);
ctpl_value_set_array_floatv (value, count, ap);
va_end (ap);
}
/**
* ctpl_value_set_array_stringv:
* @value: A #CtplValue
* @count: The number of given elements
* @ap: A %NULL-ended va_list of strings (as const char*)
*
* Sets the value of a #CtplValue from the given strings.
* This is a convenience wrapper around ctpl_value_set_arrayv(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_stringv (CtplValue *value,
gsize count,
va_list ap)
{
ctpl_value_set_arrayv (value, CTPL_VTYPE_STRING, count, ap);
}
/**
* ctpl_value_set_array_string:
* @value: A #CtplValue
* @count: The number of given elements
* @...: A %NULL-ended list of strings (as const char*)
*
* Sets the value of a #CtplValue from the given strings.
* This is a convenience wrapper around ctpl_value_set_array(), and the same
* care have to been taken about.
*/
void
ctpl_value_set_array_string (CtplValue *value,
gsize count,
...)
{
va_list ap;
va_start (ap, count);
ctpl_value_set_array_stringv (value, count, ap);
va_end (ap);
}
/**
* ctpl_value_array_append:
* @value: A #CtplValue holding an array
* @val: A #CtplValue to append
*
* Appends a #CtplValue to another #CtplValue holding an array. The appended
* value is copied.
*/
void
ctpl_value_array_append (CtplValue *value,
const CtplValue *val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_append (value->value.v_array,
ctpl_value_dup (val));
}
/**
* ctpl_value_array_prepend:
* @value: A #CtplValue holding an array
* @val: A #CtplValue to prepend
*
* Prepends a #CtplValue to another #CtplValue holding an array. The prepended
* value is copied.
*/
void
ctpl_value_array_prepend (CtplValue *value,
const CtplValue *val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_prepend (value->value.v_array,
ctpl_value_dup (val));
}
/**
* ctpl_value_array_append_int:
* @value: A #CtplValue holding an array
* @val: An integer to append
*
* Appends an integer to a #CtplValue holding an array.
*/
void
ctpl_value_array_append_int (CtplValue *value,
glong val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_append (value->value.v_array,
ctpl_value_new_int (val));
}
/**
* ctpl_value_array_prepend_int:
* @value: A #CtplValue holding an array
* @val: An integer to prepend
*
* Prepends an integer to a #CtplValue holding an array.
*/
void
ctpl_value_array_prepend_int (CtplValue *value,
glong val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_prepend (value->value.v_array,
ctpl_value_new_int (val));
}
/**
* ctpl_value_array_append_float:
* @value: A #CtplValue holding an array
* @val: A float to append
*
* Appends a float to a #CtplValue holding an array.
*/
void
ctpl_value_array_append_float (CtplValue *value,
gdouble val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_append (value->value.v_array,
ctpl_value_new_float (val));
}
/**
* ctpl_value_array_prepend_float:
* @value: A #CtplValue holding an array
* @val: A float to prepend
*
* Prepends a float to a #CtplValue holding an array.
*/
void
ctpl_value_array_prepend_float (CtplValue *value,
gdouble val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_prepend (value->value.v_array,
ctpl_value_new_float (val));
}
/**
* ctpl_value_array_append_string:
* @value: A #CtplValue holding an array
* @val: A string to append
*
* Appends a string to a #CtplValue holding an array. The string is copied.
*/
void
ctpl_value_array_append_string (CtplValue *value,
const gchar *val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_append (value->value.v_array,
ctpl_value_new_string (val));
}
/**
* ctpl_value_array_prepend_string:
* @value: A #CtplValue holding an array
* @val: A string to prepend
*
* Prepends a string to a #CtplValue holding an array. The string is copied.
*/
void
ctpl_value_array_prepend_string (CtplValue *value,
const gchar *val)
{
g_return_if_fail (CTPL_VALUE_HOLDS_ARRAY (value));
value->value.v_array = g_slist_prepend (value->value.v_array,
ctpl_value_new_string (val));
}
/**
* ctpl_value_array_length:
* @value: A #CtplValue holding an array
*
* Gets the number of elements in a #CtplValue that holds an array.
*
* Returns: The number of elements in @value.
*/
gsize
ctpl_value_array_length (const CtplValue *value)
{
return g_slist_length (value->value.v_array);
}
/**
* ctpl_value_array_index:
* @value: A #CtplValue holding an array
* @idx: The array's index to get
*
* Index an array, getting its @idx-th element.
*
* Returns: The @idx-th element of @value, or %NULL if @idx is out of bounds.
*/
CtplValue *
ctpl_value_array_index (const CtplValue *value,
gsize idx)
{
gsize i;
GSList *tmp;
g_return_val_if_fail (CTPL_VALUE_HOLDS_ARRAY (value), NULL);
for (i = 0, tmp = value->value.v_array; i < idx && tmp; i++) {
tmp = tmp->next;
}
return tmp ? tmp->data : NULL;
}
/**
* ctpl_value_get_held_type:
* @value: A #CtplValue
*
* Gets the type held by the a #CtplValue.
*
* Returns: The type held by the value.
*/
CtplValueType
ctpl_value_get_held_type (const CtplValue *value)
{
return value->type;
}
/**
* ctpl_value_type_get_name:
* @type: A #CtplValueType
*
* Gets a human-readable name for a value type.
*
* Returns: A static string of a displayable name for @type. This string must
* not be modified or freed.
*/
const gchar *
ctpl_value_type_get_name (CtplValueType type)
{
switch (type) {
case CTPL_VTYPE_INT:
return "integer";
case CTPL_VTYPE_FLOAT:
return "float";
case CTPL_VTYPE_STRING:
return "string";
case CTPL_VTYPE_ARRAY:
/* TODO: return the array type? (e.g. "array of int",
* "array of strings and floats", etc?) */
return "array";
}
return "???";
}
/**
* ctpl_value_get_int:
* @value: A #CtplValue holding a int
*
* Gets the value of a #CtplValue holding a integer.
*
* Returns: The integer value held by @value.
*/
glong
ctpl_value_get_int (const CtplValue *value)
{
g_return_val_if_fail (CTPL_VALUE_HOLDS_INT (value), 0);
return value->value.v_int;
}
/**
* ctpl_value_get_float:
* @value: A #CtplValue holding a float
*
* Gets the value of a #CtplValue holding a float.
*
* Returns: The float value held by @value.
*/
gdouble
ctpl_value_get_float (const CtplValue *value)
{
g_return_val_if_fail (CTPL_VALUE_HOLDS_FLOAT (value), 0.0);
return value->value.v_float;
}
/**
* ctpl_value_get_string:
* @value: A #CtplValue holding a string
*
* Gets the value of a #CtplValue holding a string.
*
* Returns: A string owned by the value that should not be modified or freed, or
* %NULL if an error occurs.
*/
const gchar *
ctpl_value_get_string (const CtplValue *value)
{
g_return_val_if_fail (CTPL_VALUE_HOLDS_STRING (value), NULL);
return value->value.v_string;
}
/**
* ctpl_value_get_array:
* @value: A #CtplValue holding an array
*
* Gets the values of a #CtplValue holding an array as a #GSList in which each
* element holds a #CtplValue holding the element value.
*
* Returns: (element-type Ctpl.Value) (transfer none): A #GSList owned by the
* value that must not be freed, neither the list itself nor its
* values, or %NULL on error.
*/
const GSList *
ctpl_value_get_array (const CtplValue *value)
{
g_return_val_if_fail (CTPL_VALUE_HOLDS_ARRAY (value), NULL);
return value->value.v_array;
}
/**
* ctpl_value_get_array_int:
* @value: A #CtplValue holding an array of integers
* @length: (out) (allow-none): Return location for the array length, or %NULL
*
* Gets the values of a #CtplValue as an array of int.
* The value must hold an array and all array's elements must be integers.
*
* Returns: (array length=length) (transfer full): A newly allocated array of
* integers that should be freed with g_free() or %NULL on error.
*/
glong *
ctpl_value_get_array_int (const CtplValue *value,
gsize *length)
{
const GSList *values;
const GSList *i;
gsize n;
gsize len;
glong *array;
values = ctpl_value_get_array (value);
g_return_val_if_fail (values != NULL, NULL);
/* cast is because g_slist_length() takes a non-const pointer, see
* http://bugzilla.gnome.org/show_bug.cgi?id=50953 */
len = g_slist_length ((GSList *)values);
array = g_new0 (glong, len);
for (n = 0, i = values; i != NULL; n ++, i = i->next) {
CtplValue *v = i->data;
if (! CTPL_VALUE_HOLDS_INT (v)) {
goto fail;
} else {
array[n] = v->value.v_int;
}
}
if (length) *length = len;
return array;
fail:
g_free (array);
return NULL;
}
/**
* ctpl_value_get_array_float:
* @value: A #CtplValue holding an array of floats
* @length: (out) (allow-none): Return location for the array length, or %NULL
*
* Gets the values of a #CtplValue as an array of floats.
* @value must hold an array and all array's elements must be floats.
*
* Returns: (array length=length) (transfer full): A newly allocated array of
* floats that should be freed with g_free() or %NULL on error.
*/
gdouble *
ctpl_value_get_array_float (const CtplValue *value,
gsize *length)
{
const GSList *values;
const GSList *i;
gsize n;
gsize len;
gdouble *array;
values = ctpl_value_get_array (value);
g_return_val_if_fail (values != NULL, NULL);
/* cast is because g_slist_length() takes a non-const pointer, see
* http://bugzilla.gnome.org/show_bug.cgi?id=50953 */
len = g_slist_length ((GSList *)values);
array = g_new0 (gdouble, len);
for (n = 0, i = values; i != NULL; n ++, i = i->next) {
CtplValue *v = i->data;
if (! CTPL_VALUE_HOLDS_FLOAT (v)) {
goto fail;
} else {
array[n] = v->value.v_float;
}
}
if (length) *length = len;
return array;
fail:
g_free (array);
return NULL;
}
/**
* ctpl_value_get_array_string:
* @value: A #CtplValue holding an array of strings
* @length: (out) (allow-none): Return location for the length of the returned
* array, or %NULL.
*
* Gets the values held by a #CtplValue as an array of strings.
* @value must hold an array containing only strings.
*
* Returns: (array length=length) (transfer full): A newly allocated
* %NULL-terminated array of strings, or %NULL on error. Free with
* g_strfreev() when no longer needed.
*/
gchar **
ctpl_value_get_array_string (const CtplValue *value,
gsize *length)
{
const GSList *values;
const GSList *i;
gsize n;
gsize len;
gchar **array;
values = ctpl_value_get_array (value);
g_return_val_if_fail (values != NULL, NULL);
/* cast is because g_slist_length() takes a non-const pointer, see
* http://bugzilla.gnome.org/show_bug.cgi?id=50953 */
len = g_slist_length ((GSList *)values);
array = g_new0 (gchar*, len + 1);
for (n = 0, i = values; i != NULL; n ++, i = i->next) {
CtplValue *v = i->data;
if (! CTPL_VALUE_HOLDS_STRING (v)) {
goto fail;
} else {
array[n] = g_strdup (v->value.v_string);
}
}
array[n] = NULL;
if (length) *length = len;
return array;
fail:
g_free (array);
return NULL;
}
/**
* ctpl_value_to_string:
* @value: A #CtplValue
*
* Converts a #CtplValue to a string.
*
* <note>
* <para>
* Arrays are flattened to the form [val1, val2, val3]. It may not be what
* you want, but flattening an array is not the primary goal of this
* function and you should consider doing it yourself if it is what you
* want - flattening an array.
* </para>
* </note>
*
* Returns: A newly allocated string representing the value. You should free
* this value with g_free() when no longer needed.
*/
gchar *
ctpl_value_to_string (const CtplValue *value)
{
gchar *val = NULL;
switch (ctpl_value_get_held_type (value)) {
case CTPL_VTYPE_ARRAY: {
/* FIXME: should we warn when converting arrays to strings? */
const GSList *subvalues;
GString *string;
string = g_string_new ("[");
for (subvalues = ctpl_value_get_array (value);
subvalues;
subvalues = subvalues->next) {
gchar *item;
item = ctpl_value_to_string (subvalues->data);
g_string_append (string, item);
g_free (item);
/* append a comma if there is a next element */
if (subvalues->next) {
g_string_append (string, ", ");
}
}
g_string_append (string, "]");
val = g_string_free (string, FALSE);
break;
}
case CTPL_VTYPE_FLOAT:
val = ctpl_math_float_to_string (value->value.v_float);
break;
case CTPL_VTYPE_INT:
val = g_strdup_printf ("%ld", value->value.v_int);
break;
case CTPL_VTYPE_STRING:
val = g_strdup (value->value.v_string);
break;
}
return val;
}
/**
* ctpl_value_convert:
* @value: A #CtplValue to convert
* @vtype: The type to which convert @value
*
* Tries to convert a #CtplValue to another type.
*
* The performed conversion might be called "non-destructive": the value will
* not loose precision, but the conversion will rather fail if it would lead to
* a loss.
* An good example is converting a floating-point value to an integer one:
* the conversion will only happen if it would not truncate the floating part.
*
* <warning>
* <para>
* The current implementation of floating-point value comparison might be
* lossy, and then the above example might be somewhat wrong in practice.
* </para>
* </warning>
*
* <note>
* <para>
* Converting to a string uses ctpl_value_to_string().
* Even if it will never fail, the result might not be the one you expect
* when converting an array.
* </para>
* </note>
*
* Returns: %TRUE if the conversion succeeded, %FALSE otherwise.
*/
gboolean
ctpl_value_convert (CtplValue *value,
CtplValueType vtype)
{
gboolean rv = TRUE;
CtplValueType actual_type;
actual_type = ctpl_value_get_held_type (value);
if (actual_type != vtype) {
switch (vtype) {
/* convert to array */
case CTPL_VTYPE_ARRAY:
switch (actual_type) {
case CTPL_VTYPE_FLOAT:
ctpl_value_set_array_float (value, 1,
ctpl_value_get_float (value), NULL);
break;
case CTPL_VTYPE_INT:
ctpl_value_set_array_int (value, 1,
ctpl_value_get_int (value), NULL);
break;
case CTPL_VTYPE_STRING: {
gchar *v;
v = g_strdup (ctpl_value_get_string (value));
ctpl_value_set_array_string (value, 1, v, NULL);
g_free (v);
break;
}
default:
rv = FALSE;
}
break;
/* convert to float */
case CTPL_VTYPE_FLOAT:
switch (actual_type) {
case CTPL_VTYPE_INT: {
glong val;
val = ctpl_value_get_int (value);
ctpl_value_set_float (value, (gdouble)val);
break;
}
case CTPL_VTYPE_STRING: {
gdouble vfloat;
rv = ctpl_math_string_to_float (ctpl_value_get_string (value), &vfloat);
if (rv) {
ctpl_value_set_float (value, vfloat);
}
break;
}
default:
rv = FALSE;
}
break;
/* convert to integer */
case CTPL_VTYPE_INT:
switch (actual_type) {
case CTPL_VTYPE_FLOAT: {
gdouble val;
val = ctpl_value_get_float (value);
if (! CTPL_MATH_FLOAT_EQ (val, (gdouble)(glong)val)) {
rv = FALSE;
} else {
ctpl_value_set_int (value, (glong)val);
}
break;
}
case CTPL_VTYPE_STRING: {
glong vint;
rv = ctpl_math_string_to_int (ctpl_value_get_string (value), &vint);
if (rv) {
ctpl_value_set_int (value, vint);
}
break;
}
default:
rv = FALSE;
}
break;
/* convert to string */
case CTPL_VTYPE_STRING: {
gchar *val;
val = ctpl_value_to_string (value);
ctpl_value_set_string (value, val);
rv = (val != NULL);
g_free (val);
break;
}
}
}
return rv;
}
|