Skip to content

Commit

Permalink
AURORA: Add LanguageManager::unParseColorCodes()
Browse files Browse the repository at this point in the history
The reverse operation of preParseColorCodes().
  • Loading branch information
DrMcCoy committed Mar 3, 2016
1 parent 254ad67 commit 2b51482
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/aurora/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,87 @@ Common::MemoryReadStream *LanguageManager::preParseColorCodes(Common::SeekableRe
return new Common::MemoryReadStream(output.getData(), output.size(), true);
}

Common::MemoryReadStream *LanguageManager::unParseColorCodes(Common::SeekableReadStream &stream) {
Common::MemoryWriteStreamDynamic output;

output.reserve(stream.size());

int state = 0;

std::vector<byte> collect;
collect.reserve(11);

byte color[8];

byte b;
while (stream.read(&b, 1) == 1) {
if (state == 0) {
if (b == '<') {
collect.push_back(b);
state = 1;
} else
output.writeByte(b);

continue;
}

if (state == 1) {
if (b == 'c') {
collect.push_back(b);
state = 2;
} else {
output.write(&collect[0], collect.size());
output.writeByte(b);
collect.clear();
state = 0;
}

continue;
}

if ((state >= 2) && (state <= 9)) {
collect.push_back(b);

if ((b >= '0') && (b <= '9')) {
color[state - 2] = b - '0';
state++;
} else if ((b >= 'a') && (b <= 'f')) {
color[state - 2] = (b - 'a') + 10;
state++;
} else if ((b >= 'A') && (b <= 'F')) {
color[state - 2] = (b - 'A') + 10;
state++;
} else {
output.write(&collect[0], collect.size());
collect.clear();
state = 0;
}

continue;
}

if (state == 10) {
if (b == '>') {
output.writeString("<c");
output.writeByte((color[0] << 4) + color[1]);
output.writeByte((color[2] << 4) + color[3]);
output.writeByte((color[4] << 4) + color[5]);
output.writeString(">");
collect.clear();
state = 0;

} else {
output.write(&collect[0], collect.size());
output.writeByte(b);
collect.clear();
state = 0;
}

continue;
}
}

return new Common::MemoryReadStream(output.getData(), output.size(), true);
}

} // End of namespace Aurora
8 changes: 8 additions & 0 deletions src/aurora/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ class LanguageManager : public Common::Singleton<LanguageManager> {
* inside multibyte sequences. This is reasonable likely to never happen.
*/
static Common::MemoryReadStream *preParseColorCodes(Common::SeekableReadStream &stream);

/** Reverse the pre-parsing and fixing of color codes.
*
* This turns the data back into the original, broken Aurora format.
*
* See also preParseColorCodes().
*/
static Common::MemoryReadStream *unParseColorCodes(Common::SeekableReadStream &stream);
// '---

private:
Expand Down

0 comments on commit 2b51482

Please sign in to comment.