
/*
  File: include/mia/MID.h
  Description: MIA unique ID definitions
  Part of the Mantis project
  (c) 2002 Lycos Inc.

  This file is distributed for the sole purpose of allowing you to write
  software to interface with Sonique 2 and other Mantis programs. It is
  not to be modified or used for any other purpose without written
  authorization.

  If you redistribute this file, it must be in the original package
  complete with all other files provided, including the usage agreement.

  Do not remove this notice.
*/

#ifndef _MID_H_
#define _MID_H_

#ifdef DEBUG_INCLUDE_ORDER
#pragma message("Including " __FILE__ "\n" )
#endif

#if _MSC_VER > 1000
#pragma once
#endif

#include "charstring.h"

class MID{          // size is 16
	public:
	    unsigned long	Data1;
	    unsigned short	Data2;
	    unsigned short	Data3;
	    unsigned char	Data4[8];

	    inline bool operator==( const MID &other ) const
	    {
			if(other.Data1 != Data1)
				return(false);

			if(other.Data2 != Data2)
				return(false);
			if(other.Data3 != Data3)
				return(false);

			if(other.Data4[0] != Data4[0])
				return(false);
			if(other.Data4[1] != Data4[1])
				return(false);
			if(other.Data4[2] != Data4[2])
				return(false);
			if(other.Data4[3] != Data4[3])
				return(false);
			if(other.Data4[4] != Data4[4])
				return(false);
			if(other.Data4[5] != Data4[5])
				return(false);
			if(other.Data4[6] != Data4[6])
				return(false);
			if(other.Data4[7] != Data4[7])
				return(false);

			return(true);

	    }

	    inline bool operator!=( const MID &other ) const
	    {
			if(other.Data1 != Data1)
				return(true);

			if(other.Data2 != Data2)
				return(true);
			if(other.Data3 != Data3)
				return(true);

			if(other.Data4[0] != Data4[0])
				return(true);
			if(other.Data4[1] != Data4[1])
				return(true);
			if(other.Data4[2] != Data4[2])
				return(true);
			if(other.Data4[3] != Data4[3])
				return(true);
			if(other.Data4[4] != Data4[4])
				return(true);
			if(other.Data4[5] != Data4[5])
				return(true);
			if(other.Data4[6] != Data4[6])
				return(true);
			if(other.Data4[7] != Data4[7])
				return(true);

			return(false);

	    }

		void SetZero()
		{
			memset( this, 0, sizeof(*this) );
		}

		AString PrintA() const
		{
			char szMid[40];
			sprint( szMid,
					lenof(szMid),
					"%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
					Data1,
					Data2,
					Data3,
					Data4[0],Data4[1],
					Data4[2],Data4[3],Data4[4],Data4[5],Data4[6],Data4[7]);
			return szMid;
		}
		WString PrintW() const
		{
			wchar_t wsMid[40];
			swprint(wsMid,
					lenof(wsMid),
					L"%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
					Data1,
					Data2,
					Data3,
					Data4[0],Data4[1],
					Data4[2],Data4[3],Data4[4],Data4[5],Data4[6],Data4[7]);
			return wsMid;
		}
		bool ReadFromString(const char* asString)
		{
			if( asString && *asString )
			{
				char buf[sizeof(MID)+sizeof(int)];

				MID& guid = *(MID*)buf;

				if( 11 == 	sscanf(	asString,
							"%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
							&guid.Data1,
							&guid.Data2,
							&guid.Data3,
							&guid.Data4[0],&guid.Data4[1],
							&guid.Data4[2],&guid.Data4[3],&guid.Data4[4],&guid.Data4[5],&guid.Data4[6],&guid.Data4[7])
					)
				{
					*this = guid;
					return true;
				}
			}
			return false;
		}
		bool ReadFromString(const wchar_t* wsString)
		{
			return ReadFromString(AString(wsString));
		}
	};


#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C    extern "C"
#else
#define EXTERN_C    extern
#endif
#endif

#ifdef INITMID
#define DEFINE_MID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
		EXTERN_C const MID  name \
                = { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }
#else
#define DEFINE_MID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
		EXTERN_C const MID name
#endif // INITMID



#ifdef DEBUG_INCLUDE_ORDER
#pragma message("Done including " __FILE__ "\n" )
#endif



#endif//_MID_H_

