2001-12-20 14:52:15 +00:00
|
|
|
|
/* This file is part of
|
2002-03-21 17:27:08 +00:00
|
|
|
|
* ======================================================
|
|
|
|
|
*
|
2001-12-20 14:52:15 +00:00
|
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2001-12-20 14:52:15 +00:00
|
|
|
|
* Copyright 2000-2001 The LyX Team.
|
|
|
|
|
*
|
|
|
|
|
* @author: J<EFBFBD>rgen Vigna
|
|
|
|
|
*
|
2002-03-21 17:27:08 +00:00
|
|
|
|
* ======================================================
|
2001-12-20 14:52:15 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "tabular_funcs.h"
|
2001-12-20 17:23:17 +00:00
|
|
|
|
|
|
|
|
|
#include "support/LIstream.h"
|
2001-12-20 14:52:15 +00:00
|
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
|
using std::strlen;
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-12-20 17:23:17 +00:00
|
|
|
|
using std::istream;
|
2001-12-26 14:45:16 +00:00
|
|
|
|
using std::getline;
|
2001-12-20 14:52:15 +00:00
|
|
|
|
|
|
|
|
|
// Perfect case for a template... (Lgb)
|
|
|
|
|
// or perhaps not...
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
string const write_attribute(string const & name, bool const & b)
|
|
|
|
|
{
|
2002-02-22 13:25:03 +00:00
|
|
|
|
// we write only true attribute values so we remove a bit of the
|
|
|
|
|
// file format bloat for tabulars.
|
|
|
|
|
if (!b)
|
|
|
|
|
return string();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-02-26 08:24:48 +00:00
|
|
|
|
return write_attribute(name, tostr(b));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
string const write_attribute(string const & name, int const & i)
|
|
|
|
|
{
|
|
|
|
|
// we write only true attribute values so we remove a bit of the
|
|
|
|
|
// file format bloat for tabulars.
|
|
|
|
|
if (!i)
|
|
|
|
|
return string();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-02-26 08:24:48 +00:00
|
|
|
|
return write_attribute(name, tostr(i));
|
2001-12-20 14:52:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
string const write_attribute(string const & name, LyXLength const & value)
|
|
|
|
|
{
|
2002-02-22 13:25:03 +00:00
|
|
|
|
// we write only the value if we really have one same reson as above.
|
|
|
|
|
if (value.zero())
|
|
|
|
|
return string();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-12-20 14:52:15 +00:00
|
|
|
|
return write_attribute(name, value.asString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string const tostr(LyXAlignment const & num)
|
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
|
switch (num) {
|
2001-12-20 14:52:15 +00:00
|
|
|
|
case LYX_ALIGN_NONE:
|
|
|
|
|
return "none";
|
|
|
|
|
case LYX_ALIGN_BLOCK:
|
|
|
|
|
return "block";
|
|
|
|
|
case LYX_ALIGN_LEFT:
|
|
|
|
|
return "left";
|
|
|
|
|
case LYX_ALIGN_CENTER:
|
|
|
|
|
return "center";
|
|
|
|
|
case LYX_ALIGN_RIGHT:
|
|
|
|
|
return "right";
|
|
|
|
|
case LYX_ALIGN_LAYOUT:
|
|
|
|
|
return "layout";
|
|
|
|
|
case LYX_ALIGN_SPECIAL:
|
|
|
|
|
return "special";
|
|
|
|
|
}
|
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const tostr(LyXTabular::VAlignment const & num)
|
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
|
switch (num) {
|
2001-12-20 14:52:15 +00:00
|
|
|
|
case LyXTabular::LYX_VALIGN_TOP:
|
|
|
|
|
return "top";
|
|
|
|
|
case LyXTabular::LYX_VALIGN_CENTER:
|
|
|
|
|
return "center";
|
|
|
|
|
case LyXTabular::LYX_VALIGN_BOTTOM:
|
|
|
|
|
return "bottom";
|
|
|
|
|
}
|
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const tostr(LyXTabular::BoxType const & num)
|
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
|
switch (num) {
|
2001-12-20 14:52:15 +00:00
|
|
|
|
case LyXTabular::BOX_NONE:
|
|
|
|
|
return "none";
|
|
|
|
|
case LyXTabular::BOX_PARBOX:
|
|
|
|
|
return "parbox";
|
|
|
|
|
case LyXTabular::BOX_MINIPAGE:
|
|
|
|
|
return "minipage";
|
|
|
|
|
}
|
|
|
|
|
return string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I would have liked a fromstr template a lot better. (Lgb)
|
|
|
|
|
bool string2type(string const str, LyXAlignment & num)
|
|
|
|
|
{
|
|
|
|
|
if (str == "none")
|
|
|
|
|
num = LYX_ALIGN_NONE;
|
|
|
|
|
else if (str == "block")
|
|
|
|
|
num = LYX_ALIGN_BLOCK;
|
|
|
|
|
else if (str == "left")
|
|
|
|
|
num = LYX_ALIGN_LEFT;
|
|
|
|
|
else if (str == "center")
|
|
|
|
|
num = LYX_ALIGN_CENTER;
|
|
|
|
|
else if (str == "right")
|
|
|
|
|
num = LYX_ALIGN_RIGHT;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool string2type(string const str, LyXTabular::VAlignment & num)
|
|
|
|
|
{
|
|
|
|
|
if (str == "top")
|
|
|
|
|
num = LyXTabular::LYX_VALIGN_TOP;
|
|
|
|
|
else if (str == "center")
|
|
|
|
|
num = LyXTabular::LYX_VALIGN_CENTER;
|
|
|
|
|
else if (str == "bottom")
|
|
|
|
|
num = LyXTabular::LYX_VALIGN_BOTTOM;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool string2type(string const str, LyXTabular::BoxType & num)
|
|
|
|
|
{
|
|
|
|
|
if (str == "none")
|
|
|
|
|
num = LyXTabular::BOX_NONE;
|
|
|
|
|
else if (str == "parbox")
|
|
|
|
|
num = LyXTabular::BOX_PARBOX;
|
|
|
|
|
else if (str == "minipage")
|
|
|
|
|
num = LyXTabular::BOX_MINIPAGE;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool string2type(string const str, bool & num)
|
|
|
|
|
{
|
|
|
|
|
if (str == "true")
|
|
|
|
|
num = true;
|
|
|
|
|
else if (str == "false")
|
|
|
|
|
num = false;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token, string & ret)
|
|
|
|
|
{
|
2002-02-26 08:24:48 +00:00
|
|
|
|
ret.erase();
|
2001-12-20 14:52:15 +00:00
|
|
|
|
size_t token_length = strlen(token);
|
|
|
|
|
string::size_type pos = str.find(token);
|
|
|
|
|
|
|
|
|
|
if (pos == string::npos || pos + token_length + 1 >= str.length()
|
|
|
|
|
|| str[pos + token_length] != '=')
|
|
|
|
|
return false;
|
|
|
|
|
pos += token_length + 1;
|
|
|
|
|
char ch = str[pos];
|
|
|
|
|
if ((ch != '"') && (ch != '\'')) { // only read till next space
|
|
|
|
|
ret += ch;
|
|
|
|
|
ch = ' ';
|
|
|
|
|
}
|
|
|
|
|
while ((pos < str.length() - 1) && (str[++pos] != ch))
|
|
|
|
|
ret += str[pos];
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token, int & num)
|
|
|
|
|
{
|
|
|
|
|
string tmp;
|
2002-02-26 08:24:48 +00:00
|
|
|
|
num = 0;
|
2001-12-20 14:52:15 +00:00
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
num = strToInt(tmp);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
|
|
|
|
|
{
|
|
|
|
|
string tmp;
|
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
return string2type(tmp, num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token,
|
|
|
|
|
LyXTabular::VAlignment & num)
|
|
|
|
|
{
|
|
|
|
|
string tmp;
|
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
return string2type(tmp, num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token,
|
|
|
|
|
LyXTabular::BoxType & num)
|
|
|
|
|
{
|
|
|
|
|
string tmp;
|
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
return string2type(tmp, num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token, bool & flag)
|
|
|
|
|
{
|
2002-02-22 13:25:03 +00:00
|
|
|
|
// set the flag always to false as this should be the default for bools
|
|
|
|
|
// not in the file-format.
|
|
|
|
|
flag = false;
|
2001-12-20 14:52:15 +00:00
|
|
|
|
string tmp;
|
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
return string2type(tmp, flag);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
}
|
2001-12-20 14:52:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool getTokenValue(string const & str, const char * token, LyXLength & len)
|
|
|
|
|
{
|
2002-02-22 13:25:03 +00:00
|
|
|
|
// set the lenght to be zero() as default as this it should be if not
|
|
|
|
|
// in the file format.
|
|
|
|
|
len = LyXLength();
|
2001-12-20 14:52:15 +00:00
|
|
|
|
string tmp;
|
|
|
|
|
if (!getTokenValue(str, token, tmp))
|
|
|
|
|
return false;
|
|
|
|
|
return isValidLength(tmp, &len);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
}
|
2001-12-20 14:52:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void l_getline(istream & is, string & str)
|
|
|
|
|
{
|
|
|
|
|
str.erase();
|
|
|
|
|
while (str.empty()) {
|
2001-12-26 14:45:16 +00:00
|
|
|
|
getline(is, str);
|
2001-12-20 14:52:15 +00:00
|
|
|
|
if (!str.empty() && str[str.length() - 1] == '\r')
|
|
|
|
|
str.erase(str.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|