INIFile.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #ifndef INIFILE_H
  19. #define INIFILE_H
  20. class QFile;
  21. class QTextStream;
  22. #include <QFile>
  23. #include <QString>
  24. #include <QTextStream>
  25. class INIFile {
  26. QFile* file = nullptr;
  27. QTextStream* stream = nullptr;
  28. protected:
  29. virtual void handleKey(QString key, QString value) = 0;
  30. virtual void handleSection(QString section) = 0;
  31. virtual bool writeOutputToStream() = 0;
  32. virtual QString codec() {
  33. return "ASCII";
  34. }
  35. public:
  36. INIFile() {}
  37. bool readFromFile(QString fileName) {
  38. file = new QFile(fileName);
  39. if (file->open(QIODevice::ReadOnly | QIODevice::Text)) {
  40. stream = new QTextStream(file);
  41. stream->setCodec(this->codec().toStdString().c_str());
  42. while (!stream->atEnd()) {
  43. QString line = stream->readLine();
  44. QStringList kv = line.split("=");
  45. if (kv.length() == 2) {
  46. handleKey(kv[0], kv[1]);
  47. } else if (line[0] == '[' && line[line.length() - 1] == ']') {
  48. handleSection(line.mid(1, line.length() - 2));
  49. }
  50. }
  51. }
  52. // if(file) delete file;
  53. // if(stream) delete stream;
  54. return true;
  55. }
  56. bool writeToFile(QString fileName) {
  57. file = new QFile(fileName);
  58. if (file->open(QIODevice::WriteOnly | QIODevice::Text)) {
  59. stream = new QTextStream(file);
  60. stream->setCodec("ASCII");
  61. writeOutputToStream();
  62. // strea,
  63. }
  64. if (stream) delete stream;
  65. if (file) delete file;
  66. return true;
  67. }
  68. void writeSection(QString section) {
  69. (*stream) << "[" << section << "]\n";
  70. }
  71. void writeKey(QString key, QString value) {
  72. (*stream) << key << "=" << value << "\n";
  73. }
  74. };
  75. #endif // INIFILE_H
  76. // http://linuxsagas.digitaleagle.net/2013/09/15/synthesizing-an-accapella-song-with-festival-speech-synthesis/