|
| 1 | +/*==LICENSE==* |
| 2 | +
|
| 3 | +CyanWorlds.com Engine - MMOG client, server and tools |
| 4 | +Copyright (C) 2011 Cyan Worlds, Inc. |
| 5 | +
|
| 6 | +This program 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 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +
|
| 19 | +Additional permissions under GNU GPL version 3 section 7 |
| 20 | +
|
| 21 | +If you modify this Program, or any covered work, by linking or |
| 22 | +combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
| 23 | +NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
| 24 | +JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
| 25 | +(or a modified version of those libraries), |
| 26 | +containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
| 27 | +PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
| 28 | +JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
| 29 | +licensors of this Program grant you additional |
| 30 | +permission to convey the resulting work. Corresponding Source for a |
| 31 | +non-source form of such a combination shall include the source code for |
| 32 | +the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
| 33 | +work. |
| 34 | +
|
| 35 | +You can contact Cyan Worlds, Inc. by email [email protected] |
| 36 | + or by snail mail at: |
| 37 | + Cyan Worlds, Inc. |
| 38 | + 14617 N Newport Hwy |
| 39 | + Mead, WA 99021 |
| 40 | +
|
| 41 | +*==LICENSE==*/ |
| 42 | + |
| 43 | +#include "hsIniHelper.h" |
| 44 | +#include "hsStringTokenizer.h" |
| 45 | + |
| 46 | +hsIniEntry::hsIniEntry(ST::string line): |
| 47 | +fCommand(), fComment() { |
| 48 | + if(line.size() == 0) { |
| 49 | + fType = kBlankLine; |
| 50 | + } else if(line[0] == '#') { |
| 51 | + fType = kComment; |
| 52 | + fComment = line.after_first('#'); |
| 53 | + } else if(line == "\n") { |
| 54 | + fType = kBlankLine; |
| 55 | + } else { |
| 56 | + fType = kCommandValue; |
| 57 | + hsStringTokenizer tokenizer = hsStringTokenizer(line.c_str(), " "); |
| 58 | + char *str; |
| 59 | + int i = 0; |
| 60 | + while((str = tokenizer.next())) { |
| 61 | + if (i==0) { |
| 62 | + fCommand = str; |
| 63 | + } else { |
| 64 | + fValues.push_back(str); |
| 65 | + } |
| 66 | + i++; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void hsIniEntry::setValue(size_t idx, ST::string value) { |
| 72 | + if (fValues.size() >= idx) { |
| 73 | + fValues[idx] = value; |
| 74 | + } else { |
| 75 | + for (int i=fValues.size(); i<idx; i++) { |
| 76 | + fValues.push_back(""); |
| 77 | + } |
| 78 | + fValues.push_back(value); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +std::optional<ST::string> hsIniEntry::getValue(size_t idx) { |
| 83 | + if (fValues.size() < idx) { |
| 84 | + return std::optional<ST::string>(); |
| 85 | + } else { |
| 86 | + return fValues[idx]; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +hsIniFile::hsIniFile(plFileName filename) { |
| 92 | + |
| 93 | + this->filename = filename; |
| 94 | + readFile(); |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +hsIniFile::hsIniFile(hsStream& stream) { |
| 99 | + readStream(stream); |
| 100 | +} |
| 101 | + |
| 102 | +void hsIniFile::readStream(hsStream& stream) { |
| 103 | + ST::string line; |
| 104 | + while(stream.ReadLn(line)) { |
| 105 | + std::shared_ptr<hsIniEntry> entry = std::make_shared<hsIniEntry>(line); |
| 106 | + fEntries.push_back(entry); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +void hsIniFile::writeFile() { |
| 111 | + hsAssert(filename.GetSize() > 0, "writeFile requires contructor with filename"); |
| 112 | + |
| 113 | + hsBufferedStream s; |
| 114 | + s.Open(filename, "w"); |
| 115 | + writeStream(s); |
| 116 | + s.Close(); |
| 117 | +} |
| 118 | + |
| 119 | +void hsIniFile::readFile() { |
| 120 | + hsAssert(filename.GetSize() > 0, "writeFile requires contructor with filename"); |
| 121 | + |
| 122 | + fEntries.clear(); |
| 123 | + |
| 124 | + hsBufferedStream s; |
| 125 | + s.Open(filename); |
| 126 | + readStream(s); |
| 127 | + s.Close(); |
| 128 | +} |
| 129 | + |
| 130 | +void hsIniFile::writeFile(plFileName filename) { |
| 131 | + hsBufferedStream s; |
| 132 | + s.Open(filename, "w"); |
| 133 | + writeStream(s); |
| 134 | + s.Close(); |
| 135 | +} |
| 136 | + |
| 137 | +void hsIniFile::writeStream(hsStream& stream) { |
| 138 | + for (std::shared_ptr<hsIniEntry> entry: fEntries) { |
| 139 | + switch (entry->fType) { |
| 140 | + case hsIniEntry::kBlankLine: |
| 141 | + stream.WriteSafeString("\n"); |
| 142 | + break; |
| 143 | + case hsIniEntry::kComment: |
| 144 | + stream.WriteSafeString("#" + entry.get()->fComment + "\n"); |
| 145 | + break; |
| 146 | + case hsIniEntry::kCommandValue: |
| 147 | + ST::string line = entry->fCommand; |
| 148 | + for (ST::string value: entry->fValues) { |
| 149 | + line += " " + value; |
| 150 | + } |
| 151 | + line += "\n"; |
| 152 | + stream.WriteString(line); |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +std::shared_ptr<hsIniEntry> hsIniFile::findByCommand(ST::string command) { |
| 159 | + for (std::shared_ptr<hsIniEntry> entry: fEntries) { |
| 160 | + if(entry->fCommand == command) { |
| 161 | + return entry; |
| 162 | + } |
| 163 | + } |
| 164 | + return std::shared_ptr<hsIniEntry>(nullptr); |
| 165 | +} |
0 commit comments