//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
//
#include "stdafx.h"
namespace Acf {
//---------------------------------------------------------------------
// struct Byte
//
// Static Methods
/*static*/ byte Byte::Parse(String* s, NumberStyles style)
{
if (s == null)
throw ArgumentNullException();
const wchar_t* format = (style == NumberStyles::HexNumber) ? L"%x" : L"%d";
int value = 0;
int ret = swscanf(s->GetCString(), format, &value);
if (ret == 0 || ret == EOF)
throw FormatException();
if (value > Byte::MaxValue)
throw FormatException();
return (byte)value;
}
/*static*/ StringPtr Byte::ToString(byte value, String* format,
IFormatProvider* provider)
{
wchar_t fmt[10];
if (format == null || format->Length == 0)
{
wcscpy(fmt, L"%d");
}
else
{
fmt[0] = L'%';
wcscpy(fmt + 1, format->GetCString());
}
wchar_t buf[2 * _MAX_INT_DIG];
swprintf(buf, fmt, (int)value);
return new String(buf);
}
/*static*/ StringPtr Byte::ToString(byte value, const wchar_t* format,
IFormatProvider* provider)
{
StringPtr format2 = new String(format);
return ToString(value, format2, provider);
}
// Methods
int Byte::GetHashCode()
{
return this->Value;
}
StringPtr Byte::ToString(String* format, IFormatProvider* provider)
{
return ToString(this->Value, format, provider);
}
StringPtr Byte::ToString(const wchar_t* format, IFormatProvider* provider)
{
return ToString(this->Value, format, provider);
}
//---------------------------------------------------------------------
// class ByteObject
/*override*/ bool ByteObject::Equals(Object* obj)
{
ByteObject* o = dynamic_cast<ByteObject*>(obj);
if (o == null)
return false;
else
return (this->Value == o->Value);
}
/*override*/ int ByteObject::GetHashCode()
{
return this->Value.GetHashCode();
}
/*override*/ StringPtr ByteObject::ToString()
{
return this->Value.ToString();
}
/*virtual*/ StringPtr ByteObject::ToString(String* format, IFormatProvider* provider)
{
return this->Value.ToString(format, provider);
}
} // namespace Acf