GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
kvp-scm.c
1 #include "config.h"
2 
3 #include "qof.h"
4 #include <libguile.h>
5 #include "engine-helpers-guile.h"
6 
7 #include "kvp-scm.h"
8 #include "guile-mappings.h"
9 #include "gnc-guile-utils.h"
10 #include "swig-runtime.h"
11 
12 /* NOTE: There are some problems with this approach. Currently,
13  * guids are stored simply as strings in scheme, so some
14  * strings could be mistaken for guids, although that is
15  * unlikely. The general problem is distinguishing kvp
16  * types based only on the scheme type.
17  */
18 
19 KvpValue *
20 gnc_scm_to_kvp_value_ptr(SCM val)
21 {
22  if (scm_is_number(val))
23  {
24  /* in guile 1.8 (exact? ) only works on numbers */
25  if (scm_is_exact (val) && gnc_gh_gint64_p(val))
26  {
27  return kvp_value_new_gint64(scm_to_int64(val));
28  }
29  else
30  {
31  return kvp_value_new_double(scm_to_double(val));
32  }
33  }
34  else if (gnc_numeric_p(val))
35  {
36  return kvp_value_new_gnc_numeric(gnc_scm_to_numeric(val));
37  }
38  else if (gnc_guid_p(val))
39  {
40  GncGUID tmpguid = gnc_scm2guid(val);
41  return kvp_value_new_guid(&tmpguid);
42  }
43  else if (gnc_timepair_p(val))
44  {
45  Timespec ts = gnc_timepair2timespec(val);
46  return kvp_value_new_timespec(ts);
47  }
48  else if (scm_is_string(val))
49  {
50  gchar *newstr;
51  KvpValue *ret;
52  newstr = gnc_scm_to_utf8_string (val);
53  ret = kvp_value_new_string(newstr);
54  g_free (newstr);
55  return ret;
56  }
57  else if (SWIG_IsPointerOfType(val, SWIG_TypeQuery("_p_KvpFrame")))
58  {
59 #define FUNC_NAME G_STRFUNC
60  KvpFrame *frame = SWIG_MustGetPtr(val, SWIG_TypeQuery("_p_KvpFrame"),
61  1, 0);
62 #undef FUNC_NAME
63  return kvp_value_new_frame (frame);
64  }
65  /* FIXME: add list handler here */
66  return NULL;
67 }
68 
69 SCM
70 gnc_kvp_value_ptr_to_scm(KvpValue* val)
71 {
72  const gchar *string;
73  switch (kvp_value_get_type(val))
74  {
75  case KVP_TYPE_GINT64:
76  return scm_from_int64(kvp_value_get_gint64(val));
77  break;
78  case KVP_TYPE_DOUBLE:
79  return scm_from_double (kvp_value_get_double(val));
80  break;
81  case KVP_TYPE_NUMERIC:
82  return gnc_numeric_to_scm(kvp_value_get_numeric(val));
83  break;
84  case KVP_TYPE_STRING:
85  string = kvp_value_get_string(val);
86  return string ? scm_from_utf8_string(string) : SCM_BOOL_F;
87  break;
88  case KVP_TYPE_GUID:
89  {
90  GncGUID *tempguid = kvp_value_get_guid(val);
91  return gnc_guid2scm(*tempguid);
92  }
93  break;
94  case KVP_TYPE_TIMESPEC:
95  return gnc_timespec2timepair(kvp_value_get_timespec(val));
96  break;
97 
98  case KVP_TYPE_FRAME:
99  {
100  KvpFrame *frame = kvp_value_get_frame(val);
101  if (frame)
102  return SWIG_NewPointerObj(frame, SWIG_TypeQuery("_p_KvpFrame"), 0);
103  }
104  break;
105  case KVP_TYPE_GDATE:
106  return gnc_timespec2timepair(gdate_to_timespec(kvp_value_get_gdate(val)));
107 
108  /* FIXME: handle types below */
109  case KVP_TYPE_GLIST:
110  default:
111  break;
112  }
113  return SCM_BOOL_F;
114 }
115 
116 void
117 gnc_kvp_frame_delete_at_path (KvpFrame *frame, GSList *key_path)
118 {
119  kvp_frame_set_slot_path_gslist (frame, NULL, key_path);
120 }
void kvp_frame_set_slot_path_gslist(KvpFrame *frame, KvpValue *value, GSList *key_path)
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
Definition: guid.h:65
gint64 kvp_value_get_gint64(const KvpValue *value)
char * kvp_value_get_string(const KvpValue *value)
#define kvp_value_new_gnc_numeric
Definition: kvp_frame.h:466
GncGUID * kvp_value_get_guid(const KvpValue *value)
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
GDate kvp_value_get_gdate(const KvpValue *value)
KvpFrame * kvp_value_get_frame(const KvpValue *value)
Timespec gdate_to_timespec(GDate d)
struct KvpValueImpl KvpValue
Definition: kvp_frame.h:80