]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
mathed35.diff
[lyx.git] / src / mathed / array.C
1
2 #include <config.h>
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "array.h"
9 #include "math_iter.h"
10 #include "math_inset.h"
11
12 // Is this still needed? (Lgb)
13 static inline
14 void * my_memcpy(void * ps_in, void const * pt_in, size_t n)
15 {
16         char * ps = static_cast<char *>(ps_in);
17         char const * pt = static_cast<char const *>(pt_in);
18         while (n--) *ps++ = *pt++;
19         return ps_in;
20 }
21
22
23 MathedArray::MathedArray()
24         : bf_(1, '\0'), last_(0)
25 {}
26
27
28 MathedArray::~MathedArray()
29 {
30         // deep destruction
31         // let's leak for a while... 
32 /*
33         MathedIter it;
34         it.SetData(this);
35         while (it.OK()) {
36                 if (it.IsInset()) {
37                         MathedInset * inset = it.GetInset();
38                         delete inset;
39                 }
40                 it.Next();
41         }
42 */
43 }
44
45
46 MathedArray::MathedArray(MathedArray const & array)
47 {
48         // this "implementation" is obviously wrong: MathedIter should be
49         // implemented by MathedArray (not the other way round) but I think
50         // getting the _interface_ of MathedArray right is more important right
51         // now (Andre')
52
53         // shallow copy
54         bf_   = array.bf_;
55         last_ = array.last_;
56
57         // deep copy
58         // we'll not yet get exeption safety
59         MathedIter it(this);
60         while (it.OK()) {
61                 if (it.IsInset()) {
62                         MathedInset * inset = it.GetInset();
63                         inset = inset->Clone();
64                         raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
65                 }
66                 it.Next();
67         }
68 }
69
70
71 MathedArray & MathedArray::operator=(MathedArray const & array)
72 {
73         MathedArray tmp(array);
74         swap(tmp);
75         return *this;
76 }
77
78 void MathedArray::clear()
79 {
80         last_ = 0;
81         bf_.resize(1);
82         bf_[0] = 0;
83 }
84
85 void MathedArray::swap(MathedArray & array)
86 {
87         if (this != &array) {
88                 bf_.swap(array.bf_);
89                 std::swap(last_, array.last_);
90         }
91 }
92
93
94 MathedArray::iterator MathedArray::begin() 
95 {
96         return bf_.begin();
97 }
98
99
100 MathedArray::iterator MathedArray::end() 
101 {
102         return bf_.end();
103 }
104
105
106 MathedArray::const_iterator MathedArray::begin() const
107 {
108         return bf_.begin();
109 }
110
111
112 MathedArray::const_iterator MathedArray::end() const
113 {
114         return bf_.end();
115 }
116
117
118 int MathedArray::empty() const
119 {
120         return (last_ == 0);
121 }
122    
123
124 int MathedArray::last() const
125 {
126         return last_;
127 }
128
129
130 void MathedArray::last(int l)
131 {
132         last_ = l;
133 }
134
135
136 void MathedArray::need_size(int needed)
137 {
138         if (needed >= static_cast<int>(bf_.size()))
139                 resize(needed);
140 }
141
142
143 void MathedArray::resize(int newsize)
144 {
145         // still a bit smelly...
146         ++newsize;
147         bf_.resize(newsize + 1);
148         if (last_ >= newsize)
149                 last_ = newsize - 1;
150         bf_[last_] = 0;
151 }
152
153
154 void MathedArray::move(int p, int shift)
155 {
156         if (p <= last_) {
157                 need_size(last_ + shift);
158                 memmove(&bf_[p + shift], &bf_[p], last_ - p);
159                 last_ += shift;
160                 bf_[last_] = 0;
161         }
162 }
163
164
165
166 void MathedArray::shrink(int pos1, int pos2)
167 {
168         if (pos1 == 0 && pos2 >= last())        
169                 return;
170
171         short fc = 0;
172         if (pos1 > 0 && bf_[pos1] > ' ') {
173                 for (int p = pos1; p >= 0; --p) {
174                         if (MathIsFont(bf_[p])) {
175                                 if (p != pos1 - 1)
176                                         fc = bf_[p];
177                                 else
178                                         --pos1;
179                                 break;
180                         }
181                 }
182         }
183
184         if (pos2 > 0 && bf_[pos2] >= ' ' && MathIsFont(bf_[pos2 - 1]))
185                 --pos2;
186
187         int dx = pos2 - pos1;
188         MathedArray a;
189         a.resize(dx + 1);
190         strange_copy(&a, (fc) ? 1 : 0, pos1, dx);
191         if (fc) {
192                 a[0] = fc;
193                 ++dx;
194         }
195         a.last(dx);
196         a[dx] = '\0';
197
198         MathedIter it(&a);
199         it.Reset();
200
201         while (it.OK()) {
202                 if (it.IsInset()) {
203                         MathedInset * inset = it.GetInset();
204                         inset = inset->Clone();
205                         a.raw_pointer_insert(inset, it.getPos() + 1, sizeof(inset));
206                 }
207                 it.Next();
208         }
209         swap(a);
210 }
211
212
213 #if 0
214 void MathedArray::insert(MathedArray::iterator pos,
215                          MathedArray::const_iterator beg,
216                          MathedArray::const_iterator end)
217 {
218         bf_.insert(pos, beg, end);
219         last_ = bf_.size() - 1;
220 }
221 #else
222 void MathedArray::merge(MathedArray const & a, int p)
223 {
224         my_memcpy(&bf_[p], &a.bf_[0], a.last());
225 }
226 #endif
227
228
229 void MathedArray::raw_pointer_copy(MathedInset ** p, int pos) const
230 {
231         my_memcpy(p, &bf_[pos], sizeof(MathedInset*));
232 }
233
234
235 #if 0
236 void MathedArray::insertInset(int pos, MathedInset * p, int type)
237 {
238         //bf_.insert(pos, type);
239         InsetTable tmp(pos, p);
240         insetList_.push_back(tmp);
241 }
242
243
244 MathedInset * MathedArray::getInset(int pos) 
245 {
246         InsetList::const_iterator cit = insetList_.begin();
247         InsetList::const_iterator end = insetList_.end();
248         for (; cit != end; ++cit) {
249                 if ((*cit).pos == pos)
250                         return (*cit).inset;
251         }
252         // not found
253         return 0;
254         // We would really like to throw an exception instead... (Lgb)
255         // throw inset_not_found();
256 }
257
258 #else
259 void MathedArray::raw_pointer_insert(void * p, int pos, int len)
260 {
261         my_memcpy(&bf_[pos], &p, len);
262 }
263 #endif
264
265
266 void MathedArray::strange_copy(MathedArray * dest, int dpos,
267                                 int spos, int len)
268 {
269         my_memcpy(&dest->bf_[dpos], &bf_[spos], len);
270 }
271
272
273 byte MathedArray::operator[](int i) const
274 {
275         return bf_[i];
276 }
277
278
279 byte & MathedArray::operator[](int i)
280 {
281         return bf_[i];
282 }