Skip to content

Commit

Permalink
Added a simple type handler for 0x15 (appvar)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed May 1, 2017
1 parent ca759c1 commit fb53eb0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TIVarTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace tivars
insertType("RecallWindow", 0x10, {"82z", "83z", "8xz", "8xz", "8xz", "8xz", "8xz"});
insertType("TableRange", 0x11, {"82t", "83t", "8xt", "8xt", "8xt", "8xt", "8xt"});
insertType("Backup", 0x13, {"82b", "83b", "" , "8xb", "8cb", "" , "" });
insertType("AppVar", 0x15, { "" , "" , "" , "8xv", "8xv", "8xv", "8xv"});
insertType("AppVar", 0x15, { "" , "" , "" , "8xv", "8xv", "8xv", "8xv"}, make_handler_pair(TH_0x15) );
insertType("TemporaryItem", 0x16, { "" , "" , "" , "" , "" , "" , "" });
insertType("GroupObject", 0x17, {"82g", "83g", "8xg", "8xg", "8xg", "8cg", "8cg"});
insertType("RealFration", 0x18, { "" , "" , "" , "8xn", "8xn", "8xn", "8xn"});
Expand Down
62 changes: 62 additions & 0 deletions src/TypeHandlers/TH_0x15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Part of tivars_lib_cpp
* (C) 2015-2017 Adrien 'Adriweb' Bertrand
* https://github.com/adriweb/tivars_lib_cpp
* License: MIT
*/

#include "TypeHandlers.h"
#include "../utils.h"

using namespace std;

namespace tivars
{

data_t TH_0x15::makeDataFromString(const string& str, const options_t& options)
{
(void)options;

bool formatOk = regex_match(str, regex("^([0-9a-fA-F]{2})+$"));

const size_t length = str.size();
const size_t bytes = length / 2;

if (length == 0 || !formatOk || bytes > 0xFFFF)
{
throw new invalid_argument("Invalid input string. Needs to be a valid hex data block");
}

data_t data = { (uchar)(bytes & 0xFF), (uchar)((bytes >> 8) & 0xFF) };

for (size_t i = 0; i < length; i += 2)
{
data.push_back(hexdec(str.substr(i, 2)));
}

return data;
}

string TH_0x15::makeStringFromData(const data_t& data, const options_t& options)
{
(void)options;

const size_t byteCount = data.size();
const size_t lengthExp = (size_t) ((data[0] & 0xFF) + ((data[1] & 0xFF) << 8));
const size_t lengthDat = byteCount - 2;

if (lengthExp != lengthDat)
{
throw new invalid_argument("Invalid data array. Expected " + to_string(lengthExp) + " bytes, got " + to_string(lengthDat));
}

string str;

for (size_t i=2; i<byteCount; i++)
{
str += strtoupper(dechex(data[i]));
}

return str;
}
}
2 changes: 2 additions & 0 deletions src/TypeHandlers/TypeHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace tivars

namespace TH_0x0D { th(); } // Complex list

namespace TH_0x15 { th(); } // Application variable

namespace TH_0x1B { th(); } // Exact Complex Fraction

namespace TH_0x1C { th(); } // Exact Real Radical
Expand Down
6 changes: 6 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ int main(int argc, char** argv)
assert(TIVarTypes::getIDFromName("ExactRealPi") == 32);


TIVarFile testAppVar = TIVarFile::createNew(TIVarType::createFromName("AppVar"), "TEST");
testAppVar.setContentFromString("ABCD1234C9C8C7C6"); // random but valid hex string
assert(testAppVar.getReadableContent() == "ABCD1234C9C8C7C6");
assert(testAppVar.getRawContent().size() == strlen("ABCD1234C9C8C7C6") / 2 + 2);
testAppVar.saveVarToFile("assets/testData", "testAVnew");

TIVarFile testString = TIVarFile::loadFromFile("assets/testData/String.8xs");
assert(testString.getReadableContent() == "Hello World");

Expand Down

0 comments on commit fb53eb0

Please sign in to comment.