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 #include "kernel_cpp.h" 14 15 #include "Array.h" 16 #include "UdfDebug.h" 17 18 namespace Udf { 19 20 /*! \brief String 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 */ 26 class String { 27 public: 28 String(); 29 String(const char *utf8); 30 String(const char *cs0, uint32 length); 31 template <uint32 length> 32 String(const array<char, length> &dString); 33 ~String(); 34 35 void SetTo(const char *utf8); 36 void SetTo(const char *cs0, uint32 length); 37 template <uint32 length> 38 void SetTo(const array<char, length> &dString); 39 40 template <uint32 length> 41 String& operator=(const array<char, length> &dString); 42 43 const char* Cs0() const { return fCs0String; } 44 const char* Utf8() const { return fUtf8String; } 45 uint32 Cs0Length() const { return fCs0Length; } 46 uint32 Utf8Length() const { return fUtf8String ? strlen(fUtf8String) : 0; } 47 48 private: 49 void _Clear(); 50 51 char *fCs0String; 52 uint32 fCs0Length; 53 char *fUtf8String; 54 }; 55 56 /*! \brief Creates a new String object from the given d-string. 57 */ 58 template <uint32 length> 59 String::String(const array<char, length> &dString) 60 : fCs0String(NULL) 61 , fUtf8String(NULL) 62 { 63 DEBUG_INIT_ETC("String", ("dString.length(): %ld", dString.length())); 64 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 */ 73 template <uint32 length> 74 void 75 String::SetTo(const array<char, length> &dString) 76 { 77 uint8 dataLength = dString.length() == 0 78 ? 0 79 : reinterpret_cast<const uint8*>(dString.data)[dString.length()-1]; 80 if (dataLength == 0 81 || dataLength == 1 /* technically illegal, but... */) 82 { 83 SetTo(NULL); 84 } else { 85 if (dataLength > dString.length()-1) 86 dataLength = dString.length()-1; 87 SetTo(reinterpret_cast<const char*>(dString.data), dataLength); 88 } 89 } 90 91 /*! \brief Assignment from a d-string. 92 */ 93 template <uint32 length> 94 String& 95 String::operator=(const array<char, length> &dString) 96 { 97 SetTo(dString); 98 return *this; 99 } 100 101 102 }; // namespace UDF 103 104 105 106 #endif // _UDF_STRING_H 107