1d40fb303STyler Dauwalder //----------------------------------------------------------------------
2*2ca13760SColdfirex // This software is part of the Haiku distribution and is covered
3b6f76ebeSAugustin Cavalier // by the MIT License.
4d40fb303STyler Dauwalder //
5d40fb303STyler Dauwalder // Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6d40fb303STyler Dauwalder //---------------------------------------------------------------------
7d40fb303STyler Dauwalder
8f8af3d31STyler Dauwalder #ifndef _UDF_STRING_H
9f8af3d31STyler Dauwalder #define _UDF_STRING_H
10d40fb303STyler Dauwalder
11d40fb303STyler Dauwalder #include <stdio.h>
12d40fb303STyler Dauwalder
13d40fb303STyler Dauwalder #include "Array.h"
14d40fb303STyler Dauwalder #include "UdfDebug.h"
15d40fb303STyler Dauwalder
16942cf5fdSAdrien Destugues #include <new>
17f48bfbccSSalvatore Benedetto #include <string.h>
18d40fb303STyler Dauwalder
1949a6a0ebSAdrien Destugues
20f48bfbccSSalvatore Benedetto /*! \brief UdfString class that takes as input either a UTF8 string or a
21f8af3d31STyler Dauwalder CS0 unicode string and then provides access to said string in both
22f8af3d31STyler Dauwalder formats.
23d40fb303STyler Dauwalder
24d40fb303STyler Dauwalder For CS0 info, see: ECMA-167 1/7.2.2 (not very helpful), UDF-2.01 2.1.1
25d40fb303STyler Dauwalder */
26f48bfbccSSalvatore Benedetto class UdfString {
27d40fb303STyler Dauwalder public:
28f48bfbccSSalvatore Benedetto UdfString();
29f48bfbccSSalvatore Benedetto UdfString(const char *utf8);
30f48bfbccSSalvatore Benedetto UdfString(const char *cs0, uint32 length);
31f48bfbccSSalvatore Benedetto
32d40fb303STyler Dauwalder template <uint32 length>
33f48bfbccSSalvatore Benedetto UdfString(const array<char, length> &dString);
34f48bfbccSSalvatore Benedetto ~UdfString();
35d40fb303STyler Dauwalder
36f8af3d31STyler Dauwalder void SetTo(const char *utf8);
37d40fb303STyler Dauwalder void SetTo(const char *cs0, uint32 length);
38d40fb303STyler Dauwalder template <uint32 length>
398c32c267STyler Dauwalder void SetTo(const array<char, length> &dString);
40d40fb303STyler Dauwalder
41d40fb303STyler Dauwalder template <uint32 length>
42f48bfbccSSalvatore Benedetto UdfString& operator=(const array<char, length> &dString);
43d40fb303STyler Dauwalder
Cs0()44cd0f0e99STyler Dauwalder const char *Cs0() const { return fCs0String; }
Utf8()45f8af3d31STyler Dauwalder const char *Utf8() const { return fUtf8String; }
Cs0Length()46cd0f0e99STyler Dauwalder uint32 Cs0Length() const { return fCs0Length; }
Utf8Length()47f8af3d31STyler Dauwalder uint32 Utf8Length() const { return fUtf8String ? strlen(fUtf8String) : 0; }
48d40fb303STyler Dauwalder
49d40fb303STyler Dauwalder private:
50d40fb303STyler Dauwalder void _Clear();
51d40fb303STyler Dauwalder
52f8af3d31STyler Dauwalder char *fCs0String;
53f8af3d31STyler Dauwalder uint32 fCs0Length;
54d40fb303STyler Dauwalder char *fUtf8String;
55d40fb303STyler Dauwalder };
56d40fb303STyler Dauwalder
57f48bfbccSSalvatore Benedetto /*! \brief Creates a new UdfString object from the given d-string.
58f8af3d31STyler Dauwalder */
59d40fb303STyler Dauwalder template <uint32 length>
UdfString(const array<char,length> & dString)60f48bfbccSSalvatore Benedetto UdfString::UdfString(const array<char, length> &dString)
61f8af3d31STyler Dauwalder : fCs0String(NULL)
62f8af3d31STyler Dauwalder , fUtf8String(NULL)
63d40fb303STyler Dauwalder {
647e5b0f96SJérôme Duval TRACE(("UdfString::UdfString: dString.length(): %" B_PRIu32,
657e5b0f96SJérôme Duval dString.length()));
668c32c267STyler Dauwalder SetTo(dString);
67d40fb303STyler Dauwalder }
68d40fb303STyler Dauwalder
698c32c267STyler Dauwalder /*! \brief Assignment from a d-string.
708c32c267STyler Dauwalder
718c32c267STyler Dauwalder The last byte of a d-string specifies the data length of the
728c32c267STyler Dauwalder enclosed Cs0 string.
73f8af3d31STyler Dauwalder */
74d40fb303STyler Dauwalder template <uint32 length>
75d40fb303STyler Dauwalder void
SetTo(const array<char,length> & dString)76f48bfbccSSalvatore Benedetto UdfString::SetTo(const array<char, length> &dString)
77d40fb303STyler Dauwalder {
788c32c267STyler Dauwalder uint8 dataLength = dString.length() == 0
7956b5c637SSalvatore Benedetto ? 0 : reinterpret_cast<const uint8 *>(dString.data)[dString.length() - 1];
808c32c267STyler Dauwalder if (dataLength == 0
8156b5c637SSalvatore Benedetto || dataLength == 1 /* technically illegal, but... */) {
828c32c267STyler Dauwalder SetTo(NULL);
838c32c267STyler Dauwalder } else {
848c32c267STyler Dauwalder if (dataLength > dString.length() - 1)
858c32c267STyler Dauwalder dataLength = dString.length() - 1;
868c32c267STyler Dauwalder SetTo(reinterpret_cast<const char *>(dString.data), dataLength);
878c32c267STyler Dauwalder }
88d40fb303STyler Dauwalder }
89d40fb303STyler Dauwalder
908c32c267STyler Dauwalder /*! \brief Assignment from a d-string.
91f8af3d31STyler Dauwalder */
92d40fb303STyler Dauwalder template <uint32 length>
93f48bfbccSSalvatore Benedetto UdfString&
94f48bfbccSSalvatore Benedetto UdfString::operator=(const array<char, length> &dString)
95d40fb303STyler Dauwalder {
968c32c267STyler Dauwalder SetTo(dString);
97d40fb303STyler Dauwalder return *this;
98d40fb303STyler Dauwalder }
99d40fb303STyler Dauwalder
100f8af3d31STyler Dauwalder #endif // _UDF_STRING_H
101