summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKai Wohlfahrt <kai.wohlfahrt@gmail.com>2020-06-10 16:48:52 +0100
committerMartin Michlmayr <tbm@cyrius.com>2021-01-31 09:59:02 +0800
commitff1d1d9e5c93fa84a3abcb5bb5e21013ee8f77d9 (patch)
tree6ce78371b8d99e33352a8713c6c7865450c76129 /src
parent0e44585153b82c9e6730e49af3fa27aeb5b82b5f (diff)
downloadfork-ledger-ff1d1d9e5c93fa84a3abcb5bb5e21013ee8f77d9.tar.gz
fork-ledger-ff1d1d9e5c93fa84a3abcb5bb5e21013ee8f77d9.tar.bz2
fork-ledger-ff1d1d9e5c93fa84a3abcb5bb5e21013ee8f77d9.zip
Replace if/else ladder in convert with loop
Diffstat (limited to 'src')
-rw-r--r--src/csv.cc27
-rw-r--r--src/csv.h33
2 files changed, 19 insertions, 41 deletions
diff --git a/src/csv.cc b/src/csv.cc
index 4ee7ff60..632ec8d6 100644
--- a/src/csv.cc
+++ b/src/csv.cc
@@ -107,28 +107,13 @@ void csv_reader::read_index(std::istream& in)
string field = read_field(instr);
names.push_back(field);
- if (date_mask.match(field))
- index.push_back(FIELD_DATE);
- else if (date_aux_mask.match(field))
- index.push_back(FIELD_DATE_AUX);
- else if (code_mask.match(field))
- index.push_back(FIELD_CODE);
- else if (payee_mask.match(field))
- index.push_back(FIELD_PAYEE);
- else if (credit_mask.match(field))
- index.push_back(FIELD_CREDIT);
- else if (debit_mask.match(field))
- index.push_back(FIELD_DEBIT);
- else if (cost_mask.match(field))
- index.push_back(FIELD_COST);
- else if (total_mask.match(field))
- index.push_back(FIELD_TOTAL);
- else if (note_mask.match(field))
- index.push_back(FIELD_NOTE);
- else
- index.push_back(FIELD_UNKNOWN);
-
DEBUG("csv.parse", "Header field: " << field);
+ for (auto& mask : masks) {
+ if (mask.first.match(field)) {
+ index.push_back(mask.second);
+ break;
+ }
+ }
}
}
diff --git a/src/csv.h b/src/csv.h
index 9747a775..d7083211 100644
--- a/src/csv.h
+++ b/src/csv.h
@@ -71,31 +71,24 @@ class csv_reader
FIELD_UNKNOWN
};
- mask_t date_mask;
- mask_t date_aux_mask;
- mask_t code_mask;
- mask_t payee_mask;
- mask_t credit_mask;
- mask_t debit_mask;
- mask_t cost_mask;
- mask_t total_mask;
- mask_t note_mask;
-
- std::vector<int> index;
+ std::array<std::pair<mask_t, headers_t>, 10> masks;
+
+ std::vector<headers_t> index;
std::vector<string> names;
public:
csv_reader(parse_context_t& _context)
: context(_context),
- date_mask("date"),
- date_aux_mask("posted( ?date)?"),
- code_mask("code"),
- payee_mask("(payee|desc(ription)?|title)"),
- credit_mask("credit|amount"),
- debit_mask("debit"),
- cost_mask("cost"),
- total_mask("total"),
- note_mask("note") {
+ masks{ std::make_pair(mask_t("date"), FIELD_DATE),
+ std::make_pair(mask_t("posted( ?date)?"), FIELD_DATE_AUX),
+ std::make_pair(mask_t("code"), FIELD_CODE),
+ std::make_pair(mask_t("(payee|desc(ription)?|title)"), FIELD_PAYEE),
+ std::make_pair(mask_t("credit|amount"), FIELD_CREDIT),
+ std::make_pair(mask_t("debit"), FIELD_DEBIT),
+ std::make_pair(mask_t("cost"), FIELD_COST),
+ std::make_pair(mask_t("total"), FIELD_TOTAL),
+ std::make_pair(mask_t("note"), FIELD_NOTE),
+ std::make_pair(mask_t(""), FIELD_UNKNOWN) } {
read_index(*context.stream.get());
TRACE_CTOR(csv_reader, "parse_context_t&");
}