1//---------------------------------------------------------------------- 2// This software is part of the OpenBeOS distribution and is covered 3// by the OpenBeOS license. 4// 5// Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net 6//--------------------------------------------------------------------- 7 8#ifndef _UDF_STRING_H 9#define _UDF_STRING_H 10 11#include <stdio.h> 12 13 14#include "Array.h" 15#include "UdfDebug.h" 16 17#include <string.h> 18 19#include <util/kernel_cpp.h> 20/*! \brief UdfString class that takes as input either a UTF8 string or a 21 CS0 unicode string and then provides access to said string in both 22 formats. 23 24 For CS0 info, see: ECMA-167 1/7.2.2 (not very helpful), UDF-2.01 2.1.1 25*/ 26class UdfString { 27public: 28 UdfString(); 29 UdfString(const char *utf8); 30 UdfString(const char *cs0, uint32 length); 31 32 template <uint32 length> 33 UdfString(const array<char, length> &dString); 34 ~UdfString(); 35 36 void SetTo(const char *utf8); 37 void SetTo(const char *cs0, uint32 length); 38 template <uint32 length> 39 void SetTo(const array<char, length> &dString); 40 41 template <uint32 length> 42 UdfString& operator=(const array<char, length> &dString); 43 44 const char *Cs0() const { return fCs0String; } 45 const char *Utf8() const { return fUtf8String; } 46 uint32 Cs0Length() const { return fCs0Length; } 47 uint32 Utf8Length() const { return fUtf8String ? strlen(fUtf8String) : 0; } 48 49private: 50 void _Clear(); 51 52 char *fCs0String; 53 uint32 fCs0Length; 54 char *fUtf8String; 55}; 56 57/*! \brief Creates a new UdfString object from the given d-string. 58*/ 59template <uint32 length> 60UdfString::UdfString(const array<char, length> &dString) 61 : fCs0String(NULL) 62 , fUtf8String(NULL) 63{ 64 TRACE(("UdfString::UdfString: dString.length(): %ld", dString.length())); 65 SetTo(dString); 66} 67 68/*! \brief Assignment from a d-string. 69 70 The last byte of a d-string specifies the data length of the 71 enclosed Cs0 string. 72*/ 73template <uint32 length> 74void 75UdfString::SetTo(const array<char, length> &dString) 76{ 77 uint8 dataLength = dString.length() == 0 78 ? 0 : reinterpret_cast<const uint8 *>(dString.data)[dString.length() - 1]; 79 if (dataLength == 0 80 || dataLength == 1 /* technically illegal, but... */) { 81 SetTo(NULL); 82 } else { 83 if (dataLength > dString.length() - 1) 84 dataLength = dString.length() - 1; 85 SetTo(reinterpret_cast<const char *>(dString.data), dataLength); 86 } 87} 88 89/*! \brief Assignment from a d-string. 90*/ 91template <uint32 length> 92UdfString& 93UdfString::operator=(const array<char, length> &dString) 94{ 95 SetTo(dString); 96 return *this; 97} 98 99#endif // _UDF_STRING_H 100