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"
18 #include "support/lstrings.h"
20 using namespace lyx::support;
22 #ifndef CXX_GLOBAL_CSTD
29 // Perfect case for a template... (Lgb)
33 string const write_attribute(string const & name, bool const & b)
35 // we write only true attribute values so we remove a bit of the
36 // file format bloat for tabulars.
40 return write_attribute(name, tostr(b));
44 string const write_attribute(string const & name, int const & i)
46 // we write only true attribute values so we remove a bit of the
47 // file format bloat for tabulars.
51 return write_attribute(name, tostr(i));
55 string const write_attribute(string const & name, LyXLength const & value)
57 // we write only the value if we really have one same reson as above.
61 return write_attribute(name, value.asString());
65 string const tostr(LyXAlignment const & num)
74 case LYX_ALIGN_CENTER:
78 case LYX_ALIGN_LAYOUT:
80 case LYX_ALIGN_SPECIAL:
87 string const tostr(LyXTabular::VAlignment const & num)
90 case LyXTabular::LYX_VALIGN_TOP:
92 case LyXTabular::LYX_VALIGN_CENTER:
94 case LyXTabular::LYX_VALIGN_BOTTOM:
101 string const tostr(LyXTabular::BoxType const & num)
104 case LyXTabular::BOX_NONE:
106 case LyXTabular::BOX_PARBOX:
108 case LyXTabular::BOX_MINIPAGE:
114 // I would have liked a fromstr template a lot better. (Lgb)
115 bool string2type(string const str, LyXAlignment & num)
118 num = LYX_ALIGN_NONE;
119 else if (str == "block")
120 num = LYX_ALIGN_BLOCK;
121 else if (str == "left")
122 num = LYX_ALIGN_LEFT;
123 else if (str == "center")
124 num = LYX_ALIGN_CENTER;
125 else if (str == "right")
126 num = LYX_ALIGN_RIGHT;
133 bool string2type(string const str, LyXTabular::VAlignment & num)
136 num = LyXTabular::LYX_VALIGN_TOP;
137 else if (str == "center")
138 num = LyXTabular::LYX_VALIGN_CENTER;
139 else if (str == "bottom")
140 num = LyXTabular::LYX_VALIGN_BOTTOM;
147 bool string2type(string const str, LyXTabular::BoxType & num)
150 num = LyXTabular::BOX_NONE;
151 else if (str == "parbox")
152 num = LyXTabular::BOX_PARBOX;
153 else if (str == "minipage")
154 num = LyXTabular::BOX_MINIPAGE;
161 bool string2type(string const str, bool & num)
165 else if (str == "false")
173 bool getTokenValue(string const & str, const char * token, string & ret)
176 size_t token_length = strlen(token);
177 string::size_type pos = str.find(token);
179 if (pos == string::npos || pos + token_length + 1 >= str.length()
180 || str[pos + token_length] != '=')
182 pos += token_length + 1;
184 if ((ch != '"') && (ch != '\'')) { // only read till next space
188 while ((pos < str.length() - 1) && (str[++pos] != ch))
195 bool getTokenValue(string const & str, const char * token, int & num)
199 if (!getTokenValue(str, token, tmp))
206 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
209 if (!getTokenValue(str, token, tmp))
211 return string2type(tmp, num);
215 bool getTokenValue(string const & str, const char * token,
216 LyXTabular::VAlignment & num)
219 if (!getTokenValue(str, token, tmp))
221 return string2type(tmp, num);
225 bool getTokenValue(string const & str, const char * token,
226 LyXTabular::BoxType & num)
229 if (!getTokenValue(str, token, tmp))
231 return string2type(tmp, num);
235 bool getTokenValue(string const & str, const char * token, bool & flag)
237 // set the flag always to false as this should be the default for bools
238 // not in the file-format.
241 if (!getTokenValue(str, token, tmp))
243 return string2type(tmp, flag);
247 bool getTokenValue(string const & str, const char * token, LyXLength & len)
249 // set the lenght to be zero() as default as this it should be if not
250 // in the file format.
253 if (!getTokenValue(str, token, tmp))
255 return isValidLength(tmp, &len);
259 void l_getline(istream & is, string & str)
262 while (str.empty()) {
264 if (!str.empty() && str[str.length() - 1] == '\r')
265 str.erase(str.length() - 1);