]> git.lyx.org Git - lyx.git/blobdiff - src/layout.C
implement getLabelList
[lyx.git] / src / layout.C
index 797f2855b1fa6aad79398705c2e38b448553a457..58fd7f1bb93ec9b31cc1b7a8965196533bbf3521 100644 (file)
 #include "debug.h"
 #include "gettext.h"
 #include "support/LAssert.h"
+#include "support/lyxfunctional.h"
 
 using std::pair;
 using std::make_pair;
 using std::sort;
 using std::endl;
+using std::find_if;
+using std::remove_if;
 
 // Global variable: textclass table.
 LyXTextClassList textclasslist;
@@ -185,7 +188,7 @@ bool LyXLayout::Read (LyXLex & lexrc, LyXTextClass const & tclass)
        while (!finished && lexrc.IsOK() && !error) {
                int le = lexrc.lex();
                // See comment in lyxrc.C.
-               switch(le) {
+               switch (le) {
                case LyXLex::LEX_FEOF:
                        continue; 
 
@@ -195,7 +198,7 @@ bool LyXLayout::Read (LyXLex & lexrc, LyXTextClass const & tclass)
                        continue; 
                default: break;
                }
-               switch(static_cast<LayoutTags>(le)) {
+               switch (static_cast<LayoutTags>(le)) {
                case LT_END:            // end of structure
                        finished = true;
                        break;
@@ -426,7 +429,7 @@ void LyXLayout::readAlign(LyXLex & lexrc)
                return; 
        default: break;
        };
-       switch(static_cast<AlignTags>(le)) {
+       switch (static_cast<AlignTags>(le)) {
        case AT_BLOCK:
                align = LYX_ALIGN_BLOCK;
                break;
@@ -595,19 +598,25 @@ void LyXLayout::readLabelType(LyXLex & lexrc)
        }
 }
 
-static keyword_item endlabelTypeTags[] = {
+
+namespace {
+
+keyword_item endlabelTypeTags[] = {
        { "box",        END_LABEL_BOX },
        { "filled_box", END_LABEL_FILLED_BOX },
        { "no_label",   END_LABEL_NO_LABEL },
        { "static",     END_LABEL_STATIC }
 };
 
+} // namespace anon
+
+
 void LyXLayout::readEndLabelType(LyXLex & lexrc)
 {
        pushpophelper pph(lexrc, endlabelTypeTags,
                          END_LABEL_ENUM_LAST-END_LABEL_ENUM_FIRST+1);
        int le = lexrc.lex();
-       switch(le) {
+       switch (le) {
        case LyXLex::LEX_UNDEF:
                lexrc.printError("Unknown labeltype tag `$$Token'");
                break;
@@ -638,7 +647,7 @@ void LyXLayout::readMargin(LyXLex & lexrc)
        pushpophelper pph(lexrc, marginTags, MARGIN_RIGHT_ADDRESS_BOX);
 
        int le = lexrc.lex();
-       switch(le) {
+       switch (le) {
        case LyXLex::LEX_UNDEF:
                lexrc.printError("Unknown margin type tag `$$Token'");
                return;
@@ -707,13 +716,13 @@ void LyXLayout::readSpacing(LyXLex & lexrc)
 
        pushpophelper pph(lexrc, spacingTags, ST_OTHER);
        int le = lexrc.lex();
-       switch(le) {
+       switch (le) {
        case LyXLex::LEX_UNDEF:
                lexrc.printError("Unknown spacing token `$$Token'");
                return;
        default: break;
        }
-       switch(static_cast<SpacingTags>(le)) {
+       switch (static_cast<SpacingTags>(le)) {
        case ST_SPACING_SINGLE:
                spacing.set(Spacing::Single);
                break;
@@ -832,7 +841,7 @@ bool LyXTextClass::Read(string const & filename, bool merge)
        // parsing
        while (lexrc.IsOK() && !error) {
                int le = lexrc.lex();
-               switch(le) {
+               switch (le) {
                case LyXLex::LEX_FEOF:
                        continue; 
 
@@ -842,7 +851,7 @@ bool LyXTextClass::Read(string const & filename, bool merge)
                        continue; 
                default: break;
                }
-               switch(static_cast<TextClassTags>(le)) {
+               switch (static_cast<TextClassTags>(le)) {
                case TC_OUTPUTTYPE:   // output type definition
                        readOutputType(lexrc);
                        break;
@@ -870,7 +879,7 @@ bool LyXTextClass::Read(string const & filename, bool merge)
                                        error = do_readStyle(lexrc, lay);
                                } else {
                                        LyXLayout lay;
-                                       lay.name(name);
+                                       lay.setName(name);
                                        if (!(error = do_readStyle(lexrc, lay)))
                                                layoutlist.push_back(lay);
                                }
@@ -898,7 +907,7 @@ bool LyXTextClass::Read(string const & filename, bool merge)
                        
                case TC_SIDES:
                        if (lexrc.next()) {
-                               switch(lexrc.GetInteger()) {
+                               switch (lexrc.GetInteger()) {
                                case 1: sides_ = OneSide; break;
                                case 2: sides_ = TwoSides; break;
                                default:
@@ -1000,7 +1009,7 @@ void LyXTextClass::readOutputType(LyXLex & lexrc)
        pushpophelper pph(lexrc, outputTypeTags, LITERATE);
 
        int le = lexrc.lex();
-       switch(le) {
+       switch (le) {
        case LyXLex::LEX_UNDEF:
                lexrc.printError("Unknown output type `$$Token'");
                return;
@@ -1050,7 +1059,7 @@ void LyXTextClass::readMaxCounter(LyXLex & lexrc)
 
        pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
        int le = lexrc.lex();
-       switch(le) {
+       switch (le) {
        case LyXLex::LEX_UNDEF:
                lexrc.printError("Unknown MaxCounter tag `$$Token'");
                return; 
@@ -1142,49 +1151,43 @@ void LyXTextClass::readClassOptions(LyXLex & lexrc)
 
 bool LyXTextClass::hasLayout(string const & name) const
 {
-       for (LayoutList::const_iterator cit = layoutlist.begin();
-            cit != layoutlist.end(); ++cit) {
-               if ((*cit).name() == name)
-                       return true;
-       }
-       return false;
+       return find_if(layoutlist.begin(), layoutlist.end(),
+                      compare_memfun(&LyXLayout::name, name))
+               != layoutlist.end();
 }
 
 
 LyXLayout const & LyXTextClass::GetLayout (string const & name) const
 {
-       for (LayoutList::const_iterator cit = layoutlist.begin();
-            cit != layoutlist.end(); ++cit) {
-               if ((*cit).name() == name)
-                       return (*cit);
-       }
-       Assert(false); // we actually require the name to exist.
-       return layoutlist.front();
+       LayoutList::const_iterator cit =
+               find_if(layoutlist.begin(),
+                       layoutlist.end(),
+                       compare_memfun(&LyXLayout::name, name));
+       Assert(cit != layoutlist.end()); // we require the name to exist
+       return (*cit);
 }
 
 
 LyXLayout & LyXTextClass::GetLayout(string const & name)
 {
-       for (LayoutList::iterator it = layoutlist.begin();
-            it != layoutlist.end(); ++it) {
-               if ((*it).name() == name)
-                       return (*it);
-       }
-       Assert(false); // we actually require the name to exist.
-       return layoutlist.front();
+       LayoutList::iterator it =
+               find_if(layoutlist.begin(),
+                       layoutlist.end(),
+                       compare_memfun(&LyXLayout::name, name));
+       Assert(it != layoutlist.end()); // we require the name to exist
+       return (*it);
 }
 
 
-bool LyXTextClass::delete_layout (string const & name)
+bool LyXTextClass::delete_layout(string const & name)
 {
-       for(LayoutList::iterator it = layoutlist.begin();
-           it != layoutlist.end(); ++it) {
-               if ((*it).name() == name) {
-                       layoutlist.erase(it);
-                       return true;
-               }
-       }
-       return false;
+       LayoutList::iterator it =
+               remove_if(layoutlist.begin(), layoutlist.end(),
+                         compare_memfun(&LyXLayout::name, name));
+       LayoutList::iterator end = layoutlist.end();
+       bool const ret = (it != end);
+       layoutlist.erase(it, end);
+       return ret;
 }
 
 
@@ -1194,7 +1197,7 @@ void LyXTextClass::load()
        if (loaded) return;
 
        // Read style-file
-       string real_file = LibFileSearch("layouts", name_, "layout");
+       string const real_file = LibFileSearch("layouts", name_, "layout");
 
        if (Read(real_file)) {
                lyxerr << "Error reading `"
@@ -1210,16 +1213,15 @@ void LyXTextClass::load()
 //////////////////////////////////////////
 
 // Gets textclass number from name
-pair<bool, LyXTextClassList::size_type>
+pair<bool, LyXTextClassList::size_type> const
 LyXTextClassList::NumberOfClass(string const & textclass) const
 {
-       for (ClassList::const_iterator cit = classlist.begin();
-            cit != classlist.end(); ++cit) {
-               if ((*cit).name() == textclass)
-                       return make_pair(true,
-                                        size_type(cit - classlist.begin()));
-       }
-       return make_pair(false, size_type(0));
+       ClassList::const_iterator cit =
+               find_if(classlist.begin(), classlist.end(),
+                       compare_memfun(&LyXTextClass::name, textclass));
+       return cit != classlist.end() ?
+               make_pair(true, size_type(cit - classlist.begin())) :
+               make_pair(false, size_type(0));
 }
 
 
@@ -1236,12 +1238,12 @@ LyXTextClassList::Style(LyXTextClassList::size_type textclass,
 
 
 // Gets layout number from name and textclass number
-pair<bool, LyXTextClass::size_type>
+pair<bool, LyXTextClass::size_type> const
 LyXTextClassList::NumberOfLayout(LyXTextClassList::size_type textclass,
                                 string const & name) const
 {
        classlist[textclass].load();
-       for(unsigned int i = 0; i < classlist[textclass].numLayouts(); ++i) {
+       for (unsigned int i = 0; i < classlist[textclass].numLayouts(); ++i) {
                if (classlist[textclass][i].name() == name)
                        return make_pair(true, i);
        }
@@ -1450,3 +1452,4 @@ std::ostream & operator<<(std::ostream & os, LyXTextClass::PageSides p)
        }
        return os;
 }
+