]> git.lyx.org Git - lyx.git/blob - src/tabular-old.C
More fixes to the autocollapsing of paragraphs.
[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     getTokenValue(line, "endhead", endhead.row);
170     getTokenValue(line, "endfirsthead", endfirsthead.row);
171     getTokenValue(line, "endfoot", endfoot.row);
172     getTokenValue(line, "endlastfoot", endlastfoot.row);
173
174     for (int i = 0; i < rows_; ++i) {
175         l_getline(is, line);
176         if (!prefixIs(line, "<Row ")) {
177             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
178                 line << ")" << endl;
179             return;
180         }
181         getTokenValue(line, "topline", row_info[i].top_line);
182         getTokenValue(line, "bottomline", row_info[i].bottom_line);
183         getTokenValue(line, "newpage", row_info[i].newpage);
184         for (int j = 0; j < columns_; ++j) {
185             l_getline(is,line);
186             if (!prefixIs(line,"<Column")) {
187                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
188                     line << ")" << endl;
189                 return;
190             }
191             if (!i) {
192                 getTokenValue(line, "alignment", column_info[j].alignment);
193                 getTokenValue(line, "valignment", column_info[j].valignment);
194                 getTokenValue(line, "leftline", column_info[j].left_line);
195                 getTokenValue(line, "rightline", column_info[j].right_line);
196                 getTokenValue(line, "width", column_info[j].p_width);
197                 getTokenValue(line, "special", column_info[j].align_special);
198             }
199             l_getline(is, line);
200             if (!prefixIs(line, "<Cell")) {
201                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
202                     line << ")" << endl;
203                 return;
204             }
205             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
206             getTokenValue(line, "alignment", cell_info[i][j].alignment);
207             getTokenValue(line, "valignment", cell_info[i][j].valignment);
208             getTokenValue(line, "topline", cell_info[i][j].top_line);
209             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
210             getTokenValue(line, "leftline", cell_info[i][j].left_line);
211             getTokenValue(line, "rightline", cell_info[i][j].right_line);
212             getTokenValue(line, "rotate", cell_info[i][j].rotate);
213             getTokenValue(line, "usebox", cell_info[i][j].usebox);
214             getTokenValue(line, "width", cell_info[i][j].p_width);
215             getTokenValue(line, "special", cell_info[i][j].align_special);
216             l_getline(is, line);
217             if (prefixIs(line, "\\begin_inset")) {
218                 cell_info[i][j].inset.read(buf, lex);
219                 l_getline(is, line);
220             }
221             if (line != "</Cell>") {
222                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
223                     line << ")" << endl;
224                 return;
225             }
226             l_getline(is, line);
227             if (line != "</Column>") {
228                 lyxerr << "Wrong tabular format (expected </Column> got" <<
229                     line << ")" << endl;
230                 return;
231             }
232         }
233         l_getline(is, line);
234         if (line != "</Row>") {
235             lyxerr << "Wrong tabular format (expected </Row> got" <<
236                 line << ")" << endl;
237             return;
238         }
239     }
240     while (line != "</LyXTabular>") {
241         l_getline(is, line);
242     }
243     set_row_column_number_info();
244 }