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