diff --git a/libgnucash/engine/gnc-commodity.cpp b/libgnucash/engine/gnc-commodity.cpp index 1bbef638a1..26ef9af7f6 100644 --- a/libgnucash/engine/gnc-commodity.cpp +++ b/libgnucash/engine/gnc-commodity.cpp @@ -42,6 +42,7 @@ #include "guid.h" #include "qofinstance.h" +#include #include static QofLogModule log_module = GNC_MOD_COMMODITY; @@ -166,7 +167,7 @@ public: , m_internal_name{int_name ? int_name: ""} { }; }; -using QuoteSourceVec = std::vector; +using QuoteSourceList = std::list; /* To update the following lists scan * from github.com/finance-quote/finance-quote @@ -176,7 +177,7 @@ using QuoteSourceVec = std::vector; * * Apply changes here also to the FQ appendix of help. */ -static QuoteSourceVec currency_quote_sources = +static QuoteSourceList currency_quote_sources = { { true, SOURCE_CURRENCY, "Currency", "currency" } }; @@ -185,7 +186,7 @@ static QuoteSourceVec currency_quote_sources = * sometimes it gets the suffix "_direct" * and the failover method is without suffix. */ -static QuoteSourceVec single_quote_sources = +static QuoteSourceList single_quote_sources = { { false, SOURCE_SINGLE, "Alphavantage, US", "alphavantage" }, { false, SOURCE_SINGLE, "Amsterdam Euronext eXchange, NL", "aex" }, @@ -218,7 +219,7 @@ static QuoteSourceVec single_quote_sources = { false, SOURCE_SINGLE, "Yahoo Web", "yahooweb" }, }; -static QuoteSourceVec multiple_quote_sources = +static QuoteSourceList multiple_quote_sources = { { false, SOURCE_MULTI, "Australia (ASX, ...)", "australia" }, { false, SOURCE_MULTI, "Canada (Alphavantage, TSX, ...)", "canada" }, @@ -236,10 +237,10 @@ static QuoteSourceVec multiple_quote_sources = { false, SOURCE_MULTI, "USA (alphavantage, yahoo_json, ...)", "usa" }, }; -static QuoteSourceVec new_quote_sources; +static QuoteSourceList new_quote_sources; // cannot use map or unordered_map because order must be preserved -static const std::vector> quote_sources_map = +static const std::vector> quote_sources_map = { { SOURCE_CURRENCY, currency_quote_sources }, { SOURCE_SINGLE, single_quote_sources }, @@ -272,7 +273,7 @@ gnc_quote_source_fq_version (void) return fq_version.c_str(); } -static QuoteSourceVec& +static QuoteSourceList& get_quote_source_from_type (QuoteSourceType type) { auto quote_sources_it = std::find_if (quote_sources_map.begin(), quote_sources_map.end(), @@ -292,7 +293,8 @@ get_quote_source_from_type (QuoteSourceType type) ********************************************************************/ gint gnc_quote_source_num_entries(QuoteSourceType type) { - return get_quote_source_from_type(type).size(); + auto source{get_quote_source_from_type(type)}; + return std::distance(source.begin(), source.end()); } @@ -329,9 +331,9 @@ gnc_quote_source_lookup_by_ti (QuoteSourceType type, gint index) auto& sources = get_quote_source_from_type (type); if ((size_t) index < sources.size()) { - auto& source = sources[index]; - LEAVE("found %s", source.get_user_name()); - return &source; + auto it = std::next(sources.begin(), index); + LEAVE("found %s", it->get_user_name()); + return &*it; } LEAVE("not found"); @@ -1041,7 +1043,7 @@ gnc_commodity_get_quote_source(const gnc_commodity *cm) if (!cm) return nullptr; priv = GET_PRIVATE(cm); if (!priv->quote_source && gnc_commodity_is_iso(cm)) - return ¤cy_quote_sources[0]; + return ¤cy_quote_sources.front(); return priv->quote_source; } @@ -1049,7 +1051,7 @@ gnc_quote_source* gnc_commodity_get_default_quote_source(const gnc_commodity *cm) { if (cm && gnc_commodity_is_iso(cm)) - return ¤cy_quote_sources[0]; + return ¤cy_quote_sources.front(); /* Should make this a user option at some point. */ return gnc_quote_source_lookup_by_internal("alphavantage"); } diff --git a/libgnucash/engine/test/test-commodities.cpp b/libgnucash/engine/test/test-commodities.cpp index 14cab424fa..f2fe8ef58a 100644 --- a/libgnucash/engine/test/test-commodities.cpp +++ b/libgnucash/engine/test/test-commodities.cpp @@ -23,6 +23,7 @@ * 02110-1301, USA. */ #include +#include #include #include "gnc-commodity.h" @@ -53,6 +54,17 @@ test_quote_sources () // internal name: do_test (gnc_quote_source_lookup_by_internal("treasure") == nullptr, "lookup_by_internal: treasure doesn't exist"); + + auto first = gnc_quote_source_lookup_by_ti (SOURCE_UNKNOWN, 0); + g_assert (first != nullptr); + + auto name = gnc_quote_source_get_user_name (first); + do_test (!g_strcmp0 (name, "test-source"), "get_user_name: name is as expected"); + + for (auto i = 0; i < 20; ++i) + gnc_quote_source_add_new (std::string(std::to_string(i)).c_str(), false); + + do_test (gnc_quote_source_get_user_name (first) == name, "get_user_name hasn't moved"); } static void