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