add commodity_table_copy, commodity_copy routines.

convert to use string-cache insted of g_strdup


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@9098 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2003-08-17 04:20:25 +00:00
parent 1752f2c33d
commit 20dbc5f11c
2 changed files with 171 additions and 110 deletions

View File

@ -463,14 +463,18 @@ gnc_commodity_new(const char * fullname,
const char * exchange_code, const char * exchange_code,
int fraction) int fraction)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
gnc_commodity * retval = g_new0(gnc_commodity, 1); gnc_commodity * retval = g_new0(gnc_commodity, 1);
retval->fullname = g_strdup(fullname); retval->fullname = g_cache_insert(str_cache, (gpointer)fullname);
retval->namespace = g_strdup(namespace); retval->namespace = g_cache_insert(str_cache, (gpointer)namespace);
retval->mnemonic = g_strdup(mnemonic); retval->mnemonic = g_cache_insert(str_cache, (gpointer)mnemonic);
retval->exchange_code = g_strdup(exchange_code); retval->exchange_code = g_cache_insert(str_cache, (gpointer)exchange_code);
retval->fraction = fraction; retval->fraction = fraction;
retval->mark = 0; retval->mark = 0;
retval->quote_flag = 0;
retval->quote_source = NULL;
retval->quote_tz = NULL;
reset_printname(retval); reset_printname(retval);
reset_unique_name(retval); reset_unique_name(retval);
@ -486,25 +490,26 @@ gnc_commodity_new(const char * fullname,
void void
gnc_commodity_destroy(gnc_commodity * cm) gnc_commodity_destroy(gnc_commodity * cm)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
if(!cm) return; if(!cm) return;
/* Set at creation */ /* Set at creation */
g_free(cm->fullname); g_cache_remove(str_cache, cm->fullname);
cm->fullname = NULL; cm->fullname = NULL;
g_free(cm->namespace); g_cache_remove(str_cache, cm->namespace);
cm->namespace = NULL; cm->namespace = NULL;
g_free(cm->exchange_code); g_cache_remove(str_cache, cm->exchange_code);
cm->exchange_code = NULL; cm->exchange_code = NULL;
g_free(cm->mnemonic); g_cache_remove(str_cache, cm->mnemonic);
cm->mnemonic = NULL; cm->mnemonic = NULL;
/* Set through accessor functions */ /* Set through accessor functions */
cm->quote_source = NULL; cm->quote_source = NULL;
g_free(cm->quote_tz); g_cache_remove(str_cache, cm->quote_tz);
cm->quote_tz = NULL; cm->quote_tz = NULL;
/* Automatically generated */ /* Automatically generated */
@ -519,6 +524,40 @@ gnc_commodity_destroy(gnc_commodity * cm)
g_free(cm); g_free(cm);
} }
void
gnc_commodity_copy(gnc_commodity * dest, gnc_commodity *src)
{
gnc_commodity_set_fullname (dest, src->fullname);
gnc_commodity_set_namespace (dest, src->namespace);
gnc_commodity_set_fraction (dest, src->fraction);
gnc_commodity_set_exchange_code (dest, src->exchange_code);
gnc_commodity_set_quote_flag (dest, src->quote_flag);
gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
gnc_commodity_set_quote_tz (dest, src->quote_tz);
}
gnc_commodity *
gnc_commodity_clone(gnc_commodity *src)
{
GCache *str_cache = gnc_engine_get_string_cache ();
gnc_commodity * dest = g_new0(gnc_commodity, 1);
dest->fullname = g_cache_insert(str_cache, src->fullname);
dest->namespace = g_cache_insert(str_cache, src->namespace);
dest->mnemonic = g_cache_insert(str_cache, src->mnemonic);
dest->exchange_code = g_cache_insert(str_cache, src->exchange_code);
dest->fraction = src->fraction;
dest->mark = 0;
dest->quote_flag = src->quote_flag;
dest->quote_source = g_cache_insert(str_cache, src->quote_source);
dest->quote_tz = g_cache_insert(str_cache, src->quote_tz);
reset_printname(dest);
reset_unique_name(dest);
return dest;
}
/******************************************************************** /********************************************************************
* gnc_commodity_get_mnemonic * gnc_commodity_get_mnemonic
@ -663,11 +702,12 @@ gnc_commodity_get_quote_tz(const gnc_commodity *cm)
void void
gnc_commodity_set_mnemonic(gnc_commodity * cm, const char * mnemonic) gnc_commodity_set_mnemonic(gnc_commodity * cm, const char * mnemonic)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
if(!cm) return; if(!cm) return;
if(cm->mnemonic == mnemonic) return; if(cm->mnemonic == mnemonic) return;
g_free(cm->mnemonic); g_cache_remove(str_cache, cm->mnemonic);
cm->mnemonic = g_strdup(mnemonic); cm->mnemonic = g_cache_insert(str_cache, (gpointer)mnemonic);
reset_printname(cm); reset_printname(cm);
reset_unique_name(cm); reset_unique_name(cm);
@ -680,11 +720,12 @@ gnc_commodity_set_mnemonic(gnc_commodity * cm, const char * mnemonic)
void void
gnc_commodity_set_namespace(gnc_commodity * cm, const char * namespace) gnc_commodity_set_namespace(gnc_commodity * cm, const char * namespace)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
if(!cm) return; if(!cm) return;
if(cm->namespace == namespace) return; if(cm->namespace == namespace) return;
g_free(cm->namespace); g_cache_remove(str_cache, cm->namespace);
cm->namespace = g_strdup(namespace); cm->namespace = g_cache_insert(str_cache, (gpointer)namespace);
reset_printname(cm); reset_printname(cm);
reset_unique_name(cm); reset_unique_name(cm);
@ -697,11 +738,12 @@ gnc_commodity_set_namespace(gnc_commodity * cm, const char * namespace)
void void
gnc_commodity_set_fullname(gnc_commodity * cm, const char * fullname) gnc_commodity_set_fullname(gnc_commodity * cm, const char * fullname)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
if(!cm) return; if(!cm) return;
if(cm->fullname == fullname) return; if(cm->fullname == fullname) return;
g_free(cm->fullname); g_cache_remove(str_cache, cm->fullname);
cm->fullname = g_strdup(fullname); cm->fullname = g_cache_insert(str_cache, (gpointer)fullname);
reset_printname(cm); reset_printname(cm);
} }
@ -714,11 +756,12 @@ void
gnc_commodity_set_exchange_code(gnc_commodity * cm, gnc_commodity_set_exchange_code(gnc_commodity * cm,
const char * exchange_code) const char * exchange_code)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
if(!cm) return; if(!cm) return;
if(cm->exchange_code == exchange_code) return; if(cm->exchange_code == exchange_code) return;
g_free(cm->exchange_code); g_cache_remove(str_cache, cm->exchange_code);
cm->exchange_code = g_strdup(exchange_code); cm->exchange_code = g_cache_insert(str_cache, (gpointer)exchange_code);
} }
/******************************************************************** /********************************************************************
@ -764,10 +807,12 @@ gnc_commodity_set_quote_flag(gnc_commodity *cm, const gboolean flag)
void void
gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src) gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
ENTER ("(cm=%p, src=%p(%s))", cm, src, src ? src->internal_name : "unknown"); ENTER ("(cm=%p, src=%p(%s))", cm, src, src ? src->internal_name : "unknown");
if(!cm) return; if(!cm) return;
cm->quote_source = src; g_cache_remove(str_cache, cm->quote_source);
cm->quote_source = g_cache_insert(str_cache, src);
LEAVE(" "); LEAVE(" ");
} }
@ -778,17 +823,16 @@ gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
void void
gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz) gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
ENTER ("(cm=%p, tz=%s)", cm, tz); ENTER ("(cm=%p, tz=%s)", cm, tz);
if(!cm) return; if(!cm) return;
if (cm->quote_tz) { g_cache_remove(str_cache, cm->quote_tz);
g_free(cm->quote_tz); if (cm->quote_tz) cm->quote_tz = NULL;
cm->quote_tz = NULL;
} if (tz && *tz) cm->quote_tz = g_cache_insert(str_cache, (gpointer)tz);
if (tz && *tz)
cm->quote_tz = g_strdup(tz);
LEAVE(" "); LEAVE(" ");
} }
@ -1011,7 +1055,8 @@ gnc_commodity_table_lookup_unique(const gnc_commodity_table *table,
gnc_commodity * gnc_commodity *
gnc_commodity_table_find_full(const gnc_commodity_table * table, gnc_commodity_table_find_full(const gnc_commodity_table * table,
const char * namespace, const char * namespace,
const char * fullname) { const char * fullname)
{
gnc_commodity * retval=NULL; gnc_commodity * retval=NULL;
GList * all; GList * all;
GList * iterator; GList * iterator;
@ -1044,6 +1089,7 @@ gnc_commodity *
gnc_commodity_table_insert(gnc_commodity_table * table, gnc_commodity_table_insert(gnc_commodity_table * table,
gnc_commodity * comm) gnc_commodity * comm)
{ {
GCache *str_cache;
gnc_commodity_namespace * nsp = NULL; gnc_commodity_namespace * nsp = NULL;
gnc_commodity *c; gnc_commodity *c;
@ -1053,39 +1099,33 @@ gnc_commodity_table_insert(gnc_commodity_table * table,
ENTER ("(table=%p, comm=%p) %s %s", table, comm, comm->mnemonic, comm->fullname); ENTER ("(table=%p, comm=%p) %s %s", table, comm, comm->mnemonic, comm->fullname);
c = gnc_commodity_table_lookup (table, comm->namespace, comm->mnemonic); c = gnc_commodity_table_lookup (table, comm->namespace, comm->mnemonic);
if (c) { if (c)
{
if (c == comm) if (c == comm)
{ {
return c; return c;
} }
gnc_commodity_copy (c, comm);
gnc_commodity_set_fullname (c, gnc_commodity_get_fullname (comm));
gnc_commodity_set_fraction (c, gnc_commodity_get_fraction (comm));
gnc_commodity_set_exchange_code (c,
gnc_commodity_get_exchange_code (comm));
gnc_commodity_set_quote_flag (c, gnc_commodity_get_quote_flag (comm));
gnc_commodity_set_quote_source (c, gnc_commodity_get_quote_source (comm));
gnc_commodity_set_quote_tz (c, gnc_commodity_get_quote_tz (comm));
gnc_commodity_destroy (comm); gnc_commodity_destroy (comm);
return c; return c;
} }
nsp = g_hash_table_lookup(table->table, (gpointer)(comm->namespace)); nsp = g_hash_table_lookup(table->table, (gpointer)(comm->namespace));
str_cache = gnc_engine_get_string_cache ();
if(!nsp) if(!nsp)
{ {
nsp = g_new0(gnc_commodity_namespace, 1); nsp = g_new0(gnc_commodity_namespace, 1);
nsp->table = g_hash_table_new(g_str_hash, g_str_equal); nsp->table = g_hash_table_new(g_str_hash, g_str_equal);
nsp->namespace = g_strdup(comm->namespace); nsp->namespace = g_cache_insert(str_cache, comm->namespace);
g_hash_table_insert(table->table, g_hash_table_insert(table->table,
g_strdup(comm->namespace), nsp->namespace,
(gpointer)nsp); (gpointer)nsp);
} }
PINFO ("insert %p %s into nsp=%p %s", comm->mnemonic, comm->mnemonic, nsp->table, nsp->namespace); PINFO ("insert %p %s into nsp=%p %s", comm->mnemonic, comm->mnemonic, nsp->table, nsp->namespace);
g_hash_table_insert(nsp->table, g_hash_table_insert(nsp->table,
(gpointer)g_strdup(comm->mnemonic), (gpointer)g_cache_insert(str_cache, comm->mnemonic),
(gpointer)comm); (gpointer)comm);
LEAVE ("(table=%p, comm=%p)", table, comm); LEAVE ("(table=%p, comm=%p)", table, comm);
@ -1297,16 +1337,19 @@ gnc_commodity_table_add_namespace(gnc_commodity_table * table,
{ {
gnc_commodity_namespace * ns = NULL; gnc_commodity_namespace * ns = NULL;
if(table) { if(table)
{
ns = g_hash_table_lookup(table->table, (gpointer)namespace); ns = g_hash_table_lookup(table->table, (gpointer)namespace);
} }
if(!ns) { if(!ns)
{
GCache *str_cache = gnc_engine_get_string_cache ();
ns = g_new0(gnc_commodity_namespace, 1); ns = g_new0(gnc_commodity_namespace, 1);
ns->table = g_hash_table_new(g_str_hash, g_str_equal); ns->table = g_hash_table_new(g_str_hash, g_str_equal);
ns->namespace = g_strdup(namespace); ns->namespace = g_cache_insert(str_cache, (gpointer)namespace);
g_hash_table_insert(table->table, g_hash_table_insert(table->table,
(gpointer) g_strdup(namespace), (gpointer) ns->namespace,
(gpointer) ns); (gpointer) ns);
} }
} }
@ -1322,7 +1365,6 @@ ns_helper(gpointer key, gpointer value, gpointer user_data)
{ {
gnc_commodity * c = value; gnc_commodity * c = value;
gnc_commodity_destroy(c); gnc_commodity_destroy(c);
g_free(key);
return TRUE; return TRUE;
} }
@ -1330,25 +1372,19 @@ void
gnc_commodity_table_delete_namespace(gnc_commodity_table * table, gnc_commodity_table_delete_namespace(gnc_commodity_table * table,
const char * namespace) const char * namespace)
{ {
gpointer orig_key; gnc_commodity_namespace * ns;
gnc_commodity_namespace * value; if (!table) return;
if(table) ns = g_hash_table_lookup(table->table, namespace);
{ if (ns)
if(g_hash_table_lookup_extended(table->table,
(gpointer) namespace,
&orig_key,
(gpointer)&value))
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
g_hash_table_remove(table->table, namespace); g_hash_table_remove(table->table, namespace);
g_hash_table_foreach_remove(value->table, ns_helper, NULL); g_hash_table_foreach_remove(ns->table, ns_helper, NULL);
g_hash_table_destroy(value->table); g_hash_table_destroy(ns->table);
g_free(value->namespace); g_cache_remove (str_cache, ns->namespace);
g_free(value); g_free(ns);
g_free(orig_key);
}
} }
} }
@ -1409,13 +1445,15 @@ gnc_commodity_table_foreach_commodity (const gnc_commodity_table * tbl,
static int static int
ct_helper(gpointer key, gpointer value, gpointer data) ct_helper(gpointer key, gpointer value, gpointer data)
{ {
GCache *str_cache = gnc_engine_get_string_cache ();
gnc_commodity_namespace * ns = value; gnc_commodity_namespace * ns = value;
g_hash_table_foreach_remove(ns->table, ns_helper, NULL); g_hash_table_foreach_remove(ns->table, ns_helper, NULL);
g_hash_table_destroy(ns->table); g_hash_table_destroy(ns->table);
ns->table = NULL; ns->table = NULL;
g_free(ns->namespace); g_cache_remove (str_cache, ns->namespace);
g_cache_remove (str_cache, key);
g_free(ns); g_free(ns);
g_free(key);
return TRUE; return TRUE;
} }
@ -1432,6 +1470,8 @@ gnc_commodity_table_destroy(gnc_commodity_table * t)
LEAVE ("table=%p", t); LEAVE ("table=%p", t);
} }
/* =========================================================== */
static gboolean static gboolean
table_equal_helper (gnc_commodity *cm_1, gpointer user_data) table_equal_helper (gnc_commodity *cm_1, gpointer user_data)
{ {
@ -1468,6 +1508,23 @@ gnc_commodity_table_equal(gnc_commodity_table *t_1,
return gnc_commodity_table_foreach_commodity (t_2, table_equal_helper, t_1); return gnc_commodity_table_foreach_commodity (t_2, table_equal_helper, t_1);
} }
/* =========================================================== */
static gboolean
table_copy_helper (gnc_commodity *src_cm, gpointer user_data)
{
gnc_commodity_table *dest = user_data;
gnc_commodity_table_insert (dest, gnc_commodity_clone (src_cm));
return TRUE;
}
void
gnc_commodity_table_copy(gnc_commodity_table *dest,
gnc_commodity_table *src)
{
gnc_commodity_table_foreach_commodity (src, table_copy_helper, dest);
}
/******************************************************************** /********************************************************************
* gnc_commodity_table_add_default_data * gnc_commodity_table_add_default_data
********************************************************************/ ********************************************************************/

View File

@ -259,6 +259,12 @@ gnc_commodity * gnc_commodity_new(const char * fullname,
* @param cm The commodity to destroy. * @param cm The commodity to destroy.
*/ */
void gnc_commodity_destroy(gnc_commodity * cm); void gnc_commodity_destroy(gnc_commodity * cm);
/** Copy src into dest */
void gnc_commodity_copy(gnc_commodity * dest, gnc_commodity *src);
/** allocate and copy */
gnc_commodity * gnc_commodity_clone(gnc_commodity *src);
/** @} */ /** @} */
@ -469,16 +475,6 @@ void gnc_commodity_set_exchange_code(gnc_commodity * cm,
*/ */
void gnc_commodity_set_fraction(gnc_commodity * cm, int smallest_fraction); void gnc_commodity_set_fraction(gnc_commodity * cm, int smallest_fraction);
/** Set the 'mark' field for the specified commodity.
*
* @note This is a private field used by the Postgres back end.
*
* @param cm A pointer to a commodity data structure.
*
* @param mark The new value of the mark field.
*/
void gnc_commodity_set_mark(gnc_commodity * cm, gint16 mark);
/** Set the automatic price quote flag for the specified commodity. /** Set the automatic price quote flag for the specified commodity.
* This flag indicates whether stock quotes should be retrieved for * This flag indicates whether stock quotes should be retrieved for
* the specified stock. * the specified stock.
@ -559,49 +555,25 @@ gboolean gnc_commodity_is_iso(const gnc_commodity * cm);
/* =============================================================== */ /* =============================================================== */
/** @name Commodity Table Creation */ /** @name Commodity Table */
/** @{ */ /** @{ */
/* gnc_commodity_table functions : operate on a database of commodity
* info */
/** You proably shouldn't be using gnc_commodity_table_new() directly,
* its for internal use only. You should probably be using
* gnc_commodity_table_get_table()
*/
gnc_commodity_table * gnc_commodity_table_new(void);
void gnc_commodity_table_destroy(gnc_commodity_table * table);
/** Returns the commodity table assoicated with a book. /** Returns the commodity table assoicated with a book.
*/ */
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book); gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book);
/** You should probably not be using gnc_commodity_table_set_table()
* directly. Its for internal use only.
*/
void gnc_commodity_table_set_table(QofBook *book, gnc_commodity_table *ct);
/** You should probably not be using gnc_commodity_table_register()
* It is an internal routine for registering the gncObject for the
* commodity table.
*/
gboolean gnc_commodity_table_register (void);
/** @} */
/* XXX backwards compat function; remove me someday */ /* XXX backwards compat function; remove me someday */
#define gnc_book_get_commodity_table gnc_commodity_table_get_table #define gnc_book_get_commodity_table gnc_commodity_table_get_table
/** compare two tables for equality */
/** @name Commodity Table Comparison */
/** @{ */
gboolean gnc_commodity_table_equal(gnc_commodity_table *t_1, gboolean gnc_commodity_table_equal(gnc_commodity_table *t_1,
gnc_commodity_table *t_2); gnc_commodity_table *t_2);
/** copy all commodities from src table to dest table */
void gnc_commodity_table_copy(gnc_commodity_table *dest,
gnc_commodity_table *src);
/** @} */ /** @} */
/* ---------------------------------------------------------- */
/** @name Commodity Table Lookup functions */ /** @name Commodity Table Lookup functions */
/** @{ */ /** @{ */
gnc_commodity * gnc_commodity_table_lookup(const gnc_commodity_table * table, gnc_commodity * gnc_commodity_table_lookup(const gnc_commodity_table * table,
@ -614,8 +586,7 @@ gnc_commodity * gnc_commodity_table_find_full(const gnc_commodity_table * t,
const char * namespace, const char * namespace,
const char * fullname); const char * fullname);
/** @} */ /** @} */
/* ---------------------------------------------------------- */
/** @name Commodity Table Maintenance functions */ /** @name Commodity Table Maintenance functions */
/** @{ */ /** @{ */
@ -653,10 +624,9 @@ void gnc_commodity_table_remove(gnc_commodity_table * table,
* *
* @param table A pointer to the commodity table for the book. */ * @param table A pointer to the commodity table for the book. */
gboolean gnc_commodity_table_add_default_data(gnc_commodity_table *table); gboolean gnc_commodity_table_add_default_data(gnc_commodity_table *table);
/** @} */ /** @} */
/* ---------------------------------------------------------- */
/** @name Commodity Table Namespace functions */ /** @name Commodity Table Namespace functions */
/** @{ */ /** @{ */
@ -711,8 +681,7 @@ void gnc_commodity_table_add_namespace(gnc_commodity_table * table,
void gnc_commodity_table_delete_namespace(gnc_commodity_table * t, void gnc_commodity_table_delete_namespace(gnc_commodity_table * t,
const char * namespace); const char * namespace);
/** @} */ /** @} */
/* ---------------------------------------------------------- */
/** @name Commodity Table Accessor functions */ /** @name Commodity Table Accessor functions */
/** @{ */ /** @{ */
@ -777,5 +746,40 @@ gboolean gnc_commodity_table_foreach_commodity(const gnc_commodity_table * table
gpointer user_data); gpointer user_data);
/** @} */ /** @} */
/* ---------------------------------------------------------- */
/** @name Commodity Table Private/Internal-Use Only Routines */
/** @{ */
/** Set the 'mark' field for the specified commodity.
*
* @note This is a private field used by the Postgres back end.
*
* @param cm A pointer to a commodity data structure.
*
* @param mark The new value of the mark field.
*/
void gnc_commodity_set_mark(gnc_commodity * cm, gint16 mark);
/** You proably shouldn't be using gnc_commodity_table_new() directly,
* its for internal use only. You should probably be using
* gnc_commodity_table_get_table()
*/
gnc_commodity_table * gnc_commodity_table_new(void);
void gnc_commodity_table_destroy(gnc_commodity_table * table);
/** You should probably not be using gnc_commodity_table_set_table()
* directly. Its for internal use only.
*/
void gnc_commodity_table_set_table(QofBook *book, gnc_commodity_table *ct);
/** You should probably not be using gnc_commodity_table_register()
* It is an internal routine for registering the gncObject for the
* commodity table.
*/
gboolean gnc_commodity_table_register (void);
/** @} */
#endif /* GNC_COMMODITY_H */ #endif /* GNC_COMMODITY_H */
/** @} */ /** @} */