/*========================================================================= Library: CTK Copyright (c) Kitware Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.commontk.org/LICENSE Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =========================================================================*/ // Qt includes #include #include #include #include #include // CTK includes #include // STD includes #include #include //---------------------------------------------------------------------------- QString help(const QString& progName) { QString msg = "Usage: %1 [-paths Label | -sort Label]"; return msg.arg(progName); } //---------------------------------------------------------------------------- void displayError(const QString& progName, const QString& msg) { std::cerr << qPrintable(QString("%1\n%2\n%3\n").arg(progName). arg(msg). arg(help(progName))); } //---------------------------------------------------------------------------- int getOrGenerateId(QHash& vertexIdToLabel, QHash& vertexLabelToId, const QString& label) { // If needed, generate vertex id int vertexId = -1; if (!vertexLabelToId.keys().contains(label)) { vertexId = vertexLabelToId.keys().size() + 1; vertexLabelToId[label] = vertexId; vertexIdToLabel[vertexId] = label; } else { vertexId = vertexLabelToId[label]; } return vertexId; } //---------------------------------------------------------------------------- int main(int argc, char** argv) { bool verbose = false; bool outputTopologicalOrder = true; // a graph file is expected if (argc < 2) { displayError(argv[0], QLatin1String("Missing one argument")); return EXIT_FAILURE; } bool outputPath = false; bool outputSort = false; QString label; if (argc == 3) { displayError(argv[0], QLatin1String("Wrong argument")); return EXIT_FAILURE; } if (argc == 4) { QString arg2 = QString::fromLatin1(argv[2]); if (arg2.compare("-paths")!=0 && arg2.compare("-sort")!=0) { displayError(argv[0], QString("Wrong argument: %1").arg(arg2)); return EXIT_FAILURE; } label = QLatin1String(argv[3]); outputTopologicalOrder = false; if (arg2.compare("-paths") == 0) { outputPath = true; } else { outputSort = true; } if (verbose) { qDebug() << "label:" << label; } } QString filepath = QString::fromLatin1(argv[1]); if (!QFile::exists(filepath)) { displayError(argv[0], QString("File '%1' doesn't exists !").arg(filepath)); return EXIT_FAILURE; } QFile data(filepath); if (!data.open(QFile::ReadOnly)) { displayError(argv[0], QString("Failed to open file '%1' !").arg(filepath)); return EXIT_FAILURE; } QTextStream in(&data); QString header = in.readLine(); if (header.isNull()) { displayError(argv[0], QString("Failed to read Header line in file '%1' !").arg(filepath)); return EXIT_FAILURE; } // Regular expression to extract two integers QRegExp twoint_re("^([0-9]+)\\s+([0-9]+)"); // Extract numberOfVertices and numberOfEdges int pos = twoint_re.indexIn(header.trimmed()); if (pos != 0) { displayError(argv[0], QString("Error in file '%1' - First line should look like: <#Vertices> <#Edges>") .arg(filepath)); return EXIT_FAILURE; } QStringList list = twoint_re.capturedTexts(); Q_ASSERT(list.size() == 3); int numberOfVertices = list[1].toInt(); int numberOfEdges = list[2].toInt(); if (verbose) { qDebug() << "#Vertices:" << numberOfVertices << "#Edges:" << numberOfEdges; } // Init ctkDependencyGraph mygraph(numberOfVertices); mygraph.setVerbose(verbose); // Map between vertex label and vertex id QHash vertexIdToLabel; QHash vertexLabelToId; // Regular expression to extract two label QRegExp twolabel_re("^\\s*(\\S+)\\s*$|^\\s*(\\S+)\\s*(\\S+)\\s*$"); // Read vertex connection int lineNumber = 2; QString line = in.readLine(); do { // Skip empty line or commented line if (line.isEmpty() || line.startsWith("#")) { line = in.readLine(); continue; } // Extract vertex points int pos = twolabel_re.indexIn(line.trimmed()); if (pos != 0) { displayError(argv[0], QString("Error in file '%1' - line:%2 - Expected format is: