Simplify source file names and go from .c to .cpp
[haiku.git] / src / add-ons / kernel / file_systems / udf / UdfString.h
blob8db6127ddfbecea3c4732669a366cb2c04ec6f7d
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>
14 #include "Array.h"
15 #include "UdfDebug.h"
17 #include <string.h>
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.
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(): %ld", dString.length()));
65 SetTo(dString);
68 /*! \brief Assignment from a d-string.
70 The last byte of a d-string specifies the data length of the
71 enclosed Cs0 string.
73 template <uint32 length>
74 void
75 UdfString::SetTo(const array<char, length> &dString)
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);
89 /*! \brief Assignment from a d-string.
91 template <uint32 length>
92 UdfString&
93 UdfString::operator=(const array<char, length> &dString)
95 SetTo(dString);
96 return *this;
99 #endif // _UDF_STRING_H