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