]> git.lyx.org Git - lyx.git/blob - src/tabular_funcs.C
couple more fixes
[lyx.git] / src / tabular_funcs.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ======================================================
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "tabular_funcs.h"
20
21 #include "support/LIstream.h"
22
23 #ifndef CXX_GLOBAL_CSTD
24 using std::strlen;
25 #endif
26
27 using std::istream;
28 using std::getline;
29
30 // Perfect case for a template... (Lgb)
31 // or perhaps not...
32
33 template <>
34 string const write_attribute(string const & name, bool const & b)
35 {
36         // we write only true attribute values so we remove a bit of the
37         // file format bloat for tabulars.
38         if (!b)
39                 return string();
40
41         return write_attribute(name, tostr(b));
42 }
43
44 template <>
45 string const write_attribute(string const & name, int const & i)
46 {
47         // we write only true attribute values so we remove a bit of the
48         // file format bloat for tabulars.
49         if (!i)
50                 return string();
51
52         return write_attribute(name, tostr(i));
53 }
54
55 template <>
56 string const write_attribute(string const & name, LyXLength const & value)
57 {
58         // we write only the value if we really have one same reson as above.
59         if (value.zero())
60                 return string();
61
62         return write_attribute(name, value.asString());
63 }
64
65 string const tostr(LyXAlignment const & num)
66 {
67         switch (num) {
68         case LYX_ALIGN_NONE:
69                 return "none";
70         case LYX_ALIGN_BLOCK:
71                 return "block";
72         case LYX_ALIGN_LEFT:
73                 return "left";
74         case LYX_ALIGN_CENTER:
75                 return "center";
76         case LYX_ALIGN_RIGHT:
77                 return "right";
78         case LYX_ALIGN_LAYOUT:
79                 return "layout";
80         case LYX_ALIGN_SPECIAL:
81                 return "special";
82         }
83         return string();
84 }
85
86
87 string const tostr(LyXTabular::VAlignment const & num)
88 {
89         switch (num) {
90         case LyXTabular::LYX_VALIGN_TOP:
91                 return "top";
92         case LyXTabular::LYX_VALIGN_CENTER:
93                 return "center";
94         case LyXTabular::LYX_VALIGN_BOTTOM:
95                 return "bottom";
96         }
97         return string();
98 }
99
100
101 string const tostr(LyXTabular::BoxType const & num)
102 {
103         switch (num) {
104         case LyXTabular::BOX_NONE:
105                 return "none";
106         case LyXTabular::BOX_PARBOX:
107                 return "parbox";
108         case LyXTabular::BOX_MINIPAGE:
109                 return "minipage";
110         }
111         return string();
112 }
113
114 // I would have liked a fromstr template a lot better. (Lgb)
115 bool string2type(string const str, LyXAlignment & num)
116 {
117         if (str == "none")
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;
127         else
128                 return false;
129         return true;
130 }
131
132
133 bool string2type(string const str, LyXTabular::VAlignment & num)
134 {
135         if (str == "top")
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;
141         else
142                 return false;
143         return true;
144 }
145
146
147 bool string2type(string const str, LyXTabular::BoxType & num)
148 {
149         if (str == "none")
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;
155         else
156                 return false;
157         return true;
158 }
159
160
161 bool string2type(string const str, bool & num)
162 {
163         if (str == "true")
164                 num = true;
165         else if (str == "false")
166                 num = false;
167         else
168                 return false;
169         return true;
170 }
171
172
173 bool getTokenValue(string const & str, const char * token, string & ret)
174 {
175         ret.erase();
176         size_t token_length = strlen(token);
177         string::size_type pos = str.find(token);
178
179         if (pos == string::npos || pos + token_length + 1 >= str.length()
180                 || str[pos + token_length] != '=')
181                 return false;
182         pos += token_length + 1;
183         char ch = str[pos];
184         if ((ch != '"') && (ch != '\'')) { // only read till next space
185                 ret += ch;
186                 ch = ' ';
187         }
188         while ((pos < str.length() - 1) && (str[++pos] != ch))
189                 ret += str[pos];
190
191         return true;
192 }
193
194
195 bool getTokenValue(string const & str, const char * token, int & num)
196 {
197         string tmp;
198         num = 0;
199         if (!getTokenValue(str, token, tmp))
200                 return false;
201         num = strToInt(tmp);
202         return true;
203 }
204
205
206 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
207 {
208         string tmp;
209         if (!getTokenValue(str, token, tmp))
210                 return false;
211         return string2type(tmp, num);
212 }
213
214
215 bool getTokenValue(string const & str, const char * token,
216                                    LyXTabular::VAlignment & num)
217 {
218         string tmp;
219         if (!getTokenValue(str, token, tmp))
220                 return false;
221         return string2type(tmp, num);
222 }
223
224
225 bool getTokenValue(string const & str, const char * token,
226                                    LyXTabular::BoxType & num)
227 {
228         string tmp;
229         if (!getTokenValue(str, token, tmp))
230                 return false;
231         return string2type(tmp, num);
232 }
233
234
235 bool getTokenValue(string const & str, const char * token, bool & flag)
236 {
237         // set the flag always to false as this should be the default for bools
238         // not in the file-format.
239         flag = false;
240         string tmp;
241         if (!getTokenValue(str, token, tmp))
242                 return false;
243         return string2type(tmp, flag);
244 }
245
246
247 bool getTokenValue(string const & str, const char * token, LyXLength & len)
248 {
249         // set the lenght to be zero() as default as this it should be if not
250         // in the file format.
251         len = LyXLength();
252         string tmp;
253         if (!getTokenValue(str, token, tmp))
254                 return false;
255         return isValidLength(tmp, &len);
256 }
257
258
259 void l_getline(istream & is, string & str)
260 {
261         str.erase();
262         while (str.empty()) {
263                 getline(is, str);
264                 if (!str.empty() && str[str.length() - 1] == '\r')
265                         str.erase(str.length() - 1);
266         }
267 }