GnuCash c935c2f+
Loading...
Searching...
No Matches
qof-string-cache.cpp
1/********************************************************************\
2 * qof-string-cache.c -- QOF string cache functions *
3 * Copyright (C) 1997 Robin D. Clark *
4 * Copyright (C) 1997-2001,2004 Linas Vepstas <linas@linas.org> *
5 * Copyright 2006 Neil Williams <linux@codehelp.co.uk> *
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License as *
9 * published by the Free Software Foundation; either version 2 of *
10 * the License, or (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License*
18 * along with this program; if not, contact: *
19 * *
20 * Free Software Foundation Voice: +1-617-542-5942 *
21 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22 * Boston, MA 02110-1301, USA gnu@gnu.org *
23 * *
24 * Author: Rob Clark (rclark@cs.hmc.edu) *
25 * Author: Linas Vepstas (linas@linas.org) *
26 * Author: Phil Longstaff (phil.longstaff@yahoo.ca) *
27\********************************************************************/
28#include <glib.h>
29
30#include <config.h>
31
32#include <ctype.h>
33#include <stdlib.h>
34#include <string.h>
35#include "qof.h"
36
37/* Uncomment if you need to log anything.
38static QofLogModule log_module = QOF_MOD_UTIL;
39*/
40/* =================================================================== */
41/* The QOF string cache */
42/* */
43/* The cache is a GHashTable where a copy of the string is the key, */
44/* and a ref count is the value */
45/* =================================================================== */
46
47static GHashTable* qof_string_cache = NULL;
48
49static GHashTable*
50qof_get_string_cache(void)
51{
52 if (!qof_string_cache)
53 {
54 qof_string_cache = g_hash_table_new_full(
55 g_str_hash, /* hash_func */
56 g_str_equal, /* key_equal_func */
57 g_free, /* key_destroy_func */
58 g_free); /* value_destroy_func */
59 }
60 return qof_string_cache;
61}
62
63void
65{
66 (void)qof_get_string_cache();
67}
68
69void
71{
72 if (qof_string_cache)
73 {
74 g_hash_table_destroy(qof_string_cache);
75 }
76 qof_string_cache = NULL;
77}
78
79/* If the key exists in the cache, check the refcount. If 1, just
80 * remove the key. Otherwise, decrement the refcount */
81void
82qof_string_cache_remove(const char * key)
83{
84 if (key && key[0] != 0)
85 {
86 GHashTable* cache = qof_get_string_cache();
87 gpointer value;
88 gpointer cache_key;
89 if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
90 {
91 guint* refcount = (guint*)value;
92 if (*refcount == 1)
93 {
94 g_hash_table_remove(cache, key);
95 }
96 else
97 {
98 --(*refcount);
99 }
100 }
101 }
102}
103
104/* If the key exists in the cache, increment the refcount. Otherwise,
105 * add it with a refcount of 1. */
106const char *
107qof_string_cache_insert(const char * key)
108{
109 if (key)
110 {
111 if (key[0] == 0)
112 {
113 return "";
114 }
115
116 GHashTable* cache = qof_get_string_cache();
117 gpointer value;
118 gpointer cache_key;
119 if (g_hash_table_lookup_extended(cache, key, &cache_key, &value))
120 {
121 guint* refcount = (guint*)value;
122 ++(*refcount);
123 return static_cast <char *> (cache_key);
124 }
125 else
126 {
127 gpointer new_key = g_strdup(static_cast<const char*>(key));
128 guint* refcount = static_cast<unsigned int*>(g_malloc(sizeof(guint)));
129 *refcount = 1;
130 g_hash_table_insert(cache, new_key, refcount);
131 return static_cast <char *> (new_key);
132 }
133 }
134 return NULL;
135}
136
137const char *
138qof_string_cache_replace(char const * dst, char const * src)
139{
140 const char * tmp {qof_string_cache_insert (src)};
142 return tmp;
143}
144/* ************************ END OF FILE ***************************** */
void qof_string_cache_init(void)
The QOF String Cache:
const char * qof_string_cache_insert(const char *key)
You can use this function with g_hash_table_insert(), for the key (or value), as long as you use the ...
const char * qof_string_cache_replace(char const *dst, char const *src)
Same as CACHE_REPLACE below, but safe to call from C++.
void qof_string_cache_destroy(void)
Destroy the qof_string_cache.
void qof_string_cache_remove(const char *key)
You can use this function as a destroy notifier for a GHashTable that uses common strings as keys (or...