Loading...
Searching...
No Matches
csv.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
42#pragma once
43
44#include "value.h"
45#include "context.h"
46
47namespace ledger {
48
49class xact_t;
50class journal_t;
51class account_t;
52
53DECLARE_EXCEPTION(csv_error, std::runtime_error);
54
56{
57 parse_context_t context;
58
59 enum headers_t {
60 FIELD_DATE = 0,
61 FIELD_DATE_AUX,
62 FIELD_CODE,
63 FIELD_PAYEE,
64 FIELD_CREDIT,
65 FIELD_DEBIT,
66 FIELD_COST,
67 FIELD_TOTAL,
68 FIELD_NOTE,
69
70 FIELD_UNKNOWN
71 };
72
73 std::array<std::pair<mask_t, headers_t>, 10> masks;
74
75 std::vector<headers_t> index;
76 std::vector<string> names;
77
78public:
80 : context(_context),
81 masks{ std::make_pair(mask_t("date"), FIELD_DATE),
82 std::make_pair(mask_t("posted( ?date)?"), FIELD_DATE_AUX),
83 std::make_pair(mask_t("code"), FIELD_CODE),
84 std::make_pair(mask_t("(payee|desc(ription)?|title)"), FIELD_PAYEE),
85 std::make_pair(mask_t("credit|amount"), FIELD_CREDIT),
86 std::make_pair(mask_t("debit"), FIELD_DEBIT),
87 std::make_pair(mask_t("cost"), FIELD_COST),
88 std::make_pair(mask_t("total"), FIELD_TOTAL),
89 std::make_pair(mask_t("note"), FIELD_NOTE),
90 std::make_pair(mask_t(""), FIELD_UNKNOWN) } {
91 read_index(*context.stream.get());
92 TRACE_CTOR(csv_reader, "parse_context_t&");
93 }
97
98 void read_index(std::istream& in);
99 string read_field(std::istream& in);
100 char * next_line(std::istream& in);
101
103
104 const char * get_last_line() const {
105 return context.linebuf;
106 }
108 return context.pathname;
109 }
110 std::size_t get_linenum() const {
111 return context.linenum;
112 }
113};
114
115} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
Abstract dynamic type representing various numeric types.
#define DECLARE_EXCEPTION(name, kind)
Definition error.h:88
boost::filesystem::path path
Definition utils.h:68
T & downcast(U &object)
Definition utils.h:468
shared_ptr< std::istream > stream
Definition context.h:62
std::size_t linenum
Definition context.h:72
char linebuf[MAX_LINE+1]
Definition context.h:69
std::size_t get_linenum() const
Definition csv.h:110
csv_reader(parse_context_t &_context)
Definition csv.h:79
const char * get_last_line() const
Definition csv.h:104
string read_field(std::istream &in)
path get_pathname() const
Definition csv.h:107
xact_t * read_xact(bool rich_data)
char * next_line(std::istream &in)
void read_index(std::istream &in)