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
72 void MathedArray::deep_copy()
77 MathedInset * inset = it.GetInset();
78 inset = inset->Clone();
79 raw_pointer_insert(inset, it.getPos() + 1);
86 void MathedArray::substitute(MathMacro * m)
94 MathedInset * inset = it.GetInset();
95 if (inset->GetType() == LM_OT_MACRO_ARG) {
96 int n = static_cast<MathMacroArgument *>(inset)->number() - 1;
97 //lyxerr << "substituting an argument inset: " << n << "\n";
98 inset = m->arg(n)->Clone();
100 inset->substitute(m);
103 MathParInset * pinset = static_cast<MathParInset *>(inset);
104 int n = pinset->getMaxArgumentIdx();
105 int idx = pinset->getArgumentIdx();
106 for (int i = 0; i <= n; ++i) {
107 pinset->setArgumentIdx(i);
108 pinset->GetData().substitute(m);
110 pinset->setArgumentIdx(idx);
114 //lyxerr << "substituting in an ordinary inset\n";
116 raw_pointer_insert(inset, it.getPos() + 1);
123 MathedArray & MathedArray::operator=(MathedArray const & array)
125 MathedArray tmp(array);
131 void MathedArray::push_back(MathedInset * inset, int t)
136 it.insertInset(inset, t);
140 void MathedArray::push_back(byte b, MathedTextCodes c)
149 void MathedArray::clear()
157 void MathedArray::swap(MathedArray & array)
159 if (this != &array) {
161 std::swap(last_, array.last_);
166 MathedArray::iterator MathedArray::begin()
172 MathedArray::iterator MathedArray::end()
178 MathedArray::const_iterator MathedArray::begin() const
184 MathedArray::const_iterator MathedArray::end() const
190 int MathedArray::empty() const
196 int MathedArray::last() const
202 void MathedArray::last(int l)
208 void MathedArray::need_size(int needed)
210 if (needed >= static_cast<int>(bf_.size()))
215 void MathedArray::resize(int newsize)
217 // still a bit smelly...
219 bf_.resize(newsize + 1);
220 if (last_ >= newsize)
226 void MathedArray::move(int p, int shift)
229 need_size(last_ + shift);
230 std::memmove(&bf_[p + shift], &bf_[p], last_ - p);
238 void MathedArray::shrink(int pos1, int pos2)
240 if (pos1 == 0 && pos2 >= last())
244 if (pos1 > 0 && bf_[pos1] > ' ') {
245 for (int p = pos1; p >= 0; --p) {
246 if (MathIsFont(bf_[p])) {
256 if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
259 int dx = pos2 - pos1;
262 strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
276 void MathedArray::insert(MathedArray::iterator pos,
277 MathedArray::const_iterator beg,
278 MathedArray::const_iterator end)
280 bf_.insert(pos, beg, end);
281 last_ = bf_.size() - 1;
284 void MathedArray::merge(MathedArray const & a, int p)
286 my_memcpy(&bf_[p], &a.bf_[0], a.last());
291 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
293 my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
298 void MathedArray::insertInset(int pos, MathedInset * p, int type)
300 //bf_.insert(pos, type);
301 InsetTable tmp(pos, p);
302 insetList_.push_back(tmp);
306 MathedInset * MathedArray::getInset(int pos)
308 InsetList::const_iterator cit = insetList_.begin();
309 InsetList::const_iterator end = insetList_.end();
310 for (; cit != end; ++cit) {
311 if ((*cit).pos == pos)
316 // We would really like to throw an exception instead... (Lgb)
317 // throw inset_not_found();
321 void MathedArray::raw_pointer_insert(void * p, int pos)
323 my_memcpy(&bf_[pos], &p, sizeof(p));
328 void MathedArray::strange_copy(MathedArray * dest, int dpos,
331 my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
335 byte MathedArray::operator[](int i) const
341 byte & MathedArray::operator[](int i)
347 void MathedArray::dump2(ostream & os) const
349 buffer_type::const_iterator cit = bf_.begin();
350 buffer_type::const_iterator end = bf_.end();
351 for (; cit != end; ++cit) {
358 void MathedArray::dump(ostream & os) const
360 MathedIter it( const_cast<MathedArray*>(this) );
363 MathedInset * inset = it.GetInset();
364 os << "<inset: " << inset << ">";
370 else if (it.IsScript())
372 else if (it.IsFont())
373 os << "<font: " << int(it.at()) << ">";
374 else if (it.at() >= 32 && it.at() < 127)
377 os << "<unknown: " << int(it.at()) << ">";