* Clean up

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26882 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Salvatore Benedetto
2008-08-08 23:27:23 +00:00
parent b328324d19
commit d9409b5cda
2 changed files with 64 additions and 57 deletions
+36 -24
View File
@@ -1,47 +1,56 @@
/*
* Copyright 2003, Tyler Dauwalder, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "DString.h" #include "DString.h"
#include <string.h> #include <string.h>
using namespace Udf; /*! \brief Creates a useless, empty string object. */
/*! \brief Creates a useless, empty string object.
*/
DString::DString() DString::DString()
: fString(NULL) :
, fLength(0) fLength(0),
fString(NULL)
{ {
} }
/*! \brief Create a new DString object that is a copy of \a ref.
*/ /*! \brief Create a new DString object that is a copy of \a ref. */
DString::DString(const DString &ref) DString::DString(const DString &ref)
: fString(NULL) :
, fLength(0) fLength(0),
fString(NULL)
{ {
SetTo(ref); SetTo(ref);
} }
/*! \brief Creates a new DString \a fieldLength bytes long that contains /*! \brief Creates a new DString \a fieldLength bytes long that contains
at most the first \c (fieldLength-1) bytes of \a string.Cs0(). at most the first \c (fieldLength-1) bytes of \a string.Cs0().
*/ */
DString::DString(const Udf::String &string, uint8 fieldLength) DString::DString(const UdfString &string, uint8 fieldLength)
: fString(NULL) :
, fLength(0) fLength(0),
fString(NULL)
{ {
SetTo(string, fieldLength); SetTo(string, fieldLength);
} }
/*! \brief Creates a new DString \a fieldLength bytes long that contains /*! \brief Creates a new DString \a fieldLength bytes long that contains
at most the first \c (fieldLength-1) bytes of the Cs0 representation at most the first \c (fieldLength-1) bytes of the Cs0 representation
of the NULL-terminated UTF8 string \a utf8. of the NULL-terminated UTF8 string \a utf8.
*/ */
DString::DString(const char *utf8, uint8 fieldLength) DString::DString(const char *utf8, uint8 fieldLength)
: fString(NULL) :
, fLength(0) fLength(0),
fString(NULL)
{ {
SetTo(utf8, fieldLength); SetTo(utf8, fieldLength);
} }
void void
DString::SetTo(const DString &ref) DString::SetTo(const DString &ref)
{ {
@@ -55,11 +64,12 @@ DString::SetTo(const DString &ref)
} }
} }
/*! \brief Sets the DString be \a fieldLength bytes long and contain /*! \brief Sets the DString be \a fieldLength bytes long and contain
at most the first \c (fieldLength-1) bytes of \a string.Cs0(). at most the first \c (fieldLength-1) bytes of \a string.Cs0().
*/ */
void void
DString::SetTo(const Udf::String &string, uint8 fieldLength) DString::SetTo(const UdfString &string, uint8 fieldLength)
{ {
_Clear(); _Clear();
if (fieldLength > 0) { if (fieldLength > 0) {
@@ -70,21 +80,21 @@ DString::SetTo(const Udf::String &string, uint8 fieldLength)
// Figure out how many bytes to copy // Figure out how many bytes to copy
uint32 sourceLength = string.Cs0Length(); uint32 sourceLength = string.Cs0Length();
if (sourceLength > 0) { if (sourceLength > 0) {
uint8 destLength = sourceLength > uint8(fieldLength-1) uint8 destLength = sourceLength > uint8(fieldLength - 1)
? uint8(fieldLength-1) ? uint8(fieldLength - 1) : uint8(sourceLength);
: uint8(sourceLength);
// If the source string is 16-bit unicode, make sure any dangling // If the source string is 16-bit unicode, make sure any dangling
// half-character at the end of the string is not copied // half-character at the end of the string is not copied
if (string.Cs0()[1] == '\x10' && destLength > 0 && destLength % 2 == 0) if (string.Cs0()[1] == '\x10' && destLength > 0
&& destLength % 2 == 0)
destLength--; destLength--;
// Copy // Copy
memcpy(fString, string.Cs0(), destLength); memcpy(fString, string.Cs0(), destLength);
// Zero any characters between the end of the string and // Zero any characters between the end of the string and
// the terminating string length character // the terminating string length character
if (destLength < fieldLength-1) if (destLength < fieldLength - 1)
memset(&fString[destLength], 0, fieldLength-1-destLength); memset(&fString[destLength], 0, fieldLength - 1 - destLength);
// Write the string length to the last character in the field // Write the string length to the last character in the field
fString[fieldLength-1] = destLength; fString[fieldLength - 1] = destLength;
} else { } else {
// Empty strings are to contain all zeros // Empty strings are to contain all zeros
memset(fString, 0, fieldLength); memset(fString, 0, fieldLength);
@@ -93,6 +103,7 @@ DString::SetTo(const Udf::String &string, uint8 fieldLength)
} }
} }
/*! \brief Sets the DString be \a fieldLength bytes long and contain /*! \brief Sets the DString be \a fieldLength bytes long and contain
at most the first \c (fieldLength-1) bytes of the Cs0 representation at most the first \c (fieldLength-1) bytes of the Cs0 representation
of the NULL-terminated UTF8 string \a utf8. of the NULL-terminated UTF8 string \a utf8.
@@ -100,10 +111,11 @@ DString::SetTo(const Udf::String &string, uint8 fieldLength)
void void
DString::SetTo(const char *utf8, uint8 fieldLength) DString::SetTo(const char *utf8, uint8 fieldLength)
{ {
Udf::String string(utf8); UdfString string(utf8);
SetTo(string, fieldLength); SetTo(string, fieldLength);
} }
void void
DString::_Clear() DString::_Clear()
{ {
+21 -26
View File
@@ -1,20 +1,18 @@
//---------------------------------------------------------------------- /*
// This software is part of the OpenBeOS distribution and is covered * Copyright 2003, Tyler Dauwalder, [email protected].
// by the OpenBeOS license. * Distributed under the terms of the MIT License.
// */
// Copyright (c) 2003 Tyler Dauwalder, [email protected]
//---------------------------------------------------------------------
#ifndef _D_STRING_H #ifndef _D_STRING_H
#define _D_STRING_H #define _D_STRING_H
#include "kernel_cpp.h"
#include "UdfString.h"
#include "UdfDebug.h" #include "UdfDebug.h"
namespace Udf { #include "UdfString.h"
/*! \brief Fixed-length d-string class that takes a Udf::String as input #include <util/kernel_cpp.h>
/*! \brief Fixed-length d-string class that takes a UdfString as input
and provides a properly formatted ECMA-167 d-string of the given and provides a properly formatted ECMA-167 d-string of the given
field length as ouput. field length as ouput.
@@ -22,26 +20,23 @@ namespace Udf {
*/ */
class DString { class DString {
public: public:
DString(); DString();
DString(const DString &ref); DString(const DString &ref);
DString(const Udf::String &string, uint8 fieldLength); DString(const UdfString &string, uint8 fieldLength);
DString(const char *utf8, uint8 fieldLength); DString(const char *utf8, uint8 fieldLength);
void SetTo(const DString &ref); uint8 Length() const { return fLength; }
void SetTo(const Udf::String &string, uint8 fieldLength);
void SetTo(const char *utf8, uint8 fieldLength);
const uint8* String() const { return fString; } void SetTo(const DString &ref);
uint8 Length() const { return fLength; } void SetTo(const UdfString &string, uint8 fieldLength);
void SetTo(const char *utf8, uint8 fieldLength);
const uint8* String() const { return fString; }
private: private:
void _Clear(); void _Clear();
uint8 *fString; uint8 fLength;
uint8 fLength; uint8 *fString;
}; };
}; // namespace UDF
#endif // _D_STRING_H #endif // _D_STRING_H