Skip to content

Commit

Permalink
[gnc-commodity.cpp] [untested] dynamically find index, remove init
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Feb 14, 2024
1 parent 34cb43e commit dff80c0
Showing 1 changed file with 112 additions and 115 deletions.
227 changes: 112 additions & 115 deletions libgnucash/engine/gnc-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,20 @@ struct gnc_quote_source_s
private:
gboolean m_supported;
QuoteSourceType m_type;
gint m_index;
std::string m_user_name; /* User friendly name incl. region code*/
std::string m_old_internal_name; /* Name used internally (deprecated) */
std::string m_internal_name; /* Name used internally and by finance::quote. */
public:
bool get_supported () const { return m_supported; }
void set_supported (bool supported) { m_supported = supported; }
QuoteSourceType get_type () const { return m_type; }
size_t get_index () const { return m_index; }
void set_index (size_t index) { m_index = index; }
const char* get_user_name () const { return m_user_name.c_str(); }
const char* get_old_internal_name () const { return m_old_internal_name.c_str(); }
const char* get_internal_name () const { return m_internal_name.c_str(); }
gnc_quote_source_s (gboolean supported, QuoteSourceType type, gint index,
gnc_quote_source_s (gboolean supported, QuoteSourceType type,
const char* username, const char* old_int_name, const char* int_name)
: m_supported{supported}
, m_type{type}
, m_index{index}
, m_user_name{username}
, m_old_internal_name{old_int_name}
, m_internal_name{int_name} { };
Expand All @@ -186,100 +182,100 @@ struct gnc_quote_source_s
* Apply changes here also to the FQ appendix of help.
*/
static gnc_quote_source currency_quote_source =
{ true, SOURCE_CURRENCY, 0, "Currency", "CURRENCY", "currency" };
{ true, SOURCE_CURRENCY, "Currency", "CURRENCY", "currency" };

/* The single quote method is usually the module name, but
* sometimes it gets the suffix "_direct"
* and the failover method is without suffix.
*/
static gnc_quote_source single_quote_sources[] =
{
{ false, SOURCE_SINGLE, 0, "Alphavantage, US", "ALPHAVANTAGE", "alphavantage" },
{ false, SOURCE_SINGLE, 0, "Amsterdam Euronext eXchange, NL", "AEX", "aex" },
{ false, SOURCE_SINGLE, 0, "American International Assurance, HK", "AIAHK", "aiahk" },
{ false, SOURCE_SINGLE, 0, "Association of Mutual Funds in India", "AMFIINDIA", "amfiindia" },
{ false, SOURCE_SINGLE, 0, "Athens Stock Exchange, GR", "ASEGR", "asegr" },
{ false, SOURCE_SINGLE, 0, "Australian Stock Exchange, AU", "ASX", "asx" },
{ false, SOURCE_SINGLE, 0, "BAMOSZ funds, HU", "BAMOSZ", "bamosz" },
{ false, SOURCE_SINGLE, 0, "BMO NesbittBurns, CA", "BMONESBITTBURNS", "bmonesbittburns" },
{ false, SOURCE_SINGLE, 0, "Bucharest Stock Exchange, RO", "BSERO", "bsero" },
{ false, SOURCE_SINGLE, 0, "Budapest Stock Exchange (BET), ex-BUX, HU", "BSE", "bse" },
{ false, SOURCE_SINGLE, 0, "Canada Mutual", "CANADAMUTUAL", "canadamutual" },
{ false, SOURCE_SINGLE, 0, "Citywire Funds, GB", "citywire", "citywire" },
{ false, SOURCE_SINGLE, 0, "Colombo Stock Exchange, LK", "CSE", "cse" },
{ false, SOURCE_SINGLE, 0, "Cominvest, ex-Adig, DE", "COMINVEST", "cominvest" },
{ false, SOURCE_SINGLE, 0, "Deka Investments, DE", "DEKA", "deka" },
{ false, SOURCE_SINGLE, 0, "Dutch", "DUTCH", "dutch" },
{ false, SOURCE_SINGLE, 0, "DWS, DE", "DWS", "dwsfunds" },
{ false, SOURCE_SINGLE, 0, "Equinox Unit Trusts, ZA", "ZA_unittrusts", "za_unittrusts" },
{ false, SOURCE_SINGLE, 0, "Fidelity Direct", "FIDELITY_DIRECT", "fidelity_direct" },
{ false, SOURCE_SINGLE, 0, "Fidelity Fixed", "FIDELITY_DIRECT", "fidelityfixed" },
{ false, SOURCE_SINGLE, 0, "Finance Canada", "FINANCECANADA", "financecanada" },
{ false, SOURCE_SINGLE, 0, "Financial Times Funds service, GB", "FTFUNDS", "ftfunds" },
{ false, SOURCE_SINGLE, 0, "Finanzpartner, DE", "FINANZPARTNER", "finanzpartner" },
{ false, SOURCE_SINGLE, 0, "First Trust Portfolios, US", "FTPORTFOLIOS", "ftportfolios" },
{ false, SOURCE_SINGLE, 0, "Fund Library, CA", "FUNDLIBRARY", "fundlibrary" },
{ false, SOURCE_SINGLE, 0, "GoldMoney spot rates, JE", "GOLDMONEY", "goldmoney" },
{ false, SOURCE_SINGLE, 0, "Greece", "GREECE", "greece" },
{ false, SOURCE_SINGLE, 0, "Helsinki stock eXchange, FI", "HEX", "hex" },
{ false, SOURCE_SINGLE, 0, "Hungary", "HU", "hu" },
{ false, SOURCE_SINGLE, 0, "India Mutual", "INDIAMUTUAL", "indiamutual" },
{ false, SOURCE_SINGLE, 0, "Man Investments, AU", "maninv", "maninv" },
{ false, SOURCE_SINGLE, 0, "Morningstar, GB", "MSTARUK", "mstaruk" },
{ false, SOURCE_SINGLE, 0, "Morningstar, JP", "MORNINGSTARJP", "morningstarjp" },
{ false, SOURCE_SINGLE, 0, "Morningstar, SE", "MORNINGSTAR", "morningstar" },
{ false, SOURCE_SINGLE, 0, "Motley Fool, US", "FOOL", "fool" },
{ false, SOURCE_SINGLE, 0, "New Zealand stock eXchange, NZ", "NZX", "nzx" },
{ false, SOURCE_SINGLE, 0, "Paris Stock Exchange/Boursorama, FR", "BOURSO", "bourso" },
{ false, SOURCE_SINGLE, 0, "Paris Stock Exchange/LeRevenu, FR", "LEREVENU", "lerevenu" },
{ false, SOURCE_SINGLE, 0, "Platinum Asset Management, AU", "PLATINUM", "platinum" },
{ false, SOURCE_SINGLE, 0, "Romania", "romania", "romania" },
{ false, SOURCE_SINGLE, 0, "SIX Swiss Exchange funds, CH", "SIXFUNDS", "sixfunds" },
{ false, SOURCE_SINGLE, 0, "SIX Swiss Exchange shares, CH", "SIXSHARES", "sixshares" },
{ false, SOURCE_SINGLE, 0, "Skandinaviska Enskilda Banken, SE", "SEB_FUNDS", "seb_funds" },
{ false, SOURCE_SINGLE, 0, "Sharenet, ZA", "ZA", "za" },
{ false, SOURCE_SINGLE, 0, "StockHouse Canada", "STOCKHOUSE_FUND", "stockhousecanada_fund" },
{ false, SOURCE_SINGLE, 0, "TD Waterhouse Funds, CA", "TDWATERHOUSE", "tdwaterhouse" },
{ false, SOURCE_SINGLE, 0, "TD Efunds, CA", "TDEFUNDS", "tdefunds" },
{ false, SOURCE_SINGLE, 0, "TIAA-CREF, US", "TIAACREF", "tiaacref" },
{ false, SOURCE_SINGLE, 0, "Toronto Stock eXchange, CA", "TSX", "tsx" },
{ false, SOURCE_SINGLE, 0, "T. Rowe Price", "TRPRICE", "troweprice" },
{ false, SOURCE_SINGLE, 0, "T. Rowe Price, US", "TRPRICE_DIRECT", "troweprice_direct" },
{ false, SOURCE_SINGLE, 0, "Trustnet via tnetuk.pm, GB", "TNETUK", "tnetuk" },
{ false, SOURCE_SINGLE, 0, "Trustnet via trustnet.pm, GB", "TRUSTNET", "trustnet" },
{ false, SOURCE_SINGLE, 0, "U.K. Unit Trusts", "UKUNITTRUSTS", "uk_unit_trusts" },
{ false, SOURCE_SINGLE, 0, "Union Investment, DE", "UNIONFUNDS", "unionfunds" },
{ false, SOURCE_SINGLE, 0, "US Treasury Bonds", "usfedbonds", "usfedbonds" },
{ false, SOURCE_SINGLE, 0, "US Govt. Thrift Savings Plan", "TSP", "tsp" },
{ false, SOURCE_SINGLE, 0, "Vanguard", "VANGUARD", "vanguard" }, /* Method of Alphavantage */
{ false, SOURCE_SINGLE, 0, "VWD, DE (unmaintained)", "VWD", "vwd" },
{ false, SOURCE_SINGLE, 0, "Yahoo as JSON", "YAHOO_JSON", "yahoo_json" },
{ false, SOURCE_SINGLE, 0, "Yahoo as YQL", "YAHOO_YQL", "yahoo_yql" },
{ false, SOURCE_SINGLE, "Alphavantage, US", "ALPHAVANTAGE", "alphavantage" },
{ false, SOURCE_SINGLE, "Amsterdam Euronext eXchange, NL", "AEX", "aex" },
{ false, SOURCE_SINGLE, "American International Assurance, HK", "AIAHK", "aiahk" },
{ false, SOURCE_SINGLE, "Association of Mutual Funds in India", "AMFIINDIA", "amfiindia" },
{ false, SOURCE_SINGLE, "Athens Stock Exchange, GR", "ASEGR", "asegr" },
{ false, SOURCE_SINGLE, "Australian Stock Exchange, AU", "ASX", "asx" },
{ false, SOURCE_SINGLE, "BAMOSZ funds, HU", "BAMOSZ", "bamosz" },
{ false, SOURCE_SINGLE, "BMO NesbittBurns, CA", "BMONESBITTBURNS", "bmonesbittburns" },
{ false, SOURCE_SINGLE, "Bucharest Stock Exchange, RO", "BSERO", "bsero" },
{ false, SOURCE_SINGLE, "Budapest Stock Exchange (BET), ex-BUX, HU", "BSE", "bse" },
{ false, SOURCE_SINGLE, "Canada Mutual", "CANADAMUTUAL", "canadamutual" },
{ false, SOURCE_SINGLE, "Citywire Funds, GB", "citywire", "citywire" },
{ false, SOURCE_SINGLE, "Colombo Stock Exchange, LK", "CSE", "cse" },
{ false, SOURCE_SINGLE, "Cominvest, ex-Adig, DE", "COMINVEST", "cominvest" },
{ false, SOURCE_SINGLE, "Deka Investments, DE", "DEKA", "deka" },
{ false, SOURCE_SINGLE, "Dutch", "DUTCH", "dutch" },
{ false, SOURCE_SINGLE, "DWS, DE", "DWS", "dwsfunds" },
{ false, SOURCE_SINGLE, "Equinox Unit Trusts, ZA", "ZA_unittrusts", "za_unittrusts" },
{ false, SOURCE_SINGLE, "Fidelity Direct", "FIDELITY_DIRECT", "fidelity_direct" },
{ false, SOURCE_SINGLE, "Fidelity Fixed", "FIDELITY_DIRECT", "fidelityfixed" },
{ false, SOURCE_SINGLE, "Finance Canada", "FINANCECANADA", "financecanada" },
{ false, SOURCE_SINGLE, "Financial Times Funds service, GB", "FTFUNDS", "ftfunds" },
{ false, SOURCE_SINGLE, "Finanzpartner, DE", "FINANZPARTNER", "finanzpartner" },
{ false, SOURCE_SINGLE, "First Trust Portfolios, US", "FTPORTFOLIOS", "ftportfolios" },
{ false, SOURCE_SINGLE, "Fund Library, CA", "FUNDLIBRARY", "fundlibrary" },
{ false, SOURCE_SINGLE, "GoldMoney spot rates, JE", "GOLDMONEY", "goldmoney" },
{ false, SOURCE_SINGLE, "Greece", "GREECE", "greece" },
{ false, SOURCE_SINGLE, "Helsinki stock eXchange, FI", "HEX", "hex" },
{ false, SOURCE_SINGLE, "Hungary", "HU", "hu" },
{ false, SOURCE_SINGLE, "India Mutual", "INDIAMUTUAL", "indiamutual" },
{ false, SOURCE_SINGLE, "Man Investments, AU", "maninv", "maninv" },
{ false, SOURCE_SINGLE, "Morningstar, GB", "MSTARUK", "mstaruk" },
{ false, SOURCE_SINGLE, "Morningstar, JP", "MORNINGSTARJP", "morningstarjp" },
{ false, SOURCE_SINGLE, "Morningstar, SE", "MORNINGSTAR", "morningstar" },
{ false, SOURCE_SINGLE, "Motley Fool, US", "FOOL", "fool" },
{ false, SOURCE_SINGLE, "New Zealand stock eXchange, NZ", "NZX", "nzx" },
{ false, SOURCE_SINGLE, "Paris Stock Exchange/Boursorama, FR", "BOURSO", "bourso" },
{ false, SOURCE_SINGLE, "Paris Stock Exchange/LeRevenu, FR", "LEREVENU", "lerevenu" },
{ false, SOURCE_SINGLE, "Platinum Asset Management, AU", "PLATINUM", "platinum" },
{ false, SOURCE_SINGLE, "Romania", "romania", "romania" },
{ false, SOURCE_SINGLE, "SIX Swiss Exchange funds, CH", "SIXFUNDS", "sixfunds" },
{ false, SOURCE_SINGLE, "SIX Swiss Exchange shares, CH", "SIXSHARES", "sixshares" },
{ false, SOURCE_SINGLE, "Skandinaviska Enskilda Banken, SE", "SEB_FUNDS", "seb_funds" },
{ false, SOURCE_SINGLE, "Sharenet, ZA", "ZA", "za" },
{ false, SOURCE_SINGLE, "StockHouse Canada", "STOCKHOUSE_FUND", "stockhousecanada_fund" },
{ false, SOURCE_SINGLE, "TD Waterhouse Funds, CA", "TDWATERHOUSE", "tdwaterhouse" },
{ false, SOURCE_SINGLE, "TD Efunds, CA", "TDEFUNDS", "tdefunds" },
{ false, SOURCE_SINGLE, "TIAA-CREF, US", "TIAACREF", "tiaacref" },
{ false, SOURCE_SINGLE, "Toronto Stock eXchange, CA", "TSX", "tsx" },
{ false, SOURCE_SINGLE, "T. Rowe Price", "TRPRICE", "troweprice" },
{ false, SOURCE_SINGLE, "T. Rowe Price, US", "TRPRICE_DIRECT", "troweprice_direct" },
{ false, SOURCE_SINGLE, "Trustnet via tnetuk.pm, GB", "TNETUK", "tnetuk" },
{ false, SOURCE_SINGLE, "Trustnet via trustnet.pm, GB", "TRUSTNET", "trustnet" },
{ false, SOURCE_SINGLE, "U.K. Unit Trusts", "UKUNITTRUSTS", "uk_unit_trusts" },
{ false, SOURCE_SINGLE, "Union Investment, DE", "UNIONFUNDS", "unionfunds" },
{ false, SOURCE_SINGLE, "US Treasury Bonds", "usfedbonds", "usfedbonds" },
{ false, SOURCE_SINGLE, "US Govt. Thrift Savings Plan", "TSP", "tsp" },
{ false, SOURCE_SINGLE, "Vanguard", "VANGUARD", "vanguard" }, /* Method of Alphavantage */
{ false, SOURCE_SINGLE, "VWD, DE (unmaintained)", "VWD", "vwd" },
{ false, SOURCE_SINGLE, "Yahoo as JSON", "YAHOO_JSON", "yahoo_json" },
{ false, SOURCE_SINGLE, "Yahoo as YQL", "YAHOO_YQL", "yahoo_yql" },
};

static gnc_quote_source multiple_quote_sources[] =
{
{ false, SOURCE_MULTI, 0, "Australia (ASX, ...)", "AUSTRALIA", "australia" },
{ false, SOURCE_MULTI, 0, "Canada (Alphavantage, TSX, ...)", "CANADA", "canada" },
{ false, SOURCE_MULTI, 0, "Canada Mutual (Fund Library, StockHouse, ...)", "CANADAMUTUAL", "canadamutual" },
{ false, SOURCE_MULTI, 0, "Dutch (AEX, ...)", "DUTCH", "dutch" },
{ false, SOURCE_MULTI, 0, "Europe (asegr,.bsero, hex ...)", "EUROPE", "europe" },
{ false, SOURCE_MULTI, 0, "Greece (ASE, ...)", "GREECE", "greece" },
{ false, SOURCE_MULTI, 0, "Hungary (Bamosz, BET, ...)", "HU", "hu" },
{ false, SOURCE_MULTI, 0, "India Mutual (AMFI, ...)", "INDIAMUTUAL", "indiamutual" },
{ false, SOURCE_MULTI, 0, "Fidelity (Fidelity, ...)", "FIDELITY", "fidelity" },
{ false, SOURCE_MULTI, 0, "Finland (HEX, ...)", "FINLAND", "finland" },
{ false, SOURCE_MULTI, 0, "First Trust (First Trust, ...)", "FTPORTFOLIOS", "ftportfolios" },
{ false, SOURCE_MULTI, 0, "France (bourso, ĺerevenu, ...)", "FRANCE", "france" },
{ false, SOURCE_MULTI, 0, "Nasdaq (alphavantage, fool, ...)", "NASDAQ", "nasdaq" },
{ false, SOURCE_MULTI, 0, "New Zealand (NZX, ...)", "NZ", "nz" },
{ false, SOURCE_MULTI, 0, "NYSE (alphavantage, fool, ...)", "NYSE", "nyse" },
{ false, SOURCE_MULTI, 0, "South Africa (Sharenet, ...)", "ZA", "za" },
{ false, SOURCE_MULTI, 0, "Romania (BSE-RO, ...)", "romania", "romania" },
{ false, SOURCE_MULTI, 0, "T. Rowe Price", "TRPRICE", "troweprice" },
{ false, SOURCE_MULTI, 0, "U.K. Funds (citywire, FTfunds, MStar, tnetuk, ...)", "ukfunds", "ukfunds" },
{ false, SOURCE_MULTI, 0, "U.K. Unit Trusts (trustnet, ...)", "UKUNITTRUSTS", "uk_unit_trusts" },
{ false, SOURCE_MULTI, 0, "USA (Alphavantage, Fool, ...)", "USA", "usa" },
{ false, SOURCE_MULTI, "Australia (ASX, ...)", "AUSTRALIA", "australia" },
{ false, SOURCE_MULTI, "Canada (Alphavantage, TSX, ...)", "CANADA", "canada" },
{ false, SOURCE_MULTI, "Canada Mutual (Fund Library, StockHouse, ...)", "CANADAMUTUAL", "canadamutual" },
{ false, SOURCE_MULTI, "Dutch (AEX, ...)", "DUTCH", "dutch" },
{ false, SOURCE_MULTI, "Europe (asegr,.bsero, hex ...)", "EUROPE", "europe" },
{ false, SOURCE_MULTI, "Greece (ASE, ...)", "GREECE", "greece" },
{ false, SOURCE_MULTI, "Hungary (Bamosz, BET, ...)", "HU", "hu" },
{ false, SOURCE_MULTI, "India Mutual (AMFI, ...)", "INDIAMUTUAL", "indiamutual" },
{ false, SOURCE_MULTI, "Fidelity (Fidelity, ...)", "FIDELITY", "fidelity" },
{ false, SOURCE_MULTI, "Finland (HEX, ...)", "FINLAND", "finland" },
{ false, SOURCE_MULTI, "First Trust (First Trust, ...)", "FTPORTFOLIOS", "ftportfolios" },
{ false, SOURCE_MULTI, "France (bourso, ĺerevenu, ...)", "FRANCE", "france" },
{ false, SOURCE_MULTI, "Nasdaq (alphavantage, fool, ...)", "NASDAQ", "nasdaq" },
{ false, SOURCE_MULTI, "New Zealand (NZX, ...)", "NZ", "nz" },
{ false, SOURCE_MULTI, "NYSE (alphavantage, fool, ...)", "NYSE", "nyse" },
{ false, SOURCE_MULTI, "South Africa (Sharenet, ...)", "ZA", "za" },
{ false, SOURCE_MULTI, "Romania (BSE-RO, ...)", "romania", "romania" },
{ false, SOURCE_MULTI, "T. Rowe Price", "TRPRICE", "troweprice" },
{ false, SOURCE_MULTI, "U.K. Funds (citywire, FTfunds, MStar, tnetuk, ...)", "ukfunds", "ukfunds" },
{ false, SOURCE_MULTI, "U.K. Unit Trusts (trustnet, ...)", "UKUNITTRUSTS", "uk_unit_trusts" },
{ false, SOURCE_MULTI, "USA (Alphavantage, Fool, ...)", "USA", "usa" },
};

static const int num_single_quote_sources =
Expand Down Expand Up @@ -333,26 +329,6 @@ gint gnc_quote_source_num_entries(QuoteSourceType type)
return new_quote_sources.size();
}

