Misc Utilities
[Query Object Framework]


Files

file  qofutil.h
 QOF utility functions.

Convenience wrappers

void qof_init (void)
 Initialise the Query Object Framework.
void qof_close (void)
 Safely close down the Query Object Framework.

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)
#define DEFINE_ENUM(name, list)
#define AS_STRING_DEC(name, list)   const gchar* name##asString(name n);
#define AS_STRING_FUNC(name, list)
#define FROM_STRING_DEC(name, list)
#define FROM_STRING_FUNC(name, list)

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.

You can precede the FROM_STRING_FUNC_NON_TYPEDEF and AS_STRING_FUNC_NON_TYPEDEF macros with the keyword static if appropriate.

ENUM_BODY is used in both types.

#define DEFINE_ENUM_NON_TYPEDEF(name, list)
#define FROM_STRING_DEC_NON_TYPEDEF(name, list)
#define FROM_STRING_CASE_NON_TYPEDEF(name, value)   if (strcmp(str, #name) == 0) { *type = name; }
#define FROM_STRING_FUNC_NON_TYPEDEF(name, list)
#define AS_STRING_DEC_NON_TYPEDEF(name, list)   const gchar* name##asString(enum name n);
#define AS_STRING_FUNC_NON_TYPEDEF(name, list)
#define AS_STRING_CASE_NON_TYPEDEF(name, value)   case name: { return #name; }

Defines

#define QOF_MOD_UTIL   "qof.utilities"
#define stpcpy   g_stpcpy
#define CACHE_INSERT(str)   qof_util_string_cache_insert((gconstpointer)(str))
#define CACHE_REMOVE(str)   qof_util_string_cache_remove((str))
#define CACHE_REPLACE(dst, src)
#define QOF_CACHE_NEW(void)   qof_util_string_cache_insert("")

Functions

gboolean qof_utf8_substr_nocase (const gchar *haystack, const gchar *needle)
gint qof_utf8_strcasecmp (const gchar *da, const gchar *db)
gint safe_strcmp (const gchar *da, const gchar *db)
gint safe_strcasecmp (const gchar *da, const gchar *db)
gint null_strcmp (const gchar *da, const gchar *db)
gchar * ultostr (gulong val, gint base)
gboolean gnc_strisnum (const guchar *s)
const gchar * qof_util_whitespace_filter (const gchar *val)
gint qof_util_bool_to_int (const gchar *val)
gchar * qof_util_param_as_string (QofInstance *ent, QofParam *param)
 Converts a parameter to a printable string.
void qof_util_string_cache_destroy (void)
void qof_util_string_cache_remove (gconstpointer key)
gpointer qof_util_string_cache_insert (gconstpointer key)
gboolean qof_begin_edit (QofInstance *inst)
gboolean qof_commit_edit (QofInstance *inst)
gboolean qof_commit_edit_part2 (QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))


Define Documentation

#define AS_STRING_FUNC ( name,
list   ) 

Value:

const gchar* name##asString(name n) { \
        switch (n) {                      \
            list(AS_STRING_CASE)          \
            default: return "";  } }

Definition at line 82 of file qofutil.h.

#define AS_STRING_FUNC_NON_TYPEDEF ( name,
list   ) 

Value:

const gchar* name##asString(enum name n) {     \
       switch (n) {                               \
           list(AS_STRING_CASE_NON_TYPEDEF)       \
           default: return ""; } }

Definition at line 135 of file qofutil.h.

#define CACHE_REPLACE ( dst,
src   ) 

Value:

do {          \
        gpointer tmp = CACHE_INSERT((src));   \
        CACHE_REMOVE((dst));                  \
        (dst) = tmp;                          \
    } while (0)

Definition at line 295 of file qofutil.h.

#define DEFINE_ENUM ( name,
list   ) 

Value:

typedef enum {                       \
        list(ENUM_BODY)                  \
    }name;

