2 // File : QAD_ParserSettings.cxx
3 // Created : Sat Sep 08 22:00:08 2001
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE
11 #include "QAD_ParserSettings.h"
17 #include <qfileinfo.h>
18 #include <qtextstream.h>
24 QAD_ParserSettings::QAD_ParserSettings()
32 QAD_ParserSettings::~QAD_ParserSettings()
38 Gets the contents of a file.
40 QString QAD_ParserSettings::getContents(QString fileName, bool comments,
41 bool sections, bool whiteSpace,
44 QFileInfo fi(fileName);
45 QFile f(fi.absFilePath());
46 QString s=""; // Buffer for the whole file
48 if(f.open(IO_ReadOnly)) { // file opened successfully
49 QTextStream t(&f); // use a text stream
51 s = t.read(); // The whole file in a string
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);
68 Gets the body of a section from a string.
70 QString QAD_ParserSettings::getSection(QString s, QString sectionName)
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
79 if((i=s.find(sectionName, 0, false)) >= 0 && // Jump to section [styles]
80 (i=s.find('{', i)) >= 0 ) {
84 while(i+l<(int)s.length()) {
85 ch = s.at(i+l); // Single byte
89 if(ch=='{') ++bracketCounter;
90 if(ch=='}') --bracketCounter;
92 if(bracketCounter==0) break;
95 result = s.mid(i, l-1);
104 Gets the HTML comments out of a file.
106 QString QAD_ParserSettings::getHtmlComment(QString s)
109 int length; // length of the sub string
112 if((ind =s.find("<!--", ind))>=0 &&
114 (length=s.find("-->", ind)-ind)!=0) {
116 result=s.mid(ind, length-3);
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.
128 QString QAD_ParserSettings::getNextStringBetween(QString s, int& startIndex,
129 QChar starter, QChar stopper)
132 int length=0; // length of the sub string
134 if((startIndex =s.find(starter, startIndex))>=0 &&
135 (length=s.find(stopper, startIndex+1)-startIndex)!=0) {
137 result=s.mid(startIndex+1, length-1);
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.
149 QString QAD_ParserSettings::getNextStringUntil(QString s, int& startIndex, QChar stopper)
152 int length; // length of the sub string
154 if((length=s.find(stopper, startIndex)-startIndex)!=0) {
155 result=s.mid(startIndex, length);
164 Removes all comments (between '/ *' and '* /').
166 QString QAD_ParserSettings::removeComments(QString s)
171 while((i2=s.find("/*", i1))>=0) {
172 result += s.mid(i1, i2-i1);
179 result += s.mid(i1, s.length()-i1);
187 Removes all HTML comments (between '<!--' and '-->').
189 QString QAD_ParserSettings::removeHtmlComments(QString s)
194 while((i2=s.find("<!--", i1))>=0) {
195 result += s.mid(i1, i2-i1);
198 i1=s.find("-->", i1);
202 result += s.mid(i1, s.length()-i1);
210 Removes all sections ('[section] { }').
212 QString QAD_ParserSettings::removeSections(QString s)
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
220 while(i<(int)s.length()) {
221 ch = s.at(i); // Single byte
225 while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
227 while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
230 while(i<(int)s.length() && bracketCounter!=0) {
232 if(ch=='{') ++bracketCounter;
233 if(ch=='}') --bracketCounter;
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.
254 QString QAD_ParserSettings::plainTextToHtml(QString s, int autoBreak)
261 for(i=0; i<(int)s.length(); ++i) {
271 else if(col==autoBreak && autoBreak!=0) {
279 else if(s[i]=='\t') {
280 while(col%8!=0) { result+=" "; ++col; }
292 // Normal char / special code:
295 if(s[i].isLetter() || s[i].isNumber()) {
299 result+=charToHtml(s[i]);
313 Converts a special character to html code (e.g.: '»' to "»")
315 QString QAD_ParserSettings::charToHtml(QChar c)
319 uc.setNum(c.unicode());