Loading...
Searching...
No Matches
context.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 "utils.h"
45#include "times.h"
46
47#if HAVE_GPGME
48#include "gpgme.h"
49#endif
50
51namespace ledger {
52
53class journal_t;
54class account_t;
55class scope_t;
56
58{
59public:
60 static const std::size_t MAX_LINE = 4096;
61
63
69 char linebuf[MAX_LINE + 1];
70 std::istream::pos_type line_beg_pos;
71 std::istream::pos_type curr_pos;
72 std::size_t linenum;
73 std::size_t errors;
74 std::size_t count;
75 std::size_t sequence;
76 std::string last;
77
78 explicit parse_context_t(const path& cwd)
80 linenum(0), errors(0), count(0), sequence(1) {}
81
86
88 : stream(context.stream),
89 pathname(context.pathname),
91 journal(context.journal),
92 master(context.master),
93 scope(context.scope),
95 curr_pos(context.curr_pos),
96 linenum(context.linenum),
97 errors(context.errors),
98 count(context.count),
99 sequence(context.sequence) {
100 std::memcpy(linebuf, context.linebuf, MAX_LINE);
101 }
102
103 string location() const {
105 }
106
107 void warning(const string& what) const {
108 warning_func(location() + " " + what);
109 }
110 void warning(const boost::format& what) const {
111 warning_func(location() + " " + string(what.str()));
112 }
113};
114
115inline parse_context_t open_for_reading(const path& pathname,
116 const path& cwd)
117{
118 path filename = resolve_path(pathname);
119 filename = filesystem::absolute(filename, cwd);
120 if (! exists(filename) || is_directory(filename))
121 throw_(std::runtime_error,
122 _f("Cannot read journal file %1%") % filename);
123
124 path parent(filename.parent_path());
125#if HAVE_GPGME
127#else
128 shared_ptr<std::istream> stream(new ifstream(filename));
129#endif
130 parse_context_t context(stream, parent);
131 context.pathname = filename;
132 return context;
133}
134
136{
137 std::list<parse_context_t> parsing_context;
138
139public:
140 void push() {
141 parsing_context.push_front(parse_context_t(filesystem::current_path()));
142 }
144 const path& cwd = filesystem::current_path()) {
145 parsing_context.push_front(parse_context_t(stream, cwd));
146 }
147 void push(const path& pathname,
148 const path& cwd = filesystem::current_path()) {
149 parsing_context.push_front(open_for_reading(pathname, cwd));
150 }
151
152 void push(const parse_context_t& context) {
153 parsing_context.push_front(context);
154 }
155
156 void pop() {
157 assert(! parsing_context.empty());
158 parsing_context.pop_front();
159 }
160
162 assert(! parsing_context.empty());
163 return parsing_context.front();
164 }
165};
166
167} // namespace ledger
General utility facilities used by Ledger.
#define assert(x)
Definition utils.h:92
A utility class for reading encrypted journal data.
datetime_t and date_t objects
#define throw_(cls, msg)
Definition error.h:55
boost::filesystem::ifstream ifstream
Definition utils.h:69
void warning_func(const string &message)
Definition error.h:59
boost::filesystem::path path
Definition utils.h:68
path resolve_path(const path &pathname)
T & downcast(U &object)
Definition utils.h:468
parse_context_t open_for_reading(const path &pathname, const path &cwd)
Definition context.h:115
string file_context(const path &file, std::size_t line)
parse_context_t(const parse_context_t &context)
Definition context.h:87
static const std::size_t MAX_LINE
Definition context.h:60
shared_ptr< std::istream > stream
Definition context.h:62
string location() const
Definition context.h:103
std::size_t errors
Definition context.h:73
std::istream::pos_type line_beg_pos
Definition context.h:70
journal_t * journal
Definition context.h:66
std::size_t linenum
Definition context.h:72
std::size_t sequence
Definition context.h:75
std::istream::pos_type curr_pos
Definition context.h:71
parse_context_t(const path &cwd)
Definition context.h:78
char linebuf[MAX_LINE+1]
Definition context.h:69
void warning(const boost::format &what) const
Definition context.h:110
account_t * master
Definition context.h:67
void warning(const string &what) const
Definition context.h:107
std::size_t count
Definition context.h:74
parse_context_t(shared_ptr< std::istream > _stream, const path &cwd)
Definition context.h:82
void push(const path &pathname, const path &cwd=filesystem::current_path())
Definition context.h:147
void push(shared_ptr< std::istream > stream, const path &cwd=filesystem::current_path())
Definition context.h:143
void push(const parse_context_t &context)
Definition context.h:152
parse_context_t & get_current()
Definition context.h:161
static std::istream * open_stream(const path &filename)