File indexing completed on 2024-12-15 04:56:11
0001 /* 0002 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program; if not, write to the 0016 * Free Software Foundation, Inc., 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0018 */ 0019 0020 #include "print.h" 0021 0022 #include "../datasetdefinition.h" 0023 #include "../dataset.h" 0024 0025 #include <QDir> 0026 #include <QObject> 0027 0028 #include <iostream> 0029 0030 namespace HAWD 0031 { 0032 0033 Print::Print() 0034 : Module() 0035 { 0036 Syntax top("print", &Print::print); 0037 setSyntax(top); 0038 setDescription(QObject::tr("Prints a table from a dataset; you can provide a list of rows to output")); 0039 } 0040 0041 bool Print::print(const QStringList &commands, State &state) 0042 { 0043 if (commands.isEmpty()) { 0044 std::cout << QObject::tr("print requires a dataset to be named").toStdString() << std::endl; 0045 return true; 0046 } 0047 0048 const QString datasetName = commands.first(); 0049 0050 QDir project(state.projectPath()); 0051 Dataset dataset(datasetName, state); 0052 0053 if (!dataset.isValid()) { 0054 std::cout << QObject::tr("The dataset %1 could not be loaded; try checking it with the check command").arg(datasetName).toStdString() << std::endl; 0055 return true; 0056 } 0057 0058 QStringList cols; 0059 if (commands.size() > 1) { 0060 cols = commands; 0061 cols.removeFirst(); 0062 } 0063 0064 std::cout << dataset.tableHeaders(cols).toStdString() << std::endl; 0065 0066 dataset.eachRow( 0067 [cols](const Dataset::Row &row) { 0068 std::cout << row.toString(cols).toStdString() << std::endl; 0069 }); 0070 return true; 0071 } 0072 0073 } // namespace HAWD 0074