61 #ifndef QOF_DISABLE_DEPRECATED
64 KvpValueBinaryData binary;
78 kvp_hash_func (gconstpointer v)
80 return g_str_hash (v);
84 kvp_comp_func (gconstpointer v, gconstpointer v2)
86 return g_str_equal (v, v2);
90 init_frame_body_if_needed (
KvpFrame * f)
94 f->hash = g_hash_table_new (&kvp_hash_func, &kvp_comp_func);
96 return (f->hash != NULL);
110 kvp_frame_delete_worker (gpointer key, gpointer value,
111 gpointer user_data __attribute__ ((unused)))
126 g_hash_table_foreach (frame->hash, &kvp_frame_delete_worker,
130 g_hash_table_destroy (frame->hash);
147 kvp_frame_copy_worker (gpointer key, gpointer value, gpointer user_data)
150 g_hash_table_insert (dest->hash,
165 if (!init_frame_body_if_needed (retval))
167 g_hash_table_foreach (frame->hash,
168 &kvp_frame_copy_worker, (gpointer) retval);
182 gpointer orig_value = NULL;
187 if (!init_frame_body_if_needed (frame))
190 key_exists = g_hash_table_lookup_extended (frame->hash, slot,
191 &orig_key, &orig_value);
194 g_hash_table_remove (frame->hash, slot);
200 g_hash_table_insert (frame->hash,
209 kvp_frame_set_slot_destructively (
KvpFrame * frame,
const gchar *slot,
223 get_or_make (
KvpFrame * fr,
const gchar *key)
228 value = kvp_frame_get_slot (fr, key);
245 kvp_frame_get_frame_slash_trash (
KvpFrame * frame, gchar *key_path)
248 if (!frame || !key_path)
261 next = strchr (key,
'/');
265 frame = get_or_make (frame, key);
279 kvp_frame_get_frame_or_null_slash_trash (
const KvpFrame * frame,
284 if (!frame || !key_path)
297 next = strchr (key,
'/');
301 value = kvp_frame_get_slot (frame, key);
319 get_trailer_make (
KvpFrame * frame,
const gchar *key_path,
324 if (!frame || !key_path || (0 == key_path[0]))
327 last_key = strrchr (key_path,
'/');
328 if (NULL == last_key)
329 last_key = (gchar *) key_path;
330 else if (last_key == key_path)
332 else if (0 == last_key[1])
337 root = g_strdup (key_path);
338 lkey = strrchr (root,
'/');
340 frame = kvp_frame_get_frame_slash_trash (frame, root);
356 get_trailer_or_null (
const KvpFrame * frame,
const gchar *key_path,
361 if (!frame || !key_path || (0 == key_path[0]))
364 last_key = strrchr (key_path,
'/');
365 if (NULL == last_key)
366 last_key = (gchar *) key_path;
367 else if (last_key == key_path)
369 else if (0 == last_key[1])
374 root = g_strdup (key_path);
375 lkey = strrchr (root,
'/');
377 frame = kvp_frame_get_frame_or_null_slash_trash (frame, root);
393 value = kvp_value_new_gint64 (ival);
403 value = kvp_value_new_double (dval);
413 value = kvp_value_new_time (qt);
424 value = kvp_value_new_numeric (nval);
446 value = kvp_value_new_string (str);
457 value = kvp_value_new_guid (guid);
467 value = kvp_value_new_frame (fr);
491 frame = get_trailer_make (frame, key_path, &last_key);
494 kvp_frame_set_slot_destructively (frame, last_key, value);
505 frame = get_trailer_make (frame, key_path, &last_key);
511 kvp_frame_set_slot_destructively (frame, last_key, new_value);
524 frame = get_trailer_make (frame, key_path, &last_key);
527 (
KvpFrame *) get_trailer_or_null (frame, key_path, &last_key);
538 kvp_frame_add_value_nc (
KvpFrame * frame,
const gchar *path,
544 frame = (
KvpFrame *) get_trailer_or_null (frame, path, &key);
545 oldvalue = kvp_frame_get_slot (frame, key);
547 ENTER (
"old frame=%s", kvp_frame_to_string (frame));
553 GList *vlist = oldvalue->value.list;
554 vlist = g_list_append (vlist, value);
555 oldvalue->value.list = vlist;
563 vlist = g_list_append (vlist, oldvalue);
564 vlist = g_list_append (vlist, value);
569 LEAVE (
"new frame=%s", kvp_frame_to_string (frame));
576 LEAVE (
"new frame=%s", kvp_frame_to_string (frame));
584 frame = kvp_frame_add_value_nc (frame, path, value);
594 value = kvp_value_new_gint64 (ival);
595 frame = kvp_frame_add_value_nc (frame, path, value);
604 value = kvp_value_new_double (dval);
605 frame = kvp_frame_add_value_nc (frame, path, value);
615 value = kvp_value_new_numeric (nval);
616 frame = kvp_frame_add_value_nc (frame, path, value);
625 value = kvp_value_new_time (qt);
626 frame = kvp_frame_add_value_nc (frame, path, value);
632 kvp_frame_add_boolean (
KvpFrame * frame,
const gchar * path, gboolean val)
636 frame = kvp_frame_add_value_nc (frame, path, value);
645 value = kvp_value_new_string (str);
646 frame = kvp_frame_add_value_nc (frame, path, value);
655 value = kvp_value_new_guid (guid);
656 frame = kvp_frame_add_value_nc (frame, path, value);
665 value = kvp_value_new_frame (fr);
666 frame = kvp_frame_add_value_nc (frame, path, value);
676 frame = kvp_frame_add_value_nc (frame, path, value);
692 g_return_if_fail (slot && *slot !=
'\0');
696 kvp_frame_set_slot_destructively (frame, slot, new_value);
706 g_return_if_fail (slot && *slot !=
'\0');
708 kvp_frame_set_slot_destructively (frame, slot, value);
712 kvp_frame_get_slot (
const KvpFrame * frame,
const gchar *slot)
719 v = g_hash_table_lookup (frame->hash, slot);
727 const KvpValue * new_value,
const gchar *first_key, ...)
735 g_return_if_fail (first_key && *first_key !=
'\0');
737 va_start (ap, first_key);
744 const gchar *next_key;
746 next_key = va_arg (ap,
const gchar *);
753 g_return_if_fail (*next_key !=
'\0');
755 value = kvp_frame_get_slot (frame, key);
759 KvpValue *frame_value = kvp_value_new_frame (new_frame);
763 value = kvp_frame_get_slot (frame, key);
780 const KvpValue * new_value, GSList * key_path)
782 if (!frame || !key_path)
787 const gchar *key = key_path->data;
793 g_return_if_fail (*key !=
'\0');
795 key_path = key_path->next;
802 value = kvp_frame_get_slot (frame, key);
806 KvpValue *frame_value = kvp_value_new_frame (new_frame);
810 value = kvp_frame_get_slot (frame, key);
833 p = strchr (enc,
'+');
840 p = strchr (enc,
'%');
860 *w = (gchar) (ch << 4 | cl);
887 buff = g_strdup (enc);
918 kvp_frame_get_gint64 (
const KvpFrame * frame,
const gchar *path)
921 frame = get_trailer_or_null (frame, path, &key);
926 kvp_frame_get_double (
const KvpFrame * frame,
const gchar *path)
929 frame = get_trailer_or_null (frame, path, &key);
930 return kvp_value_get_double (kvp_frame_get_slot (frame, key));
934 kvp_frame_get_numeric (
const KvpFrame * frame,
const gchar *path)
937 frame = get_trailer_or_null (frame, path, &key);
938 return kvp_value_get_numeric (kvp_frame_get_slot (frame, key));
942 kvp_frame_get_string (
const KvpFrame * frame,
const gchar *path)
945 frame = get_trailer_or_null (frame, path, &key);
950 kvp_frame_get_boolean (
const KvpFrame * frame,
const gchar * path)
953 frame = get_trailer_or_null (frame, path, &key);
954 return kvp_value_get_boolean (kvp_frame_get_slot (frame, key));
958 kvp_frame_get_guid (
const KvpFrame * frame,
const gchar *path)
961 frame = get_trailer_or_null (frame, path, &key);
966 kvp_frame_get_binary (
const KvpFrame * frame,
const gchar *path,
967 guint64 * size_return)
970 frame = get_trailer_or_null (frame, path, &key);
976 kvp_frame_get_time (
const KvpFrame * frame,
const gchar *path)
979 frame = get_trailer_or_null (frame, path, &key);
980 return kvp_value_get_time (kvp_frame_get_slot (frame, key));
987 frame = get_trailer_or_null (frame, path, &key);
992 kvp_frame_get_value (
const KvpFrame * frame,
const gchar *path)
995 frame = get_trailer_or_null (frame, path, &key);
996 return kvp_frame_get_slot (frame, key);
1009 const gchar *key = key_path->data;
1014 frame = get_or_make (frame, key);
1018 key_path = key_path->next;
1034 frame = get_or_make (frame, key);
1037 key = va_arg (ap,
const char *);
1048 if (!frame || !key_path)
1051 root = g_strdup (key_path);
1052 frame = kvp_frame_get_frame_slash_trash (frame, root);
1066 if (!frame || !first_key)
1069 va_start (ap, first_key);
1076 value = kvp_frame_get_slot (frame, key);
1080 key = va_arg (ap,
const gchar *);
1100 if (!frame || !key_path)
1105 const gchar *key = key_path->data;
1111 value = kvp_frame_get_slot (frame, key);
1115 key_path = key_path->next;
1137 for (node = list; node; node = node->next)
1150 GList *retval = NULL;
1158 retval = g_list_copy ((GList *) list);
1161 for (lptr = retval; lptr; lptr = lptr->next)
1179 if (!list1 && list2)
1181 if (list1 && !list2)
1208 kvp_value_new_gint64 (gint64 value)
1212 retval->value.int64 = value;
1217 kvp_value_new_double (gdouble value)
1221 retval->value.dbl = value;
1230 retval->value.gbool = value;
1239 retval->value.numeric = value;
1244 kvp_value_new_string (
const gchar *value)
1252 retval->value.str = g_strdup (value);
1257 kvp_value_new_guid (
const GUID * value)
1265 retval->value.guid = g_new0 (
GUID, 1);
1266 memcpy (retval->value.guid, value, sizeof (
GUID));
1271 kvp_value_new_time (
QofTime *value)
1275 retval->value.qt = value;
1280 kvp_value_new_binary (gconstpointer value, guint64 datasize)
1288 retval->value.binary.data = g_new0 (gpointer, datasize);
1289 retval->value.binary.datasize = datasize;
1290 memcpy (retval->value.binary.data, value, datasize);
1303 retval->value.binary.data = value;
1304 retval->value.binary.datasize = datasize;
1330 retval->value.list = value;
1335 kvp_value_new_frame (
const KvpFrame * value)
1356 retval->value.frame = value;
1366 switch (value->type)
1369 g_free (value->value.str);
1372 g_free (value->value.guid);
1375 g_free (value->value.binary.data);
1394 kvp_value_get_type (
const KvpValue * value)
1407 return value->value.int64;
1410 PERR (
" value type %d does not match KVP_TYPE_GINT64",
1417 kvp_value_get_double (
const KvpValue * value)
1422 return value->value.dbl;
1425 PERR (
" value type %d does not match KVP_TYPE_DOUBLE",
1432 kvp_value_get_numeric (
const KvpValue * value)
1437 return value->value.numeric;
1440 PERR (
" value type %d does not match KVP_TYPE_NUMERIC",
1452 return value->value.str;
1455 PERR (
" value type %d does not match KVP_TYPE_STRING",
1462 kvp_value_get_boolean (
const KvpValue * value)
1467 return value->value.gbool;
1470 PERR (
" value type %d does not match KVP_TYPE_BOOLEAN",
1482 return value->value.guid;
1485 PERR (
" value type %d does not match KVP_TYPE_GUID",
1492 kvp_value_get_time (
const KvpValue * value)
1497 return value->value.qt;
1500 PERR (
" value type %d does not match KVP_TYPE_TIME",
1513 PERR (
" no size specified");
1520 *size_return = value->value.binary.datasize;
1521 return value->value.binary.data;
1527 PERR (
" value type %d does not match KVP_TYPE_BINARY",
1539 return value->value.list;
1542 PERR (
" value type %d does not match KVP_TYPE_GLIST",
1554 return value->value.frame;
1557 PERR (
" value type %d does not match KVP_TYPE_FRAME",
1571 PERR (
" value type %d does not match KVP_TYPE_FRAME",
1575 oldframe = value->value.frame;
1576 value->value.frame = newframe;
1588 PERR (
" value type %d does not match KVP_TYPE_GLIST",
1593 oldlist = value->value.list;
1594 value->value.list = newlist;
1606 switch (value->type)
1609 return kvp_value_new_gint64 (value->value.int64);
1612 return kvp_value_new_double (value->value.dbl);
1615 return kvp_value_new_numeric (value->value.numeric);
1618 return kvp_value_new_string (value->value.str);
1621 return kvp_value_new_guid (value->value.guid);
1628 return kvp_value_new_time (value->value.qt);
1630 #ifndef QOF_DISABLE_DEPRECATED
1632 return kvp_value_new_timespec (value->value.timespec);
1636 return kvp_value_new_binary (value->value.binary.data,
1637 value->value.binary.datasize);
1643 return kvp_value_new_frame (value->value.frame);
1658 g_hash_table_foreach (f->hash, (GHFunc) proc, data);
1672 if (kva->type < kvb->type)
1674 if (kva->type > kvb->type)
1680 if (kva->value.int64 < kvb->value.int64)
1682 if (kva->value.int64 > kvb->value.int64)
1691 kvb->value.numeric);
1694 return strcmp (kva->value.str, kvb->value.str);
1697 return guid_compare (kva->value.guid, kvb->value.guid);
1702 if (kva->value.gbool != kvb->value.gbool)
1703 return (kva->value.gbool) ? 1 : -1;
1710 #ifndef QOF_DISABLE_DEPRECATED
1712 return timespec_cmp (&(kva->value.timespec),
1713 &(kvb->value.timespec));
1717 if (kva->value.binary.datasize < kvb->value.binary.datasize)
1719 if (kva->value.binary.datasize > kvb->value.binary.datasize)
1721 return memcmp (kva->value.binary.data,
1722 kvb->value.binary.data, kva->value.binary.datasize);
1741 kvp_frame_compare_helper (
const gchar *key,
KvpValue * val, gpointer data)
1743 KvpFrameCompare *status = (KvpFrameCompare *) data;
1744 if (status->compare == 0)
1746 KvpFrame *other_frame = status->other_frame;
1747 KvpValue *other_val = kvp_frame_get_slot (other_frame, key);
1752 status->compare = 1;
1759 KvpFrameCompare status;
1770 if (!fa->hash && fb->hash)
1772 if (fa->hash && !fb->hash)
1776 status.other_frame = (
KvpFrame *) fb;
1781 if (status.compare != 0)
1782 return status.compare;
1784 status.other_frame = (
KvpFrame *) fa;
1789 return (-status.compare);
1794 binary_to_string (gconstpointer data, guint32 size)
1798 guchar *data_str = (guchar *) data;
1800 output = g_string_sized_new (size *
sizeof (gchar));
1802 for (i = 0; i < size; i++)
1804 g_string_append_printf (output,
"%02x",
1805 (guint32) (data_str[i]));
1812 kvp_value_glist_to_string (
const GList * list)
1816 const GList *cursor;
1818 tmp1 = g_strdup_printf (
"[ ");
1820 for (cursor = list; cursor; cursor = cursor->next)
1825 tmp2 = g_strdup_printf (
"%s %s,", tmp1, tmp3 ? tmp3 :
"");
1831 tmp2 = g_strdup_printf (
"%s ]", tmp1);
1838 kvp_frame_to_bare_string_helper (gpointer key __attribute__ ((unused)),
1839 gpointer value, gpointer data)
1841 gchar **str = (gchar **) data;
1843 g_strdup_printf (
"%s",
1854 g_return_val_if_fail (val, NULL);
1855 tmp1 = g_strdup (
"");
1856 switch (kvp_value_get_type (val))
1860 return g_strdup_printf (
"%" G_GINT64_FORMAT,
1866 return g_strdup_printf (
"(%g)", kvp_value_get_double (val));
1872 tmp2 = g_strdup_printf (
"%s", tmp1 ? tmp1 :
"");
1880 return g_strdup_printf (
"%s", tmp1 ? tmp1 :
"");
1886 tmp2 = g_strdup_printf (
"%s", ctmp ? ctmp :
"");
1890 #ifndef QOF_DISABLE_DEPRECATED
1894 t = timespecToTime_t (kvp_value_get_timespec (val));
1896 return qof_print_date (t);
1901 return (kvp_value_get_boolean (val)) ?
"TRUE" :
"FALSE";
1907 tmp1 = binary_to_string (data, len);
1908 return g_strdup_printf (
"%s", tmp1 ? tmp1 :
"");
1915 tmp2 = g_strdup_printf (
"%s", tmp1 ? tmp1 :
"");
1927 tmp1 = g_strdup (
"");
1928 g_hash_table_foreach (frame->hash,
1929 kvp_frame_to_bare_string_helper, &tmp1);
1935 return g_strdup_printf (
" ");
1947 g_return_val_if_fail (val, NULL);
1949 switch (kvp_value_get_type (val))
1953 return g_strdup_printf (
"KVP_VALUE_GINT64(%" G_GINT64_FORMAT
")",
1959 return g_strdup_printf (
"KVP_VALUE_DOUBLE(%g)",
1960 kvp_value_get_double (val));
1966 tmp2 = g_strdup_printf (
"KVP_VALUE_NUMERIC(%s)", tmp1 ? tmp1 :
"");
1974 return g_strdup_printf (
"KVP_VALUE_STRING(%s)", tmp1 ? tmp1 :
"");
1981 tmp2 = g_strdup_printf (
"KVP_VALUE_GUID(%s)", ctmp ? ctmp :
"");
1985 #ifndef QOF_DISABLE_DEPRECATED
1988 tmp1 = g_new0 (gchar, 40);
1989 gnc_timespec_to_iso8601_buff (kvp_value_get_timespec (val), tmp1);
1990 tmp2 = g_strdup_printf (
"KVP_VALUE_TIMESPEC(%s)", tmp1);
2001 tmp1 = binary_to_string (data, len);
2002 return g_strdup_printf (
"KVP_VALUE_BINARY(%s)",
2009 tmp2 = g_strdup_printf (
"KVP_VALUE_GLIST(%s)", tmp1 ? tmp1 :
"");
2017 tmp2 = g_strdup_printf (
"KVP_VALUE_FRAME(%s)", tmp1 ? tmp1 :
"");
2023 return g_strdup_printf (
" ");
2029 kvp_frame_to_string_helper (gpointer key, gpointer value, gpointer data)
2032 gchar **str = (gchar **) data;
2033 gchar *old_data = *str;
2037 *str = g_strdup_printf (
"%s %s => %s,\n",
2038 *str ? *str :
"", key ? (gchar *) key :
"", tmp_val ? tmp_val :
"");
2045 kvp_frame_to_string (
const KvpFrame * frame)
2049 g_return_val_if_fail (frame != NULL, NULL);
2051 tmp1 = g_strdup_printf (
"{\n");
2054 g_hash_table_foreach (frame->hash, kvp_frame_to_string_helper,
2058 tmp2 = g_strdup_printf (
"%s}\n", tmp1);
2067 kvp_frame_get_hash (
const KvpFrame * frame)
2069 g_return_val_if_fail (frame != NULL, NULL);