/********************************************************************
* gnc_quote_source_init_tables
*
* Update the type/index values for prices sources.
********************************************************************/
static void
gnc_quote_source_init_tables (void)
{
gint i;

for (i = 0; i < num_single_quote_sources; i++)
{
single_quote_sources[i].set_index(i);
}

for (i = 0; i < num_multiple_quote_sources; i++)
{
multiple_quote_sources[i].set_index(i);
}
}


/********************************************************************
Expand All @@ -372,7 +348,7 @@ gnc_quote_source_add_new (const char *source_name, gboolean supported)
/* This name is permanent and must be kept the same if/when support
* for this price source is integrated into gnucash (i.e. for a
* nice user name). */
return &new_quote_sources.emplace_back (supported, SOURCE_UNKNOWN, (gint)new_quote_sources.size(),
return &new_quote_sources.emplace_back (supported, SOURCE_UNKNOWN,
source_name, source_name, source_name);
}

Expand Down Expand Up @@ -488,15 +464,38 @@ gnc_quote_source_get_type (const gnc_quote_source *source)
gint
gnc_quote_source_get_index (const gnc_quote_source *source)
{
ENTER("%p", source);
if (!source)
{
LEAVE("bad source");
PWARN ("bad source");
return 0;
}

LEAVE("index is %ld", source->get_index());
return source->get_index();
switch (source->get_type())
{
case SOURCE_CURRENCY:
return 0;
case SOURCE_SINGLE:
for (auto i = 0; i < num_single_quote_sources; ++i)
if (&single_quote_sources[i] == source)
return i;
break;
case SOURCE_MULTI:
for (auto i = 0; i < num_multiple_quote_sources; ++i)
if (&multiple_quote_sources[i] == source)
return i;
break;
case SOURCE_UNKNOWN:
{
for (size_t i = 0; i < new_quote_sources.size(); ++i)
if (&new_quote_sources[i] == source)
return i;
break;
}
default:
break;
}
PWARN ("couldn't locate source");
return 0;
}

gboolean
Expand Down Expand Up @@ -2529,8 +2528,6 @@ static QofObject commodity_table_object_def =
gboolean
gnc_commodity_table_register (void)
{
gnc_quote_source_init_tables();

if (!qof_object_register (&commodity_object_def))
return FALSE;
if (!qof_object_register (&namespace_object_def))
Expand Down

0 comments on commit dff80c0

Please sign in to comment.