From f849bc4a629e15389f629fd4f187790bb68db464 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 4 Jul 2002 13:54:28 +0000 Subject: [PATCH] separate insetcommand.[Ch] into two files we should be ble to use this later to reduce include dependencies git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4530 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/ChangeLog | 7 ++ src/insets/Makefile.am | 2 + src/insets/insetcommand.C | 158 ----------------------------- src/insets/insetcommand.h | 47 +-------- src/insets/insetcommandparams.C | 174 ++++++++++++++++++++++++++++++++ src/insets/insetcommandparams.h | 73 ++++++++++++++ 6 files changed, 257 insertions(+), 204 deletions(-) create mode 100644 src/insets/insetcommandparams.C create mode 100644 src/insets/insetcommandparams.h diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 979b57f0cf..0373696c39 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,10 @@ + +2002-07-04 André Pönitz + + * insetcommandparams.[Ch]: new files + + * insetcommand.[Ch]: move code to insetcommandparams.[Ch] + 2002-06-15 Herbert Voss * insetgraphics.C (prepareFile): bugfix; return always diff --git a/src/insets/Makefile.am b/src/insets/Makefile.am index 8817b75e06..4063687fbd 100644 --- a/src/insets/Makefile.am +++ b/src/insets/Makefile.am @@ -29,6 +29,8 @@ libinsets_la_SOURCES = \ insetcollapsable.h \ insetcommand.C \ insetcommand.h \ + insetcommandparams.C \ + insetcommandparams.h \ inseterror.C \ inseterror.h \ insetert.C \ diff --git a/src/insets/insetcommand.C b/src/insets/insetcommand.C index dd1b3afac8..1c47948e7e 100644 --- a/src/insets/insetcommand.C +++ b/src/insets/insetcommand.C @@ -17,169 +17,11 @@ #include "insetcommand.h" #include "debug.h" #include "frontends/Painter.h" -#include "lyxlex.h" using std::ostream; using std::endl; -InsetCommandParams::InsetCommandParams() -{} - - -InsetCommandParams::InsetCommandParams(string const & n, - string const & c, - string const & o) - : cmdname(n), contents(c), options(o) -{} - - -string const InsetCommandParams::getAsString() const -{ - return cmdname + "|++|" + contents + "|++|" + options; -} - - -void InsetCommandParams::setFromString(string const & b) -{ - string::size_type idx = b.find("|++|"); - if (idx == string::npos) { - cmdname = b; - contents = ""; - options = ""; - return; - } - - cmdname = b.substr(0, idx); - string tmp = b.substr(idx+4); - - idx = tmp.find("|++|"); - if (idx == string::npos) { - contents = tmp; - options = ""; - } else { - contents = tmp.substr(0, idx); - options = tmp.substr(idx+4); - } -} - - -bool InsetCommandParams::operator==(InsetCommandParams const & o) const -{ - return cmdname == o.cmdname && contents == o.contents - && options == o.options; -} - - -bool InsetCommandParams::operator!=(InsetCommandParams const & o) const -{ - return !(*this == o); -} - - -void InsetCommandParams::scanCommand(string const & cmd) -{ - string tcmdname, toptions, tcontents; - - if (cmd.empty()) return; - - enum { WS, CMDNAME, OPTION, CONTENT } state = WS; - - // Used to handle things like \command[foo[bar]]{foo{bar}} - int nestdepth = 0; - - for (string::size_type i = 0; i < cmd.length(); ++i) { - char c = cmd[i]; - if ((state == CMDNAME && c == ' ') || - (state == CMDNAME && c == '[') || - (state == CMDNAME && c == '{')) { - state = WS; - } - if ((state == OPTION && c == ']') || - (state == CONTENT && c == '}')) { - if (nestdepth == 0) { - state = WS; - } else { - --nestdepth; - } - } - if ((state == OPTION && c == '[') || - (state == CONTENT && c == '{')) { - ++nestdepth; - } - switch (state) { - case CMDNAME: tcmdname += c; break; - case OPTION: toptions += c; break; - case CONTENT: tcontents += c; break; - case WS: - if (c == '\\') { - state = CMDNAME; - } else if (c == '[') { - state = OPTION; - nestdepth = 0; // Just to be sure - } else if (c == '{') { - state = CONTENT; - nestdepth = 0; // Just to be sure - } - break; - } - } - - // Don't mess with this. - if (!tcmdname.empty()) setCmdName(tcmdname); - if (!toptions.empty()) setOptions(toptions); - if (!tcontents.empty()) setContents(tcontents); - - if (lyxerr.debugging(Debug::PARSER)) - lyxerr << "Command <" << cmd - << "> == <" << getCommand() - << "> == <" << getCmdName() - << '|' << getContents() - << '|' << getOptions() << '>' << endl; -} - - -// This function will not be necessary when lyx3 -void InsetCommandParams::read(LyXLex & lex) -{ - string token; - - if (lex.eatLine()) { - token = lex.getString(); - scanCommand(token); - } else { - lex.printError("InsetCommand: Parse error: `$$Token'"); - } - - while (lex.isOK()) { - lex.nextToken(); - token = lex.getString(); - if (token == "\\end_inset") - break; - } - if (token != "\\end_inset") { - lex.printError("Missing \\end_inset at this point. " - "Read: `$$Token'"); - } -} - - -void InsetCommandParams::write(ostream & os) const -{ - os << "LatexCommand " << getCommand() << "\n"; -} - - -string const InsetCommandParams::getCommand() const -{ - string s; - if (!getCmdName().empty()) s += "\\"+getCmdName(); - if (!getOptions().empty()) s += "["+getOptions()+']'; - s += "{"+getContents()+'}'; - return s; -} - - InsetCommand::InsetCommand(InsetCommandParams const & p, bool) : p_(p.getCmdName(), p.getContents(), p.getOptions()) {} diff --git a/src/insets/insetcommand.h b/src/insets/insetcommand.h index 466bef87f6..edae513119 100644 --- a/src/insets/insetcommand.h +++ b/src/insets/insetcommand.h @@ -17,6 +17,7 @@ #endif #include "insetbutton.h" +#include "insetcommandparams.h" #include #include @@ -26,52 +27,6 @@ * Similar to InsetLaTeX but having control of the basic structure of a * LaTeX command: \name[options]{contents}. */ -class InsetCommandParams { -public: - /// - InsetCommandParams(); - /// - explicit - InsetCommandParams(string const & n, - string const & c = string(), - string const & o = string()); - /// - bool operator==(InsetCommandParams const &) const; - /// - bool operator!=(InsetCommandParams const &) const; - /// - void read(LyXLex &); - /// Parse the command - void scanCommand(string const &); - /// - void write(std::ostream &) const; - /// Build the complete LaTeX command - string const getCommand() const; - /// - string const & getCmdName() const { return cmdname; } - /// - string const & getOptions() const { return options; } - /// - string const & getContents() const { return contents; } - /// - void setCmdName(string const & n) { cmdname = n; } - /// - void setOptions(string const & o) { options = o; } - /// - void setContents(string const & c) { contents = c; } - /// - string const getAsString() const; - /// - void setFromString(string const &); -private: - /// - string cmdname; - /// - string contents; - /// - string options; -}; - /// class InsetCommand : public InsetButton, boost::noncopyable { diff --git a/src/insets/insetcommandparams.C b/src/insets/insetcommandparams.C new file mode 100644 index 0000000000..93a3070d49 --- /dev/null +++ b/src/insets/insetcommandparams.C @@ -0,0 +1,174 @@ +/* This file is part of + * ====================================================== + * + * LyX, The Document Processor + * + * Copyright 2002-2002 The LyX Team. + * + * ====================================================== */ + + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "insetcommandparams.h" +#include "lyxlex.h" +#include "debug.h" + + +InsetCommandParams::InsetCommandParams() +{} + + +InsetCommandParams::InsetCommandParams(string const & n, + string const & c, + string const & o) + : cmdname(n), contents(c), options(o) +{} + + +string const InsetCommandParams::getAsString() const +{ + return cmdname + "|++|" + contents + "|++|" + options; +} + + +void InsetCommandParams::setFromString(string const & b) +{ + string::size_type idx = b.find("|++|"); + if (idx == string::npos) { + cmdname = b; + contents = ""; + options = ""; + return; + } + + cmdname = b.substr(0, idx); + string tmp = b.substr(idx+4); + + idx = tmp.find("|++|"); + if (idx == string::npos) { + contents = tmp; + options = ""; + } else { + contents = tmp.substr(0, idx); + options = tmp.substr(idx+4); + } +} + + +bool InsetCommandParams::operator==(InsetCommandParams const & o) const +{ + return cmdname == o.cmdname && contents == o.contents + && options == o.options; +} + + +bool InsetCommandParams::operator!=(InsetCommandParams const & o) const +{ + return !(*this == o); +} + + +void InsetCommandParams::scanCommand(string const & cmd) +{ + string tcmdname, toptions, tcontents; + + if (cmd.empty()) return; + + enum { WS, CMDNAME, OPTION, CONTENT } state = WS; + + // Used to handle things like \command[foo[bar]]{foo{bar}} + int nestdepth = 0; + + for (string::size_type i = 0; i < cmd.length(); ++i) { + char c = cmd[i]; + if ((state == CMDNAME && c == ' ') || + (state == CMDNAME && c == '[') || + (state == CMDNAME && c == '{')) { + state = WS; + } + if ((state == OPTION && c == ']') || + (state == CONTENT && c == '}')) { + if (nestdepth == 0) { + state = WS; + } else { + --nestdepth; + } + } + if ((state == OPTION && c == '[') || + (state == CONTENT && c == '{')) { + ++nestdepth; + } + switch (state) { + case CMDNAME: tcmdname += c; break; + case OPTION: toptions += c; break; + case CONTENT: tcontents += c; break; + case WS: + if (c == '\\') { + state = CMDNAME; + } else if (c == '[') { + state = OPTION; + nestdepth = 0; // Just to be sure + } else if (c == '{') { + state = CONTENT; + nestdepth = 0; // Just to be sure + } + break; + } + } + + // Don't mess with this. + if (!tcmdname.empty()) setCmdName(tcmdname); + if (!toptions.empty()) setOptions(toptions); + if (!tcontents.empty()) setContents(tcontents); + + if (lyxerr.debugging(Debug::PARSER)) + lyxerr << "Command <" << cmd + << "> == <" << getCommand() + << "> == <" << getCmdName() + << '|' << getContents() + << '|' << getOptions() << '>' << endl; +} + + +void InsetCommandParams::read(LyXLex & lex) +{ + string token; + + if (lex.eatLine()) { + token = lex.getString(); + scanCommand(token); + } else { + lex.printError("InsetCommand: Parse error: `$$Token'"); + } + + while (lex.isOK()) { + lex.nextToken(); + token = lex.getString(); + if (token == "\\end_inset") + break; + } + if (token != "\\end_inset") { + lex.printError("Missing \\end_inset at this point. " + "Read: `$$Token'"); + } +} + + +void InsetCommandParams::write(ostream & os) const +{ + os << "LatexCommand " << getCommand() << "\n"; +} + + +string const InsetCommandParams::getCommand() const +{ + string s; + if (!getCmdName().empty()) s += "\\"+getCmdName(); + if (!getOptions().empty()) s += "["+getOptions()+']'; + s += "{"+getContents()+'}'; + return s; +} + diff --git a/src/insets/insetcommandparams.h b/src/insets/insetcommandparams.h new file mode 100644 index 0000000000..90bb0eb56a --- /dev/null +++ b/src/insets/insetcommandparams.h @@ -0,0 +1,73 @@ +// -*- C++ -*- +/* This file is part of* + * ====================================================== + * + * LyX, The Document Processor + * + * Copyright 2002-2001 The LyX Team. + * + * ====================================================== */ + +#ifndef INSETCOMMANDPARAMS_H +#define INSETCOMMANDPARAMS_H + +#ifdef __GNUG__ +#pragma interface +#endif + +#include + +#include "LString.h" + +#include + +class LyXLex; + +class InsetCommandParams { +public: + /// + InsetCommandParams(); + /// + explicit + InsetCommandParams(string const & n, + string const & c = string(), + string const & o = string()); + /// + bool operator==(InsetCommandParams const &) const; + /// + bool operator!=(InsetCommandParams const &) const; + /// + void read(LyXLex &); + /// Parse the command + void scanCommand(string const &); + /// + void write(std::ostream &) const; + /// Build the complete LaTeX command + string const getCommand() const; + /// + string const & getCmdName() const { return cmdname; } + /// + string const & getOptions() const { return options; } + /// + string const & getContents() const { return contents; } + /// + void setCmdName(string const & n) { cmdname = n; } + /// + void setOptions(string const & o) { options = o; } + /// + void setContents(string const & c) { contents = c; } + /// + string const getAsString() const; + /// + void setFromString(string const &); +private: + /// + string cmdname; + /// + string contents; + /// + string options; +}; + + +#endif -- 2.39.2