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