From 11e12e9088bb5360c9075372203b9d5155a3391d Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Thu, 31 Jul 2003 06:57:02 +0000 Subject: [PATCH] New constructor that accepts c-strings. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4154 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kernel/file_systems/udf/CS0String.cpp | 71 +++++++++++++++++++ .../kernel/file_systems/udf/CS0String.h | 62 ++-------------- 2 files changed, 75 insertions(+), 58 deletions(-) diff --git a/src/add-ons/kernel/file_systems/udf/CS0String.cpp b/src/add-ons/kernel/file_systems/udf/CS0String.cpp index 2cdadd76ad..2fc5b857e7 100644 --- a/src/add-ons/kernel/file_systems/udf/CS0String.cpp +++ b/src/add-ons/kernel/file_systems/udf/CS0String.cpp @@ -1,5 +1,8 @@ #include "CS0String.h" +#include "ByteOrder.h" + + /*! \brief Converts the given unicode character to utf8. */ void @@ -32,6 +35,12 @@ CS0String::CS0String() { } +CS0String::CS0String(const char *cs0, uint32 length) + : fUtf8String(NULL) +{ + SetTo(cs0, length); +} + CS0String::~CS0String() { DEBUG_INIT(CF_HELPER | CF_HIGH_VOLUME, "CS0String"); @@ -39,6 +48,68 @@ CS0String::~CS0String() _Clear(); } +void +CS0String::SetTo(const char *cs0, uint32 length) +{ + DEBUG_INIT(CF_HELPER | CF_HIGH_VOLUME, "CS0String"); + + _Clear(); + + // The first byte of the CS0 string is the compression ID. + // - 8: 1 byte characters + // - 16: 2 byte, big endian characters + // - 254: "CS0 expansion is empty and unique", 1 byte characters + // - 255: "CS0 expansion is empty and unique", 2 byte, big endian characters + PRINT(("compression ID: %d\n", cs0[0])); + switch (reinterpret_cast(cs0)[0]) { + case 8: + case 254: + { + const uint8 *inputString = reinterpret_cast(&(cs0[1])); + int32 maxLength = length-1; // Max length of input string in uint8 characters + int32 allocationLength = maxLength*2+1; // Need at most 2 utf8 chars per uint8 char + fUtf8String = new char[allocationLength]; + if (fUtf8String) { + char *outputString = fUtf8String; + + for (int32 i = 0; i < maxLength && inputString[i]; i++) { + unicode_to_utf8(inputString[i], &outputString); + } + outputString[0] = 0; + } else { + PRINT(("new fUtf8String[%ld] allocation failed\n", allocationLength)); + } + + break; + } + + case 16: + case 255: + { + const uint16 *inputString = reinterpret_cast(&(cs0[1])); + int32 maxLength = (length-1) / 2; // Max length of input string in uint16 characters + int32 allocationLength = maxLength*3+1; // Need at most 3 utf8 chars per uint16 char + fUtf8String = new char[allocationLength]; + if (fUtf8String) { + char *outputString = fUtf8String; + + for (int32 i = 0; i < maxLength && inputString[i]; i++) { + unicode_to_utf8(B_BENDIAN_TO_HOST_INT16(inputString[i]), &outputString); + } + outputString[0] = 0; + } else { + PRINT(("new fUtf8String[%ld] allocation failed\n", allocationLength)); + } + + break; + } + + default: + PRINT(("invalid compression id!\n")); + break; + } +} + void CS0String::_Clear() { diff --git a/src/add-ons/kernel/file_systems/udf/CS0String.h b/src/add-ons/kernel/file_systems/udf/CS0String.h index a34f567281..bef2a990bf 100644 --- a/src/add-ons/kernel/file_systems/udf/CS0String.h +++ b/src/add-ons/kernel/file_systems/udf/CS0String.h @@ -15,8 +15,6 @@ #include "Array.h" #include "UdfDebug.h" -//#include "SupportDefs.h" - namespace Udf { /*! \brief String class that takes as input CS0 unicode strings, @@ -27,10 +25,12 @@ namespace Udf { class CS0String { public: CS0String(); + CS0String(const char *cs0, uint32 length); template CS0String(array &cs0); ~CS0String(); + void SetTo(const char *cs0, uint32 length); template void SetTo(array &cs0); @@ -38,6 +38,7 @@ public: CS0String& operator=(array &cs0); const char* String() const { return fUtf8String; } + uint32 Length() const { return fUtf8String ? strlen(fUtf8String) : 0; } private: void _Clear(); @@ -60,62 +61,7 @@ template void CS0String::SetTo(array &cs0) { - DEBUG_INIT(CF_HELPER | CF_HIGH_VOLUME, "CS0String"); - - _Clear(); - - // The first byte of the CS0 string is the compression ID. - // - 8: 1 byte characters - // - 16: 2 byte, big endian characters - // - 254: "CS0 expansion is empty and unique", 1 byte characters - // - 255: "CS0 expansion is empty and unique", 2 byte, big endian characters - PRINT(("compression ID == %d\n", cs0.data[0])); - switch (cs0.data[0]) { - case 8: - case 254: - { - uint8 *inputString = reinterpret_cast(&(cs0.data[1])); - int32 maxLength = length-1; // Max length of input string in uint8 characters - int32 allocationLength = maxLength*2+1; // Need at most 2 utf8 chars per uint8 char - fUtf8String = new char[allocationLength]; - if (fUtf8String) { - char *outputString = fUtf8String; - - for (int32 i = 0; i < maxLength && inputString[i]; i++) { - unicode_to_utf8(inputString[i], &outputString); - } - outputString[0] = 0; - } else { - PRINT(("new fUtf8String[%ld] allocation failed\n", allocationLength)); - } - - break; - } - - case 16: - case 255: - { - uint16 *inputString = reinterpret_cast(&(cs0.data[1])); - int32 maxLength = (length-1) / 2; // Max length of input string in uint16 characters - int32 allocationLength = maxLength*3+1; // Need at most 3 utf8 chars per uint16 char - fUtf8String = new char[allocationLength]; - if (fUtf8String) { - char *outputString = fUtf8String; - - for (int32 i = 0; i < maxLength && inputString[i]; i++) { - unicode_to_utf8(B_BENDIAN_TO_HOST_INT16(inputString[i]), &outputString); - } - outputString[0] = 0; - } else { - PRINT(("new fUtf8String[%ld] allocation failed\n", allocationLength)); - } - - break; - } - - default: - PRINT(("invalid compression id\n")); - } + SetTo(reinterpret_cast(cs0.data), length); } template