Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / SALOMEGUI / QAD_ParserSettings.cxx
1 using namespace std;
2 //  File      : QAD_ParserSettings.cxx
3 //  Created   : Sat Sep 08 22:00:08 2001
4 //  Author    : Nicolas REJNERI
5 //  Project   : SALOME
6 //  Module    : SALOMEGUI
7 //  Copyright : Open CASCADE
8 //  $Header$
9
10
11 #include "QAD_ParserSettings.h"
12
13 #include <stdio.h>
14
15 // QT Includes
16 #include <qfile.h>
17 #include <qfileinfo.h>
18 #include <qtextstream.h>
19
20
21 /*!
22     Constructor.
23 */
24 QAD_ParserSettings::QAD_ParserSettings()
25 {
26 }
27
28
29 /*!
30     Destructor.
31 */
32 QAD_ParserSettings::~QAD_ParserSettings()
33 {
34 }
35
36
37 /*!
38     Gets the contents of a file.
39 */
40 QString QAD_ParserSettings::getContents(QString fileName, bool comments,
41                                         bool sections, bool whiteSpace,
42                                         bool htmlComments)
43 {
44   QFileInfo fi(fileName);
45   QFile f(fi.absFilePath());
46   QString s="";                   // Buffer for the whole file
47
48   if(f.open(IO_ReadOnly)) {       // file opened successfully
49     QTextStream t(&f);            // use a text stream
50
51     s = t.read();                 // The whole file in a string
52     f.close();
53
54     if(!comments)     s = QAD_ParserSettings::removeComments(s);
55     if(!sections)     s = QAD_ParserSettings::removeSections(s);
56     if(!whiteSpace)   s = s.simplifyWhiteSpace();
57     if(!htmlComments) s = QAD_ParserSettings::removeHtmlComments(s);
58   }
59   else {
60     // Can't open file
61   }
62
63   return s;
64 }
65
66
67 /*!
68     Gets the body of a section from a string.
69 */
70 QString QAD_ParserSettings::getSection(QString s, QString sectionName)
71 {
72   QString result="";
73
74   QChar ch;                     // A single byte of the file
75   int bracketCounter=1;         // Bracket counter (increase on '{' and decrese on '}'.
76   int i;                        // Current index
77   int l=0;                      // Length of current part we must have
78
79   if((i=s.find(sectionName, 0, false)) >= 0 &&       // Jump to section [styles]
80      (i=s.find('{', i))                >= 0    ) {
81
82     ++i;
83
84     while(i+l<(int)s.length()) {
85       ch = s.at(i+l);           // Single byte
86
87       ++l;
88
89       if(ch=='{') ++bracketCounter;
90       if(ch=='}') --bracketCounter;
91
92       if(bracketCounter==0) break;
93     }
94
95     result = s.mid(i, l-1);
96   }
97
98   return result;
99 }
100
101
102
103 /*!
104     Gets the HTML comments out of a file.
105 */
106 QString QAD_ParserSettings::getHtmlComment(QString s)
107 {
108   QString result="";
109   int length;              // length of the sub string
110   int ind=0;
111
112   if((ind =s.find("<!--", ind))>=0 &&
113      (ind+=4) &&
114      (length=s.find("-->",  ind)-ind)!=0) {
115
116     result=s.mid(ind, length-3);
117   }
118
119   return result;
120 }
121
122
123
124 /*! 
125     Gets the next String between two given characters. The index-parameter gets moved to the character after
126     the stopper or to -1 if the starter / stopper were not found.
127 */
128 QString QAD_ParserSettings::getNextStringBetween(QString s, int& startIndex, 
129                                                  QChar starter, QChar stopper)
130 {
131   QString result="";
132   int length=0;              // length of the sub string
133
134   if((startIndex =s.find(starter, startIndex))>=0 &&
135      (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
136
137     result=s.mid(startIndex+1, length-1);
138     startIndex+=2;
139   }
140
141   return result;
142 }
143
144
145 /*!
146     Gets the next String between the given index and a given stopper character. The index-parameter gets moved to
147     the character after the stopper or to -1 if the stopper was not found.
148 */
149 QString QAD_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
150 {
151   QString result="";
152   int length;              // length of the sub string
153
154   if((length=s.find(stopper, startIndex)-startIndex)!=0) {
155     result=s.mid(startIndex, length);
156     startIndex++;
157   }
158
159   return result;
160 }
161
162
163 /*!
164     Removes all comments (between '/ *' and '* /').
165 */
166 QString QAD_ParserSettings::removeComments(QString s)
167 {
168   QString result="";
169   int i1=0, i2;
170
171   while((i2=s.find("/*", i1))>=0) {
172     result += s.mid(i1, i2-i1);
173
174     i1+=2;
175     i1=s.find("*/", i1);
176     i1+=2;
177   }
178
179   result += s.mid(i1, s.length()-i1);
180
181   return result;
182 }
183
184
185
186 /*!
187     Removes all HTML comments (between '<!--' and '-->').
188 */
189 QString QAD_ParserSettings::removeHtmlComments(QString s)
190 {
191   QString result="";
192   int i1=0, i2;
193
194   while((i2=s.find("<!--", i1))>=0) {
195     result += s.mid(i1, i2-i1);
196
197     i1+=4;
198     i1=s.find("-->", i1);
199     i1+=3;
200   }
201
202   result += s.mid(i1, s.length()-i1);
203
204   return result;
205 }
206
207
208
209 /*!
210     Removes all sections ('[section] { }').
211 */
212 QString QAD_ParserSettings::removeSections(QString s)
213 {
214   QString result="";
215
216   QChar ch;                     // A single byte of the file
217   int bracketCounter;           // Bracket counter (increase on '{' and decrese on '}'.
218   int i=0;                      // Current index
219
220   while(i<(int)s.length()) {
221     ch = s.at(i);           // Single byte
222
223     if(ch=='[') {
224       bracketCounter=1;
225       while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
226       ++i;
227       while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
228       ++i;
229
230       while(i<(int)s.length() && bracketCounter!=0) {
231         ch = s.at(i);
232         if(ch=='{') ++bracketCounter;
233         if(ch=='}') --bracketCounter;
234         ++i;
235       }
236       ++i;
237     }
238     else {
239       result+=ch;
240     }
241
242     ++i;
243   }
244
245   return result;
246 }
247
248
249 /*!
250     Format plain text into HTML-code with a given maximal width.
251     Spaces get replaced with non breaking spaces. Tabulators get filled up
252     with non breaking spaces.
253 */
254 QString QAD_ParserSettings::plainTextToHtml(QString s, int autoBreak)
255 {
256   QString result="\n";
257
258   if(!s.isEmpty()) {
259     int col=1, i;
260
261     for(i=0; i<(int)s.length(); ++i) {
262       // Line feed:
263       //
264       if(s[i]=='\n') {
265         result+="<BR>\n";
266         col=1;
267       }
268
269       // Auto break:
270       //
271       else if(col==autoBreak && autoBreak!=0) {
272         result+="<BR>\n";
273         result+=s[i];
274         col=1;
275       }
276
277       // Tab:
278       //
279       else if(s[i]=='\t') {
280         while(col%8!=0) { result+="&nbsp;"; ++col; }
281         result+="&nbsp;";
282         ++col;
283       }
284
285       // Space:
286       //
287       else if(s[i]==' ') {
288         result+="&nbsp;";
289         ++col;
290       }
291
292       // Normal char / special code:
293       //
294       else {
295         if(s[i].isLetter() || s[i].isNumber()) {
296           result+=s[i];
297         }
298         else {
299           result+=charToHtml(s[i]);
300         }
301         ++col;
302       }
303     }
304
305     result+="\n";
306   }
307
308   return result;
309 }
310
311
312 /*!
313     Converts a special character to html code (e.g.: 'ยป' to "&#187;")
314 */
315 QString QAD_ParserSettings::charToHtml(QChar c)
316 {
317   QString s;
318   QString uc;
319   uc.setNum(c.unicode());
320   s = "&#" + uc + ";";
321   return s;
322 }
323
324
325 // EOF