gnc-engine-util.c

00001 /********************************************************************\
00002  * gnc-engine-util.c -- QOF utility functions                       *
00003  * Copyright (C) 1997 Robin D. Clark                                *
00004  * Copyright (C) 1997-2001,2004 Linas Vepstas <linas@linas.org>     *
00005  *                                                                  *
00006  * This program is free software; you can redistribute it and/or    *
00007  * modify it under the terms of the GNU General Public License as   *
00008  * published by the Free Software Foundation; either version 2 of   *
00009  * the License, or (at your option) any later version.              *
00010  *                                                                  *
00011  * This program is distributed in the hope that it will be useful,  *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00014  * GNU General Public License for more details.                     *
00015  *                                                                  *
00016  * You should have received a copy of the GNU General Public License*
00017  * along with this program; if not, contact:                        *
00018  *                                                                  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942       *
00020  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00021  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00022  *                                                                  *
00023  *   Author: Rob Clark (rclark@cs.hmc.edu)                          *
00024  *   Author: Linas Vepstas (linas@linas.org)                        *
00025 \********************************************************************/
00026 
00027 #include "config.h"
00028 
00029 #include <ctype.h>
00030 #include <glib.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include "qof.h"
00034 #include "gnc-engine-util.h"
00035 
00036 
00037 /********************************************************************\
00038 \********************************************************************/
00039 
00040 /* Search for str2 in first nchar chars of str1, ignore case..  Return
00041  * pointer to first match, or null.  */
00042 char *
00043 strncasestr(const char *str1, const char *str2, size_t len) 
00044 {
00045   while (*str1 && len--) 
00046   {
00047     if (toupper(*str1) == toupper(*str2)) 
00048     {
00049       if (strncasecmp(str1,str2,strlen(str2)) == 0) 
00050       {
00051         return (char *) str1;
00052       }
00053     }
00054     str1++;
00055   }
00056   return NULL;
00057 }
00058 
00059 /* Search for str2 in str1, ignore case.  Return pointer to first
00060  * match, or null.  */
00061 char *
00062 strcasestr(const char *str1, const char *str2) 
00063 {
00064    size_t len = strlen (str1);
00065    char * retval = strncasestr (str1, str2, len);
00066    return retval;
00067 }
00068 
00069 /********************************************************************\
00070 \********************************************************************/
00071 
00072 int 
00073 safe_strcmp (const char * da, const char * db)
00074 {
00075    SAFE_STRCMP (da, db);
00076    return 0;
00077 }
00078 
00079 int 
00080 safe_strcasecmp (const char * da, const char * db)
00081 {
00082    SAFE_STRCASECMP (da, db);
00083    return 0;
00084 }
00085 
00086 int 
00087 null_strcmp (const char * da, const char * db)
00088 {
00089    if (da && db) return strcmp (da, db);
00090    if (!da && db && 0==db[0]) return 0;
00091    if (!db && da && 0==da[0]) return 0;
00092    if (!da && db) return -1;
00093    if (da && !db) return +1;
00094    return 0;
00095 }
00096 
00097 /********************************************************************\
00098 \********************************************************************/
00099 
00100 #define MAX_DIGITS 50
00101 
00102 /* inverse of strtoul */
00103 char *
00104 ultostr (unsigned long val, int base)
00105 {
00106   char buf[MAX_DIGITS];
00107   unsigned long broke[MAX_DIGITS];
00108   int i;
00109   unsigned long places=0, reval;
00110   
00111   if ((2>base) || (36<base)) return NULL;
00112 
00113   /* count digits */
00114   places = 0;
00115   for (i=0; i<MAX_DIGITS; i++) {
00116      broke[i] = val;
00117      places ++;
00118      val /= base;
00119      if (0 == val) break;
00120   }
00121 
00122   /* normalize */
00123   reval = 0;
00124   for (i=places-2; i>=0; i--) {
00125     reval += broke[i+1];
00126     reval *= base;
00127     broke[i] -= reval;
00128   }
00129 
00130   /* print */
00131   for (i=0; i<(int)places; i++) {
00132     if (10>broke[i]) {
00133        buf[places-1-i] = 0x30+broke[i];  /* ascii digit zero */
00134     } else {
00135        buf[places-1-i] = 0x41-10+broke[i];  /* ascii capital A */
00136     }
00137   }
00138   buf[places] = 0x0;
00139 
00140   return g_strdup (buf);
00141 }
00142 
00143 /********************************************************************\
00144  * returns TRUE if the string is a number, possibly with whitespace
00145 \********************************************************************/
00146 
00147 gboolean
00148 gnc_strisnum(const char *s)
00149 {
00150   if (s == NULL) return FALSE;
00151   if (*s == 0) return FALSE;
00152 
00153   while (*s && isspace(*s))
00154     s++;
00155 
00156   if (*s == 0) return FALSE;
00157   if (!isdigit(*s)) return FALSE;
00158 
00159   while (*s && isdigit(*s))
00160     s++;
00161 
00162   if (*s == 0) return TRUE;
00163 
00164   while (*s && isspace(*s))
00165     s++;
00166 
00167   if (*s == 0) return TRUE;
00168 
00169   return FALSE;
00170 }
00171 
00172 /********************************************************************\
00173  * our own version of stpcpy
00174 \********************************************************************/
00175 
00176 char *
00177 gnc_stpcpy (char *dest, const char *src)
00178 {
00179   strcpy (dest, src);
00180   return (dest + strlen (src));
00181 }
00182 
00183 /* =================================================================== */
00184 /* Return NULL if the field is whitespace (blank, tab, formfeed etc.)  
00185  * Else return pointer to first non-whitespace character. */
00186 
00187 const char *
00188 qof_util_whitespace_filter (const char * val)
00189 {
00190         size_t len;
00191         if (!val) return NULL;
00192 
00193         len = strspn (val, "\a\b\t\n\v\f\r ");
00194         if (0 == val[len]) return NULL;
00195         return val+len;
00196 }
00197 
00198 /* =================================================================== */
00199 /* Return integer 1 if the string starts with 't' or 'T' or contains the 
00200  * word 'true' or 'TRUE'; if string is a number, return that number. */
00201 
00202 int
00203 qof_util_bool_to_int (const char * val)
00204 {
00205         const char * p = qof_util_whitespace_filter (val);
00206         if (!p) return 0;
00207         if ('t' == p[0]) return 1;
00208         if ('T' == p[0]) return 1;
00209         if ('y' == p[0]) return 1;
00210         if ('Y' == p[0]) return 1;
00211         if (strstr (p, "true")) return 1;
00212         if (strstr (p, "TRUE")) return 1;
00213         if (strstr (p, "yes")) return 1;
00214         if (strstr (p, "YES")) return 1;
00215         return atoi (val);
00216 }
00217 
00218 /********************************************************************\
00219  * The engine string cache
00220 \********************************************************************/
00221 
00222 static GCache * gnc_string_cache = NULL;
00223 
00224 #ifdef THESE_CAN_BE_USEFUL_FOR_DEGUGGING
00225 static guint g_str_hash_KEY(gconstpointer v) {
00226     return g_str_hash(v);
00227 }
00228 static guint g_str_hash_VAL(gconstpointer v) {
00229     return g_str_hash(v);
00230 }
00231 static gpointer g_strdup_VAL(gpointer v) {
00232     return g_strdup(v);
00233 }
00234 static gpointer g_strdup_KEY(gpointer v) {
00235     return g_strdup(v);
00236 }
00237 static void g_free_VAL(gpointer v) {
00238     return g_free(v);
00239 }
00240 static void g_free_KEY(gpointer v) {
00241     return g_free(v);
00242 }
00243 static gboolean gnc_str_equal(gconstpointer v, gconstpointer v2)
00244 {
00245     return (v && v2) ? g_str_equal(v, v2) : FALSE;
00246 }
00247 #endif
00248 
00249 GCache*
00250 gnc_engine_get_string_cache(void)
00251 {
00252     if(!gnc_string_cache) {
00253         gnc_string_cache = g_cache_new(
00254             (GCacheNewFunc) g_strdup, /* value_new_func     */
00255             g_free,                   /* value_destroy_func */
00256             (GCacheDupFunc) g_strdup, /* key_dup_func       */
00257             g_free,                   /* key_destroy_func   */
00258             g_str_hash,               /* hash_key_func      */
00259             g_str_hash,               /* hash_value_func    */
00260             g_str_equal);             /* key_equal_func     */
00261     }
00262     return gnc_string_cache;
00263 }
00264 
00265 void
00266 gnc_engine_string_cache_destroy (void)
00267 {
00268     if (gnc_string_cache)
00269   g_cache_destroy (gnc_string_cache);
00270   gnc_string_cache = NULL;
00271 }
00272 
00273 void
00274 gnc_string_cache_remove(gconstpointer key)
00275 {
00276   g_cache_remove(gnc_engine_get_string_cache(), key);
00277 }
00278 
00279 
00280 gpointer
00281 gnc_string_cache_insert(gpointer key)
00282 {
00283   return g_cache_insert(gnc_engine_get_string_cache(), key);
00284 }
00285 
00286 void
00287 qof_init (void)
00288 {
00289         gnc_engine_get_string_cache ();
00290         guid_init ();
00291         qof_object_initialize ();
00292         qof_query_init ();
00293         qof_book_register ();
00294 }
00295 
00296 void
00297 qof_close(void)
00298 {
00299         qof_query_shutdown ();
00300         qof_object_shutdown ();
00301         guid_shutdown ();
00302         gnc_engine_string_cache_destroy ();
00303 }
00304 
00305 
00306 /************************* END OF FILE ******************************\
00307 \********************************************************************/

Generated on Fri Oct 21 15:49:54 2005 for QOF by  doxygen 1.4.5