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