]> git.lyx.org Git - lyx.git/blob - src/tabular-old.C
mathed31.diff
[lyx.git] / src / tabular-old.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000 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
19 using std::istream;
20 using std::getline;
21 using std::endl;
22
23
24 static
25 bool getTokenValue(string const & str, const char * token, string & ret)
26 {
27     size_t token_length = strlen(token);
28     string::size_type pos = str.find(token);
29
30     if (pos == string::npos || pos+token_length+1 >= str.length()
31         || str[pos+token_length] != '=')
32         return false;
33     ret.erase();
34     pos += token_length + 1;
35     char ch = str[pos];
36     if ((ch != '"') && (ch != '\'')) { // only read till next space
37         ret += ch;
38         ch = ' ';
39     }
40     while((pos < str.length() - 1) && (str[++pos] != ch))
41         ret += str[pos];
42
43     return true;
44 }
45
46
47 static
48 bool getTokenValue(string const & str, const char * token, int & num)
49 {
50     string::size_type pos = str.find(token);
51     char ch = str[pos + strlen(token)];
52
53     if ((pos == string::npos) || (ch != '='))
54         return false;
55     string ret;
56     pos += strlen(token) + 1;
57     ch = str[pos];
58     if ((ch != '"') && (ch != '\'')) { // only read till next space
59         if (!isdigit(ch))
60             return false;
61         ret += ch;
62     }
63     ++pos;
64     while((pos < str.length() - 1) && isdigit(str[pos]))
65         ret += str[pos++];
66
67     num = strToInt(ret);
68     return true;
69 }
70
71
72 static
73 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
74 {
75     int tmp;
76     bool const ret = getTokenValue(str, token, tmp);
77     num = static_cast<LyXAlignment>(tmp);
78     return ret;
79 }
80
81
82 static
83 bool getTokenValue(string const & str, const char * token,
84                    LyXTabular::VAlignment & num)
85 {
86     int tmp;
87     bool const ret = getTokenValue(str, token, tmp);
88     num = static_cast<LyXTabular::VAlignment>(tmp);
89     return ret;
90 }
91
92
93 static
94 bool getTokenValue(string const & str, const char * token,
95                    LyXTabular::BoxType & num)
96 {
97     int tmp;
98     bool ret = getTokenValue(str, token, tmp);
99     num = static_cast<LyXTabular::BoxType>(tmp);
100     return ret;
101 }
102
103
104 static
105 bool getTokenValue(string const & str, const char * 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 static inline
130 void l_getline(istream & is, string & str)
131 {
132     getline(is, str);
133     while(str.empty())
134         getline(is, str);
135 }
136
137
138 void LyXTabular::ReadOld(Buffer const * buf, istream & is,
139                          LyXLex & lex, string const & l)
140 {
141     string line(l);
142     int rows_arg;
143     int columns_arg;
144     if (!getTokenValue(line, "rows", rows_arg))
145         return;
146     if (!getTokenValue(line, "columns", columns_arg))
147         return;
148     Init(rows_arg, columns_arg);
149     l_getline(is, line);
150     if (!prefixIs(line, "<Features ")) {
151         lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
152             line << ")" << endl;
153         return;
154     }
155     getTokenValue(line, "islongtable", is_long_tabular);
156     getTokenValue(line, "endhead", endhead);
157     getTokenValue(line, "endfirsthead", endfirsthead);
158     getTokenValue(line, "endfoot", endfoot);
159     getTokenValue(line, "endlastfoot", endlastfoot);
160
161     for (int i = 0; i < rows_; ++i) {
162         l_getline(is, line);
163         if (!prefixIs(line, "<Row ")) {
164             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
165                 line << ")" << endl;
166             return;
167         }
168         getTokenValue(line, "topline", row_info[i].top_line);
169         getTokenValue(line, "bottomline", row_info[i].bottom_line);
170         getTokenValue(line, "newpage", row_info[i].newpage);
171         for (int j = 0; j < columns_; ++j) {
172             l_getline(is,line);
173             if (!prefixIs(line,"<Column")) {
174                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
175                     line << ")" << endl;
176                 return;
177             }
178             if (!i) {
179                 getTokenValue(line, "alignment", column_info[j].alignment);
180                 getTokenValue(line, "valignment", column_info[j].valignment);
181                 getTokenValue(line, "leftline", column_info[j].left_line);
182                 getTokenValue(line, "rightline", column_info[j].right_line);
183                 getTokenValue(line, "width", column_info[j].p_width);
184                 getTokenValue(line, "special", column_info[j].align_special);
185             }
186             l_getline(is, line);
187             if (!prefixIs(line, "<Cell")) {
188                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
189                     line << ")" << endl;
190                 return;
191             }
192             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
193             getTokenValue(line, "alignment", cell_info[i][j].alignment);
194             getTokenValue(line, "valignment", cell_info[i][j].valignment);
195             getTokenValue(line, "topline", cell_info[i][j].top_line);
196             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
197             getTokenValue(line, "leftline", cell_info[i][j].left_line);
198             getTokenValue(line, "rightline", cell_info[i][j].right_line);
199             getTokenValue(line, "rotate", cell_info[i][j].rotate);
200             getTokenValue(line, "usebox", cell_info[i][j].usebox);
201             getTokenValue(line, "width", cell_info[i][j].p_width);
202             getTokenValue(line, "special", cell_info[i][j].align_special);
203             l_getline(is, line);
204             if (prefixIs(line, "\\begin_inset")) {
205                 cell_info[i][j].inset.Read(buf, lex);
206                 l_getline(is, line);
207             }
208             if (line != "</Cell>") {
209                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
210                     line << ")" << endl;
211                 return;
212             }
213             l_getline(is, line);
214             if (line != "</Column>") {
215                 lyxerr << "Wrong tabular format (expected </Column> got" <<
216                     line << ")" << endl;
217                 return;
218             }
219         }
220         l_getline(is, line);
221         if (line != "</Row>") {
222             lyxerr << "Wrong tabular format (expected </Row> got" <<
223                 line << ")" << endl;
224             return;
225         }
226     }
227     while (line != "</LyXTabular>") {
228         l_getline(is, line);
229     }
230     set_row_column_number_info();
231 }