]> git.lyx.org Git - features.git/blob - src/mathed/array.C
mathed67.diff
[features.git] / src / mathed / array.C
1
2 #include <config.h>
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "debug.h"
9 #include "array.h"
10 #include "math_iter.h"
11 #include "math_inset.h"
12 #include "math_macro.h"
13
14 #include "support/LOstream.h"
15
16 using std::ostream;
17 using std::endl;
18
19 namespace {
20
21 inline
22 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
23 {
24         char * ps = static_cast<char *>(ps_in);
25         char const * pt = static_cast<char const *>(pt_in);
26         while (n--) *ps++ = *pt++;
27         return ps_in;
28 }
29
30 } // namespace anon
31
32
33 MathedArray::MathedArray()
34         : bf_(1, '\0'), last_(0)
35 {}
36
37
38 MathedArray::~MathedArray()
39 {
40         // deep destruction
41         // let's leak for a while... 
42 /*
43         MathedIter it;
44         it.SetData(this);
45         while (it.OK()) {
46                 if (it.IsInset()) {
47                         MathedInset * inset = it.GetInset();
48                         delete inset;
49                 }
50                 it.Next();
51         }
52 */
53 }
54
55
56 MathedArray::MathedArray(MathedArray const & array)
57 {
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
61         // now (Andre')
62
63         // shallow copy
64         bf_   = array.bf_;
65         last_ = array.last_;
66
67         // deep copy
68         deep_copy();
69 }
70
71 void MathedArray::deep_copy()
72 {
73         MathedIter it(this);
74         while (it.OK()) {
75                 if (it.IsInset()) {
76                         MathedInset * inset = it.GetInset();
77                         inset = inset->Clone();
78                         raw_pointer_insert(inset, it.getPos() + 1);
79                 }
80                 it.Next();
81         }
82 }
83
84 void MathedArray::substitute(MathMacro * m)
85 {
86         if (m->nargs() == 0)
87                 return;
88
89         MathedIter it(this);
90         while (it.OK()) {
91                 if (it.IsInset()) {
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();
97                         } else {
98                                 inset->substitute(m);
99 /*                              
100                                 if (it.IsActive()) {
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);
107                                         }
108                                         pinset->setArgumentIdx(idx);
109                                 }
110 */
111
112                                 //lyxerr << "substituting in an ordinary inset\n";
113                         }
114                         raw_pointer_insert(inset, it.getPos() + 1);
115                 }
116                 it.Next();
117         }
118 }
119
120
121 MathedArray & MathedArray::operator=(MathedArray const & array)
122 {
123         MathedArray tmp(array);
124         swap(tmp);
125         return *this;
126 }
127
128 void MathedArray::push_back(MathedInset * inset, int t)
129 {
130         MathedIter it(this);
131         while (it.Next())
132                 ;
133         it.insertInset(inset, t);
134 }
135
136 void MathedArray::push_back(byte b, MathedTextCodes c)
137 {
138         MathedIter it(this);
139         while (it.Next())
140                 ;
141         it.insert(b, c);
142 }
143
144 void MathedArray::clear()
145 {
146         last_ = 0;
147         bf_.resize(1);
148         bf_[0] = 0;
149 }
150
151 void MathedArray::swap(MathedArray & array)
152 {
153         if (this != &array) {
154                 bf_.swap(array.bf_);
155                 std::swap(last_, array.last_);
156         }
157 }
158
159
160 MathedArray::iterator MathedArray::begin() 
161 {
162         return bf_.begin();
163 }
164
165
166 MathedArray::iterator MathedArray::end() 
167 {
168         return bf_.end();
169 }
170
171
172 MathedArray::const_iterator MathedArray::begin() const
173 {
174         return bf_.begin();
175 }
176
177
178 MathedArray::const_iterator MathedArray::end() const
179 {
180         return bf_.end();
181 }
182
183
184 int MathedArray::empty() const
185 {
186         return (last_ == 0);
187 }
188    
189
190 int MathedArray::last() const
191 {
192         return last_;
193 }
194
195
196 void MathedArray::last(int l)
197 {
198         last_ = l;
199 }
200
201
202 void MathedArray::need_size(int needed)
203 {
204         if (needed >= static_cast<int>(bf_.size()))
205                 resize(needed);
206 }
207
208
209 void MathedArray::resize(int newsize)
210 {
211         // still a bit smelly...
212         ++newsize;
213         bf_.resize(newsize + 1);
214         if (last_ >= newsize)
215                 last_ = newsize - 1;
216         bf_[last_] = 0;
217 }
218
219
220 void MathedArray::move(int p, int shift)
221 {
222         if (p <= last_) {
223                 need_size(last_ + shift);
224                 memmove(&bf_[p + shift], &bf_[p], last_ - p);
225                 last_ += shift;
226                 bf_[last_] = 0;
227         }
228 }
229
230
231
232 void MathedArray::shrink(int pos1, int pos2)
233 {
234         if (pos1 == 0 && pos2 >= last())        
235                 return;
236
237         short fc = 0;
238         if (pos1 > 0 && bf_[pos1] > ' ') {
239                 for (int p = pos1; p >= 0; --p) {
240                         if (MathIsFont(bf_[p])) {
241                                 if (p != pos1 - 1)
242                                         fc = bf_[p];
243                                 else
244                                         --pos1;
245                                 break;
246                         }
247                 }
248         }
249
250         if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
251                 --pos2;
252
253         int dx = pos2 - pos1;
254         MathedArray a;
255         a.resize(dx + 1);
256         strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
257         if (fc) {
258                 a[0] = fc;
259                 ++dx;
260         }
261         a.last(dx);
262         a[dx] = '\0';
263
264         swap(a);
265         deep_copy();
266 }
267
268
269 #if 0
270 void MathedArray::insert(MathedArray::iterator pos,
271                          MathedArray::const_iterator beg,
272                          MathedArray::const_iterator end)
273 {
274         bf_.insert(pos, beg, end);
275         last_ = bf_.size() - 1;
276 }
277 #else
278 void MathedArray::merge(MathedArray const & a, int p)
279 {
280         my_memcpy(&bf_[p], &a.bf_[0], a.last());
281 }
282 #endif
283
284
285 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
286 {
287         my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
288 }
289
290
291 #if 0
292 void MathedArray::insertInset(int pos, MathedInset * p, int type)
293 {
294         //bf_.insert(pos, type);
295         InsetTable tmp(pos, p);
296         insetList_.push_back(tmp);
297 }
298
299
300 MathedInset * MathedArray::getInset(int pos) 
301 {
302         InsetList::const_iterator cit = insetList_.begin();
303         InsetList::const_iterator end = insetList_.end();
304         for (; cit != end; ++cit) {
305                 if ((*cit).pos == pos)
306                         return (*cit).inset;
307         }
308         // not found
309         return 0;
310         // We would really like to throw an exception instead... (Lgb)
311         // throw inset_not_found();
312 }
313
314 #else
315 void MathedArray::raw_pointer_insert(void * p, int pos)
316 {
317         my_memcpy(&bf_[pos], &p, sizeof(p));
318 }
319 #endif
320
321
322 void MathedArray::strange_copy(MathedArray * dest, int dpos,
323                                 int spos, int len)
324 {
325         my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
326 }
327
328
329 byte MathedArray::operator[](int i) const
330 {
331         return bf_[i];
332 }
333
334
335 byte & MathedArray::operator[](int i)
336 {
337         return bf_[i];
338 }
339
340
341 void MathedArray::dump2(ostream & os) const
342 {
343         buffer_type::const_iterator cit = bf_.begin();
344         buffer_type::const_iterator end = bf_.end();
345         for (; cit != end; ++cit) {
346                 os << (*cit);
347         }
348         os << endl;
349 }
350
351 void MathedArray::dump(ostream & os) const
352 {
353         MathedIter it( const_cast<MathedArray*>(this) );
354         while (it.OK()) {
355                 if (it.IsInset()) {
356                         MathedInset * inset = it.GetInset();
357                         os << "<inset: " << inset << ">";
358                 } 
359                 else if (it.IsTab())
360                         os << "<tab>";
361                 else if (it.IsCR())
362                         os << "<cr>";
363                 else if (it.IsScript())
364                         os << "<script>";
365                 else if (it.IsFont())
366                         os << "<font: " << int(it.at()) << ">";
367                 else if (it.at() >= 32 && it.at() < 127)
368                         os << it.at();
369                 else
370                         os << "<unknown: " << int(it.at()) << ">";
371                 it.Next();
372         }
373 }
374
375