00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00041
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
00060
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
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
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
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
00131 for (i=0; i<(int)places; i++) {
00132 if (10>broke[i]) {
00133 buf[places-1-i] = 0x30+broke[i];
00134 } else {
00135 buf[places-1-i] = 0x41-10+broke[i];
00136 }
00137 }
00138 buf[places] = 0x0;
00139
00140 return g_strdup (buf);
00141 }
00142
00143
00144
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
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
00185
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
00200
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
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,
00255 g_free,
00256 (GCacheDupFunc) g_strdup,
00257 g_free,
00258 g_str_hash,
00259 g_str_hash,
00260 g_str_equal);
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
00307