1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : QAD_ParserSettings.cxx
25 // Author : Nicolas REJNERI
29 #include "QAD_ParserSettings.h"
35 #include <qfileinfo.h>
36 #include <qtextstream.h>
43 QAD_ParserSettings::QAD_ParserSettings()
51 QAD_ParserSettings::~QAD_ParserSettings()
57 Gets the contents of a file.
59 QString QAD_ParserSettings::getContents(QString fileName, bool comments,
60 bool sections, bool whiteSpace,
63 QFileInfo fi(fileName);
64 QFile f(fi.absFilePath());
65 QString s=""; // Buffer for the whole file
67 if(f.open(IO_ReadOnly)) { // file opened successfully
68 QTextStream t(&f); // use a text stream
70 s = t.read(); // The whole file in a string
73 if(!comments) s = QAD_ParserSettings::removeComments(s);
74 if(!sections) s = QAD_ParserSettings::removeSections(s);
75 if(!whiteSpace) s = s.simplifyWhiteSpace();
76 if(!htmlComments) s = QAD_ParserSettings::removeHtmlComments(s);
87 Gets the body of a section from a string.
89 QString QAD_ParserSettings::getSection(QString s, QString sectionName)
93 QChar ch; // A single byte of the file
94 int bracketCounter=1; // Bracket counter (increase on '{' and decrese on '}'.
95 int i; // Current index
96 int l=0; // Length of current part we must have
98 if((i=s.find(sectionName, 0, false)) >= 0 && // Jump to section [styles]
99 (i=s.find('{', i)) >= 0 ) {
103 while(i+l<(int)s.length()) {
104 ch = s.at(i+l); // Single byte
108 if(ch=='{') ++bracketCounter;
109 if(ch=='}') --bracketCounter;
111 if(bracketCounter==0) break;
114 result = s.mid(i, l-1);
123 Gets the HTML comments out of a file.
125 QString QAD_ParserSettings::getHtmlComment(QString s)
128 int length; // length of the sub string
131 if((ind =s.find("<!--", ind))>=0 &&
133 (length=s.find("-->", ind)-ind)!=0) {
135 result=s.mid(ind, length-3);
144 Gets the next String between two given characters. The index-parameter gets moved to the character after
145 the stopper or to -1 if the starter / stopper were not found.
147 QString QAD_ParserSettings::getNextStringBetween(QString s, int& startIndex,
148 QChar starter, QChar stopper)
151 int length=0; // length of the sub string
153 if((startIndex =s.find(starter, startIndex))>=0 &&
154 (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
156 result=s.mid(startIndex+1, length-1);
165 Gets the next String between the given index and a given stopper character. The index-parameter gets moved to
166 the character after the stopper or to -1 if the stopper was not found.
168 QString QAD_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
171 int length; // length of the sub string
173 if((length=s.find(stopper, startIndex)-startIndex)!=0) {
174 result=s.mid(startIndex, length);
183 Removes all comments (between '/ *' and '* /').
185 QString QAD_ParserSettings::removeComments(QString s)
190 while((i2=s.find("/*", i1))>=0) {
191 result += s.mid(i1, i2-i1);
198 result += s.mid(i1, s.length()-i1);
206 Removes all HTML comments (between '<!--' and '-->').
208 QString QAD_ParserSettings::removeHtmlComments(QString s)
213 while((i2=s.find("<!--", i1))>=0) {
214 result += s.mid(i1, i2-i1);
217 i1=s.find("-->", i1);
221 result += s.mid(i1, s.length()-i1);
229 Removes all sections ('[section] { }').
231 QString QAD_ParserSettings::removeSections(QString s)
235 QChar ch; // A single byte of the file
236 int bracketCounter; // Bracket counter (increase on '{' and decrese on '}'.
237 int i=0; // Current index
239 while(i<(int)s.length()) {
240 ch = s.at(i); // Single byte
244 while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
246 while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
249 while(i<(int)s.length() && bracketCounter!=0) {
251 if(ch=='{') ++bracketCounter;
252 if(ch=='}') --bracketCounter;
269 Format plain text into HTML-code with a given maximal width.
270 Spaces get replaced with non breaking spaces. Tabulators get filled up
271 with non breaking spaces.
273 QString QAD_ParserSettings::plainTextToHtml(QString s, int autoBreak)
280 for(i=0; i<(int)s.length(); ++i) {
290 else if(col==autoBreak && autoBreak!=0) {
298 else if(s[i]=='\t') {
299 while(col%8!=0) { result+=" "; ++col; }
311 // Normal char / special code:
314 if(s[i].isLetter() || s[i].isNumber()) {
318 result+=charToHtml(s[i]);
332 Converts a special character to html code (e.g.: '»' to "»")
334 QString QAD_ParserSettings::charToHtml(QChar c)
338 uc.setNum(c.unicode());