|
| 1 | +#include "ConfigFile.hpp" |
| 2 | +#include <stdlib.h> |
| 3 | +#include "Logger.hpp" |
| 4 | +//#include "util/StringBuffer.hpp" |
| 5 | + |
| 6 | +namespace botwsavs::fs { |
| 7 | + |
| 8 | +bool ConfigFile::Save() { |
| 9 | + if (!mFile.Open()) { |
| 10 | + return false; |
| 11 | + } |
| 12 | + |
| 13 | + if (!mFile.Clear()) { |
| 14 | + mFile.Close(); |
| 15 | + return false; |
| 16 | + } |
| 17 | + mSuccess = true; |
| 18 | + mMode = Mode::Save; |
| 19 | + SaveInternal(); |
| 20 | + mFile.Close(); |
| 21 | + return mSuccess; |
| 22 | +} |
| 23 | + |
| 24 | +bool ConfigFile::Load() { |
| 25 | + if (!mFile.Open()) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + mSuccess = true; |
| 29 | + mMode = Mode::Load; |
| 30 | + mBuffer.Clear(); |
| 31 | + LoadInternal(); |
| 32 | + mFile.Close(); |
| 33 | + return mSuccess; |
| 34 | +} |
| 35 | + |
| 36 | +bool ConfigFile::ReadLine(u32* outLineLength) { |
| 37 | + u32 lineLength; |
| 38 | + if (!mBuffer.IndexOf('\n', 0, &lineLength)) { |
| 39 | + // no new line character found, try reading more |
| 40 | + s64 readSize = mFile.Read(mBuffer); |
| 41 | + if (readSize < 0) { |
| 42 | + error("Reached end of file"); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + if (!mBuffer.IndexOf('\n', 0, &lineLength)) { |
| 47 | + // still no new line, probably error |
| 48 | + error("Line is too long and cannot fit in buffer"); |
| 49 | + return false; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + mBuffer.SafeSet(lineLength, '\0'); |
| 54 | + // find a comment character |
| 55 | + u32 commentIndex; |
| 56 | + if (mBuffer.IndexOf('#', 0, &commentIndex)) { |
| 57 | + // remove comment |
| 58 | + mBuffer.SafeSet(commentIndex, '\0'); |
| 59 | + } |
| 60 | + |
| 61 | + *outLineLength = lineLength; |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +bool ConfigFile::WriteIntegerInternal(const char* fieldName, u64 value) { |
| 66 | + if (mMode == Mode::Load) { |
| 67 | + error("Cannot write in load mode"); |
| 68 | + return false; |
| 69 | + } |
| 70 | + mBuffer.Clear(); |
| 71 | + mBuffer.SafeAppendF("0x%016x", value); |
| 72 | + mBuffer.SafeAppendF("# %s\n", fieldName); |
| 73 | + return mFile.Write(mBuffer); |
| 74 | +} |
| 75 | + |
| 76 | +bool ConfigFile::ReadIntegerInternal(u64* outValue) { |
| 77 | + if (mMode == Mode::Save) { |
| 78 | + error("Cannot read in save mode"); |
| 79 | + return false; |
| 80 | + } |
| 81 | + u32 lineLength; |
| 82 | + if (!ReadLine(&lineLength)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + // Convert to hex |
| 87 | + u64 value = strtol(mBuffer.Content(), nullptr, 0); |
| 88 | + mBuffer.SafeDeleteFront(lineLength + 1); |
| 89 | + *outValue = value; |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +bool ConfigFile::WriteStringInternal(const char* fieldName, const char* string) { |
| 94 | + if (mMode == Mode::Load) { |
| 95 | + error("Cannot write in load mode"); |
| 96 | + return false; |
| 97 | + } |
| 98 | + mBuffer.Clear(); |
| 99 | + mBuffer.SafeAppendF("%s", string); |
| 100 | + mBuffer.SafeAppendF("# %s\n", fieldName); |
| 101 | + return mFile.Write(mBuffer); |
| 102 | +} |
| 103 | + |
| 104 | +bool ConfigFile::ReadStringInternal(char* outString, const u32 bufferLength) { |
| 105 | + if (mMode == Mode::Save) { |
| 106 | + error("Cannot read in save mode"); |
| 107 | + return false; |
| 108 | + } |
| 109 | + u32 lineLength; |
| 110 | + if (!ReadLine(&lineLength)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + strncpy(outString, mBuffer.Content(), bufferLength); |
| 114 | + mBuffer.SafeDeleteFront(lineLength + 1); |
| 115 | + outString[bufferLength - 1] = '\0'; |
| 116 | + |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace botwsavs::fs |
0 commit comments