]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/array.C
further code uglification to make Jean-Marc's compiler happy
[lyx.git] / src / mathed / array.C
index c1cee61e430c1d9d3e96aaeb9b5970f7cf8dd1aa..cdf974e615889e990e7a862f4d770d72a1b02c92 100644 (file)
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "math_inset.h"
+#include "math_charinset.h"
+#include "math_scriptinset.h"
 #include "debug.h"
 #include "array.h"
-#include "math_scriptinset.h"
-#include "math_parser.h"
 #include "mathed/support.h"
 
 using std::ostream;
 using std::endl;
 
+
 MathArray::MathArray()
 {}
 
 
-MathArray::~MathArray()
-{
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       delete nextInset(pos);
-}
-
-
-MathArray::MathArray(MathArray const & array)
-       : bf_(array.bf_)
-{
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       replace(pos, nextInset(pos)->clone());
-}
-
-MathArray::MathArray(MathArray const & array, int from, int to)
-       : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
-{
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       replace(pos, nextInset(pos)->clone());
-}
-
-
-bool MathArray::next(int & pos) const
-{
-       if (pos >= size() - 1)
-               return false;
-
-       pos += item_size(pos);
-       return true;
-}
-
-
-bool MathArray::prev(int & pos) const
-{
-       if (pos == 0)
-               return false;
-
-       pos -= item_size(pos - 1);
-       return true;
-}
-
-
-bool MathArray::last(int & pos) const
-{
-       pos = bf_.size();
-       return prev(pos);
-}
-
-
-int MathArray::item_size(int pos) const
-{
-       return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
-}
+MathArray::MathArray(MathArray const & array, size_type from, size_type to)
+       : bf_(array.begin() + from, array.begin() + to)
+{}
 
 
 void MathArray::substitute(MathMacro const & m)
 {
-       MathArray tmp;
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) 
-                       nextInset(pos)->substitute(tmp, m);
-               else 
-                       tmp.push_back(GetChar(pos), GetCode(pos));
-       }
-       swap(tmp);
-}
-
-
-MathArray & MathArray::operator=(MathArray const & array)
-{
-       MathArray tmp(array);
-       swap(tmp);
-       return *this;
-}
-
-
-MathInset * MathArray::nextInset(int pos) const
-{
-       if (!isInset(pos))
-               return 0;
-       MathInset * p;
-       memcpy(&p, &bf_[0] + pos + 1, sizeof(p));
-       return p;
-}
-
-MathInset * MathArray::prevInset(int pos) const
-{
-       if (!pos)
-               return 0;
-       prev(pos);
-       return nextInset(pos);
-}
-
-
-unsigned char MathArray::GetChar(int pos) const
-{
-       return pos < size() ? bf_[pos + 1] : '\0';
-}
-
-
-string MathArray::GetString(int & pos) const
-{
-       string s;
-       if (isInset(pos))
-               return s;
-
-       MathTextCodes const fcode = GetCode(pos);
-       do {
-               s += GetChar(pos);
-               next(pos);
-       } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
-
-       return s;
-}
-
-
-MathTextCodes MathArray::GetCode(int pos) const
-{
-       return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
+       for (iterator it = begin(); it != end(); ++it)
+               it->substitute(m);
 }
 
 
-void MathArray::setCode(int pos, MathTextCodes t)
+MathAtom * MathArray::at(size_type pos)
 {
-       if (pos > size() || isInset(pos))
-               return;
-       bf_[pos] = t;
-       bf_[pos + 2] = t;
+       return pos < size() ? &bf_[pos] : 0;
 }
 
 
-void MathArray::replace(int pos, MathInset * p)
+MathAtom const * MathArray::at(size_type pos) const
 {
-       memcpy(&bf_[pos + 1], &p, sizeof(p));
+       return pos < size() ? &bf_[pos] : 0;
 }
 
 
-void MathArray::insert(int pos, MathInset * p)
+void MathArray::insert(size_type pos, MathInset * p)
 {
-       bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
-       memcpy(&bf_[pos + 1], &p, sizeof(p));
+       //cerr << "\n  1: "; p->write(cerr, true); cerr << p << "\n";
+       // inserting here invalidates the pointer!
+       bf_.insert(begin() + pos, MathAtom(p));
+       //cerr << "\n  2: "; p->write(cerr, true); cerr << p << "\n";
 }
 
 
-void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
+void MathArray::insert(size_type pos, MathArray const & array)
 {
-       bf_.insert(bf_.begin() + pos, 3, t);
-       bf_[pos + 1] = b;
-}
-
-
-void MathArray::insert(int pos, MathArray const & array)
-{
-       bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
-       for (int p = pos; p < pos + array.size(); next(p)) 
-               if (isInset(p)) 
-                       replace(p, nextInset(p)->clone());
+       bf_.insert(begin() + pos, array.begin(), array.end());
 }
 
 
@@ -186,12 +62,6 @@ void MathArray::push_back(MathInset * p)
 }
 
 
-void MathArray::push_back(unsigned char b, MathTextCodes c)
-{
-       insert(size(), b, c);
-}
-
-
 void MathArray::push_back(MathArray const & array)
 {
        insert(size(), array);
@@ -200,10 +70,7 @@ void MathArray::push_back(MathArray const & array)
 
 void MathArray::clear()
 {
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       delete nextInset(pos);
-       bf_.clear();
+       erase();
 }
 
 
@@ -220,7 +87,7 @@ bool MathArray::empty() const
 }
    
 
