QOF  0.7.5
qofutil.h
Go to the documentation of this file.
1 /********************************************************************\
2  * qofutil.h -- QOF utility functions *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20 \********************************************************************/
21 
32 #ifndef QOF_UTIL_H
33 #define QOF_UTIL_H
34 
35 #include <stddef.h>
36 #include "qoflog.h"
37 #include "qofdate.h"
38 #include "qofutil.h"
39 #include "qofbackend-p.h"
40 #include "qofbook.h"
41 #include "qofinstance.h"
42 
44 #if HAVE_SCANF_LLD
45 # define QOF_SCANF_LLD "%lld"
46 #else
47 # define QOF_SCANF_LLD "%qd"
48 #endif
49 
50 #define QOF_MOD_UTIL "qof-utilities"
51 
55 #define ENUM_BODY(name, value) \
56  name value,
57 
58 #define AS_STRING_CASE(name, value) \
59  case name: { return #name; }
60 
61 #define FROM_STRING_CASE(name, value) \
62  if (strcmp(str, #name) == 0) { \
63  return name; }
64 
65 #define DEFINE_ENUM(name, list) \
66  typedef enum { \
67  list(ENUM_BODY) \
68  }name;
69 
70 #define AS_STRING_DEC(name, list) \
71  const gchar* name##asString(name n);
72 
73 #define AS_STRING_FUNC(name, list) \
74  const gchar* name##asString(name n) { \
75  switch (n) { \
76  list(AS_STRING_CASE) \
77  default: return ""; } }
78 
79 #define FROM_STRING_DEC(name, list) \
80  name name##fromString \
81  (const gchar* str);
82 
83 #define FROM_STRING_FUNC(name, list) \
84  name name##fromString \
85  (const gchar* str) { \
86  if(str == NULL) { return 0; } \
87  list(FROM_STRING_CASE) \
88  return 0; }
89 
105 #define DEFINE_ENUM_NON_TYPEDEF(name, list) \
106  enum name { \
107  list(ENUM_BODY) \
108  };
109 
110 #define FROM_STRING_DEC_NON_TYPEDEF(name, list) \
111  void name##fromString \
112  (const gchar* str, enum name *type);
113 
114 #define FROM_STRING_CASE_NON_TYPEDEF(name, value) \
115  if (strcmp(str, #name) == 0) { *type = name; }
116 
117 #define FROM_STRING_FUNC_NON_TYPEDEF(name, list) \
118  void name##fromString \
119  (const gchar* str, enum name *type) { \
120  if(str == NULL) { return; } \
121  list(FROM_STRING_CASE_NON_TYPEDEF) }
122 
123 #define AS_STRING_DEC_NON_TYPEDEF(name, list) \
124  const gchar* name##asString(enum name n);
125 
126 #define AS_STRING_FUNC_NON_TYPEDEF(name, list) \
127  const gchar* name##asString(enum name n) { \
128  switch (n) { \
129  list(AS_STRING_CASE_NON_TYPEDEF) \
130  default: return ""; } }
131 
132 #define AS_STRING_CASE_NON_TYPEDEF(name, value) \
133  case name: { return #name; }
134 
146 void qof_init (void);
147 
154 void qof_close (void);
155 
158 /* **** Prototypes *********************************************/
159 
173 gint safe_strcmp (const gchar * da, const gchar * db);
174 
187 gint safe_strcasecmp (const gchar * da, const gchar * db);
188 
193 gint null_strcmp (const gchar * da, const gchar * db);
194 
198 extern gchar *strncasestr (const guchar * str1, const guchar * str2,
199  size_t len);
200 
201 extern gchar *strcasestr (const gchar * str1, const gchar * str2);
202 
206 gchar *ultostr (gulong val, gint base);
207 
210 gboolean qof_util_string_isnum (const guchar * s);
211 
213 gint
214 qof_util_double_compare (gdouble v1, gdouble v2);
215 
216 #ifndef HAVE_STPCPY
217 
218 #define stpcpy g_stpcpy
219 #endif
220 
224 const gchar *qof_util_whitespace_filter (const gchar * val);
225 
229 gint qof_util_bool_to_int (const gchar * val);
230 
239 gchar *
240 qof_util_param_to_string (QofEntity * ent, const QofParam * param);
241 
262 gboolean
263 qof_util_param_set_string (QofEntity * ent, const QofParam * param,
264  const gchar * value_string);
265 
275 gchar *
276 qof_util_make_utf8 (gchar * string);
277 
309 
313 void qof_util_string_cache_remove (gconstpointer key);
314 
318 gpointer qof_util_string_cache_insert (gconstpointer key);
319 
320 #define CACHE_INSERT(str) qof_util_string_cache_insert((gconstpointer)(str))
321 #define CACHE_REMOVE(str) qof_util_string_cache_remove((str))
322 
323 /* Replace cached string currently in 'dst' with string in 'src'.
324  * Typical usage:
325  * void foo_set_name(Foo *f, const char *str) {
326  * CACHE_REPLACE(f->name, str);
327  * }
328  * It avoids unnecessary ejection by doing INSERT before REMOVE.
329 */
330 #define CACHE_REPLACE(dst, src) do { \
331  gpointer tmp = CACHE_INSERT((src)); \
332  CACHE_REMOVE((dst)); \
333  (dst) = tmp; \
334  } while (0)
335 
336 #define QOF_CACHE_NEW(void) qof_util_string_cache_insert("")
337 
369 gboolean
370 qof_util_param_edit (QofInstance * inst, const QofParam * param);
371 
386 gboolean
387 qof_util_param_commit (QofInstance * inst, const QofParam * param);
388 
389 #endif /* QOF_UTIL_H */
390