diff --git a/MString.cpp b/MString.cpp index b9aca9d..8b11859 100644 --- a/MString.cpp +++ b/MString.cpp @@ -1,6 +1,7 @@ #include "MString.h" #include "Log.h" #include "uuid/uuid.h" +#include "Exception.h" #include #include #include @@ -279,5 +280,130 @@ namespace coreutils { int MString::offset() { return cursor - data; } + + MString MString::toBinary() { + push(); + reset(); + MString target; + char temp; + while(!eod()) { + temp = nextChar(); + if(strchr("\\'\".\0\r\n", temp)) + target.write('\\'); + target.write(temp); + } + pop(); + return target; + } + MString MString::fromBinary() { + push(); + reset(); + MString target; + while(!eod()) { + if(ifNext("\\r")) + target.write(13); + else if(ifNext("\\n")) + target.write(10); + else if(ifNext("\\0")) + target.write(0); + else if(ifNext("\\\\")) + target.write("\\"); + else if(ifNext("\\.")) + target.write("."); + else if(ifNext("\\\"")) + target.write("\""); + else if(ifNext("\\\'")) + target.write("'"); + else + target.write(nextChar()); + } + pop(); + return target; + } + + MString MString::toHex() { + push(); + reset(); + MString target; + char temp; + while(!eod()) { + temp = nextChar(); + char temp2 = temp; + temp >>= 4; + target.write(hexChar(temp)); + target.write(hexChar(temp2)); + } + pop(); + return target; + } + + MString MString::fromHex() { + push(); + reset(); + MString target; + while(!eod()) { + char ch1 = nextChar(); + ch1 -= 48; + if(ch1 > 9) + ch1 -= 7; + ch1 <<= 4; + ch1 &= 240; + if(eod()) + coreutils::Exception("conversion from hex requires even number of characters."); + char ch2 = nextChar(); + ch2 -= 48; + if(ch2 > 9) + ch2 -= 7; + ch2 &= 15; + ch1 |= ch2; + target.write(ch1); + } + pop(); + return target; + } + + MString MString::toBase64() { + MString target; + return target; + } + + + MString MString::fromBase64() { + MString target; + return target; + } + + + MString MString::toUpper() { + MString target; + return target; + } + + + MString MString::toLower() { + MString target; + return target; + } + + + MString MString::toCGI() { + MString target; + return target; + } + + + MString MString::fromCGI() { + MString target; + return target; + } + + char MString::hexChar(char c) { + c &= 15; + c += 48; + if(c > 57) + c += 7; + return c; + } + } diff --git a/MString.h b/MString.h index dbd05d5..c0fde6d 100644 --- a/MString.h +++ b/MString.h @@ -231,14 +231,81 @@ namespace coreutils { /// MString &read(int fd); + + /// + /// + /// int offset(); + /// + /// + /// + + MString toBinary(); + + /// + /// + /// + + MString fromBinary(); + + /// + /// + /// + + MString toHex(); + + /// + /// + /// + + MString fromHex(); + + /// + /// + /// + + MString toBase64(); + + /// + /// + /// + + MString fromBase64(); + + /// + /// + /// + + MString toUpper(); + + /// + /// + /// + + + MString toLower(); + + /// + /// + /// + + MString toCGI(); + + /// + /// + /// + + MString fromCGI(); + protected: void setSize(int size); private: int bufferSize = 0; + char hexChar(char c); + }; } diff --git a/testing/jstring_test b/testing/jstring_test index f131c2b..8142a0d 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp index 47e0596..051588b 100644 --- a/testing/mstring_test.cpp +++ b/testing/mstring_test.cpp @@ -94,6 +94,9 @@ int main(int argc, char **argv) { int test29 = 402; test28 = test29; std::cout << "int: " << test28 << std::endl; + + coreutils::MString test30 = "ABCDEF"; + std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl; return 0; }