]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insetcommandparams.C
1 /**
2  * \file insetcommandparams.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetcommandparams.h"
18 #include "lyxlex.h"
19 #include "debug.h"
20
21 #include "support/LOstream.h"
22
23 using std::ostream;
24 using std::endl;
25
26
27 InsetCommandParams::InsetCommandParams()
28 {}
29
30
31 InsetCommandParams::InsetCommandParams(string const & n,
32                                         string const & c,
33                                         string const & o)
34         : cmdname(n), contents(c), options(o), preview_(false)
35 {}
36
37
38 string const InsetCommandParams::getAsString() const
39 {
40         return cmdname + "|++|" + contents + "|++|" + options;
41 }
42
43
44 void InsetCommandParams::setFromString(string const & b)
45 {
46         string::size_type idx = b.find("|++|");
47         if (idx == string::npos) {
48                 cmdname = b;
49                 contents = "";
50                 options = "";
51                 return;
52         }
53
54         cmdname = b.substr(0, idx);
55         string tmp = b.substr(idx+4);
56
57         idx = tmp.find("|++|");
58         if (idx == string::npos) {
59                 contents = tmp;
60                 options = "";
61         } else {
62                 contents  = tmp.substr(0, idx);
63                 options = tmp.substr(idx+4);
64         }
65 }
66
67
68 void InsetCommandParams::scanCommand(string const & cmd)
69 {
70         string tcmdname, toptions, tcontents;
71
72         if (cmd.empty()) return;
73
74         enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
75
76         // Used to handle things like \command[foo[bar]]{foo{bar}}
77         int nestdepth = 0;
78
79         for (string::size_type i = 0; i < cmd.length(); ++i) {
80                 char c = cmd[i];
81                 if ((state == CMDNAME && c == ' ') ||
82                     (state == CMDNAME && c == '[') ||
83                     (state == CMDNAME && c == '{')) {
84                         state = WS;
85                 }
86                 if ((state == OPTION  && c == ']') ||
87                     (state == CONTENT && c == '}')) {
88                         if (nestdepth == 0) {
89                                 state = WS;
90                         } else {
91                                 --nestdepth;
92                         }
93                 }
94                 if ((state == OPTION  && c == '[') ||
95                     (state == CONTENT && c == '{')) {
96                         ++nestdepth;
97                 }
98                 switch (state) {
99                 case CMDNAME:   tcmdname += c; break;
100                 case OPTION:    toptions += c; break;
101                 case CONTENT:   tcontents += c; break;
102                 case WS:
103                         if (c == '\\') {
104                                 state = CMDNAME;
105                         } else if (c == '[') {
106                                 state = OPTION;
107                                 nestdepth = 0; // Just to be sure
108                         } else if (c == '{') {
109                                 state = CONTENT;
110                                 nestdepth = 0; // Just to be sure
111                         }
112                         break;
113                 }
114         }
115
116         // Don't mess with this.
117         if (!tcmdname.empty())  setCmdName(tcmdname);
118         if (!toptions.empty())  setOptions(toptions);
119         if (!tcontents.empty()) setContents(tcontents);
120
121         if (lyxerr.debugging(Debug::PARSER))
122                 lyxerr << "Command <" <<  cmd
123                        << "> == <" << getCommand()
124                        << "> == <" << getCmdName()
125                        << '|' << getContents()
126                        << '|' << getOptions() << '>' << endl;
127 }
128
129
130 void InsetCommandParams::read(LyXLex & lex)
131 {
132         string token;
133
134         if (lex.eatLine()) {
135                 token = lex.getString();
136                 scanCommand(token);
137         } else {
138                 lex.printError("InsetCommand: Parse error: `$$Token'");
139         }
140
141         while (lex.isOK()) {
142                 lex.next();
143                 token = lex.getString();
144                 if (token == "\\end_inset")
145                         break;
146                 if (token == "preview") {
147                         lex.next();
148                         preview_ = lex.getBool();
149                 }
150         }
151         if (token != "\\end_inset") {
152                 lex.printError("Missing \\end_inset at this point. "
153                                "Read: `$$Token'");
154         }
155 }
156
157
158 void InsetCommandParams::write(ostream & os) const
159 {
160         os << "LatexCommand " << getCommand() << "\n";
161 }
162
163
164 string const InsetCommandParams::getCommand() const
165 {
166         string s;
167         if (!getCmdName().empty()) s += "\\"+getCmdName();
168         if (!getOptions().empty()) s += "["+getOptions()+']';
169         s += "{"+getContents()+'}';
170         return s;
171 }
172
173
174 bool operator==(InsetCommandParams const & o1,
175                 InsetCommandParams const & o2)
176 {
177         return o1.getCmdName() == o2.getCmdName()
178                 && o1.getContents() == o2.getContents()
179                 && o1.getOptions() == o2.getOptions()
180                 && o1.preview() == o2.preview();
181 }
182
183
184 bool operator!=(InsetCommandParams const & o1,
185                 InsetCommandParams const & o2)
186 {
187         return !(o1 == o2);
188 }