]> git.lyx.org Git - lyx.git/blob - src/tabular-old.C
Fix fuer #209
[lyx.git] / src / tabular-old.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 #include "tabular.h"
16 #include "debug.h"
17 #include "support/lstrings.h"
18 #include "support/textutils.h"
19
20 using std::istream;
21 using std::getline;
22 using std::endl;
23
24 #ifndef CXX_GLOBAL_CSTD
25 using std::strlen;
26 #endif
27
28 namespace {
29
30 bool getTokenValue(string const & str, char const * token, string & ret)
31 {
32     size_t token_length = strlen(token);
33     string::size_type pos = str.find(token);
34
35     if (pos == string::npos || pos + token_length + 1 >= str.length()
36         || str[pos + token_length] != '=')
37         return false;
38     ret.erase();
39     pos += token_length + 1;
40     char ch = str[pos];
41     if ((ch != '"') && (ch != '\'')) { // only read till next space
42         ret += ch;
43         ch = ' ';
44     }
45     while ((pos < str.length() - 1) && (str[++pos] != ch))
46         ret += str[pos];
47
48     return true;
49 }
50
51
52 bool getTokenValue(string const & str, char const * token, int & num)
53 {
54     string::size_type pos = str.find(token);
55     char ch = str[pos + strlen(token)];
56
57     if ((pos == string::npos) || (ch != '='))
58         return false;
59     string ret;
60     pos += strlen(token) + 1;
61     ch = str[pos];
62     if ((ch != '"') && (ch != '\'')) { // only read till next space
63         if (!IsDigit(ch))
64             return false;
65         ret += ch;
66     }
67     ++pos;
68     while ((pos < str.length() - 1) && IsDigit(str[pos]))
69         ret += str[pos++];
70
71     num = strToInt(ret);
72     return true;
73 }
74
75
76 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
77 {
78     int tmp;
79     bool const ret = getTokenValue(str, token, tmp);
80     num = static_cast<LyXAlignment>(tmp);
81     return ret;
82 }
83
84
85 bool getTokenValue(string const & str, char const * token,
86                    LyXTabular::VAlignment & num)
87 {
88     int tmp;
89     bool const ret = getTokenValue(str, token, tmp);
90     num = static_cast<LyXTabular::VAlignment>(tmp);
91     return ret;
92 }
93
94
95 bool getTokenValue(string const & str, char const * token,
96                    LyXTabular::BoxType & num)
97 {
98     int tmp;
99     bool ret = getTokenValue(str, token, tmp);
100     num = static_cast<LyXTabular::BoxType>(tmp);
101     return ret;
102 }
103
104
105 bool getTokenValue(string const & str, char const * token, bool & flag)
106 {
107     string::size_type pos = str.find(token);
108     char ch = str[pos + strlen(token)];
109
110     if ((pos == string::npos) || (ch != '='))
111         return false;
112     string ret;
113     pos += strlen(token) + 1;
114     ch = str[pos];
115     if ((ch != '"') && (ch != '\'')) { // only read till next space
116         if (!IsDigit(ch))
117             return false;
118         ret += ch;
119     }
120     ++pos;
121     while ((pos < str.length() - 1) && IsDigit(str[pos]))
122         ret += str[pos++];
123
124     flag = strToInt(ret);
125     return true;
126 }
127
128
129 bool getTokenValue(string const & str, const char * token, LyXLength & len)
130 {
131         string tmp;
132         if (!getTokenValue(str, token, tmp))
133                 return false;
134         return isValidLength(tmp, &len);
135 }    
136
137
138 inline
139 void l_getline(istream & is, string & str)
140 {
141 #ifdef WITH_WARNINGS
142 //#warning old l_getline
143 #endif
144     getline(is, str);
145     while (str.empty())
146         getline(is, str);
147 }
148
149 } // namespace anon
150
151
152 void LyXTabular::ReadOld(Buffer const * buf, istream & is,
153                          LyXLex & lex, string const & l)
154 {
155     string line(l);
156     int rows_arg;
157     int columns_arg;
158     if (!getTokenValue(line, "rows", rows_arg))
159         return;
160     if (!getTokenValue(line, "columns", columns_arg))
161         return;
162     Init(rows_arg, columns_arg);
163     l_getline(is, line);
164     if (!prefixIs(line, "<Features ")) {
165         lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
166             line << ")" << endl;
167         return;
168     }
169     getTokenValue(line, "islongtable", is_long_tabular);
170         int hrow;
171         int fhrow;
172         int frow;
173         int lfrow;
174
175         getTokenValue(line, "endhead", hrow);
176         getTokenValue(line, "endfirsthead", fhrow);
177         getTokenValue(line, "endfoot", frow);
178         getTokenValue(line, "endlastfoot", lfrow);
179         setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
180
181     for (int i = 0; i < rows_; ++i) {
182         l_getline(is, line);
183         if (!prefixIs(line, "<Row ")) {
184             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
185                 line << ")" << endl;
186             return;
187         }
188         getTokenValue(line, "topline", row_info[i].top_line);
189         getTokenValue(line, "bottomline", row_info[i].bottom_line);
190         getTokenValue(line, "newpage", row_info[i].newpage);
191         for (int j = 0; j < columns_; ++j) {
192             l_getline(is,line);
193             if (!prefixIs(line,"<Column")) {
194                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
195                     line << ")" << endl;
196                 return;
197             }
198             if (!i) {
199                 getTokenValue(line, "alignment", column_info[j].alignment);
200                 getTokenValue(line, "valignment", column_info[j].valignment);
201                 getTokenValue(line, "leftline", column_info[j].left_line);
202                 getTokenValue(line, "rightline", column_info[j].right_line);
203                 getTokenValue(line, "width", column_info[j].p_width);
204                 getTokenValue(line, "special", column_info[j].align_special);
205             }
206             l_getline(is, line);
207             if (!prefixIs(line, "<Cell")) {
208                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
209                     line << ")" << endl;
210                 return;
211             }
212             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
213             getTokenValue(line, "alignment", cell_info[i][j].alignment);
214             getTokenValue(line, "valignment", cell_info[i][j].valignment);
215             getTokenValue(line, "topline", cell_info[i][j].top_line);
216             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
217             getTokenValue(line, "leftline", cell_info[i][j].left_line);
218             getTokenValue(line, "rightline", cell_info[i][j].right_line);
219             getTokenValue(line, "rotate", cell_info[i][j].rotate);
220             getTokenValue(line, "usebox", cell_info[i][j].usebox);
221             getTokenValue(line, "width", cell_info[i][j].p_width);
222             getTokenValue(line, "special", cell_info[i][j].align_special);
223             l_getline(is, line);
224             if (prefixIs(line, "\\begin_inset")) {
225                 cell_info[i][j].inset.read(buf, lex);
226                 l_getline(is, line);
227             }
228             if (line != "</Cell>") {
229                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
230                     line << ")" << endl;
231                 return;
232             }
233             l_getline(is, line);
234             if (line != "</Column>") {
235                 lyxerr << "Wrong tabular format (expected </Column> got" <<
236                     line << ")" << endl;
237                 return;
238             }
239         }
240         l_getline(is, line);
241         if (line != "</Row>") {
242             lyxerr << "Wrong tabular format (expected </Row> got" <<
243                 line << ")" << endl;
244             return;
245         }
246     }
247     while (line != "</LyXTabular>") {
248         l_getline(is, line);
249     }
250     set_row_column_number_info();
251 }