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>
39 /*! Constructor. Do nothing.*/
40 SUIT_ParserSettings::SUIT_ParserSettings()
46 Destructor. Do nothing.
48 SUIT_ParserSettings::~SUIT_ParserSettings()
54 Gets the contents of a file.
56 QString SUIT_ParserSettings::getContents(QString fileName, bool comments,
57 bool sections, bool whiteSpace,
60 QFileInfo fi(fileName);
61 QFile f(fi.absFilePath());
62 QString s=""; // Buffer for the whole file
64 if(f.open(IO_ReadOnly)) { // file opened successfully
65 QTextStream t(&f); // use a text stream
67 s = t.read(); // The whole file in a string
70 if(!comments) s = SUIT_ParserSettings::removeComments(s);
71 if(!sections) s = SUIT_ParserSettings::removeSections(s);
72 if(!whiteSpace) s = s.simplifyWhiteSpace();
73 if(!htmlComments) s = SUIT_ParserSettings::removeHtmlComments(s);
84 Gets the body of a section from a string.
86 QString SUIT_ParserSettings::getSection(QString s, QString sectionName)
90 QChar ch; // A single byte of the file
91 int bracketCounter=1; // Bracket counter (increase on '{' and decrese on '}'.
92 int i; // Current index
93 int l=0; // Length of current part we must have
95 if((i=s.find(sectionName, 0, false)) >= 0 && // Jump to section [styles]
96 (i=s.find('{', i)) >= 0 ) {
100 while(i+l<(int)s.length()) {
101 ch = s.at(i+l); // Single byte
105 if(ch=='{') ++bracketCounter;
106 if(ch=='}') --bracketCounter;
108 if(bracketCounter==0) break;
111 result = s.mid(i, l-1);
120 Gets the HTML comments out of a file.
122 QString SUIT_ParserSettings::getHtmlComment(QString s)
125 int length; // length of the sub string
128 if((ind =s.find("<!--", ind))>=0 &&
130 (length=s.find("-->", ind)-ind)!=0) {
132 result=s.mid(ind, length-3);
141 Gets the next String between two given characters. The index-parameter gets moved to the character after
142 the stopper or to -1 if the starter / stopper were not found.
144 QString SUIT_ParserSettings::getNextStringBetween(QString s, int& startIndex,
145 QChar starter, QChar stopper)
148 int length=0; // length of the sub string
150 if((startIndex =s.find(starter, startIndex))>=0 &&
151 (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
153 result=s.mid(startIndex+1, length-1);
162 Gets the next String between the given index and a given stopper character. The index-parameter gets moved to
163 the character after the stopper or to -1 if the stopper was not found.
165 QString SUIT_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
168 int length; // length of the sub string
170 if((length=s.find(stopper, startIndex)-startIndex)!=0) {
171 result=s.mid(startIndex, length);
180 Removes all comments (between '/ *' and '* /').
182 QString SUIT_ParserSettings::removeComments(QString s)
187 while((i2=s.find("/*", i1))>=0) {
188 result += s.mid(i1, i2-i1);
195 result += s.mid(i1, s.length()-i1);
203 Removes all HTML comments (between '<!--' and '-->') \image html html_comments.gif
205 QString SUIT_ParserSettings::removeHtmlComments(QString s)
210 while((i2=s.find("<!--", i1))>=0) {
211 result += s.mid(i1, i2-i1);
214 i1=s.find("-->", i1);
218 result += s.mid(i1, s.length()-i1);
226 Removes all sections ('[section] { }').
228 QString SUIT_ParserSettings::removeSections(QString s)
232 QChar ch; // A single byte of the file
233 int bracketCounter; // Bracket counter (increase on '{' and decrese on '}'.
234 int i=0; // Current index
236 while(i<(int)s.length()) {
237 ch = s.at(i); // Single byte
241 while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
243 while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
246 while(i<(int)s.length() && bracketCounter!=0) {
248 if(ch=='{') ++bracketCounter;
249 if(ch=='}') --bracketCounter;
266 Format plain text into HTML-code with a given maximal width.
267 Spaces get replaced with non breaking spaces. Tabulators get filled up
268 with non breaking spaces.
270 QString SUIT_ParserSettings::plainTextToHtml(QString s, int autoBreak)
277 for(i=0; i<(int)s.length(); ++i) {
287 else if(col==autoBreak && autoBreak!=0) {
295 else if(s[i]=='\t') {
296 while(col%8!=0) { result+=" "; ++col; }
308 // Normal char / special code:
311 if(s[i].isLetter() || s[i].isNumber()) {
315 result+=charToHtml(s[i]);
329 Converts a special character to html code (e.g.: '»' to "»")
331 QString SUIT_ParserSettings::charToHtml(QChar c)
335 uc.setNum(c.unicode());