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 : SUIT_ParserSettings.cxx
25 // Author : Nicolas REJNERI
29 #include "SUIT_ParserSettings.h"
35 #include <qfileinfo.h>
36 #include <qtextstream.h>
42 SUIT_ParserSettings::SUIT_ParserSettings()
50 SUIT_ParserSettings::~SUIT_ParserSettings()
56 Gets the contents of a file.
58 QString SUIT_ParserSettings::getContents(QString fileName, bool comments,
59 bool sections, bool whiteSpace,
62 QFileInfo fi(fileName);
63 QFile f(fi.absFilePath());
64 QString s=""; // Buffer for the whole file
66 if(f.open(IO_ReadOnly)) { // file opened successfully
67 QTextStream t(&f); // use a text stream
69 s = t.read(); // The whole file in a string
72 if(!comments) s = SUIT_ParserSettings::removeComments(s);
73 if(!sections) s = SUIT_ParserSettings::removeSections(s);
74 if(!whiteSpace) s = s.simplifyWhiteSpace();
75 if(!htmlComments) s = SUIT_ParserSettings::removeHtmlComments(s);
86 Gets the body of a section from a string.
88 QString SUIT_ParserSettings::getSection(QString s, QString sectionName)
92 QChar ch; // A single byte of the file
93 int bracketCounter=1; // Bracket counter (increase on '{' and decrese on '}'.
94 int i; // Current index
95 int l=0; // Length of current part we must have
97 if((i=s.find(sectionName, 0, false)) >= 0 && // Jump to section [styles]
98 (i=s.find('{', i)) >= 0 ) {
102 while(i+l<(int)s.length()) {
103 ch = s.at(i+l); // Single byte
107 if(ch=='{') ++bracketCounter;
108 if(ch=='}') --bracketCounter;
110 if(bracketCounter==0) break;
113 result = s.mid(i, l-1);
122 Gets the HTML comments out of a file.
124 QString SUIT_ParserSettings::getHtmlComment(QString s)
127 int length; // length of the sub string
130 if((ind =s.find("<!--", ind))>=0 &&
132 (length=s.find("-->", ind)-ind)!=0) {
134 result=s.mid(ind, length-3);
143 Gets the next String between two given characters. The index-parameter gets moved to the character after
144 the stopper or to -1 if the starter / stopper were not found.
146 QString SUIT_ParserSettings::getNextStringBetween(QString s, int& startIndex,
147 QChar starter, QChar stopper)
150 int length=0; // length of the sub string
152 if((startIndex =s.find(starter, startIndex))>=0 &&
153 (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
155 result=s.mid(startIndex+1, length-1);
164 Gets the next String between the given index and a given stopper character. The index-parameter gets moved to
165 the character after the stopper or to -1 if the stopper was not found.
167 QString SUIT_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
170 int length; // length of the sub string
172 if((length=s.find(stopper, startIndex)-startIndex)!=0) {
173 result=s.mid(startIndex, length);
182 Removes all comments (between '/ *' and '* /').
184 QString SUIT_ParserSettings::removeComments(QString s)
189 while((i2=s.find("/*", i1))>=0) {
190 result += s.mid(i1, i2-i1);
197 result += s.mid(i1, s.length()-i1);
205 Removes all HTML comments (between '<!--' and '-->').
207 QString SUIT_ParserSettings::removeHtmlComments(QString s)
212 while((i2=s.find("<!--", i1))>=0) {
213 result += s.mid(i1, i2-i1);
216 i1=s.find("-->", i1);
220 result += s.mid(i1, s.length()-i1);
228 Removes all sections ('[section] { }').
230 QString SUIT_ParserSettings::removeSections(QString s)
234 QChar ch; // A single byte of the file
235 int bracketCounter; // Bracket counter (increase on '{' and decrese on '}'.
236 int i=0; // Current index
238 while(i<(int)s.length()) {
239 ch = s.at(i); // Single byte
243 while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
245 while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
248 while(i<(int)s.length() && bracketCounter!=0) {
250 if(ch=='{') ++bracketCounter;
251 if(ch=='}') --bracketCounter;
268 Format plain text into HTML-code with a given maximal width.
269 Spaces get replaced with non breaking spaces. Tabulators get filled up
270 with non breaking spaces.
272 QString SUIT_ParserSettings::plainTextToHtml(QString s, int autoBreak)
279 for(i=0; i<(int)s.length(); ++i) {
289 else if(col==autoBreak && autoBreak!=0) {
297 else if(s[i]=='\t') {
298 while(col%8!=0) { result+=" "; ++col; }
310 // Normal char / special code:
313 if(s[i].isLetter() || s[i].isNumber()) {
317 result+=charToHtml(s[i]);
331 Converts a special character to html code (e.g.: '»' to "»")
333 QString SUIT_ParserSettings::charToHtml(QChar c)
337 uc.setNum(c.unicode());