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