Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_ParserSettings.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SUIT_ParserSettings.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SUIT_ParserSettings.h"
30
31 #include <stdio.h>
32
33 // QT Includes
34 #include <qfile.h>
35 #include <qfileinfo.h>
36 #include <qtextstream.h>
37
38
39 /*! Constructor. Do nothing.*/
40 SUIT_ParserSettings::SUIT_ParserSettings()
41 {
42 }
43
44
45 /*!
46     Destructor. Do nothing.
47 */
48 SUIT_ParserSettings::~SUIT_ParserSettings()
49 {
50 }
51
52
53 /*!
54     Gets the contents of a file.
55 */
56 QString SUIT_ParserSettings::getContents(QString fileName, bool comments,
57                                         bool sections, bool whiteSpace,
58                                         bool htmlComments)
59 {
60   QFileInfo fi(fileName);
61   QFile f(fi.absFilePath());
62   QString s="";                   // Buffer for the whole file
63
64   if(f.open(IO_ReadOnly)) {       // file opened successfully
65     QTextStream t(&f);            // use a text stream
66
67     s = t.read();                 // The whole file in a string
68     f.close();
69
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);
74   }
75   else {
76     // Can't open file
77   }
78
79   return s;
80 }
81
82
83 /*!
84     Gets the body of a section from a string.
85 */
86 QString SUIT_ParserSettings::getSection(QString s, QString sectionName)
87 {
88   QString result="";
89
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
94
95   if((i=s.find(sectionName, 0, false)) >= 0 &&       // Jump to section [styles]
96      (i=s.find('{', i))                >= 0    ) {
97
98     ++i;
99
100     while(i+l<(int)s.length()) {
101       ch = s.at(i+l);           // Single byte
102
103       ++l;
104
105       if(ch=='{') ++bracketCounter;
106       if(ch=='}') --bracketCounter;
107
108       if(bracketCounter==0) break;
109     }
110
111     result = s.mid(i, l-1);
112   }
113
114   return result;
115 }
116
117
118
119 /*!
120     Gets the HTML comments out of a file.
121 */
122 QString SUIT_ParserSettings::getHtmlComment(QString s)
123 {
124   QString result="";
125   int length;              // length of the sub string
126   int ind=0;
127
128   if((ind =s.find("<!--", ind))>=0 &&
129      (ind+=4) &&
130      (length=s.find("-->",  ind)-ind)!=0) {
131
132     result=s.mid(ind, length-3);
133   }
134
135   return result;
136 }
137
138
139
140 /*! 
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.
143 */
144 QString SUIT_ParserSettings::getNextStringBetween(QString s, int& startIndex, 
145                                                  QChar starter, QChar stopper)
146 {
147   QString result="";
148   int length=0;              // length of the sub string
149
150   if((startIndex =s.find(starter, startIndex))>=0 &&
151      (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
152
153     result=s.mid(startIndex+1, length-1);
154     startIndex+=2;
155   }
156
157   return result;
158 }
159
160
161 /*!
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.
164 */
165 QString SUIT_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
166 {
167   QString result="";
168   int length;              // length of the sub string
169
170   if((length=s.find(stopper, startIndex)-startIndex)!=0) {
171     result=s.mid(startIndex, length);
172     startIndex++;
173   }
174
175   return result;
176 }
177
178
179 /*!
180     Removes all comments (between '/ *' and '* /').
181 */
182 QString SUIT_ParserSettings::removeComments(QString s)
183 {
184   QString result="";
185   int i1=0, i2;
186
187   while((i2=s.find("/*", i1))>=0) {
188     result += s.mid(i1, i2-i1);
189
190     i1+=2;
191     i1=s.find("*/", i1);
192     i1+=2;
193   }
194
195   result += s.mid(i1, s.length()-i1);
196
197   return result;
198 }
199
200
201
202 /*!
203     Removes all HTML comments (between '<!--' and '-->') \image html html_comments.gif
204 */
205 QString SUIT_ParserSettings::removeHtmlComments(QString s)
206 {
207   QString result="";
208   int i1=0, i2;
209
210   while((i2=s.find("<!--", i1))>=0) {
211     result += s.mid(i1, i2-i1);
212
213     i1+=4;
214     i1=s.find("-->", i1);
215     i1+=3;
216   }
217
218   result += s.mid(i1, s.length()-i1);
219
220   return result;
221 }
222
223
224
225 /*!
226     Removes all sections ('[section] { }').
227 */
228 QString SUIT_ParserSettings::removeSections(QString s)
229 {
230   QString result="";
231
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
235
236   while(i<(int)s.length()) {
237     ch = s.at(i);           // Single byte
238
239     if(ch=='[') {
240       bracketCounter=1;
241       while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
242       ++i;
243       while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
244       ++i;
245
246       while(i<(int)s.length() && bracketCounter!=0) {
247         ch = s.at(i);
248         if(ch=='{') ++bracketCounter;
249         if(ch=='}') --bracketCounter;
250         ++i;
251       }
252       ++i;
253     }
254     else {
255       result+=ch;
256     }
257
258     ++i;
259   }
260
261   return result;
262 }
263
264
265 /*!
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.
269 */
270 QString SUIT_ParserSettings::plainTextToHtml(QString s, int autoBreak)
271 {
272   QString result="\n";
273
274   if(!s.isEmpty()) {
275     int col=1, i;
276
277     for(i=0; i<(int)s.length(); ++i) {
278       // Line feed:
279       //
280       if(s[i]=='\n') {
281         result+="<BR>\n";
282         col=1;
283       }
284
285       // Auto break:
286       //
287       else if(col==autoBreak && autoBreak!=0) {
288         result+="<BR>\n";
289         result+=s[i];
290         col=1;
291       }
292
293       // Tab:
294       //
295       else if(s[i]=='\t') {
296         while(col%8!=0) { result+="&nbsp;"; ++col; }
297         result+="&nbsp;";
298         ++col;
299       }
300
301       // Space:
302       //
303       else if(s[i]==' ') {
304         result+="&nbsp;";
305         ++col;
306       }
307
308       // Normal char / special code:
309       //
310       else {
311         if(s[i].isLetter() || s[i].isNumber()) {
312           result+=s[i];
313         }
314         else {
315           result+=charToHtml(s[i]);
316         }
317         ++col;
318       }
319     }
320
321     result+="\n";
322   }
323
324   return result;
325 }
326
327
328 /*!
329     Converts a special character to html code (e.g.: 'ยป' to "&#187;")
330 */
331 QString SUIT_ParserSettings::charToHtml(QChar c)
332 {
333   QString s;
334   QString uc;
335   uc.setNum(c.unicode());
336   s = "&#" + uc + ";";
337   return s;
338 }
339
340
341 // EOF