update credits
[librepilot.git] / ground / uavobjgenerator / generators / generator_io.cpp
blobfa352e2773dfaf0fee781d7836b4139b40f78e43
1 /**
2 ******************************************************************************
4 * @file generator_io.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief I/O Code for the generator
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "generator_io.h"
29 using namespace std;
31 /**
32 * Read a file and return its contents as a string
34 QString readFile(QString name, bool do_warn)
36 QFile file(name);
38 if (!file.open(QFile::ReadOnly)) {
39 if (do_warn) {
40 std::cout << "Warning: Could not open " << name.toStdString() << endl;
42 return QString();
45 QTextStream fileStr(&file);
46 QString str = fileStr.readAll();
47 file.close();
48 return str;
51 /**
52 * Read a file and return its contents as a string
54 QString readFile(QString name)
56 return readFile(name, true);
59 /**
60 * Write contents of string to file
62 bool writeFile(QString name, QString & str)
64 QFile file(name);
66 if (!file.open(QFile::WriteOnly)) {
67 return false;
69 QTextStream fileStr(&file);
70 fileStr << str;
71 file.close();
72 return true;
75 /**
76 * Write contents of string to file if the content changes
78 bool writeFileIfDifferent(QString name, QString & str)
80 if (str == readFile(name, false)) {
81 return true;
83 return writeFile(name, str);