Loading...
Searching...
No Matches
format.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 "expr.h"
45#include "unistring.h"
46
47#include <boost/smart_ptr/scoped_ptr.hpp>
48
49namespace ledger {
50
51class unistring;
52
53DECLARE_EXCEPTION(format_error, std::runtime_error);
54
55class format_t : public expr_base_t<string>, public noncopyable
56{
58
59 struct element_t : public flags::supports_flags<>, public noncopyable
60 {
61#define ELEMENT_ALIGN_LEFT 0x01
62
63 enum kind_t { STRING, EXPR };
64
65 kind_t type;
66 std::size_t min_width;
67 std::size_t max_width;
70
71 element_t() throw()
72 : supports_flags<>(), type(STRING), min_width(0), max_width(0) {
73 TRACE_CTOR(element_t, "");
74 }
75 ~element_t() throw() {
76 TRACE_DTOR(element_t);
77 }
78
79 element_t& operator=(const element_t& elem) {
80 if (this != &elem) {
81 supports_flags<>::operator=(elem);
82 type = elem.type;
83 min_width = elem.min_width;
84 max_width = elem.max_width;
85 data = elem.data;
86 }
87 return *this;
88 }
89
90 friend inline void mark_red(std::ostream& out, const element_t * elem) {
91 out.setf(std::ios::left);
92 out.width(0);
93 out << "\033[31m";
94
95 if (elem->has_flags(ELEMENT_ALIGN_LEFT))
96 out << std::left;
97 else
98 out << std::right;
99
100 if (elem->min_width > 0)
101 out.width(static_cast<std::streamsize>(elem->min_width));
102 }
103
104 void dump(std::ostream& out) const;
105 };
106
107 scoped_ptr<element_t> elements;
108
109public:
116
118
119private:
120 static element_t * parse_elements(const string& fmt,
121 const optional<format_t&>& tmpl);
122
123public:
125 TRACE_CTOR(format_t, "");
126 }
127 format_t(const string& _str, scope_t * context = NULL)
128 : base_type(context) {
129 if (! _str.empty())
131 TRACE_CTOR(format_t, "const string&");
132 }
133 virtual ~format_t() {
135 }
136
137 void parse_format(const string& _format,
138 const optional<format_t&>& tmpl = none) {
139 elements.reset(parse_elements(_format, tmpl));
141 }
142
143 virtual void mark_uncompiled() {
144 for (element_t * elem = elements.get(); elem; elem = elem->next.get()) {
145 if (elem->type == element_t::EXPR) {
146 expr_t& expr(boost::get<expr_t>(elem->data));
147 expr.mark_uncompiled();
148 }
149 }
150 }
151
153
154 virtual void dump(std::ostream& out) const {
155 for (const element_t * elem = elements.get();
156 elem;
157 elem = elem->next.get())
158 elem->dump(out);
159 }
160
161 static string truncate(const unistring& str,
162 const std::size_t width,
163 const std::size_t account_abbrev_length = 0);
164};
165
166} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define DECLARE_EXCEPTION(name, kind)
Definition error.h:88
#define ELEMENT_ALIGN_LEFT
Definition format.h:61
T & downcast(U &object)
Definition utils.h:468
virtual void mark_uncompiled()
Definition exprbase.h:134
void set_text(const string &txt)
Definition exprbase.h:118
static enum ledger::format_t::elision_style_t default_style
virtual void dump(std::ostream &out) const
Definition format.h:154
format_t(const string &_str, scope_t *context=NULL)
Definition format.h:127
virtual void mark_uncompiled()
Definition format.h:143
static string truncate(const unistring &str, const std::size_t width, const std::size_t account_abbrev_length=0)
virtual ~format_t()
Definition format.h:133
void parse_format(const string &_format, const optional< format_t & > &tmpl=none)
Definition format.h:137
virtual result_type real_calc(scope_t &scope)
static bool default_style_changed
Definition format.h:117
Abstract working with UTF-32 encoded Unicode strings.
Definition unistring.h:57