Definition at line 74 of file qofutil.h.

#define DEFINE_ENUM_NON_TYPEDEF ( name,
list   ) 

Value:

enum name {                               \
        list(ENUM_BODY)                       \
    };

Definition at line 114 of file qofutil.h.

#define FROM_STRING_CASE ( name,
value   ) 

Value:

if (strcmp(str, #name) == 0) {       \
        return name;  }

Definition at line 70 of file qofutil.h.

#define FROM_STRING_DEC ( name,
list   ) 

Value:

name name##fromString                \
    (const gchar* str);

Definition at line 88 of file qofutil.h.

#define FROM_STRING_DEC_NON_TYPEDEF ( name,
list   ) 

Value:

void name##fromString                          \
   (const gchar* str, enum name *type);

Definition at line 119 of file qofutil.h.

#define FROM_STRING_FUNC ( name,
list   ) 

Value:

name name##fromString                \
    (const gchar* str) {                 \
    if(str == NULL) { return 0; }        \
        list(FROM_STRING_CASE)           \
        return 0;  }

Definition at line 92 of file qofutil.h.

#define FROM_STRING_FUNC_NON_TYPEDEF ( name,
list   ) 

Value:

void name##fromString                          \
   (const gchar* str, enum name *type) {          \
   if(str == NULL) { return; }                    \
    list(FROM_STRING_CASE_NON_TYPEDEF) }

Definition at line 126 of file qofutil.h.

#define QOF_MOD_UTIL   "qof.utilities"

Do not use these for printf, only scanf

Definition at line 59 of file qofutil.h.


Function Documentation

gboolean gnc_strisnum ( const guchar *  s  ) 

Returns true if string s is a number, possibly surrounded by whitespace.

Definition at line 180 of file qofutil.c.

00181 {
00182   if (s == NULL) return FALSE;
00183   if (*s == 0) return FALSE;
00184 
00185   while (*s && isspace(*s))
00186     s++;
00187 
00188   if (*s == 0) return FALSE;
00189   if (!isdigit(*s)) return FALSE;
00190 
00191   while (*s && isdigit(*s))
00192     s++;
00193 
00194   if (*s == 0) return TRUE;
00195 
00196   while (*s && isspace(*s))
00197     s++;
00198 
00199   if (*s == 0) return TRUE;
00200 
00201   return FALSE;
00202 }

