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"
20 #include "support/lstrings.h"
23 // Perfect case for a template... (Lgb)
26 string const write_attribute(string const & name, T const & t)
28 string str = " " + name + "=\"" + tostr(t) + "\"";
33 string const write_attribute(string const & name, bool const & b)
35 return write_attribute(name, int(b));
39 string const write_attribute(string const & name, LyXLength const & value)
41 return write_attribute(name, value.asString());
44 string const tostr(LyXAlignment const & num)
53 case LYX_ALIGN_CENTER:
57 case LYX_ALIGN_LAYOUT:
59 case LYX_ALIGN_SPECIAL:
66 string const tostr(LyXTabular::VAlignment const & num)
69 case LyXTabular::LYX_VALIGN_TOP:
71 case LyXTabular::LYX_VALIGN_CENTER:
73 case LyXTabular::LYX_VALIGN_BOTTOM:
80 string const tostr(LyXTabular::BoxType const & num)
83 case LyXTabular::BOX_NONE:
85 case LyXTabular::BOX_PARBOX:
87 case LyXTabular::BOX_MINIPAGE:
93 // I would have liked a fromstr template a lot better. (Lgb)
94 bool string2type(string const str, LyXAlignment & num)
98 else if (str == "block")
99 num = LYX_ALIGN_BLOCK;
100 else if (str == "left")
101 num = LYX_ALIGN_LEFT;
102 else if (str == "center")
103 num = LYX_ALIGN_CENTER;
104 else if (str == "right")
105 num = LYX_ALIGN_RIGHT;
112 bool string2type(string const str, LyXTabular::VAlignment & num)
115 num = LyXTabular::LYX_VALIGN_TOP;
116 else if (str == "center")
117 num = LyXTabular::LYX_VALIGN_CENTER;
118 else if (str == "bottom")
119 num = LyXTabular::LYX_VALIGN_BOTTOM;
126 bool string2type(string const str, LyXTabular::BoxType & num)
129 num = LyXTabular::BOX_NONE;
130 else if (str == "parbox")
131 num = LyXTabular::BOX_PARBOX;
132 else if (str == "minipage")
133 num = LyXTabular::BOX_MINIPAGE;
140 bool string2type(string const str, bool & num)
144 else if (str == "false")
152 bool getTokenValue(string const & str, const char * token, string & ret)
154 size_t token_length = strlen(token);
155 string::size_type pos = str.find(token);
157 if (pos == string::npos || pos + token_length + 1 >= str.length()
158 || str[pos + token_length] != '=')
161 pos += token_length + 1;
163 if ((ch != '"') && (ch != '\'')) { // only read till next space
167 while ((pos < str.length() - 1) && (str[++pos] != ch))
174 bool getTokenValue(string const & str, const char * token, int & num)
177 if (!getTokenValue(str, token, tmp))
184 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
187 if (!getTokenValue(str, token, tmp))
189 return string2type(tmp, num);
193 bool getTokenValue(string const & str, const char * token,
194 LyXTabular::VAlignment & num)
197 if (!getTokenValue(str, token, tmp))
199 return string2type(tmp, num);
203 bool getTokenValue(string const & str, const char * token,
204 LyXTabular::BoxType & num)
207 if (!getTokenValue(str, token, tmp))
209 return string2type(tmp, num);
213 bool getTokenValue(string const & str, const char * token, bool & flag)
216 if (!getTokenValue(str, token, tmp))
218 return string2type(tmp, flag);
222 bool getTokenValue(string const & str, const char * token, LyXLength & len)
225 if (!getTokenValue(str, token, tmp))
227 return isValidLength(tmp, &len);
231 void l_getline(istream & is, string & str)
234 while (str.empty()) {
236 if (!str.empty() && str[str.length() - 1] == '\r')
237 str.erase(str.length() - 1);