1//----------------------------------------------------------------------
2//  This software is part of the Haiku distribution and is covered
3//  by the MIT 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 "Array.h"
14#include "UdfDebug.h"
15
16#include <new>
17#include <string.h>
18
19
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(): %" B_PRIu32,
65		dString.length()));
66	SetTo(dString);
67}
68
69/*! \brief Assignment from a d-string.
70
71	The last byte of a d-string specifies the data length of the
72	enclosed Cs0 string.
73*/
74template <uint32 length>
75void
76UdfString::SetTo(const array<char, length> &dString)
77{
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);
87	}
88}
89
90/*! \brief Assignment from a d-string.
91*/
92template <uint32 length>
93UdfString&
94UdfString::operator=(const array<char, length> &dString)
95{
96	SetTo(dString);
97	return *this;
98}
99
100#endif	// _UDF_STRING_H
101