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