#ifndef __MISC_H_
#define __MISC_H_

#include "typedef.h"
//#include "lang\resource.h"

void misc_SortStringArray(CStringArray& ar, CStringArray* ar2);

BOOL dispatchMessages();

CString misc_get_str(UINT res);
void	misc_Message(LPCTSTR msg);
CString misc_VariantToStr(const COleVariant& var);
int		misc_VariantToInt(const COleVariant& var);
double	misc_VariantToDouble(const COleVariant& var);
COleVariant misc_StrToVariant(LPCTSTR str, short type, long size = 0);

extern CString g_sDir;
CString misc_GetModuleDir();

#define DELETE_MAP_VALS(KEY,VAL,MAP)		\
{											\
	POSITION p = (MAP).GetStartPosition();	\
	KEY	key;								\
	VAL val;								\
	while (p)								\
	{										\
		(MAP).GetNextAssoc(p, key, val);	\
		delete val;							\
	}										\
	(MAP).RemoveAll();						\
}

#define DELETE_LIST_VALS(VAL,LIST)			\
{											\
	POSITION p = (LIST).GetHeadPosition();	\
	VAL val;								\
	while (p)								\
	{										\
		val = (LIST).GetNext(p);			\
		delete val;							\
	}										\
	(LIST).RemoveAll();						\
}

#define ENTER_SECTION(SEC)					\
{											\
	(SEC).Lock();							\
}

#define LEAVE_SECTION(SEC)					\
{											\
	(SEC).Unlock();							\
}

#define BEGIN_PARSE_MAP(X) { POSITION p = (X).GetStartPosition(); while(p) {
#define GET_NEXT_ASSOC(K,V,X) K MAP_KEY; V MAP_VAL; (X).GetNextAssoc(p,MAP_KEY,MAP_VAL);
#define END_PARSE_MAP() } }
#define LOOKUP_MAP(k,V,M) V MAP_VAL; (M).Lookup(k,MAP_VAL);

#define BEGIN_PARSE_MAP2(X,S,T1,T2)		\
	BEGIN_PARSE_MAP(X)					\
		GET_NEXT_ASSOC(S,T1,(X));		\
		T1 map = MAP_VAL;				\
		BEGIN_PARSE_MAP(*map)			\
			GET_NEXT_ASSOC(S,T2,*map);

			
#define END_PARSE_MAP2()	\
		END_PARSE_MAP()		\
	END_PARSE_MAP()

#define SWITCH(X)  CString switch_val = X;
#define CASE(X)    if (switch_val == X)
#define CASE2(X,Y) if (switch_val == X || switch_val == Y)
#define CASE3(X,Y,Z) if (switch_val == X || switch_val == Y || switch_val == Z)
#define CASE4(X,Y,Z,A) if (switch_val == X || switch_val == Y || switch_val == Z || switch_val == A)
#define CASE5(X,Y,Z,A,B) if (switch_val == X || switch_val == Y || switch_val == Z || switch_val == A || switch_val == B)
#define CASE6(X,Y,Z,A,B,C) if (switch_val == X || switch_val == Y || switch_val == Z || switch_val == A || switch_val == B || switch_val == C)

#define GET_EDIT(IDC)  ((CEdit*)GetDlgItem(IDC))
#define GET_COMBO(IDC) ((CComboBox*)GetDlgItem(IDC))
#define GET_BUTTON(IDC) ((CButton*)GetDlgItem(IDC))
#define GET_SLIDER(IDC) ((CSliderCtrl*)GetDlgItem(IDC))
#define GET_PROGRESS(IDC) ((CProgressCtrl*)GetDlgItem(IDC))

#define ERR_MSG0(msg) AfxMessageBox(msg,MB_ICONSTOP|MB_OK);
#define ERR_MSG1(msg,param1)			\
{ 										\
	CString s;							\
	s.Format(msg,param1);				\
	AfxMessageBox(s,MB_ICONSTOP|MB_OK); \
}

#define ERR_MSG2(msg,param1,param2)		\
{ 										\
	CString s;							\
	s.Format(msg,param1,param2);		\
	AfxMessageBox(s,MB_ICONSTOP|MB_OK); \
}

#endif