diff options
-rw-r--r-- | src/csv.cc | 27 | ||||
-rw-r--r-- | src/csv.h | 33 |
2 files changed, 19 insertions, 41 deletions
@@ -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; + } + } } } @@ -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&"); } |