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 374 375 376 377 378 379 380 381 382
|
/* massXpert - the true massist's program.
--------------------------------------
Copyright(C) 2006,2007 Filippo Rusconi
http://www.massxpert.org/massXpert
This file is part of the massXpert project.
The massxpert project is the successor to the "GNU polyxmass"
project that is an official GNU project package(see
www.gnu.org). The massXpert project is not endorsed by the GNU
project, although it is released ---in its entirety--- under the
GNU General Public License. A huge part of the code in massXpert
is actually a C++ rewrite of code in GNU polyxmass. As such
massXpert was started at the Centre National de la Recherche
Scientifique(FRANCE), that granted me the formal authorization to
publish it under this Free Software License.
This software is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License version 3, as published by the Free Software Foundation.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; if not, write to the
Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/////////////////////// Qt includes
#include<QFile>
#include<QDir>
#include<QDebug>
/////////////////////// Local includes
#include "polChemDefCatParser.hpp"
#include "configSettings.hpp"
namespace massXpert
{
PolChemDefCatParser::PolChemDefCatParser()
{
mp_configSettings = 0;
m_pendingMode = 0;
m_configSysUser = 0;
return ;
}
PolChemDefCatParser::~PolChemDefCatParser()
{
return;
}
void
PolChemDefCatParser::setFilePath(const QString &filePath)
{
m_filePath = filePath;
}
const QString &
PolChemDefCatParser::getFilePath()
{
return m_filePath;
}
void
PolChemDefCatParser::setPendingMode(int pendingMode)
{
m_pendingMode = pendingMode;
}
int
PolChemDefCatParser::getPendingMode()
{
return m_pendingMode;
}
void
PolChemDefCatParser::setConfigSysUser(int configSysUser)
{
m_configSysUser = configSysUser;
}
int
PolChemDefCatParser::getConfigSysUser()
{
return m_configSysUser;
}
int
PolChemDefCatParser::parseFiles(const ConfigSettings &configSettings,
QList<PolChemDefSpec *>
*polChemDefSpecList)
{
mp_configSettings = &configSettings;
QDir dir;
int count = 0;
int totCount = 0;
// Polymer chemistry definition catalogue files are located in
// pol-chem-defs directories that are located in turn in either
// system-wide configuration places, like
// /etc/massxpert/pol-chem-defs, for example on UNIX-like systems,
// and/or in private user locations, like
// $HOME/.massxpert/pol-chem-defs.
Q_ASSERT(polChemDefSpecList);
// Depending on m_configSysUser, we are going to get all the files
// that are polymer chemistry def catalogues in the different
// locations and parse these files one after the other.
if (m_configSysUser & POLCHEMDEF_CAT_PARSE_SYSTEM_CONFIG)
{
// We have to parse all the catalogue files in the system
// configuration directory.
dir.setPath(configSettings.systemPolChemDefCatDir());
if(dir.exists())
{
// All the polchem definition catalogues must have the
// "pol-chem-defs-cat" filename suffix. So we filter the list of
// files in the directory according to that wildcard expression.
QStringList filters;
filters << "*pol-chem-defs-cat";
dir.setNameFilters(filters);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for (int iter = 0; iter < list.size(); ++iter)
{
QFileInfo fileInfo = list.at(iter);
m_filePath = fileInfo.absoluteFilePath();
count = parse(polChemDefSpecList,
POLCHEMDEF_CAT_PARSE_SYSTEM_CONFIG);
if(count == -1)
return -1;
else
totCount += count;
}
}
}
if (m_configSysUser & POLCHEMDEF_CAT_PARSE_USER_CONFIG)
{
// We have to parse all the catalogue files in the user
// configuration directory.
dir.setPath(configSettings.userPolChemDefCatDir());
if(dir.exists())
{
// All the atom definition catalogues must have the
// "atom-defs-cat" filename suffix. So we filter the list of
// files in the directory according to that wildcard expression.
QStringList filters;
filters << "*pol-chem-defs-cat";
dir.setNameFilters(filters);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for (int iter = 0; iter < list.size(); ++iter)
{
QFileInfo fileInfo = list.at(iter);
m_filePath = fileInfo.absoluteFilePath();
count = parse(polChemDefSpecList,
POLCHEMDEF_CAT_PARSE_USER_CONFIG);
if(count == -1)
return -1;
else
totCount += count;
}
}
}
return totCount;
}
int
PolChemDefCatParser::parse(QList<PolChemDefSpec *>
*polChemDefSpecList,
int forSystemOrUser)
{
qint64 lineLength;
QString line;
char buffer [1024];
int equalSignIdx = 0;
int count = 0;
PolChemDefSpec *polChemDefSpec = 0;
// We are given a QList in which to store all the
// PolChemDefSpec* instances that we create by parsing the
// catalogue files.
//
// Each line in the catalogue(s) that we may have to parse gives the
// name of a polymer chemistry definition, like "protein", and the
// full pathname of the file in which that definition is stored and
// the full pathname to the directory where all its corresponding
// stuff is stored. Example:
// protein=/some/dir/protein/protein.xml
//
// All we have to do is parse each line and for each valid one
// create a PolChemDefSpec item that we append/prepend to the
// QList passed as parameter.
Q_ASSERT(polChemDefSpecList);
QFile file(m_filePath);
if (! file.open(QFile::ReadOnly))
return -1;
//qDebug() << "Begin file:" << m_filePath.toAscii();
// Get the first line of the file. Next we enter in to a
// while loop.
lineLength = file.readLine(buffer, sizeof(buffer));
while(lineLength != -1)
{
// The line is now in buffer, and we want to convert
// it to Unicode by setting it in a QString.
line = buffer;
// Remove all the spaces from the borders: Whitespace means any
// character for which QChar::isSpace() returns true. This
// includes the ASCII characters '\t', '\n', '\v', '\f', '\r',
// and ' '.
line = line.trimmed();
// The line that is in line should contain something like:
// protein=protein/protein.xml(relative path)
// or:
// protein=/some/dir/protein/protein.xml(absolute path)
//
// Note, however that lines beginning with either ' '(space) or
// '\n'(newline) or '#' are comments.
if(line.isEmpty() || line.startsWith('#', Qt::CaseInsensitive))
{
lineLength = file.readLine(buffer, sizeof(buffer));
continue;
}
// Now some other checks.
equalSignIdx = line.indexOf('=', 0, Qt::CaseInsensitive);
if(equalSignIdx == -1 || line.count('=', Qt::CaseInsensitive) > 1)
return -1;
// Ok at this point, we might have a nicely parseable line, it
// makes sens to allocate a new AtomDefSpec object.
polChemDefSpec = new(PolChemDefSpec);
polChemDefSpec->setName(line.left(equalSignIdx));
// Of course we have to make sure that we are getting
// a file path corresponding to something we actually
// can use.
QString polChemDefFile = line.right(line.length()
- equalSignIdx - 1);
QFileInfo fileInfo(polChemDefFile);
if(fileInfo.isRelative())
{
// The polymer chemistry definition file is not an
// absolute filePath. We have to change it into an
// absolute filePath by prepending the directory path
// where it is located.
if (forSystemOrUser == POLCHEMDEF_CAT_PARSE_SYSTEM_CONFIG)
{
QFile file(mp_configSettings->systemPolChemDefCatDir() +
QDir::separator() + polChemDefFile);
if(!file.exists())
{
delete polChemDefSpec;
return -1;
}
polChemDefSpec->setFilePath(file.fileName());
}
else if (forSystemOrUser == POLCHEMDEF_CAT_PARSE_USER_CONFIG)
{
QFile file(mp_configSettings->userPolChemDefCatDir() +
QDir::separator() + polChemDefFile);
if(!file.exists())
{
delete polChemDefSpec;
return -1;
}
polChemDefSpec->setFilePath(file.fileName());
}
else
qFatal("%s@%d: Failed to parse polymer chemistry definition "
"catalogues.", __FILE__, __LINE__);
}
else
{
if (!fileInfo.exists())
{
delete polChemDefSpec;
return -1;
}
polChemDefSpec->setFilePath(polChemDefFile);
}
// OK, the file exists, and thus we can set the newly allocated
// polChemDefSpec object to the QList passed as parameter.
if(m_pendingMode == POLCHEMDEF_CAT_PARSE_APPEND_CONFIG)
polChemDefSpecList->append(polChemDefSpec);
else
polChemDefSpecList->prepend(polChemDefSpec);
++count;
//qDebug() << "Parsed line: " << line.toAscii();
lineLength = file.readLine(buffer, sizeof(buffer));
}
// while(lineLength != -1)
file.close();
//qDebug() << "Endfile:" << m_filePath.toAscii();
return count;
}
} // namespace massXpert
|