00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <globalsearch/queueinterfaces/local.h>
00017
00018 #include <globalsearch/macros.h>
00019 #include <globalsearch/optimizer.h>
00020 #include <globalsearch/queuemanager.h>
00021 #include <globalsearch/structure.h>
00022
00023 #include <QtCore/QDir>
00024 #include <QtCore/QFile>
00025 #include <QtCore/QHash>
00026 #include <QtCore/QProcess>
00027 #include <QtCore/QString>
00028 #include <QtCore/QTextStream>
00029
00030 namespace GlobalSearch {
00031
00032 LocalQueueInterface::LocalQueueInterface(OptBase *parent,
00033 const QString &settingFile) :
00034 QueueInterface(parent)
00035 {
00036 m_idString = "Local";
00037 m_hasDialog = false;
00038 }
00039
00040 LocalQueueInterface::~LocalQueueInterface()
00041 {
00042 }
00043
00044 bool LocalQueueInterface::writeFiles
00045 (Structure *s, const QHash<QString, QString> &fileHash) const
00046 {
00047
00048 QList<QFile*> files;
00049 QStringList filenames = fileHash.keys();
00050 for (int i = 0; i < filenames.size(); i++) {
00051 files.append(new QFile (s->fileName() + "/" + filenames.at(i)));
00052 }
00053
00054
00055 for (int i = 0; i < files.size(); i++) {
00056 if (!files.at(i)->open( QIODevice::WriteOnly | QIODevice::Text ) ) {
00057 m_opt->error(tr("Cannot write input file %1 (file writing failure)", "1 is a file path").arg(files.at(i)->fileName()));
00058 qDeleteAll(files);
00059 return false;
00060 }
00061 }
00062
00063
00064 QList<QTextStream*> streams;
00065 for (int i = 0; i < files.size(); i++) {
00066 streams.append(new QTextStream (files.at(i)));
00067 }
00068
00069
00070 for (int i = 0; i < streams.size(); i++) {
00071 *(streams.at(i)) << fileHash[filenames.at(i)];
00072 }
00073
00074
00075 for (int i = 0; i < files.size(); i++) {
00076 files.at(i)->close();
00077 }
00078
00079
00080 qDeleteAll(streams);
00081 qDeleteAll(files);
00082 return true;
00083 }
00084
00085 bool LocalQueueInterface::prepareForStructureUpdate(Structure *s) const
00086 {
00087
00088 return true;
00089 }
00090
00091 bool LocalQueueInterface::checkIfFileExists(Structure *s,
00092 const QString &filename,
00093 bool *exists)
00094 {
00095 *exists = QFile::exists(QString("%1/%2")
00096 .arg(s->fileName())
00097 .arg(filename));
00098 return true;
00099 }
00100
00101 bool LocalQueueInterface::fetchFile(Structure *s,
00102 const QString &rel_filename,
00103 QString *contents) const
00104 {
00105 QString filename = s->fileName() + "/" + rel_filename;
00106 QFile output (filename);
00107 if (!output.open(QFile::ReadOnly | QFile::Text)) {
00108 return false;
00109 }
00110 *contents = QString(output.readAll());
00111 output.close();
00112 return true;
00113 }
00114
00115 bool LocalQueueInterface::grepFile(Structure *s,
00116 const QString &matchText,
00117 const QString &filename,
00118 QStringList *matches,
00119 int *exitcode,
00120 const bool caseSensitive) const
00121 {
00122 if (exitcode) {
00123 *exitcode = 1;
00124 }
00125 QStringList list;
00126 QString contents;
00127 if (!fetchFile(s, filename, &contents)) {
00128 if (exitcode) {
00129 *exitcode = 2;
00130 }
00131 return false;
00132 }
00133
00134 list = contents.split("\n", QString::SkipEmptyParts);
00135
00136 Qt::CaseSensitivity cs;
00137 if (caseSensitive) {
00138 cs = Qt::CaseSensitive;
00139 }
00140 else {
00141 cs = Qt::CaseInsensitive;
00142 }
00143
00144 for (QStringList::const_iterator
00145 it = list.begin(),
00146 it_end = list.end();
00147 it != it_end;
00148 ++it) {
00149 if ((*it).contains(matchText, cs)) {
00150 if (exitcode) {
00151 *exitcode = 0;
00152 }
00153 if (matches) {
00154 *matches << *it;
00155 }
00156 }
00157 }
00158
00159 return true;
00160 }
00161 }