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