1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
15 #include "tabular_funcs.h"
17 #include "support/LIstream.h"
19 #ifndef CXX_GLOBAL_CSTD
26 // Perfect case for a template... (Lgb)
30 string const write_attribute(string const & name, bool const & b)
32 // we write only true attribute values so we remove a bit of the
33 // file format bloat for tabulars.
37 return write_attribute(name, tostr(b));
41 string const write_attribute(string const & name, int const & i)
43 // we write only true attribute values so we remove a bit of the
44 // file format bloat for tabulars.
48 return write_attribute(name, tostr(i));
52 string const write_attribute(string const & name, LyXLength const & value)
54 // we write only the value if we really have one same reson as above.
58 return write_attribute(name, value.asString());
61 string const tostr(LyXAlignment const & num)
70 case LYX_ALIGN_CENTER:
74 case LYX_ALIGN_LAYOUT:
76 case LYX_ALIGN_SPECIAL:
83 string const tostr(LyXTabular::VAlignment const & num)
86 case LyXTabular::LYX_VALIGN_TOP:
88 case LyXTabular::LYX_VALIGN_CENTER:
90 case LyXTabular::LYX_VALIGN_BOTTOM:
97 string const tostr(LyXTabular::BoxType const & num)
100 case LyXTabular::BOX_NONE:
102 case LyXTabular::BOX_PARBOX:
104 case LyXTabular::BOX_MINIPAGE:
110 // I would have liked a fromstr template a lot better. (Lgb)
111 bool string2type(string const str, LyXAlignment & num)
114 num = LYX_ALIGN_NONE;
115 else if (str == "block")
116 num = LYX_ALIGN_BLOCK;
117 else if (str == "left")
118 num = LYX_ALIGN_LEFT;
119 else if (str == "center")
120 num = LYX_ALIGN_CENTER;
121 else if (str == "right")
122 num = LYX_ALIGN_RIGHT;
129 bool string2type(string const str, LyXTabular::VAlignment & num)
132 num = LyXTabular::LYX_VALIGN_TOP;
133 else if (str == "center")
134 num = LyXTabular::LYX_VALIGN_CENTER;
135 else if (str == "bottom")
136 num = LyXTabular::LYX_VALIGN_BOTTOM;
143 bool string2type(string const str, LyXTabular::BoxType & num)
146 num = LyXTabular::BOX_NONE;
147 else if (str == "parbox")
148 num = LyXTabular::BOX_PARBOX;
149 else if (str == "minipage")
150 num = LyXTabular::BOX_MINIPAGE;
157 bool string2type(string const str, bool & num)
161 else if (str == "false")
169 bool getTokenValue(string const & str, const char * token, string & ret)
172 size_t token_length = strlen(token);
173 string::size_type pos = str.find(token);
175 if (pos == string::npos || pos + token_length + 1 >= str.length()
176 || str[pos + token_length] != '=')
178 pos += token_length + 1;
180 if ((ch != '"') && (ch != '\'')) { // only read till next space
184 while ((pos < str.length() - 1) && (str[++pos] != ch))
191 bool getTokenValue(string const & str, const char * token, int & num)
195 if (!getTokenValue(str, token, tmp))
202 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
205 if (!getTokenValue(str, token, tmp))
207 return string2type(tmp, num);
211 bool getTokenValue(string const & str, const char * token,
212 LyXTabular::VAlignment & num)
215 if (!getTokenValue(str, token, tmp))
217 return string2type(tmp, num);
221 bool getTokenValue(string const & str, const char * token,
222 LyXTabular::BoxType & num)
225 if (!getTokenValue(str, token, tmp))
227 return string2type(tmp, num);
231 bool getTokenValue(string const & str, const char * token, bool & flag)
233 // set the flag always to false as this should be the default for bools
234 // not in the file-format.
237 if (!getTokenValue(str, token, tmp))
239 return string2type(tmp, flag);
243 bool getTokenValue(string const & str, const char * token, LyXLength & len)
245 // set the lenght to be zero() as default as this it should be if not
246 // in the file format.
249 if (!getTokenValue(str, token, tmp))
251 return isValidLength(tmp, &len);
255 void l_getline(istream & is, string & str)
258 while (str.empty()) {
260 if (!str.empty() && str[str.length() - 1] == '\r')
261 str.erase(str.length() - 1);