-int MathArray::size() const
+MathArray::size_type MathArray::size() const
 {
        return bf_.size();
 }
@@ -232,55 +99,36 @@ void MathArray::erase()
 }
 
 
-void MathArray::erase(int pos)
+void MathArray::erase(size_type pos)
 {
-       if (pos < static_cast<int>(bf_.size()))
-               erase(pos, pos + item_size(pos));
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathArray::erase(int pos1, int pos2)
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
+       bf_.erase(begin() + pos1, begin() + pos2);
 }
 
 
-bool MathArray::isInset(int pos) const
+MathAtom & MathArray::back()
 {
-       if (pos >= size())
-               return false;
-       return MathIsInset(static_cast<MathTextCodes>(bf_[pos]));
-}
-
-
-MathInset * MathArray::back_inset() const
-{
-       if (!empty()) {
-               int pos = size();
-               prev(pos);
-               if (isInset(pos))
-                       return nextInset(pos);
-       }
-       return 0;
+       return bf_.back();
 }
 
 
 void MathArray::dump2(ostream & os) const
 {
-       for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
-               os << int(*it) << ' ';
-       os << endl;
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << *it << ' ';
 }
 
 
 void MathArray::dump(ostream & os) const
 {
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) 
-                       os << "<inset: " << nextInset(pos) << ">";
-               else 
-                       os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
-       }
+       for (const_iterator it = begin(); it != end(); ++it)
+               os << "<" << *it << ">";
 }
 
 
@@ -291,81 +139,90 @@ std::ostream & operator<<(std::ostream & os, MathArray const & ar)
 }
 
 
-void MathArray::Write(ostream & os, bool fragile) const
+// returns sequence of char with same code starting at it up to end
+// it might be less, though...
+string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
 {
-       if (empty())
-               return;
+       string s;
+       MathCharInset const * p = it->nucleus()->asCharInset();
+       if (!p)
+               return s;
 
-       int brace = 0;
-       
-       for (int pos = 0; pos < size(); next(pos)) {
-               if (isInset(pos)) {
+       for (MathTextCodes c = p->code(); it != end; ++it) {
+               p = it->nucleus()->asCharInset();
+               if (!p || it->up() || it->down() || p->code() != c)
+                       break;
+               s += p->getChar();
+       }
+       return s;
+}
 
-                       nextInset(pos)->Write(os, fragile);
 
+void MathArray::write(ostream & os, bool fragile) const
+{
+       for (const_iterator it = begin(); it != end(); ++it) {
+               MathCharInset const * p = it->nucleus()->asCharInset();
+               if (p && !it->up() && !it->down()) {
+                       // special handling for character sequences with the same code
+                       string s = charSequence(it, end());
+                       p->writeHeader(os);
+                       os << s;
+                       p->writeTrailer(os);
+                       it += s.size() - 1;
                } else {
-
-                       MathTextCodes fcode = GetCode(pos);
-                       unsigned char c = GetChar(pos);
-
-                       if (MathIsSymbol(fcode)) {
-                               latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
-
-                               if (l == 0) {
-                                       l = lm_get_key_by_id(c, LM_TK_BIGSYM);
-                               }
-
-                               if (l) {
-                                       os << '\\' << l->name << ' ';
-                               } else {
-                                       lyxerr << "Could not find the LaTeX name for  " << c << " and fcode " << fcode << "!" << std::endl;
-                               }
-                       } else {
-                               if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) 
-                                       os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
-
-                               // Is there a standard logical XOR?
-                               if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
-                                               (fcode == LM_TC_SPECIAL))
-                                       os << '\\';
-                               else {
-                                       if (c == '{')
-                                               ++brace;
-                                       if (c == '}')
-                                               --brace;
-                               }
-                               if (c == '}' && fcode == LM_TC_TEX && brace < 0) 
-                                       lyxerr <<"Math warning: Unexpected closing brace.\n";
-                               else           
-                                       os << c;
-                       }
-
-                       if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
-                               os << '}';
-                       
+                       it->write(os, fragile);
                }
        }
-
-       if (brace > 0)
-               os << string(brace, '}');
 }
 
 
-void MathArray::WriteNormal(ostream & os) const
+void MathArray::writeNormal(ostream & os) const
 {
        if (empty()) {
                os << "[par] ";
                return;
        }
 
-       Write(os, true);
+       write(os, true);
+}
+
+
+void MathArray::validate(LaTeXFeatures & features) const
+{
+       for (const_iterator it = begin(); it != end(); ++it)
+               it->validate(features);
+}
+
+
+void MathArray::pop_back()
+{      
+       if (!size()) {
+               lyxerr << "pop_back from empty array!\n";
+               return;
+       }
+       bf_.pop_back();
+}
+
+
+MathArray::const_iterator MathArray::begin() const
+{
+       return bf_.begin();
 }
 
 
-void MathArray::Validate(LaTeXFeatures & features) const
+MathArray::const_iterator MathArray::end() const
 {
-       for (int pos = 0; pos < size(); next(pos)) 
-               if (isInset(pos)) 
-                       nextInset(pos)->Validate(features);
+       return bf_.end();
 }
 
+
+MathArray::iterator MathArray::begin()
+{
+       return bf_.begin();
+}
+
+
+MathArray::iterator MathArray::end()
+{
+       return bf_.end();
+}