gint null_strcmp ( 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.

Definition at line 122 of file qofutil.c.

00123 {
00124    if (da && db) return strcmp (da, db);
00125    if (!da && db && 0==db[0]) return 0;
00126    if (!db && da && 0==da[0]) return 0;
00127    if (!da && db) return -1;
00128    if (da && !db) return +1;
00129    return 0;
00130 }

gboolean qof_begin_edit ( QofInstance inst  ) 

begin_edit

Parameters:
inst,: an instance of QofInstance
The caller should use this macro first and then perform any other operations.

Definition at line 889 of file qofinstance.c.

00890 {
00891     QofInstancePrivate *priv;
00892     QofBackend * be;
00893 
00894     if (!inst) return FALSE;
00895 
00896     priv = GET_PRIVATE(inst);
00897     priv->editlevel++;
00898     if (1 < priv->editlevel) return FALSE;
00899     if (0 >= priv->editlevel) 
00900         priv->editlevel = 1;
00901 
00902     be = qof_book_get_backend(priv->book);
00903     if (be && qof_backend_begin_exists(be))
00904         qof_backend_run_begin(be, inst);
00905     else
00906         priv->dirty = TRUE; 
00907   
00908     return TRUE;
00909 }

void qof_close ( void   ) 

Safely close down the Query Object Framework.

Use in place of separate close / shutdown functions (like guid_shutdown(), qof_query_shutdown() etc.) to protect against future changes.

Definition at line 469 of file qofutil.c.

00470 {
00471         qof_query_shutdown ();
00472         qof_object_shutdown ();
00473         guid_shutdown ();
00474         qof_util_string_cache_destroy ();
00475     qof_log_shutdown();
00476 }

gboolean qof_commit_edit ( QofInstance inst  ) 

part1 -- deal with the editlevel

Parameters:
inst,: an instance of QofInstance

Definition at line 911 of file qofinstance.c.

00912 {
00913     QofInstancePrivate *priv;
00914     QofBackend * be;
00915 
00916     if (!inst) return FALSE;
00917 
00918     priv = GET_PRIVATE(inst);
00919     priv->editlevel--;
00920     if (0 < priv->editlevel) return FALSE;
00921 
00922     if ((0 == priv->editlevel) && priv->dirty) {
00923         be = qof_book_get_backend(priv->book);
00924         if (be && qof_backend_commit_exists(be)) {
00925             qof_backend_run_commit(be, inst);
00926         }
00927     }
00928     if (0 > priv->editlevel) { 
00929         PERR ("unbalanced call - resetting (was %d)", priv->editlevel);
00930         priv->editlevel = 0;
00931     }
00932     return TRUE;
00933 }

gboolean qof_commit_edit_part2 ( QofInstance inst,
void(*)(QofInstance *, QofBackendError on_error,
void(*)(QofInstance *)  on_done,
void(*)(QofInstance *)  on_free 
)

part2 -- deal with the backend

Parameters:
inst,: an instance of QofInstance
on_error,: a function called if there is a backend error. void (*on_error)(inst, QofBackendError)
on_done,: a function called after the commit is completed successfully for an object which remained valid. void (*on_done)(inst)
on_free,: a function called if the commit succeeded and the instance is to be freed. void (*on_free)(inst)
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.

Definition at line 936 of file qofinstance.c.

00940 {
00941     QofInstancePrivate *priv;
00942     QofBackend * be;
00943     gboolean dirty;
00944 
00945     priv = GET_PRIVATE(inst);
00946     dirty = priv->dirty;
00947 
00948     /* See if there's a backend.  If there is, invoke it. */
00949     be = qof_book_get_backend(priv->book);
00950     if (be && qof_backend_commit_exists(be)) {
00951         QofBackendError errcode;
00952         
00953         /* clear errors */
00954         do {
00955             errcode = qof_backend_get_error(be);
00956         } while (ERR_BACKEND_NO_ERR != errcode);
00957 
00958         qof_backend_run_commit(be, inst);
00959         errcode = qof_backend_get_error(be);
00960         if (ERR_BACKEND_NO_ERR != errcode) {
00961             /* XXX Should perform a rollback here */
00962             priv->do_free = FALSE;
00963 
00964             /* Push error back onto the stack */
00965             qof_backend_set_error (be, errcode);
00966             if (on_error)
00967                 on_error(inst, errcode);
00968             return FALSE;
00969         }   
00970         /* XXX the backend commit code should clear dirty!! */
00971         priv->dirty = FALSE;
00972     }
00973 //    if (dirty && qof_get_alt_dirty_mode() && 
00974 //        !(priv->infant && priv->do_free)) {
00975 //      qof_collection_mark_dirty(priv->collection);
00976 //      qof_book_mark_dirty(priv->book);
00977 //    }
00978     priv->infant = FALSE;
00979 
00980     if (priv->do_free) {
00981         if (on_free)
00982             on_free(inst);
00983         return TRUE;
00984     }
00985     
00986     if (on_done)
00987         on_done(inst);
00988     return TRUE;
00989 }

void qof_init ( void   ) 

Initialise the Query Object Framework.

Use in place of separate init functions (like guid_init() and qof_query_init() etc.) to protect against future changes.

Definition at line 457 of file qofutil.c.

00458 {
00459         g_type_init();
00460         qof_log_init();
00461         qof_util_get_string_cache ();
00462         guid_init ();
00463         qof_object_initialize ();
00464         qof_query_init ();
00465         qof_book_register ();
00466 }

gint qof_utf8_strcasecmp ( const gchar *  da,
const gchar *  db 
)

Use g_utf8_casefold and g_utf8_collate to compare two utf8 strings, ignore case. Return < 0 if da compares before db, 0 if they compare equal, > 0 if da compares after db.

Definition at line 66 of file qofutil.c.

00067 {
00068     gchar *da_casefold, *db_casefold;
00069     gint retval;
00070 
00071     g_return_val_if_fail (da != NULL, 0);
00072     g_return_val_if_fail (db != NULL, 0);
00073 
00074     da_casefold = g_utf8_casefold (da, -1);
00075     db_casefold = g_utf8_casefold (db, -1);
00076     retval = g_utf8_collate (da_casefold, db_casefold);
00077     g_free (da_casefold);
00078     g_free (db_casefold);
00079 
00080     return retval;
00081 }

gboolean qof_utf8_substr_nocase ( const gchar *  haystack,
const gchar *  needle 
)

Search for an occurence of the substring needle in the string haystack, ignoring case. Return TRUE if one is found or FALSE otherwise.

Definition at line 40 of file qofutil.c.

00041 {
00042     gchar *haystack_casefold, *haystack_normalized;
00043     gchar *needle_casefold, *needle_normalized;
00044     gchar *p;
00045     gint offset;
00046 
00047     g_return_val_if_fail (haystack && needle, FALSE);
00048 
00049     haystack_casefold = g_utf8_casefold (haystack, -1);
00050     haystack_normalized = g_utf8_normalize (haystack_casefold, -1,
00051                                             G_NORMALIZE_ALL);
00052     g_free (haystack_casefold);
00053 
00054     needle_casefold = g_utf8_casefold (needle, -1);
00055     needle_normalized = g_utf8_normalize (needle_casefold, -1, G_NORMALIZE_ALL);
00056     g_free (needle_casefold);
00057 
00058     p = strstr (haystack_normalized, needle_normalized);
00059     g_free (haystack_normalized);
00060     g_free (needle_normalized);
00061 
00062     return p != NULL;
00063 }

gint qof_util_bool_to_int ( const gchar *  val  ) 

Return integer 1 if the string starts with 't' or 'T' or contains the word 'true' or 'TRUE'; if string is a number, return that number. (Leading whitespace is ignored).

Definition at line 226 of file qofutil.c.

00227 {
00228         const gchar * p = qof_util_whitespace_filter (val);
00229         if (!p) return 0;
00230         if ('t' == p[0]) return 1;
00231         if ('T' == p[0]) return 1;
00232         if ('y' == p[0]) return 1;
00233         if ('Y' == p[0]) return 1;
00234         if (strstr (p, "true")) return 1;
00235         if (strstr (p, "TRUE")) return 1;
00236         if (strstr (p, "yes")) return 1;
00237         if (strstr (p, "YES")) return 1;
00238         return atoi (val);
00239 }

gchar* qof_util_param_as_string ( QofInstance ent,
QofParam param 
)

Converts a parameter to a printable string.

The returned string must be freed by the caller.

Definition at line 311 of file qofutil.c.

00312 {
00313         gchar       *param_string, param_date[MAX_DATE_LENGTH];
00314         gchar       param_sa[GUID_ENCODING_LENGTH + 1];
00315     gboolean    known_type;
00316         QofType     paramType;
00317         const GUID *param_guid;
00318         time_t      param_t;
00319         gnc_numeric param_numeric,  (*numeric_getter) (QofInstance*, QofParam*);
00320         Timespec    param_ts,       (*date_getter)    (QofInstance*, QofParam*);
00321         double      param_double,   (*double_getter)  (QofInstance*, QofParam*);
00322         gboolean    param_boolean,  (*boolean_getter) (QofInstance*, QofParam*);
00323         gint32      param_i32,      (*int32_getter)   (QofInstance*, QofParam*);
00324         gint64      param_i64,      (*int64_getter)   (QofInstance*, QofParam*);
00325         gchar       param_char,     (*char_getter)    (QofInstance*, QofParam*);
00326 
00327         param_string = NULL;
00328     known_type = FALSE;
00329         paramType = param->param_type;
00330         if(safe_strcmp(paramType, QOF_TYPE_STRING) == 0)  { 
00331                         param_string = g_strdup(param->param_getfcn(ent, param));
00332                         if(param_string == NULL) { param_string = ""; }
00333             known_type = TRUE;
00334                         return param_string;
00335                 }
00336                 if(safe_strcmp(paramType, QOF_TYPE_DATE) == 0) { 
00337                         date_getter = (Timespec (*)(QofInstance*, QofParam*))param->param_getfcn;
00338                         param_ts = date_getter(ent, param);
00339                         param_t = timespecToTime_t(param_ts);
00340                         qof_strftime(param_date, MAX_DATE_LENGTH, 
00341                 QOF_UTC_DATE_FORMAT, gmtime(&param_t));
00342                         param_string = g_strdup(param_date);
00343             known_type = TRUE;
00344                         return param_string;
00345                 }
00346                 if((safe_strcmp(paramType, QOF_TYPE_NUMERIC) == 0)  ||
00347                 (safe_strcmp(paramType, QOF_TYPE_DEBCRED) == 0)) { 
00348                         numeric_getter = (gnc_numeric (*)(QofInstance*, QofParam*)) param->param_getfcn;
00349                         param_numeric = numeric_getter(ent, param);
00350                         param_string = g_strdup(gnc_numeric_to_string(param_numeric));
00351             known_type = TRUE;
00352                         return param_string;
00353                 }
00354                 if(safe_strcmp(paramType, QOF_TYPE_GUID) == 0) { 
00355                         param_guid = param->param_getfcn(ent, param);
00356                         guid_to_string_buff(param_guid, param_sa);
00357                         param_string = g_strdup(param_sa);
00358             known_type = TRUE;
00359                         return param_string;
00360                 }
00361                 if(safe_strcmp(paramType, QOF_TYPE_INT32) == 0) { 
00362                         int32_getter = (gint32 (*)(QofInstance*, QofParam*)) param->param_getfcn;
00363                         param_i32 = int32_getter(ent, param);
00364                         param_string = g_strdup_printf("%d", param_i32);
00365             known_type = TRUE;
00366                         return param_string;
00367                 }
00368                 if(safe_strcmp(paramType, QOF_TYPE_INT64) == 0) { 
00369                         int64_getter = (gint64 (*)(QofInstance*, QofParam*)) param->param_getfcn;
00370                         param_i64 = int64_getter(ent, param);
00371                         param_string = g_strdup_printf("%"G_GINT64_FORMAT, param_i64);
00372             known_type = TRUE;
00373                         return param_string;
00374                 }
00375                 if(safe_strcmp(paramType, QOF_TYPE_DOUBLE) == 0) { 
00376                         double_getter = (double (*)(QofInstance*, QofParam*)) param->param_getfcn;
00377                         param_double = double_getter(ent, param);
00378                         param_string = g_strdup_printf("%f", param_double);
00379             known_type = TRUE;
00380                         return param_string;
00381                 }
00382                 if(safe_strcmp(paramType, QOF_TYPE_BOOLEAN) == 0){ 
00383                         boolean_getter = (gboolean (*)(QofInstance*, QofParam*)) param->param_getfcn;
00384                         param_boolean = boolean_getter(ent, param);
00385                         /* Boolean values need to be lowercase for QSF validation. */
00386                         if(param_boolean == TRUE) { param_string = g_strdup("true"); }
00387                         else { param_string = g_strdup("false"); }
00388             known_type = TRUE;
00389                         return param_string;
00390                 }
00391                 /* "kvp" contains repeating values, cannot be a single string for the frame. */
00392                 if(safe_strcmp(paramType, QOF_TYPE_KVP) == 0) {
00393             KvpFrame *frame = NULL;
00394             frame = param->param_getfcn(ent, param);
00395             known_type = TRUE;
00396             if(!kvp_frame_is_empty(frame)) 
00397             {
00398                 GHashTable *hash = kvp_frame_get_hash(frame);
00399                 param_string = g_strdup_printf("%s(%d)", QOF_TYPE_KVP,
00400                     g_hash_table_size(hash));
00401             }
00402             return param_string; 
00403         }
00404                 if(safe_strcmp(paramType, QOF_TYPE_CHAR) == 0) { 
00405                         char_getter = (gchar (*)(QofInstance*, QofParam*)) param->param_getfcn;
00406                         param_char = char_getter(ent, param);
00407             known_type = TRUE;
00408                         return g_strdup_printf("%c", param_char);
00409                 }
00410                 /* "collect" contains repeating values, cannot be a single string. */
00411         if(safe_strcmp(paramType, QOF_TYPE_COLLECT) == 0)
00412         {
00413             QofCollection *col = NULL;
00414             col = param->param_getfcn(ent, param);
00415             known_type = TRUE;
00416             return g_strdup_printf("%s(%d)", 
00417                 qof_collection_get_type(col), qof_collection_count(col));
00418         }
00419         if(safe_strcmp(paramType, QOF_TYPE_CHOICE) == 0)
00420         {
00421             QofInstance *child = NULL;
00422             child = param->param_getfcn(ent, param);
00423             if(!child) { return param_string; }
00424             known_type = TRUE;
00425             return g_strdup(qof_object_printable(child->e_type, child));
00426         }
00427         if(safe_strcmp(paramType, QOF_PARAM_BOOK) == 0)
00428         {
00429             QofBackend *be;
00430             QofBook *book;
00431             book = param->param_getfcn(ent, param);
00432             PINFO (" book param %p", book);
00433             be = qof_book_get_backend(book);
00434             known_type = TRUE;
00435             PINFO (" backend=%p", be);
00436             if(!be) { return QOF_PARAM_BOOK; }
00437             param_string = g_strdup(be->fullpath);
00438             PINFO (" fullpath=%s", param_string);
00439             if(param_string) { return param_string; }
00440                         param_guid = qof_book_get_guid(book);
00441                         guid_to_string_buff(param_guid, param_sa);
00442             PINFO (" book GUID=%s", param_sa);
00443                         param_string = g_strdup(param_sa);
00444             return param_string;
00445         }
00446         if(!known_type)
00447         {
00448             QofInstance *child = NULL;
00449             child = param->param_getfcn(ent, param);
00450             if(!child) { return param_string; }
00451             return g_strdup(qof_object_printable(child->e_type, child));
00452         }
00453         return g_strdup("");
00454 }

void qof_util_string_cache_destroy ( void   ) 

Destroy the qof_util_string_cache

Definition at line 288 of file qofutil.c.

00289 {
00290     if (qof_string_cache)
00291         g_cache_destroy (qof_string_cache);
00292     qof_string_cache = NULL;
00293 }

gpointer qof_util_string_cache_insert ( gconstpointer  key  ) 

You can use this function with g_hash_table_insert(), for the key (or value), as long as you use the destroy notifier above.

Definition at line 303 of file qofutil.c.

00304 {
00305     if (key)
00306         return g_cache_insert(qof_util_get_string_cache(), (gpointer)key);
00307     return NULL;
00308 }

void qof_util_string_cache_remove ( gconstpointer  key  ) 

You can use this function as a destroy notifier for a GHashTable that uses common strings as keys (or values, for that matter.)

Definition at line 296 of file qofutil.c.

00297 {
00298     if (key)
00299     g_cache_remove(qof_util_get_string_cache(), key);
00300 }

const gchar* qof_util_whitespace_filter ( const gchar *  val  ) 

Return NULL if the field is whitespace (blank, tab, formfeed etc.) Else return pointer to first non-whitespace character.

Definition at line 210 of file qofutil.c.

00211 {
00212         size_t len;
00213         if (!val) return NULL;
00214 
00215         len = strspn (val, "\a\b\t\n\v\f\r ");
00216         if (0 == val[len]) return NULL;
00217         return val+len;
00218 }

gint safe_strcasecmp ( const gchar *  da,
const gchar *  db 
)

case sensitive comparison of strings da and db - either may be NULL. A non-NULL string is greater than a NULL string.

Parameters:
da string 1.
db string 2.
Returns:
If da == NULL && db != NULL, returns -1. If da != NULL && db == NULL, returns +1. If da != NULL && db != NULL, returns the result of strcmp(da, db). If da == NULL && db == NULL, returns 0.

Definition at line 103 of file qofutil.c.

00104 {
00105         if ((da) && (db)) {
00106                 if ((da) != (db)) {
00107                         gint retval = qof_utf8_strcasecmp ((da), (db));
00108                         /* if strings differ, return */
00109                         if (retval) return retval;
00110                 }     
00111         } else
00112         if ((!(da)) && (db)) {
00113                 return -1;
00114         } else
00115         if ((da) && (!(db))) {
00116                 return +1;
00117     }
00118    return 0;
00119 }

gint safe_strcmp ( const gchar *  da,
const gchar *  db 
)

The safe_strcmp compares strings da and db the same way that strcmp() does, except that either may be null. This routine assumes that a non-null string is always greater than a null string.

Parameters:
da string 1.
db string 2.
Returns:
If da == NULL && db != NULL, returns -1. If da != NULL && db == NULL, returns +1. If da != NULL && db != NULL, returns the result of strcmp(da, db). If da == NULL && db == NULL, returns 0.

Definition at line 84 of file qofutil.c.

00085 {
00086         if ((da) && (db)) {
00087                 if ((da) != (db)) {
00088                         gint retval = strcmp ((da), (db));
00089                         /* if strings differ, return */
00090                         if (retval) return retval;
00091                 }     
00092         } else
00093         if ((!(da)) && (db)) {
00094                 return -1;
00095         } else
00096         if ((da) && (!(db))) {
00097                 return +1;
00098         }
00099         return 0;
00100 }

gchar* ultostr ( gulong  val,
gint  base 
)

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.

Definition at line 136 of file qofutil.c.

00137 {
00138   gchar buf[MAX_DIGITS];
00139   gulong broke[MAX_DIGITS];
00140   gint i;
00141   gulong places=0, reval;
00142   
00143   if ((2>base) || (36<base)) return NULL;
00144 
00145   /* count digits */
00146   places = 0;
00147   for (i=0; i<MAX_DIGITS; i++) {
00148      broke[i] = val;
00149      places ++;
00150      val /= base;
00151      if (0 == val) break;
00152   }
00153 
00154   /* normalize */
00155   reval = 0;
00156   for (i=places-2; i>=0; i--) {
00157     reval += broke[i+1];
00158     reval *= base;
00159     broke[i] -= reval;
00160   }
00161 
00162   /* print */
00163   for (i=0; i<(gint)places; i++) {
00164     if (10>broke[i]) {
00165        buf[places-1-i] = 0x30+broke[i];  /* ascii digit zero */
00166     } else {
00167        buf[places-1-i] = 0x41-10+broke[i];  /* ascii capital A */
00168     }
00169   }
00170   buf[places] = 0x0;
00171 
00172   return g_strdup (buf);
00173 }


Generated on Sun Oct 12 05:08:01 2008 for GnuCash by  doxygen 1.5.2