]> git.lyx.org Git - lyx.git/blob - src/tabular-old.C
Use paragraph iterators in CutAndPaste::SwitchLayoutsBetweenClasses
[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 inline
129 void l_getline(istream & is, string & str)
130 {
131 #ifdef WITH_WARNINGS
132 //#warning old l_getline
133 #endif
134     getline(is, str);
135     while (str.empty())
136         getline(is, str);
137 }
138
139 } // namespace anon
140
141
142 void LyXTabular::ReadOld(Buffer const * buf, istream & is,
143                          LyXLex & lex, string const & l)
144 {
145     string line(l);
146     int rows_arg;
147     int columns_arg;
148     if (!getTokenValue(line, "rows", rows_arg))
149         return;
150     if (!getTokenValue(line, "columns", columns_arg))
151         return;
152     Init(rows_arg, columns_arg);
153     l_getline(is, line);
154     if (!prefixIs(line, "<Features ")) {
155         lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
156             line << ")" << endl;
157         return;
158     }
159     getTokenValue(line, "islongtable", is_long_tabular);
160     getTokenValue(line, "endhead", endhead.row);
161     getTokenValue(line, "endfirsthead", endfirsthead.row);
162     getTokenValue(line, "endfoot", endfoot.row);
163     getTokenValue(line, "endlastfoot", endlastfoot.row);
164
165     for (int i = 0; i < rows_; ++i) {
166         l_getline(is, line);
167         if (!prefixIs(line, "<Row ")) {
168             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
169                 line << ")" << endl;
170             return;
171         }
172         getTokenValue(line, "topline", row_info[i].top_line);
173         getTokenValue(line, "bottomline", row_info[i].bottom_line);
174         getTokenValue(line, "newpage", row_info[i].newpage);
175         for (int j = 0; j < columns_; ++j) {
176             l_getline(is,line);
177             if (!prefixIs(line,"<Column")) {
178                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
179                     line << ")" << endl;
180                 return;
181             }
182             if (!i) {
183                 getTokenValue(line, "alignment", column_info[j].alignment);
184                 getTokenValue(line, "valignment", column_info[j].valignment);
185                 getTokenValue(line, "leftline", column_info[j].left_line);
186                 getTokenValue(line, "rightline", column_info[j].right_line);
187                 getTokenValue(line, "width", column_info[j].p_width);
188                 getTokenValue(line, "special", column_info[j].align_special);
189             }
190             l_getline(is, line);
191             if (!prefixIs(line, "<Cell")) {
192                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
193                     line << ")" << endl;
194                 return;
195             }
196             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
197             getTokenValue(line, "alignment", cell_info[i][j].alignment);
198             getTokenValue(line, "valignment", cell_info[i][j].valignment);
199             getTokenValue(line, "topline", cell_info[i][j].top_line);
200             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
201             getTokenValue(line, "leftline", cell_info[i][j].left_line);
202             getTokenValue(line, "rightline", cell_info[i][j].right_line);
203             getTokenValue(line, "rotate", cell_info[i][j].rotate);
204             getTokenValue(line, "usebox", cell_info[i][j].usebox);
205             getTokenValue(line, "width", cell_info[i][j].p_width);
206             getTokenValue(line, "special", cell_info[i][j].align_special);
207             l_getline(is, line);
208             if (prefixIs(line, "\\begin_inset")) {
209                 cell_info[i][j].inset.read(buf, lex);
210                 l_getline(is, line);
211             }
212             if (line != "</Cell>") {
213                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
214                     line << ")" << endl;
215                 return;
216             }
217             l_getline(is, line);
218             if (line != "</Column>") {
219                 lyxerr << "Wrong tabular format (expected </Column> got" <<
220                     line << ")" << endl;
221                 return;
222             }
223         }
224         l_getline(is, line);
225         if (line != "</Row>") {
226             lyxerr << "Wrong tabular format (expected </Row> got" <<
227                 line << ")" << endl;
228             return;
229         }
230     }
231     while (line != "</LyXTabular>") {
232         l_getline(is, line);
233     }
234     set_row_column_number_info();
235 }