BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / file_systems / udf / UdfString.h
blob01eef59443abddb1bc471d9b4fdb4e2ba5b6f5de
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 //---------------------------------------------------------------------
8 #ifndef _UDF_STRING_H
9 #define _UDF_STRING_H
11 #include <stdio.h>
13 #include "Array.h"
14 #include "UdfDebug.h"
16 #include <new>
17 #include <string.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.
24 For CS0 info, see: ECMA-167 1/7.2.2 (not very helpful), UDF-2.01 2.1.1
26 class UdfString {
27 public:
28 UdfString();
29 UdfString(const char *utf8);
30 UdfString(const char *cs0, uint32 length);
32 template <uint32 length>
33 UdfString(const array<char, length> &dString);
34 ~UdfString();
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);
41 template <uint32 length>
42 UdfString& operator=(const array<char, length> &dString);
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; }
49 private:
50 void _Clear();
52 char *fCs0String;
53 uint32 fCs0Length;
54 char *fUtf8String;
57 /*! \brief Creates a new UdfString object from the given d-string.
59 template <uint32 length>
60 UdfString::UdfString(const array<char, length> &dString)
61 : fCs0String(NULL)
62 , fUtf8String(NULL)
64 TRACE(("UdfString::UdfString: dString.length(): %" B_PRIu32,
65 dString.length()));
66 SetTo(dString);
69 /*! \brief Assignment from a d-string.
71 The last byte of a d-string specifies the data length of the
72 enclosed Cs0 string.
74 template <uint32 length>
75 void
76 UdfString::SetTo(const array<char, length> &dString)
78 uint8 dataLength = dString.length() == 0
79 ? 0 : reinterpret_cast<const uint8 *>(dString.data)[dString.length() - 1];
80 if (dataLength == 0
81 || dataLength == 1 /* technically illegal, but... */) {
82 SetTo(NULL);
83 } else {
84 if (dataLength > dString.length() - 1)
85 dataLength = dString.length() - 1;
86 SetTo(reinterpret_cast<const char *>(dString.data), dataLength);
90 /*! \brief Assignment from a d-string.
92 template <uint32 length>
93 UdfString&
94 UdfString::operator=(const array<char, length> &dString)
96 SetTo(dString);
97 return *this;
100 #endif // _UDF_STRING_H