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();
99 //lyxerr << "substituting in an ordinary inset\n";
101 raw_pointer_insert(inset, it.getPos() + 1);
108 MathedArray & MathedArray::operator=(MathedArray const & array)
110 MathedArray tmp(array);
115 void MathedArray::clear()
122 void MathedArray::swap(MathedArray & array)
124 if (this != &array) {
126 std::swap(last_, array.last_);
131 MathedArray::iterator MathedArray::begin()
137 MathedArray::iterator MathedArray::end()
143 MathedArray::const_iterator MathedArray::begin() const
149 MathedArray::const_iterator MathedArray::end() const
155 int MathedArray::empty() const
161 int MathedArray::last() const
167 void MathedArray::last(int l)
173 void MathedArray::need_size(int needed)
175 if (needed >= static_cast<int>(bf_.size()))
180 void MathedArray::resize(int newsize)
182 // still a bit smelly...
184 bf_.resize(newsize + 1);
185 if (last_ >= newsize)
191 void MathedArray::move(int p, int shift)
194 need_size(last_ + shift);
195 memmove(&bf_[p + shift], &bf_[p], last_ - p);
203 void MathedArray::shrink(int pos1, int pos2)
205 if (pos1 == 0 && pos2 >= last())
209 if (pos1 > 0 && bf_[pos1] > ' ') {
210 for (int p = pos1; p >= 0; --p) {
211 if (MathIsFont(bf_[p])) {
221 if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
224 int dx = pos2 - pos1;
227 strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
241 void MathedArray::insert(MathedArray::iterator pos,
242 MathedArray::const_iterator beg,
243 MathedArray::const_iterator end)
245 bf_.insert(pos, beg, end);
246 last_ = bf_.size() - 1;
249 void MathedArray::merge(MathedArray const & a, int p)
251 my_memcpy(&bf_[p], &a.bf_[0], a.last());
256 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
258 my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
263 void MathedArray::insertInset(int pos, MathedInset * p, int type)
265 //bf_.insert(pos, type);
266 InsetTable tmp(pos, p);
267 insetList_.push_back(tmp);
271 MathedInset * MathedArray::getInset(int pos)
273 InsetList::const_iterator cit = insetList_.begin();
274 InsetList::const_iterator end = insetList_.end();
275 for (; cit != end; ++cit) {
276 if ((*cit).pos == pos)
281 // We would really like to throw an exception instead... (Lgb)
282 // throw inset_not_found();
286 void MathedArray::raw_pointer_insert(void * p, int pos)
288 my_memcpy(&bf_[pos], &p, sizeof(p));
293 void MathedArray::strange_copy(MathedArray * dest, int dpos,
296 my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
300 byte MathedArray::operator[](int i) const
306 byte & MathedArray::operator[](int i)
312 void MathedArray::dump2(ostream & os) const
314 buffer_type::const_iterator cit = bf_.begin();
315 buffer_type::const_iterator end = bf_.end();
316 for (; cit != end; ++cit) {
322 void MathedArray::dump(ostream & os) const
324 MathedIter it( const_cast<MathedArray*>(this) );
327 MathedInset * inset = it.GetInset();
328 os << "<inset: " << inset << ">";
334 else if (it.IsScript())
336 else if (it.IsFont())
337 os << "<font: " << int(it.at()) << ">";
338 else if (it.at() >= 32 && it.at() < 127)
341 os << "<unknown: " << int(it.at()) << ">";