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 * ======================================================
16 #pragma implementation
19 #include "tabular_funcs.h"
21 #include "support/lstrings.h"
22 #include "support/LIstream.h"
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, int(b));
42 string const write_attribute(string const & name, LyXLength const & value)
44 // we write only the value if we really have one same reson as above.
48 return write_attribute(name, value.asString());
51 string const tostr(LyXAlignment const & num)
60 case LYX_ALIGN_CENTER:
64 case LYX_ALIGN_LAYOUT:
66 case LYX_ALIGN_SPECIAL:
73 string const tostr(LyXTabular::VAlignment const & num)
76 case LyXTabular::LYX_VALIGN_TOP:
78 case LyXTabular::LYX_VALIGN_CENTER:
80 case LyXTabular::LYX_VALIGN_BOTTOM:
87 string const tostr(LyXTabular::BoxType const & num)
90 case LyXTabular::BOX_NONE:
92 case LyXTabular::BOX_PARBOX:
94 case LyXTabular::BOX_MINIPAGE:
100 // I would have liked a fromstr template a lot better. (Lgb)
101 bool string2type(string const str, LyXAlignment & num)
104 num = LYX_ALIGN_NONE;
105 else if (str == "block")
106 num = LYX_ALIGN_BLOCK;
107 else if (str == "left")
108 num = LYX_ALIGN_LEFT;
109 else if (str == "center")
110 num = LYX_ALIGN_CENTER;
111 else if (str == "right")
112 num = LYX_ALIGN_RIGHT;
119 bool string2type(string const str, LyXTabular::VAlignment & num)
122 num = LyXTabular::LYX_VALIGN_TOP;
123 else if (str == "center")
124 num = LyXTabular::LYX_VALIGN_CENTER;
125 else if (str == "bottom")
126 num = LyXTabular::LYX_VALIGN_BOTTOM;
133 bool string2type(string const str, LyXTabular::BoxType & num)
136 num = LyXTabular::BOX_NONE;
137 else if (str == "parbox")
138 num = LyXTabular::BOX_PARBOX;
139 else if (str == "minipage")
140 num = LyXTabular::BOX_MINIPAGE;
147 bool string2type(string const str, bool & num)
151 else if (str == "false")
159 bool getTokenValue(string const & str, const char * token, string & ret)
161 size_t token_length = strlen(token);
162 string::size_type pos = str.find(token);
164 if (pos == string::npos || pos + token_length + 1 >= str.length()
165 || str[pos + token_length] != '=')
168 pos += token_length + 1;
170 if ((ch != '"') && (ch != '\'')) { // only read till next space
174 while ((pos < str.length() - 1) && (str[++pos] != ch))
181 bool getTokenValue(string const & str, const char * token, int & num)
184 if (!getTokenValue(str, token, tmp))
191 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
194 if (!getTokenValue(str, token, tmp))
196 return string2type(tmp, num);
200 bool getTokenValue(string const & str, const char * token,
201 LyXTabular::VAlignment & num)
204 if (!getTokenValue(str, token, tmp))
206 return string2type(tmp, num);
210 bool getTokenValue(string const & str, const char * token,
211 LyXTabular::BoxType & num)
214 if (!getTokenValue(str, token, tmp))
216 return string2type(tmp, num);
220 bool getTokenValue(string const & str, const char * token, bool & flag)
222 // set the flag always to false as this should be the default for bools
223 // not in the file-format.
226 if (!getTokenValue(str, token, tmp))
228 return string2type(tmp, flag);
232 bool getTokenValue(string const & str, const char * token, LyXLength & len)
234 // set the lenght to be zero() as default as this it should be if not
235 // in the file format.
238 if (!getTokenValue(str, token, tmp))
240 return isValidLength(tmp, &len);
244 void l_getline(istream & is, string & str)
247 while (str.empty()) {
249 if (!str.empty() && str[str.length() - 1] == '\r')
250 str.erase(str.length() - 1);