1*3871cd30STyler Dauwalder #include "DString.h" 2*3871cd30STyler Dauwalder 3*3871cd30STyler Dauwalder #include <string.h> 4*3871cd30STyler Dauwalder 5*3871cd30STyler Dauwalder using namespace Udf; 6*3871cd30STyler Dauwalder 7*3871cd30STyler Dauwalder /*! \brief Creates a useless, empty string object. 8*3871cd30STyler Dauwalder */ 9*3871cd30STyler Dauwalder DString::DString() 10*3871cd30STyler Dauwalder : fString(NULL) 11*3871cd30STyler Dauwalder , fLength(0) 12*3871cd30STyler Dauwalder { 13*3871cd30STyler Dauwalder } 14*3871cd30STyler Dauwalder 15*3871cd30STyler Dauwalder /*! \brief Create a new DString object that is a copy of \a ref. 16*3871cd30STyler Dauwalder */ 17*3871cd30STyler Dauwalder DString::DString(const DString &ref) 18*3871cd30STyler Dauwalder : fString(NULL) 19*3871cd30STyler Dauwalder , fLength(0) 20*3871cd30STyler Dauwalder { 21*3871cd30STyler Dauwalder SetTo(ref); 22*3871cd30STyler Dauwalder } 23*3871cd30STyler Dauwalder 24*3871cd30STyler Dauwalder /*! \brief Creates a new DString \a fieldLength bytes long that contains 25*3871cd30STyler Dauwalder at most the first \c (fieldLength-1) bytes of \a string.Cs0(). 26*3871cd30STyler Dauwalder */ 27*3871cd30STyler Dauwalder DString::DString(const Udf::String &string, uint8 fieldLength) 28*3871cd30STyler Dauwalder : fString(NULL) 29*3871cd30STyler Dauwalder , fLength(0) 30*3871cd30STyler Dauwalder { 31*3871cd30STyler Dauwalder SetTo(string, fieldLength); 32*3871cd30STyler Dauwalder } 33*3871cd30STyler Dauwalder 34*3871cd30STyler Dauwalder /*! \brief Creates a new DString \a fieldLength bytes long that contains 35*3871cd30STyler Dauwalder at most the first \c (fieldLength-1) bytes of the Cs0 representation 36*3871cd30STyler Dauwalder of the NULL-terminated UTF8 string \a utf8. 37*3871cd30STyler Dauwalder */ 38*3871cd30STyler Dauwalder DString::DString(const char *utf8, uint8 fieldLength) 39*3871cd30STyler Dauwalder : fString(NULL) 40*3871cd30STyler Dauwalder , fLength(0) 41*3871cd30STyler Dauwalder { 42*3871cd30STyler Dauwalder SetTo(utf8, fieldLength); 43*3871cd30STyler Dauwalder } 44*3871cd30STyler Dauwalder 45*3871cd30STyler Dauwalder void 46*3871cd30STyler Dauwalder DString::SetTo(const DString &ref) 47*3871cd30STyler Dauwalder { 48*3871cd30STyler Dauwalder _Clear(); 49*3871cd30STyler Dauwalder if (ref.Length() > 0) { 50*3871cd30STyler Dauwalder fString = new(nothrow) uint8[ref.Length()]; 51*3871cd30STyler Dauwalder if (fString) { 52*3871cd30STyler Dauwalder fLength = ref.Length(); 53*3871cd30STyler Dauwalder memcpy(fString, ref.String(), fLength); 54*3871cd30STyler Dauwalder } 55*3871cd30STyler Dauwalder } 56*3871cd30STyler Dauwalder } 57*3871cd30STyler Dauwalder 58*3871cd30STyler Dauwalder /*! \brief Sets the DString be \a fieldLength bytes long and contain 59*3871cd30STyler Dauwalder at most the first \c (fieldLength-1) bytes of \a string.Cs0(). 60*3871cd30STyler Dauwalder */ 61*3871cd30STyler Dauwalder void 62*3871cd30STyler Dauwalder DString::SetTo(const Udf::String &string, uint8 fieldLength) 63*3871cd30STyler Dauwalder { 64*3871cd30STyler Dauwalder _Clear(); 65*3871cd30STyler Dauwalder if (fieldLength > 0) { 66*3871cd30STyler Dauwalder // Allocate our string 67*3871cd30STyler Dauwalder fString = new(nothrow) uint8[fieldLength]; 68*3871cd30STyler Dauwalder status_t error = fString ? B_OK : B_NO_MEMORY; 69*3871cd30STyler Dauwalder if (!error) { 70*3871cd30STyler Dauwalder // Figure out how many bytes to copy 71*3871cd30STyler Dauwalder uint32 sourceLength = string.Cs0Length(); 72*3871cd30STyler Dauwalder if (sourceLength > 0) { 73*3871cd30STyler Dauwalder uint8 destLength = sourceLength > uint8(fieldLength-1) 74*3871cd30STyler Dauwalder ? uint8(fieldLength-1) 75*3871cd30STyler Dauwalder : uint8(sourceLength); 76*3871cd30STyler Dauwalder // If the source string is 16-bit unicode, make sure any dangling 77*3871cd30STyler Dauwalder // half-character at the end of the string is not copied 78*3871cd30STyler Dauwalder if (string.Cs0()[1] == '\x10' && destLength > 0 && destLength % 2 == 0) 79*3871cd30STyler Dauwalder destLength--; 80*3871cd30STyler Dauwalder // Copy 81*3871cd30STyler Dauwalder memcpy(fString, string.Cs0(), destLength); 82*3871cd30STyler Dauwalder // Zero any characters between the end of the string and 83*3871cd30STyler Dauwalder // the terminating string length character 84*3871cd30STyler Dauwalder if (destLength < fieldLength-1) 85*3871cd30STyler Dauwalder memset(&fString[destLength], 0, fieldLength-1-destLength); 86*3871cd30STyler Dauwalder // Write the string length to the last character in the field 87*3871cd30STyler Dauwalder fString[fieldLength-1] = destLength; 88*3871cd30STyler Dauwalder } else { 89*3871cd30STyler Dauwalder // Empty strings are to contain all zeros 90*3871cd30STyler Dauwalder memset(fString, 0, fieldLength); 91*3871cd30STyler Dauwalder } 92*3871cd30STyler Dauwalder } 93*3871cd30STyler Dauwalder } 94*3871cd30STyler Dauwalder } 95*3871cd30STyler Dauwalder 96*3871cd30STyler Dauwalder /*! \brief Sets the DString be \a fieldLength bytes long and contain 97*3871cd30STyler Dauwalder at most the first \c (fieldLength-1) bytes of the Cs0 representation 98*3871cd30STyler Dauwalder of the NULL-terminated UTF8 string \a utf8. 99*3871cd30STyler Dauwalder */ 100*3871cd30STyler Dauwalder void 101*3871cd30STyler Dauwalder DString::SetTo(const char *utf8, uint8 fieldLength) 102*3871cd30STyler Dauwalder { 103*3871cd30STyler Dauwalder Udf::String string(utf8); 104*3871cd30STyler Dauwalder SetTo(string, fieldLength); 105*3871cd30STyler Dauwalder } 106*3871cd30STyler Dauwalder 107*3871cd30STyler Dauwalder void 108*3871cd30STyler Dauwalder DString::_Clear() 109*3871cd30STyler Dauwalder { 110*3871cd30STyler Dauwalder DEBUG_INIT("DString"); 111*3871cd30STyler Dauwalder delete [] fString; 112*3871cd30STyler Dauwalder fString = NULL; 113*3871cd30STyler Dauwalder fLength = 0; 114*3871cd30STyler Dauwalder } 115