1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/***************************************************************************
rfileparser.cpp - description
-------------------
begin : Sun Sep 12 1999
copyright : (C) 1999 by Andreas Mustun
email : andrew@ribbonsoft.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "rfileparser.h"
#include "rprgdef.h"
#include <stdio.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qtextstream.h>
/**
* \class RFileParser
* Class RFileParser offers some useful methode for parsing MAS-files.
* A section is defined as a Header '[section]' and a body '{ ... }'.
* Section Headers are not case sensitive.
*/
/**
* Constructor.
*/
RFileParser::RFileParser()
{
}
/**
* Destructor.
*/
RFileParser::~RFileParser()
{
}
/**
* Gets the contents of a file.
*
* \param fileName File name.
* \param comments Include comments?
* \param sections Include sections in form [section] { } ?
* \param whiteSpace Include whitespace (false means that white space gets simplified)
* \param htmlComments Include HTML comments (<!-- -->)?
*/
QString
RFileParser::getContents(QString fileName, bool comments, bool sections, bool whiteSpace, bool htmlComments)
{
QFileInfo fi(fileName);
QFile f(fi.absFilePath());
QString s=""; // Buffer for the whole file
if(f.open(IO_ReadOnly)) { // file opened successfully
QTextStream t(&f); // use a text stream
s = t.read(); // The whole file in a string
f.close();
if(!comments) s = RFileParser::removeComments(s);
if(!sections) s = RFileParser::removeSections(s);
if(!whiteSpace) s = s.simplifyWhiteSpace();
if(!htmlComments) s = RFileParser::removeHtmlComments(s);
}
else {
// Can't open file
}
return s;
}
/**
* Gets the body of a section from a string.
* Comment lines starting with '//' get removed.
*
* \param s The string.
* \param sectionName Name of the section (e.g. '[styles]'). This methode ignores case for section names.
*/
QString
RFileParser::getSection(QString s, QString sectionName)
{
QString result="";
QChar ch; // A single byte of the file
int bracketCounter=1; // Bracket counter (increase on '{' and decrese on '}'.
int i=0; // Current index
int l=0; // Length of current part we must have
if((i=s.find(sectionName, 0, false)) >= 0 && // Jump to section [styles]
(i=s.find('{', i)) >= 0 ) {
++i;
while(i+l<(int)s.length()) {
ch = s.at(i+l); // Single byte
++l;
if(ch=='{') ++bracketCounter;
if(ch=='}') --bracketCounter;
if(bracketCounter==0) break;
}
result = s.mid(i, l-1);
}
return result;
}
/**
* Gets the HTML comments out of a file.
*
* \param
*/
QString
RFileParser::getHtmlComment(QString s)
{
QString result="";
int length=0; // length of the sub string
int index=0;
if((index =s.find("<!--", index))>=0 &&
(index+=4) &&
(length=s.find("-->", index)-index)!=0) {
result=s.mid(index, length-3);
}
return result;
}
/**
* Gets the next String between two given characters. The index-parameter gets moved to the character after
* the stopper or to -1 if the starter / stopper were not found.
*
* \param s The original string to search through.
* \param index The index where the search starts.
* \param starter The starter character.
* \param stopper The stopper character.
* \return The next string between the two characters excluding the characters themselves or an empty string.
*/
QString
RFileParser::getNextStringBetween(QString s, int& index, QChar starter, QChar stopper)
{
QString result="";
int length=0; // length of the sub string
if((index =s.find(starter, index))>=0 &&
(length=s.find(stopper, index+1)-index)!=0) {
result=s.mid(index+1, length-1);
index+=2;
}
return result;
}
/**
* Gets the next String between the given index and a given stopper character. The index-parameter gets moved to
* the character after the stopper or to -1 if the stopper was not found.
*
* \param s The original string to search through.
* \param index The index where the search starts.
* \param stopper The stopper character.
* \return The next string between the given index and the stopper character excluding the stopper character or an empty string.
*/
QString
RFileParser::getNextStringUntil(QString s, int& index, QChar stopper)
{
QString result="";
int length=0; // length of the sub string
if((length=s.find(stopper, index)-index)!=0) {
result=s.mid(index, length);
index++;
}
return result;
}
/**
* Removes all comments (between '/ *' and '* /').
*/
QString
RFileParser::removeComments(QString s)
{
QString result="";
int i1=0, i2=0;
while((i2=s.find("/*", i1))>=0) {
result += s.mid(i1, i2-i1);
i1+=2;
i1=s.find("*/", i1);
i1+=2;
}
result += s.mid(i1, s.length()-i1);
return result;
}
/**
* Removes all HTML comments (between '<!--' and '-->').
*/
QString
RFileParser::removeHtmlComments(QString s)
{
QString result="";
int i1=0, i2=0;
while((i2=s.find("<!--", i1))>=0) {
result += s.mid(i1, i2-i1);
i1+=4;
i1=s.find("-->", i1);
i1+=3;
}
result += s.mid(i1, s.length()-i1);
return result;
}
/**
* Removes all sections ('[section] { }').
*/
QString
RFileParser::removeSections(QString s)
{
QString result="";
QChar ch; // A single byte of the file
int bracketCounter; // Bracket counter (increase on '{' and decrese on '}'.
int i=0; // Current index
while(i<(int)s.length()) {
ch = s.at(i); // Single byte
if(ch=='[') {
bracketCounter=1;
while(i<(int)s.length() && ch!=']') { ch = s.at(i); ++i; }
++i;
while(i<(int)s.length() && ch!='{') { ch = s.at(i); ++i; }
++i;
while(i<(int)s.length() && bracketCounter!=0) {
ch = s.at(i);
if(ch=='{') ++bracketCounter;
if(ch=='}') --bracketCounter;
++i;
}
++i;
}
else {
result+=ch;
}
++i;
}
return result;
}
/**
* Format plain text into HTML-code with a given maximal width.
* Spaces get replaced with non breaking spaces. Tabulators get filled up
* with non breaking spaces.
*
* \param s The original plain text string
*/
QString
RFileParser::plainTextToHtml(QString s, int autoBreak)
{
QString result="\n";
if(!s.isEmpty()) {
int col=1, i;
for(i=0; i<(int)s.length(); ++i) {
// Line feed:
//
if(s[i]=='\n') {
result+="<BR>\n";
col=1;
}
// Auto break:
//
else if(col==autoBreak && autoBreak!=0) {
result+="<BR>\n";
result+=s[i];
col=1;
}
// Tab:
//
else if(s[i]=='\t') {
while(col%8!=0) { result+=" "; ++col; }
result+=" ";
++col;
}
// Space:
//
else if(s[i]==' ') {
result+=" ";
++col;
}
// Normal char / special code:
//
else {
if(s[i].isLetter() || s[i].isNumber()) {
result+=s[i];
}
else {
result+=charToHtml(s[i]);
}
++col;
}
}
result+="\n";
}
return result;
}
/**
* Converts a special character to html code (e.g.: '' to "»")
*/
QString
RFileParser::charToHtml(QChar c)
{
QString s;
QString uc;
uc.setNum(c.unicode());
s = "&#" + uc + ";";
return s;
}
// EOF
|