37 #include "qofundo-p.h" 45 strncasestr (
const guchar * str1,
const guchar * str2,
size_t len)
47 while (*str1 && len--)
49 if (toupper (*str1) == toupper (*str2))
52 l = strlen ((gchar*)str2);
53 if (strncasecmp ((gchar*)str1, (gchar*)str2, l) == 0)
54 return (gchar *) str1;
61 #ifndef HAVE_STRCASESTR 65 strcasestr (
const gchar * str1,
const gchar * str2)
67 size_t len = strlen (str1);
81 gint retval = strcmp ((da), (db));
87 else if ((!(da)) && (db))
89 else if ((da) && (!(db)))
101 gint retval = strcasecmp ((da), (db));
107 else if ((!(da)) && (db))
109 else if ((da) && (!(db)))
118 return strcmp (da, db);
119 if (!da && db && 0 == db[0])
121 if (!db && da && 0 == da[0])
130 #define MAX_DIGITS 50 136 gchar buf[MAX_DIGITS];
137 gulong broke[MAX_DIGITS];
139 gulong places = 0, reval;
141 if ((2 > base) || (36 < base))
146 for (i = 0; i < MAX_DIGITS; i++)
157 for (i = places - 2; i >= 0; i--)
159 reval += broke[i + 1];
165 for (i = 0; i < (gint) places; i++)
169 buf[places - 1 - i] = 0x30 + broke[i];
173 buf[places - 1 - i] = 0x41 - 10 + broke[i];
178 return g_strdup (buf);
184 if (isnan (d1) && isnan (d2))
205 while (*s && isspace (*s))
213 while (*s && isdigit (*s))
219 while (*s && isspace (*s))
240 len = strspn (val,
"\a\b\t\n\v\f\r ");
265 if (strstr (p,
"true"))
267 if (strstr (p,
"TRUE"))
269 if (strstr (p,
"yes"))
271 if (strstr (p,
"YES"))
289 if (1 < inst->editlevel)
291 if (0 >= inst->editlevel)
298 if (undo_data->undo_operation_open)
301 if (be && qof_backend_begin_exists (be))
302 qof_backend_run_begin (be, inst);
317 if (0 < inst->editlevel)
321 if (be && qof_backend_commit_exists (be))
322 qof_backend_run_commit (be, inst);
326 if (undo_data->undo_operation_open)
339 if (g_utf8_validate (
string, -1, NULL))
341 value = g_locale_to_utf8 (
string, -1, NULL, NULL, NULL);
344 PWARN (
" unable to convert from locale %s",
string);
345 PINFO (
"trying to convert from ISO-8859-15.");
346 value = g_convert (
string, -1,
"UTF-8",
"ISO-8859-15",
350 PERR (
" conversion failed");
363 GHashTable *key_table;
364 GHashTable *value_table;
367 static QStrCache *qof_string_cache = NULL;
374 static inline QStrCacheNode* g_cache_node_new (gpointer value) {
375 QStrCacheNode *node = g_slice_new (QStrCacheNode);
381 static inline void g_cache_node_destroy (QStrCacheNode *node) {
382 g_slice_free (QStrCacheNode, node);
385 static QStrCache* qof_cache_new (
void) {
387 cache = g_slice_new (QStrCache);
388 cache->key_table = g_hash_table_new (g_str_hash, g_str_equal);
389 cache->value_table = g_hash_table_new (g_str_hash, NULL);
393 static void qof_cache_destroy (QStrCache *cache) {
394 g_return_if_fail (cache != NULL);
395 g_hash_table_destroy (cache->key_table);
396 g_hash_table_destroy (cache->value_table);
397 g_slice_free (QStrCache, cache);
400 static gpointer qof_cache_insert (QStrCache *cache, gpointer key) {
404 g_return_val_if_fail (cache != NULL, NULL);
405 node = g_hash_table_lookup (cache->key_table, key);
407 node->ref_count += 1;
410 key = g_strdup (key);
411 value = g_strdup (key);
412 node = g_cache_node_new (value);
413 g_hash_table_insert (cache->key_table, key, node);
414 g_hash_table_insert (cache->value_table, value, key);
418 static void qof_cache_remove (QStrCache *cache, gconstpointer value) {
422 g_return_if_fail (cache != NULL);
423 key = g_hash_table_lookup (cache->value_table, value);
424 node = g_hash_table_lookup (cache->key_table, key);
425 g_return_if_fail (node != NULL);
426 node->ref_count -= 1;
427 if (node->ref_count == 0) {
428 g_hash_table_remove (cache->value_table, value);
429 g_hash_table_remove (cache->key_table, key);
431 g_free (node->value);
432 g_cache_node_destroy (node);
436 static QStrCache * qof_util_get_string_cache (
void) {
437 if (!qof_string_cache) {
438 qof_string_cache = qof_cache_new ();
440 return qof_string_cache;
444 if (qof_string_cache) {
445 qof_cache_destroy (qof_string_cache);
447 qof_string_cache = NULL;
452 qof_cache_remove (qof_util_get_string_cache (), key);
458 return qof_cache_insert(qof_util_get_string_cache(), (gpointer)key);
470 const GUID *param_guid;
472 gdouble param_double, (*double_getter) (QofEntity *,
const QofParam *);
473 gboolean param_boolean, (*boolean_getter) (QofEntity *,
const QofParam *);
474 gint32 param_i32, (*int32_getter) (QofEntity *,
const QofParam *);
475 gint64 param_i64, (*int64_getter) (QofEntity *,
const QofParam *);
476 gchar param_char, (*char_getter) (QofEntity *,
const QofParam *);
480 g_return_val_if_fail (ent && param, NULL);
481 paramType = param->param_type;
484 param_string = g_strdup (param->param_getfcn (ent, param));
485 if (param_string == NULL)
486 param_string = g_strdup(
"");
494 param_qt = param->param_getfcn (ent, param);
498 if ((
safe_strcmp (paramType, QOF_TYPE_NUMERIC) == 0) ||
503 param_numeric = numeric_getter (ent, param);
510 param_guid = param->param_getfcn (ent, param);
512 param_string = g_strdup (param_sa);
519 (gint32 (*)(QofEntity *,
const QofParam *)) param->param_getfcn;
520 param_i32 = int32_getter (ent, param);
521 param_string = g_strdup_printf (
"%d", param_i32);
528 (gint64 (*)(QofEntity *,
const QofParam *)) param->param_getfcn;
529 param_i64 = int64_getter (ent, param);
530 param_string = g_strdup_printf (
"%" G_GINT64_FORMAT, param_i64);
537 (double (*)(QofEntity *,
const QofParam *)) param->param_getfcn;
538 param_double = double_getter (ent, param);
539 param_string = g_strdup_printf (
"%f", param_double);
543 if (
safe_strcmp (paramType, QOF_TYPE_BOOLEAN) == 0)
546 (gboolean (*)(QofEntity *,
const QofParam *)) param->param_getfcn;
547 param_boolean = boolean_getter (ent, param);
549 if (param_boolean == TRUE)
551 param_string = g_strdup (
"true");
555 param_string = g_strdup (
"false");
564 frame = param->param_getfcn (ent, param);
568 GHashTable *hash = kvp_frame_get_hash (frame);
569 param_string = g_strdup_printf (
"%s(%d)", QOF_TYPE_KVP,
570 g_hash_table_size (hash));
576 param_string = g_strdup(
"");
583 (gchar (*)(QofEntity *,
const QofParam *)) param->param_getfcn;
584 param_char = char_getter (ent, param);
586 return g_strdup_printf (
"%c", param_char);
592 col = param->param_getfcn (ent, param);
594 return g_strdup_printf (
"%s(%d)",
599 QofEntity *child = NULL;
600 child = param->param_getfcn (ent, param);
612 book = param->param_getfcn (ent, param);
613 PINFO (
" book param %p", book);
616 PINFO (
" backend=%p", be);
621 param_string = g_strdup (be->
fullpath);
622 PINFO (
" fullpath=%s", param_string);
629 PINFO (
" book GUID=%s", param_sa);
630 param_string = g_strdup (param_sa);
635 QofEntity *child = NULL;
636 child = param->param_getfcn (ent, param);
643 return g_strdup (
"");
648 const gchar * value_string)
650 void (*string_setter) (
QofEntity *,
const gchar *);
654 void (*double_setter) (
QofEntity *, gdouble);
655 void (*boolean_setter) (
QofEntity *, gboolean);
656 void (*i32_setter) (
QofEntity *, gint32);
657 void (*i64_setter) (
QofEntity *, gint64);
658 void (*char_setter) (
QofEntity *, gchar);
663 g_return_val_if_fail (ent, FALSE);
664 g_return_val_if_fail (param, FALSE);
665 g_return_val_if_fail (value_string, FALSE);
667 if (
safe_strcmp (param->param_type, QOF_TYPE_STRING) == 0)
671 const gchar *)) param->param_setfcn;
672 if (string_setter != NULL)
673 string_setter (ent, value_string);
676 if (
safe_strcmp (param->param_type, QOF_TYPE_TIME) == 0)
688 if ((time_setter != NULL) && (qof_time_is_valid (qt)))
689 time_setter (ent, qt);
693 if ((
safe_strcmp (param->param_type, QOF_TYPE_NUMERIC) == 0) ||
694 (
safe_strcmp (param->param_type, QOF_TYPE_DEBCRED) == 0))
703 if (numeric_setter != NULL)
704 numeric_setter (ent, num);
707 if (
safe_strcmp (param->param_type, QOF_TYPE_GUID) == 0)
715 const GUID *)) param->param_setfcn;
718 if (guid_setter != NULL)
719 guid_setter (ent, guid);
722 if (
safe_strcmp (param->param_type, QOF_TYPE_INT32) == 0)
729 (void (*)(
QofEntity *, gint32)) param->param_setfcn;
731 (gint32) strtol (value_string, &tail, 0);
732 if ((i32_setter != NULL) && (errno == 0))
734 i32_setter (ent, i32);
737 if (
safe_strcmp (param->param_type, QOF_TYPE_INT64) == 0)
743 i64 = strtoll (value_string, &tail, 0);
745 (void (*)(
QofEntity *, gint64)) param->param_setfcn;
746 if ((i64_setter != NULL) && (errno == 0))
747 i64_setter (ent, i64);
750 if (
safe_strcmp (param->param_type, QOF_TYPE_DOUBLE) == 0)
756 db = strtod (value_string, &tail);
758 (void (*)(
QofEntity *, gdouble)) param->param_setfcn;
759 if ((double_setter != NULL) && (errno == 0))
760 double_setter (ent, db);
763 if (
safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN) == 0)
766 gboolean G_GNUC_UNUSED b;
769 (void (*)(
QofEntity *, gboolean)) param->param_setfcn;
771 if ((val > 1) || (val < 0))
773 b = (val == 1) ? TRUE : FALSE;
774 if (boolean_setter != NULL)
775 boolean_setter (ent, val);
778 if (
safe_strcmp (param->param_type, QOF_TYPE_KVP) == 0)
791 if (
safe_strcmp (param->param_type, QOF_TYPE_CHAR) == 0)
794 (void (*)(
QofEntity *, gchar)) param->param_setfcn;
795 if (char_setter != NULL)
796 char_setter (ent, value_string[0]);
831 qof_util_get_string_cache ();
834 qof_object_initialize ();
842 qof_query_shutdown ();
843 qof_object_shutdown ();
gchar * qof_util_make_utf8(gchar *string)
Convert strings received from the wrapped objects into UTF-8.
const gchar * qof_util_whitespace_filter(const gchar *val)
QofNumericErrorCode qof_numeric_check(QofNumeric in)
#define QOF_DATE_FORMAT_UTC
QOF UTC format, xsd:date compatible. QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ".
#define PERR(format, args...)
#define QOF_TYPE_COLLECT
secondary collections are used for one-to-many references between entities and are implemented using ...
#define PINFO(format, args...)
struct _QofNumeric QofNumeric
A rational-number type.
void qof_date_free(QofDate *date)
gboolean qof_book_register(void)
QofDate * qof_date_from_qtime(const QofTime *qt)
gboolean string_to_guid(const gchar *string, GUID *guid)
void qof_date_close(void)
close down the QofDate tables
struct _KvpFrame KvpFrame
gint safe_strcasecmp(const gchar *da, const gchar *db)
gchar * qof_date_print(const QofDate *date, QofDateFormat df)
Convert a QofDate to a timestamp according to the specified date format.
gint qof_util_bool_to_int(const gchar *val)
gboolean kvp_frame_is_empty(KvpFrame *frame)
#define GUID_ENCODING_LENGTH
Private QofBook interface.
QofTime * qof_date_to_qtime(const QofDate *qd)
Full range replacement for struct tm.
struct QofCollection_s QofCollection
void qof_date_init(void)
initialise the QofDate tables
gboolean qof_util_param_edit(QofInstance *inst, const QofParam *param)
Prepare to edit a parameter.
QofDate * qof_date_parse(const gchar *str, QofDateFormat df)
Convert a timestamp to a QofTime.
gpointer qof_util_string_cache_insert(gconstpointer key)
guint qof_collection_count(QofCollection *col)
void qof_query_init(void)
void qof_util_string_cache_remove(gconstpointer key)
gboolean qof_numeric_from_string(const gchar *str, QofNumeric *n)
const gchar * qof_object_printable(QofIdTypeConst type_name, gpointer obj)
void qof_util_string_cache_destroy(void)
gchar * qof_util_param_to_string(QofEntity *ent, const QofParam *param)
Converts a parameter to a string for storage or display.
gint null_strcmp(const gchar *da, const gchar *db)
gchar * guid_to_string_buff(const GUID *guid, gchar *buff)
gboolean qof_util_param_commit(QofInstance *inst, const QofParam *param)
Commit this parameter change, with undo support.
gchar * ultostr(gulong val, gint base)
gboolean qof_util_param_set_string(QofEntity *ent, const QofParam *param, const gchar *value_string)
Set a parameter from a value string.
QofIdType qof_collection_get_type(QofCollection *col)
gchar * qof_numeric_to_string(QofNumeric n)
void guid_new(GUID *guid)
void qof_close(void)
Safely close down the Query Object Framework.
#define PWARN(format, args...)
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
const GUID * qof_entity_get_guid(QofEntity *ent)
gboolean qof_util_string_isnum(const guchar *s)
gint qof_util_double_compare(gdouble d1, gdouble d2)
Compare two gdouble values.
QofBackend * qof_book_get_backend(QofBook *book)
Retrieve the backend used by this book.
gint safe_strcmp(const gchar *da, const gchar *db)
void qof_undo_commit(QofInstance *instance, const QofParam *param)
gchar * strncasestr(const guchar *str1, const guchar *str2, size_t len)
#define QOF_TYPE_CHOICE
Identify an object as containing a choice.
void qof_init(void)
Initialise the Query Object Framework.
void qof_undo_modify(QofInstance *instance, const QofParam *param)
const gchar * QofLogModule