Salome HOME
Initial version
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 /*!
40     Constructor.
41 */
42 SUIT_ParserSettings::SUIT_ParserSettings()
43 {
44 }
45
46
47 /*!
48     Destructor.
49 */
50 SUIT_ParserSettings::~SUIT_ParserSettings()
51 {
52 }
53
54
55 /*!
56     Gets the contents of a file.
57 */
58 QString SUIT_ParserSettings::getContents(QString fileName, bool comments,
59                                         bool sections, bool whiteSpace,
60                                         bool htmlComments)
61 {
62   QFileInfo fi(fileName);
63   QFile f(fi.absFilePath());
64   QString s="";                   // Buffer for the whole file
65
66   if(f.open(IO_ReadOnly)) {       // file opened successfully
67     QTextStream t(&f);            // use a text stream
68
69     s = t.read();                 // The whole file in a string
70     f.close();
71
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);
76   }
77   else {
78     // Can't open file
79   }
80
81   return s;
82 }
83
84
85 /*!
86     Gets the body of a section from a string.
87 */
88 QString SUIT_ParserSettings::getSection(QString s, QString sectionName)
89 {
90   QString result="";
91
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
96
97   if((i=s.find(sectionName, 0, false)) >= 0 &&       // Jump to section [styles]
98      (i=s.find('{', i))                >= 0    ) {
99
100     ++i;
101
102     while(i+l<(int)s.length()) {
103       ch = s.at(i+l);           // Single byte
104
105       ++l;
106
107       if(ch=='{') ++bracketCounter;
108       if(ch=='}') --bracketCounter;
109
110       if(bracketCounter==0) break;
111     }
112
113     result = s.mid(i, l-1);
114   }
115
116   return result;
117 }
118
119
120
121 /*!
122     Gets the HTML comments out of a file.
123 */
124 QString SUIT_ParserSettings::getHtmlComment(QString s)
125 {
126   QString result="";
127   int length;              // length of the sub string
128   int ind=0;
129
130   if((ind =s.find("<!--", ind))>=0 &&
131      (ind+=4) &&
132      (length=s.find("-->",  ind)-ind)!=0) {
133
134     result=s.mid(ind, length-3);
135   }
136
137   return result;
138 }
139
140
141
142 /*! 
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.
145 */
146 QString SUIT_ParserSettings::getNextStringBetween(QString s, int& startIndex, 
147                                                  QChar starter, QChar stopper)
148 {
149   QString result="";
150   int length=0;              // length of the sub string
151
152   if((startIndex =s.find(starter, startIndex))>=0 &&
153      (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
154
155     result=s.mid(startIndex+1, length-1);
156     startIndex+=2;
157   }
158
159   return result;
160 }
161
162
163 /*!
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.
166 */
167 QString SUIT_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
168 {
169   QString result="";
170   int length;              // length of the sub string
171
172   if((length=s.find(stopper, startIndex)-startIndex)!=0) {
173     result=s.mid(startIndex, length);
174     startIndex++;
175   }
176
177   return result;
178 }
179
180
181 /*!
182     Removes all comments (between '/ *' and '* /').
183 */
184 QString SUIT_ParserSettings::removeComments(QString s)
185 {
186   QString result="";
187   int i1=0, i2;
188
189   while((i2=s.find("/*", i1))>=0) {
190     result += s.mid(i1, i2-i1);
191
192     i1+=2;
193     i1=s.find("*/", i1);
194     i1+=2;
195   }
196
197   result += s.mid(i1, s.length()-i1);
198
199   return result;
200 }
201
202
203
204 /*!
205     Removes all HTML comments (between '<!--' and '-->').
206 */
207 QString SUIT_ParserSettings::removeHtmlComments(QString s)
208 {
209   QString result="";
210   int i1=0, i2;
211
212   while((i2=s.find("<!--", i1))>=0) {
213     result += s.mid(i1, i2-i1);
214
215     i1+=4;
216     i1=s.find("-->", i1);
217     i1+=3;
218   }
219
220   result += s.mid(i1, s.length()-i1);
221
222   return result;
223 }
224
225
226
227 /*!
228     Removes all sections ('[section] { }').
229 */
230 QString SUIT_ParserSettings::removeSections(QString s)
231 {
232   QString result="";
233
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
237
238   while(i<(int)s.length()) {
239     ch = s.at(i);           // Single byte
240
241     if(ch=='[') {
242       bracketCounter=1;
243       while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
244       ++i;
245       while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
246       ++i;
247
248       while(i<(int)s.length() && bracketCounter!=0) {
249         ch = s.at(i);
250         if(ch=='{') ++bracketCounter;
251         if(ch=='}') --bracketCounter;
252         ++i;
253       }
254       ++i;
255     }
256     else {
257       result+=ch;
258     }
259
260     ++i;
261   }
262
263   return result;
264 }
265
266
267 /*!
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.
271 */
272 QString SUIT_ParserSettings::plainTextToHtml(QString s, int autoBreak)
273 {
274   QString result="\n";
275
276   if(!s.isEmpty()) {
277     int col=1, i;
278
279     for(i=0; i<(int)s.length(); ++i) {
280       // Line feed:
281       //
282       if(s[i]=='\n') {
283         result+="<BR>\n";
284         col=1;
285       }
286
287       // Auto break:
288       //
289       else if(col==autoBreak && autoBreak!=0) {
290         result+="<BR>\n";
291         result+=s[i];
292         col=1;
293       }
294
295       // Tab:
296       //
297       else if(s[i]=='\t') {
298         while(col%8!=0) { result+="&nbsp;"; ++col; }
299         result+="&nbsp;";
300         ++col;
301       }
302
303       // Space:
304       //
305       else if(s[i]==' ') {
306         result+="&nbsp;";
307         ++col;
308       }
309
310       // Normal char / special code:
311       //
312       else {
313         if(s[i].isLetter() || s[i].isNumber()) {
314           result+=s[i];
315         }
316         else {
317           result+=charToHtml(s[i]);
318         }
319         ++col;
320       }
321     }
322
323     result+="\n";
324   }
325
326   return result;
327 }
328
329
330 /*!
331     Converts a special character to html code (e.g.: 'ยป' to "&#187;")
332 */
333 QString SUIT_ParserSettings::charToHtml(QChar c)
334 {
335   QString s;
336   QString uc;
337   uc.setNum(c.unicode());
338   s = "&#" + uc + ";";
339   return s;
340 }
341
342
343 // EOF