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