From 7fbbac4a6556ef7c76e00e40edbef263319962ad Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Wed, 13 Nov 2024 14:51:34 -0800 Subject: [PATCH] Laid foundation for modifiers and built a couple. --- Global.cpp | 23 ++++++++++- Modifiers.cpp | 85 +++++++++++++++++++++++++++++++++++++++++ Modifiers.h | 29 ++++++++++++++ tests/testmodifiers.jet | 10 +++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 Modifiers.cpp create mode 100644 Modifiers.h create mode 100755 tests/testmodifiers.jet diff --git a/Global.cpp b/Global.cpp index c580ae2..05fbc56 100644 --- a/Global.cpp +++ b/Global.cpp @@ -1,6 +1,7 @@ #include "Global.h" #include "Exception.h" #include "__mysql.h" +#include "Modifiers.h" #include #include @@ -38,9 +39,29 @@ namespace jet { } coreutils::MString& Global::processModifier(coreutils::MString &value, coreutils::MString &modifier) { + Modifiers modifiers; if(modifier.getLength() == 0) return value; - lastConverted = modifier; + if(modifier == "tobinary") + modifiers.processToBinaryModifier(value, lastConverted); + if(modifier == "frombinary") + modifiers.processFromBinaryModifier(value, lastConverted); + if(modifier == "tohex") + modifiers.processToHexModifier(value, lastConverted); + if(modifier == "fromhex") + modifiers.processFromHexModifier(value, lastConverted); + if(modifier == "tobase64") + modifiers.processToBase64Modifier(value, lastConverted); + if(modifier == "frombase64") + modifiers.processFromBase64Modifier(value, lastConverted); + if(modifier == "toupper") + modifiers.processToUpperModifier(value, lastConverted); + if(modifier == "tolower") + modifiers.processToLowerModifier(value, lastConverted); + if(modifier == "tocgi") + modifiers.processToCGIModifier(value, lastConverted); + if(modifier == "fromcgi") + modifiers.processFromCGIModifier(value, lastConverted); return lastConverted; } diff --git a/Modifiers.cpp b/Modifiers.cpp new file mode 100644 index 0000000..6279213 --- /dev/null +++ b/Modifiers.cpp @@ -0,0 +1,85 @@ +#include "Modifiers.h" +#include "Exception.h" + +namespace jet { + + void Modifiers::processToBinaryModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + value.reset(); + lastConverted = ""; + char temp; + while(!value.eod()) { + temp = value.nextChar(); + if(strchr("\\'\".\0\r\n", temp)) + lastConverted.write('\\'); + lastConverted.write(temp); + } + value.reset(); + } + + void Modifiers::processFromBinaryModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processToHexModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + value.reset(); + lastConverted = ""; + char temp; + while(!value.eod()) { + temp = value.nextChar(); + char temp2 = temp; + temp >>= 4; + lastConverted.write(hexChar(temp)); + lastConverted.write(hexChar(temp2)); + } + value.reset(); + } + + void Modifiers::processFromHexModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + value.reset(); + lastConverted = ""; + while(!value.eod()) { + char ch1 = value.nextChar(); + ch1 -= 48; + if(ch1 > 9) + ch1 -= 7; + ch1 <<= 4; + ch1 &= 240; + if(value.eod()) + coreutils::Exception("conversion from hex requires even number of characters."); + char ch2 = value.nextChar(); + ch2 -= 48; + if(ch2 > 9) + ch2 -= 7; + ch2 &= 15; + ch1 |= ch2; + lastConverted.write(ch1); + } + value.reset(); + } + + void Modifiers::processToBase64Modifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processFromBase64Modifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processToUpperModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processToLowerModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processToCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + void Modifiers::processFromCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted) { + } + + char Modifiers::hexChar(char c) { + c &= 15; + c += 48; + if(c > 57) + c += 7; + return c; + } + +} diff --git a/Modifiers.h b/Modifiers.h new file mode 100644 index 0000000..3c8383f --- /dev/null +++ b/Modifiers.h @@ -0,0 +1,29 @@ +#ifndef __MODIFIERS_H__ +#define __MODIFIERS_H__ + +#include "MString.h" + +namespace jet { + + class Modifiers { + + public: + void processToBinaryModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processFromBinaryModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processToHexModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processFromHexModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processToBase64Modifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processFromBase64Modifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processToUpperModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processToLowerModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processToCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted); + void processFromCGIModifier(coreutils::MString &value, coreutils::MString &lastConverted); + + private: + char hexChar(char c); + + }; + +} + +#endif diff --git a/tests/testmodifiers.jet b/tests/testmodifiers.jet new file mode 100755 index 0000000..07bc6e4 --- /dev/null +++ b/tests/testmodifiers.jet @@ -0,0 +1,10 @@ +#!../jet-2.0 + + + +1) $[tester1;tobinary] +2) $[tester1;tohex] +3) $[tester2;fromhex] +4) $[tester1] +5) $[tester2] + \ No newline at end of file