xref: /haiku/src/add-ons/kernel/file_systems/udf/DString.cpp (revision 6195b696db6172be1779dbaa2d0ca4f0dbbd879f)
1d9409b5cSSalvatore Benedetto /*
2d9409b5cSSalvatore Benedetto  * Copyright 2003, Tyler Dauwalder, tyler@dauwalder.net.
3d9409b5cSSalvatore Benedetto  * Distributed under the terms of the MIT License.
4d9409b5cSSalvatore Benedetto  */
5d9409b5cSSalvatore Benedetto 
6*6195b696SAxel Dörfler 
73871cd30STyler Dauwalder #include "DString.h"
83871cd30STyler Dauwalder 
93871cd30STyler Dauwalder #include <string.h>
103871cd30STyler Dauwalder 
11*6195b696SAxel Dörfler 
12d9409b5cSSalvatore Benedetto /*! \brief Creates a useless, empty string object. */
DString()133871cd30STyler Dauwalder DString::DString()
14d9409b5cSSalvatore Benedetto 	:
15d9409b5cSSalvatore Benedetto 	fLength(0),
16d9409b5cSSalvatore Benedetto 	fString(NULL)
173871cd30STyler Dauwalder {
183871cd30STyler Dauwalder }
193871cd30STyler Dauwalder 
20d9409b5cSSalvatore Benedetto 
21d9409b5cSSalvatore Benedetto /*! \brief Create a new DString object that is a copy of \a ref. */
DString(const DString & ref)223871cd30STyler Dauwalder DString::DString(const DString &ref)
23d9409b5cSSalvatore Benedetto 	:
24d9409b5cSSalvatore Benedetto 	fLength(0),
25d9409b5cSSalvatore Benedetto 	fString(NULL)
263871cd30STyler Dauwalder {
273871cd30STyler Dauwalder 	SetTo(ref);
283871cd30STyler Dauwalder }
293871cd30STyler Dauwalder 
30d9409b5cSSalvatore Benedetto 
313871cd30STyler Dauwalder /*! \brief Creates a new DString \a fieldLength bytes long that contains
323871cd30STyler Dauwalder 	at most the first \c (fieldLength-1) bytes of \a string.Cs0().
333871cd30STyler Dauwalder */
DString(const UdfString & string,uint8 fieldLength)34d9409b5cSSalvatore Benedetto DString::DString(const UdfString &string, uint8 fieldLength)
35d9409b5cSSalvatore Benedetto 	:
36d9409b5cSSalvatore Benedetto 	fLength(0),
37d9409b5cSSalvatore Benedetto 	fString(NULL)
383871cd30STyler Dauwalder {
393871cd30STyler Dauwalder 	SetTo(string, fieldLength);
403871cd30STyler Dauwalder }
413871cd30STyler Dauwalder 
42d9409b5cSSalvatore Benedetto 
433871cd30STyler Dauwalder /*! \brief Creates a new DString \a fieldLength bytes long that contains
443871cd30STyler Dauwalder 	at most the first \c (fieldLength-1) bytes of the Cs0 representation
453871cd30STyler Dauwalder 	of the NULL-terminated UTF8 string \a utf8.
463871cd30STyler Dauwalder */
DString(const char * utf8,uint8 fieldLength)473871cd30STyler Dauwalder DString::DString(const char *utf8, uint8 fieldLength)
48d9409b5cSSalvatore Benedetto 	:
49d9409b5cSSalvatore Benedetto 	fLength(0),
50d9409b5cSSalvatore Benedetto 	fString(NULL)
513871cd30STyler Dauwalder {
523871cd30STyler Dauwalder 	SetTo(utf8, fieldLength);
533871cd30STyler Dauwalder }
543871cd30STyler Dauwalder 
55d9409b5cSSalvatore Benedetto 
~DString()56*6195b696SAxel Dörfler DString::~DString()
57*6195b696SAxel Dörfler {
58*6195b696SAxel Dörfler 	delete[] fString;
59*6195b696SAxel Dörfler }
60*6195b696SAxel Dörfler 
61*6195b696SAxel Dörfler 
623871cd30STyler Dauwalder void
SetTo(const DString & ref)633871cd30STyler Dauwalder DString::SetTo(const DString &ref)
643871cd30STyler Dauwalder {
653871cd30STyler Dauwalder 	_Clear();
663871cd30STyler Dauwalder 	if (ref.Length() > 0) {
673871cd30STyler Dauwalder 		fString = new(nothrow) uint8[ref.Length()];
68*6195b696SAxel Dörfler 		if (fString != NULL) {
693871cd30STyler Dauwalder 			fLength = ref.Length();
703871cd30STyler Dauwalder 			memcpy(fString, ref.String(), fLength);
713871cd30STyler Dauwalder 		}
723871cd30STyler Dauwalder 	}
733871cd30STyler Dauwalder }
743871cd30STyler Dauwalder 
75d9409b5cSSalvatore Benedetto 
763871cd30STyler Dauwalder /*! \brief Sets the DString be \a fieldLength bytes long and contain
773871cd30STyler Dauwalder 	at most the first \c (fieldLength-1) bytes of \a string.Cs0().
783871cd30STyler Dauwalder */
793871cd30STyler Dauwalder void
SetTo(const UdfString & string,uint8 fieldLength)80d9409b5cSSalvatore Benedetto DString::SetTo(const UdfString &string, uint8 fieldLength)
813871cd30STyler Dauwalder {
823871cd30STyler Dauwalder 	_Clear();
833871cd30STyler Dauwalder 	if (fieldLength > 0) {
843871cd30STyler Dauwalder 		// Allocate our string
853871cd30STyler Dauwalder 		fString = new(nothrow) uint8[fieldLength];
863871cd30STyler Dauwalder 		status_t error = fString ? B_OK : B_NO_MEMORY;
873871cd30STyler Dauwalder 		if (!error) {
883871cd30STyler Dauwalder 			// Figure out how many bytes to copy
893871cd30STyler Dauwalder 			uint32 sourceLength = string.Cs0Length();
903871cd30STyler Dauwalder 			if (sourceLength > 0) {
913871cd30STyler Dauwalder 				uint8 destLength = sourceLength > uint8(fieldLength - 1)
92d9409b5cSSalvatore Benedetto 					? uint8(fieldLength - 1) : uint8(sourceLength);
933871cd30STyler Dauwalder 				// If the source string is 16-bit unicode, make sure any dangling
943871cd30STyler Dauwalder 				// half-character at the end of the string is not copied
95d9409b5cSSalvatore Benedetto 				if (string.Cs0()[1] == '\x10' && destLength > 0
96d9409b5cSSalvatore Benedetto 					&& destLength % 2 == 0)
973871cd30STyler Dauwalder 					destLength--;
983871cd30STyler Dauwalder 				// Copy
993871cd30STyler Dauwalder 				memcpy(fString, string.Cs0(), destLength);
1003871cd30STyler Dauwalder 				// Zero any characters between the end of the string and
1013871cd30STyler Dauwalder 				// the terminating string length character
1023871cd30STyler Dauwalder 				if (destLength < fieldLength - 1)
1033871cd30STyler Dauwalder 					memset(&fString[destLength], 0, fieldLength - 1 - destLength);
1043871cd30STyler Dauwalder 				// Write the string length to the last character in the field
1053871cd30STyler Dauwalder 				fString[fieldLength - 1] = destLength;
1063871cd30STyler Dauwalder 			} else {
1073871cd30STyler Dauwalder 				// Empty strings are to contain all zeros
1083871cd30STyler Dauwalder 				memset(fString, 0, fieldLength);
1093871cd30STyler Dauwalder 			}
1103871cd30STyler Dauwalder 		}
1113871cd30STyler Dauwalder 	}
1123871cd30STyler Dauwalder }
1133871cd30STyler Dauwalder 
114d9409b5cSSalvatore Benedetto 
1153871cd30STyler Dauwalder /*! \brief Sets the DString be \a fieldLength bytes long and contain
1163871cd30STyler Dauwalder 	at most the first \c (fieldLength-1) bytes of the Cs0 representation
1173871cd30STyler Dauwalder 	of the NULL-terminated UTF8 string \a utf8.
1183871cd30STyler Dauwalder */
1193871cd30STyler Dauwalder void
SetTo(const char * utf8,uint8 fieldLength)1203871cd30STyler Dauwalder DString::SetTo(const char *utf8, uint8 fieldLength)
1213871cd30STyler Dauwalder {
122d9409b5cSSalvatore Benedetto 	UdfString string(utf8);
1233871cd30STyler Dauwalder 	SetTo(string, fieldLength);
1243871cd30STyler Dauwalder }
1253871cd30STyler Dauwalder 
126d9409b5cSSalvatore Benedetto 
1273871cd30STyler Dauwalder void
_Clear()1283871cd30STyler Dauwalder DString::_Clear()
1293871cd30STyler Dauwalder {
1303871cd30STyler Dauwalder 	DEBUG_INIT("DString");
1313871cd30STyler Dauwalder 	delete[] fString;
1323871cd30STyler Dauwalder 	fString = NULL;
1333871cd30STyler Dauwalder 	fLength = 0;
1343871cd30STyler Dauwalder }
135