lyx_mirror/src/frontends/controllers/ControlParagraph.C
Abdelrazak Younes 4d5ae916ad Fix bug 3293 by Bernhard Roider:
This changes the semantics of isOK() and operator(), comments from Bernhard below:

With the old version of lyxlex it was _impossible_ to check whether reading an integer, float, ... succeeded or not. The current solution to check for is.bad() in some cases and in other cases use is.good() does not give the desired information. Moreover the result of is.bad() depends on the stl implementation and behaves different for linux and windows.

the bug was introduced by the patch that fixed the bug that crashed lyx when "inset-insert ert" was executed from the command buffer.
The lexer has the method isOK() which reflects the status of the stream is.
The operators void* and ! are not really well defined (they depend on the value of is.bad()). What is missing is a test if the last reading operation was successful and thus the returned value is valid.
That's what i implemented in this patch.

The new rule for using the lexer:

if you want to know if the lexer still has data to read (either from the stream or from the pushed token) then use "lex.isOK()".
If you want to test if the last reading operation was successful then use eg. "if (lex) {...}" or unsuccessful then use eg. "if (!lex) {...}"

an example:

int readParam(LyxLex &lex) {

    int param = 1; // default value
    if (lex.isOK()) { // the lexer has data to read
        int p;    // temporary variable
        lex >> p;
        if (lex) param = p; // only use the input if the reading operation was successful
    }
    return param;
}




git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17569 a592a061-630c-0410-9148-cb99ea01b6c8
2007-03-26 13:43:49 +00:00

170 lines
3.1 KiB
C

/**
* \file ControlParagraph.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Edwin Leuven
* \author Angus Leeming
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "ControlParagraph.h"
#include "ButtonController.h"
#include "funcrequest.h"
#include "lyxlex.h"
#include "paragraph.h"
#include "ParagraphParameters.h"
#include <sstream>
using std::istringstream;
using std::ostringstream;
using std::string;
namespace lyx {
namespace frontend {
ControlParagraph::ControlParagraph(Dialog & parent)
: Dialog::Controller(parent), ininset_(false)
{}
bool ControlParagraph::initialiseParams(string const & data)
{
istringstream is(data);
LyXLex lex(0,0);
lex.setStream(is);
// Set tri-state flag:
// action == 0: show dialog
// action == 1: update dialog, accept changes
// action == 2: update dialog, do not accept changes
int action = 0;
if (lex.isOK()) {
lex.next();
string const token = lex.getString();
if (token == "show") {
action = 0;
} else if (token == "update") {
lex.next();
bool const accept = lex.getBool();
if (lex) {
action = accept ? 1 : 2;
} else {
// Unrecognised update option
return false;
}
} else if (!token.empty()) {
// Unrecognised token
return false;
}
}
ParagraphParameters * tmp = new ParagraphParameters;
tmp->read(lex);
// For now, only reset the params on "show".
// Don't bother checking if the params are different on "update"
if (action == 0) {
params_.reset(tmp);
} else {
delete tmp;
}
// Read the rest of the data irrespective of "show" or "update"
int nset = 0;
while (lex.isOK()) {
lex.next();
string const token = lex.getString();
if (token.empty())
continue;
int Int = 0;
if (token == "\\alignpossible" ||
token == "\\aligndefault" ||
token == "\\ininset") {
lex.next();
Int = lex.getInteger();
} else {
// Unrecognised token
return false;
}
++nset;
if (token == "\\alignpossible") {
alignpossible_ = static_cast<LyXAlignment>(Int);
} else if (token == "\\aligndefault") {
aligndefault_ = static_cast<LyXAlignment>(Int);
} else {
ininset_ = Int;
}
}
if (nset != 3) {
return false;
}
// If "update", then set the activation status of the button controller
if (action > 0) {
bool const accept = action == 1;
dialog().bc().valid(accept);
}
return true;
}
void ControlParagraph::clearParams()
{
params_.reset();
}
void ControlParagraph::dispatchParams()
{
ostringstream data;
params().write(data);
FuncRequest const fr(LFUN_PARAGRAPH_PARAMS_APPLY, data.str());
kernel().dispatch(fr);
}
ParagraphParameters & ControlParagraph::params()
{
BOOST_ASSERT(params_.get());
return *params_;
}
ParagraphParameters const & ControlParagraph::params() const
{
BOOST_ASSERT(params_.get());
return *params_;
}
bool ControlParagraph::inInset() const
{
return ininset_;
}
LyXAlignment ControlParagraph::alignPossible() const
{
return alignpossible_;
}
LyXAlignment ControlParagraph::alignDefault() const
{
return aligndefault_;
}
} // namespace frontend
} // namespace lyx