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 #ifndef CXX_GLOBAL_CSTD
27 // Perfect case for a template... (Lgb)
31 string const write_attribute(string const & name, bool const & b)
33 // we write only true attribute values so we remove a bit of the
34 // file format bloat for tabulars.
38 return write_attribute(name, tostr(b));
42 string const write_attribute(string const & name, int const & i)
44 // we write only true attribute values so we remove a bit of the
45 // file format bloat for tabulars.
49 return write_attribute(name, tostr(i));
53 string const write_attribute(string const & name, LyXLength const & value)
55 // we write only the value if we really have one same reson as above.
59 return write_attribute(name, value.asString());
63 string const tostr(LyXAlignment const & num)
72 case LYX_ALIGN_CENTER:
76 case LYX_ALIGN_LAYOUT:
78 case LYX_ALIGN_SPECIAL:
85 string const tostr(LyXTabular::VAlignment const & num)
88 case LyXTabular::LYX_VALIGN_TOP:
90 case LyXTabular::LYX_VALIGN_CENTER:
92 case LyXTabular::LYX_VALIGN_BOTTOM:
99 string const tostr(LyXTabular::BoxType const & num)
102 case LyXTabular::BOX_NONE:
104 case LyXTabular::BOX_PARBOX:
106 case LyXTabular::BOX_MINIPAGE:
112 // I would have liked a fromstr template a lot better. (Lgb)
113 bool string2type(string const str, LyXAlignment & num)
116 num = LYX_ALIGN_NONE;
117 else if (str == "block")
118 num = LYX_ALIGN_BLOCK;
119 else if (str == "left")
120 num = LYX_ALIGN_LEFT;
121 else if (str == "center")
122 num = LYX_ALIGN_CENTER;
123 else if (str == "right")
124 num = LYX_ALIGN_RIGHT;
131 bool string2type(string const str, LyXTabular::VAlignment & num)
134 num = LyXTabular::LYX_VALIGN_TOP;
135 else if (str == "center")
136 num = LyXTabular::LYX_VALIGN_CENTER;
137 else if (str == "bottom")
138 num = LyXTabular::LYX_VALIGN_BOTTOM;
145 bool string2type(string const str, LyXTabular::BoxType & num)
148 num = LyXTabular::BOX_NONE;
149 else if (str == "parbox")
150 num = LyXTabular::BOX_PARBOX;
151 else if (str == "minipage")
152 num = LyXTabular::BOX_MINIPAGE;
159 bool string2type(string const str, bool & num)
163 else if (str == "false")
171 bool getTokenValue(string const & str, const char * token, string & ret)
174 size_t token_length = strlen(token);
175 string::size_type pos = str.find(token);
177 if (pos == string::npos || pos + token_length + 1 >= str.length()
178 || str[pos + token_length] != '=')
180 pos += token_length + 1;
182 if ((ch != '"') && (ch != '\'')) { // only read till next space
186 while ((pos < str.length() - 1) && (str[++pos] != ch))
193 bool getTokenValue(string const & str, const char * token, int & num)
197 if (!getTokenValue(str, token, tmp))
204 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
207 if (!getTokenValue(str, token, tmp))
209 return string2type(tmp, num);
213 bool getTokenValue(string const & str, const char * token,
214 LyXTabular::VAlignment & num)
217 if (!getTokenValue(str, token, tmp))
219 return string2type(tmp, num);
223 bool getTokenValue(string const & str, const char * token,
224 LyXTabular::BoxType & num)
227 if (!getTokenValue(str, token, tmp))
229 return string2type(tmp, num);
233 bool getTokenValue(string const & str, const char * token, bool & flag)
235 // set the flag always to false as this should be the default for bools
236 // not in the file-format.
239 if (!getTokenValue(str, token, tmp))
241 return string2type(tmp, flag);
245 bool getTokenValue(string const & str, const char * token, LyXLength & len)
247 // set the lenght to be zero() as default as this it should be if not
248 // in the file format.
251 if (!getTokenValue(str, token, tmp))
253 return isValidLength(tmp, &len);
257 void l_getline(istream & is, string & str)
260 while (str.empty()) {
262 if (!str.empty() && str[str.length() - 1] == '\r')
263 str.erase(str.length() - 1);