Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.67.by mice / tube / src / win_errors

.h
Скачиваний:
9
Добавлен:
17.04.2013
Размер:
2.66 Кб
Скачать
#ifndef WIN_ERRORS_INCLUDED
#define WIN_ERRORS_INCLUDED

#include <afx.h>
#include <afxwin.h>
#include <cstringt.h>
#include <time.h>


union sfx_data {
	short s;
	unsigned short us;
	int i;
	unsigned int ui;
	void *p;

	sfx_data(): p(NULL) {}
	sfx_data(short sv) { s = sv; }
	sfx_data(unsigned short usv) { us = usv; }
	sfx_data(int iv) { i = iv; }
	sfx_data(unsigned int uiv) { ui = uiv; }
	sfx_data(void *pv) { p = pv; }

	operator short() const { return s; }
	operator unsigned short() const { return us; }
	operator int() const { return i; }
	operator unsigned int() const { return ui; }
	operator void *() const { return p; }
	template<class T> operator T *() const { return (T *)p; }
};



static int get_os_error_str(CString &str, const int ercode) {
	char *os_msg;
	int rev = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
				  FORMAT_MESSAGE_ALLOCATE_BUFFER|
				  FORMAT_MESSAGE_ARGUMENT_ARRAY,
				  NULL, ercode, 0, (LPSTR)&os_msg, 0, NULL);
	if (0 == rev) return -1;
	str.Format("%s", os_msg);
	LocalFree(os_msg);

	return 0;
}


static int show_error_msg(const char *const str, int ercode = 0, CWnd *wnd = NULL) {
	if (0 == ercode) ercode = GetLastError();
	SetLastError(0);
	CString t;
	if (0 != get_os_error_str(t, ercode)) t = "[unknown error or no error at all]";

	CString msg_str;
	msg_str.Format("Error: %s\n\nWith message: %s", str, t);

	HWND hwnd;
	CString er_cap;
	if (NULL != wnd) {
		hwnd = wnd->GetSafeHwnd();
		wnd->GetWindowText(t);
		er_cap.Format("Error [%s]", t);
	} else {
		hwnd = NULL;
		er_cap = "Claw error";
	}

	MessageBox(hwnd, msg_str, er_cap, MB_OK|MB_APPLMODAL|MB_ICONERROR);

	return 0;
}


static int cl_enable_window(const HWND hwnd, BOOL is_enable) {
	if (FALSE == is_enable) {
		PostMessage(hwnd, WM_CANCELMODE, 0, 0);
	}
	PostMessage(hwnd, WM_ENABLE, is_enable, 0);

	return 0;
}

static int edit_set_readonly(const HWND hwnd, BOOL is_readonly) {
	PostMessage(hwnd, EM_SETREADONLY, is_readonly, 0);

	return 0;
}


static int size_to_str(const unsigned int sz, CString &str) {
	if (sz < 1024) {
		str.Format("%u b", sz);
	} else if (sz < 1024*1024) {
		str.Format("%.3g kb", (double)sz/(1024));
	} else if (sz < 1024*1024*1024) {
		str.Format("%.3g mb", (double)sz/(1024*1024));
	} else {
		str.Format("%.3g gb", (double)sz/(1024*1024*1024));
	}

	return 0;
}


static void clock_to_str(const clock_t t, CString &str) {
	int s = (int)(t / CLOCKS_PER_SEC);
	int secs = s % 60; s /= 60;
	int mins = s % 60; s /= 60;
	int hors = s % 60;
	str.Format("%02i:%02i:%02i", hors, mins, secs);
}



#endif
Соседние файлы в папке src