10 #include "math_iter.h"
11 #include "math_inset.h"
12 #include "math_macro.h"
14 #include "support/LOstream.h"
22 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
24 char * ps = static_cast<char *>(ps_in);
25 char const * pt = static_cast<char const *>(pt_in);
26 while (n--) *ps++ = *pt++;
33 MathedArray::MathedArray()
34 : bf_(1, '\0'), last_(0)
38 MathedArray::~MathedArray()
41 // let's leak for a while...
47 MathedInset * inset = it.GetInset();
56 MathedArray::MathedArray(MathedArray const & array)
58 // this "implementation" is obviously wrong: MathedIter should be
59 // implemented by MathedArray (not the other way round) but I think
60 // getting the _interface_ of MathedArray right is more important right
71 void MathedArray::deep_copy()
76 MathedInset * inset = it.GetInset();
77 inset = inset->Clone();
78 raw_pointer_insert(inset, it.getPos() + 1);
84 void MathedArray::substitute(MathMacro * m)
92 MathedInset * inset = it.GetInset();
93 if (inset->GetType() == LM_OT_MACRO_ARG) {
94 int n = static_cast<MathMacroArgument *>(inset)->number() - 1;
95 //lyxerr << "substituting an argument inset: " << n << "\n";
96 inset = m->arg(n)->Clone();
101 MathParInset * pinset = static_cast<MathParInset *>(inset);
102 int n = pinset->getMaxArgumentIdx();
103 int idx = pinset->getArgumentIdx();
104 for (int i = 0; i <= n; ++i) {
105 pinset->setArgumentIdx(i);
106 pinset->GetData().substitute(m);
108 pinset->setArgumentIdx(idx);
112 //lyxerr << "substituting in an ordinary inset\n";
114 raw_pointer_insert(inset, it.getPos() + 1);
121 MathedArray & MathedArray::operator=(MathedArray const & array)
123 MathedArray tmp(array);
128 void MathedArray::push_back(MathedInset * inset, int t)
133 it.insertInset(inset, t);
136 void MathedArray::push_back(byte b, MathedTextCodes c)
144 void MathedArray::clear()
151 void MathedArray::swap(MathedArray & array)
153 if (this != &array) {
155 std::swap(last_, array.last_);
160 MathedArray::iterator MathedArray::begin()
166 MathedArray::iterator MathedArray::end()
172 MathedArray::const_iterator MathedArray::begin() const
178 MathedArray::const_iterator MathedArray::end() const
184 int MathedArray::empty() const
190 int MathedArray::last() const
196 void MathedArray::last(int l)
202 void MathedArray::need_size(int needed)
204 if (needed >= static_cast<int>(bf_.size()))
209 void MathedArray::resize(int newsize)
211 // still a bit smelly...
213 bf_.resize(newsize + 1);
214 if (last_ >= newsize)
220 void MathedArray::move(int p, int shift)
223 need_size(last_ + shift);
224 memmove(&bf_[p + shift], &bf_[p], last_ - p);
232 void MathedArray::shrink(int pos1, int pos2)
234 if (pos1 == 0 && pos2 >= last())
238 if (pos1 > 0 && bf_[pos1] > ' ') {
239 for (int p = pos1; p >= 0; --p) {
240 if (MathIsFont(bf_[p])) {
250 if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
253 int dx = pos2 - pos1;
256 strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
270 void MathedArray::insert(MathedArray::iterator pos,
271 MathedArray::const_iterator beg,
272 MathedArray::const_iterator end)
274 bf_.insert(pos, beg, end);
275 last_ = bf_.size() - 1;
278 void MathedArray::merge(MathedArray const & a, int p)
280 my_memcpy(&bf_[p], &a.bf_[0], a.last());
285 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
287 my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
292 void MathedArray::insertInset(int pos, MathedInset * p, int type)
294 //bf_.insert(pos, type);
295 InsetTable tmp(pos, p);
296 insetList_.push_back(tmp);
300 MathedInset * MathedArray::getInset(int pos)
302 InsetList::const_iterator cit = insetList_.begin();
303 InsetList::const_iterator end = insetList_.end();
304 for (; cit != end; ++cit) {
305 if ((*cit).pos == pos)
310 // We would really like to throw an exception instead... (Lgb)
311 // throw inset_not_found();
315 void MathedArray::raw_pointer_insert(void * p, int pos)
317 my_memcpy(&bf_[pos], &p, sizeof(p));
322 void MathedArray::strange_copy(MathedArray * dest, int dpos,
325 my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
329 byte MathedArray::operator[](int i) const
335 byte & MathedArray::operator[](int i)
341 void MathedArray::dump2(ostream & os) const
343 buffer_type::const_iterator cit = bf_.begin();
344 buffer_type::const_iterator end = bf_.end();
345 for (; cit != end; ++cit) {
351 void MathedArray::dump(ostream & os) const
353 MathedIter it( const_cast<MathedArray*>(this) );
356 MathedInset * inset = it.GetInset();
357 os << "<inset: " << inset << ">";
363 else if (it.IsScript())
365 else if (it.IsFont())
366 os << "<font: " << int(it.at()) << ">";
367 else if (it.at() >= 32 && it.at() < 127)
370 os << "<unknown: " << int(it.at()) << ">";