QOF 0.8.4
qofutil.h
Go to the documentation of this file.
00001 /********************************************************************\
00002  * qofutil.h -- QOF utility functions                              *
00003  *                                                                  *
00004  * This program is free software; you can redistribute it and/or    *
00005  * modify it under the terms of the GNU General Public License as   *
00006  * published by the Free Software Foundation; either version 2 of   *
00007  * the License, or (at your option) any later version.              *
00008  *                                                                  *
00009  * This program is distributed in the hope that it will be useful,  *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00012  * GNU General Public License for more details.                     *
00013  *                                                                  *
00014  * You should have received a copy of the GNU General Public License*
00015  * along with this program; if not, contact:                        *
00016  *                                                                  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942       *
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00020 \********************************************************************/
00021 
00032 #ifndef QOF_UTIL_H
00033 #define QOF_UTIL_H
00034 
00035 #include <stddef.h>
00036 #include "qoflog.h"
00037 #include "qofdate.h"
00038 #include "qofutil.h"
00039 #include "qofbackend-p.h"
00040 #include "qofbook.h"
00041 #include "qofinstance.h"
00042 
00043 #define QOF_MOD_UTIL "qof-utilities"
00044 
00048 #define ENUM_BODY(name, value)           \
00049     name value,
00050 
00051 #define AS_STRING_CASE(name, value)      \
00052     case name: { return #name; }
00053 
00054 #define FROM_STRING_CASE(name, value)    \
00055     if (strcmp(str, #name) == 0) {       \
00056         return name;  }
00057 
00058 #define DEFINE_ENUM(name, list)          \
00059     typedef enum {                       \
00060         list(ENUM_BODY)                  \
00061     }name;
00062 
00063 #define AS_STRING_DEC(name, list)        \
00064     const gchar* name##asString(name n);
00065 
00066 #define AS_STRING_FUNC(name, list)        \
00067     const gchar* name##asString(name n) { \
00068         switch (n) {                      \
00069             list(AS_STRING_CASE)          \
00070             default: return "";  } }
00071 
00072 #define FROM_STRING_DEC(name, list)      \
00073     name name##fromString                \
00074     (const gchar* str);
00075 
00076 #define FROM_STRING_FUNC(name, list)     \
00077     name name##fromString                \
00078     (const gchar* str) {                 \
00079     if(str == NULL) { return 0; }        \
00080         list(FROM_STRING_CASE)           \
00081         return 0;  }
00082 
00098 #define DEFINE_ENUM_NON_TYPEDEF(name, list)   \
00099     enum name {                               \
00100         list(ENUM_BODY)                       \
00101     };
00102 
00103 #define FROM_STRING_DEC_NON_TYPEDEF(name, list)   \
00104    void name##fromString                          \
00105    (const gchar* str, enum name *type);
00106 
00107 #define FROM_STRING_CASE_NON_TYPEDEF(name, value) \
00108    if (strcmp(str, #name) == 0) { *type = name; }
00109 
00110 #define FROM_STRING_FUNC_NON_TYPEDEF(name, list)  \
00111    void name##fromString                          \
00112    (const gchar* str, enum name *type) {          \
00113    if(str == NULL) { return; }                    \
00114     list(FROM_STRING_CASE_NON_TYPEDEF) }
00115 
00116 #define AS_STRING_DEC_NON_TYPEDEF(name, list)     \
00117    const gchar* name##asString(enum name n);
00118 
00119 #define AS_STRING_FUNC_NON_TYPEDEF(name, list)    \
00120    const gchar* name##asString(enum name n) {     \
00121        switch (n) {                               \
00122            list(AS_STRING_CASE_NON_TYPEDEF)       \
00123            default: return ""; } }
00124 
00125 #define AS_STRING_CASE_NON_TYPEDEF(name, value)   \
00126    case name: { return #name; }
00127 
00139 void qof_init (void);
00140 
00147 void qof_close (void);
00148 
00151 /* **** Prototypes *********************************************/
00152 
00166 gint safe_strcmp (const gchar * da, const gchar * db);
00167 
00180 gint safe_strcasecmp (const gchar * da, const gchar * db);
00181 
00186 gint null_strcmp (const gchar * da, const gchar * db);
00187 
00191 extern gchar *strncasestr (const guchar * str1, const guchar * str2,
00192                            size_t len);
00193 
00194 #ifndef _STRING_H
00195 extern gchar *strcasestr (const gchar * str1, const gchar * str2);
00196 #endif
00197 
00201 gchar *ultostr (gulong val, gint base);
00202 
00205 gboolean qof_util_string_isnum (const guchar * s);
00206 
00208 gint 
00209 qof_util_double_compare (gdouble v1, gdouble v2);
00210 
00211 #ifndef HAVE_STPCPY
00212 
00213 #define stpcpy g_stpcpy
00214 #endif
00215 
00219 const gchar *qof_util_whitespace_filter (const gchar * val);
00220 
00224 gint qof_util_bool_to_int (const gchar * val);
00225 
00234 gchar *
00235 qof_util_param_to_string (QofEntity * ent, const QofParam * param);
00236 
00257 gboolean
00258 qof_util_param_set_string (QofEntity * ent, const QofParam * param,
00259     const gchar * value_string);
00260 
00270 gchar *
00271 qof_util_make_utf8 (gchar * string);
00272 
00303 void qof_util_string_cache_destroy (void);
00304 
00308 void qof_util_string_cache_remove (gconstpointer key);
00309 
00313 gpointer qof_util_string_cache_insert (gconstpointer key);
00314 
00315 #define CACHE_INSERT(str) qof_util_string_cache_insert((gconstpointer)(str))
00316 #define CACHE_REMOVE(str) qof_util_string_cache_remove((str))
00317 
00318 /* Replace cached string currently in 'dst' with string in 'src'.
00319  * Typical usage:
00320  *     void foo_set_name(Foo *f, const char *str) {
00321  *        CACHE_REPLACE(f->name, str);
00322  *     }
00323  * It avoids unnecessary ejection by doing INSERT before REMOVE.
00324 */
00325 #define CACHE_REPLACE(dst, src) do {          \
00326         gpointer tmp = CACHE_INSERT((src));   \
00327         CACHE_REMOVE((dst));                  \
00328         (dst) = tmp;                          \
00329     } while (0)
00330 
00331 #define QOF_CACHE_NEW(void) qof_util_string_cache_insert("")
00332 
00364 gboolean
00365 qof_util_param_edit (QofInstance * inst, const QofParam * param);
00366 
00381 gboolean
00382 qof_util_param_commit (QofInstance * inst, const QofParam * param);
00383 
00384 #endif /* QOF_UTIL_H */
00385