]> git.lyx.org Git - lyx.git/blob - src/tabular_funcs.C
fix typo that put too many include paths for most people
[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/lstrings.h"
22 #include "support/LIstream.h"
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 string const tostr(LyXAlignment const & num)
63 {
64         switch (num) {
65         case LYX_ALIGN_NONE:
66                 return "none";
67         case LYX_ALIGN_BLOCK:
68                 return "block";
69         case LYX_ALIGN_LEFT:
70                 return "left";
71         case LYX_ALIGN_CENTER:
72                 return "center";
73         case LYX_ALIGN_RIGHT:
74                 return "right";
75         case LYX_ALIGN_LAYOUT:
76                 return "layout";
77         case LYX_ALIGN_SPECIAL:
78                 return "special";
79         }
80         return string();
81 }
82
83
84 string const tostr(LyXTabular::VAlignment const & num)
85 {
86         switch (num) {
87         case LyXTabular::LYX_VALIGN_TOP:
88                 return "top";
89         case LyXTabular::LYX_VALIGN_CENTER:
90                 return "center";
91         case LyXTabular::LYX_VALIGN_BOTTOM:
92                 return "bottom";
93         }
94         return string();
95 }
96
97
98 string const tostr(LyXTabular::BoxType const & num)
99 {
100         switch (num) {
101         case LyXTabular::BOX_NONE:
102                 return "none";
103         case LyXTabular::BOX_PARBOX:
104                 return "parbox";
105         case LyXTabular::BOX_MINIPAGE:
106                 return "minipage";
107         }
108         return string();
109 }
110
111 // I would have liked a fromstr template a lot better. (Lgb)
112 bool string2type(string const str, LyXAlignment & num)
113 {
114         if (str == "none")
115                 num = LYX_ALIGN_NONE;
116         else if (str == "block")
117                 num = LYX_ALIGN_BLOCK;
118         else if (str == "left")
119                 num = LYX_ALIGN_LEFT;
120         else if (str == "center")
121                 num = LYX_ALIGN_CENTER;
122         else if (str == "right")
123                 num = LYX_ALIGN_RIGHT;
124         else
125                 return false;
126         return true;
127 }
128
129
130 bool string2type(string const str, LyXTabular::VAlignment & num)
131 {
132         if (str == "top")
133                 num = LyXTabular::LYX_VALIGN_TOP;
134         else if (str == "center")
135                 num = LyXTabular::LYX_VALIGN_CENTER;
136         else if (str == "bottom")
137                 num = LyXTabular::LYX_VALIGN_BOTTOM;
138         else
139                 return false;
140         return true;
141 }
142
143
144 bool string2type(string const str, LyXTabular::BoxType & num)
145 {
146         if (str == "none")
147                 num = LyXTabular::BOX_NONE;
148         else if (str == "parbox")
149                 num = LyXTabular::BOX_PARBOX;
150         else if (str == "minipage")
151                 num = LyXTabular::BOX_MINIPAGE;
152         else
153                 return false;
154         return true;
155 }
156
157
158 bool string2type(string const str, bool & num)
159 {
160         if (str == "true")
161                 num = true;
162         else if (str == "false")
163                 num = false;
164         else
165                 return false;
166         return true;
167 }
168
169
170 bool getTokenValue(string const & str, const char * token, string & ret)
171 {
172         ret.erase();
173         size_t token_length = strlen(token);
174         string::size_type pos = str.find(token);
175
176         if (pos == string::npos || pos + token_length + 1 >= str.length()
177                 || str[pos + token_length] != '=')
178                 return false;
179         pos += token_length + 1;
180         char ch = str[pos];
181         if ((ch != '"') && (ch != '\'')) { // only read till next space
182                 ret += ch;
183                 ch = ' ';
184         }
185         while ((pos < str.length() - 1) && (str[++pos] != ch))
186                 ret += str[pos];
187
188         return true;
189 }
190
191
192 bool getTokenValue(string const & str, const char * token, int & num)
193 {
194         string tmp;
195         num = 0;
196         if (!getTokenValue(str, token, tmp))
197                 return false;
198         num = strToInt(tmp);
199         return true;
200 }
201
202
203 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
204 {
205         string tmp;
206         if (!getTokenValue(str, token, tmp))
207                 return false;
208         return string2type(tmp, num);
209 }
210
211
212 bool getTokenValue(string const & str, const char * token,
213                                    LyXTabular::VAlignment & num)
214 {
215         string tmp;
216         if (!getTokenValue(str, token, tmp))
217                 return false;
218         return string2type(tmp, num);
219 }
220
221
222 bool getTokenValue(string const & str, const char * token,
223                                    LyXTabular::BoxType & num)
224 {
225         string tmp;
226         if (!getTokenValue(str, token, tmp))
227                 return false;
228         return string2type(tmp, num);
229 }
230
231
232 bool getTokenValue(string const & str, const char * token, bool & flag)
233 {
234         // set the flag always to false as this should be the default for bools
235         // not in the file-format.
236         flag = false;
237         string tmp;
238         if (!getTokenValue(str, token, tmp))
239                 return false;
240         return string2type(tmp, flag);
241 }
242
243
244 bool getTokenValue(string const & str, const char * token, LyXLength & len)
245 {
246         // set the lenght to be zero() as default as this it should be if not
247         // in the file format.
248         len = LyXLength();
249         string tmp;
250         if (!getTokenValue(str, token, tmp))
251                 return false;
252         return isValidLength(tmp, &len);
253 }
254
255
256 void l_getline(istream & is, string & str)
257 {
258         str.erase();
259         while (str.empty()) {
260                 getline(is, str);
261                 if (!str.empty() && str[str.length() - 1] == '\r')
262                         str.erase(str.length() - 1);
263         }
264 }