]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
Fix a bunch of small performance issues spotted by cppcheck
[lyx.git] / src / sgml.cpp
1 /**
2  * \file sgml.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "sgml.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "Counters.h"
19 #include "Layout.h"
20 #include "OutputParams.h"
21 #include "Paragraph.h"
22 #include "Text.h"
23 #include "TextClass.h"
24
25 #include "support/convert.h"
26 #include "support/docstream.h"
27 #include "support/lstrings.h"
28 #include "support/textutils.h"
29
30 #include <map>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37
38 docstring sgml::escapeChar(char_type c)
39 {
40         docstring str;
41         switch (c) {
42         case ' ':
43                 str += " ";
44                 break;
45         case '&':
46                 str += "&amp;";
47                 break;
48         case '<':
49                 str += "&lt;";
50                 break;
51         case '>':
52                 str += "&gt;";
53                 break;
54 #if 0
55         case '$':
56                 str += "&dollar;";
57                 break;
58         case '#':
59                 str += "&num;";
60                 break;
61         case '%':
62                 str += "&percnt;";
63                 break;
64         case '[':
65                 str += "&lsqb;";
66                 break;
67         case ']':
68                 str += "&rsqb;";
69                 break;
70         case '{':
71                 str += "&lcub;";
72                 break;
73         case '}':
74                 str += "&rcub;";
75                 break;
76         case '~':
77                 str += "&tilde;";
78                 break;
79         case '"':
80                 str += "&quot;";
81                 break;
82         case '\\':
83                 str += "&bsol;";
84                 break;
85 #endif
86         default:
87                 str += c;
88                 break;
89         }
90         return str;
91 }
92
93
94 docstring sgml::escapeString(docstring const & raw)
95 {
96         docstring bin;
97         bin.reserve(raw.size() * 2); // crude approximation is sufficient
98         for (size_t i = 0; i != raw.size(); ++i)
99                 bin += sgml::escapeChar(raw[i]);
100
101         return bin;
102 }
103
104
105 docstring const sgml::uniqueID(docstring const & label)
106 {
107         // FIXME THREAD
108         // It seems unlikely there could be a problem here,
109         // but we could have concurrent access, in principle.
110         static unsigned int seed = 1000;
111         return label + convert<docstring>(++seed);
112 }
113
114
115 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
116         docstring const & orig)
117 {
118         // The standard DocBook SGML declaration only allows letters,
119         // digits, '-' and '.' in a name.
120         // Since users might change that declaration one has to cater
121         // for additional allowed characters.
122         // This routine replaces illegal characters by '-' or '.'
123         // and adds a number for uniqueness.
124         // If you know what you are doing, you can set allowed==""
125         // to disable this mangling.
126         DocumentClass const & tclass = buf.params().documentClass();
127         docstring const allowed = from_ascii(
128                 runparams.flavor == OutputParams::XML ? ".-_:" : tclass.options());
129
130         if (allowed.empty())
131                 return orig;
132
133         docstring::const_iterator it  = orig.begin();
134         docstring::const_iterator end = orig.end();
135
136         docstring content;
137
138         // FIXME THREAD
139         typedef map<docstring, docstring> MangledMap;
140         static MangledMap mangledNames;
141         static int mangleID = 1;
142
143         MangledMap::const_iterator const known = mangledNames.find(orig);
144         if (known != mangledNames.end())
145                 return known->second;
146
147         // make sure it starts with a letter
148         if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
149                 content += "x";
150
151         bool mangle = false;
152         for (; it != end; ++it) {
153                 char_type c = *it;
154                 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
155                       || allowed.find(c) < allowed.size())
156                         content += c;
157                 else if (c == '_' || c == ' ') {
158                         mangle = true;
159                         content += "-";
160                 }
161                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
162                         mangle = true;
163                         content += ".";
164                 }
165                 else {
166                         mangle = true;
167                 }
168         }
169
170         if (mangle)
171                 content += "-" + convert<docstring>(mangleID++);
172         else if (isDigitASCII(content[content.size() - 1]))
173                 content += ".";
174
175         mangledNames[orig] = content;
176
177         return content;
178 }
179
180
181 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
182 {
183         // FIXME UNICODE
184         // This should be fixed in layout files later.
185         string param = subst(attribute, "<", "\"");
186         param = subst(param, ">", "\"");
187
188         // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
189         // if the name is *dummy*.
190         // We ignore dummy because dummy is not a valid docbook element and it is
191         // the internal name given to single paragraphs in the latex output.
192         // This allow us to simplify the code a lot and is a reasonable compromise.
193         if (!name.empty() && name != "!-- --" && name != "dummy") {
194                 os << '<' << from_ascii(name);
195                 if (!param.empty())
196                         os << ' ' << from_ascii(param);
197                 os << '>';
198         }
199 }
200
201
202 void sgml::closeTag(odocstream & os, string const & name)
203 {
204         if (!name.empty() && name != "!-- --" && name != "dummy")
205                 os << "</" << from_ascii(name) << '>';
206 }
207
208
209 void sgml::openTag(Buffer const & buf, odocstream & os,
210         OutputParams const & runparams, Paragraph const & par)
211 {
212         Layout const & style = par.layout();
213         string const & name = style.latexname();
214         string param = style.latexparam();
215         Counters & counters = buf.params().documentClass().counters();
216
217         string id = par.getID(buf, runparams);
218
219         string attribute;
220         if (!id.empty()) {
221                 if (param.find('#') != string::npos) {
222                         string::size_type pos = param.find("id=<");
223                         string::size_type end = param.find(">");
224                         if( pos != string::npos && end != string::npos)
225                                 param.erase(pos, end-pos + 1);
226                 }
227                 attribute = id + ' ' + param;
228         } else {
229                 if (param.find('#') != string::npos) {
230                         // FIXME UNICODE
231                         if (!style.counter.empty())
232                                 // This uses InternalUpdate at the moment becuase sgml output
233                                 // does not do anything with tracked counters, and it would need
234                                 // to track layouts if it did want to use them.
235                                 counters.step(style.counter, InternalUpdate);
236                         else
237                                 counters.step(from_ascii(name), InternalUpdate);
238                         int i = counters.value(from_ascii(name));
239                         attribute = subst(param, "#", convert<string>(i));
240                 } else {
241                         attribute = param;
242                 }
243         }
244         openTag(os, name, attribute);
245 }
246
247
248 void sgml::closeTag(odocstream & os, Paragraph const & par)
249 {
250         Layout const & style = par.layout();
251         closeTag(os, style.latexname());
252 }
253
254
255 } // namespace lyx