2006-04-07 13:49:12 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* qof-util.h -- QOF utility functions *
|
|
|
|
* *
|
|
|
|
* 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, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup Utilities
|
|
|
|
@{ */
|
2008-03-22 19:01:05 -05:00
|
|
|
/** @file qofutil.h
|
2006-04-07 13:49:12 -05:00
|
|
|
@brief QOF utility functions
|
|
|
|
@author Copyright (C) 1997 Robin D. Clark <rclark@cs.hmc.edu>
|
|
|
|
@author Copyright (C) 2000 Bill Gribble <grib@billgribble.com>
|
|
|
|
@author Copyright (C) 1997-2002,2004 Linas Vepstas <linas@linas.org>
|
|
|
|
@author Copyright 2006 Neil Williams <linux@codehelp.co.uk>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef QOF_UTIL_H
|
|
|
|
#define QOF_UTIL_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "qof.h"
|
|
|
|
#include "qoflog.h"
|
2006-05-25 19:10:35 -05:00
|
|
|
#include "qofbackend.h"
|
|
|
|
#include "qofclass.h"
|
2006-04-07 13:49:12 -05:00
|
|
|
#include "qofbook.h"
|
|
|
|
#include "qofinstance.h"
|
|
|
|
|
2023-03-03 09:58:33 -06:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2007-02-09 11:35:00 -06:00
|
|
|
#define QOF_MOD_UTIL "qof.utilities"
|
2006-04-07 13:49:12 -05:00
|
|
|
|
|
|
|
/** \name typedef enum as string macros
|
|
|
|
@{
|
|
|
|
*/
|
|
|
|
#define ENUM_BODY(name, value) \
|
|
|
|
name value,
|
|
|
|
|
|
|
|
#define AS_STRING_CASE(name, value) \
|
|
|
|
case name: { return #name; }
|
|
|
|
|
|
|
|
#define FROM_STRING_CASE(name, value) \
|
|
|
|
if (strcmp(str, #name) == 0) { \
|
|
|
|
return name; }
|
|
|
|
|
|
|
|
#define DEFINE_ENUM(name, list) \
|
|
|
|
typedef enum { \
|
|
|
|
list(ENUM_BODY) \
|
|
|
|
}name;
|
|
|
|
|
|
|
|
#define AS_STRING_DEC(name, list) \
|
|
|
|
const gchar* name##asString(name n);
|
|
|
|
|
|
|
|
#define AS_STRING_FUNC(name, list) \
|
|
|
|
const gchar* name##asString(name n) { \
|
|
|
|
switch (n) { \
|
|
|
|
list(AS_STRING_CASE) \
|
|
|
|
default: return ""; } }
|
|
|
|
|
|
|
|
#define FROM_STRING_DEC(name, list) \
|
|
|
|
name name##fromString \
|
|
|
|
(const gchar* str);
|
|
|
|
|
|
|
|
#define FROM_STRING_FUNC(name, list) \
|
|
|
|
name name##fromString \
|
|
|
|
(const gchar* str) { \
|
|
|
|
if(str == NULL) { return 0; } \
|
|
|
|
list(FROM_STRING_CASE) \
|
|
|
|
return 0; }
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** \name enum as string with no typedef
|
|
|
|
@{
|
|
|
|
|
|
|
|
Similar but used when the enum is NOT a typedef
|
|
|
|
Make sure you use the DEFINE_ENUM_NON_TYPEDEF macro.
|
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
You can precede the FROM_STRING_FUNC_NON_TYPEDEF
|
|
|
|
and AS_STRING_FUNC_NON_TYPEDEF macros with the
|
2006-04-07 13:49:12 -05:00
|
|
|
keyword static if appropriate.
|
2009-09-18 14:40:57 -05:00
|
|
|
|
2006-04-07 13:49:12 -05:00
|
|
|
ENUM_BODY is used in both types.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFINE_ENUM_NON_TYPEDEF(name, list) \
|
|
|
|
enum name { \
|
|
|
|
list(ENUM_BODY) \
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FROM_STRING_DEC_NON_TYPEDEF(name, list) \
|
|
|
|
void name##fromString \
|
|
|
|
(const gchar* str, enum name *type);
|
|
|
|
|
|
|
|
#define FROM_STRING_CASE_NON_TYPEDEF(name, value) \
|
|
|
|
if (strcmp(str, #name) == 0) { *type = name; }
|
|
|
|
|
|
|
|
#define FROM_STRING_FUNC_NON_TYPEDEF(name, list) \
|
|
|
|
void name##fromString \
|
|
|
|
(const gchar* str, enum name *type) { \
|
|
|
|
if(str == NULL) { return; } \
|
|
|
|
list(FROM_STRING_CASE_NON_TYPEDEF) }
|
|
|
|
|
|
|
|
#define AS_STRING_DEC_NON_TYPEDEF(name, list) \
|
|
|
|
const gchar* name##asString(enum name n);
|
|
|
|
|
|
|
|
#define AS_STRING_FUNC_NON_TYPEDEF(name, list) \
|
|
|
|
const gchar* name##asString(enum name n) { \
|
|
|
|
switch (n) { \
|
|
|
|
list(AS_STRING_CASE_NON_TYPEDEF) \
|
|
|
|
default: return ""; } }
|
|
|
|
|
|
|
|
#define AS_STRING_CASE_NON_TYPEDEF(name, value) \
|
|
|
|
case name: { return #name; }
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** @name Convenience wrappers
|
|
|
|
@{
|
|
|
|
*/
|
2009-09-18 14:40:57 -05:00
|
|
|
|
|
|
|
/** \brief Initialise the Query Object Framework
|
2006-04-07 13:49:12 -05:00
|
|
|
|
2014-07-25 16:38:33 -05:00
|
|
|
Use in place of separate init functions (like qof_query_init(),
|
|
|
|
etc.) to protect against future changes.
|
2006-04-07 13:49:12 -05:00
|
|
|
*/
|
|
|
|
void qof_init (void);
|
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
/** \brief Safely close down the Query Object Framework
|
2006-04-07 13:49:12 -05:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
Use in place of separate close / shutdown functions
|
2014-07-25 16:38:33 -05:00
|
|
|
(like qof_query_shutdown(), etc.) to protect
|
2006-04-07 13:49:12 -05:00
|
|
|
against future changes.
|
|
|
|
*/
|
|
|
|
void qof_close (void);
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/* **** Prototypes *********************************************/
|
|
|
|
|
Bug #638225: Sort when saving as XML
Patch by Jim Radford (with code beautified and re-indented by myself):
The attached patches sort the slots, lots, book accounts, bill terms,
customers, employees, entries, invoices, jobs, orders, tax tables and
vendors before saving them to the GnuCash XML file.
This is an attempt to make saves more idempotent thereby facilitating
the use of a revision control system on the GnuCash XML files.
With these patches most of the needless and seemingly random churn is
gone and I can add or remove a transaction and expect
there to be no unrelated changes to the GnuCash file.
I've been using and refining this patches for the last few years, so it has
received quite a bit of testing.
David Fraser adds: Without specific testing, I'm using this on an average-sized gnucash file
(5.7MB) without noticing any particular slowdown in saving, but a wonderful
reduction in diffs when comparing changes.
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@20067 57a11ea4-9604-0410-9ed3-97b8803252fd
2011-01-11 14:40:19 -06:00
|
|
|
/** Calls the given function for each of the key/value pairs in the
|
|
|
|
* GHashTable in an order determined by the GCompareFunc applied to
|
|
|
|
* the keys. The function is passed the key and value of each pair,
|
|
|
|
* and the given user_data parameter. */
|
|
|
|
void g_hash_table_foreach_sorted(GHashTable *hash_table, GHFunc func, gpointer user_data, GCompareFunc compare_func);
|
|
|
|
|
2017-01-16 16:03:50 -06:00
|
|
|
/** Search for an occurrence of the substring needle in the string
|
2007-10-02 20:18:13 -05:00
|
|
|
* haystack, ignoring case. Return TRUE if one is found or FALSE
|
|
|
|
* otherwise. */
|
|
|
|
gboolean qof_utf8_substr_nocase (const gchar *haystack, const gchar *needle);
|
|
|
|
|
2006-04-07 13:49:12 -05:00
|
|
|
/** case sensitive comparison of strings da and db - either
|
|
|
|
may be NULL. A non-NULL string is greater than a NULL string.
|
2009-09-18 14:40:57 -05:00
|
|
|
|
2006-04-07 13:49:12 -05:00
|
|
|
@param da string 1.
|
|
|
|
@param db string 2.
|
2009-09-18 14:40:57 -05:00
|
|
|
|
2006-04-07 13:49:12 -05:00
|
|
|
@return If da == NULL && db != NULL, returns -1.
|
|
|
|
If da != NULL && db == NULL, returns +1.
|
2009-09-18 14:40:57 -05:00
|
|
|
If da != NULL && db != NULL, returns the result of
|
2006-04-07 13:49:12 -05:00
|
|
|
strcmp(da, db).
|
2009-09-18 14:40:57 -05:00
|
|
|
If da == NULL && db == NULL, returns 0.
|
2006-04-07 13:49:12 -05:00
|
|
|
*/
|
|
|
|
gint safe_strcasecmp (const gchar * da, const gchar * db);
|
|
|
|
|
|
|
|
/** The null_strcmp compares strings a and b the same way that strcmp()
|
|
|
|
* does, except that either may be null. This routine assumes that
|
|
|
|
* a null string is equal to the empty string.
|
|
|
|
*/
|
|
|
|
gint null_strcmp (const gchar * da, const gchar * db);
|
|
|
|
|
|
|
|
/** The ultostr() subroutine is the inverse of strtoul(). It accepts a
|
|
|
|
* number and prints it in the indicated base. The returned string
|
|
|
|
* should be g_freed when done. */
|
|
|
|
gchar * ultostr (gulong val, gint base);
|
|
|
|
|
|
|
|
/** Returns true if string s is a number, possibly surrounded by
|
|
|
|
* whitespace. */
|
2009-07-24 15:07:14 -05:00
|
|
|
gboolean gnc_strisnum(const gchar *s);
|
2006-04-07 13:49:12 -05:00
|
|
|
|
|
|
|
#ifndef HAVE_STPCPY
|
|
|
|
#define stpcpy g_stpcpy
|
|
|
|
#endif
|
|
|
|
|
2007-04-30 12:06:48 -05:00
|
|
|
/** begin_edit
|
2006-04-07 13:49:12 -05:00
|
|
|
*
|
|
|
|
* @param inst: an instance of QofInstance
|
|
|
|
*
|
|
|
|
* The caller should use this macro first and then perform any other operations.
|
|
|
|
*/
|
|
|
|
gboolean qof_begin_edit(QofInstance *inst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* commit_edit helpers
|
|
|
|
*
|
2009-09-18 14:40:57 -05:00
|
|
|
* The caller should call PART1 as the first thing, then
|
2006-04-07 13:49:12 -05:00
|
|
|
* perform any local operations prior to calling the backend.
|
2009-09-18 14:40:57 -05:00
|
|
|
* Then call PART2.
|
2006-04-07 13:49:12 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* part1 -- deal with the editlevel
|
2009-09-18 14:40:57 -05:00
|
|
|
*
|
2006-04-07 13:49:12 -05:00
|
|
|
* @param inst: an instance of QofInstance
|
|
|
|
*/
|
|
|
|
gboolean qof_commit_edit(QofInstance *inst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* part2 -- deal with the backend
|
2009-09-18 14:40:57 -05:00
|
|
|
*
|
2006-04-07 13:49:12 -05:00
|
|
|
* @param inst: an instance of QofInstance
|
|
|
|
* @param on_error: a function called if there is a backend error.
|
|
|
|
* void (*on_error)(inst, QofBackendError)
|
2009-09-18 14:40:57 -05:00
|
|
|
* @param on_done: a function called after the commit is completed
|
2006-04-07 13:49:12 -05:00
|
|
|
* successfully for an object which remained valid.
|
|
|
|
* void (*on_done)(inst)
|
|
|
|
* @param on_free: a function called if the commit succeeded and the instance
|
2009-09-18 14:40:57 -05:00
|
|
|
* is to be freed.
|
2006-04-07 13:49:12 -05:00
|
|
|
* void (*on_free)(inst)
|
2009-09-18 14:40:57 -05:00
|
|
|
*
|
2006-04-07 13:49:12 -05:00
|
|
|
* Note that only *one* callback will be called (or zero, if that
|
|
|
|
* callback is NULL). In particular, 'on_done' will not be called for
|
|
|
|
* an object which is to be freed.
|
|
|
|
*
|
|
|
|
* Returns TRUE, if the commit succeeded, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
gboolean
|
2009-09-18 14:40:57 -05:00
|
|
|
qof_commit_edit_part2(QofInstance *inst,
|
|
|
|
void (*on_error)(QofInstance *, QofBackendError),
|
|
|
|
void (*on_done)(QofInstance *),
|
2006-04-07 13:49:12 -05:00
|
|
|
void (*on_free)(QofInstance *));
|
2009-09-18 14:40:57 -05:00
|
|
|
|
2014-04-25 15:41:11 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-07 13:49:12 -05:00
|
|
|
#endif /* QOF_UTIL_H */
|
|
|
|
/** @} */
|