| 1 | /* |
| 2 | * File: ConfigFile.h |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This object manages the configuration file |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #ifndef CONFIG_FILE |
| 28 | #define CONFIG_FILE |
| 29 | |
| 30 | #include "SystemConfigLine.h" |
| 31 | #include "DeviceConfigLine.h" |
| 32 | |
| 33 | #include <QPlainTextEdit> |
| 34 | #include <QTextBlock> |
| 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
| 38 | typedef std::vector<ConfigLine> FileArray; |
| 39 | #define MAX_CONFIG_LINE 512 |
| 40 | |
| 41 | struct SynonymTableEntry |
| 42 | { |
| 43 | std::string keyword1; |
| 44 | std::string keyword2; |
| 45 | }; |
| 46 | |
| 47 | class ConfigFile |
| 48 | { |
| 49 | public: |
| 50 | ConfigFile(const std::string& fileName); |
| 51 | virtual ~ConfigFile(); |
| 52 | |
| 53 | void initialize(); |
| 54 | void updateEditor(QPlainTextEdit& document); |
| 55 | const std::string& getFileName() const; |
| 56 | SystemConfigLine * operator[] (int index) const; |
| 57 | DeviceConfigLine * getDevice(int index) const; |
| 58 | DeviceConfigLine * getDevice(const std::string& devno) const; |
| 59 | DeviceConfigLine * findFirstDeviceByType(const char * type) const; |
| 60 | int numberOfDevices(); |
| 61 | bool write(const std::string& fileName); |
| 62 | bool write(); |
| 63 | int size() const; |
| 64 | void addNonDev(SystemConfigLine * configLine); |
| 65 | void addDev(DeviceConfigLine * configLine); |
| 66 | void appendNewLines(); |
| 67 | const ConfigLine * locateLine(const std::string& keyword, bool create=false, bool synonyms=true); |
| 68 | void deleteLine(int index); |
| 69 | inline int getLastSys() { return mLastSys; }; |
| 70 | bool changed(); |
| 71 | inline bool isNew() const {return mNewConfig;}; |
| 72 | void setNew(bool newVal); |
| 73 | |
| 74 | private: |
| 75 | enum BuildType |
| 76 | { |
| 77 | File=1, |
| 78 | TextEdit |
| 79 | }; |
| 80 | |
| 81 | std::string mFileName; |
| 82 | FileArray mFileArray; |
| 83 | std::vector<SystemConfigLine> mNewLines; |
| 84 | std::vector<DeviceConfigLine> mNewDeviceLines; |
| 85 | static struct SynonymTableEntry mSynonyms[]; |
| 86 | FILE * mFile; |
| 87 | QPlainTextEdit * mEditor; |
| 88 | QTextBlock mEditorIterator; |
| 89 | |
| 90 | int mLastSys; // last line before device statements |
| 91 | bool mChanged; // updates were done to file which were not saved yet |
| 92 | bool mNewConfig; // never saved |
| 93 | static const char *configTemplate[]; // template for new config file |
| 94 | int templateCounter; // line number to read from template |
| 95 | |
| 96 | void openTemplate(); |
| 97 | char * getNextLine(char * buff, int len, BuildType type); |
| 98 | |
| 99 | void buildConfig(BuildType); |
| 100 | }; |
| 101 | |
| 102 | #endif |