INIFile.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <QTextStream>
  24. #include <QString>
  25. class INIFile
  26. {
  27. QFile* file=nullptr;
  28. QTextStream* stream=nullptr;
  29. protected:
  30. virtual void handleKey(QString key,QString value)=0;
  31. virtual void handleSection(QString section)=0;
  32. virtual bool writeOutputToStream()=0;
  33. virtual QString codec(){return "ASCII";}
  34. public:
  35. INIFile(){}
  36. bool readFromFile(QString fileName)
  37. {
  38. file = new QFile(fileName);
  39. if(file->open(QIODevice::ReadOnly | QIODevice::Text))
  40. {
  41. stream = new QTextStream(file);
  42. stream->setCodec("ASCII");
  43. while (!stream->atEnd()) {
  44. QString line = stream->readLine();
  45. QStringList kv = line.split("=");
  46. if(kv.length()==2)
  47. {
  48. handleKey(kv[0],kv[1]);
  49. }
  50. else if(line[0]=='[' && line[line.length()-1]==']')
  51. {
  52. handleSection(line.mid(1,line.length()-2));
  53. }
  54. }
  55. }
  56. //if(file) delete file;
  57. //if(stream) delete stream;
  58. return true;
  59. }
  60. bool writeToFile(QString fileName)
  61. {
  62. file = new QFile(fileName);
  63. if(file->open(QIODevice::WriteOnly | QIODevice::Text))
  64. {
  65. stream = new QTextStream(file);
  66. stream->setCodec("ASCII");
  67. writeOutputToStream();
  68. //strea,
  69. }
  70. if(stream) delete stream;
  71. if(file) delete file;
  72. return true;
  73. }
  74. void writeSection(QString section)
  75. {
  76. (*stream) << "[" << section << "]\n";
  77. }
  78. void writeKey(QString key, QString value)
  79. {
  80. (*stream) << key << "=" << value <<"\n";
  81. }
  82. };
  83. #endif // INIFILE_H
  84. //http://linuxsagas.digitaleagle.net/2013/09/15/synthesizing-an-accapella-song-with-festival-speech-synthesis/