New constructor that accepts c-strings.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4154 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder
2003-07-31 06:57:02 +00:00
parent f7e948c39a
commit 11e12e9088
2 changed files with 75 additions and 58 deletions
@@ -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<const uint8*>(cs0)[0]) {
case 8:
case 254:
{
const uint8 *inputString = reinterpret_cast<const uint8*>(&(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<const uint16*>(&(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()
{
@@ -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 <uint32 length>
CS0String(array<char, length> &cs0);
~CS0String();
void SetTo(const char *cs0, uint32 length);
template <uint32 length>
void SetTo(array<char, length> &cs0);
@@ -38,6 +38,7 @@ public:
CS0String& operator=(array<char, length> &cs0);
const char* String() const { return fUtf8String; }
uint32 Length() const { return fUtf8String ? strlen(fUtf8String) : 0; }
private:
void _Clear();
@@ -60,62 +61,7 @@ template <uint32 length>
void
CS0String::SetTo(array<char, length> &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<uint8*>(&(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<uint16*>(&(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<char*>(cs0.data), length);
}
template <uint32 length>