Macaulay2 Engine
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1// Copyright 1997 by Michael E. Stillman
2
3#ifndef _buffer_hpp_
4#define _buffer_hpp_
5
36
37#include "newdelete.hpp"
38#include "engine-includes.hpp"
39#include <string>
40
42
43// forward declarations (from ringelem.hpp)
44struct cc_struct;
46struct cci_struct;
47
48struct indent
49{
50 int n;
51 indent(int n0) : n(n0) {}
52};
53
54class buffer : public our_new_delete
55{
56 int _size;
58 char *_buf;
59 void expand(int newcap);
60
61 public:
69 void reset() { _size = 0; }
70 int size() { return _size; }
71 int capacity() { return _capacity; }
72 char *str()
73 {
74 _buf[_size] = '\0';
75 return _buf;
76 }
77 char *truncate(int newsize)
78 {
79 if (newsize < _size) _size = newsize;
80 _buf[_size] = '\0';
81 return _buf;
82 }
83
84 M2_string to_string(); // Copies the string, leaves buffer intact
85
86 void put(const char *s); // Place null-terminated string into buffer
87 void put(const char *s,
88 long len); // Place a string possible containing null chars
89 void put(char c); // Place a single character
90 void put(int n); // Format the integer, place into buffer
91 void put(int n, int width); // Format the integer, with given width field.
92 void put(long n); // Format the integer, place into buffer
93 void put(double n); // Format the double, place into buffer
94 void put(long n, int width); // Format the integer, with given width field.
95 void put(unsigned int n); // Format the integer, place into buffer
96 void put(unsigned int n,
97 int width); // Format the integer, with given width field.
98 void put(unsigned long n); // Format the integer, place into buffer
99 void put(unsigned long long n); // Format the integer, place into buffer
100 void put(unsigned long n,
101 int width); // Format the integer, with given width field.
102 void put(mpfr_srcptr x);
103 void put(mpfi_srcptr x);
104 void put(cc_struct const *x);
105 void put(cc_doubles_struct const *x);
106 void put(cci_struct const *x);
107 void put(std::string s) { put(s.data(), s.size()); }
108 // To put an endline in:
109 // o.put(newline);
110
111 // To print the resulting string
112 // buffer o;
113 // o.put("hi there");
114 // o.put(5);
115 // o.put(newline);
116 // cerr << o.str();
117
118 buffer &operator<<(const char *s)
119 {
120 put(s);
121 return *this;
122 }
123 buffer &operator<<(M2_string s)
124 {
125 put((char *)s->array, s->len);
126 return *this;
127 }
128 buffer &operator<<(std::string s)
129 {
130 put(s);
131 return *this;
132 }
134 {
135 put(n);
136 return *this;
137 }
139 {
140 put(n);
141 return *this;
142 }
143 buffer &operator<<(unsigned int n)
144 {
145 put(n);
146 return *this;
147 }
148 buffer &operator<<(unsigned long n)
149 {
150 put(n);
151 return *this;
152 }
153 buffer &operator<<(unsigned long long n)
154 {
155 put(n);
156 return *this;
157 }
158 buffer &operator<<(unsigned short n)
159 {
160 put(static_cast<unsigned int>(n));
161 return *this;
162 }
164 {
165 put(n);
166 return *this;
167 }
169 {
170 put(c);
171 return *this;
172 }
173 buffer &operator<<(unsigned char c)
174 {
175 put(static_cast<char>(c));
176 return *this;
177 }
178 buffer &operator<<(mpfr_srcptr x)
179 {
180 put(x);
181 return *this;
182 }
183 buffer &operator<<(mpfi_srcptr x)
184 {
185 put(x);
186 return *this;
187 }
189 {
190 put(x);
191 return *this;
192 }
194 {
195 put(x);
196 return *this;
197 }
199 {
200 put(x);
201 return *this;
202 }
204 {
205 buffer &o = *this;
206 const int &n = s.n;
207 if (n < 10)
208 o << " ";
209 else if (n < 100)
210 o << " ";
211 o << n;
212 return o;
213 }
214};
215
216#endif
217
218// Local Variables:
219// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
220// indent-tabs-mode: nil
221// End:
const int BUFFER_INITIAL_CAPACITY
Definition buffer.hpp:41
buffer & operator<<(long n)
Definition buffer.hpp:133
int size()
Definition buffer.hpp:70
buffer & operator<<(unsigned long n)
Definition buffer.hpp:148
~buffer()
Definition buffer.hpp:68
buffer & operator<<(cci_struct const *x)
Definition buffer.hpp:198
char * str()
Definition buffer.hpp:72
buffer & operator<<(mpfi_srcptr x)
Definition buffer.hpp:183
int _capacity
Definition buffer.hpp:57
int _size
Definition buffer.hpp:56
buffer & operator<<(int n)
Definition buffer.hpp:163
buffer()
Definition buffer.hpp:62
buffer & operator<<(std::string s)
Definition buffer.hpp:128
void put(cc_struct const *x)
buffer & operator<<(char c)
Definition buffer.hpp:168
void reset()
Definition buffer.hpp:69
buffer & operator<<(unsigned int n)
Definition buffer.hpp:143
int capacity()
Definition buffer.hpp:71
buffer & operator<<(cc_struct const *x)
Definition buffer.hpp:188
void put(cc_doubles_struct const *x)
char * truncate(int newsize)
Definition buffer.hpp:77
void put(const char *s)
Definition buffer.cpp:35
buffer & operator<<(double n)
Definition buffer.hpp:138
void expand(int newcap)
Definition buffer.cpp:9
buffer & operator<<(unsigned char c)
Definition buffer.hpp:173
buffer & operator<<(unsigned long long n)
Definition buffer.hpp:153
buffer & operator<<(cc_doubles_struct const *x)
Definition buffer.hpp:193
buffer & operator<<(unsigned short n)
Definition buffer.hpp:158
void put(cci_struct const *x)
M2_string to_string()
Definition buffer.cpp:20
buffer & operator<<(const char *s)
Definition buffer.hpp:118
buffer & operator<<(M2_string s)
Definition buffer.hpp:123
void put(std::string s)
Definition buffer.hpp:107
char * _buf
Definition buffer.hpp:58
buffer & operator<<(mpfr_srcptr x)
Definition buffer.hpp:178
buffer & operator<<(indent s)
Definition buffer.hpp:203
Engine-wide include prelude — a single point of truth for portability shims.
void freemem(void *s)
Definition m2-mem.cpp:103
void size_t s
Definition m2-mem.cpp:271
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
our_new_delete — per-class opt-in routing of new / delete through bdwgc.
volatile int x
indent(int n0)
Definition buffer.hpp:51
int n
Definition buffer.hpp:50